Line data Source code
1 : /*
2 : * SPDX-License-Identifier: MPL-2.0
3 : *
4 : * This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 : *
8 : * Copyright 2024 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : #include "monetdb_config.h"
14 : #include <wctype.h>
15 : #include "sql_mem.h"
16 : #include "sql_scan.h"
17 : #include "sql_types.h"
18 : #include "sql_symbol.h"
19 : #include "sql_mvc.h"
20 : #include "sql_parser.tab.h"
21 : #include "sql_semantic.h"
22 : #include "sql_parser.h" /* for sql_error() */
23 :
24 : #include "stream.h"
25 : #include "mapi_prompt.h"
26 : #include <unistd.h>
27 : #include <string.h>
28 : #include <ctype.h>
29 : #include "sql_keyword.h"
30 :
31 : /**
32 : * Removes all comments before the query. In query comments are kept.
33 : */
34 : char *
35 400407 : query_cleaned(allocator *sa, const char *query)
36 : {
37 400407 : char *q, *r, *c = NULL;
38 400407 : int lines = 0;
39 400407 : int quote = 0; /* inside quotes ('..', "..", {..}) */
40 400407 : bool bs = false; /* seen a backslash in a quoted string */
41 400407 : bool incomment1 = false; /* inside traditional C style comment */
42 400407 : bool incomment2 = false; /* inside comment starting with -- */
43 400407 : bool inline_comment = false;
44 :
45 400407 : r = SA_NEW_ARRAY(sa, char, strlen(query) + 1);
46 400615 : if(!r)
47 : return NULL;
48 :
49 67752424 : for (q = r; *query; query++) {
50 67351809 : if (incomment1) {
51 16116 : if (*query == '/' && query[-1] == '*') {
52 233 : incomment1 = false;
53 233 : if (c == r && lines > 0) {
54 225 : q = r; // reset to beginning
55 225 : lines = 0;
56 225 : continue;
57 : }
58 : }
59 15891 : if (*query == '\n') lines++;
60 15891 : *q++ = *query;
61 67335693 : } else if (incomment2) {
62 827673 : if (*query == '\n') {
63 2832 : incomment2 = false;
64 2832 : inline_comment = false;
65 : /* add newline only if comment doesn't
66 : * occupy whole line */
67 2832 : if (q > r && q[-1] != '\n'){
68 939 : *q++ = '\n';
69 939 : lines++;
70 : }
71 824841 : } else if (inline_comment){
72 20650 : *q++ = *query; // preserve in line query comments
73 : }
74 66508020 : } else if (quote) {
75 22257472 : if (bs) {
76 : bs = false;
77 22254175 : } else if (*query == '\\') {
78 : bs = true;
79 22250878 : } else if (*query == quote) {
80 673932 : quote = 0;
81 : }
82 22257472 : *q++ = *query;
83 44250548 : } else if (*query == '"' || *query == '\'') {
84 673469 : quote = *query;
85 673469 : *q++ = *query;
86 43577079 : } else if (*query == '{') {
87 508 : quote = '}';
88 508 : *q++ = *query;
89 43576571 : } else if (*query == '-' && query[1] == '-') {
90 2832 : if (q > r && q[-1] != '\n') {
91 939 : inline_comment = true;
92 939 : *q++ = *query; // preserve in line query comments
93 : }
94 : incomment2 = true;
95 43573739 : } else if (*query == '/' && query[1] == '*') {
96 233 : incomment1 = true;
97 233 : c = q;
98 233 : *q++ = *query;
99 43573506 : } else if (*query == '\n') {
100 : /* collapse newlines */
101 862596 : if (q > r && q[-1] != '\n') {
102 820761 : *q++ = '\n';
103 820761 : lines++;
104 : }
105 42710910 : } else if (*query == ' ' || *query == '\t') {
106 : /* collapse white space */
107 7044616 : if (q > r && q[-1] != ' ')
108 5560388 : *q++ = ' ';
109 : } else {
110 35666294 : *q++ = *query;
111 : }
112 : }
113 400615 : *q = 0;
114 400615 : return r;
115 : }
116 :
117 : int
118 328 : scanner_init_keywords(void)
119 : {
120 328 : int failed = 0;
121 :
122 328 : failed += keywords_insert("false", BOOL_FALSE);
123 328 : failed += keywords_insert("true", BOOL_TRUE);
124 328 : failed += keywords_insert("bool", sqlBOOL);
125 :
126 328 : failed += keywords_insert("ALTER", ALTER);
127 328 : failed += keywords_insert("ADD", ADD);
128 328 : failed += keywords_insert("AND", AND);
129 :
130 328 : failed += keywords_insert("RANK", RANK);
131 328 : failed += keywords_insert("DENSE_RANK", RANK);
132 328 : failed += keywords_insert("PERCENT_RANK", RANK);
133 328 : failed += keywords_insert("CUME_DIST", RANK);
134 328 : failed += keywords_insert("ROW_NUMBER", RANK);
135 328 : failed += keywords_insert("NTILE", RANK);
136 328 : failed += keywords_insert("LAG", RANK);
137 328 : failed += keywords_insert("LEAD", RANK);
138 328 : failed += keywords_insert("FETCH", FETCH);
139 328 : failed += keywords_insert("FIRST_VALUE", RANK);
140 328 : failed += keywords_insert("LAST_VALUE", RANK);
141 328 : failed += keywords_insert("NTH_VALUE", RANK);
142 :
143 328 : failed += keywords_insert("BEST", BEST);
144 328 : failed += keywords_insert("EFFORT", EFFORT);
145 :
146 328 : failed += keywords_insert("AS", AS);
147 328 : failed += keywords_insert("ASC", ASC);
148 328 : failed += keywords_insert("AUTHORIZATION", AUTHORIZATION);
149 328 : failed += keywords_insert("BETWEEN", BETWEEN);
150 328 : failed += keywords_insert("SYMMETRIC", SYMMETRIC);
151 328 : failed += keywords_insert("ASYMMETRIC", ASYMMETRIC);
152 328 : failed += keywords_insert("BY", BY);
153 328 : failed += keywords_insert("CAST", CAST);
154 328 : failed += keywords_insert("CONVERT", CONVERT);
155 328 : failed += keywords_insert("CHARACTER", CHARACTER);
156 328 : failed += keywords_insert("CHAR", CHARACTER);
157 328 : failed += keywords_insert("VARYING", VARYING);
158 328 : failed += keywords_insert("VARCHAR", VARCHAR);
159 328 : failed += keywords_insert("BINARY", BINARY);
160 328 : failed += keywords_insert("LARGE", LARGE);
161 328 : failed += keywords_insert("OBJECT", OBJECT);
162 328 : failed += keywords_insert("CLOB", CLOB);
163 328 : failed += keywords_insert("BLOB", sqlBLOB);
164 328 : failed += keywords_insert("TEXT", sqlTEXT);
165 328 : failed += keywords_insert("TINYTEXT", sqlTEXT);
166 328 : failed += keywords_insert("STRING", CLOB); /* ? */
167 328 : failed += keywords_insert("CHECK", CHECK);
168 328 : failed += keywords_insert("CLIENT", CLIENT);
169 328 : failed += keywords_insert("SERVER", SERVER);
170 328 : failed += keywords_insert("COMMENT", COMMENT);
171 328 : failed += keywords_insert("CONSTRAINT", CONSTRAINT);
172 328 : failed += keywords_insert("CREATE", CREATE);
173 328 : failed += keywords_insert("CROSS", CROSS);
174 328 : failed += keywords_insert("COPY", COPY);
175 328 : failed += keywords_insert("RECORDS", RECORDS);
176 328 : failed += keywords_insert("DELIMITERS", DELIMITERS);
177 328 : failed += keywords_insert("STDIN", STDIN);
178 328 : failed += keywords_insert("STDOUT", STDOUT);
179 :
180 328 : failed += keywords_insert("TINYINT", TINYINT);
181 328 : failed += keywords_insert("SMALLINT", SMALLINT);
182 328 : failed += keywords_insert("INTEGER", sqlINTEGER);
183 328 : failed += keywords_insert("INT", sqlINTEGER);
184 328 : failed += keywords_insert("MEDIUMINT", sqlINTEGER);
185 328 : failed += keywords_insert("BIGINT", BIGINT);
186 : #ifdef HAVE_HGE
187 328 : failed += keywords_insert("HUGEINT", HUGEINT);
188 : #endif
189 328 : failed += keywords_insert("DEC", sqlDECIMAL);
190 328 : failed += keywords_insert("DECIMAL", sqlDECIMAL);
191 328 : failed += keywords_insert("NUMERIC", sqlDECIMAL);
192 328 : failed += keywords_insert("DECLARE", DECLARE);
193 328 : failed += keywords_insert("DEFAULT", DEFAULT);
194 328 : failed += keywords_insert("DESC", DESC);
195 328 : failed += keywords_insert("DISTINCT", DISTINCT);
196 328 : failed += keywords_insert("DOUBLE", sqlDOUBLE);
197 328 : failed += keywords_insert("REAL", sqlREAL);
198 328 : failed += keywords_insert("DROP", DROP);
199 328 : failed += keywords_insert("ESCAPE", ESCAPE);
200 328 : failed += keywords_insert("EXISTS", EXISTS);
201 328 : failed += keywords_insert("UESCAPE", UESCAPE);
202 328 : failed += keywords_insert("EXTRACT", EXTRACT);
203 328 : failed += keywords_insert("FLOAT", sqlFLOAT);
204 328 : failed += keywords_insert("FOR", FOR);
205 328 : failed += keywords_insert("FOREIGN", FOREIGN);
206 328 : failed += keywords_insert("FROM", FROM);
207 328 : failed += keywords_insert("FWF", FWF);
208 :
209 328 : failed += keywords_insert("BIG", BIG);
210 328 : failed += keywords_insert("LITTLE", LITTLE);
211 328 : failed += keywords_insert("NATIVE", NATIVE);
212 328 : failed += keywords_insert("ENDIAN", ENDIAN);
213 :
214 328 : failed += keywords_insert("REFERENCES", REFERENCES);
215 :
216 328 : failed += keywords_insert("MATCH", MATCH);
217 328 : failed += keywords_insert("FULL", FULL);
218 328 : failed += keywords_insert("PARTIAL", PARTIAL);
219 328 : failed += keywords_insert("SIMPLE", SIMPLE);
220 :
221 328 : failed += keywords_insert("INSERT", INSERT);
222 328 : failed += keywords_insert("UPDATE", UPDATE);
223 328 : failed += keywords_insert("DELETE", sqlDELETE);
224 328 : failed += keywords_insert("TRUNCATE", TRUNCATE);
225 328 : failed += keywords_insert("MATCHED", MATCHED);
226 :
227 328 : failed += keywords_insert("ACTION", ACTION);
228 328 : failed += keywords_insert("CASCADE", CASCADE);
229 328 : failed += keywords_insert("RESTRICT", RESTRICT);
230 328 : failed += keywords_insert("FIRST", FIRST);
231 328 : failed += keywords_insert("GLOBAL", GLOBAL);
232 328 : failed += keywords_insert("GROUP", sqlGROUP);
233 328 : failed += keywords_insert("GROUPING", GROUPING);
234 328 : failed += keywords_insert("ROLLUP", ROLLUP);
235 328 : failed += keywords_insert("CUBE", CUBE);
236 328 : failed += keywords_insert("HAVING", HAVING);
237 328 : failed += keywords_insert("ILIKE", ILIKE);
238 328 : failed += keywords_insert("IMPRINTS", IMPRINTS);
239 328 : failed += keywords_insert("IN", sqlIN);
240 328 : failed += keywords_insert("INNER", INNER);
241 328 : failed += keywords_insert("INTO", INTO);
242 328 : failed += keywords_insert("IS", IS);
243 328 : failed += keywords_insert("JOIN", JOIN);
244 328 : failed += keywords_insert("KEY", KEY);
245 328 : failed += keywords_insert("LATERAL", LATERAL);
246 328 : failed += keywords_insert("LEFT", LEFT);
247 328 : failed += keywords_insert("LIKE", LIKE);
248 328 : failed += keywords_insert("LIMIT", LIMIT);
249 328 : failed += keywords_insert("SAMPLE", SAMPLE);
250 328 : failed += keywords_insert("SEED", SEED);
251 328 : failed += keywords_insert("LAST", LAST);
252 328 : failed += keywords_insert("LOCAL", LOCAL);
253 328 : failed += keywords_insert("NATURAL", NATURAL);
254 328 : failed += keywords_insert("NOT", NOT);
255 328 : failed += keywords_insert("NULL", sqlNULL);
256 328 : failed += keywords_insert("NULLS", NULLS);
257 328 : failed += keywords_insert("OFFSET", OFFSET);
258 328 : failed += keywords_insert("ON", ON);
259 328 : failed += keywords_insert("OPTIONS", OPTIONS);
260 328 : failed += keywords_insert("OPTION", OPTION);
261 328 : failed += keywords_insert("OR", OR);
262 328 : failed += keywords_insert("ORDER", ORDER);
263 328 : failed += keywords_insert("ORDERED", ORDERED);
264 328 : failed += keywords_insert("OUTER", OUTER);
265 328 : failed += keywords_insert("OVER", OVER);
266 328 : failed += keywords_insert("PARTITION", PARTITION);
267 328 : failed += keywords_insert("PATH", PATH);
268 328 : failed += keywords_insert("PRECISION", PRECISION);
269 328 : failed += keywords_insert("PRIMARY", PRIMARY);
270 :
271 328 : failed += keywords_insert("USER", USER);
272 328 : failed += keywords_insert("RENAME", RENAME);
273 328 : failed += keywords_insert("UNENCRYPTED", UNENCRYPTED);
274 328 : failed += keywords_insert("ENCRYPTED", ENCRYPTED);
275 328 : failed += keywords_insert("PASSWORD", PASSWORD);
276 328 : failed += keywords_insert("GRANT", GRANT);
277 328 : failed += keywords_insert("REVOKE", REVOKE);
278 328 : failed += keywords_insert("ROLE", ROLE);
279 328 : failed += keywords_insert("ADMIN", ADMIN);
280 328 : failed += keywords_insert("PRIVILEGES", PRIVILEGES);
281 328 : failed += keywords_insert("PUBLIC", PUBLIC);
282 328 : failed += keywords_insert("CURRENT_USER", CURRENT_USER);
283 328 : failed += keywords_insert("CURRENT_ROLE", CURRENT_ROLE);
284 328 : failed += keywords_insert("SESSION_USER", SESSION_USER);
285 328 : failed += keywords_insert("CURRENT_SCHEMA", CURRENT_SCHEMA);
286 328 : failed += keywords_insert("SESSION", sqlSESSION);
287 328 : failed += keywords_insert("MAX_MEMORY", MAX_MEMORY);
288 328 : failed += keywords_insert("MAX_WORKERS", MAX_WORKERS);
289 328 : failed += keywords_insert("OPTIMIZER", OPTIMIZER);
290 :
291 328 : failed += keywords_insert("RIGHT", RIGHT);
292 328 : failed += keywords_insert("SCHEMA", SCHEMA);
293 328 : failed += keywords_insert("SELECT", SELECT);
294 328 : failed += keywords_insert("SET", SET);
295 328 : failed += keywords_insert("SETS", SETS);
296 328 : failed += keywords_insert("AUTO_COMMIT", AUTO_COMMIT);
297 :
298 328 : failed += keywords_insert("ALL", ALL);
299 328 : failed += keywords_insert("ANY", ANY);
300 328 : failed += keywords_insert("SOME", SOME);
301 328 : failed += keywords_insert("EVERY", ANY);
302 : /*
303 : failed += keywords_insert("SQLCODE", SQLCODE );
304 : */
305 328 : failed += keywords_insert("COLUMN", COLUMN);
306 328 : failed += keywords_insert("TABLE", TABLE);
307 328 : failed += keywords_insert("TEMPORARY", TEMPORARY);
308 328 : failed += keywords_insert("TEMP", TEMP);
309 328 : failed += keywords_insert("REMOTE", REMOTE);
310 328 : failed += keywords_insert("MERGE", MERGE);
311 328 : failed += keywords_insert("REPLICA", REPLICA);
312 328 : failed += keywords_insert("UNLOGGED", UNLOGGED);
313 328 : failed += keywords_insert("TO", TO);
314 328 : failed += keywords_insert("UNION", UNION);
315 328 : failed += keywords_insert("EXCEPT", EXCEPT);
316 328 : failed += keywords_insert("INTERSECT", INTERSECT);
317 328 : failed += keywords_insert("CORRESPONDING", CORRESPONDING);
318 328 : failed += keywords_insert("UNIQUE", UNIQUE);
319 328 : failed += keywords_insert("USING", USING);
320 328 : failed += keywords_insert("VALUES", VALUES);
321 328 : failed += keywords_insert("VIEW", VIEW);
322 328 : failed += keywords_insert("WHERE", WHERE);
323 328 : failed += keywords_insert("WITH", WITH);
324 328 : failed += keywords_insert("WITHOUT", WITHOUT);
325 328 : failed += keywords_insert("DATA", DATA);
326 :
327 328 : failed += keywords_insert("DATE", sqlDATE);
328 328 : failed += keywords_insert("TIME", TIME);
329 328 : failed += keywords_insert("TIMESTAMP", TIMESTAMP);
330 328 : failed += keywords_insert("INTERVAL", INTERVAL);
331 328 : failed += keywords_insert("CURRENT_DATE", CURRENT_DATE);
332 328 : failed += keywords_insert("CURRENT_TIME", CURRENT_TIME);
333 328 : failed += keywords_insert("CURRENT_TIMESTAMP", CURRENT_TIMESTAMP);
334 328 : failed += keywords_insert("CURRENT_TIMEZONE", CURRENT_TIMEZONE);
335 328 : failed += keywords_insert("NOW", CURRENT_TIMESTAMP);
336 328 : failed += keywords_insert("LOCALTIME", LOCALTIME);
337 328 : failed += keywords_insert("LOCALTIMESTAMP", LOCALTIMESTAMP);
338 328 : failed += keywords_insert("ZONE", ZONE);
339 :
340 328 : failed += keywords_insert("CENTURY", CENTURY);
341 328 : failed += keywords_insert("DECADE", DECADE);
342 328 : failed += keywords_insert("YEAR", YEAR);
343 328 : failed += keywords_insert("QUARTER", QUARTER);
344 328 : failed += keywords_insert("MONTH", MONTH);
345 328 : failed += keywords_insert("WEEK", WEEK);
346 328 : failed += keywords_insert("DOW", DOW);
347 328 : failed += keywords_insert("DOY", DOY);
348 328 : failed += keywords_insert("DAY", DAY);
349 328 : failed += keywords_insert("HOUR", HOUR);
350 328 : failed += keywords_insert("MINUTE", MINUTE);
351 328 : failed += keywords_insert("SECOND", SECOND);
352 328 : failed += keywords_insert("EPOCH", EPOCH);
353 :
354 328 : failed += keywords_insert("POSITION", POSITION);
355 328 : failed += keywords_insert("SUBSTRING", SUBSTRING);
356 328 : failed += keywords_insert("SPLIT_PART", SPLIT_PART);
357 328 : failed += keywords_insert("TRIM", TRIM);
358 328 : failed += keywords_insert("LEADING", LEADING);
359 328 : failed += keywords_insert("TRAILING", TRAILING);
360 328 : failed += keywords_insert("BOTH", BOTH);
361 :
362 328 : failed += keywords_insert("CASE", CASE);
363 328 : failed += keywords_insert("WHEN", WHEN);
364 328 : failed += keywords_insert("THEN", THEN);
365 328 : failed += keywords_insert("ELSE", ELSE);
366 328 : failed += keywords_insert("END", END);
367 328 : failed += keywords_insert("NULLIF", NULLIF);
368 328 : failed += keywords_insert("COALESCE", COALESCE);
369 328 : failed += keywords_insert("ELSEIF", ELSEIF);
370 328 : failed += keywords_insert("IF", IF);
371 328 : failed += keywords_insert("WHILE", WHILE);
372 328 : failed += keywords_insert("DO", DO);
373 :
374 328 : failed += keywords_insert("COMMIT", COMMIT);
375 328 : failed += keywords_insert("ROLLBACK", ROLLBACK);
376 328 : failed += keywords_insert("SAVEPOINT", SAVEPOINT);
377 328 : failed += keywords_insert("RELEASE", RELEASE);
378 328 : failed += keywords_insert("WORK", WORK);
379 328 : failed += keywords_insert("CHAIN", CHAIN);
380 328 : failed += keywords_insert("PRESERVE", PRESERVE);
381 328 : failed += keywords_insert("ROWS", ROWS);
382 328 : failed += keywords_insert("NO", NO);
383 328 : failed += keywords_insert("START", START);
384 328 : failed += keywords_insert("TRANSACTION", TRANSACTION);
385 328 : failed += keywords_insert("READ", READ);
386 328 : failed += keywords_insert("WRITE", WRITE);
387 328 : failed += keywords_insert("ONLY", ONLY);
388 328 : failed += keywords_insert("ISOLATION", ISOLATION);
389 328 : failed += keywords_insert("LEVEL", LEVEL);
390 328 : failed += keywords_insert("UNCOMMITTED", UNCOMMITTED);
391 328 : failed += keywords_insert("COMMITTED", COMMITTED);
392 328 : failed += keywords_insert("REPEATABLE", sqlREPEATABLE);
393 328 : failed += keywords_insert("SNAPSHOT", SNAPSHOT);
394 328 : failed += keywords_insert("SERIALIZABLE", SERIALIZABLE);
395 328 : failed += keywords_insert("DIAGNOSTICS", DIAGNOSTICS);
396 328 : failed += keywords_insert("SIZE", sqlSIZE);
397 328 : failed += keywords_insert("STORAGE", STORAGE);
398 :
399 328 : failed += keywords_insert("TYPE", TYPE);
400 328 : failed += keywords_insert("PROCEDURE", PROCEDURE);
401 328 : failed += keywords_insert("FUNCTION", FUNCTION);
402 328 : failed += keywords_insert("LOADER", sqlLOADER);
403 328 : failed += keywords_insert("REPLACE", REPLACE);
404 :
405 328 : failed += keywords_insert("FIELD", FIELD);
406 328 : failed += keywords_insert("FILTER", FILTER);
407 328 : failed += keywords_insert("AGGREGATE", AGGREGATE);
408 328 : failed += keywords_insert("RETURNS", RETURNS);
409 328 : failed += keywords_insert("EXTERNAL", EXTERNAL);
410 328 : failed += keywords_insert("NAME", sqlNAME);
411 328 : failed += keywords_insert("RETURN", RETURN);
412 328 : failed += keywords_insert("CALL", CALL);
413 328 : failed += keywords_insert("LANGUAGE", LANGUAGE);
414 :
415 328 : failed += keywords_insert("ANALYZE", ANALYZE);
416 328 : failed += keywords_insert("EXPLAIN", SQL_EXPLAIN);
417 328 : failed += keywords_insert("PLAN", SQL_PLAN);
418 328 : failed += keywords_insert("TRACE", SQL_TRACE);
419 328 : failed += keywords_insert("PREPARE", PREPARE);
420 328 : failed += keywords_insert("PREP", PREP);
421 328 : failed += keywords_insert("EXECUTE", EXECUTE);
422 328 : failed += keywords_insert("EXEC", EXEC);
423 328 : failed += keywords_insert("DEALLOCATE", DEALLOCATE);
424 :
425 328 : failed += keywords_insert("INDEX", INDEX);
426 :
427 328 : failed += keywords_insert("SEQUENCE", SEQUENCE);
428 328 : failed += keywords_insert("RESTART", RESTART);
429 328 : failed += keywords_insert("INCREMENT", INCREMENT);
430 328 : failed += keywords_insert("MAXVALUE", MAXVALUE);
431 328 : failed += keywords_insert("MINVALUE", MINVALUE);
432 328 : failed += keywords_insert("CYCLE", CYCLE);
433 328 : failed += keywords_insert("CACHE", CACHE);
434 328 : failed += keywords_insert("NEXT", NEXT);
435 328 : failed += keywords_insert("VALUE", VALUE);
436 328 : failed += keywords_insert("GENERATED", GENERATED);
437 328 : failed += keywords_insert("ALWAYS", ALWAYS);
438 328 : failed += keywords_insert("IDENTITY", IDENTITY);
439 328 : failed += keywords_insert("SERIAL", SERIAL);
440 328 : failed += keywords_insert("BIGSERIAL", BIGSERIAL);
441 328 : failed += keywords_insert("AUTO_INCREMENT", AUTO_INCREMENT);
442 328 : failed += keywords_insert("CONTINUE", CONTINUE);
443 :
444 328 : failed += keywords_insert("TRIGGER", TRIGGER);
445 328 : failed += keywords_insert("ATOMIC", ATOMIC);
446 328 : failed += keywords_insert("BEGIN", BEGIN);
447 328 : failed += keywords_insert("OF", OF);
448 328 : failed += keywords_insert("BEFORE", BEFORE);
449 328 : failed += keywords_insert("AFTER", AFTER);
450 328 : failed += keywords_insert("ROW", ROW);
451 328 : failed += keywords_insert("STATEMENT", STATEMENT);
452 328 : failed += keywords_insert("NEW", sqlNEW);
453 328 : failed += keywords_insert("OLD", OLD);
454 328 : failed += keywords_insert("EACH", EACH);
455 328 : failed += keywords_insert("REFERENCING", REFERENCING);
456 :
457 328 : failed += keywords_insert("RANGE", RANGE);
458 328 : failed += keywords_insert("UNBOUNDED", UNBOUNDED);
459 328 : failed += keywords_insert("PRECEDING", PRECEDING);
460 328 : failed += keywords_insert("FOLLOWING", FOLLOWING);
461 328 : failed += keywords_insert("CURRENT", CURRENT);
462 328 : failed += keywords_insert("EXCLUDE", EXCLUDE);
463 328 : failed += keywords_insert("OTHERS", OTHERS);
464 328 : failed += keywords_insert("TIES", TIES);
465 328 : failed += keywords_insert("GROUPS", GROUPS);
466 328 : failed += keywords_insert("WINDOW", WINDOW);
467 :
468 : /* special SQL/XML keywords */
469 328 : failed += keywords_insert("XMLCOMMENT", XMLCOMMENT);
470 328 : failed += keywords_insert("XMLCONCAT", XMLCONCAT);
471 328 : failed += keywords_insert("XMLDOCUMENT", XMLDOCUMENT);
472 328 : failed += keywords_insert("XMLELEMENT", XMLELEMENT);
473 328 : failed += keywords_insert("XMLATTRIBUTES", XMLATTRIBUTES);
474 328 : failed += keywords_insert("XMLFOREST", XMLFOREST);
475 328 : failed += keywords_insert("XMLPARSE", XMLPARSE);
476 328 : failed += keywords_insert("STRIP", STRIP);
477 328 : failed += keywords_insert("WHITESPACE", WHITESPACE);
478 328 : failed += keywords_insert("XMLPI", XMLPI);
479 328 : failed += keywords_insert("XMLQUERY", XMLQUERY);
480 328 : failed += keywords_insert("PASSING", PASSING);
481 328 : failed += keywords_insert("XMLTEXT", XMLTEXT);
482 328 : failed += keywords_insert("NIL", NIL);
483 328 : failed += keywords_insert("REF", REF);
484 328 : failed += keywords_insert("ABSENT", ABSENT);
485 328 : failed += keywords_insert("DOCUMENT", DOCUMENT);
486 328 : failed += keywords_insert("ELEMENT", ELEMENT);
487 328 : failed += keywords_insert("CONTENT", CONTENT);
488 328 : failed += keywords_insert("XMLNAMESPACES", XMLNAMESPACES);
489 328 : failed += keywords_insert("NAMESPACE", NAMESPACE);
490 328 : failed += keywords_insert("XMLVALIDATE", XMLVALIDATE);
491 328 : failed += keywords_insert("RETURNING", RETURNING);
492 328 : failed += keywords_insert("LOCATION", LOCATION);
493 328 : failed += keywords_insert("ID", ID);
494 328 : failed += keywords_insert("ACCORDING", ACCORDING);
495 328 : failed += keywords_insert("XMLSCHEMA", XMLSCHEMA);
496 328 : failed += keywords_insert("URI", URI);
497 328 : failed += keywords_insert("XMLAGG", XMLAGG);
498 :
499 : /* keywords for opengis */
500 328 : failed += keywords_insert("GEOMETRY", GEOMETRY);
501 :
502 328 : failed += keywords_insert("POINT", GEOMETRYSUBTYPE);
503 328 : failed += keywords_insert("LINESTRING", GEOMETRYSUBTYPE);
504 328 : failed += keywords_insert("POLYGON", GEOMETRYSUBTYPE);
505 328 : failed += keywords_insert("MULTIPOINT", GEOMETRYSUBTYPE);
506 328 : failed += keywords_insert("MULTILINESTRING", GEOMETRYSUBTYPE);
507 328 : failed += keywords_insert("MULTIPOLYGON", GEOMETRYSUBTYPE);
508 328 : failed += keywords_insert("GEOMETRYCOLLECTION", GEOMETRYSUBTYPE);
509 :
510 328 : failed += keywords_insert("POINTZ", GEOMETRYSUBTYPE);
511 328 : failed += keywords_insert("LINESTRINGZ", GEOMETRYSUBTYPE);
512 328 : failed += keywords_insert("POLYGONZ", GEOMETRYSUBTYPE);
513 328 : failed += keywords_insert("MULTIPOINTZ", GEOMETRYSUBTYPE);
514 328 : failed += keywords_insert("MULTILINESTRINGZ", GEOMETRYSUBTYPE);
515 328 : failed += keywords_insert("MULTIPOLYGONZ", GEOMETRYSUBTYPE);
516 328 : failed += keywords_insert("GEOMETRYCOLLECTIONZ", GEOMETRYSUBTYPE);
517 :
518 328 : failed += keywords_insert("POINTM", GEOMETRYSUBTYPE);
519 328 : failed += keywords_insert("LINESTRINGM", GEOMETRYSUBTYPE);
520 328 : failed += keywords_insert("POLYGONM", GEOMETRYSUBTYPE);
521 328 : failed += keywords_insert("MULTIPOINTM", GEOMETRYSUBTYPE);
522 328 : failed += keywords_insert("MULTILINESTRINGM", GEOMETRYSUBTYPE);
523 328 : failed += keywords_insert("MULTIPOLYGONM", GEOMETRYSUBTYPE);
524 328 : failed += keywords_insert("GEOMETRYCOLLECTIONM", GEOMETRYSUBTYPE);
525 :
526 328 : failed += keywords_insert("POINTZM", GEOMETRYSUBTYPE);
527 328 : failed += keywords_insert("LINESTRINGZM", GEOMETRYSUBTYPE);
528 328 : failed += keywords_insert("POLYGONZM", GEOMETRYSUBTYPE);
529 328 : failed += keywords_insert("MULTIPOINTZM", GEOMETRYSUBTYPE);
530 328 : failed += keywords_insert("MULTILINESTRINGZM", GEOMETRYSUBTYPE);
531 328 : failed += keywords_insert("MULTIPOLYGONZM", GEOMETRYSUBTYPE);
532 328 : failed += keywords_insert("GEOMETRYCOLLECTIONZM", GEOMETRYSUBTYPE);
533 328 : failed += keywords_insert("LOGIN", LOGIN);
534 : // odbc keywords
535 328 : failed += keywords_insert("d", ODBC_DATE_ESCAPE_PREFIX);
536 328 : failed += keywords_insert("t", ODBC_TIME_ESCAPE_PREFIX);
537 328 : failed += keywords_insert("ts", ODBC_TIMESTAMP_ESCAPE_PREFIX);
538 328 : failed += keywords_insert("guid", ODBC_GUID_ESCAPE_PREFIX);
539 328 : failed += keywords_insert("fn", ODBC_FUNC_ESCAPE_PREFIX);
540 328 : failed += keywords_insert("oj", ODBC_OJ_ESCAPE_PREFIX);
541 328 : failed += keywords_insert("DAYNAME", DAYNAME);
542 328 : failed += keywords_insert("IFNULL", IFNULL);
543 328 : failed += keywords_insert("MONTHNAME", MONTHNAME);
544 328 : failed += keywords_insert("TIMESTAMPADD", TIMESTAMPADD);
545 328 : failed += keywords_insert("TIMESTAMPDIFF", TIMESTAMPDIFF);
546 328 : failed += keywords_insert("SQL_BIGINT", SQL_BIGINT);
547 328 : failed += keywords_insert("SQL_BINARY", SQL_BINARY);
548 328 : failed += keywords_insert("SQL_BIT", SQL_BIT);
549 328 : failed += keywords_insert("SQL_CHAR", SQL_CHAR);
550 328 : failed += keywords_insert("SQL_DATE", SQL_DATE);
551 328 : failed += keywords_insert("SQL_DECIMAL", SQL_DECIMAL);
552 328 : failed += keywords_insert("SQL_DOUBLE", SQL_DOUBLE);
553 328 : failed += keywords_insert("SQL_FLOAT", SQL_FLOAT);
554 328 : failed += keywords_insert("SQL_GUID", SQL_GUID);
555 328 : failed += keywords_insert("SQL_HUGEINT", SQL_HUGEINT);
556 328 : failed += keywords_insert("SQL_INTEGER", SQL_INTEGER);
557 328 : failed += keywords_insert("SQL_INTERVAL_DAY", SQL_INTERVAL_DAY);
558 328 : failed += keywords_insert("SQL_INTERVAL_DAY_TO_HOUR", SQL_INTERVAL_DAY_TO_HOUR);
559 328 : failed += keywords_insert("SQL_INTERVAL_DAY_TO_MINUTE", SQL_INTERVAL_DAY_TO_MINUTE);
560 328 : failed += keywords_insert("SQL_INTERVAL_DAY_TO_SECOND", SQL_INTERVAL_DAY_TO_SECOND);
561 328 : failed += keywords_insert("SQL_INTERVAL_HOUR", SQL_INTERVAL_HOUR);
562 328 : failed += keywords_insert("SQL_INTERVAL_HOUR_TO_MINUTE", SQL_INTERVAL_HOUR_TO_MINUTE);
563 328 : failed += keywords_insert("SQL_INTERVAL_HOUR_TO_SECOND", SQL_INTERVAL_HOUR_TO_SECOND);
564 328 : failed += keywords_insert("SQL_INTERVAL_MINUTE", SQL_INTERVAL_MINUTE);
565 328 : failed += keywords_insert("SQL_INTERVAL_MINUTE_TO_SECOND", SQL_INTERVAL_MINUTE_TO_SECOND);
566 328 : failed += keywords_insert("SQL_INTERVAL_MONTH", SQL_INTERVAL_MONTH);
567 328 : failed += keywords_insert("SQL_INTERVAL_SECOND", SQL_INTERVAL_SECOND);
568 328 : failed += keywords_insert("SQL_INTERVAL_YEAR", SQL_INTERVAL_YEAR);
569 328 : failed += keywords_insert("SQL_INTERVAL_YEAR_TO_MONTH", SQL_INTERVAL_YEAR_TO_MONTH);
570 328 : failed += keywords_insert("SQL_LONGVARBINARY", SQL_LONGVARBINARY);
571 328 : failed += keywords_insert("SQL_LONGVARCHAR", SQL_LONGVARCHAR);
572 328 : failed += keywords_insert("SQL_NUMERIC", SQL_NUMERIC);
573 328 : failed += keywords_insert("SQL_REAL", SQL_REAL);
574 328 : failed += keywords_insert("SQL_SMALLINT", SQL_SMALLINT);
575 328 : failed += keywords_insert("SQL_TIME", SQL_TIME);
576 328 : failed += keywords_insert("SQL_TIMESTAMP", SQL_TIMESTAMP);
577 328 : failed += keywords_insert("SQL_TINYINT", SQL_TINYINT);
578 328 : failed += keywords_insert("SQL_VARBINARY", SQL_VARBINARY);
579 328 : failed += keywords_insert("SQL_VARCHAR", SQL_VARCHAR);
580 328 : failed += keywords_insert("SQL_WCHAR", SQL_WCHAR);
581 328 : failed += keywords_insert("SQL_WLONGVARCHAR", SQL_WLONGVARCHAR);
582 328 : failed += keywords_insert("SQL_WVARCHAR", SQL_WVARCHAR);
583 328 : failed += keywords_insert("SQL_TSI_FRAC_SECOND", SQL_TSI_FRAC_SECOND);
584 328 : failed += keywords_insert("SQL_TSI_SECOND", SQL_TSI_SECOND);
585 328 : failed += keywords_insert("SQL_TSI_MINUTE", SQL_TSI_MINUTE);
586 328 : failed += keywords_insert("SQL_TSI_HOUR", SQL_TSI_HOUR);
587 328 : failed += keywords_insert("SQL_TSI_DAY", SQL_TSI_DAY);
588 328 : failed += keywords_insert("SQL_TSI_WEEK", SQL_TSI_WEEK);
589 328 : failed += keywords_insert("SQL_TSI_MONTH", SQL_TSI_MONTH);
590 328 : failed += keywords_insert("SQL_TSI_QUARTER", SQL_TSI_QUARTER);
591 328 : failed += keywords_insert("SQL_TSI_YEAR", SQL_TSI_YEAR);
592 :
593 328 : failed += keywords_insert("LEAST", MARGFUNC);
594 328 : failed += keywords_insert("GREATEST", MARGFUNC);
595 328 : return failed;
596 : }
597 :
598 : #define find_keyword_bs(lc, s) find_keyword(lc->rs->buf+lc->rs->pos+s)
599 :
600 : void
601 245022 : scanner_init(struct scanner *s, bstream *rs, stream *ws)
602 : {
603 490044 : *s = (struct scanner) {
604 : .rs = rs,
605 : .ws = ws,
606 : .mode = LINE_N,
607 245022 : .raw_string_mode = GDKgetenv_istrue("raw_strings"),
608 : .aborted = false,
609 : };
610 245022 : }
611 :
612 : void
613 1291951 : scanner_query_processed(struct scanner *s)
614 : {
615 1291951 : int cur;
616 :
617 1291951 : if (s->yybak) {
618 501924 : s->rs->buf[s->rs->pos + s->yycur] = s->yybak;
619 501924 : s->yybak = 0;
620 : }
621 1291951 : if (s->rs) {
622 1291951 : s->rs->pos += s->yycur;
623 : /* completely eat the query including white space after the ; */
624 2438558 : while (s->rs->pos < s->rs->len &&
625 2104622 : (cur = s->rs->buf[s->rs->pos], iswspace(cur))) {
626 1146607 : s->rs->pos++;
627 : }
628 : }
629 : /*assert(s->rs->pos <= s->rs->len);*/
630 1291951 : s->yycur = 0;
631 1291951 : s->started = 0;
632 1291951 : s->as = 0;
633 1291951 : s->schema = NULL;
634 1291951 : }
635 :
636 : static int
637 33 : scanner_error(mvc *lc, int cur)
638 : {
639 33 : switch (cur) {
640 0 : case EOF:
641 0 : (void) sql_error(lc, 1, SQLSTATE(42000) "Unexpected end of input");
642 0 : return EOF;
643 33 : default:
644 : /* on Windows at least, iswcntrl returns TRUE for
645 : * U+FEFF, but we just want consistent error
646 : * messages */
647 33 : (void) sql_error(lc, 1, SQLSTATE(42000) "Unexpected%s character (U+%04X)", iswcntrl(cur) && cur != 0xFEFF ? " control" : "", (unsigned) cur);
648 : }
649 33 : return LEX_ERROR;
650 : }
651 :
652 :
653 : /*
654 : UTF-8 encoding is as follows:
655 : U-00000000 - U-0000007F: 0xxxxxxx
656 : U-00000080 - U-000007FF: 110xxxxx 10xxxxxx
657 : U-00000800 - U-0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
658 : U-00010000 - U-001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
659 : U-00200000 - U-03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
660 : U-04000000 - U-7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
661 : */
662 : /* To be correctly coded UTF-8, the sequence should be the shortest
663 : possible encoding of the value being encoded. This means that for
664 : an encoding of length n+1 (1 <= n <= 5), at least one of the bits in
665 : utf8chkmsk[n] should be non-zero (else the encoding could be
666 : shorter).
667 : */
668 : static const int utf8chkmsk[] = {
669 : 0x0000007f,
670 : 0x00000780,
671 : 0x0000f800,
672 : 0x001f0000,
673 : 0x03e00000,
674 : 0x7c000000
675 : };
676 :
677 : static void
678 31293907 : utf8_putchar(struct scanner *lc, int ch)
679 : {
680 31293907 : if ((ch) < 0x80) {
681 31293902 : lc->yycur--;
682 5 : } else if ((ch) < 0x800) {
683 0 : lc->yycur -= 2;
684 5 : } else if ((ch) < 0x10000) {
685 5 : lc->yycur -= 3;
686 : } else {
687 0 : lc->yycur -= 4;
688 : }
689 31293907 : }
690 :
691 : static inline int
692 136076572 : scanner_read_more(struct scanner *lc, size_t n)
693 : {
694 136076572 : bstream *b = lc->rs;
695 136076572 : bool more = false;
696 :
697 :
698 136076572 : if (lc->aborted)
699 : return EOF;
700 136080879 : while (b->len < b->pos + lc->yycur + n) {
701 :
702 127359 : if (lc->mode == LINE_1 || !lc->started)
703 : return EOF;
704 :
705 : /* query is not finished ask for more */
706 0 : if (b->eof || !isa_block_stream(b->s)) {
707 0 : if (bstream_getoob(b)) {
708 0 : lc->aborted = true;
709 0 : return EOF;
710 : }
711 2157 : if (mnstr_write(lc->ws, PROMPT2, sizeof(PROMPT2) - 1, 1) == 1)
712 2157 : mnstr_flush(lc->ws, MNSTR_FLUSH_DATA);
713 2157 : b->eof = false;
714 2157 : more = true;
715 : }
716 : /* we need more query text */
717 4314 : if (bstream_next(b) < 0) {
718 0 : if (mnstr_errnr(b->s) == MNSTR_INTERRUPT) {
719 : // now what?
720 0 : lc->errstr = "Query aborted";
721 0 : lc->aborted = true;
722 0 : mnstr_clearerr(b->s);
723 : }
724 0 : return EOF;
725 4314 : } else if (/* we asked for more data but didn't get any */
726 2157 : (more && b->eof && b->len < b->pos + lc->yycur + n))
727 : return EOF;
728 4307 : if (more && b->pos + lc->yycur + 2 == b->len && b->buf[b->pos + lc->yycur] == '\200' && b->buf[b->pos + lc->yycur + 1] == '\n') {
729 0 : lc->errstr = "Query aborted";
730 0 : b->len -= 2;
731 0 : b->buf[b->len] = 0;
732 0 : return EOF;
733 : }
734 : }
735 : return 1;
736 : }
737 :
738 : static inline int
739 134820491 : scanner_getc(struct scanner *lc)
740 : {
741 134820491 : bstream *b = lc->rs;
742 134820491 : unsigned char *s = NULL;
743 134820491 : int c, m, n, mask;
744 :
745 134820491 : if (scanner_read_more(lc, 1) == EOF) {
746 : //lc->errstr = SQLSTATE(42000) "end of input stream";
747 : return EOF;
748 : }
749 134712840 : lc->errstr = NULL;
750 :
751 134712840 : s = (unsigned char *) b->buf + b->pos + lc->yycur++;
752 134712840 : if (((c = *s) & 0x80) == 0) {
753 : /* 7-bit char */
754 : return c;
755 : }
756 88250 : for (n = 0, m = 0x40; c & m; n++, m >>= 1)
757 : ;
758 : /* n now is number of 10xxxxxx bytes that should follow */
759 29443 : if (n == 0 || n >= 6 || (b->pos + n) > b->len) {
760 : /* incorrect UTF-8 sequence */
761 : /* n==0: c == 10xxxxxx */
762 : /* n>=6: c == 1111111x */
763 0 : lc->errstr = SQLSTATE(42000) "invalid start of UTF-8 sequence";
764 0 : goto error;
765 : }
766 :
767 29443 : if (scanner_read_more(lc, (size_t) n) == EOF)
768 : return EOF;
769 29443 : s = (unsigned char *) b->buf + b->pos + lc->yycur;
770 :
771 29443 : mask = utf8chkmsk[n];
772 29443 : c &= ~(0xFFC0 >> n); /* remove non-x bits */
773 88249 : while (--n >= 0) {
774 58807 : c <<= 6;
775 58807 : lc->yycur++;
776 58807 : if (((m = *s++) & 0xC0) != 0x80) {
777 : /* incorrect UTF-8 sequence: byte is not 10xxxxxx */
778 : /* this includes end-of-string (m == 0) */
779 1 : lc->errstr = SQLSTATE(42000) "invalid continuation in UTF-8 sequence";
780 1 : goto error;
781 : }
782 58806 : c |= m & 0x3F;
783 : }
784 29442 : if ((c & mask) == 0) {
785 : /* incorrect UTF-8 sequence: not shortest possible */
786 0 : lc->errstr = SQLSTATE(42000) "not shortest possible UTF-8 sequence";
787 0 : goto error;
788 : }
789 :
790 : return c;
791 :
792 1 : error:
793 1 : if (b->pos + lc->yycur < b->len) /* skip bogus char */
794 0 : lc->yycur++;
795 : return EOF;
796 : }
797 :
798 : static int
799 27926716 : scanner_token(struct scanner *lc, int token)
800 : {
801 27926716 : lc->yybak = lc->rs->buf[lc->rs->pos + lc->yycur];
802 27926716 : lc->rs->buf[lc->rs->pos + lc->yycur] = 0;
803 27926716 : lc->yyval = token;
804 27926716 : return lc->yyval;
805 : }
806 :
807 : static int
808 2077899 : scanner_string(mvc *c, int quote, bool escapes)
809 : {
810 2077899 : struct scanner *lc = &c->scanner;
811 2077899 : bstream *rs = lc->rs;
812 2077899 : int cur = quote;
813 2077899 : bool escape = false;
814 2077899 : const size_t limit = quote == '"' ? 1 << 11 : 1 << 30;
815 :
816 2077899 : lc->started = 1;
817 2115540 : while (cur != EOF) {
818 2115525 : size_t pos = 0;
819 2115525 : const size_t yycur = rs->pos + lc->yycur;
820 :
821 35046210 : while (cur != EOF && (quote != '"' || cur != 0xFEFF) && pos < limit &&
822 32930685 : (((cur = rs->buf[yycur + pos++]) & 0x80) == 0) &&
823 65831916 : cur && (cur != quote || escape)) {
824 30815161 : if (escapes && cur == '\\')
825 6612 : escape = !escape;
826 : else
827 : escape = false;
828 : }
829 2115525 : if (pos == limit) {
830 0 : (void) sql_error(c, 2, SQLSTATE(42000) "string too long");
831 0 : return LEX_ERROR;
832 : }
833 : /* BOM character not allowed as an identifier */
834 2115525 : if (cur == EOF || (quote == '"' && cur == 0xFEFF))
835 1 : return scanner_error(c, cur);
836 2115524 : lc->yycur += pos;
837 : /* check for quote escaped quote: Obscure SQL Rule */
838 2115524 : if (cur == quote && rs->buf[yycur + pos] == quote) {
839 8200 : lc->yycur++;
840 8200 : continue;
841 : }
842 2107324 : assert(yycur + pos <= rs->len + 1);
843 2107324 : if (cur == quote && !escape) {
844 2077869 : return scanner_token(lc, STRING);
845 : }
846 29455 : lc->yycur--; /* go back to current (possibly invalid) char */
847 : /* long utf8, if correct isn't the quote */
848 29455 : if (!cur) {
849 30 : if (lc->rs->len >= lc->rs->pos + lc->yycur + 1) {
850 14 : (void) sql_error(c, 2, SQLSTATE(42000) "NULL byte in string");
851 14 : return LEX_ERROR;
852 : }
853 16 : cur = scanner_read_more(lc, 1);
854 : } else {
855 29425 : cur = scanner_getc(lc);
856 : }
857 : }
858 15 : (void) sql_error(c, 2, "%s", lc->errstr ? lc->errstr : SQLSTATE(42000) "Unexpected end of input");
859 15 : return EOF;
860 : }
861 :
862 : /* scan a structure {blah} into a string. We only count the matching {}
863 : * unless escaped. We do not consider embeddings in string literals yet
864 : */
865 :
866 : static int
867 230 : scanner_body(mvc *c)
868 : {
869 230 : struct scanner *lc = &c->scanner;
870 230 : bstream *rs = lc->rs;
871 230 : int cur = (int) 'x';
872 230 : int blk = 1;
873 230 : bool escape = false;
874 :
875 230 : lc->started = 1;
876 230 : assert(rs->buf[rs->pos + lc->yycur-1] == '{');
877 286 : while (cur != EOF) {
878 286 : size_t pos = rs->pos + lc->yycur;
879 :
880 31815 : while ((((cur = rs->buf[pos++]) & 0x80) == 0) && cur && (blk || escape)) {
881 31529 : if (cur != '\\')
882 : escape = false;
883 : else
884 12 : escape = !escape;
885 31529 : blk += cur =='{';
886 31529 : blk -= cur =='}';
887 : }
888 286 : lc->yycur = pos - rs->pos;
889 286 : assert(pos <= rs->len + 1);
890 286 : if (blk == 0 && !escape){
891 230 : lc->yycur--; /* go back to current (possibly invalid) char */
892 230 : return scanner_token(lc, X_BODY);
893 : }
894 56 : lc->yycur--; /* go back to current (possibly invalid) char */
895 56 : if (!cur) {
896 56 : if (lc->rs->len >= lc->rs->pos + lc->yycur + 1) {
897 0 : (void) sql_error(c, 2, SQLSTATE(42000) "NULL byte in string");
898 0 : return LEX_ERROR;
899 : }
900 56 : cur = scanner_read_more(lc, 1);
901 : } else {
902 0 : cur = scanner_getc(lc);
903 : }
904 : }
905 0 : (void) sql_error(c, 2, SQLSTATE(42000) "Unexpected end of input");
906 0 : return EOF;
907 : }
908 :
909 : static int
910 13471153 : keyword_or_ident(mvc * c, int cur)
911 : {
912 13471153 : struct scanner *lc = &c->scanner;
913 13471153 : keyword *k = NULL;
914 13471153 : size_t s;
915 :
916 13471153 : lc->started = 1;
917 13471153 : utf8_putchar(lc, cur);
918 13471145 : s = lc->yycur;
919 13471145 : lc->yyval = IDENT;
920 80560709 : while ((cur = scanner_getc(lc)) != EOF) {
921 80560642 : if (!iswalnum(cur) && cur != '_') {
922 13471078 : utf8_putchar(lc, cur);
923 13471134 : (void)scanner_token(lc, IDENT);
924 13471134 : if ((k = find_keyword_bs(lc,s)))
925 8265540 : lc->yyval = k->token;
926 13471380 : return lc->yyval;
927 : }
928 : }
929 : if (cur < 0)
930 : return cur;
931 : (void)scanner_token(lc, IDENT);
932 : if ((k = find_keyword_bs(lc,s)))
933 : lc->yyval = k->token;
934 : return lc->yyval;
935 : }
936 :
937 : static int
938 14028256 : skip_white_space(struct scanner * lc)
939 : {
940 17638102 : int cur;
941 :
942 17638102 : do {
943 17638102 : lc->yysval = lc->yycur;
944 17638102 : } while ((cur = scanner_getc(lc)) != EOF && iswspace(cur));
945 14027721 : return cur;
946 : }
947 :
948 : static int
949 69190 : skip_c_comment(struct scanner * lc)
950 : {
951 69190 : int cur;
952 69190 : int prev = 0;
953 69190 : int started = lc->started;
954 69190 : int depth = 1;
955 :
956 69190 : lc->started = 1;
957 1395388 : while (depth > 0 && (cur = scanner_getc(lc)) != EOF) {
958 1326198 : if (prev == '*' && cur == '/')
959 69190 : depth--;
960 1257008 : else if (prev == '/' && cur == '*') {
961 : /* block comments can nest */
962 0 : cur = 0; /* prevent slash-star-slash from matching */
963 0 : depth++;
964 : }
965 : prev = cur;
966 : }
967 69190 : lc->yysval = lc->yycur;
968 69190 : lc->started = started;
969 : /* a comment is equivalent to a newline */
970 69190 : return cur == EOF ? cur : '\n';
971 : }
972 :
973 : static int
974 3176 : skip_sql_comment(struct scanner * lc)
975 : {
976 3176 : int cur;
977 3176 : int started = lc->started;
978 :
979 3176 : lc->started = 1;
980 828366 : while ((cur = scanner_getc(lc)) != EOF && (cur != '\n'))
981 : ;
982 3176 : lc->yysval = lc->yycur;
983 3176 : lc->started = started;
984 : /* a comment is equivalent to a newline */
985 3176 : return cur;
986 : }
987 :
988 : static int tokenize(mvc * lc, int cur);
989 :
990 5549919 : static inline bool is_valid_decimal_digit(int cur) { return (iswdigit(cur)); }
991 13 : static inline bool is_valid_binary_digit(int cur) { return (iswdigit(cur) && cur < '2'); }
992 10 : static inline bool is_valid_octal_digit(int cur) { return (iswdigit(cur) && cur < '8'); }
993 3688 : static inline bool is_valid_hexadecimal_digit(int cur) { return iswxdigit(cur); }
994 :
995 1847288 : static inline int check_validity_number(mvc* c, int pcur, bool initial_underscore_allowed, int *token, int type) {
996 1847288 : struct scanner *lc = &c->scanner;
997 1847288 : bool (*is_valid_n_ary_digit)(int);
998 :
999 1847288 : if (pcur == '_' && !initial_underscore_allowed) /* ERROR: initial underscore not allowed */ {
1000 0 : *token = 0;
1001 0 : return '_';
1002 : }
1003 :
1004 1847288 : switch (type) {
1005 : case BINARYNUM:
1006 : is_valid_n_ary_digit = &is_valid_binary_digit;
1007 : break;
1008 3 : case OCTALNUM:
1009 3 : is_valid_n_ary_digit = &is_valid_octal_digit;
1010 3 : break;
1011 280 : case HEXADECIMALNUM:
1012 280 : is_valid_n_ary_digit = &is_valid_hexadecimal_digit;
1013 280 : break;
1014 1847003 : default:
1015 1847003 : is_valid_n_ary_digit = &is_valid_decimal_digit;
1016 1847003 : break;
1017 : }
1018 :
1019 1847288 : if ( !(pcur == '_' || is_valid_n_ary_digit(pcur)) ) /* ERROR: first digit is not valid */ {
1020 17 : *token = 0;
1021 17 : return pcur;
1022 : }
1023 :
1024 1847406 : int cur = scanner_getc(lc);
1025 1847380 : *token = type;
1026 3717737 : while (cur != EOF) {
1027 3717572 : if (cur == '_') {
1028 25 : if (pcur == '_') /* ERROR: multiple consecutive underscores */ {
1029 2 : *token = 0;
1030 2 : return '_';
1031 : }
1032 : }
1033 3717547 : else if (!is_valid_n_ary_digit(cur))
1034 : break;
1035 1870266 : pcur = cur;
1036 1870266 : cur = scanner_getc(lc);
1037 : }
1038 :
1039 1847327 : if (pcur == '_') {
1040 3 : *token = 0;
1041 3 : if (iswalnum(cur)) /* ERROR: not a valid digit */
1042 : return cur;
1043 : else /* ERROR: number ends with underscore */
1044 : return '_';
1045 : }
1046 :
1047 : return cur;
1048 : }
1049 :
1050 : static int
1051 1833688 : number(mvc * c, int cur)
1052 : {
1053 1833688 : struct scanner *lc = &c->scanner;
1054 1833688 : int token = sqlINT;
1055 :
1056 : /* a number has one of these forms (expressed in regular expressions):
1057 : * 0x[0-9A-Fa-f]+ -- (hexadecimal) INTEGER
1058 : * \.[0-9]+ -- DECIMAL
1059 : * [0-9]+\.[0-9]* -- DECIMAL
1060 : * [0-9]+@0 -- OID
1061 : * [0-9]*\.[0-9]+[eE][-+]?[0-9]+ -- REAL
1062 : * [0-9]+(\.[0-9]*)?[eE][-+]?[0-9]+ -- REAL
1063 : * [0-9]+ -- (decimal) INTEGER
1064 : */
1065 1833688 : lc->started = 1;
1066 1833688 : if (cur == '0') {
1067 285941 : switch ((cur = scanner_getc(lc))) {
1068 2 : case 'b':
1069 2 : cur = scanner_getc(lc);
1070 2 : if ((cur = check_validity_number(c, cur, true, &token, BINARYNUM)) == EOF) return cur;
1071 : break;
1072 3 : case 'o':
1073 3 : cur = scanner_getc(lc);
1074 3 : if ((cur = check_validity_number(c, cur, true, &token, OCTALNUM)) == EOF) return cur;
1075 : break;
1076 280 : case 'x':
1077 280 : cur = scanner_getc(lc);
1078 280 : if ((cur = check_validity_number(c, cur, true, &token, HEXADECIMALNUM)) == EOF) return cur;
1079 : break;
1080 285657 : default:
1081 285657 : utf8_putchar(lc, cur);
1082 285657 : cur = '0';
1083 : }
1084 : }
1085 1833694 : if (token == sqlINT) {
1086 1833657 : if ((cur = check_validity_number(c, cur, false, &token, sqlINT)) == EOF) return cur;
1087 1833741 : if (cur == '@') {
1088 0 : if (token == sqlINT) {
1089 0 : cur = scanner_getc(lc);
1090 0 : if (cur == EOF)
1091 : return cur;
1092 0 : if (cur == '0') {
1093 0 : cur = scanner_getc(lc);
1094 0 : if (cur == EOF)
1095 : return cur;
1096 0 : token = OIDNUM;
1097 : } else {
1098 : /* number + '@' not followed by 0: show '@' as erroneous */
1099 0 : utf8_putchar(lc, cur);
1100 0 : cur = '@';
1101 0 : token = 0;
1102 : }
1103 : }
1104 : } else {
1105 1833741 : if (cur == '.') {
1106 11075 : cur = scanner_getc(lc);
1107 11075 : if (iswalnum(cur)) /* early exit for numerical forms with final . e.g. 10. */
1108 11069 : if ((cur = check_validity_number(c, cur, false, &token, INTNUM)) == EOF) return cur;
1109 : }
1110 1833741 : if (token != 0)
1111 1833684 : if (cur == 'e' || cur == 'E') {
1112 2225 : cur = scanner_getc(lc);
1113 2225 : if (cur == '+' || cur == '-')
1114 2111 : cur = scanner_getc(lc);
1115 2225 : if ((cur = check_validity_number(c, cur, false, &token, APPROXNUM)) == EOF) return cur;
1116 : }
1117 : }
1118 : }
1119 :
1120 1831553 : assert(cur != EOF);
1121 :
1122 1833778 : if (iswalnum(cur)) /* ERROR: not a valid digit */
1123 6 : token = 0;
1124 :
1125 1833778 : utf8_putchar(lc, cur);
1126 :
1127 1833797 : if (token) {
1128 1833787 : return scanner_token(lc, token);
1129 : } else {
1130 10 : (void)sql_error( c, 2, SQLSTATE(42000) "Unexpected symbol %lc", (wint_t) cur);
1131 10 : return LEX_ERROR;
1132 : }
1133 : }
1134 :
1135 : static
1136 12687176 : int scanner_symbol(mvc * c, int cur)
1137 : {
1138 12687176 : struct scanner *lc = &c->scanner;
1139 12687176 : int next = 0;
1140 12687176 : int started = lc->started;
1141 :
1142 12687176 : switch (cur) {
1143 71831 : case '/':
1144 71831 : lc->started = 1;
1145 71831 : next = scanner_getc(lc);
1146 71831 : if (next < 0)
1147 : return EOF;
1148 71831 : if (next == '*') {
1149 69190 : lc->started = started;
1150 69190 : cur = skip_c_comment(lc);
1151 69190 : if (cur < 0)
1152 : return EOF;
1153 69190 : return tokenize(c, cur);
1154 : } else {
1155 2641 : utf8_putchar(lc, next);
1156 2641 : return scanner_token(lc, cur);
1157 : }
1158 0 : case '0':
1159 : case '1':
1160 : case '2':
1161 : case '3':
1162 : case '4':
1163 : case '5':
1164 : case '6':
1165 : case '7':
1166 : case '8':
1167 : case '9':
1168 0 : return number(c, cur);
1169 5 : case '#':
1170 5 : if ((cur = skip_sql_comment(lc)) == EOF)
1171 : return cur;
1172 5 : return tokenize(c, cur);
1173 775349 : case '\'':
1174 775349 : if (lc->raw_string_mode || lc->next_string_is_raw)
1175 46 : return scanner_string(c, cur, false);
1176 775303 : return scanner_string(c, cur, true);
1177 1295510 : case '"':
1178 1295510 : return scanner_string(c, cur, false);
1179 496 : case '{':
1180 : // if previous tokens like LANGUAGE IDENT
1181 : // TODO checking on IDENT only may not be enough
1182 496 : if (lc->yylast == IDENT)
1183 230 : return scanner_body(c);
1184 266 : lc->started = 1;
1185 266 : return scanner_token(lc, cur);
1186 266 : case '}':
1187 266 : lc->started = 1;
1188 266 : return scanner_token(lc, cur);
1189 29889 : case '-':
1190 29889 : lc->started = 1;
1191 29889 : next = scanner_getc(lc);
1192 29889 : if (next < 0)
1193 : return EOF;
1194 29888 : if (next == '-') {
1195 3171 : lc->started = started;
1196 3171 : if ((cur = skip_sql_comment(lc)) == EOF)
1197 : return cur;
1198 3171 : return tokenize(c, cur);
1199 : }
1200 26717 : lc->started = 1;
1201 26717 : utf8_putchar(lc, next);
1202 26717 : return scanner_token(lc, cur);
1203 12 : case '~': /* binary not */
1204 12 : lc->started = 1;
1205 12 : next = scanner_getc(lc);
1206 12 : if (next < 0)
1207 : return EOF;
1208 12 : if (next == '=')
1209 5 : return scanner_token(lc, GEOM_MBR_EQUAL);
1210 7 : utf8_putchar(lc, next);
1211 7 : return scanner_token(lc, cur);
1212 7103286 : case '^': /* binary xor */
1213 : case '*':
1214 : case '?':
1215 : case ':':
1216 : case '%':
1217 : case '+':
1218 : case '(':
1219 : case ')':
1220 : case ',':
1221 : case '=':
1222 : case '[':
1223 : case ']':
1224 7103286 : lc->started = 1;
1225 7103286 : return scanner_token(lc, cur);
1226 6251 : case '&':
1227 6251 : lc->started = 1;
1228 6251 : cur = scanner_getc(lc);
1229 6251 : if (cur < 0)
1230 : return EOF;
1231 6251 : if (cur < 0)
1232 : return EOF;
1233 6251 : if(cur == '<') {
1234 3 : next = scanner_getc(lc);
1235 3 : if (next < 0)
1236 : return EOF;
1237 3 : if(next == '|') {
1238 0 : return scanner_token(lc, GEOM_OVERLAP_OR_BELOW);
1239 : } else {
1240 3 : utf8_putchar(lc, next); //put the char back
1241 3 : return scanner_token(lc, GEOM_OVERLAP_OR_LEFT);
1242 : }
1243 6248 : } else if(cur == '>')
1244 3 : return scanner_token(lc, GEOM_OVERLAP_OR_RIGHT);
1245 6245 : else if(cur == '&')
1246 3 : return scanner_token(lc, GEOM_OVERLAP);
1247 : else {/* binary and */
1248 6242 : utf8_putchar(lc, cur); //put the char back
1249 6242 : return scanner_token(lc, '&');
1250 : }
1251 19 : case '@':
1252 19 : lc->started = 1;
1253 19 : return scanner_token(lc, AT);
1254 982077 : case ';':
1255 982077 : lc->started = 0;
1256 982077 : return scanner_token(lc, SCOLON);
1257 27 : case '!':
1258 27 : lc->started = 1;
1259 27 : cur = scanner_getc(lc);
1260 27 : if (cur < 0)
1261 : return EOF;
1262 27 : else if (cur == '=') {
1263 21 : lc->rs->buf[lc->rs->pos + lc->yycur - 2] = '<';
1264 21 : lc->rs->buf[lc->rs->pos + lc->yycur - 1] = '>';
1265 21 : return scanner_token( lc, COMPARISON);
1266 : } else {
1267 6 : utf8_putchar(lc, cur); //put the char back
1268 : }
1269 6 : return scanner_token(lc, '!');
1270 51296 : case '<':
1271 51296 : lc->started = 1;
1272 51296 : cur = scanner_getc(lc);
1273 51296 : if (cur < 0)
1274 : return EOF;
1275 51296 : if (cur == '=') {
1276 3123 : return scanner_token( lc, COMPARISON);
1277 48173 : } else if (cur == '>') {
1278 34731 : return scanner_token( lc, COMPARISON);
1279 13442 : } else if (cur == '<') {
1280 44 : next = scanner_getc(lc);
1281 44 : if (next < 0)
1282 : return EOF;
1283 44 : if (next == '=') {
1284 4 : return scanner_token( lc, LEFT_SHIFT_ASSIGN);
1285 40 : } else if (next == '|') {
1286 1 : return scanner_token(lc, GEOM_BELOW);
1287 : } else {
1288 39 : utf8_putchar(lc, next); //put the char back
1289 39 : return scanner_token( lc, LEFT_SHIFT);
1290 : }
1291 13398 : } else if(cur == '-') {
1292 19 : next = scanner_getc(lc);
1293 19 : if (next < 0)
1294 : return EOF;
1295 19 : if(next == '>') {
1296 7 : return scanner_token(lc, GEOM_DIST);
1297 : } else {
1298 : //put the characters back and fall in the next possible case
1299 12 : utf8_putchar(lc, next);
1300 12 : utf8_putchar(lc, cur);
1301 12 : return scanner_token( lc, COMPARISON);
1302 : }
1303 : } else {
1304 13379 : utf8_putchar(lc, cur);
1305 13379 : return scanner_token( lc, COMPARISON);
1306 : }
1307 47893 : case '>':
1308 47893 : lc->started = 1;
1309 47893 : cur = scanner_getc(lc);
1310 47893 : if (cur < 0)
1311 : return EOF;
1312 47893 : if (cur == '>') {
1313 2657 : cur = scanner_getc(lc);
1314 2657 : if (cur < 0)
1315 : return EOF;
1316 2657 : if (cur == '=')
1317 3 : return scanner_token( lc, RIGHT_SHIFT_ASSIGN);
1318 2654 : utf8_putchar(lc, cur);
1319 2654 : return scanner_token( lc, RIGHT_SHIFT);
1320 45236 : } else if (cur != '=') {
1321 42984 : utf8_putchar(lc, cur);
1322 42984 : return scanner_token( lc, COMPARISON);
1323 : } else {
1324 2252 : return scanner_token( lc, COMPARISON);
1325 : }
1326 2141654 : case '.':
1327 2141654 : lc->started = 1;
1328 2141654 : cur = scanner_getc(lc);
1329 2141654 : if (cur < 0)
1330 : return EOF;
1331 2141653 : if (!iswdigit(cur)) {
1332 2141640 : utf8_putchar(lc, cur);
1333 2141640 : return scanner_token( lc, '.');
1334 : } else {
1335 13 : utf8_putchar(lc, cur);
1336 13 : cur = '.';
1337 13 : return number(c, cur);
1338 : }
1339 181305 : case '|': /* binary or or string concat */
1340 181305 : lc->started = 1;
1341 181305 : cur = scanner_getc(lc);
1342 181305 : if (cur < 0)
1343 : return EOF;
1344 181305 : if (cur == '|') {
1345 181280 : return scanner_token(lc, CONCATSTRING);
1346 25 : } else if (cur == '&') {
1347 0 : next = scanner_getc(lc);
1348 0 : if (next < 0)
1349 : return EOF;
1350 0 : if(next == '>') {
1351 0 : return scanner_token(lc, GEOM_OVERLAP_OR_ABOVE);
1352 : } else {
1353 0 : utf8_putchar(lc, next); //put the char back
1354 0 : utf8_putchar(lc, cur); //put the char back
1355 0 : return scanner_token(lc, '|');
1356 : }
1357 25 : } else if (cur == '>') {
1358 1 : next = scanner_getc(lc);
1359 1 : if (next < 0)
1360 : return EOF;
1361 1 : if(next == '>') {
1362 1 : return scanner_token(lc, GEOM_ABOVE);
1363 : } else {
1364 0 : utf8_putchar(lc, next); //put the char back
1365 0 : utf8_putchar(lc, cur); //put the char back
1366 0 : return scanner_token(lc, '|');
1367 : }
1368 : } else {
1369 24 : utf8_putchar(lc, cur);
1370 24 : return scanner_token(lc, '|');
1371 : }
1372 : }
1373 10 : (void)sql_error( c, 3, SQLSTATE(42000) "Unexpected symbol (%lc)", (wint_t) cur);
1374 10 : return LEX_ERROR;
1375 : }
1376 :
1377 : static int
1378 28015921 : tokenize(mvc * c, int cur)
1379 : {
1380 28015921 : struct scanner *lc = &c->scanner;
1381 56028651 : while (1) {
1382 42022286 : if (cur == 0xFEFF) {
1383 : /* on Linux at least, iswpunct returns TRUE
1384 : * for U+FEFF, but we don't want that, we just
1385 : * want to go to the scanner_error case
1386 : * below */
1387 : ;
1388 42022678 : } else if (iswspace(cur)) {
1389 14023773 : if ((cur = skip_white_space(lc)) == EOF)
1390 : return cur;
1391 14006365 : continue; /* try again */
1392 27998905 : } else if (iswdigit(cur)) {
1393 1834140 : return number(c, cur);
1394 26164765 : } else if (iswalpha(cur) || cur == '_') {
1395 13442827 : switch (cur) {
1396 653045 : case 'e': /* string with escapes */
1397 : case 'E':
1398 653045 : if (scanner_read_more(lc, 1) != EOF &&
1399 653045 : lc->rs->buf[lc->rs->pos + lc->yycur] == '\'') {
1400 3747 : return scanner_string(c, scanner_getc(lc), true);
1401 : }
1402 : break;
1403 416347 : case 'x': /* blob */
1404 : case 'X':
1405 : case 'r': /* raw string */
1406 : case 'R':
1407 416347 : if (scanner_read_more(lc, 1) != EOF &&
1408 416347 : lc->rs->buf[lc->rs->pos + lc->yycur] == '\'') {
1409 3276 : return scanner_string(c, scanner_getc(lc), false);
1410 : }
1411 : break;
1412 153887 : case 'u': /* unicode string */
1413 : case 'U':
1414 153887 : if (scanner_read_more(lc, 1) != EOF &&
1415 153904 : lc->rs->buf[lc->rs->pos + lc->yycur] == '&' &&
1416 17 : scanner_read_more(lc, 2) != EOF &&
1417 17 : (lc->rs->buf[lc->rs->pos + lc->yycur + 1] == '\'' ||
1418 : lc->rs->buf[lc->rs->pos + lc->yycur + 1] == '"')) {
1419 17 : cur = scanner_getc(lc); /* '&' */
1420 17 : return scanner_string(c, scanner_getc(lc), false);
1421 : }
1422 : break;
1423 : default:
1424 : break;
1425 : }
1426 13471160 : return keyword_or_ident(c, cur);
1427 12686565 : } else if (iswpunct(cur)) {
1428 12686141 : return scanner_symbol(c, cur);
1429 : }
1430 32 : if (cur == EOF) {
1431 0 : if (lc->mode == LINE_1 || !lc->started )
1432 : return cur;
1433 0 : return scanner_error(c, cur);
1434 : }
1435 : /* none of the above: error */
1436 32 : return scanner_error(c, cur);
1437 : }
1438 : }
1439 :
1440 : /* SQL 'quoted' idents consist of a set of any character of
1441 : * the source language character set other than a 'quote'
1442 : *
1443 : * MonetDB has 3 restrictions:
1444 : * 1 we disallow '%' as the first character.
1445 : * 2 the length is limited to 1024 characters
1446 : * 3 the identifier 'TID%' is not allowed
1447 : */
1448 : static bool
1449 1295499 : valid_ident(const char *restrict s, char *restrict dst)
1450 : {
1451 1295499 : int p = 0;
1452 :
1453 1295499 : if (*s == '%')
1454 : return false;
1455 :
1456 9584264 : while (*s) {
1457 8288765 : if ((dst[p++] = *s++) == '"' && *s == '"')
1458 68 : s++;
1459 8288765 : if (p >= 1024)
1460 : return false;
1461 : }
1462 1295499 : dst[p] = '\0';
1463 1295499 : if (strcmp(dst, TID + 1) == 0) /* an index named 'TID%' could interfere with '%TID%' */
1464 : return false;
1465 : return true;
1466 : }
1467 :
1468 : static inline int
1469 28107685 : sql_get_next_token(YYSTYPE *yylval, void *parm)
1470 : {
1471 28107685 : mvc *c = (mvc*)parm;
1472 28107685 : struct scanner *lc = &c->scanner;
1473 28107685 : int token = 0, cur = 0;
1474 :
1475 28107685 : if (lc->rs->buf == NULL) /* malloc failure */
1476 : return EOF;
1477 :
1478 28107685 : if (lc->yynext) {
1479 61611 : int next = lc->yynext;
1480 :
1481 61611 : lc->yynext = 0;
1482 61611 : return(next);
1483 : }
1484 :
1485 28046074 : if (lc->yybak) {
1486 27032310 : lc->rs->buf[lc->rs->pos + lc->yycur] = lc->yybak;
1487 27032310 : lc->yybak = 0;
1488 : }
1489 :
1490 28046074 : lc->yysval = lc->yycur;
1491 28046074 : lc->yylast = lc->yyval;
1492 28046074 : cur = scanner_getc(lc);
1493 28053554 : if (cur < 0)
1494 : return EOF;
1495 27942674 : token = tokenize(c, cur);
1496 :
1497 27935984 : yylval->sval = (lc->rs->buf + lc->rs->pos + lc->yysval);
1498 :
1499 27935984 : if (token == KW_TYPE)
1500 49006 : token = aTYPE;
1501 :
1502 27935984 : if (token == IDENT || token == COMPARISON ||
1503 22633643 : token == RANK || token == aTYPE || token == MARGFUNC) {
1504 5361225 : yylval->sval = sa_strndup(c->sa, yylval->sval, lc->yycur-lc->yysval);
1505 5361121 : lc->next_string_is_raw = false;
1506 22574759 : } else if (token == STRING) {
1507 2077869 : char quote = *yylval->sval;
1508 2077869 : char *str = sa_alloc( c->sa, (lc->yycur-lc->yysval-2)*2 + 1 );
1509 2077869 : char *dst;
1510 :
1511 2077869 : assert(quote == '"' || quote == '\'' || quote == 'E' || quote == 'e' || quote == 'U' || quote == 'u' || quote == 'X' || quote == 'x' || quote == 'R' || quote == 'r');
1512 :
1513 2077869 : lc->rs->buf[lc->rs->pos + lc->yycur - 1] = 0;
1514 2077869 : switch (quote) {
1515 1295499 : case '"':
1516 1295499 : if (valid_ident(yylval->sval+1,str)) {
1517 : token = IDENT;
1518 : } else {
1519 0 : sql_error(c, 1, SQLSTATE(42000) "Invalid identifier '%s'", yylval->sval+1);
1520 0 : return LEX_ERROR;
1521 : }
1522 : break;
1523 3746 : case 'e':
1524 : case 'E':
1525 3746 : assert(yylval->sval[1] == '\'');
1526 3746 : if (GDKstrFromStr((unsigned char *) str,
1527 : (unsigned char *) yylval->sval + 2,
1528 3746 : lc->yycur-lc->yysval - 2, '\'') < 0) {
1529 1 : char *err = GDKerrbuf;
1530 1 : if (strncmp(err, GDKERROR, strlen(GDKERROR)) == 0)
1531 1 : err += strlen(GDKERROR);
1532 0 : else if (*err == '!')
1533 0 : err++;
1534 1 : sql_error(c, 1, SQLSTATE(42000) "%s", err);
1535 1 : return LEX_ERROR;
1536 : }
1537 : quote = '\'';
1538 : break;
1539 17 : case 'u':
1540 : case 'U':
1541 17 : assert(yylval->sval[1] == '&');
1542 17 : assert(yylval->sval[2] == '\'' || yylval->sval[2] == '"');
1543 17 : strcpy(str, yylval->sval + 3);
1544 17 : token = yylval->sval[2] == '\'' ? USTRING : UIDENT;
1545 17 : quote = yylval->sval[2];
1546 17 : lc->next_string_is_raw = true;
1547 17 : break;
1548 1 : case 'x':
1549 : case 'X':
1550 1 : assert(yylval->sval[1] == '\'');
1551 1 : dst = str;
1552 5 : for (char *src = yylval->sval + 2; *src; dst++)
1553 4 : if ((*dst = *src++) == '\'' && *src == '\'')
1554 0 : src++;
1555 1 : *dst = 0;
1556 1 : quote = '\'';
1557 1 : token = XSTRING;
1558 1 : lc->next_string_is_raw = true;
1559 1 : break;
1560 3268 : case 'r':
1561 : case 'R':
1562 3268 : assert(yylval->sval[1] == '\'');
1563 3268 : dst = str;
1564 449797 : for (char *src = yylval->sval + 2; *src; dst++)
1565 446529 : if ((*dst = *src++) == '\'' && *src == '\'')
1566 2732 : src++;
1567 3268 : quote = '\'';
1568 3268 : *dst = 0;
1569 3268 : break;
1570 775338 : default:
1571 775338 : if (lc->raw_string_mode || lc->next_string_is_raw) {
1572 46 : dst = str;
1573 436 : for (char *src = yylval->sval + 1; *src; dst++)
1574 390 : if ((*dst = *src++) == '\'' && *src == '\'')
1575 1 : src++;
1576 46 : *dst = 0;
1577 : } else {
1578 775292 : if (GDKstrFromStr((unsigned char *)str,
1579 775292 : (unsigned char *)yylval->sval + 1,
1580 775292 : lc->yycur - lc->yysval - 1,
1581 : '\'') < 0) {
1582 1 : sql_error(c, 1, SQLSTATE(42000) "%s", GDKerrbuf);
1583 1 : return LEX_ERROR;
1584 : }
1585 : }
1586 : break;
1587 : }
1588 2077867 : yylval->sval = str;
1589 :
1590 : /* reset original */
1591 2077867 : lc->rs->buf[lc->rs->pos+lc->yycur- 1] = quote;
1592 : } else {
1593 20496890 : lc->next_string_is_raw = false;
1594 : }
1595 :
1596 : return(token);
1597 : }
1598 :
1599 : static int scanner( YYSTYPE *yylval, void *m, bool log);
1600 :
1601 : static int
1602 27978379 : scanner(YYSTYPE * yylval, void *parm, bool log)
1603 : {
1604 27978379 : int token;
1605 27978379 : mvc *c = (mvc *) parm;
1606 27978379 : struct scanner *lc = &c->scanner;
1607 27978379 : size_t pos;
1608 :
1609 : /* store position for when view's query ends */
1610 27978379 : pos = lc->rs->pos + lc->yycur;
1611 :
1612 27978379 : token = sql_get_next_token(yylval, parm);
1613 :
1614 27972871 : if (token == NOT) {
1615 74343 : int next = scanner(yylval, parm, false);
1616 :
1617 74343 : if (next == NOT) {
1618 2 : return scanner(yylval, parm, false);
1619 : } else if (next == EXISTS) {
1620 : token = NOT_EXISTS;
1621 : } else if (next == BETWEEN) {
1622 : token = NOT_BETWEEN;
1623 : } else if (next == sqlIN) {
1624 : token = NOT_IN;
1625 : } else if (next == LIKE) {
1626 : token = NOT_LIKE;
1627 : } else if (next == ILIKE) {
1628 : token = NOT_ILIKE;
1629 : } else {
1630 61611 : lc->yynext = next;
1631 : }
1632 27898528 : } else if (token == SCOLON) {
1633 : /* ignore semi-colon(s) following a semi-colon */
1634 982042 : if (lc->yylast == SCOLON) {
1635 131638 : size_t prev = lc->yycur;
1636 131639 : while ((token = sql_get_next_token(yylval, parm)) == SCOLON)
1637 1 : prev = lc->yycur;
1638 :
1639 : /* skip the skipped stuff also in the buffer */
1640 131668 : lc->rs->pos += prev;
1641 131668 : lc->yycur -= prev;
1642 : }
1643 : }
1644 :
1645 27972899 : if (lc->log && log)
1646 0 : mnstr_write(lc->log, lc->rs->buf+pos, lc->rs->pos + lc->yycur - pos, 1);
1647 :
1648 27972899 : lc->started += (token != EOF);
1649 27972899 : return token;
1650 : }
1651 :
1652 : /* also see sql_parser.y */
1653 : extern int sqllex(YYSTYPE * yylval, void *parm);
1654 :
1655 : int
1656 27905567 : sqllex(YYSTYPE * yylval, void *parm)
1657 : {
1658 27905567 : return scanner(yylval, parm, true);
1659 : }
|