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