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