1 /* 2 ** 2001 September 15 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 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. 10 ** 11 ************************************************************************* 12 ** An tokenizer for SQL 13 ** 14 ** This file contains C code that implements the sqlite3_complete() API. 15 ** This code used to be part of the tokenizer.c source file. But by 16 ** separating it out, the code will be automatically omitted from 17 ** static links that do not use it. 18 */ 19 #include "sqliteInt.h" 20 #ifndef SQLITE_OMIT_COMPLETE 21 22 /* 23 ** This is defined in tokenize.c. We just have to import the definition. 24 */ 25 #ifndef SQLITE_AMALGAMATION 26 #ifdef SQLITE_ASCII 27 #define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) 28 #endif 29 #ifdef SQLITE_EBCDIC 30 extern const char sqlite3IsEbcdicIdChar[]; 31 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) 32 #endif 33 #endif /* SQLITE_AMALGAMATION */ 34 35 36 /* 37 ** Token types used by the sqlite3_complete() routine. See the header 38 ** comments on that procedure for additional information. 39 */ 40 #define tkSEMI 0 41 #define tkWS 1 42 #define tkOTHER 2 43 #define tkEXPLAIN 3 44 #define tkCREATE 4 45 #define tkTEMP 5 46 #define tkTRIGGER 6 47 #define tkEND 7 48 49 /* 50 ** Return TRUE if the given SQL string ends in a semicolon. 51 ** 52 ** Special handling is require for CREATE TRIGGER statements. 53 ** Whenever the CREATE TRIGGER keywords are seen, the statement 54 ** must end with ";END;". 55 ** 56 ** This implementation uses a state machine with 7 states: 57 ** 58 ** (0) START At the beginning or end of an SQL statement. This routine 59 ** returns 1 if it ends in the START state and 0 if it ends 60 ** in any other state. 61 ** 62 ** (1) NORMAL We are in the middle of statement which ends with a single 63 ** semicolon. 64 ** 65 ** (2) EXPLAIN The keyword EXPLAIN has been seen at the beginning of 66 ** a statement. 67 ** 68 ** (3) CREATE The keyword CREATE has been seen at the beginning of a 69 ** statement, possibly preceeded by EXPLAIN and/or followed by 70 ** TEMP or TEMPORARY 71 ** 72 ** (4) TRIGGER We are in the middle of a trigger definition that must be 73 ** ended by a semicolon, the keyword END, and another semicolon. 74 ** 75 ** (5) SEMI We've seen the first semicolon in the ";END;" that occurs at 76 ** the end of a trigger definition. 77 ** 78 ** (6) END We've seen the ";END" of the ";END;" that occurs at the end 79 ** of a trigger difinition. 80 ** 81 ** Transitions between states above are determined by tokens extracted 82 ** from the input. The following tokens are significant: 83 ** 84 ** (0) tkSEMI A semicolon. 85 ** (1) tkWS Whitespace 86 ** (2) tkOTHER Any other SQL token. 87 ** (3) tkEXPLAIN The "explain" keyword. 88 ** (4) tkCREATE The "create" keyword. 89 ** (5) tkTEMP The "temp" or "temporary" keyword. 90 ** (6) tkTRIGGER The "trigger" keyword. 91 ** (7) tkEND The "end" keyword. 92 ** 93 ** Whitespace never causes a state transition and is always ignored. 94 ** 95 ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed 96 ** to recognize the end of a trigger can be omitted. All we have to do 97 ** is look for a semicolon that is not part of an string or comment. 98 */ 99 int sqlite3_complete(const char *zSql){ 100 u8 state = 0; /* Current state, using numbers defined in header comment */ 101 u8 token; /* Value of the next token */ 102 103 #ifndef SQLITE_OMIT_TRIGGER 104 /* A complex statement machine used to detect the end of a CREATE TRIGGER 105 ** statement. This is the normal case. 106 */ 107 static const u8 trans[7][8] = { 108 /* Token: */ 109 /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */ 110 /* 0 START: */ { 0, 0, 1, 2, 3, 1, 1, 1, }, 111 /* 1 NORMAL: */ { 0, 1, 1, 1, 1, 1, 1, 1, }, 112 /* 2 EXPLAIN: */ { 0, 2, 2, 1, 3, 1, 1, 1, }, 113 /* 3 CREATE: */ { 0, 3, 1, 1, 1, 3, 4, 1, }, 114 /* 4 TRIGGER: */ { 5, 4, 4, 4, 4, 4, 4, 4, }, 115 /* 5 SEMI: */ { 5, 5, 4, 4, 4, 4, 4, 6, }, 116 /* 6 END: */ { 0, 6, 4, 4, 4, 4, 4, 4, }, 117 }; 118 #else 119 /* If triggers are not suppored by this compile then the statement machine 120 ** used to detect the end of a statement is much simplier 121 */ 122 static const u8 trans[2][3] = { 123 /* Token: */ 124 /* State: ** SEMI WS OTHER */ 125 /* 0 START: */ { 0, 0, 1, }, 126 /* 1 NORMAL: */ { 0, 1, 1, }, 127 }; 128 #endif /* SQLITE_OMIT_TRIGGER */ 129 130 while( *zSql ){ 131 switch( *zSql ){ 132 case ';': { /* A semicolon */ 133 token = tkSEMI; 134 break; 135 } 136 case ' ': 137 case '\r': 138 case '\t': 139 case '\n': 140 case '\f': { /* White space is ignored */ 141 token = tkWS; 142 break; 143 } 144 case '/': { /* C-style comments */ 145 if( zSql[1]!='*' ){ 146 token = tkOTHER; 147 break; 148 } 149 zSql += 2; 150 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; } 151 if( zSql[0]==0 ) return 0; 152 zSql++; 153 token = tkWS; 154 break; 155 } 156 case '-': { /* SQL-style comments from "--" to end of line */ 157 if( zSql[1]!='-' ){ 158 token = tkOTHER; 159 break; 160 } 161 while( *zSql && *zSql!='\n' ){ zSql++; } 162 if( *zSql==0 ) return state==0; 163 token = tkWS; 164 break; 165 } 166 case '[': { /* Microsoft-style identifiers in [...] */ 167 zSql++; 168 while( *zSql && *zSql!=']' ){ zSql++; } 169 if( *zSql==0 ) return 0; 170 token = tkOTHER; 171 break; 172 } 173 case '`': /* Grave-accent quoted symbols used by MySQL */ 174 case '"': /* single- and double-quoted strings */ 175 case '\'': { 176 int c = *zSql; 177 zSql++; 178 while( *zSql && *zSql!=c ){ zSql++; } 179 if( *zSql==0 ) return 0; 180 token = tkOTHER; 181 break; 182 } 183 default: { 184 #ifdef SQLITE_EBCDIC 185 unsigned char c; 186 #endif 187 if( IdChar((u8)*zSql) ){ 188 /* Keywords and unquoted identifiers */ 189 int nId; 190 for(nId=1; IdChar(zSql[nId]); nId++){} 191 #ifdef SQLITE_OMIT_TRIGGER 192 token = tkOTHER; 193 #else 194 switch( *zSql ){ 195 case 'c': case 'C': { 196 if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){ 197 token = tkCREATE; 198 }else{ 199 token = tkOTHER; 200 } 201 break; 202 } 203 case 't': case 'T': { 204 if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){ 205 token = tkTRIGGER; 206 }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){ 207 token = tkTEMP; 208 }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){ 209 token = tkTEMP; 210 }else{ 211 token = tkOTHER; 212 } 213 break; 214 } 215 case 'e': case 'E': { 216 if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){ 217 token = tkEND; 218 }else 219 #ifndef SQLITE_OMIT_EXPLAIN 220 if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){ 221 token = tkEXPLAIN; 222 }else 223 #endif 224 { 225 token = tkOTHER; 226 } 227 break; 228 } 229 default: { 230 token = tkOTHER; 231 break; 232 } 233 } 234 #endif /* SQLITE_OMIT_TRIGGER */ 235 zSql += nId-1; 236 }else{ 237 /* Operators and special symbols */ 238 token = tkOTHER; 239 } 240 break; 241 } 242 } 243 state = trans[state][token]; 244 zSql++; 245 } 246 return state==0; 247 } 248 249 #ifndef SQLITE_OMIT_UTF16 250 /* 251 ** This routine is the same as the sqlite3_complete() routine described 252 ** above, except that the parameter is required to be UTF-16 encoded, not 253 ** UTF-8. 254 */ 255 int sqlite3_complete16(const void *zSql){ 256 sqlite3_value *pVal; 257 char const *zSql8; 258 int rc = SQLITE_NOMEM; 259 260 #ifndef SQLITE_OMIT_AUTOINIT 261 rc = sqlite3_initialize(); 262 if( rc ) return rc; 263 #endif 264 pVal = sqlite3ValueNew(0); 265 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC); 266 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8); 267 if( zSql8 ){ 268 rc = sqlite3_complete(zSql8); 269 }else{ 270 rc = SQLITE_NOMEM; 271 } 272 sqlite3ValueFree(pVal); 273 return sqlite3ApiExit(0, rc); 274 } 275 #endif /* SQLITE_OMIT_UTF16 */ 276 #endif /* SQLITE_OMIT_COMPLETE */ 277