Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 6f7bab35bc1a06b40b0bd33e250a5fbfc146f57f [file] [log] [blame]
drh75897232000-05-29 14:26:001/*
drhb19a2bc2001-09-16 00:13:262** 2001 September 15
drh75897232000-05-29 14:26:003**
drhb19a2bc2001-09-16 00:13:264** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:006**
drhb19a2bc2001-09-16 00:13:267** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:0010**
11*************************************************************************
12** An tokenizer for SQL
13**
14** This file contains C code that splits an SQL input string up into
15** individual tokens and sends those tokens one-by-one over to the
16** parser for analysis.
drh75897232000-05-29 14:26:0017*/
18#include "sqliteInt.h"
drhdcc581c2000-05-30 13:44:1919#include <stdlib.h>
drh75897232000-05-29 14:26:0020
drh34dcee62016-02-08 19:15:4821/* Character classes for tokenizing
22**
23** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
24** using a lookup table, whereas a switch() directly on c uses a binary search.
25** The lookup table is much faster. To maximize speed, and to ensure that
26** a lookup table is used, all of the classes need to be small integers and
27** all of them need to be used within the switch.
28*/
drhe96f3612016-02-08 19:36:4629#define CC_X 0 /* The letter 'x', or start of BLOB literal */
drhd1032f92020-11-27 20:56:1630#define CC_KYWD0 1 /* First letter of a keyword */
31#define CC_KYWD 2 /* Alphabetics or '_'. Usable in a keyword */
drh89743312016-02-08 02:30:5032#define CC_DIGIT 3 /* Digits */
33#define CC_DOLLAR 4 /* '$' */
34#define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */
35#define CC_VARNUM 6 /* '?'. Numeric SQL variables */
36#define CC_SPACE 7 /* Space characters */
37#define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */
38#define CC_QUOTE2 9 /* '['. [...] style quoted ids */
39#define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */
40#define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
41#define CC_LT 12 /* '<'. Part of < or <= or <> */
42#define CC_GT 13 /* '>'. Part of > or >= */
43#define CC_EQ 14 /* '='. Part of = or == */
44#define CC_BANG 15 /* '!'. Part of != */
45#define CC_SLASH 16 /* '/'. / or c-style comment */
46#define CC_LP 17 /* '(' */
47#define CC_RP 18 /* ')' */
48#define CC_SEMI 19 /* ';' */
49#define CC_PLUS 20 /* '+' */
50#define CC_STAR 21 /* '*' */
51#define CC_PERCENT 22 /* '%' */
52#define CC_COMMA 23 /* ',' */
53#define CC_AND 24 /* '&' */
54#define CC_TILDA 25 /* '~' */
55#define CC_DOT 26 /* '.' */
drhd1032f92020-11-27 20:56:1656#define CC_ID 27 /* unicode characters usable in IDs */
57#define CC_ILLEGAL 28 /* Illegal character */
58#define CC_NUL 29 /* 0x00 */
drhba9ebc22021-04-24 12:24:0859#define CC_BOM 30 /* First byte of UTF8 BOM: 0xEF 0xBB 0xBF */
drh89743312016-02-08 02:30:5060
61static const unsigned char aiClass[] = {
drh34dcee62016-02-08 19:15:4862#ifdef SQLITE_ASCII
drh89743312016-02-08 02:30:5063/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
drhd1032f92020-11-27 20:56:1664/* 0x */ 29, 28, 28, 28, 28, 28, 28, 28, 28, 7, 7, 28, 7, 7, 28, 28,
65/* 1x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
drh89743312016-02-08 02:30:5066/* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16,
67/* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6,
68/* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
drhd1032f92020-11-27 20:56:1669/* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 9, 28, 28, 28, 2,
drh89743312016-02-08 02:30:5070/* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
drhd1032f92020-11-27 20:56:1671/* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 28, 10, 28, 25, 28,
drhba9ebc22021-04-24 12:24:0872/* 8x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
73/* 9x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
74/* Ax */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
75/* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
76/* Cx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
77/* Dx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
78/* Ex */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 30,
79/* Fx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27
drh34dcee62016-02-08 19:15:4880#endif
81#ifdef SQLITE_EBCDIC
82/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
drhd1032f92020-11-27 20:56:1683/* 0x */ 29, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 7, 7, 28, 28,
larrybr4cf34a52021-03-19 15:02:5984/* 1x */ 28, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
drhd1032f92020-11-27 20:56:1685/* 2x */ 28, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
86/* 3x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
87/* 4x */ 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 26, 12, 17, 20, 10,
88/* 5x */ 24, 28, 28, 28, 28, 28, 28, 28, 28, 28, 15, 4, 21, 18, 19, 28,
89/* 6x */ 11, 16, 28, 28, 28, 28, 28, 28, 28, 28, 28, 23, 22, 2, 13, 6,
90/* 7x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 8, 5, 5, 5, 8, 14, 8,
91/* 8x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
92/* 9x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
93/* Ax */ 28, 25, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28,
94/* Bx */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 9, 28, 28, 28, 28, 28,
95/* Cx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
96/* Dx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
97/* Ex */ 28, 28, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28,
98/* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 28, 28, 28, 28, 28, 28,
drh34dcee62016-02-08 19:15:4899#endif
drh89743312016-02-08 02:30:50100};
101
drh75897232000-05-29 14:26:00102/*
drh34dcee62016-02-08 19:15:48103** The charMap() macro maps alphabetic characters (only) into their
drh9b8f4472006-04-04 01:54:55104** lower-case ASCII equivalent. On ASCII machines, this is just
105** an upper-to-lower case map. On EBCDIC machines we also need
drh34dcee62016-02-08 19:15:48106** to adjust the encoding. The mapping is only valid for alphabetics
107** which are the only characters for which this feature is used.
108**
109** Used by keywordhash.h
drh9b8f4472006-04-04 01:54:55110*/
111#ifdef SQLITE_ASCII
112# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
113#endif
114#ifdef SQLITE_EBCDIC
115# define charMap(X) ebcdicToAscii[(unsigned char)X]
116const unsigned char ebcdicToAscii[] = {
117/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
118 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
119 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
120 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
122 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
123 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
124 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
125 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
126 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
127 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
128 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
130 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
131 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
132 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
133 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
134};
135#endif
136
137/*
drh52fb6d72004-11-03 03:59:57138** The sqlite3KeywordCode function looks up an identifier to determine if
139** it is a keyword. If it is a keyword, the token code of that keyword is
drh75897232000-05-29 14:26:00140** returned. If the input is not a keyword, TK_ID is returned.
drh2090a0e2004-10-07 19:03:01141**
142** The implementation of this routine was generated by a program,
drhe96f3612016-02-08 19:36:46143** mkkeywordhash.c, located in the tool subdirectory of the distribution.
drh52fb6d72004-11-03 03:59:57144** The output of the mkkeywordhash.c program is written into a file
drh73b211a2005-01-18 04:00:42145** named keywordhash.h and then included into this source file by
drh52fb6d72004-11-03 03:59:57146** the #include below.
drh75897232000-05-29 14:26:00147*/
drh73b211a2005-01-18 04:00:42148#include "keywordhash.h"
drh75897232000-05-29 14:26:00149
drh40f20f72004-10-23 05:10:18150
drh98808ba2001-10-18 12:34:46151/*
drh9b8f4472006-04-04 01:54:55152** If X is a character that can be used in an identifier then
153** IdChar(X) will be true. Otherwise it is false.
drh98808ba2001-10-18 12:34:46154**
drh9b8f4472006-04-04 01:54:55155** For ASCII, any character with the high-order bit set is
156** allowed in an identifier. For 7-bit characters,
157** sqlite3IsIdChar[X] must be 1.
158**
159** For EBCDIC, the rules are more complex but have the same
160** end result.
drha0d1f662005-01-11 17:59:47161**
162** Ticket #1066. the SQL standard does not allow '$' in the
peter.d.reid60ec9142014-09-06 16:39:46163** middle of identifiers. But many SQL implementations do.
drha0d1f662005-01-11 17:59:47164** SQLite will allow '$' in identifiers for compatibility.
165** But the feature is undocumented.
drh98808ba2001-10-18 12:34:46166*/
drh9b8f4472006-04-04 01:54:55167#ifdef SQLITE_ASCII
drhaf2b5722009-11-16 15:11:51168#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
drh9b8f4472006-04-04 01:54:55169#endif
170#ifdef SQLITE_EBCDIC
drh46c99e02007-08-27 23:26:59171const char sqlite3IsEbcdicIdChar[] = {
drh9b8f4472006-04-04 01:54:55172/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
173 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
174 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
175 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
176 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
177 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
178 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
179 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
180 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
181 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
182 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
183 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
184 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
185};
drh46c99e02007-08-27 23:26:59186#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
drh9b8f4472006-04-04 01:54:55187#endif
drhf5ed7ad2015-06-15 14:43:25188
drh38d99642018-08-18 18:27:18189/* Make the IdChar function accessible from ctime.c and alter.c */
drh97348b32014-09-25 02:44:29190int sqlite3IsIdChar(u8 c){ return IdChar(c); }
drh9b8f4472006-04-04 01:54:55191
dan34a7d792018-06-29 20:43:33192#ifndef SQLITE_OMIT_WINDOWFUNC
dan59ff4252018-06-29 17:44:52193/*
194** Return the id of the next token in string (*pz). Before returning, set
195** (*pz) to point to the byte following the parsed token.
dan59ff4252018-06-29 17:44:52196*/
dan6e2210e2018-06-30 18:54:56197static int getToken(const unsigned char **pz){
dan59ff4252018-06-29 17:44:52198 const unsigned char *z = *pz;
dan6e2210e2018-06-30 18:54:56199 int t; /* Token type to return */
200 do {
201 z += sqlite3GetToken(z, &t);
drh08404602025-07-07 02:18:27202 }while( t==TK_SPACE || t==TK_COMMENT );
dan6e2210e2018-06-30 18:54:56203 if( t==TK_ID
204 || t==TK_STRING
205 || t==TK_JOIN_KW
206 || t==TK_WINDOW
207 || t==TK_OVER
208 || sqlite3ParserFallback(t)==TK_ID
209 ){
210 t = TK_ID;
dan59ff4252018-06-29 17:44:52211 }
212 *pz = z;
dan6e2210e2018-06-30 18:54:56213 return t;
dan59ff4252018-06-29 17:44:52214}
215
216/*
dan6e2210e2018-06-30 18:54:56217** The following three functions are called immediately after the tokenizer
218** reads the keywords WINDOW, OVER and FILTER, respectively, to determine
219** whether the token should be treated as a keyword or an SQL identifier.
220** This cannot be handled by the usual lemon %fallback method, due to
221** the ambiguity in some constructions. e.g.
dan59ff4252018-06-29 17:44:52222**
dan6e2210e2018-06-30 18:54:56223** SELECT sum(x) OVER ...
dan59ff4252018-06-29 17:44:52224**
dan6e2210e2018-06-30 18:54:56225** In the above, "OVER" might be a keyword, or it might be an alias for the
226** sum(x) expression. If a "%fallback ID OVER" directive were added to
227** grammar, then SQLite would always treat "OVER" as an alias, making it
228** impossible to call a window-function without a FILTER clause.
229**
230** WINDOW is treated as a keyword if:
231**
232** * the following token is an identifier, or a keyword that can fallback
233** to being an identifier, and
234** * the token after than one is TK_AS.
235**
236** OVER is a keyword if:
237**
238** * the previous token was TK_RP, and
239** * the next token is either TK_LP or an identifier.
240**
241** FILTER is a keyword if:
242**
243** * the previous token was TK_RP, and
244** * the next token is TK_LP.
dan59ff4252018-06-29 17:44:52245*/
dan769309f2018-06-29 19:54:51246static int analyzeWindowKeyword(const unsigned char *z){
dan59ff4252018-06-29 17:44:52247 int t;
dan6e2210e2018-06-30 18:54:56248 t = getToken(&z);
249 if( t!=TK_ID ) return TK_ID;
250 t = getToken(&z);
251 if( t!=TK_AS ) return TK_ID;
252 return TK_WINDOW;
253}
254static int analyzeOverKeyword(const unsigned char *z, int lastToken){
255 if( lastToken==TK_RP ){
256 int t = getToken(&z);
257 if( t==TK_LP || t==TK_ID ) return TK_OVER;
dan59ff4252018-06-29 17:44:52258 }
dan6e2210e2018-06-30 18:54:56259 return TK_ID;
260}
261static int analyzeFilterKeyword(const unsigned char *z, int lastToken){
262 if( lastToken==TK_RP && getToken(&z)==TK_LP ){
263 return TK_FILTER;
264 }
265 return TK_ID;
dan59ff4252018-06-29 17:44:52266}
drhc7bf5712018-07-09 22:49:01267#endif /* SQLITE_OMIT_WINDOWFUNC */
drh98808ba2001-10-18 12:34:46268
drh75897232000-05-29 14:26:00269/*
drhe96f3612016-02-08 19:36:46270** Return the length (in bytes) of the token that begins at z[0].
drh61b487d2003-09-12 02:08:14271** Store the token type in *tokenType before returning.
drh75897232000-05-29 14:26:00272*/
drh35b5a332008-04-05 18:41:42273int sqlite3GetToken(const unsigned char *z, int *tokenType){
drhaa756b02004-09-25 15:25:26274 int i, c;
drhe96f3612016-02-08 19:36:46275 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
276 ** of the token. See the comment on the CC_ defines
277 ** above. */
drh89743312016-02-08 02:30:50278 case CC_SPACE: {
drh19f81f62009-06-09 18:01:37279 testcase( z[0]==' ' );
280 testcase( z[0]=='\t' );
281 testcase( z[0]=='\n' );
282 testcase( z[0]=='\f' );
283 testcase( z[0]=='\r' );
danielk197778ca0e72009-01-20 16:53:39284 for(i=1; sqlite3Isspace(z[i]); i++){}
drh75897232000-05-29 14:26:00285 *tokenType = TK_SPACE;
286 return i;
287 }
drh89743312016-02-08 02:30:50288 case CC_MINUS: {
drh75897232000-05-29 14:26:00289 if( z[1]=='-' ){
drhaa756b02004-09-25 15:25:26290 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
drhe16b3452025-01-31 01:34:19291 *tokenType = TK_COMMENT;
drh75897232000-05-29 14:26:00292 return i;
drhd5326c32022-01-07 14:58:47293 }else if( z[1]=='>' ){
294 *tokenType = TK_PTR;
295 return 2 + (z[2]=='>');
drh75897232000-05-29 14:26:00296 }
297 *tokenType = TK_MINUS;
298 return 1;
299 }
drh89743312016-02-08 02:30:50300 case CC_LP: {
drhdab35182003-09-27 13:39:38301 *tokenType = TK_LP;
302 return 1;
drh75897232000-05-29 14:26:00303 }
drh89743312016-02-08 02:30:50304 case CC_RP: {
drh75897232000-05-29 14:26:00305 *tokenType = TK_RP;
306 return 1;
307 }
drh89743312016-02-08 02:30:50308 case CC_SEMI: {
drh75897232000-05-29 14:26:00309 *tokenType = TK_SEMI;
310 return 1;
311 }
drh89743312016-02-08 02:30:50312 case CC_PLUS: {
drh75897232000-05-29 14:26:00313 *tokenType = TK_PLUS;
314 return 1;
315 }
drh89743312016-02-08 02:30:50316 case CC_STAR: {
drh75897232000-05-29 14:26:00317 *tokenType = TK_STAR;
318 return 1;
319 }
drh89743312016-02-08 02:30:50320 case CC_SLASH: {
drh66105a82002-08-27 14:28:29321 if( z[1]!='*' || z[2]==0 ){
322 *tokenType = TK_SLASH;
323 return 1;
324 }
drhaa756b02004-09-25 15:25:26325 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
326 if( c ) i++;
drhe16b3452025-01-31 01:34:19327 *tokenType = TK_COMMENT;
drh66105a82002-08-27 14:28:29328 return i;
drh75897232000-05-29 14:26:00329 }
drh89743312016-02-08 02:30:50330 case CC_PERCENT: {
drhbf4133c2001-10-13 02:59:08331 *tokenType = TK_REM;
332 return 1;
333 }
drh89743312016-02-08 02:30:50334 case CC_EQ: {
drh75897232000-05-29 14:26:00335 *tokenType = TK_EQ;
336 return 1 + (z[1]=='=');
337 }
drh89743312016-02-08 02:30:50338 case CC_LT: {
drhaa756b02004-09-25 15:25:26339 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00340 *tokenType = TK_LE;
341 return 2;
drhaa756b02004-09-25 15:25:26342 }else if( c=='>' ){
drh75897232000-05-29 14:26:00343 *tokenType = TK_NE;
344 return 2;
drhaa756b02004-09-25 15:25:26345 }else if( c=='<' ){
drhbf4133c2001-10-13 02:59:08346 *tokenType = TK_LSHIFT;
347 return 2;
drh75897232000-05-29 14:26:00348 }else{
349 *tokenType = TK_LT;
350 return 1;
351 }
352 }
drh89743312016-02-08 02:30:50353 case CC_GT: {
drhaa756b02004-09-25 15:25:26354 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00355 *tokenType = TK_GE;
356 return 2;
drhaa756b02004-09-25 15:25:26357 }else if( c=='>' ){
drhbf4133c2001-10-13 02:59:08358 *tokenType = TK_RSHIFT;
359 return 2;
drh75897232000-05-29 14:26:00360 }else{
361 *tokenType = TK_GT;
362 return 1;
363 }
364 }
drh89743312016-02-08 02:30:50365 case CC_BANG: {
drh75897232000-05-29 14:26:00366 if( z[1]!='=' ){
367 *tokenType = TK_ILLEGAL;
drhb2bddbb2016-02-18 14:49:28368 return 1;
drh75897232000-05-29 14:26:00369 }else{
370 *tokenType = TK_NE;
371 return 2;
372 }
373 }
drh89743312016-02-08 02:30:50374 case CC_PIPE: {
drh00400772000-06-16 20:51:26375 if( z[1]!='|' ){
drhbf4133c2001-10-13 02:59:08376 *tokenType = TK_BITOR;
drh00400772000-06-16 20:51:26377 return 1;
378 }else{
379 *tokenType = TK_CONCAT;
380 return 2;
381 }
382 }
drh89743312016-02-08 02:30:50383 case CC_COMMA: {
drh75897232000-05-29 14:26:00384 *tokenType = TK_COMMA;
385 return 1;
386 }
drh89743312016-02-08 02:30:50387 case CC_AND: {
drhbf4133c2001-10-13 02:59:08388 *tokenType = TK_BITAND;
389 return 1;
390 }
drh89743312016-02-08 02:30:50391 case CC_TILDA: {
drhbf4133c2001-10-13 02:59:08392 *tokenType = TK_BITNOT;
393 return 1;
394 }
drh89743312016-02-08 02:30:50395 case CC_QUOTE: {
drh75897232000-05-29 14:26:00396 int delim = z[0];
drh19f81f62009-06-09 18:01:37397 testcase( delim=='`' );
398 testcase( delim=='\'' );
399 testcase( delim=='"' );
drhaa756b02004-09-25 15:25:26400 for(i=1; (c=z[i])!=0; i++){
401 if( c==delim ){
drh75897232000-05-29 14:26:00402 if( z[i+1]==delim ){
403 i++;
404 }else{
405 break;
406 }
407 }
408 }
drha33cb5f2008-08-07 13:05:34409 if( c=='\'' ){
drheef8b552005-10-23 11:29:40410 *tokenType = TK_STRING;
411 return i+1;
drha33cb5f2008-08-07 13:05:34412 }else if( c!=0 ){
413 *tokenType = TK_ID;
414 return i+1;
drheef8b552005-10-23 11:29:40415 }else{
416 *tokenType = TK_ILLEGAL;
417 return i;
418 }
drh75897232000-05-29 14:26:00419 }
drh89743312016-02-08 02:30:50420 case CC_DOT: {
drh76816182005-08-23 11:31:26421#ifndef SQLITE_OMIT_FLOATING_POINT
danielk197778ca0e72009-01-20 16:53:39422 if( !sqlite3Isdigit(z[1]) )
drh76816182005-08-23 11:31:26423#endif
424 {
425 *tokenType = TK_DOT;
426 return 1;
427 }
428 /* If the next character is a digit, this is a floating point
429 ** number that begins with ".". Fall thru into the next case */
drh08b92082020-08-10 14:18:00430 /* no break */ deliberate_fall_through
drh75897232000-05-29 14:26:00431 }
drh89743312016-02-08 02:30:50432 case CC_DIGIT: {
drh19f81f62009-06-09 18:01:37433 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
434 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
435 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
drh89e1caf2023-08-10 18:50:00436 testcase( z[0]=='9' ); testcase( z[0]=='.' );
drhc837e702000-06-08 16:26:24437 *tokenType = TK_INTEGER;
drh28e048c2014-07-23 01:26:51438#ifndef SQLITE_OMIT_HEX_INTEGER
439 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
dan8374f7d2024-01-22 19:38:55440 for(i=3; 1; i++){
441 if( sqlite3Isxdigit(z[i])==0 ){
dan406eb5a2024-01-23 11:20:58442 if( z[i]==SQLITE_DIGIT_SEPARATOR ){
dan8374f7d2024-01-22 19:38:55443 *tokenType = TK_QNUMBER;
444 }else{
445 break;
446 }
drh87ad06e2024-01-22 17:18:41447 }
dan3eae6662024-01-20 16:18:04448 }
dan8374f7d2024-01-22 19:38:55449 }else
450#endif
451 {
452 for(i=0; 1; i++){
453 if( sqlite3Isdigit(z[i])==0 ){
dan406eb5a2024-01-23 11:20:58454 if( z[i]==SQLITE_DIGIT_SEPARATOR ){
dan8374f7d2024-01-22 19:38:55455 *tokenType = TK_QNUMBER;
456 }else{
457 break;
458 }
459 }
460 }
drhb7f91642004-10-31 02:22:47461#ifndef SQLITE_OMIT_FLOATING_POINT
dan8374f7d2024-01-22 19:38:55462 if( z[i]=='.' ){
463 if( *tokenType==TK_INTEGER ) *tokenType = TK_FLOAT;
464 for(i++; 1; i++){
465 if( sqlite3Isdigit(z[i])==0 ){
dan406eb5a2024-01-23 11:20:58466 if( z[i]==SQLITE_DIGIT_SEPARATOR ){
dan8374f7d2024-01-22 19:38:55467 *tokenType = TK_QNUMBER;
468 }else{
469 break;
470 }
drh87ad06e2024-01-22 17:18:41471 }
dan3eae6662024-01-20 16:18:04472 }
473 }
dan8374f7d2024-01-22 19:38:55474 if( (z[i]=='e' || z[i]=='E') &&
475 ( sqlite3Isdigit(z[i+1])
476 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
477 )
478 ){
479 if( *tokenType==TK_INTEGER ) *tokenType = TK_FLOAT;
480 for(i+=2; 1; i++){
481 if( sqlite3Isdigit(z[i])==0 ){
dan406eb5a2024-01-23 11:20:58482 if( z[i]==SQLITE_DIGIT_SEPARATOR ){
dan8374f7d2024-01-22 19:38:55483 *tokenType = TK_QNUMBER;
484 }else{
485 break;
486 }
danfbb72fa2024-01-22 19:00:50487 }
dan3eae6662024-01-20 16:18:04488 }
489 }
drhb7f91642004-10-31 02:22:47490#endif
dan8374f7d2024-01-22 19:38:55491 }
drh67dd9012006-08-12 12:33:14492 while( IdChar(z[i]) ){
493 *tokenType = TK_ILLEGAL;
494 i++;
495 }
drh75897232000-05-29 14:26:00496 return i;
497 }
drh89743312016-02-08 02:30:50498 case CC_QUOTE2: {
drhaa756b02004-09-25 15:25:26499 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
drh4b2f9362008-01-22 23:37:09500 *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
drh2f4392f2002-02-14 21:42:51501 return i;
502 }
drh89743312016-02-08 02:30:50503 case CC_VARNUM: {
drh50457892003-09-06 01:10:47504 *tokenType = TK_VARIABLE;
danielk197778ca0e72009-01-20 16:53:39505 for(i=1; sqlite3Isdigit(z[i]); i++){}
drhfa6bc002004-09-07 16:19:52506 return i;
drh50457892003-09-06 01:10:47507 }
drh89743312016-02-08 02:30:50508 case CC_DOLLAR:
509 case CC_VARALPHA: {
drh288d37f2005-06-22 08:48:06510 int n = 0;
drhf59b12f2014-01-11 03:54:05511 testcase( z[0]=='$' ); testcase( z[0]=='@' );
512 testcase( z[0]==':' ); testcase( z[0]=='#' );
drh9d74b4c2004-08-24 15:23:34513 *tokenType = TK_VARIABLE;
drh288d37f2005-06-22 08:48:06514 for(i=1; (c=z[i])!=0; i++){
515 if( IdChar(c) ){
516 n++;
517#ifndef SQLITE_OMIT_TCL_VARIABLE
518 }else if( c=='(' && n>0 ){
519 do{
520 i++;
danielk197778ca0e72009-01-20 16:53:39521 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
drh288d37f2005-06-22 08:48:06522 if( c==')' ){
drh895d7472004-08-20 16:02:39523 i++;
524 }else{
drh288d37f2005-06-22 08:48:06525 *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39526 }
drh288d37f2005-06-22 08:48:06527 break;
528 }else if( c==':' && z[i+1]==':' ){
529 i++;
530#endif
531 }else{
532 break;
drh895d7472004-08-20 16:02:39533 }
534 }
drh288d37f2005-06-22 08:48:06535 if( n==0 ) *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39536 return i;
drhb7f91642004-10-31 02:22:47537 }
drhd1032f92020-11-27 20:56:16538 case CC_KYWD0: {
drh1e24dc92023-06-19 20:54:49539 if( aiClass[z[1]]>CC_KYWD ){ i = 1; break; }
540 for(i=2; aiClass[z[i]]<=CC_KYWD; i++){}
drhe96f3612016-02-08 19:36:46541 if( IdChar(z[i]) ){
542 /* This token started out using characters that can appear in keywords,
543 ** but z[i] is a character not allowed within keywords, so this must
544 ** be an identifier instead */
545 i++;
546 break;
547 }
548 *tokenType = TK_ID;
dan769309f2018-06-29 19:54:51549 return keywordCode((char*)z, i, tokenType);
drhe96f3612016-02-08 19:36:46550 }
drh89743312016-02-08 02:30:50551 case CC_X: {
dana73086d2016-02-24 16:14:07552#ifndef SQLITE_OMIT_BLOB_LITERAL
drh19f81f62009-06-09 18:01:37553 testcase( z[0]=='x' ); testcase( z[0]=='X' );
drh4b2f9362008-01-22 23:37:09554 if( z[1]=='\'' ){
danielk19773fd0a732004-05-27 13:35:19555 *tokenType = TK_BLOB;
drh3a61a5a2011-06-03 21:34:45556 for(i=2; sqlite3Isxdigit(z[i]); i++){}
557 if( z[i]!='\'' || i%2 ){
558 *tokenType = TK_ILLEGAL;
559 while( z[i] && z[i]!='\'' ){ i++; }
danielk1977c572ef72004-05-27 09:28:41560 }
drh3a61a5a2011-06-03 21:34:45561 if( z[i] ) i++;
danielk1977c572ef72004-05-27 09:28:41562 return i;
563 }
dana73086d2016-02-24 16:14:07564#endif
drhe96f3612016-02-08 19:36:46565 /* If it is not a BLOB literal, then it must be an ID, since no
566 ** SQL keywords start with the letter 'x'. Fall through */
drh08b92082020-08-10 14:18:00567 /* no break */ deliberate_fall_through
danielk1977c572ef72004-05-27 09:28:41568 }
drhd1032f92020-11-27 20:56:16569 case CC_KYWD:
drh89743312016-02-08 02:30:50570 case CC_ID: {
571 i = 1;
572 break;
573 }
drhba9ebc22021-04-24 12:24:08574 case CC_BOM: {
575 if( z[1]==0xbb && z[2]==0xbf ){
576 *tokenType = TK_SPACE;
577 return 3;
578 }
579 i = 1;
580 break;
581 }
dan2b5f1522018-06-30 20:00:35582 case CC_NUL: {
583 *tokenType = TK_ILLEGAL;
584 return 0;
585 }
drh98808ba2001-10-18 12:34:46586 default: {
drh89743312016-02-08 02:30:50587 *tokenType = TK_ILLEGAL;
588 return 1;
drh75897232000-05-29 14:26:00589 }
drh75897232000-05-29 14:26:00590 }
drh34dcee62016-02-08 19:15:48591 while( IdChar(z[i]) ){ i++; }
drh89743312016-02-08 02:30:50592 *tokenType = TK_ID;
593 return i;
drh75897232000-05-29 14:26:00594}
595
596/*
drh54bc6382021-12-31 19:20:42597** Run the parser on the given SQL string.
drh75897232000-05-29 14:26:00598*/
drh54bc6382021-12-31 19:20:42599int sqlite3RunParser(Parse *pParse, const char *zSql){
drhd9da78a2009-03-24 15:08:09600 int nErr = 0; /* Number of errors encountered */
drhd9da78a2009-03-24 15:08:09601 void *pEngine; /* The LEMON-generated LALR(1) parser */
drh9b38d662017-03-07 14:38:52602 int n = 0; /* Length of the next token token */
drhd9da78a2009-03-24 15:08:09603 int tokenType; /* type of the next token */
604 int lastTokenParsed = -1; /* type of the previous token */
drhd9da78a2009-03-24 15:08:09605 sqlite3 *db = pParse->db; /* The database connection */
606 int mxSqlLen; /* Max length of an SQL string */
dan488b5582021-11-16 13:36:50607 Parse *pParentParse = 0; /* Outer parse context, if any */
drhd26cc542017-01-28 20:46:37608#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
drh53b24592017-03-30 17:13:37609 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
drhd26cc542017-01-28 20:46:37610#endif
drh2b454e02019-02-26 16:11:46611 VVA_ONLY( u8 startedWithOom = db->mallocFailed );
drh75897232000-05-29 14:26:00612
drh96c707a2015-02-13 16:36:14613 assert( zSql!=0 );
drhd9da78a2009-03-24 15:08:09614 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
drh4f7d3a52013-06-27 23:54:02615 if( db->nVdbeActive==0 ){
dan892edb62020-03-30 13:35:05616 AtomicStore(&db->u1.isInterrupted, 0);
drh15ca1df2006-07-26 13:43:30617 }
drh4c504392000-10-16 22:06:40618 pParse->rc = SQLITE_OK;
drhe7f3f3e2009-07-03 22:54:36619 pParse->zTail = zSql;
drh9b747062019-01-31 01:39:01620#ifdef SQLITE_DEBUG
621 if( db->flags & SQLITE_ParserTrace ){
622 printf("parser: [[[%s]]]\n", zSql);
623 sqlite3ParserTrace(stdout, "parser: ");
624 }else{
625 sqlite3ParserTrace(0, 0);
626 }
627#endif
drhd26cc542017-01-28 20:46:37628#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
drh53b24592017-03-30 17:13:37629 pEngine = &sEngine;
drhfb32c442018-04-21 13:51:42630 sqlite3ParserInit(pEngine, pParse);
drhd26cc542017-01-28 20:46:37631#else
drhfb32c442018-04-21 13:51:42632 pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse);
drh75897232000-05-29 14:26:00633 if( pEngine==0 ){
drh4a642b62016-02-05 01:55:27634 sqlite3OomFault(db);
mistachkinfad30392016-02-13 23:43:46635 return SQLITE_NOMEM_BKPT;
drh75897232000-05-29 14:26:00636 }
drhd26cc542017-01-28 20:46:37637#endif
drhfa6bc002004-09-07 16:19:52638 assert( pParse->pNewTable==0 );
639 assert( pParse->pNewTrigger==0 );
640 assert( pParse->nVar==0 );
drh9bf755c2016-12-23 03:59:31641 assert( pParse->pVList==0 );
dan488b5582021-11-16 13:36:50642 pParentParse = db->pParse;
drh1cf19752019-02-08 14:55:30643 db->pParse = pParse;
drh167fbbe2016-08-10 14:40:00644 while( 1 ){
dand437ac02018-06-29 20:21:24645 n = sqlite3GetToken((u8*)zSql, &tokenType);
646 mxSqlLen -= n;
647 if( mxSqlLen<0 ){
648 pParse->rc = SQLITE_TOOBIG;
drhcd9e8632022-07-11 14:36:03649 pParse->nErr++;
dand437ac02018-06-29 20:21:24650 break;
drhe5c941b2007-05-08 13:58:26651 }
dan34a7d792018-06-29 20:43:33652#ifndef SQLITE_OMIT_WINDOWFUNC
653 if( tokenType>=TK_WINDOW ){
dan6e2210e2018-06-30 18:54:56654 assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER
dan34a7d792018-06-29 20:43:33655 || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW
drhe16b3452025-01-31 01:34:19656 || tokenType==TK_QNUMBER || tokenType==TK_COMMENT
dan34a7d792018-06-29 20:43:33657 );
658#else
drh0739e452015-11-09 02:08:09659 if( tokenType>=TK_SPACE ){
dan95295a72024-01-20 16:46:25660 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL
drhe16b3452025-01-31 01:34:19661 || tokenType==TK_QNUMBER || tokenType==TK_COMMENT
dan95295a72024-01-20 16:46:25662 );
drhc7bf5712018-07-09 22:49:01663#endif /* SQLITE_OMIT_WINDOWFUNC */
dan892edb62020-03-30 13:35:05664 if( AtomicLoad(&db->u1.isInterrupted) ){
drh0739e452015-11-09 02:08:09665 pParse->rc = SQLITE_INTERRUPT;
drh54bc6382021-12-31 19:20:42666 pParse->nErr++;
drh75897232000-05-29 14:26:00667 break;
668 }
dand437ac02018-06-29 20:21:24669 if( tokenType==TK_SPACE ){
670 zSql += n;
671 continue;
672 }
673 if( zSql[0]==0 ){
674 /* Upon reaching the end of input, call the parser two more times
675 ** with tokens TK_SEMI and 0, in that order. */
676 if( lastTokenParsed==TK_SEMI ){
677 tokenType = 0;
678 }else if( lastTokenParsed==0 ){
679 break;
680 }else{
681 tokenType = TK_SEMI;
682 }
683 n = 0;
dan34a7d792018-06-29 20:43:33684#ifndef SQLITE_OMIT_WINDOWFUNC
685 }else if( tokenType==TK_WINDOW ){
dan6e2210e2018-06-30 18:54:56686 assert( n==6 );
dan34a7d792018-06-29 20:43:33687 tokenType = analyzeWindowKeyword((const u8*)&zSql[6]);
dan6e2210e2018-06-30 18:54:56688 }else if( tokenType==TK_OVER ){
689 assert( n==4 );
690 tokenType = analyzeOverKeyword((const u8*)&zSql[4], lastTokenParsed);
691 }else if( tokenType==TK_FILTER ){
692 assert( n==6 );
693 tokenType = analyzeFilterKeyword((const u8*)&zSql[6], lastTokenParsed);
drhc7bf5712018-07-09 22:49:01694#endif /* SQLITE_OMIT_WINDOWFUNC */
drhbee4fb42025-03-05 18:18:17695 }else if( tokenType==TK_COMMENT
696 && (db->init.busy || (db->flags & SQLITE_Comments)!=0)
697 ){
698 /* Ignore SQL comments if either (1) we are reparsing the schema or
699 ** (2) SQLITE_DBCONFIG_ENABLE_COMMENTS is turned on (the default). */
drhe16b3452025-01-31 01:34:19700 zSql += n;
701 continue;
dan95295a72024-01-20 16:46:25702 }else if( tokenType!=TK_QNUMBER ){
drh4e532952022-02-08 13:41:23703 Token x;
704 x.z = zSql;
705 x.n = n;
706 sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"", &x);
drh75897232000-05-29 14:26:00707 break;
drh32eb7b42003-01-07 01:44:37708 }
drh75897232000-05-29 14:26:00709 }
dand437ac02018-06-29 20:21:24710 pParse->sLastToken.z = zSql;
711 pParse->sLastToken.n = n;
712 sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
713 lastTokenParsed = tokenType;
714 zSql += n;
drh1cf19752019-02-08 14:55:30715 assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom );
716 if( pParse->rc!=SQLITE_OK ) break;
drh75897232000-05-29 14:26:00717 }
drh0be0cf62015-04-15 08:37:42718 assert( nErr==0 );
drhec424a52008-07-25 15:39:03719#ifdef YYTRACKMAXSTACKDEPTH
drhaf89fe62015-03-23 17:25:18720 sqlite3_mutex_enter(sqlite3MallocMutex());
drhb02392e2015-10-15 15:28:56721 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
drhec424a52008-07-25 15:39:03722 sqlite3ParserStackPeak(pEngine)
723 );
drhaf89fe62015-03-23 17:25:18724 sqlite3_mutex_leave(sqlite3MallocMutex());
drhec424a52008-07-25 15:39:03725#endif /* YYDEBUG */
drhd26cc542017-01-28 20:46:37726#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
727 sqlite3ParserFinalize(pEngine);
728#else
drh17435752007-08-16 04:30:38729 sqlite3ParserFree(pEngine, sqlite3_free);
drhd26cc542017-01-28 20:46:37730#endif
drh17435752007-08-16 04:30:38731 if( db->mallocFailed ){
mistachkinfad30392016-02-13 23:43:46732 pParse->rc = SQLITE_NOMEM_BKPT;
drh71c697e2004-08-08 23:39:19733 }
drh16118262021-12-31 17:54:48734 if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE) ){
735 if( pParse->zErrMsg==0 ){
736 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
737 }
drhef636cc2024-12-06 18:35:16738 if( (pParse->prepFlags & SQLITE_PREPARE_DONT_LOG)==0 ){
739 sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail);
740 }
drh4b2f9362008-01-22 23:37:09741 nErr++;
drh75897232000-05-29 14:26:00742 }
drh875ad8c2018-06-21 23:53:54743 pParse->zTail = zSql;
drh4f3dd152008-04-28 18:46:43744#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9755982010-07-24 16:34:37745 sqlite3_free(pParse->apVtabLock);
drh4f3dd152008-04-28 18:46:43746#endif
danielk19777e6ebfb2006-06-12 11:24:37747
drh16118262021-12-31 17:54:48748 if( pParse->pNewTable && !IN_SPECIAL_PARSE ){
danielk19777e6ebfb2006-06-12 11:24:37749 /* If the pParse->declareVtab flag is set, do not delete any table
750 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
751 ** will take responsibility for freeing the Table structure.
752 */
dan1feeaed2010-07-23 15:41:47753 sqlite3DeleteTable(db, pParse->pNewTable);
danielk19777e6ebfb2006-06-12 11:24:37754 }
drh16118262021-12-31 17:54:48755 if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26756 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
757 }
drh41ce47c2022-08-22 02:00:26758 if( pParse->pVList ) sqlite3DbNNFreeNN(db, pParse->pVList);
dan488b5582021-11-16 13:36:50759 db->pParse = pParentParse;
drhf3151f02015-04-16 20:27:09760 assert( nErr==0 || pParse->rc!=SQLITE_OK );
drh75897232000-05-29 14:26:00761 return nErr;
762}
drh643d8552018-12-10 16:00:57763
764
765#ifdef SQLITE_ENABLE_NORMALIZE
766/*
767** Insert a single space character into pStr if the current string
768** ends with an identifier
769*/
770static void addSpaceSeparator(sqlite3_str *pStr){
771 if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){
772 sqlite3_str_append(pStr, " ", 1);
773 }
774}
775
776/*
777** Compute a normalization of the SQL given by zSql[0..nSql-1]. Return
778** the normalization in space obtained from sqlite3DbMalloc(). Or return
779** NULL if anything goes wrong or if zSql is NULL.
780*/
781char *sqlite3Normalize(
782 Vdbe *pVdbe, /* VM being reprepared */
drh1a6c2b12018-12-10 20:01:40783 const char *zSql /* The original SQL string */
drh643d8552018-12-10 16:00:57784){
785 sqlite3 *db; /* The database connection */
786 int i; /* Next unread byte of zSql[] */
787 int n; /* length of current token */
788 int tokenType; /* type of current token */
mistachkin844b9002019-02-02 01:27:45789 int prevType = 0; /* Previous non-whitespace token */
drh643d8552018-12-10 16:00:57790 int nParen; /* Number of nested levels of parentheses */
791 int iStartIN; /* Start of RHS of IN operator in z[] */
792 int nParenAtIN; /* Value of nParent at start of RHS of IN operator */
mistachkin16fd04c2019-10-16 17:46:22793 u32 j; /* Bytes of normalized SQL generated so far */
drh643d8552018-12-10 16:00:57794 sqlite3_str *pStr; /* The normalized SQL string under construction */
795
drh643d8552018-12-10 16:00:57796 db = sqlite3VdbeDb(pVdbe);
797 tokenType = -1;
798 nParen = iStartIN = nParenAtIN = 0;
799 pStr = sqlite3_str_new(db);
drh1a6c2b12018-12-10 20:01:40800 assert( pStr!=0 ); /* sqlite3_str_new() never returns NULL */
801 for(i=0; zSql[i] && pStr->accError==0; i+=n){
drh643d8552018-12-10 16:00:57802 if( tokenType!=TK_SPACE ){
803 prevType = tokenType;
804 }
805 n = sqlite3GetToken((unsigned char*)zSql+i, &tokenType);
806 if( NEVER(n<=0) ) break;
807 switch( tokenType ){
drhde506172025-02-01 20:53:17808 case TK_COMMENT:
drh643d8552018-12-10 16:00:57809 case TK_SPACE: {
810 break;
811 }
812 case TK_NULL: {
813 if( prevType==TK_IS || prevType==TK_NOT ){
814 sqlite3_str_append(pStr, " NULL", 5);
815 break;
816 }
817 /* Fall through */
818 }
819 case TK_STRING:
820 case TK_INTEGER:
821 case TK_FLOAT:
822 case TK_VARIABLE:
823 case TK_BLOB: {
824 sqlite3_str_append(pStr, "?", 1);
825 break;
826 }
827 case TK_LP: {
828 nParen++;
829 if( prevType==TK_IN ){
830 iStartIN = pStr->nChar;
831 nParenAtIN = nParen;
832 }
833 sqlite3_str_append(pStr, "(", 1);
834 break;
835 }
836 case TK_RP: {
837 if( iStartIN>0 && nParen==nParenAtIN ){
mistachkin16fd04c2019-10-16 17:46:22838 assert( pStr->nChar>=(u32)iStartIN );
drh643d8552018-12-10 16:00:57839 pStr->nChar = iStartIN+1;
840 sqlite3_str_append(pStr, "?,?,?", 5);
841 iStartIN = 0;
842 }
843 nParen--;
844 sqlite3_str_append(pStr, ")", 1);
845 break;
846 }
847 case TK_ID: {
drh9042ff22018-12-10 16:49:33848 iStartIN = 0;
drh643d8552018-12-10 16:00:57849 j = pStr->nChar;
850 if( sqlite3Isquote(zSql[i]) ){
851 char *zId = sqlite3DbStrNDup(db, zSql+i, n);
852 int nId;
853 int eType = 0;
854 if( zId==0 ) break;
855 sqlite3Dequote(zId);
856 if( zSql[i]=='"' && sqlite3VdbeUsesDoubleQuotedString(pVdbe, zId) ){
857 sqlite3_str_append(pStr, "?", 1);
858 sqlite3DbFree(db, zId);
859 break;
860 }
861 nId = sqlite3Strlen30(zId);
862 if( sqlite3GetToken((u8*)zId, &eType)==nId && eType==TK_ID ){
863 addSpaceSeparator(pStr);
864 sqlite3_str_append(pStr, zId, nId);
865 }else{
866 sqlite3_str_appendf(pStr, "\"%w\"", zId);
867 }
868 sqlite3DbFree(db, zId);
869 }else{
870 addSpaceSeparator(pStr);
871 sqlite3_str_append(pStr, zSql+i, n);
872 }
873 while( j<pStr->nChar ){
874 pStr->zText[j] = sqlite3Tolower(pStr->zText[j]);
875 j++;
876 }
877 break;
878 }
drh9042ff22018-12-10 16:49:33879 case TK_SELECT: {
880 iStartIN = 0;
881 /* fall through */
882 }
drh643d8552018-12-10 16:00:57883 default: {
884 if( sqlite3IsIdChar(zSql[i]) ) addSpaceSeparator(pStr);
885 j = pStr->nChar;
886 sqlite3_str_append(pStr, zSql+i, n);
887 while( j<pStr->nChar ){
888 pStr->zText[j] = sqlite3Toupper(pStr->zText[j]);
889 j++;
890 }
891 break;
892 }
893 }
894 }
drh9042ff22018-12-10 16:49:33895 if( tokenType!=TK_SEMI ) sqlite3_str_append(pStr, ";", 1);
drh643d8552018-12-10 16:00:57896 return sqlite3_str_finish(pStr);
897}
898#endif /* SQLITE_ENABLE_NORMALIZE */