blob: 05ca86e74fe67a40ab82454793d2a95774fb3aef [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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:00 +000010**
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:00 +000017*/
18#include "sqliteInt.h"
drhdcc581c2000-05-30 13:44:19 +000019#include <stdlib.h>
drh75897232000-05-29 14:26:00 +000020
drh34dcee62016-02-08 19:15:48 +000021/* 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:46 +000029#define CC_X 0 /* The letter 'x', or start of BLOB literal */
drh89743312016-02-08 02:30:50 +000030#define CC_KYWD 1 /* Alphabetics or '_'. Usable in a keyword */
31#define CC_ID 2 /* unicode characters usable in IDs */
32#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 /* '.' */
56#define CC_ILLEGAL 27 /* Illegal character */
dan2b5f1522018-06-30 20:00:35 +000057#define CC_NUL 28 /* 0x00 */
drh89743312016-02-08 02:30:50 +000058
59static const unsigned char aiClass[] = {
drh34dcee62016-02-08 19:15:48 +000060#ifdef SQLITE_ASCII
drh89743312016-02-08 02:30:50 +000061/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
dan2b5f1522018-06-30 20:00:35 +000062/* 0x */ 28, 27, 27, 27, 27, 27, 27, 27, 27, 7, 7, 27, 7, 7, 27, 27,
drh89743312016-02-08 02:30:50 +000063/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
64/* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16,
65/* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6,
66/* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
67/* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1,
68/* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
69/* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 10, 27, 25, 27,
70/* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
71/* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
72/* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
73/* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
74/* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
75/* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
76/* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
77/* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
drh34dcee62016-02-08 19:15:48 +000078#endif
79#ifdef SQLITE_EBCDIC
80/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
81/* 0x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 7, 7, 27, 27,
82/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
83/* 2x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
84/* 3x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
drh585a5972016-12-12 01:53:36 +000085/* 4x */ 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 26, 12, 17, 20, 10,
drh34dcee62016-02-08 19:15:48 +000086/* 5x */ 24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15, 4, 21, 18, 19, 27,
drh585a5972016-12-12 01:53:36 +000087/* 6x */ 11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22, 1, 13, 6,
drh34dcee62016-02-08 19:15:48 +000088/* 7x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 5, 5, 5, 8, 14, 8,
89/* 8x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
90/* 9x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
drh585a5972016-12-12 01:53:36 +000091/* Ax */ 27, 25, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
drh34dcee62016-02-08 19:15:48 +000092/* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 9, 27, 27, 27, 27, 27,
93/* Cx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
94/* Dx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
95/* Ex */ 27, 27, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
96/* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 27, 27, 27, 27, 27, 27,
97#endif
drh89743312016-02-08 02:30:50 +000098};
99
drh75897232000-05-29 14:26:00 +0000100/*
drh34dcee62016-02-08 19:15:48 +0000101** The charMap() macro maps alphabetic characters (only) into their
drh9b8f4472006-04-04 01:54:55 +0000102** lower-case ASCII equivalent. On ASCII machines, this is just
103** an upper-to-lower case map. On EBCDIC machines we also need
drh34dcee62016-02-08 19:15:48 +0000104** to adjust the encoding. The mapping is only valid for alphabetics
105** which are the only characters for which this feature is used.
106**
107** Used by keywordhash.h
drh9b8f4472006-04-04 01:54:55 +0000108*/
109#ifdef SQLITE_ASCII
110# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
111#endif
112#ifdef SQLITE_EBCDIC
113# define charMap(X) ebcdicToAscii[(unsigned char)X]
114const unsigned char ebcdicToAscii[] = {
115/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
116 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
117 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
118 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
119 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
120 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
122 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
123 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
124 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
125 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
126 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
127 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
128 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
129 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
130 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
131 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
132};
133#endif
134
135/*
drh52fb6d72004-11-03 03:59:57 +0000136** The sqlite3KeywordCode function looks up an identifier to determine if
137** it is a keyword. If it is a keyword, the token code of that keyword is
drh75897232000-05-29 14:26:00 +0000138** returned. If the input is not a keyword, TK_ID is returned.
drh2090a0e2004-10-07 19:03:01 +0000139**
140** The implementation of this routine was generated by a program,
drhe96f3612016-02-08 19:36:46 +0000141** mkkeywordhash.c, located in the tool subdirectory of the distribution.
drh52fb6d72004-11-03 03:59:57 +0000142** The output of the mkkeywordhash.c program is written into a file
drh73b211a2005-01-18 04:00:42 +0000143** named keywordhash.h and then included into this source file by
drh52fb6d72004-11-03 03:59:57 +0000144** the #include below.
drh75897232000-05-29 14:26:00 +0000145*/
drh73b211a2005-01-18 04:00:42 +0000146#include "keywordhash.h"
drh75897232000-05-29 14:26:00 +0000147
drh40f20f72004-10-23 05:10:18 +0000148
drh98808ba2001-10-18 12:34:46 +0000149/*
drh9b8f4472006-04-04 01:54:55 +0000150** If X is a character that can be used in an identifier then
151** IdChar(X) will be true. Otherwise it is false.
drh98808ba2001-10-18 12:34:46 +0000152**
drh9b8f4472006-04-04 01:54:55 +0000153** For ASCII, any character with the high-order bit set is
154** allowed in an identifier. For 7-bit characters,
155** sqlite3IsIdChar[X] must be 1.
156**
157** For EBCDIC, the rules are more complex but have the same
158** end result.
drha0d1f662005-01-11 17:59:47 +0000159**
160** Ticket #1066. the SQL standard does not allow '$' in the
peter.d.reid60ec9142014-09-06 16:39:46 +0000161** middle of identifiers. But many SQL implementations do.
drha0d1f662005-01-11 17:59:47 +0000162** SQLite will allow '$' in identifiers for compatibility.
163** But the feature is undocumented.
drh98808ba2001-10-18 12:34:46 +0000164*/
drh9b8f4472006-04-04 01:54:55 +0000165#ifdef SQLITE_ASCII
drhaf2b5722009-11-16 15:11:51 +0000166#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
drh9b8f4472006-04-04 01:54:55 +0000167#endif
168#ifdef SQLITE_EBCDIC
drh46c99e02007-08-27 23:26:59 +0000169const char sqlite3IsEbcdicIdChar[] = {
drh9b8f4472006-04-04 01:54:55 +0000170/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
171 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
172 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
173 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
174 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
175 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
176 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
177 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
178 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
179 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
180 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
181 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
182 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
183};
drh46c99e02007-08-27 23:26:59 +0000184#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
drh9b8f4472006-04-04 01:54:55 +0000185#endif
drhf5ed7ad2015-06-15 14:43:25 +0000186
drh38d99642018-08-18 18:27:18 +0000187/* Make the IdChar function accessible from ctime.c and alter.c */
drh97348b32014-09-25 02:44:29 +0000188int sqlite3IsIdChar(u8 c){ return IdChar(c); }
drh9b8f4472006-04-04 01:54:55 +0000189
dan34a7d792018-06-29 20:43:33 +0000190#ifndef SQLITE_OMIT_WINDOWFUNC
dan59ff4252018-06-29 17:44:52 +0000191/*
192** Return the id of the next token in string (*pz). Before returning, set
193** (*pz) to point to the byte following the parsed token.
dan59ff4252018-06-29 17:44:52 +0000194*/
dan6e2210e2018-06-30 18:54:56 +0000195static int getToken(const unsigned char **pz){
dan59ff4252018-06-29 17:44:52 +0000196 const unsigned char *z = *pz;
dan6e2210e2018-06-30 18:54:56 +0000197 int t; /* Token type to return */
198 do {
199 z += sqlite3GetToken(z, &t);
200 }while( t==TK_SPACE );
201 if( t==TK_ID
202 || t==TK_STRING
203 || t==TK_JOIN_KW
204 || t==TK_WINDOW
205 || t==TK_OVER
206 || sqlite3ParserFallback(t)==TK_ID
207 ){
208 t = TK_ID;
dan59ff4252018-06-29 17:44:52 +0000209 }
210 *pz = z;
dan6e2210e2018-06-30 18:54:56 +0000211 return t;
dan59ff4252018-06-29 17:44:52 +0000212}
213
214/*
dan6e2210e2018-06-30 18:54:56 +0000215** The following three functions are called immediately after the tokenizer
216** reads the keywords WINDOW, OVER and FILTER, respectively, to determine
217** whether the token should be treated as a keyword or an SQL identifier.
218** This cannot be handled by the usual lemon %fallback method, due to
219** the ambiguity in some constructions. e.g.
dan59ff4252018-06-29 17:44:52 +0000220**
dan6e2210e2018-06-30 18:54:56 +0000221** SELECT sum(x) OVER ...
dan59ff4252018-06-29 17:44:52 +0000222**
dan6e2210e2018-06-30 18:54:56 +0000223** In the above, "OVER" might be a keyword, or it might be an alias for the
224** sum(x) expression. If a "%fallback ID OVER" directive were added to
225** grammar, then SQLite would always treat "OVER" as an alias, making it
226** impossible to call a window-function without a FILTER clause.
227**
228** WINDOW is treated as a keyword if:
229**
230** * the following token is an identifier, or a keyword that can fallback
231** to being an identifier, and
232** * the token after than one is TK_AS.
233**
234** OVER is a keyword if:
235**
236** * the previous token was TK_RP, and
237** * the next token is either TK_LP or an identifier.
238**
239** FILTER is a keyword if:
240**
241** * the previous token was TK_RP, and
242** * the next token is TK_LP.
dan59ff4252018-06-29 17:44:52 +0000243*/
dan769309f2018-06-29 19:54:51 +0000244static int analyzeWindowKeyword(const unsigned char *z){
dan59ff4252018-06-29 17:44:52 +0000245 int t;
dan6e2210e2018-06-30 18:54:56 +0000246 t = getToken(&z);
247 if( t!=TK_ID ) return TK_ID;
248 t = getToken(&z);
249 if( t!=TK_AS ) return TK_ID;
250 return TK_WINDOW;
251}
252static int analyzeOverKeyword(const unsigned char *z, int lastToken){
253 if( lastToken==TK_RP ){
254 int t = getToken(&z);
255 if( t==TK_LP || t==TK_ID ) return TK_OVER;
dan59ff4252018-06-29 17:44:52 +0000256 }
dan6e2210e2018-06-30 18:54:56 +0000257 return TK_ID;
258}
259static int analyzeFilterKeyword(const unsigned char *z, int lastToken){
260 if( lastToken==TK_RP && getToken(&z)==TK_LP ){
261 return TK_FILTER;
262 }
263 return TK_ID;
dan59ff4252018-06-29 17:44:52 +0000264}
drhc7bf5712018-07-09 22:49:01 +0000265#endif /* SQLITE_OMIT_WINDOWFUNC */
drh98808ba2001-10-18 12:34:46 +0000266
drh75897232000-05-29 14:26:00 +0000267/*
drhe96f3612016-02-08 19:36:46 +0000268** Return the length (in bytes) of the token that begins at z[0].
drh61b487d2003-09-12 02:08:14 +0000269** Store the token type in *tokenType before returning.
drh75897232000-05-29 14:26:00 +0000270*/
drh35b5a332008-04-05 18:41:42 +0000271int sqlite3GetToken(const unsigned char *z, int *tokenType){
drhaa756b02004-09-25 15:25:26 +0000272 int i, c;
drhe96f3612016-02-08 19:36:46 +0000273 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
274 ** of the token. See the comment on the CC_ defines
275 ** above. */
drh89743312016-02-08 02:30:50 +0000276 case CC_SPACE: {
drh19f81f62009-06-09 18:01:37 +0000277 testcase( z[0]==' ' );
278 testcase( z[0]=='\t' );
279 testcase( z[0]=='\n' );
280 testcase( z[0]=='\f' );
281 testcase( z[0]=='\r' );
danielk197778ca0e72009-01-20 16:53:39 +0000282 for(i=1; sqlite3Isspace(z[i]); i++){}
drh75897232000-05-29 14:26:00 +0000283 *tokenType = TK_SPACE;
284 return i;
285 }
drh89743312016-02-08 02:30:50 +0000286 case CC_MINUS: {
drh75897232000-05-29 14:26:00 +0000287 if( z[1]=='-' ){
drhaa756b02004-09-25 15:25:26 +0000288 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
drhd3479b92009-12-14 17:42:12 +0000289 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
drh75897232000-05-29 14:26:00 +0000290 return i;
291 }
292 *tokenType = TK_MINUS;
293 return 1;
294 }
drh89743312016-02-08 02:30:50 +0000295 case CC_LP: {
drhdab35182003-09-27 13:39:38 +0000296 *tokenType = TK_LP;
297 return 1;
drh75897232000-05-29 14:26:00 +0000298 }
drh89743312016-02-08 02:30:50 +0000299 case CC_RP: {
drh75897232000-05-29 14:26:00 +0000300 *tokenType = TK_RP;
301 return 1;
302 }
drh89743312016-02-08 02:30:50 +0000303 case CC_SEMI: {
drh75897232000-05-29 14:26:00 +0000304 *tokenType = TK_SEMI;
305 return 1;
306 }
drh89743312016-02-08 02:30:50 +0000307 case CC_PLUS: {
drh75897232000-05-29 14:26:00 +0000308 *tokenType = TK_PLUS;
309 return 1;
310 }
drh89743312016-02-08 02:30:50 +0000311 case CC_STAR: {
drh75897232000-05-29 14:26:00 +0000312 *tokenType = TK_STAR;
313 return 1;
314 }
drh89743312016-02-08 02:30:50 +0000315 case CC_SLASH: {
drh66105a82002-08-27 14:28:29 +0000316 if( z[1]!='*' || z[2]==0 ){
317 *tokenType = TK_SLASH;
318 return 1;
319 }
drhaa756b02004-09-25 15:25:26 +0000320 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
321 if( c ) i++;
drhd3479b92009-12-14 17:42:12 +0000322 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
drh66105a82002-08-27 14:28:29 +0000323 return i;
drh75897232000-05-29 14:26:00 +0000324 }
drh89743312016-02-08 02:30:50 +0000325 case CC_PERCENT: {
drhbf4133c2001-10-13 02:59:08 +0000326 *tokenType = TK_REM;
327 return 1;
328 }
drh89743312016-02-08 02:30:50 +0000329 case CC_EQ: {
drh75897232000-05-29 14:26:00 +0000330 *tokenType = TK_EQ;
331 return 1 + (z[1]=='=');
332 }
drh89743312016-02-08 02:30:50 +0000333 case CC_LT: {
drhaa756b02004-09-25 15:25:26 +0000334 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000335 *tokenType = TK_LE;
336 return 2;
drhaa756b02004-09-25 15:25:26 +0000337 }else if( c=='>' ){
drh75897232000-05-29 14:26:00 +0000338 *tokenType = TK_NE;
339 return 2;
drhaa756b02004-09-25 15:25:26 +0000340 }else if( c=='<' ){
drhbf4133c2001-10-13 02:59:08 +0000341 *tokenType = TK_LSHIFT;
342 return 2;
drh75897232000-05-29 14:26:00 +0000343 }else{
344 *tokenType = TK_LT;
345 return 1;
346 }
347 }
drh89743312016-02-08 02:30:50 +0000348 case CC_GT: {
drhaa756b02004-09-25 15:25:26 +0000349 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000350 *tokenType = TK_GE;
351 return 2;
drhaa756b02004-09-25 15:25:26 +0000352 }else if( c=='>' ){
drhbf4133c2001-10-13 02:59:08 +0000353 *tokenType = TK_RSHIFT;
354 return 2;
drh75897232000-05-29 14:26:00 +0000355 }else{
356 *tokenType = TK_GT;
357 return 1;
358 }
359 }
drh89743312016-02-08 02:30:50 +0000360 case CC_BANG: {
drh75897232000-05-29 14:26:00 +0000361 if( z[1]!='=' ){
362 *tokenType = TK_ILLEGAL;
drhb2bddbb2016-02-18 14:49:28 +0000363 return 1;
drh75897232000-05-29 14:26:00 +0000364 }else{
365 *tokenType = TK_NE;
366 return 2;
367 }
368 }
drh89743312016-02-08 02:30:50 +0000369 case CC_PIPE: {
drh00400772000-06-16 20:51:26 +0000370 if( z[1]!='|' ){
drhbf4133c2001-10-13 02:59:08 +0000371 *tokenType = TK_BITOR;
drh00400772000-06-16 20:51:26 +0000372 return 1;
373 }else{
374 *tokenType = TK_CONCAT;
375 return 2;
376 }
377 }
drh89743312016-02-08 02:30:50 +0000378 case CC_COMMA: {
drh75897232000-05-29 14:26:00 +0000379 *tokenType = TK_COMMA;
380 return 1;
381 }
drh89743312016-02-08 02:30:50 +0000382 case CC_AND: {
drhbf4133c2001-10-13 02:59:08 +0000383 *tokenType = TK_BITAND;
384 return 1;
385 }
drh89743312016-02-08 02:30:50 +0000386 case CC_TILDA: {
drhbf4133c2001-10-13 02:59:08 +0000387 *tokenType = TK_BITNOT;
388 return 1;
389 }
drh89743312016-02-08 02:30:50 +0000390 case CC_QUOTE: {
drh75897232000-05-29 14:26:00 +0000391 int delim = z[0];
drh19f81f62009-06-09 18:01:37 +0000392 testcase( delim=='`' );
393 testcase( delim=='\'' );
394 testcase( delim=='"' );
drhaa756b02004-09-25 15:25:26 +0000395 for(i=1; (c=z[i])!=0; i++){
396 if( c==delim ){
drh75897232000-05-29 14:26:00 +0000397 if( z[i+1]==delim ){
398 i++;
399 }else{
400 break;
401 }
402 }
403 }
drha33cb5f2008-08-07 13:05:34 +0000404 if( c=='\'' ){
drheef8b552005-10-23 11:29:40 +0000405 *tokenType = TK_STRING;
406 return i+1;
drha33cb5f2008-08-07 13:05:34 +0000407 }else if( c!=0 ){
408 *tokenType = TK_ID;
409 return i+1;
drheef8b552005-10-23 11:29:40 +0000410 }else{
411 *tokenType = TK_ILLEGAL;
412 return i;
413 }
drh75897232000-05-29 14:26:00 +0000414 }
drh89743312016-02-08 02:30:50 +0000415 case CC_DOT: {
drh76816182005-08-23 11:31:26 +0000416#ifndef SQLITE_OMIT_FLOATING_POINT
danielk197778ca0e72009-01-20 16:53:39 +0000417 if( !sqlite3Isdigit(z[1]) )
drh76816182005-08-23 11:31:26 +0000418#endif
419 {
420 *tokenType = TK_DOT;
421 return 1;
422 }
423 /* If the next character is a digit, this is a floating point
424 ** number that begins with ".". Fall thru into the next case */
drh75897232000-05-29 14:26:00 +0000425 }
drh89743312016-02-08 02:30:50 +0000426 case CC_DIGIT: {
drh19f81f62009-06-09 18:01:37 +0000427 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
428 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
429 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
430 testcase( z[0]=='9' );
drhc837e702000-06-08 16:26:24 +0000431 *tokenType = TK_INTEGER;
drh28e048c2014-07-23 01:26:51 +0000432#ifndef SQLITE_OMIT_HEX_INTEGER
433 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
434 for(i=3; sqlite3Isxdigit(z[i]); i++){}
435 return i;
436 }
437#endif
danielk197778ca0e72009-01-20 16:53:39 +0000438 for(i=0; sqlite3Isdigit(z[i]); i++){}
drhb7f91642004-10-31 02:22:47 +0000439#ifndef SQLITE_OMIT_FLOATING_POINT
drh76816182005-08-23 11:31:26 +0000440 if( z[i]=='.' ){
441 i++;
danielk197778ca0e72009-01-20 16:53:39 +0000442 while( sqlite3Isdigit(z[i]) ){ i++; }
drhc837e702000-06-08 16:26:24 +0000443 *tokenType = TK_FLOAT;
444 }
445 if( (z[i]=='e' || z[i]=='E') &&
danielk197778ca0e72009-01-20 16:53:39 +0000446 ( sqlite3Isdigit(z[i+1])
447 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
drh75897232000-05-29 14:26:00 +0000448 )
drhc837e702000-06-08 16:26:24 +0000449 ){
450 i += 2;
danielk197778ca0e72009-01-20 16:53:39 +0000451 while( sqlite3Isdigit(z[i]) ){ i++; }
drh75897232000-05-29 14:26:00 +0000452 *tokenType = TK_FLOAT;
drh75897232000-05-29 14:26:00 +0000453 }
drhb7f91642004-10-31 02:22:47 +0000454#endif
drh67dd9012006-08-12 12:33:14 +0000455 while( IdChar(z[i]) ){
456 *tokenType = TK_ILLEGAL;
457 i++;
458 }
drh75897232000-05-29 14:26:00 +0000459 return i;
460 }
drh89743312016-02-08 02:30:50 +0000461 case CC_QUOTE2: {
drhaa756b02004-09-25 15:25:26 +0000462 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
drh4b2f9362008-01-22 23:37:09 +0000463 *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
drh2f4392f2002-02-14 21:42:51 +0000464 return i;
465 }
drh89743312016-02-08 02:30:50 +0000466 case CC_VARNUM: {
drh50457892003-09-06 01:10:47 +0000467 *tokenType = TK_VARIABLE;
danielk197778ca0e72009-01-20 16:53:39 +0000468 for(i=1; sqlite3Isdigit(z[i]); i++){}
drhfa6bc002004-09-07 16:19:52 +0000469 return i;
drh50457892003-09-06 01:10:47 +0000470 }
drh89743312016-02-08 02:30:50 +0000471 case CC_DOLLAR:
472 case CC_VARALPHA: {
drh288d37f2005-06-22 08:48:06 +0000473 int n = 0;
drhf59b12f2014-01-11 03:54:05 +0000474 testcase( z[0]=='$' ); testcase( z[0]=='@' );
475 testcase( z[0]==':' ); testcase( z[0]=='#' );
drh9d74b4c2004-08-24 15:23:34 +0000476 *tokenType = TK_VARIABLE;
drh288d37f2005-06-22 08:48:06 +0000477 for(i=1; (c=z[i])!=0; i++){
478 if( IdChar(c) ){
479 n++;
480#ifndef SQLITE_OMIT_TCL_VARIABLE
481 }else if( c=='(' && n>0 ){
482 do{
483 i++;
danielk197778ca0e72009-01-20 16:53:39 +0000484 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
drh288d37f2005-06-22 08:48:06 +0000485 if( c==')' ){
drh895d7472004-08-20 16:02:39 +0000486 i++;
487 }else{
drh288d37f2005-06-22 08:48:06 +0000488 *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000489 }
drh288d37f2005-06-22 08:48:06 +0000490 break;
491 }else if( c==':' && z[i+1]==':' ){
492 i++;
493#endif
494 }else{
495 break;
drh895d7472004-08-20 16:02:39 +0000496 }
497 }
drh288d37f2005-06-22 08:48:06 +0000498 if( n==0 ) *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000499 return i;
drhb7f91642004-10-31 02:22:47 +0000500 }
drhe96f3612016-02-08 19:36:46 +0000501 case CC_KYWD: {
502 for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
503 if( IdChar(z[i]) ){
504 /* This token started out using characters that can appear in keywords,
505 ** but z[i] is a character not allowed within keywords, so this must
506 ** be an identifier instead */
507 i++;
508 break;
509 }
510 *tokenType = TK_ID;
dan769309f2018-06-29 19:54:51 +0000511 return keywordCode((char*)z, i, tokenType);
drhe96f3612016-02-08 19:36:46 +0000512 }
drh89743312016-02-08 02:30:50 +0000513 case CC_X: {
dana73086d2016-02-24 16:14:07 +0000514#ifndef SQLITE_OMIT_BLOB_LITERAL
drh19f81f62009-06-09 18:01:37 +0000515 testcase( z[0]=='x' ); testcase( z[0]=='X' );
drh4b2f9362008-01-22 23:37:09 +0000516 if( z[1]=='\'' ){
danielk19773fd0a732004-05-27 13:35:19 +0000517 *tokenType = TK_BLOB;
drh3a61a5a2011-06-03 21:34:45 +0000518 for(i=2; sqlite3Isxdigit(z[i]); i++){}
519 if( z[i]!='\'' || i%2 ){
520 *tokenType = TK_ILLEGAL;
521 while( z[i] && z[i]!='\'' ){ i++; }
danielk1977c572ef72004-05-27 09:28:41 +0000522 }
drh3a61a5a2011-06-03 21:34:45 +0000523 if( z[i] ) i++;
danielk1977c572ef72004-05-27 09:28:41 +0000524 return i;
525 }
dana73086d2016-02-24 16:14:07 +0000526#endif
drhe96f3612016-02-08 19:36:46 +0000527 /* If it is not a BLOB literal, then it must be an ID, since no
528 ** SQL keywords start with the letter 'x'. Fall through */
danielk1977c572ef72004-05-27 09:28:41 +0000529 }
drh89743312016-02-08 02:30:50 +0000530 case CC_ID: {
531 i = 1;
532 break;
533 }
dan2b5f1522018-06-30 20:00:35 +0000534 case CC_NUL: {
535 *tokenType = TK_ILLEGAL;
536 return 0;
537 }
drh98808ba2001-10-18 12:34:46 +0000538 default: {
drh89743312016-02-08 02:30:50 +0000539 *tokenType = TK_ILLEGAL;
540 return 1;
drh75897232000-05-29 14:26:00 +0000541 }
drh75897232000-05-29 14:26:00 +0000542 }
drh34dcee62016-02-08 19:15:48 +0000543 while( IdChar(z[i]) ){ i++; }
drh89743312016-02-08 02:30:50 +0000544 *tokenType = TK_ID;
545 return i;
drh75897232000-05-29 14:26:00 +0000546}
547
mistachkin8bee11a2018-10-29 17:53:23 +0000548#ifdef SQLITE_ENABLE_NORMALIZE
549/*
550** Return the length (in bytes) of the token that begins at z[0].
551** Store the token type in *tokenType before returning. If flags has
552** SQLITE_TOKEN_NORMALIZE flag enabled, use the identifier token type
553** for keywords. Add SQLITE_TOKEN_QUOTED to flags if the token was
554** actually a quoted identifier. Add SQLITE_TOKEN_KEYWORD to flags
555** if the token was recognized as a keyword; this is useful when the
556** SQLITE_TOKEN_NORMALIZE flag is used, because it enables the caller
557** to differentiate between a keyword being treated as an identifier
558** (for normalization purposes) and an actual identifier.
559*/
560int sqlite3GetTokenNormalized(
561 const unsigned char *z,
562 int *tokenType,
563 int *flags
564){
565 int n;
566 unsigned char iClass = aiClass[*z];
567 if( iClass==CC_KYWD ){
568 int i;
569 for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
570 if( IdChar(z[i]) ){
571 /* This token started out using characters that can appear in keywords,
572 ** but z[i] is a character not allowed within keywords, so this must
573 ** be an identifier instead */
574 i++;
575 while( IdChar(z[i]) ){ i++; }
576 *tokenType = TK_ID;
577 return i;
578 }
579 *tokenType = TK_ID;
580 n = keywordCode((char*)z, i, tokenType);
581 /* If the token is no longer considered to be an identifier, then it is a
582 ** keyword of some kind. Make the token back into an identifier and then
583 ** set the SQLITE_TOKEN_KEYWORD flag. Several non-identifier tokens are
584 ** used verbatim, including IN, IS, NOT, and NULL. */
585 switch( *tokenType ){
586 case TK_ID: {
587 /* do nothing, handled by caller */
588 break;
589 }
590 case TK_IN:
591 case TK_IS:
592 case TK_NOT:
593 case TK_NULL: {
594 *flags |= SQLITE_TOKEN_KEYWORD;
595 break;
596 }
597 default: {
598 *tokenType = TK_ID;
599 *flags |= SQLITE_TOKEN_KEYWORD;
600 break;
601 }
602 }
603 }else{
604 n = sqlite3GetToken(z, tokenType);
605 /* If the token is considered to be an identifier and the character class
606 ** of the first character is a quote, set the SQLITE_TOKEN_QUOTED flag. */
607 if( *tokenType==TK_ID && (iClass==CC_QUOTE || iClass==CC_QUOTE2) ){
608 *flags |= SQLITE_TOKEN_QUOTED;
609 }
610 }
611 return n;
612}
613#endif /* SQLITE_ENABLE_NORMALIZE */
614
drh75897232000-05-29 14:26:00 +0000615/*
616** Run the parser on the given SQL string. The parser structure is
drhb19a2bc2001-09-16 00:13:26 +0000617** passed in. An SQLITE_ status code is returned. If an error occurs
drhfb45d8c2008-07-08 00:06:49 +0000618** then an and attempt is made to write an error message into
619** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
620** error message.
drh75897232000-05-29 14:26:00 +0000621*/
danielk19774adee202004-05-08 08:23:19 +0000622int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
drhd9da78a2009-03-24 15:08:09 +0000623 int nErr = 0; /* Number of errors encountered */
drhd9da78a2009-03-24 15:08:09 +0000624 void *pEngine; /* The LEMON-generated LALR(1) parser */
drh9b38d662017-03-07 14:38:52 +0000625 int n = 0; /* Length of the next token token */
drhd9da78a2009-03-24 15:08:09 +0000626 int tokenType; /* type of the next token */
627 int lastTokenParsed = -1; /* type of the previous token */
drhd9da78a2009-03-24 15:08:09 +0000628 sqlite3 *db = pParse->db; /* The database connection */
629 int mxSqlLen; /* Max length of an SQL string */
drhd26cc542017-01-28 20:46:37 +0000630#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
drh53b24592017-03-30 17:13:37 +0000631 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
drhd26cc542017-01-28 20:46:37 +0000632#endif
drh75897232000-05-29 14:26:00 +0000633
drh96c707a2015-02-13 16:36:14 +0000634 assert( zSql!=0 );
drhd9da78a2009-03-24 15:08:09 +0000635 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
drh4f7d3a52013-06-27 23:54:02 +0000636 if( db->nVdbeActive==0 ){
drh15ca1df2006-07-26 13:43:30 +0000637 db->u1.isInterrupted = 0;
638 }
drh4c504392000-10-16 22:06:40 +0000639 pParse->rc = SQLITE_OK;
drhe7f3f3e2009-07-03 22:54:36 +0000640 pParse->zTail = zSql;
drhfb45d8c2008-07-08 00:06:49 +0000641 assert( pzErrMsg!=0 );
drh3bd48ab2015-09-07 18:23:37 +0000642 /* sqlite3ParserTrace(stdout, "parser: "); */
drhd26cc542017-01-28 20:46:37 +0000643#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
drh53b24592017-03-30 17:13:37 +0000644 pEngine = &sEngine;
drhfb32c442018-04-21 13:51:42 +0000645 sqlite3ParserInit(pEngine, pParse);
drhd26cc542017-01-28 20:46:37 +0000646#else
drhfb32c442018-04-21 13:51:42 +0000647 pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse);
drh75897232000-05-29 14:26:00 +0000648 if( pEngine==0 ){
drh4a642b62016-02-05 01:55:27 +0000649 sqlite3OomFault(db);
mistachkinfad30392016-02-13 23:43:46 +0000650 return SQLITE_NOMEM_BKPT;
drh75897232000-05-29 14:26:00 +0000651 }
drhd26cc542017-01-28 20:46:37 +0000652#endif
drhfa6bc002004-09-07 16:19:52 +0000653 assert( pParse->pNewTable==0 );
654 assert( pParse->pNewTrigger==0 );
655 assert( pParse->nVar==0 );
drh9bf755c2016-12-23 03:59:31 +0000656 assert( pParse->pVList==0 );
drh167fbbe2016-08-10 14:40:00 +0000657 while( 1 ){
dand437ac02018-06-29 20:21:24 +0000658 n = sqlite3GetToken((u8*)zSql, &tokenType);
659 mxSqlLen -= n;
660 if( mxSqlLen<0 ){
661 pParse->rc = SQLITE_TOOBIG;
662 break;
drhe5c941b2007-05-08 13:58:26 +0000663 }
dan34a7d792018-06-29 20:43:33 +0000664#ifndef SQLITE_OMIT_WINDOWFUNC
665 if( tokenType>=TK_WINDOW ){
dan6e2210e2018-06-30 18:54:56 +0000666 assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER
dan34a7d792018-06-29 20:43:33 +0000667 || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW
668 );
669#else
drh0739e452015-11-09 02:08:09 +0000670 if( tokenType>=TK_SPACE ){
671 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
drhc7bf5712018-07-09 22:49:01 +0000672#endif /* SQLITE_OMIT_WINDOWFUNC */
drh0739e452015-11-09 02:08:09 +0000673 if( db->u1.isInterrupted ){
drh0739e452015-11-09 02:08:09 +0000674 pParse->rc = SQLITE_INTERRUPT;
drh75897232000-05-29 14:26:00 +0000675 break;
676 }
dand437ac02018-06-29 20:21:24 +0000677 if( tokenType==TK_SPACE ){
678 zSql += n;
679 continue;
680 }
681 if( zSql[0]==0 ){
682 /* Upon reaching the end of input, call the parser two more times
683 ** with tokens TK_SEMI and 0, in that order. */
684 if( lastTokenParsed==TK_SEMI ){
685 tokenType = 0;
686 }else if( lastTokenParsed==0 ){
687 break;
688 }else{
689 tokenType = TK_SEMI;
690 }
691 n = 0;
dan34a7d792018-06-29 20:43:33 +0000692#ifndef SQLITE_OMIT_WINDOWFUNC
693 }else if( tokenType==TK_WINDOW ){
dan6e2210e2018-06-30 18:54:56 +0000694 assert( n==6 );
dan34a7d792018-06-29 20:43:33 +0000695 tokenType = analyzeWindowKeyword((const u8*)&zSql[6]);
dan6e2210e2018-06-30 18:54:56 +0000696 }else if( tokenType==TK_OVER ){
697 assert( n==4 );
698 tokenType = analyzeOverKeyword((const u8*)&zSql[4], lastTokenParsed);
699 }else if( tokenType==TK_FILTER ){
700 assert( n==6 );
701 tokenType = analyzeFilterKeyword((const u8*)&zSql[6], lastTokenParsed);
drhc7bf5712018-07-09 22:49:01 +0000702#endif /* SQLITE_OMIT_WINDOWFUNC */
dand437ac02018-06-29 20:21:24 +0000703 }else{
drh9b38d662017-03-07 14:38:52 +0000704 sqlite3ErrorMsg(pParse, "unrecognized token: \"%.*s\"", n, zSql);
drh75897232000-05-29 14:26:00 +0000705 break;
drh32eb7b42003-01-07 01:44:37 +0000706 }
drh75897232000-05-29 14:26:00 +0000707 }
dand437ac02018-06-29 20:21:24 +0000708 pParse->sLastToken.z = zSql;
709 pParse->sLastToken.n = n;
710 sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
711 lastTokenParsed = tokenType;
712 zSql += n;
713 if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break;
drh75897232000-05-29 14:26:00 +0000714 }
drh0be0cf62015-04-15 08:37:42 +0000715 assert( nErr==0 );
drhec424a52008-07-25 15:39:03 +0000716#ifdef YYTRACKMAXSTACKDEPTH
drhaf89fe62015-03-23 17:25:18 +0000717 sqlite3_mutex_enter(sqlite3MallocMutex());
drhb02392e2015-10-15 15:28:56 +0000718 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
drhec424a52008-07-25 15:39:03 +0000719 sqlite3ParserStackPeak(pEngine)
720 );
drhaf89fe62015-03-23 17:25:18 +0000721 sqlite3_mutex_leave(sqlite3MallocMutex());
drhec424a52008-07-25 15:39:03 +0000722#endif /* YYDEBUG */
drhd26cc542017-01-28 20:46:37 +0000723#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
724 sqlite3ParserFinalize(pEngine);
725#else
drh17435752007-08-16 04:30:38 +0000726 sqlite3ParserFree(pEngine, sqlite3_free);
drhd26cc542017-01-28 20:46:37 +0000727#endif
drh17435752007-08-16 04:30:38 +0000728 if( db->mallocFailed ){
mistachkinfad30392016-02-13 23:43:46 +0000729 pParse->rc = SQLITE_NOMEM_BKPT;
drh71c697e2004-08-08 23:39:19 +0000730 }
drhb86ccfb2003-01-28 23:13:10 +0000731 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
drh22c17b82015-05-15 04:13:15 +0000732 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
drhb86ccfb2003-01-28 23:13:10 +0000733 }
drh19f81f62009-06-09 18:01:37 +0000734 assert( pzErrMsg!=0 );
drh75897232000-05-29 14:26:00 +0000735 if( pParse->zErrMsg ){
drh19f81f62009-06-09 18:01:37 +0000736 *pzErrMsg = pParse->zErrMsg;
drh875ad8c2018-06-21 23:53:54 +0000737 sqlite3_log(pParse->rc, "%s in \"%s\"",
738 *pzErrMsg, pParse->zTail);
drhb86ccfb2003-01-28 23:13:10 +0000739 pParse->zErrMsg = 0;
drh4b2f9362008-01-22 23:37:09 +0000740 nErr++;
drh75897232000-05-29 14:26:00 +0000741 }
drh875ad8c2018-06-21 23:53:54 +0000742 pParse->zTail = zSql;
drh2958a4e2004-11-12 03:56:15 +0000743 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
danielk19774adee202004-05-08 08:23:19 +0000744 sqlite3VdbeDelete(pParse->pVdbe);
drh75897232000-05-29 14:26:00 +0000745 pParse->pVdbe = 0;
746 }
danielk1977c00da102006-01-07 13:21:04 +0000747#ifndef SQLITE_OMIT_SHARED_CACHE
748 if( pParse->nested==0 ){
drh633e6d52008-07-28 19:34:53 +0000749 sqlite3DbFree(db, pParse->aTableLock);
danielk1977c00da102006-01-07 13:21:04 +0000750 pParse->aTableLock = 0;
751 pParse->nTableLock = 0;
752 }
753#endif
drh4f3dd152008-04-28 18:46:43 +0000754#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9755982010-07-24 16:34:37 +0000755 sqlite3_free(pParse->apVtabLock);
drh4f3dd152008-04-28 18:46:43 +0000756#endif
danielk19777e6ebfb2006-06-12 11:24:37 +0000757
dancf8f2892018-08-09 20:47:01 +0000758 if( !IN_SPECIAL_PARSE ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000759 /* If the pParse->declareVtab flag is set, do not delete any table
760 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
761 ** will take responsibility for freeing the Table structure.
762 */
dan1feeaed2010-07-23 15:41:47 +0000763 sqlite3DeleteTable(db, pParse->pNewTable);
danielk19777e6ebfb2006-06-12 11:24:37 +0000764 }
danc9461ec2018-08-29 21:00:16 +0000765 if( !IN_RENAME_OBJECT ){
dan5496d6a2018-08-13 17:14:26 +0000766 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
767 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000768
drhf3c57ff2016-04-12 00:16:54 +0000769 if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree);
drh9bf755c2016-12-23 03:59:31 +0000770 sqlite3DbFree(db, pParse->pVList);
drh0b9f50d2009-06-23 20:28:53 +0000771 while( pParse->pAinc ){
772 AutoincInfo *p = pParse->pAinc;
773 pParse->pAinc = p->pNext;
drhdbd6a7d2017-04-05 12:39:49 +0000774 sqlite3DbFreeNN(db, p);
drh0b9f50d2009-06-23 20:28:53 +0000775 }
drh588a9a12008-09-01 15:52:10 +0000776 while( pParse->pZombieTab ){
777 Table *p = pParse->pZombieTab;
778 pParse->pZombieTab = p->pNextZombie;
dan1feeaed2010-07-23 15:41:47 +0000779 sqlite3DeleteTable(db, p);
drh588a9a12008-09-01 15:52:10 +0000780 }
drhf3151f02015-04-16 20:27:09 +0000781 assert( nErr==0 || pParse->rc!=SQLITE_OK );
drh75897232000-05-29 14:26:00 +0000782 return nErr;
783}