blob: 1dab3c528aee9f62609891a694b31d073c2081a8 [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.
17**
drh288d37f2005-06-22 08:48:06 +000018** $Id: tokenize.c,v 1.104 2005/06/22 08:48:06 drh Exp $
drh75897232000-05-29 14:26:00 +000019*/
20#include "sqliteInt.h"
drhad75e982001-10-09 04:19:46 +000021#include "os.h"
drh75897232000-05-29 14:26:00 +000022#include <ctype.h>
drhdcc581c2000-05-30 13:44:19 +000023#include <stdlib.h>
drh75897232000-05-29 14:26:00 +000024
25/*
drh52fb6d72004-11-03 03:59:57 +000026** The sqlite3KeywordCode function looks up an identifier to determine if
27** it is a keyword. If it is a keyword, the token code of that keyword is
drh75897232000-05-29 14:26:00 +000028** returned. If the input is not a keyword, TK_ID is returned.
drh2090a0e2004-10-07 19:03:01 +000029**
30** The implementation of this routine was generated by a program,
drh73b211a2005-01-18 04:00:42 +000031** mkkeywordhash.h, located in the tool subdirectory of the distribution.
drh52fb6d72004-11-03 03:59:57 +000032** The output of the mkkeywordhash.c program is written into a file
drh73b211a2005-01-18 04:00:42 +000033** named keywordhash.h and then included into this source file by
drh52fb6d72004-11-03 03:59:57 +000034** the #include below.
drh75897232000-05-29 14:26:00 +000035*/
drh73b211a2005-01-18 04:00:42 +000036#include "keywordhash.h"
drh75897232000-05-29 14:26:00 +000037
drh40f20f72004-10-23 05:10:18 +000038
drh98808ba2001-10-18 12:34:46 +000039/*
drhba212562004-01-08 02:17:31 +000040** If X is a character that can be used in an identifier and
41** X&0x80==0 then isIdChar[X] will be 1. If X&0x80==0x80 then
42** X is always an identifier character. (Hence all UTF-8
43** characters can be part of an identifier). isIdChar[X] will
44** be 0 for every character in the lower 128 ASCII characters
45** that cannot be used as part of an identifier.
drh98808ba2001-10-18 12:34:46 +000046**
47** In this implementation, an identifier can be a string of
48** alphabetic characters, digits, and "_" plus any character
49** with the high-order bit set. The latter rule means that
50** any sequence of UTF-8 characters or characters taken from
51** an extended ISO8859 character set can form an identifier.
drha0d1f662005-01-11 17:59:47 +000052**
53** Ticket #1066. the SQL standard does not allow '$' in the
54** middle of identfiers. But many SQL implementations do.
55** SQLite will allow '$' in identifiers for compatibility.
56** But the feature is undocumented.
drh98808ba2001-10-18 12:34:46 +000057*/
58static const char isIdChar[] = {
59/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
drha0d1f662005-01-11 17:59:47 +000060 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
drh98808ba2001-10-18 12:34:46 +000061 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
62 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
63 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
64 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
65 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
drh98808ba2001-10-18 12:34:46 +000066};
67
drha0d1f662005-01-11 17:59:47 +000068#define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && isIdChar[c-0x20]))
drh98808ba2001-10-18 12:34:46 +000069
drh75897232000-05-29 14:26:00 +000070/*
drh61b487d2003-09-12 02:08:14 +000071** Return the length of the token that begins at z[0].
72** Store the token type in *tokenType before returning.
drh75897232000-05-29 14:26:00 +000073*/
danielk1977c60e9b82005-01-31 12:42:29 +000074static int getToken(const unsigned char *z, int *tokenType){
drhaa756b02004-09-25 15:25:26 +000075 int i, c;
drh75897232000-05-29 14:26:00 +000076 switch( *z ){
drh30cab802000-08-09 17:17:25 +000077 case ' ': case '\t': case '\n': case '\f': case '\r': {
drh32eb7b42003-01-07 01:44:37 +000078 for(i=1; isspace(z[i]); i++){}
drh75897232000-05-29 14:26:00 +000079 *tokenType = TK_SPACE;
80 return i;
81 }
82 case '-': {
drh75897232000-05-29 14:26:00 +000083 if( z[1]=='-' ){
drhaa756b02004-09-25 15:25:26 +000084 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
drh75897232000-05-29 14:26:00 +000085 *tokenType = TK_COMMENT;
86 return i;
87 }
88 *tokenType = TK_MINUS;
89 return 1;
90 }
91 case '(': {
drhdab35182003-09-27 13:39:38 +000092 *tokenType = TK_LP;
93 return 1;
drh75897232000-05-29 14:26:00 +000094 }
95 case ')': {
96 *tokenType = TK_RP;
97 return 1;
98 }
99 case ';': {
100 *tokenType = TK_SEMI;
101 return 1;
102 }
103 case '+': {
104 *tokenType = TK_PLUS;
105 return 1;
106 }
107 case '*': {
108 *tokenType = TK_STAR;
109 return 1;
110 }
111 case '/': {
drh66105a82002-08-27 14:28:29 +0000112 if( z[1]!='*' || z[2]==0 ){
113 *tokenType = TK_SLASH;
114 return 1;
115 }
drhaa756b02004-09-25 15:25:26 +0000116 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
117 if( c ) i++;
drh66105a82002-08-27 14:28:29 +0000118 *tokenType = TK_COMMENT;
119 return i;
drh75897232000-05-29 14:26:00 +0000120 }
drhbf4133c2001-10-13 02:59:08 +0000121 case '%': {
122 *tokenType = TK_REM;
123 return 1;
124 }
drh75897232000-05-29 14:26:00 +0000125 case '=': {
126 *tokenType = TK_EQ;
127 return 1 + (z[1]=='=');
128 }
129 case '<': {
drhaa756b02004-09-25 15:25:26 +0000130 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000131 *tokenType = TK_LE;
132 return 2;
drhaa756b02004-09-25 15:25:26 +0000133 }else if( c=='>' ){
drh75897232000-05-29 14:26:00 +0000134 *tokenType = TK_NE;
135 return 2;
drhaa756b02004-09-25 15:25:26 +0000136 }else if( c=='<' ){
drhbf4133c2001-10-13 02:59:08 +0000137 *tokenType = TK_LSHIFT;
138 return 2;
drh75897232000-05-29 14:26:00 +0000139 }else{
140 *tokenType = TK_LT;
141 return 1;
142 }
143 }
144 case '>': {
drhaa756b02004-09-25 15:25:26 +0000145 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000146 *tokenType = TK_GE;
147 return 2;
drhaa756b02004-09-25 15:25:26 +0000148 }else if( c=='>' ){
drhbf4133c2001-10-13 02:59:08 +0000149 *tokenType = TK_RSHIFT;
150 return 2;
drh75897232000-05-29 14:26:00 +0000151 }else{
152 *tokenType = TK_GT;
153 return 1;
154 }
155 }
156 case '!': {
157 if( z[1]!='=' ){
158 *tokenType = TK_ILLEGAL;
drhc837e702000-06-08 16:26:24 +0000159 return 2;
drh75897232000-05-29 14:26:00 +0000160 }else{
161 *tokenType = TK_NE;
162 return 2;
163 }
164 }
drh00400772000-06-16 20:51:26 +0000165 case '|': {
166 if( z[1]!='|' ){
drhbf4133c2001-10-13 02:59:08 +0000167 *tokenType = TK_BITOR;
drh00400772000-06-16 20:51:26 +0000168 return 1;
169 }else{
170 *tokenType = TK_CONCAT;
171 return 2;
172 }
173 }
drh75897232000-05-29 14:26:00 +0000174 case ',': {
175 *tokenType = TK_COMMA;
176 return 1;
177 }
drhbf4133c2001-10-13 02:59:08 +0000178 case '&': {
179 *tokenType = TK_BITAND;
180 return 1;
181 }
182 case '~': {
183 *tokenType = TK_BITNOT;
184 return 1;
185 }
drh75897232000-05-29 14:26:00 +0000186 case '\'': case '"': {
187 int delim = z[0];
drhaa756b02004-09-25 15:25:26 +0000188 for(i=1; (c=z[i])!=0; i++){
189 if( c==delim ){
drh75897232000-05-29 14:26:00 +0000190 if( z[i+1]==delim ){
191 i++;
192 }else{
193 break;
194 }
195 }
196 }
drhaa756b02004-09-25 15:25:26 +0000197 if( c ) i++;
drh75897232000-05-29 14:26:00 +0000198 *tokenType = TK_STRING;
199 return i;
200 }
201 case '.': {
drhbb07e9a2003-04-16 02:17:35 +0000202 *tokenType = TK_DOT;
203 return 1;
drh75897232000-05-29 14:26:00 +0000204 }
205 case '0': case '1': case '2': case '3': case '4':
206 case '5': case '6': case '7': case '8': case '9': {
drhc837e702000-06-08 16:26:24 +0000207 *tokenType = TK_INTEGER;
drh32eb7b42003-01-07 01:44:37 +0000208 for(i=1; isdigit(z[i]); i++){}
drhb7f91642004-10-31 02:22:47 +0000209#ifndef SQLITE_OMIT_FLOATING_POINT
drhbb07e9a2003-04-16 02:17:35 +0000210 if( z[i]=='.' && isdigit(z[i+1]) ){
211 i += 2;
drh32eb7b42003-01-07 01:44:37 +0000212 while( isdigit(z[i]) ){ i++; }
drhc837e702000-06-08 16:26:24 +0000213 *tokenType = TK_FLOAT;
214 }
215 if( (z[i]=='e' || z[i]=='E') &&
drh75897232000-05-29 14:26:00 +0000216 ( isdigit(z[i+1])
217 || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
218 )
drhc837e702000-06-08 16:26:24 +0000219 ){
220 i += 2;
drh32eb7b42003-01-07 01:44:37 +0000221 while( isdigit(z[i]) ){ i++; }
drh75897232000-05-29 14:26:00 +0000222 *tokenType = TK_FLOAT;
drh75897232000-05-29 14:26:00 +0000223 }
drhb7f91642004-10-31 02:22:47 +0000224#endif
drh75897232000-05-29 14:26:00 +0000225 return i;
226 }
drh2f4392f2002-02-14 21:42:51 +0000227 case '[': {
drhaa756b02004-09-25 15:25:26 +0000228 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
drh2f4392f2002-02-14 21:42:51 +0000229 *tokenType = TK_ID;
230 return i;
231 }
drh7c972de2003-09-06 22:18:07 +0000232 case '?': {
drh50457892003-09-06 01:10:47 +0000233 *tokenType = TK_VARIABLE;
drhfa6bc002004-09-07 16:19:52 +0000234 for(i=1; isdigit(z[i]); i++){}
235 return i;
drh50457892003-09-06 01:10:47 +0000236 }
drh288d37f2005-06-22 08:48:06 +0000237 case '#': {
238 for(i=1; isdigit(z[i]); i++){}
239 if( i>1 ){
240 /* Parameters of the form #NNN (where NNN is a number) are used
241 ** internally by sqlite3NestedParse. */
242 *tokenType = TK_REGISTER;
243 return i;
244 }
245 /* Fall through into the next case if the '#' is not followed by
246 ** a digit. Try to match #AAAA where AAAA is a parameter name. */
drh895d7472004-08-20 16:02:39 +0000247 }
drhb7f91642004-10-31 02:22:47 +0000248#ifndef SQLITE_OMIT_TCL_VARIABLE
drh288d37f2005-06-22 08:48:06 +0000249 case '$':
250#endif
251 case ':': {
252 int n = 0;
drh9d74b4c2004-08-24 15:23:34 +0000253 *tokenType = TK_VARIABLE;
drh288d37f2005-06-22 08:48:06 +0000254 for(i=1; (c=z[i])!=0; i++){
255 if( IdChar(c) ){
256 n++;
257#ifndef SQLITE_OMIT_TCL_VARIABLE
258 }else if( c=='(' && n>0 ){
259 do{
260 i++;
261 }while( (c=z[i])!=0 && !isspace(c) && c!=')' );
262 if( c==')' ){
drh895d7472004-08-20 16:02:39 +0000263 i++;
264 }else{
drh288d37f2005-06-22 08:48:06 +0000265 *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000266 }
drh288d37f2005-06-22 08:48:06 +0000267 break;
268 }else if( c==':' && z[i+1]==':' ){
269 i++;
270#endif
271 }else{
272 break;
drh895d7472004-08-20 16:02:39 +0000273 }
274 }
drh288d37f2005-06-22 08:48:06 +0000275 if( n==0 ) *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000276 return i;
drhb7f91642004-10-31 02:22:47 +0000277 }
drhb7f91642004-10-31 02:22:47 +0000278#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +0000279 case 'x': case 'X': {
drhaa756b02004-09-25 15:25:26 +0000280 if( (c=z[1])=='\'' || c=='"' ){
281 int delim = c;
danielk19773fd0a732004-05-27 13:35:19 +0000282 *tokenType = TK_BLOB;
drhaa756b02004-09-25 15:25:26 +0000283 for(i=2; (c=z[i])!=0; i++){
284 if( c==delim ){
danielk19773fd0a732004-05-27 13:35:19 +0000285 if( i%2 ) *tokenType = TK_ILLEGAL;
danielk1977c572ef72004-05-27 09:28:41 +0000286 break;
287 }
drhaa756b02004-09-25 15:25:26 +0000288 if( !isxdigit(c) ){
danielk19773fd0a732004-05-27 13:35:19 +0000289 *tokenType = TK_ILLEGAL;
290 return i;
291 }
danielk1977c572ef72004-05-27 09:28:41 +0000292 }
drhaa756b02004-09-25 15:25:26 +0000293 if( c ) i++;
danielk1977c572ef72004-05-27 09:28:41 +0000294 return i;
295 }
296 /* Otherwise fall through to the next case */
297 }
drhb7f91642004-10-31 02:22:47 +0000298#endif
drh98808ba2001-10-18 12:34:46 +0000299 default: {
drhaa756b02004-09-25 15:25:26 +0000300 if( !IdChar(*z) ){
drh98808ba2001-10-18 12:34:46 +0000301 break;
302 }
drhaa756b02004-09-25 15:25:26 +0000303 for(i=1; IdChar(z[i]); i++){}
danielk1977c60e9b82005-01-31 12:42:29 +0000304 *tokenType = keywordCode((char*)z, i);
drh75897232000-05-29 14:26:00 +0000305 return i;
306 }
drh75897232000-05-29 14:26:00 +0000307 }
308 *tokenType = TK_ILLEGAL;
309 return 1;
310}
danielk1977c60e9b82005-01-31 12:42:29 +0000311int sqlite3GetToken(const unsigned char *z, int *tokenType){
312 return getToken(z, tokenType);
313}
drh75897232000-05-29 14:26:00 +0000314
315/*
316** Run the parser on the given SQL string. The parser structure is
drhb19a2bc2001-09-16 00:13:26 +0000317** passed in. An SQLITE_ status code is returned. If an error occurs
318** and pzErrMsg!=NULL then an error message might be written into
319** memory obtained from malloc() and *pzErrMsg made to point to that
320** error message. Or maybe not.
drh75897232000-05-29 14:26:00 +0000321*/
danielk19774adee202004-05-08 08:23:19 +0000322int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +0000323 int nErr = 0;
324 int i;
325 void *pEngine;
drhb86ccfb2003-01-28 23:13:10 +0000326 int tokenType;
327 int lastTokenParsed = -1;
drh9bb575f2004-09-06 17:24:11 +0000328 sqlite3 *db = pParse->db;
danielk19774adee202004-05-08 08:23:19 +0000329 extern void *sqlite3ParserAlloc(void*(*)(int));
330 extern void sqlite3ParserFree(void*, void(*)(void*));
331 extern int sqlite3Parser(void*, int, Token, Parse*);
drh75897232000-05-29 14:26:00 +0000332
drh6d4abfb2001-10-22 02:58:08 +0000333 db->flags &= ~SQLITE_Interrupt;
drh4c504392000-10-16 22:06:40 +0000334 pParse->rc = SQLITE_OK;
drh75897232000-05-29 14:26:00 +0000335 i = 0;
drh132d8d62005-05-22 20:12:37 +0000336 pEngine = sqlite3ParserAlloc((void*(*)(int))sqlite3MallocX);
drh75897232000-05-29 14:26:00 +0000337 if( pEngine==0 ){
danielk19774adee202004-05-08 08:23:19 +0000338 sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
drhdefc9972005-06-06 14:45:42 +0000339 return SQLITE_NOMEM;
drh75897232000-05-29 14:26:00 +0000340 }
drhfa6bc002004-09-07 16:19:52 +0000341 assert( pParse->sLastToken.dyn==0 );
342 assert( pParse->pNewTable==0 );
343 assert( pParse->pNewTrigger==0 );
344 assert( pParse->nVar==0 );
345 assert( pParse->nVarExpr==0 );
346 assert( pParse->nVarExprAlloc==0 );
347 assert( pParse->apVarExpr==0 );
drh3f7d4e42004-07-24 14:35:58 +0000348 pParse->zTail = pParse->zSql = zSql;
danielk19776f8a5032004-05-10 10:34:51 +0000349 while( sqlite3_malloc_failed==0 && zSql[i]!=0 ){
drh32eb7b42003-01-07 01:44:37 +0000350 assert( i>=0 );
drh75897232000-05-29 14:26:00 +0000351 pParse->sLastToken.z = &zSql[i];
drh32eb7b42003-01-07 01:44:37 +0000352 assert( pParse->sLastToken.dyn==0 );
danielk1977c60e9b82005-01-31 12:42:29 +0000353 pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType);
drh75897232000-05-29 14:26:00 +0000354 i += pParse->sLastToken.n;
drh75897232000-05-29 14:26:00 +0000355 switch( tokenType ){
356 case TK_SPACE:
drh75897232000-05-29 14:26:00 +0000357 case TK_COMMENT: {
drh32eb7b42003-01-07 01:44:37 +0000358 if( (db->flags & SQLITE_Interrupt)!=0 ){
359 pParse->rc = SQLITE_INTERRUPT;
danielk19774adee202004-05-08 08:23:19 +0000360 sqlite3SetString(pzErrMsg, "interrupt", (char*)0);
drh32eb7b42003-01-07 01:44:37 +0000361 goto abort_parse;
362 }
drh75897232000-05-29 14:26:00 +0000363 break;
364 }
drh32eb7b42003-01-07 01:44:37 +0000365 case TK_ILLEGAL: {
drhae29ffb2004-09-25 14:39:18 +0000366 if( pzErrMsg ){
367 sqliteFree(*pzErrMsg);
368 *pzErrMsg = sqlite3MPrintf("unrecognized token: \"%T\"",
369 &pParse->sLastToken);
370 }
drh75897232000-05-29 14:26:00 +0000371 nErr++;
drhcaec2f12003-01-07 02:47:47 +0000372 goto abort_parse;
drh32eb7b42003-01-07 01:44:37 +0000373 }
drh326dce72003-01-29 14:06:07 +0000374 case TK_SEMI: {
375 pParse->zTail = &zSql[i];
376 /* Fall thru into the default case */
377 }
drh32eb7b42003-01-07 01:44:37 +0000378 default: {
danielk19774adee202004-05-08 08:23:19 +0000379 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
drhb86ccfb2003-01-28 23:13:10 +0000380 lastTokenParsed = tokenType;
381 if( pParse->rc!=SQLITE_OK ){
drh32eb7b42003-01-07 01:44:37 +0000382 goto abort_parse;
drh75897232000-05-29 14:26:00 +0000383 }
384 break;
drh32eb7b42003-01-07 01:44:37 +0000385 }
drh75897232000-05-29 14:26:00 +0000386 }
387 }
drh32eb7b42003-01-07 01:44:37 +0000388abort_parse:
drhb86ccfb2003-01-28 23:13:10 +0000389 if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
390 if( lastTokenParsed!=TK_SEMI ){
danielk19774adee202004-05-08 08:23:19 +0000391 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
drh326dce72003-01-29 14:06:07 +0000392 pParse->zTail = &zSql[i];
drh75897232000-05-29 14:26:00 +0000393 }
danielk19774adee202004-05-08 08:23:19 +0000394 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
drh75897232000-05-29 14:26:00 +0000395 }
drh132d8d62005-05-22 20:12:37 +0000396 sqlite3ParserFree(pEngine, sqlite3FreeX);
drh71c697e2004-08-08 23:39:19 +0000397 if( sqlite3_malloc_failed ){
398 pParse->rc = SQLITE_NOMEM;
399 }
drhb86ccfb2003-01-28 23:13:10 +0000400 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
danielk1977f20b21c2004-05-31 23:56:42 +0000401 sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc),
drh41743982003-12-06 21:43:55 +0000402 (char*)0);
drhb86ccfb2003-01-28 23:13:10 +0000403 }
drh75897232000-05-29 14:26:00 +0000404 if( pParse->zErrMsg ){
drhb86ccfb2003-01-28 23:13:10 +0000405 if( pzErrMsg && *pzErrMsg==0 ){
drh75897232000-05-29 14:26:00 +0000406 *pzErrMsg = pParse->zErrMsg;
407 }else{
408 sqliteFree(pParse->zErrMsg);
409 }
drhb86ccfb2003-01-28 23:13:10 +0000410 pParse->zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000411 if( !nErr ) nErr++;
412 }
drh2958a4e2004-11-12 03:56:15 +0000413 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
danielk19774adee202004-05-08 08:23:19 +0000414 sqlite3VdbeDelete(pParse->pVdbe);
drh75897232000-05-29 14:26:00 +0000415 pParse->pVdbe = 0;
416 }
drhfa6bc002004-09-07 16:19:52 +0000417 sqlite3DeleteTable(pParse->db, pParse->pNewTable);
418 sqlite3DeleteTrigger(pParse->pNewTrigger);
419 sqliteFree(pParse->apVarExpr);
drhb86ccfb2003-01-28 23:13:10 +0000420 if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
drh4c504392000-10-16 22:06:40 +0000421 pParse->rc = SQLITE_ERROR;
422 }
drh75897232000-05-29 14:26:00 +0000423 return nErr;
424}
drh7ad43342003-05-04 17:58:25 +0000425
drhccae6022005-02-26 17:31:26 +0000426/* The sqlite3_complete() API may be omitted (to save code space) by
427** defining the following symbol.
428*/
429#ifndef SQLITE_OMIT_COMPLETE
430
drh7ad43342003-05-04 17:58:25 +0000431/*
danielk19776f8a5032004-05-10 10:34:51 +0000432** Token types used by the sqlite3_complete() routine. See the header
drh7ad43342003-05-04 17:58:25 +0000433** comments on that procedure for additional information.
434*/
drhb7f91642004-10-31 02:22:47 +0000435#define tkSEMI 0
436#define tkWS 1
437#define tkOTHER 2
438#define tkEXPLAIN 3
439#define tkCREATE 4
440#define tkTEMP 5
441#define tkTRIGGER 6
442#define tkEND 7
drh7ad43342003-05-04 17:58:25 +0000443
444/*
445** Return TRUE if the given SQL string ends in a semicolon.
446**
447** Special handling is require for CREATE TRIGGER statements.
448** Whenever the CREATE TRIGGER keywords are seen, the statement
449** must end with ";END;".
450**
451** This implementation uses a state machine with 7 states:
452**
453** (0) START At the beginning or end of an SQL statement. This routine
454** returns 1 if it ends in the START state and 0 if it ends
455** in any other state.
456**
drhb7f91642004-10-31 02:22:47 +0000457** (1) NORMAL We are in the middle of statement which ends with a single
458** semicolon.
459**
460** (2) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
drh7ad43342003-05-04 17:58:25 +0000461** a statement.
462**
drhb7f91642004-10-31 02:22:47 +0000463** (3) CREATE The keyword CREATE has been seen at the beginning of a
drh7ad43342003-05-04 17:58:25 +0000464** statement, possibly preceeded by EXPLAIN and/or followed by
465** TEMP or TEMPORARY
466**
drh7ad43342003-05-04 17:58:25 +0000467** (4) TRIGGER We are in the middle of a trigger definition that must be
468** ended by a semicolon, the keyword END, and another semicolon.
469**
470** (5) SEMI We've seen the first semicolon in the ";END;" that occurs at
471** the end of a trigger definition.
472**
473** (6) END We've seen the ";END" of the ";END;" that occurs at the end
474** of a trigger difinition.
475**
476** Transitions between states above are determined by tokens extracted
477** from the input. The following tokens are significant:
478**
drhb7f91642004-10-31 02:22:47 +0000479** (0) tkSEMI A semicolon.
480** (1) tkWS Whitespace
481** (2) tkOTHER Any other SQL token.
482** (3) tkEXPLAIN The "explain" keyword.
483** (4) tkCREATE The "create" keyword.
484** (5) tkTEMP The "temp" or "temporary" keyword.
485** (6) tkTRIGGER The "trigger" keyword.
486** (7) tkEND The "end" keyword.
drh7ad43342003-05-04 17:58:25 +0000487**
488** Whitespace never causes a state transition and is always ignored.
drhb7f91642004-10-31 02:22:47 +0000489**
490** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
491** to recognize the end of a trigger can be omitted. All we have to do
492** is look for a semicolon that is not part of an string or comment.
drh7ad43342003-05-04 17:58:25 +0000493*/
danielk19776f8a5032004-05-10 10:34:51 +0000494int sqlite3_complete(const char *zSql){
drh7ad43342003-05-04 17:58:25 +0000495 u8 state = 0; /* Current state, using numbers defined in header comment */
496 u8 token; /* Value of the next token */
497
drhb7f91642004-10-31 02:22:47 +0000498#ifndef SQLITE_OMIT_TRIGGER
499 /* A complex statement machine used to detect the end of a CREATE TRIGGER
500 ** statement. This is the normal case.
drh7ad43342003-05-04 17:58:25 +0000501 */
502 static const u8 trans[7][8] = {
drhe1e38c42003-05-04 18:30:59 +0000503 /* Token: */
drhb7f91642004-10-31 02:22:47 +0000504 /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */
505 /* 0 START: */ { 0, 0, 1, 2, 3, 1, 1, 1, },
506 /* 1 NORMAL: */ { 0, 1, 1, 1, 1, 1, 1, 1, },
507 /* 2 EXPLAIN: */ { 0, 2, 1, 1, 3, 1, 1, 1, },
508 /* 3 CREATE: */ { 0, 3, 1, 1, 1, 3, 4, 1, },
509 /* 4 TRIGGER: */ { 5, 4, 4, 4, 4, 4, 4, 4, },
510 /* 5 SEMI: */ { 5, 5, 4, 4, 4, 4, 4, 6, },
511 /* 6 END: */ { 0, 6, 4, 4, 4, 4, 4, 4, },
drh7ad43342003-05-04 17:58:25 +0000512 };
drhb7f91642004-10-31 02:22:47 +0000513#else
514 /* If triggers are not suppored by this compile then the statement machine
515 ** used to detect the end of a statement is much simplier
516 */
517 static const u8 trans[2][3] = {
518 /* Token: */
519 /* State: ** SEMI WS OTHER */
520 /* 0 START: */ { 0, 0, 1, },
521 /* 1 NORMAL: */ { 0, 1, 1, },
522 };
523#endif /* SQLITE_OMIT_TRIGGER */
drh7ad43342003-05-04 17:58:25 +0000524
525 while( *zSql ){
526 switch( *zSql ){
527 case ';': { /* A semicolon */
528 token = tkSEMI;
529 break;
530 }
531 case ' ':
532 case '\r':
533 case '\t':
534 case '\n':
535 case '\f': { /* White space is ignored */
536 token = tkWS;
537 break;
538 }
539 case '/': { /* C-style comments */
540 if( zSql[1]!='*' ){
541 token = tkOTHER;
542 break;
543 }
544 zSql += 2;
545 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
546 if( zSql[0]==0 ) return 0;
547 zSql++;
548 token = tkWS;
549 break;
550 }
551 case '-': { /* SQL-style comments from "--" to end of line */
552 if( zSql[1]!='-' ){
553 token = tkOTHER;
554 break;
555 }
556 while( *zSql && *zSql!='\n' ){ zSql++; }
557 if( *zSql==0 ) return state==0;
558 token = tkWS;
559 break;
560 }
561 case '[': { /* Microsoft-style identifiers in [...] */
562 zSql++;
563 while( *zSql && *zSql!=']' ){ zSql++; }
564 if( *zSql==0 ) return 0;
565 token = tkOTHER;
566 break;
567 }
568 case '"': /* single- and double-quoted strings */
569 case '\'': {
570 int c = *zSql;
571 zSql++;
572 while( *zSql && *zSql!=c ){ zSql++; }
573 if( *zSql==0 ) return 0;
574 token = tkOTHER;
575 break;
576 }
577 default: {
drhaa756b02004-09-25 15:25:26 +0000578 int c;
579 if( IdChar((u8)*zSql) ){
drh7ad43342003-05-04 17:58:25 +0000580 /* Keywords and unquoted identifiers */
581 int nId;
drhaa756b02004-09-25 15:25:26 +0000582 for(nId=1; IdChar(zSql[nId]); nId++){}
drhb7f91642004-10-31 02:22:47 +0000583#ifdef SQLITE_OMIT_TRIGGER
584 token = tkOTHER;
585#else
drh7ad43342003-05-04 17:58:25 +0000586 switch( *zSql ){
587 case 'c': case 'C': {
danielk19774adee202004-05-08 08:23:19 +0000588 if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
drh7ad43342003-05-04 17:58:25 +0000589 token = tkCREATE;
590 }else{
591 token = tkOTHER;
592 }
593 break;
594 }
595 case 't': case 'T': {
danielk19774adee202004-05-08 08:23:19 +0000596 if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
drh7ad43342003-05-04 17:58:25 +0000597 token = tkTRIGGER;
danielk19774adee202004-05-08 08:23:19 +0000598 }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
drh7ad43342003-05-04 17:58:25 +0000599 token = tkTEMP;
danielk19774adee202004-05-08 08:23:19 +0000600 }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
drh7ad43342003-05-04 17:58:25 +0000601 token = tkTEMP;
602 }else{
603 token = tkOTHER;
604 }
605 break;
606 }
607 case 'e': case 'E': {
danielk19774adee202004-05-08 08:23:19 +0000608 if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
drh7ad43342003-05-04 17:58:25 +0000609 token = tkEND;
drhb7f91642004-10-31 02:22:47 +0000610 }else
611#ifndef SQLITE_OMIT_EXPLAIN
612 if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
drh7ad43342003-05-04 17:58:25 +0000613 token = tkEXPLAIN;
drhb7f91642004-10-31 02:22:47 +0000614 }else
615#endif
616 {
drh7ad43342003-05-04 17:58:25 +0000617 token = tkOTHER;
618 }
619 break;
620 }
621 default: {
622 token = tkOTHER;
623 break;
624 }
625 }
drhb7f91642004-10-31 02:22:47 +0000626#endif /* SQLITE_OMIT_TRIGGER */
drh7ad43342003-05-04 17:58:25 +0000627 zSql += nId-1;
628 }else{
629 /* Operators and special symbols */
630 token = tkOTHER;
631 }
632 break;
633 }
634 }
635 state = trans[state][token];
636 zSql++;
637 }
638 return state==0;
639}
danielk197761de0d12004-05-27 23:56:16 +0000640
drhb7f91642004-10-31 02:22:47 +0000641#ifndef SQLITE_OMIT_UTF16
danielk197761de0d12004-05-27 23:56:16 +0000642/*
643** This routine is the same as the sqlite3_complete() routine described
644** above, except that the parameter is required to be UTF-16 encoded, not
645** UTF-8.
646*/
647int sqlite3_complete16(const void *zSql){
danielk1977bfd6cce2004-06-18 04:24:54 +0000648 sqlite3_value *pVal;
danielk19775314c4d2004-06-18 06:02:35 +0000649 char const *zSql8;
danielk1977bfd6cce2004-06-18 04:24:54 +0000650 int rc = 0;
651
652 pVal = sqlite3ValueNew();
653 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
654 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
655 if( zSql8 ){
656 rc = sqlite3_complete(zSql8);
danielk1977bfd6cce2004-06-18 04:24:54 +0000657 }
658 sqlite3ValueFree(pVal);
danielk197761de0d12004-05-27 23:56:16 +0000659 return rc;
660}
drhb7f91642004-10-31 02:22:47 +0000661#endif /* SQLITE_OMIT_UTF16 */
drhccae6022005-02-26 17:31:26 +0000662#endif /* SQLITE_OMIT_COMPLETE */