GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
sql.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 * original modifications & added code by
8 * Radim Blazek <radim.blazek gmail.com>
9 * Glynn Clements <glynn gclements.plus.com>,
10 * Markus Neteler <neteler itc.it>,
11 * Martin Landa <landa.martin gmail.com>,
12 * Moritz Lennert <mlennert club.worldonline.be>,
13 * Hamish Bowman <hamish_b yahoo.com>,
14 * Daniel Calvelo Aros <dca.gis gmail.com>,
15 * Paul Kelly <paul-grass stjohnspoint.co.uk>,
16 * Alex Shevlakov <sixote yahoo.com>
17 *
18 * PURPOSE: Parse input string containing SQL statement to
19 * SQLPSTMT structure.
20 * SQL parser may be used by simple database drivers.
21 *
22 * SPDX-FileCopyrightText: 2000-2007 GRASS Development Team
23 * SPDX-License-Identifier: GPL-2.0-or-later
24 *
25 *****************************************************************************/
26
27#include <stdlib.h>
28#include <stdio.h>
29#include <string.h>
30#include <ctype.h>
31#include <assert.h>
32#include <grass/sqlp.h>
33
35
36/* save string to value */
37int sqpSaveStr(SQLPVALUE *val, char *c)
38{
39 int len = 0;
40
41 len = strlen(c) + 1;
42 val->s = (char *)realloc(val->s, len);
43
44 strcpy(val->s, c);
45
46 return (1);
47}
48
50{
51 val->type = SQLP_NULL;
52 val->s = NULL;
53 val->i = 0;
54 val->d = 0.0;
55}
56
58{
59 to->type = from->type;
60
61 if (to->s)
62 free(to->s);
63
64 if (from->s)
65 to->s = strdup(from->s);
66
67 to->i = from->i;
68 to->d = from->d;
69}
70
72{
73 sqlpStmt = st;
75
76 sqlpStmt->errmsg[0] = '\0';
77 sqlpStmt->table[0] = '\0';
78 sqlpStmt->nCol = 0;
79 sqlpStmt->nVal = 0;
82
83 return (1);
84}
85
86void sqpCommand(int command)
87{
88 sqlpStmt->command = command;
89 return;
90}
91
92void sqpTable(char *tbl)
93{
95 return;
96}
97
98void sqpColumn(char *col)
99{
100 int i;
101
102 i = sqlpStmt->nCol;
103 sqpAllocCol(sqlpStmt, i + 1);
104 sqpSaveStr(&(sqlpStmt->Col[i]), col);
105
106 sqlpStmt->nCol++;
107 return;
108}
109
110void sqpColumnDef(char *col, int type, int width, int decimals)
111{
112 int i;
113
114 i = sqlpStmt->nCol;
115 sqpAllocCol(sqlpStmt, i + 1);
116 sqpSaveStr(&(sqlpStmt->Col[i]), col);
117 sqlpStmt->ColType[i] = type;
118 sqlpStmt->ColWidth[i] = width;
120
121 sqlpStmt->nCol++;
122 return;
123}
124
125void sqpValue(char *strval, int intval, double dblval, int type)
126{
127 int i;
128
129 i = sqlpStmt->nVal;
130
131 /* allocate space for cols because if in INSERT cols were not
132 * specified array for ColNum would not be allocated */
133 sqpAllocCol(sqlpStmt, i + 1);
134
135 sqpAllocVal(sqlpStmt, i + 1);
136 sqlpStmt->Val[i].s = NULL;
137 sqlpStmt->Val[i].i = 0; /* not necessary I think */
138 sqlpStmt->Val[i].d = 0.0; /* not necessary I think */
139
140 sqlpStmt->Val[i].type = type;
141 switch (type) {
142 case (SQLP_S):
143 sqpSaveStr(&(sqlpStmt->Val[i]), strval);
144 break;
145 case (SQLP_I):
146 sqlpStmt->Val[i].i = intval;
147 break;
148 case (SQLP_D):
149 sqlpStmt->Val[i].d = dblval;
150 break;
151 /* SQLP_NULL, nothing to do */
152 }
153
154 sqlpStmt->nVal++;
155 return;
156}
157
158void sqpAssignment(char *col, char *strval, int intval, double dblval,
159 SQLPNODE *expval, int type)
160{
161 int i;
162
163 i = sqlpStmt->nCol;
164
165 sqpAllocCol(sqlpStmt, i + 1);
166 sqpSaveStr(&(sqlpStmt->Col[i]), col);
167
168 sqpAllocVal(sqlpStmt, i + 1);
169 sqlpStmt->Val[i].s = NULL;
170 sqlpStmt->Val[i].i = 0; /* not necessary I think */
171 sqlpStmt->Val[i].d = 0.0; /* not necessary I think */
172
173 sqlpStmt->Val[i].type = type;
174 switch (type) {
175 case (SQLP_S):
176 sqpSaveStr(&(sqlpStmt->Val[i]), strval);
177 break;
178 case (SQLP_I):
179 sqlpStmt->Val[i].i = intval;
180 break;
181 case (SQLP_D):
182 sqlpStmt->Val[i].d = dblval;
183 break;
184 case (SQLP_EXPR):
185 sqlpStmt->Val[i].expr = expval;
186 /* Don't do anything right now; come back to this when executing */
187 break;
188 /* SQLP_NULL, nothing to do */
189 }
190
191 sqlpStmt->nCol++;
192 sqlpStmt->nVal++;
193 return;
194}
195
196void sqpOrderColumn(char *col, int dir)
197{
200 sqlpStmt->orderDir = dir;
201 return;
202}
203
204/* Create and init new node */
206{
207 SQLPNODE *np;
208
209 np = (SQLPNODE *)calloc(1, sizeof(SQLPNODE));
210 return np;
211}
212
214{
215 SQLPNODE *np;
216
217 np = sqpNewNode();
218
220 np->oper = oper;
221 np->left = left;
222 np->right = right;
223
224 return np;
225}
226
228{
229 SQLPNODE *np;
230
231 np = sqpNewNode();
232
234 np->column_name = strdup(name);
235
236 return np;
237}
238
239SQLPNODE *sqpNewValueNode(char *strval, int intval, double dblval, int type)
240{
241 SQLPNODE *np;
242
243 np = sqpNewNode();
244
246
247 np->value.type = type;
248 if (strval)
249 np->value.s = strdup(strval);
250 np->value.i = intval;
251 np->value.d = dblval;
252
253 return np;
254}
255
257{
258 if (!np)
259 return;
260
261 if (np->left)
262 sqpFreeNode(np->left);
263
264 if (np->right)
265 sqpFreeNode(np->right);
266
267 if (np->column_name)
268 free(np->column_name);
269
270 if (np->value.s)
271 free(np->value.s);
272
273 free(np);
274}
275
276int sqpOperatorCode(char *oper)
277{
278 char *tmp, *ptr;
279
280 /* Convert to lower case */
281 tmp = strdup(oper);
282 ptr = tmp;
283 while (*ptr) {
284 *ptr = tolower(*ptr);
285 ptr++;
286 }
287
288 if (strcmp(oper, "=") == 0)
289 return SQLP_EQ;
290 else if (strcmp(oper, "<") == 0)
291 return SQLP_LT;
292 else if (strcmp(oper, "<=") == 0)
293 return SQLP_LE;
294 else if (strcmp(oper, ">") == 0)
295 return SQLP_GT;
296 else if (strcmp(oper, ">=") == 0)
297 return SQLP_GE;
298 else if (strcmp(oper, "<>") == 0)
299 return SQLP_NE;
300 else if (strcmp(oper, "~") == 0)
301 return SQLP_MTCH;
302 else if (strcmp(oper, "+") == 0)
303 return SQLP_ADD;
304 else if (strcmp(oper, "-") == 0)
305 return SQLP_SUBTR;
306 else if (strcmp(oper, "*") == 0)
307 return SQLP_MLTP;
308 else if (strcmp(oper, "/") == 0)
309 return SQLP_DIV;
310 else if (strcmp(oper, "and") == 0)
311 return SQLP_AND;
312 else if (strcmp(oper, "or") == 0)
313 return SQLP_OR;
314 else if (strcmp(oper, "not") == 0)
315 return SQLP_NOT;
316
317 free(tmp);
318
319 return 0;
320}
321
322char *sqpOperatorName(int oper)
323{
324 switch (oper) {
325 case SQLP_EQ:
326 return "=";
327 break;
328 case SQLP_LT:
329 return "<";
330 break;
331 case SQLP_LE:
332 return "<=";
333 break;
334 case SQLP_GT:
335 return ">";
336 break;
337 case SQLP_GE:
338 return ">=";
339 break;
340 case SQLP_NE:
341 return "<>";
342 break;
343 case SQLP_MTCH:
344 return "~";
345 break;
346 case SQLP_ADD:
347 return "+";
348 break;
349 case SQLP_SUBTR:
350 return "-";
351 break;
352 case SQLP_MLTP:
353 return "*";
354 break;
355 case SQLP_DIV:
356 return "/";
357 break;
358 case SQLP_AND:
359 return "AND";
360 break;
361 case SQLP_OR:
362 return "OR";
363 break;
364 case SQLP_NOT:
365 return "NOT";
366 break;
367 }
368 return "?";
369}
#define NULL
Definition ccmath.h:32
int sqpAllocCol(SQLPSTMT *st, int n)
int sqpAllocVal(SQLPSTMT *st, int n)
const char * name
Definition named_colr.c:6
struct state * st
Definition parser.c:102
#define strcpy
Definition parson.c:66
char * sqpOperatorName(int oper)
Definition sql.c:322
int sqpSaveStr(SQLPVALUE *val, char *c)
Definition sql.c:37
void sqpColumn(char *col)
Definition sql.c:98
void sqpCopyValue(SQLPVALUE *from, SQLPVALUE *to)
Definition sql.c:57
void sqpValue(char *strval, int intval, double dblval, int type)
Definition sql.c:125
SQLPNODE * sqpNewNode(void)
Definition sql.c:205
int sqpOperatorCode(char *oper)
Definition sql.c:276
void sqpAssignment(char *col, char *strval, int intval, double dblval, SQLPNODE *expval, int type)
Definition sql.c:158
SQLPNODE * sqpNewValueNode(char *strval, int intval, double dblval, int type)
Definition sql.c:239
SQLPNODE * sqpNewColumnNode(char *name)
Definition sql.c:227
SQLPNODE * sqpNewExpressionNode(int oper, SQLPNODE *left, SQLPNODE *right)
Definition sql.c:213
void sqpTable(char *tbl)
Definition sql.c:92
void sqpCommand(int command)
Definition sql.c:86
int sqpInitParser(SQLPSTMT *st)
Definition sql.c:71
void sqpInitValue(SQLPVALUE *val)
Definition sql.c:49
void sqpOrderColumn(char *col, int dir)
Definition sql.c:196
void sqpFreeNode(SQLPNODE *np)
Definition sql.c:256
SQLPSTMT * sqlpStmt
Definition sql.c:34
void sqpColumnDef(char *col, int type, int width, int decimals)
Definition sql.c:110
#define SQLP_MTCH
Definition sqlp.h:32
#define SQLP_LE
Definition sqlp.h:28
#define SQLP_S
Definition sqlp.h:44
#define SQLP_D
Definition sqlp.h:46
#define SQLP_GT
Definition sqlp.h:29
#define SQLP_ADD
Definition sqlp.h:20
#define SQLP_NODE_VALUE
Definition sqlp.h:62
#define SQLP_SUBTR
Definition sqlp.h:21
#define SQLP_NODE_EXPRESSION
Definition sqlp.h:63
#define SQLP_EXPR
Definition sqlp.h:48
#define SQLP_NE
Definition sqlp.h:31
#define SQLP_GE
Definition sqlp.h:30
#define SQLP_NODE_COLUMN
Definition sqlp.h:61
#define SQLP_EQ
Definition sqlp.h:26
#define SQLP_AND
Definition sqlp.h:38
#define SQLP_MAX_TABLE
Definition sqlp.h:57
#define SQLP_NOT
Definition sqlp.h:40
#define SQLP_MLTP
Definition sqlp.h:22
#define SQLP_LT
Definition sqlp.h:27
#define SQLP_DIV
Definition sqlp.h:23
#define SQLP_I
Definition sqlp.h:45
#define SQLP_OR
Definition sqlp.h:39
#define SQLP_NULL
Definition sqlp.h:43
void free(void *)
int nCol
Definition sqlp.h:98
char * stmt
Definition sqlp.h:88
char errmsg[500+1]
Definition sqlp.h:90
SQLPVALUE * Col
Definition sqlp.h:93
SQLPNODE * upperNodeptr
Definition sqlp.h:102
char * orderCol
Definition sqlp.h:103
int * ColWidth
Definition sqlp.h:95
int orderDir
Definition sqlp.h:105
int command
Definition sqlp.h:91
char table[200+1]
Definition sqlp.h:92
int nVal
Definition sqlp.h:101
int * ColType
Definition sqlp.h:94
char * cur
Definition sqlp.h:89
SQLPVALUE * Val
Definition sqlp.h:99
int * ColDecim
Definition sqlp.h:96
double d
Definition sqlp.h:73
char * s
Definition sqlp.h:71
struct sqlpnode * expr
Definition sqlp.h:74
int i
Definition sqlp.h:72
int type
Definition sqlp.h:70
struct sqlpnode * left
Definition sqlp.h:81
char * column_name
Definition sqlp.h:83
struct sqlpnode * right
Definition sqlp.h:82
int oper
Definition sqlp.h:80
SQLPVALUE value
Definition sqlp.h:84
int node_type
Definition sqlp.h:78