blob: a54abb96c4f36e8aa3029cdd2559adf8c167f477 [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**
drhdefc9972005-06-06 14:45:42 +000018** $Id: tokenize.c,v 1.103 2005/06/06 14:45:43 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 }
drh4e0cff62004-11-05 05:10:28 +0000186 case '#': {
drh2958a4e2004-11-12 03:56:15 +0000187 for(i=1; isdigit(z[i]) || (i==1 && z[1]=='-'); i++){}
drh4e0cff62004-11-05 05:10:28 +0000188 *tokenType = TK_REGISTER;
189 return i;
190 }
drh75897232000-05-29 14:26:00 +0000191 case '\'': case '"': {
192 int delim = z[0];
drhaa756b02004-09-25 15:25:26 +0000193 for(i=1; (c=z[i])!=0; i++){
194 if( c==delim ){
drh75897232000-05-29 14:26:00 +0000195 if( z[i+1]==delim ){
196 i++;
197 }else{
198 break;
199 }
200 }
201 }
drhaa756b02004-09-25 15:25:26 +0000202 if( c ) i++;
drh75897232000-05-29 14:26:00 +0000203 *tokenType = TK_STRING;
204 return i;
205 }
206 case '.': {
drhbb07e9a2003-04-16 02:17:35 +0000207 *tokenType = TK_DOT;
208 return 1;
drh75897232000-05-29 14:26:00 +0000209 }
210 case '0': case '1': case '2': case '3': case '4':
211 case '5': case '6': case '7': case '8': case '9': {
drhc837e702000-06-08 16:26:24 +0000212 *tokenType = TK_INTEGER;
drh32eb7b42003-01-07 01:44:37 +0000213 for(i=1; isdigit(z[i]); i++){}
drhb7f91642004-10-31 02:22:47 +0000214#ifndef SQLITE_OMIT_FLOATING_POINT
drhbb07e9a2003-04-16 02:17:35 +0000215 if( z[i]=='.' && isdigit(z[i+1]) ){
216 i += 2;
drh32eb7b42003-01-07 01:44:37 +0000217 while( isdigit(z[i]) ){ i++; }
drhc837e702000-06-08 16:26:24 +0000218 *tokenType = TK_FLOAT;
219 }
220 if( (z[i]=='e' || z[i]=='E') &&
drh75897232000-05-29 14:26:00 +0000221 ( isdigit(z[i+1])
222 || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
223 )
drhc837e702000-06-08 16:26:24 +0000224 ){
225 i += 2;
drh32eb7b42003-01-07 01:44:37 +0000226 while( isdigit(z[i]) ){ i++; }
drh75897232000-05-29 14:26:00 +0000227 *tokenType = TK_FLOAT;
drh75897232000-05-29 14:26:00 +0000228 }
drhb7f91642004-10-31 02:22:47 +0000229#endif
drh75897232000-05-29 14:26:00 +0000230 return i;
231 }
drh2f4392f2002-02-14 21:42:51 +0000232 case '[': {
drhaa756b02004-09-25 15:25:26 +0000233 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
drh2f4392f2002-02-14 21:42:51 +0000234 *tokenType = TK_ID;
235 return i;
236 }
drh7c972de2003-09-06 22:18:07 +0000237 case '?': {
drh50457892003-09-06 01:10:47 +0000238 *tokenType = TK_VARIABLE;
drhfa6bc002004-09-07 16:19:52 +0000239 for(i=1; isdigit(z[i]); i++){}
240 return i;
drh50457892003-09-06 01:10:47 +0000241 }
drh895d7472004-08-20 16:02:39 +0000242 case ':': {
drhaa756b02004-09-25 15:25:26 +0000243 for(i=1; IdChar(z[i]); i++){}
drh2c6674c2004-08-25 04:07:01 +0000244 *tokenType = i>1 ? TK_VARIABLE : TK_ILLEGAL;
245 return i;
drh895d7472004-08-20 16:02:39 +0000246 }
drhb7f91642004-10-31 02:22:47 +0000247#ifndef SQLITE_OMIT_TCL_VARIABLE
drh895d7472004-08-20 16:02:39 +0000248 case '$': {
drh9d74b4c2004-08-24 15:23:34 +0000249 *tokenType = TK_VARIABLE;
drh895d7472004-08-20 16:02:39 +0000250 if( z[1]=='{' ){
251 int nBrace = 1;
252 for(i=2; (c=z[i])!=0 && nBrace; i++){
253 if( c=='{' ){
254 nBrace++;
255 }else if( c=='}' ){
256 nBrace--;
257 }
258 }
drh9d74b4c2004-08-24 15:23:34 +0000259 if( c==0 ) *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000260 }else{
261 int n = 0;
262 for(i=1; (c=z[i])!=0; i++){
263 if( isalnum(c) || c=='_' ){
264 n++;
265 }else if( c=='(' && n>0 ){
266 do{
267 i++;
268 }while( (c=z[i])!=0 && !isspace(c) && c!=')' );
269 if( c==')' ){
270 i++;
drh895d7472004-08-20 16:02:39 +0000271 }else{
272 *tokenType = TK_ILLEGAL;
273 }
274 break;
275 }else if( c==':' && z[i+1]==':' ){
276 i++;
277 }else{
drh895d7472004-08-20 16:02:39 +0000278 break;
279 }
280 }
drh9d74b4c2004-08-24 15:23:34 +0000281 if( n==0 ) *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000282 }
283 return i;
drhb7f91642004-10-31 02:22:47 +0000284 }
285#endif
286#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +0000287 case 'x': case 'X': {
drhaa756b02004-09-25 15:25:26 +0000288 if( (c=z[1])=='\'' || c=='"' ){
289 int delim = c;
danielk19773fd0a732004-05-27 13:35:19 +0000290 *tokenType = TK_BLOB;
drhaa756b02004-09-25 15:25:26 +0000291 for(i=2; (c=z[i])!=0; i++){
292 if( c==delim ){
danielk19773fd0a732004-05-27 13:35:19 +0000293 if( i%2 ) *tokenType = TK_ILLEGAL;
danielk1977c572ef72004-05-27 09:28:41 +0000294 break;
295 }
drhaa756b02004-09-25 15:25:26 +0000296 if( !isxdigit(c) ){
danielk19773fd0a732004-05-27 13:35:19 +0000297 *tokenType = TK_ILLEGAL;
298 return i;
299 }
danielk1977c572ef72004-05-27 09:28:41 +0000300 }
drhaa756b02004-09-25 15:25:26 +0000301 if( c ) i++;
danielk1977c572ef72004-05-27 09:28:41 +0000302 return i;
303 }
304 /* Otherwise fall through to the next case */
305 }
drhb7f91642004-10-31 02:22:47 +0000306#endif
drh98808ba2001-10-18 12:34:46 +0000307 default: {
drhaa756b02004-09-25 15:25:26 +0000308 if( !IdChar(*z) ){
drh98808ba2001-10-18 12:34:46 +0000309 break;
310 }
drhaa756b02004-09-25 15:25:26 +0000311 for(i=1; IdChar(z[i]); i++){}
danielk1977c60e9b82005-01-31 12:42:29 +0000312 *tokenType = keywordCode((char*)z, i);
drh75897232000-05-29 14:26:00 +0000313 return i;
314 }
drh75897232000-05-29 14:26:00 +0000315 }
316 *tokenType = TK_ILLEGAL;
317 return 1;
318}
danielk1977c60e9b82005-01-31 12:42:29 +0000319int sqlite3GetToken(const unsigned char *z, int *tokenType){
320 return getToken(z, tokenType);
321}
drh75897232000-05-29 14:26:00 +0000322
323/*
324** Run the parser on the given SQL string. The parser structure is
drhb19a2bc2001-09-16 00:13:26 +0000325** passed in. An SQLITE_ status code is returned. If an error occurs
326** and pzErrMsg!=NULL then an error message might be written into
327** memory obtained from malloc() and *pzErrMsg made to point to that
328** error message. Or maybe not.
drh75897232000-05-29 14:26:00 +0000329*/
danielk19774adee202004-05-08 08:23:19 +0000330int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +0000331 int nErr = 0;
332 int i;
333 void *pEngine;
drhb86ccfb2003-01-28 23:13:10 +0000334 int tokenType;
335 int lastTokenParsed = -1;
drh9bb575f2004-09-06 17:24:11 +0000336 sqlite3 *db = pParse->db;
danielk19774adee202004-05-08 08:23:19 +0000337 extern void *sqlite3ParserAlloc(void*(*)(int));
338 extern void sqlite3ParserFree(void*, void(*)(void*));
339 extern int sqlite3Parser(void*, int, Token, Parse*);
drh75897232000-05-29 14:26:00 +0000340
drh6d4abfb2001-10-22 02:58:08 +0000341 db->flags &= ~SQLITE_Interrupt;
drh4c504392000-10-16 22:06:40 +0000342 pParse->rc = SQLITE_OK;
drh75897232000-05-29 14:26:00 +0000343 i = 0;
drh132d8d62005-05-22 20:12:37 +0000344 pEngine = sqlite3ParserAlloc((void*(*)(int))sqlite3MallocX);
drh75897232000-05-29 14:26:00 +0000345 if( pEngine==0 ){
danielk19774adee202004-05-08 08:23:19 +0000346 sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
drhdefc9972005-06-06 14:45:42 +0000347 return SQLITE_NOMEM;
drh75897232000-05-29 14:26:00 +0000348 }
drhfa6bc002004-09-07 16:19:52 +0000349 assert( pParse->sLastToken.dyn==0 );
350 assert( pParse->pNewTable==0 );
351 assert( pParse->pNewTrigger==0 );
352 assert( pParse->nVar==0 );
353 assert( pParse->nVarExpr==0 );
354 assert( pParse->nVarExprAlloc==0 );
355 assert( pParse->apVarExpr==0 );
drh3f7d4e42004-07-24 14:35:58 +0000356 pParse->zTail = pParse->zSql = zSql;
danielk19776f8a5032004-05-10 10:34:51 +0000357 while( sqlite3_malloc_failed==0 && zSql[i]!=0 ){
drh32eb7b42003-01-07 01:44:37 +0000358 assert( i>=0 );
drh75897232000-05-29 14:26:00 +0000359 pParse->sLastToken.z = &zSql[i];
drh32eb7b42003-01-07 01:44:37 +0000360 assert( pParse->sLastToken.dyn==0 );
danielk1977c60e9b82005-01-31 12:42:29 +0000361 pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType);
drh75897232000-05-29 14:26:00 +0000362 i += pParse->sLastToken.n;
drh75897232000-05-29 14:26:00 +0000363 switch( tokenType ){
364 case TK_SPACE:
drh75897232000-05-29 14:26:00 +0000365 case TK_COMMENT: {
drh32eb7b42003-01-07 01:44:37 +0000366 if( (db->flags & SQLITE_Interrupt)!=0 ){
367 pParse->rc = SQLITE_INTERRUPT;
danielk19774adee202004-05-08 08:23:19 +0000368 sqlite3SetString(pzErrMsg, "interrupt", (char*)0);
drh32eb7b42003-01-07 01:44:37 +0000369 goto abort_parse;
370 }
drh75897232000-05-29 14:26:00 +0000371 break;
372 }
drh32eb7b42003-01-07 01:44:37 +0000373 case TK_ILLEGAL: {
drhae29ffb2004-09-25 14:39:18 +0000374 if( pzErrMsg ){
375 sqliteFree(*pzErrMsg);
376 *pzErrMsg = sqlite3MPrintf("unrecognized token: \"%T\"",
377 &pParse->sLastToken);
378 }
drh75897232000-05-29 14:26:00 +0000379 nErr++;
drhcaec2f12003-01-07 02:47:47 +0000380 goto abort_parse;
drh32eb7b42003-01-07 01:44:37 +0000381 }
drh326dce72003-01-29 14:06:07 +0000382 case TK_SEMI: {
383 pParse->zTail = &zSql[i];
384 /* Fall thru into the default case */
385 }
drh32eb7b42003-01-07 01:44:37 +0000386 default: {
danielk19774adee202004-05-08 08:23:19 +0000387 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
drhb86ccfb2003-01-28 23:13:10 +0000388 lastTokenParsed = tokenType;
389 if( pParse->rc!=SQLITE_OK ){
drh32eb7b42003-01-07 01:44:37 +0000390 goto abort_parse;
drh75897232000-05-29 14:26:00 +0000391 }
392 break;
drh32eb7b42003-01-07 01:44:37 +0000393 }
drh75897232000-05-29 14:26:00 +0000394 }
395 }
drh32eb7b42003-01-07 01:44:37 +0000396abort_parse:
drhb86ccfb2003-01-28 23:13:10 +0000397 if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
398 if( lastTokenParsed!=TK_SEMI ){
danielk19774adee202004-05-08 08:23:19 +0000399 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
drh326dce72003-01-29 14:06:07 +0000400 pParse->zTail = &zSql[i];
drh75897232000-05-29 14:26:00 +0000401 }
danielk19774adee202004-05-08 08:23:19 +0000402 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
drh75897232000-05-29 14:26:00 +0000403 }
drh132d8d62005-05-22 20:12:37 +0000404 sqlite3ParserFree(pEngine, sqlite3FreeX);
drh71c697e2004-08-08 23:39:19 +0000405 if( sqlite3_malloc_failed ){
406 pParse->rc = SQLITE_NOMEM;
407 }
drhb86ccfb2003-01-28 23:13:10 +0000408 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
danielk1977f20b21c2004-05-31 23:56:42 +0000409 sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc),
drh41743982003-12-06 21:43:55 +0000410 (char*)0);
drhb86ccfb2003-01-28 23:13:10 +0000411 }
drh75897232000-05-29 14:26:00 +0000412 if( pParse->zErrMsg ){
drhb86ccfb2003-01-28 23:13:10 +0000413 if( pzErrMsg && *pzErrMsg==0 ){
drh75897232000-05-29 14:26:00 +0000414 *pzErrMsg = pParse->zErrMsg;
415 }else{
416 sqliteFree(pParse->zErrMsg);
417 }
drhb86ccfb2003-01-28 23:13:10 +0000418 pParse->zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000419 if( !nErr ) nErr++;
420 }
drh2958a4e2004-11-12 03:56:15 +0000421 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
danielk19774adee202004-05-08 08:23:19 +0000422 sqlite3VdbeDelete(pParse->pVdbe);
drh75897232000-05-29 14:26:00 +0000423 pParse->pVdbe = 0;
424 }
drhfa6bc002004-09-07 16:19:52 +0000425 sqlite3DeleteTable(pParse->db, pParse->pNewTable);
426 sqlite3DeleteTrigger(pParse->pNewTrigger);
427 sqliteFree(pParse->apVarExpr);
drhb86ccfb2003-01-28 23:13:10 +0000428 if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
drh4c504392000-10-16 22:06:40 +0000429 pParse->rc = SQLITE_ERROR;
430 }
drh75897232000-05-29 14:26:00 +0000431 return nErr;
432}
drh7ad43342003-05-04 17:58:25 +0000433
drhccae6022005-02-26 17:31:26 +0000434/* The sqlite3_complete() API may be omitted (to save code space) by
435** defining the following symbol.
436*/
437#ifndef SQLITE_OMIT_COMPLETE
438
drh7ad43342003-05-04 17:58:25 +0000439/*
danielk19776f8a5032004-05-10 10:34:51 +0000440** Token types used by the sqlite3_complete() routine. See the header
drh7ad43342003-05-04 17:58:25 +0000441** comments on that procedure for additional information.
442*/
drhb7f91642004-10-31 02:22:47 +0000443#define tkSEMI 0
444#define tkWS 1
445#define tkOTHER 2
446#define tkEXPLAIN 3
447#define tkCREATE 4
448#define tkTEMP 5
449#define tkTRIGGER 6
450#define tkEND 7
drh7ad43342003-05-04 17:58:25 +0000451
452/*
453** Return TRUE if the given SQL string ends in a semicolon.
454**
455** Special handling is require for CREATE TRIGGER statements.
456** Whenever the CREATE TRIGGER keywords are seen, the statement
457** must end with ";END;".
458**
459** This implementation uses a state machine with 7 states:
460**
461** (0) START At the beginning or end of an SQL statement. This routine
462** returns 1 if it ends in the START state and 0 if it ends
463** in any other state.
464**
drhb7f91642004-10-31 02:22:47 +0000465** (1) NORMAL We are in the middle of statement which ends with a single
466** semicolon.
467**
468** (2) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
drh7ad43342003-05-04 17:58:25 +0000469** a statement.
470**
drhb7f91642004-10-31 02:22:47 +0000471** (3) CREATE The keyword CREATE has been seen at the beginning of a
drh7ad43342003-05-04 17:58:25 +0000472** statement, possibly preceeded by EXPLAIN and/or followed by
473** TEMP or TEMPORARY
474**
drh7ad43342003-05-04 17:58:25 +0000475** (4) TRIGGER We are in the middle of a trigger definition that must be
476** ended by a semicolon, the keyword END, and another semicolon.
477**
478** (5) SEMI We've seen the first semicolon in the ";END;" that occurs at
479** the end of a trigger definition.
480**
481** (6) END We've seen the ";END" of the ";END;" that occurs at the end
482** of a trigger difinition.
483**
484** Transitions between states above are determined by tokens extracted
485** from the input. The following tokens are significant:
486**
drhb7f91642004-10-31 02:22:47 +0000487** (0) tkSEMI A semicolon.
488** (1) tkWS Whitespace
489** (2) tkOTHER Any other SQL token.
490** (3) tkEXPLAIN The "explain" keyword.
491** (4) tkCREATE The "create" keyword.
492** (5) tkTEMP The "temp" or "temporary" keyword.
493** (6) tkTRIGGER The "trigger" keyword.
494** (7) tkEND The "end" keyword.
drh7ad43342003-05-04 17:58:25 +0000495**
496** Whitespace never causes a state transition and is always ignored.
drhb7f91642004-10-31 02:22:47 +0000497**
498** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
499** to recognize the end of a trigger can be omitted. All we have to do
500** is look for a semicolon that is not part of an string or comment.
drh7ad43342003-05-04 17:58:25 +0000501*/
danielk19776f8a5032004-05-10 10:34:51 +0000502int sqlite3_complete(const char *zSql){
drh7ad43342003-05-04 17:58:25 +0000503 u8 state = 0; /* Current state, using numbers defined in header comment */
504 u8 token; /* Value of the next token */
505
drhb7f91642004-10-31 02:22:47 +0000506#ifndef SQLITE_OMIT_TRIGGER
507 /* A complex statement machine used to detect the end of a CREATE TRIGGER
508 ** statement. This is the normal case.
drh7ad43342003-05-04 17:58:25 +0000509 */
510 static const u8 trans[7][8] = {
drhe1e38c42003-05-04 18:30:59 +0000511 /* Token: */
drhb7f91642004-10-31 02:22:47 +0000512 /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */
513 /* 0 START: */ { 0, 0, 1, 2, 3, 1, 1, 1, },
514 /* 1 NORMAL: */ { 0, 1, 1, 1, 1, 1, 1, 1, },
515 /* 2 EXPLAIN: */ { 0, 2, 1, 1, 3, 1, 1, 1, },
516 /* 3 CREATE: */ { 0, 3, 1, 1, 1, 3, 4, 1, },
517 /* 4 TRIGGER: */ { 5, 4, 4, 4, 4, 4, 4, 4, },
518 /* 5 SEMI: */ { 5, 5, 4, 4, 4, 4, 4, 6, },
519 /* 6 END: */ { 0, 6, 4, 4, 4, 4, 4, 4, },
drh7ad43342003-05-04 17:58:25 +0000520 };
drhb7f91642004-10-31 02:22:47 +0000521#else
522 /* If triggers are not suppored by this compile then the statement machine
523 ** used to detect the end of a statement is much simplier
524 */
525 static const u8 trans[2][3] = {
526 /* Token: */
527 /* State: ** SEMI WS OTHER */
528 /* 0 START: */ { 0, 0, 1, },
529 /* 1 NORMAL: */ { 0, 1, 1, },
530 };
531#endif /* SQLITE_OMIT_TRIGGER */
drh7ad43342003-05-04 17:58:25 +0000532
533 while( *zSql ){
534 switch( *zSql ){
535 case ';': { /* A semicolon */
536 token = tkSEMI;
537 break;
538 }
539 case ' ':
540 case '\r':
541 case '\t':
542 case '\n':
543 case '\f': { /* White space is ignored */
544 token = tkWS;
545 break;
546 }
547 case '/': { /* C-style comments */
548 if( zSql[1]!='*' ){
549 token = tkOTHER;
550 break;
551 }
552 zSql += 2;
553 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
554 if( zSql[0]==0 ) return 0;
555 zSql++;
556 token = tkWS;
557 break;
558 }
559 case '-': { /* SQL-style comments from "--" to end of line */
560 if( zSql[1]!='-' ){
561 token = tkOTHER;
562 break;
563 }
564 while( *zSql && *zSql!='\n' ){ zSql++; }
565 if( *zSql==0 ) return state==0;
566 token = tkWS;
567 break;
568 }
569 case '[': { /* Microsoft-style identifiers in [...] */
570 zSql++;
571 while( *zSql && *zSql!=']' ){ zSql++; }
572 if( *zSql==0 ) return 0;
573 token = tkOTHER;
574 break;
575 }
576 case '"': /* single- and double-quoted strings */
577 case '\'': {
578 int c = *zSql;
579 zSql++;
580 while( *zSql && *zSql!=c ){ zSql++; }
581 if( *zSql==0 ) return 0;
582 token = tkOTHER;
583 break;
584 }
585 default: {
drhaa756b02004-09-25 15:25:26 +0000586 int c;
587 if( IdChar((u8)*zSql) ){
drh7ad43342003-05-04 17:58:25 +0000588 /* Keywords and unquoted identifiers */
589 int nId;
drhaa756b02004-09-25 15:25:26 +0000590 for(nId=1; IdChar(zSql[nId]); nId++){}
drhb7f91642004-10-31 02:22:47 +0000591#ifdef SQLITE_OMIT_TRIGGER
592 token = tkOTHER;
593#else
drh7ad43342003-05-04 17:58:25 +0000594 switch( *zSql ){
595 case 'c': case 'C': {
danielk19774adee202004-05-08 08:23:19 +0000596 if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
drh7ad43342003-05-04 17:58:25 +0000597 token = tkCREATE;
598 }else{
599 token = tkOTHER;
600 }
601 break;
602 }
603 case 't': case 'T': {
danielk19774adee202004-05-08 08:23:19 +0000604 if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
drh7ad43342003-05-04 17:58:25 +0000605 token = tkTRIGGER;
danielk19774adee202004-05-08 08:23:19 +0000606 }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
drh7ad43342003-05-04 17:58:25 +0000607 token = tkTEMP;
danielk19774adee202004-05-08 08:23:19 +0000608 }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
drh7ad43342003-05-04 17:58:25 +0000609 token = tkTEMP;
610 }else{
611 token = tkOTHER;
612 }
613 break;
614 }
615 case 'e': case 'E': {
danielk19774adee202004-05-08 08:23:19 +0000616 if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
drh7ad43342003-05-04 17:58:25 +0000617 token = tkEND;
drhb7f91642004-10-31 02:22:47 +0000618 }else
619#ifndef SQLITE_OMIT_EXPLAIN
620 if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
drh7ad43342003-05-04 17:58:25 +0000621 token = tkEXPLAIN;
drhb7f91642004-10-31 02:22:47 +0000622 }else
623#endif
624 {
drh7ad43342003-05-04 17:58:25 +0000625 token = tkOTHER;
626 }
627 break;
628 }
629 default: {
630 token = tkOTHER;
631 break;
632 }
633 }
drhb7f91642004-10-31 02:22:47 +0000634#endif /* SQLITE_OMIT_TRIGGER */
drh7ad43342003-05-04 17:58:25 +0000635 zSql += nId-1;
636 }else{
637 /* Operators and special symbols */
638 token = tkOTHER;
639 }
640 break;
641 }
642 }
643 state = trans[state][token];
644 zSql++;
645 }
646 return state==0;
647}
danielk197761de0d12004-05-27 23:56:16 +0000648
drhb7f91642004-10-31 02:22:47 +0000649#ifndef SQLITE_OMIT_UTF16
danielk197761de0d12004-05-27 23:56:16 +0000650/*
651** This routine is the same as the sqlite3_complete() routine described
652** above, except that the parameter is required to be UTF-16 encoded, not
653** UTF-8.
654*/
655int sqlite3_complete16(const void *zSql){
danielk1977bfd6cce2004-06-18 04:24:54 +0000656 sqlite3_value *pVal;
danielk19775314c4d2004-06-18 06:02:35 +0000657 char const *zSql8;
danielk1977bfd6cce2004-06-18 04:24:54 +0000658 int rc = 0;
659
660 pVal = sqlite3ValueNew();
661 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
662 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
663 if( zSql8 ){
664 rc = sqlite3_complete(zSql8);
danielk1977bfd6cce2004-06-18 04:24:54 +0000665 }
666 sqlite3ValueFree(pVal);
danielk197761de0d12004-05-27 23:56:16 +0000667 return rc;
668}
drhb7f91642004-10-31 02:22:47 +0000669#endif /* SQLITE_OMIT_UTF16 */
drhccae6022005-02-26 17:31:26 +0000670#endif /* SQLITE_OMIT_COMPLETE */