drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 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 | ** |
danielk1977 | c60e9b8 | 2005-01-31 12:42:29 +0000 | [diff] [blame^] | 18 | ** $Id: tokenize.c,v 1.100 2005/01/31 12:42:29 danielk1977 Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 19 | */ |
| 20 | #include "sqliteInt.h" |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 21 | #include "os.h" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 22 | #include <ctype.h> |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 24 | |
| 25 | /* |
drh | 52fb6d7 | 2004-11-03 03:59:57 +0000 | [diff] [blame] | 26 | ** 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 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 28 | ** returned. If the input is not a keyword, TK_ID is returned. |
drh | 2090a0e | 2004-10-07 19:03:01 +0000 | [diff] [blame] | 29 | ** |
| 30 | ** The implementation of this routine was generated by a program, |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 31 | ** mkkeywordhash.h, located in the tool subdirectory of the distribution. |
drh | 52fb6d7 | 2004-11-03 03:59:57 +0000 | [diff] [blame] | 32 | ** The output of the mkkeywordhash.c program is written into a file |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 33 | ** named keywordhash.h and then included into this source file by |
drh | 52fb6d7 | 2004-11-03 03:59:57 +0000 | [diff] [blame] | 34 | ** the #include below. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 35 | */ |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 36 | #include "keywordhash.h" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 37 | |
drh | 40f20f7 | 2004-10-23 05:10:18 +0000 | [diff] [blame] | 38 | |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 39 | /* |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 40 | ** 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. |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 46 | ** |
| 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. |
drh | a0d1f66 | 2005-01-11 17:59:47 +0000 | [diff] [blame] | 52 | ** |
| 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. |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 57 | */ |
| 58 | static const char isIdChar[] = { |
| 59 | /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ |
drh | a0d1f66 | 2005-01-11 17:59:47 +0000 | [diff] [blame] | 60 | 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 61 | 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 */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
drh | a0d1f66 | 2005-01-11 17:59:47 +0000 | [diff] [blame] | 68 | #define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && isIdChar[c-0x20])) |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 69 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 70 | /* |
drh | 61b487d | 2003-09-12 02:08:14 +0000 | [diff] [blame] | 71 | ** Return the length of the token that begins at z[0]. |
| 72 | ** Store the token type in *tokenType before returning. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 73 | */ |
danielk1977 | c60e9b8 | 2005-01-31 12:42:29 +0000 | [diff] [blame^] | 74 | static int getToken(const unsigned char *z, int *tokenType){ |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 75 | int i, c; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 76 | switch( *z ){ |
drh | 30cab80 | 2000-08-09 17:17:25 +0000 | [diff] [blame] | 77 | case ' ': case '\t': case '\n': case '\f': case '\r': { |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 78 | for(i=1; isspace(z[i]); i++){} |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 79 | *tokenType = TK_SPACE; |
| 80 | return i; |
| 81 | } |
| 82 | case '-': { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 83 | if( z[1]=='-' ){ |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 84 | for(i=2; (c=z[i])!=0 && c!='\n'; i++){} |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 85 | *tokenType = TK_COMMENT; |
| 86 | return i; |
| 87 | } |
| 88 | *tokenType = TK_MINUS; |
| 89 | return 1; |
| 90 | } |
| 91 | case '(': { |
drh | dab3518 | 2003-09-27 13:39:38 +0000 | [diff] [blame] | 92 | *tokenType = TK_LP; |
| 93 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 94 | } |
| 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 '/': { |
drh | 66105a8 | 2002-08-27 14:28:29 +0000 | [diff] [blame] | 112 | if( z[1]!='*' || z[2]==0 ){ |
| 113 | *tokenType = TK_SLASH; |
| 114 | return 1; |
| 115 | } |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 116 | for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} |
| 117 | if( c ) i++; |
drh | 66105a8 | 2002-08-27 14:28:29 +0000 | [diff] [blame] | 118 | *tokenType = TK_COMMENT; |
| 119 | return i; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 120 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 121 | case '%': { |
| 122 | *tokenType = TK_REM; |
| 123 | return 1; |
| 124 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 125 | case '=': { |
| 126 | *tokenType = TK_EQ; |
| 127 | return 1 + (z[1]=='='); |
| 128 | } |
| 129 | case '<': { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 130 | if( (c=z[1])=='=' ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 131 | *tokenType = TK_LE; |
| 132 | return 2; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 133 | }else if( c=='>' ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 134 | *tokenType = TK_NE; |
| 135 | return 2; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 136 | }else if( c=='<' ){ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 137 | *tokenType = TK_LSHIFT; |
| 138 | return 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 139 | }else{ |
| 140 | *tokenType = TK_LT; |
| 141 | return 1; |
| 142 | } |
| 143 | } |
| 144 | case '>': { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 145 | if( (c=z[1])=='=' ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 146 | *tokenType = TK_GE; |
| 147 | return 2; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 148 | }else if( c=='>' ){ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 149 | *tokenType = TK_RSHIFT; |
| 150 | return 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 151 | }else{ |
| 152 | *tokenType = TK_GT; |
| 153 | return 1; |
| 154 | } |
| 155 | } |
| 156 | case '!': { |
| 157 | if( z[1]!='=' ){ |
| 158 | *tokenType = TK_ILLEGAL; |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 159 | return 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 160 | }else{ |
| 161 | *tokenType = TK_NE; |
| 162 | return 2; |
| 163 | } |
| 164 | } |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 165 | case '|': { |
| 166 | if( z[1]!='|' ){ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 167 | *tokenType = TK_BITOR; |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 168 | return 1; |
| 169 | }else{ |
| 170 | *tokenType = TK_CONCAT; |
| 171 | return 2; |
| 172 | } |
| 173 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 174 | case ',': { |
| 175 | *tokenType = TK_COMMA; |
| 176 | return 1; |
| 177 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 178 | case '&': { |
| 179 | *tokenType = TK_BITAND; |
| 180 | return 1; |
| 181 | } |
| 182 | case '~': { |
| 183 | *tokenType = TK_BITNOT; |
| 184 | return 1; |
| 185 | } |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 186 | case '#': { |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 187 | for(i=1; isdigit(z[i]) || (i==1 && z[1]=='-'); i++){} |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 188 | *tokenType = TK_REGISTER; |
| 189 | return i; |
| 190 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 191 | case '\'': case '"': { |
| 192 | int delim = z[0]; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 193 | for(i=1; (c=z[i])!=0; i++){ |
| 194 | if( c==delim ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 195 | if( z[i+1]==delim ){ |
| 196 | i++; |
| 197 | }else{ |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | } |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 202 | if( c ) i++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 203 | *tokenType = TK_STRING; |
| 204 | return i; |
| 205 | } |
| 206 | case '.': { |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 207 | *tokenType = TK_DOT; |
| 208 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 209 | } |
| 210 | case '0': case '1': case '2': case '3': case '4': |
| 211 | case '5': case '6': case '7': case '8': case '9': { |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 212 | *tokenType = TK_INTEGER; |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 213 | for(i=1; isdigit(z[i]); i++){} |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 214 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 215 | if( z[i]=='.' && isdigit(z[i+1]) ){ |
| 216 | i += 2; |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 217 | while( isdigit(z[i]) ){ i++; } |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 218 | *tokenType = TK_FLOAT; |
| 219 | } |
| 220 | if( (z[i]=='e' || z[i]=='E') && |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 221 | ( isdigit(z[i+1]) |
| 222 | || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2])) |
| 223 | ) |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 224 | ){ |
| 225 | i += 2; |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 226 | while( isdigit(z[i]) ){ i++; } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 227 | *tokenType = TK_FLOAT; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 228 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 229 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 230 | return i; |
| 231 | } |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 232 | case '[': { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 233 | for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 234 | *tokenType = TK_ID; |
| 235 | return i; |
| 236 | } |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 237 | case '?': { |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 238 | *tokenType = TK_VARIABLE; |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 239 | for(i=1; isdigit(z[i]); i++){} |
| 240 | return i; |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 241 | } |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 242 | case ':': { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 243 | for(i=1; IdChar(z[i]); i++){} |
drh | 2c6674c | 2004-08-25 04:07:01 +0000 | [diff] [blame] | 244 | *tokenType = i>1 ? TK_VARIABLE : TK_ILLEGAL; |
| 245 | return i; |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 246 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 247 | #ifndef SQLITE_OMIT_TCL_VARIABLE |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 248 | case '$': { |
drh | 9d74b4c | 2004-08-24 15:23:34 +0000 | [diff] [blame] | 249 | *tokenType = TK_VARIABLE; |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 250 | 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 | } |
drh | 9d74b4c | 2004-08-24 15:23:34 +0000 | [diff] [blame] | 259 | if( c==0 ) *tokenType = TK_ILLEGAL; |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 260 | }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++; |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 271 | }else{ |
| 272 | *tokenType = TK_ILLEGAL; |
| 273 | } |
| 274 | break; |
| 275 | }else if( c==':' && z[i+1]==':' ){ |
| 276 | i++; |
| 277 | }else{ |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 278 | break; |
| 279 | } |
| 280 | } |
drh | 9d74b4c | 2004-08-24 15:23:34 +0000 | [diff] [blame] | 281 | if( n==0 ) *tokenType = TK_ILLEGAL; |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 282 | } |
| 283 | return i; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 284 | } |
| 285 | #endif |
| 286 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 287 | case 'x': case 'X': { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 288 | if( (c=z[1])=='\'' || c=='"' ){ |
| 289 | int delim = c; |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 290 | *tokenType = TK_BLOB; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 291 | for(i=2; (c=z[i])!=0; i++){ |
| 292 | if( c==delim ){ |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 293 | if( i%2 ) *tokenType = TK_ILLEGAL; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 294 | break; |
| 295 | } |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 296 | if( !isxdigit(c) ){ |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 297 | *tokenType = TK_ILLEGAL; |
| 298 | return i; |
| 299 | } |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 300 | } |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 301 | if( c ) i++; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 302 | return i; |
| 303 | } |
| 304 | /* Otherwise fall through to the next case */ |
| 305 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 306 | #endif |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 307 | default: { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 308 | if( !IdChar(*z) ){ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 309 | break; |
| 310 | } |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 311 | for(i=1; IdChar(z[i]); i++){} |
danielk1977 | c60e9b8 | 2005-01-31 12:42:29 +0000 | [diff] [blame^] | 312 | *tokenType = keywordCode((char*)z, i); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 313 | return i; |
| 314 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 315 | } |
| 316 | *tokenType = TK_ILLEGAL; |
| 317 | return 1; |
| 318 | } |
danielk1977 | c60e9b8 | 2005-01-31 12:42:29 +0000 | [diff] [blame^] | 319 | int sqlite3GetToken(const unsigned char *z, int *tokenType){ |
| 320 | return getToken(z, tokenType); |
| 321 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 322 | |
| 323 | /* |
| 324 | ** Run the parser on the given SQL string. The parser structure is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 325 | ** 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 329 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 330 | int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 331 | int nErr = 0; |
| 332 | int i; |
| 333 | void *pEngine; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 334 | int tokenType; |
| 335 | int lastTokenParsed = -1; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 336 | sqlite3 *db = pParse->db; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 337 | extern void *sqlite3ParserAlloc(void*(*)(int)); |
| 338 | extern void sqlite3ParserFree(void*, void(*)(void*)); |
| 339 | extern int sqlite3Parser(void*, int, Token, Parse*); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 340 | |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 341 | db->flags &= ~SQLITE_Interrupt; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 342 | pParse->rc = SQLITE_OK; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 343 | i = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 344 | pEngine = sqlite3ParserAlloc((void*(*)(int))malloc); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 345 | if( pEngine==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 346 | sqlite3SetString(pzErrMsg, "out of memory", (char*)0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 347 | return 1; |
| 348 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 349 | 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 ); |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 356 | pParse->zTail = pParse->zSql = zSql; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 357 | while( sqlite3_malloc_failed==0 && zSql[i]!=0 ){ |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 358 | assert( i>=0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 359 | pParse->sLastToken.z = &zSql[i]; |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 360 | assert( pParse->sLastToken.dyn==0 ); |
danielk1977 | c60e9b8 | 2005-01-31 12:42:29 +0000 | [diff] [blame^] | 361 | pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 362 | i += pParse->sLastToken.n; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 363 | switch( tokenType ){ |
| 364 | case TK_SPACE: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 365 | case TK_COMMENT: { |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 366 | if( (db->flags & SQLITE_Interrupt)!=0 ){ |
| 367 | pParse->rc = SQLITE_INTERRUPT; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 368 | sqlite3SetString(pzErrMsg, "interrupt", (char*)0); |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 369 | goto abort_parse; |
| 370 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 371 | break; |
| 372 | } |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 373 | case TK_ILLEGAL: { |
drh | ae29ffb | 2004-09-25 14:39:18 +0000 | [diff] [blame] | 374 | if( pzErrMsg ){ |
| 375 | sqliteFree(*pzErrMsg); |
| 376 | *pzErrMsg = sqlite3MPrintf("unrecognized token: \"%T\"", |
| 377 | &pParse->sLastToken); |
| 378 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 379 | nErr++; |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 380 | goto abort_parse; |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 381 | } |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 382 | case TK_SEMI: { |
| 383 | pParse->zTail = &zSql[i]; |
| 384 | /* Fall thru into the default case */ |
| 385 | } |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 386 | default: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 387 | sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 388 | lastTokenParsed = tokenType; |
| 389 | if( pParse->rc!=SQLITE_OK ){ |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 390 | goto abort_parse; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 391 | } |
| 392 | break; |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 393 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 394 | } |
| 395 | } |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 396 | abort_parse: |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 397 | if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){ |
| 398 | if( lastTokenParsed!=TK_SEMI ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 399 | sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse); |
drh | 326dce7 | 2003-01-29 14:06:07 +0000 | [diff] [blame] | 400 | pParse->zTail = &zSql[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 401 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 402 | sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 403 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 404 | sqlite3ParserFree(pEngine, free); |
drh | 71c697e | 2004-08-08 23:39:19 +0000 | [diff] [blame] | 405 | if( sqlite3_malloc_failed ){ |
| 406 | pParse->rc = SQLITE_NOMEM; |
| 407 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 408 | if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 409 | sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc), |
drh | 4174398 | 2003-12-06 21:43:55 +0000 | [diff] [blame] | 410 | (char*)0); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 411 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 412 | if( pParse->zErrMsg ){ |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 413 | if( pzErrMsg && *pzErrMsg==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 414 | *pzErrMsg = pParse->zErrMsg; |
| 415 | }else{ |
| 416 | sqliteFree(pParse->zErrMsg); |
| 417 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 418 | pParse->zErrMsg = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 419 | if( !nErr ) nErr++; |
| 420 | } |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 421 | if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 422 | sqlite3VdbeDelete(pParse->pVdbe); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 423 | pParse->pVdbe = 0; |
| 424 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 425 | sqlite3DeleteTable(pParse->db, pParse->pNewTable); |
| 426 | sqlite3DeleteTrigger(pParse->pNewTrigger); |
| 427 | sqliteFree(pParse->apVarExpr); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 428 | if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 429 | pParse->rc = SQLITE_ERROR; |
| 430 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 431 | return nErr; |
| 432 | } |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 433 | |
| 434 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 435 | ** Token types used by the sqlite3_complete() routine. See the header |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 436 | ** comments on that procedure for additional information. |
| 437 | */ |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 438 | #define tkSEMI 0 |
| 439 | #define tkWS 1 |
| 440 | #define tkOTHER 2 |
| 441 | #define tkEXPLAIN 3 |
| 442 | #define tkCREATE 4 |
| 443 | #define tkTEMP 5 |
| 444 | #define tkTRIGGER 6 |
| 445 | #define tkEND 7 |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 446 | |
| 447 | /* |
| 448 | ** Return TRUE if the given SQL string ends in a semicolon. |
| 449 | ** |
| 450 | ** Special handling is require for CREATE TRIGGER statements. |
| 451 | ** Whenever the CREATE TRIGGER keywords are seen, the statement |
| 452 | ** must end with ";END;". |
| 453 | ** |
| 454 | ** This implementation uses a state machine with 7 states: |
| 455 | ** |
| 456 | ** (0) START At the beginning or end of an SQL statement. This routine |
| 457 | ** returns 1 if it ends in the START state and 0 if it ends |
| 458 | ** in any other state. |
| 459 | ** |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 460 | ** (1) NORMAL We are in the middle of statement which ends with a single |
| 461 | ** semicolon. |
| 462 | ** |
| 463 | ** (2) EXPLAIN The keyword EXPLAIN has been seen at the beginning of |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 464 | ** a statement. |
| 465 | ** |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 466 | ** (3) CREATE The keyword CREATE has been seen at the beginning of a |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 467 | ** statement, possibly preceeded by EXPLAIN and/or followed by |
| 468 | ** TEMP or TEMPORARY |
| 469 | ** |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 470 | ** (4) TRIGGER We are in the middle of a trigger definition that must be |
| 471 | ** ended by a semicolon, the keyword END, and another semicolon. |
| 472 | ** |
| 473 | ** (5) SEMI We've seen the first semicolon in the ";END;" that occurs at |
| 474 | ** the end of a trigger definition. |
| 475 | ** |
| 476 | ** (6) END We've seen the ";END" of the ";END;" that occurs at the end |
| 477 | ** of a trigger difinition. |
| 478 | ** |
| 479 | ** Transitions between states above are determined by tokens extracted |
| 480 | ** from the input. The following tokens are significant: |
| 481 | ** |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 482 | ** (0) tkSEMI A semicolon. |
| 483 | ** (1) tkWS Whitespace |
| 484 | ** (2) tkOTHER Any other SQL token. |
| 485 | ** (3) tkEXPLAIN The "explain" keyword. |
| 486 | ** (4) tkCREATE The "create" keyword. |
| 487 | ** (5) tkTEMP The "temp" or "temporary" keyword. |
| 488 | ** (6) tkTRIGGER The "trigger" keyword. |
| 489 | ** (7) tkEND The "end" keyword. |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 490 | ** |
| 491 | ** Whitespace never causes a state transition and is always ignored. |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 492 | ** |
| 493 | ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed |
| 494 | ** to recognize the end of a trigger can be omitted. All we have to do |
| 495 | ** is look for a semicolon that is not part of an string or comment. |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 496 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 497 | int sqlite3_complete(const char *zSql){ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 498 | u8 state = 0; /* Current state, using numbers defined in header comment */ |
| 499 | u8 token; /* Value of the next token */ |
| 500 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 501 | #ifndef SQLITE_OMIT_TRIGGER |
| 502 | /* A complex statement machine used to detect the end of a CREATE TRIGGER |
| 503 | ** statement. This is the normal case. |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 504 | */ |
| 505 | static const u8 trans[7][8] = { |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 506 | /* Token: */ |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 507 | /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */ |
| 508 | /* 0 START: */ { 0, 0, 1, 2, 3, 1, 1, 1, }, |
| 509 | /* 1 NORMAL: */ { 0, 1, 1, 1, 1, 1, 1, 1, }, |
| 510 | /* 2 EXPLAIN: */ { 0, 2, 1, 1, 3, 1, 1, 1, }, |
| 511 | /* 3 CREATE: */ { 0, 3, 1, 1, 1, 3, 4, 1, }, |
| 512 | /* 4 TRIGGER: */ { 5, 4, 4, 4, 4, 4, 4, 4, }, |
| 513 | /* 5 SEMI: */ { 5, 5, 4, 4, 4, 4, 4, 6, }, |
| 514 | /* 6 END: */ { 0, 6, 4, 4, 4, 4, 4, 4, }, |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 515 | }; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 516 | #else |
| 517 | /* If triggers are not suppored by this compile then the statement machine |
| 518 | ** used to detect the end of a statement is much simplier |
| 519 | */ |
| 520 | static const u8 trans[2][3] = { |
| 521 | /* Token: */ |
| 522 | /* State: ** SEMI WS OTHER */ |
| 523 | /* 0 START: */ { 0, 0, 1, }, |
| 524 | /* 1 NORMAL: */ { 0, 1, 1, }, |
| 525 | }; |
| 526 | #endif /* SQLITE_OMIT_TRIGGER */ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 527 | |
| 528 | while( *zSql ){ |
| 529 | switch( *zSql ){ |
| 530 | case ';': { /* A semicolon */ |
| 531 | token = tkSEMI; |
| 532 | break; |
| 533 | } |
| 534 | case ' ': |
| 535 | case '\r': |
| 536 | case '\t': |
| 537 | case '\n': |
| 538 | case '\f': { /* White space is ignored */ |
| 539 | token = tkWS; |
| 540 | break; |
| 541 | } |
| 542 | case '/': { /* C-style comments */ |
| 543 | if( zSql[1]!='*' ){ |
| 544 | token = tkOTHER; |
| 545 | break; |
| 546 | } |
| 547 | zSql += 2; |
| 548 | while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; } |
| 549 | if( zSql[0]==0 ) return 0; |
| 550 | zSql++; |
| 551 | token = tkWS; |
| 552 | break; |
| 553 | } |
| 554 | case '-': { /* SQL-style comments from "--" to end of line */ |
| 555 | if( zSql[1]!='-' ){ |
| 556 | token = tkOTHER; |
| 557 | break; |
| 558 | } |
| 559 | while( *zSql && *zSql!='\n' ){ zSql++; } |
| 560 | if( *zSql==0 ) return state==0; |
| 561 | token = tkWS; |
| 562 | break; |
| 563 | } |
| 564 | case '[': { /* Microsoft-style identifiers in [...] */ |
| 565 | zSql++; |
| 566 | while( *zSql && *zSql!=']' ){ zSql++; } |
| 567 | if( *zSql==0 ) return 0; |
| 568 | token = tkOTHER; |
| 569 | break; |
| 570 | } |
| 571 | case '"': /* single- and double-quoted strings */ |
| 572 | case '\'': { |
| 573 | int c = *zSql; |
| 574 | zSql++; |
| 575 | while( *zSql && *zSql!=c ){ zSql++; } |
| 576 | if( *zSql==0 ) return 0; |
| 577 | token = tkOTHER; |
| 578 | break; |
| 579 | } |
| 580 | default: { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 581 | int c; |
| 582 | if( IdChar((u8)*zSql) ){ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 583 | /* Keywords and unquoted identifiers */ |
| 584 | int nId; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 585 | for(nId=1; IdChar(zSql[nId]); nId++){} |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 586 | #ifdef SQLITE_OMIT_TRIGGER |
| 587 | token = tkOTHER; |
| 588 | #else |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 589 | switch( *zSql ){ |
| 590 | case 'c': case 'C': { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 591 | if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 592 | token = tkCREATE; |
| 593 | }else{ |
| 594 | token = tkOTHER; |
| 595 | } |
| 596 | break; |
| 597 | } |
| 598 | case 't': case 'T': { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 599 | if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 600 | token = tkTRIGGER; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 601 | }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 602 | token = tkTEMP; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 603 | }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 604 | token = tkTEMP; |
| 605 | }else{ |
| 606 | token = tkOTHER; |
| 607 | } |
| 608 | break; |
| 609 | } |
| 610 | case 'e': case 'E': { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 611 | if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 612 | token = tkEND; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 613 | }else |
| 614 | #ifndef SQLITE_OMIT_EXPLAIN |
| 615 | if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 616 | token = tkEXPLAIN; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 617 | }else |
| 618 | #endif |
| 619 | { |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 620 | token = tkOTHER; |
| 621 | } |
| 622 | break; |
| 623 | } |
| 624 | default: { |
| 625 | token = tkOTHER; |
| 626 | break; |
| 627 | } |
| 628 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 629 | #endif /* SQLITE_OMIT_TRIGGER */ |
drh | 7ad4334 | 2003-05-04 17:58:25 +0000 | [diff] [blame] | 630 | zSql += nId-1; |
| 631 | }else{ |
| 632 | /* Operators and special symbols */ |
| 633 | token = tkOTHER; |
| 634 | } |
| 635 | break; |
| 636 | } |
| 637 | } |
| 638 | state = trans[state][token]; |
| 639 | zSql++; |
| 640 | } |
| 641 | return state==0; |
| 642 | } |
danielk1977 | 61de0d1 | 2004-05-27 23:56:16 +0000 | [diff] [blame] | 643 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 644 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 61de0d1 | 2004-05-27 23:56:16 +0000 | [diff] [blame] | 645 | /* |
| 646 | ** This routine is the same as the sqlite3_complete() routine described |
| 647 | ** above, except that the parameter is required to be UTF-16 encoded, not |
| 648 | ** UTF-8. |
| 649 | */ |
| 650 | int sqlite3_complete16(const void *zSql){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 651 | sqlite3_value *pVal; |
danielk1977 | 5314c4d | 2004-06-18 06:02:35 +0000 | [diff] [blame] | 652 | char const *zSql8; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 653 | int rc = 0; |
| 654 | |
| 655 | pVal = sqlite3ValueNew(); |
| 656 | sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 657 | zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8); |
| 658 | if( zSql8 ){ |
| 659 | rc = sqlite3_complete(zSql8); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 660 | } |
| 661 | sqlite3ValueFree(pVal); |
danielk1977 | 61de0d1 | 2004-05-27 23:56:16 +0000 | [diff] [blame] | 662 | return rc; |
| 663 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 664 | #endif /* SQLITE_OMIT_UTF16 */ |