GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-66aae0a5a0
db/sqlp/alloc.c
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * MODULE: SQL statement parser library
4  *
5  * AUTHOR(S): lex.l and yac.y were originally taken from unixODBC and
6  * probably written by Peter Harvey <pharvey@codebydesigns.com>,
7  * modifications and other code by Radim Blazek
8  *
9  * PURPOSE: Parse input string containing SQL statement to
10  * SQLPSTMT structure.
11  * SQL parser may be used by simple database drivers.
12  *
13  * COPYRIGHT: (C) 2000 by the GRASS Development Team
14  *
15  * This program is free software under the GNU General Public
16  * License (>=v2). Read the file COPYING that comes with
17  * GRASS for details.
18  *
19  *****************************************************************************/
20 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <grass/sqlp.h>
24 
25 /* alloc structure */
27 {
28  SQLPSTMT *st;
29 
30  st = (SQLPSTMT *)calloc(1, sizeof(SQLPSTMT));
31 
32  return (st);
33 }
34 
35 /* allocate space for columns */
36 int sqpAllocCol(SQLPSTMT *st, int n)
37 {
38  int i;
39 
40  if (n > st->aCol) {
41  n += 15;
42  st->Col = (SQLPVALUE *)realloc(st->Col, n * sizeof(SQLPVALUE));
43  st->ColType = (int *)realloc(st->ColType, n * sizeof(int));
44  st->ColWidth = (int *)realloc(st->ColWidth, n * sizeof(int));
45  st->ColDecim = (int *)realloc(st->ColDecim, n * sizeof(int));
46 
47  for (i = st->nCol; i < n; i++) {
48  st->Col[i].s = NULL;
49  }
50 
51  st->aCol = n;
52  }
53  return (1);
54 }
55 
56 /* allocate space for values */
57 int sqpAllocVal(SQLPSTMT *st, int n)
58 {
59  int i;
60 
61  if (n > st->aVal) {
62  n += 15;
63  st->Val = (SQLPVALUE *)realloc(st->Val, n * sizeof(SQLPVALUE));
64 
65  for (i = st->nVal; i < n; i++) {
66  st->Val[i].s = NULL;
67  }
68 
69  st->aVal = n;
70  }
71  return (1);
72 }
73 
74 /* free space allocated by parser */
76 {
77  int i;
78 
79  /* columns */
80  for (i = 0; i < st->aCol; i++)
81  free(st->Col[i].s);
82 
83  free(st->Col);
84  free(st->ColType);
85  free(st->ColWidth);
86  free(st->ColDecim);
87  st->aCol = 0;
88  st->nCol = 0;
89 
90  /* values */
91  for (i = 0; i < st->aVal; i++)
92  free(st->Val[i].s);
93 
94  free(st->Val);
95  st->aVal = 0;
96  st->nVal = 0;
97 
98  free(st->orderCol);
99 
100  /* Nodes (where) */
101  if (st->upperNodeptr)
102  sqpFreeNode(st->upperNodeptr);
103 
104  free(st);
105  return (1);
106 }
#define NULL
Definition: ccmath.h:32
int sqpFreeStmt(SQLPSTMT *st)
Definition: db/sqlp/alloc.c:75
int sqpAllocCol(SQLPSTMT *st, int n)
Definition: db/sqlp/alloc.c:36
int sqpAllocVal(SQLPSTMT *st, int n)
Definition: db/sqlp/alloc.c:57
SQLPSTMT * sqpInitStmt(void)
Definition: db/sqlp/alloc.c:26
void sqpFreeNode(SQLPNODE *)
Definition: sql.c:259
struct state * st
Definition: parser.c:104
void free(void *)
Definition: sqlp.h:87
Definition: sqlp.h:69