ds@1952: /* ds@1952: ** 2001 September 15 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** An tokenizer for SQL ds@1952: ** ds@1952: ** This file contains C code that implements the sqlite3_complete() API. ds@1952: ** This code used to be part of the tokenizer.c source file. But by ds@1952: ** separating it out, the code will be automatically omitted from ds@1952: ** static links that do not use it. ds@1952: ** ds@1952: ** $Id$ ds@1952: */ ds@1952: #include "sqliteInt.h" ds@1952: #ifndef SQLITE_OMIT_COMPLETE ds@1952: ds@1952: /* ds@1952: ** This is defined in tokenize.c. We just have to import the definition. ds@1952: */ ds@1952: #ifndef SQLITE_AMALGAMATION ds@1952: #ifdef SQLITE_ASCII ds@1952: extern const char sqlite3IsAsciiIdChar[]; ds@1952: #define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsAsciiIdChar[c-0x20])) ds@1952: #endif ds@1952: #ifdef SQLITE_EBCDIC ds@1952: extern const char sqlite3IsEbcdicIdChar[]; ds@1952: #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) ds@1952: #endif ds@1952: #endif /* SQLITE_AMALGAMATION */ ds@1952: ds@1952: ds@1952: /* ds@1952: ** Token types used by the sqlite3_complete() routine. See the header ds@1952: ** comments on that procedure for additional information. ds@1952: */ ds@1952: #define tkSEMI 0 ds@1952: #define tkWS 1 ds@1952: #define tkOTHER 2 ds@1952: #define tkEXPLAIN 3 ds@1952: #define tkCREATE 4 ds@1952: #define tkTEMP 5 ds@1952: #define tkTRIGGER 6 ds@1952: #define tkEND 7 ds@1952: ds@1952: /* ds@1952: ** Return TRUE if the given SQL string ends in a semicolon. ds@1952: ** ds@1952: ** Special handling is require for CREATE TRIGGER statements. ds@1952: ** Whenever the CREATE TRIGGER keywords are seen, the statement ds@1952: ** must end with ";END;". ds@1952: ** ds@1952: ** This implementation uses a state machine with 7 states: ds@1952: ** ds@1952: ** (0) START At the beginning or end of an SQL statement. This routine ds@1952: ** returns 1 if it ends in the START state and 0 if it ends ds@1952: ** in any other state. ds@1952: ** ds@1952: ** (1) NORMAL We are in the middle of statement which ends with a single ds@1952: ** semicolon. ds@1952: ** ds@1952: ** (2) EXPLAIN The keyword EXPLAIN has been seen at the beginning of ds@1952: ** a statement. ds@1952: ** ds@1952: ** (3) CREATE The keyword CREATE has been seen at the beginning of a ds@1952: ** statement, possibly preceeded by EXPLAIN and/or followed by ds@1952: ** TEMP or TEMPORARY ds@1952: ** ds@1952: ** (4) TRIGGER We are in the middle of a trigger definition that must be ds@1952: ** ended by a semicolon, the keyword END, and another semicolon. ds@1952: ** ds@1952: ** (5) SEMI We've seen the first semicolon in the ";END;" that occurs at ds@1952: ** the end of a trigger definition. ds@1952: ** ds@1952: ** (6) END We've seen the ";END" of the ";END;" that occurs at the end ds@1952: ** of a trigger difinition. ds@1952: ** ds@1952: ** Transitions between states above are determined by tokens extracted ds@1952: ** from the input. The following tokens are significant: ds@1952: ** ds@1952: ** (0) tkSEMI A semicolon. ds@1952: ** (1) tkWS Whitespace ds@1952: ** (2) tkOTHER Any other SQL token. ds@1952: ** (3) tkEXPLAIN The "explain" keyword. ds@1952: ** (4) tkCREATE The "create" keyword. ds@1952: ** (5) tkTEMP The "temp" or "temporary" keyword. ds@1952: ** (6) tkTRIGGER The "trigger" keyword. ds@1952: ** (7) tkEND The "end" keyword. ds@1952: ** ds@1952: ** Whitespace never causes a state transition and is always ignored. ds@1952: ** ds@1952: ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed ds@1952: ** to recognize the end of a trigger can be omitted. All we have to do ds@1952: ** is look for a semicolon that is not part of an string or comment. ds@1952: */ ds@1952: int sqlite3_complete(const char *zSql){ ds@1952: u8 state = 0; /* Current state, using numbers defined in header comment */ ds@1952: u8 token; /* Value of the next token */ ds@1952: ds@1952: #ifndef SQLITE_OMIT_TRIGGER ds@1952: /* A complex statement machine used to detect the end of a CREATE TRIGGER ds@1952: ** statement. This is the normal case. ds@1952: */ ds@1952: static const u8 trans[7][8] = { ds@1952: /* Token: */ ds@1952: /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */ ds@1952: /* 0 START: */ { 0, 0, 1, 2, 3, 1, 1, 1, }, ds@1952: /* 1 NORMAL: */ { 0, 1, 1, 1, 1, 1, 1, 1, }, ds@1952: /* 2 EXPLAIN: */ { 0, 2, 1, 1, 3, 1, 1, 1, }, ds@1952: /* 3 CREATE: */ { 0, 3, 1, 1, 1, 3, 4, 1, }, ds@1952: /* 4 TRIGGER: */ { 5, 4, 4, 4, 4, 4, 4, 4, }, ds@1952: /* 5 SEMI: */ { 5, 5, 4, 4, 4, 4, 4, 6, }, ds@1952: /* 6 END: */ { 0, 6, 4, 4, 4, 4, 4, 4, }, ds@1952: }; ds@1952: #else ds@1952: /* If triggers are not suppored by this compile then the statement machine ds@1952: ** used to detect the end of a statement is much simplier ds@1952: */ ds@1952: static const u8 trans[2][3] = { ds@1952: /* Token: */ ds@1952: /* State: ** SEMI WS OTHER */ ds@1952: /* 0 START: */ { 0, 0, 1, }, ds@1952: /* 1 NORMAL: */ { 0, 1, 1, }, ds@1952: }; ds@1952: #endif /* SQLITE_OMIT_TRIGGER */ ds@1952: ds@1952: while( *zSql ){ ds@1952: switch( *zSql ){ ds@1952: case ';': { /* A semicolon */ ds@1952: token = tkSEMI; ds@1952: break; ds@1952: } ds@1952: case ' ': ds@1952: case '\r': ds@1952: case '\t': ds@1952: case '\n': ds@1952: case '\f': { /* White space is ignored */ ds@1952: token = tkWS; ds@1952: break; ds@1952: } ds@1952: case '/': { /* C-style comments */ ds@1952: if( zSql[1]!='*' ){ ds@1952: token = tkOTHER; ds@1952: break; ds@1952: } ds@1952: zSql += 2; ds@1952: while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; } ds@1952: if( zSql[0]==0 ) return 0; ds@1952: zSql++; ds@1952: token = tkWS; ds@1952: break; ds@1952: } ds@1952: case '-': { /* SQL-style comments from "--" to end of line */ ds@1952: if( zSql[1]!='-' ){ ds@1952: token = tkOTHER; ds@1952: break; ds@1952: } ds@1952: while( *zSql && *zSql!='\n' ){ zSql++; } ds@1952: if( *zSql==0 ) return state==0; ds@1952: token = tkWS; ds@1952: break; ds@1952: } ds@1952: case '[': { /* Microsoft-style identifiers in [...] */ ds@1952: zSql++; ds@1952: while( *zSql && *zSql!=']' ){ zSql++; } ds@1952: if( *zSql==0 ) return 0; ds@1952: token = tkOTHER; ds@1952: break; ds@1952: } ds@1952: case '`': /* Grave-accent quoted symbols used by MySQL */ ds@1952: case '"': /* single- and double-quoted strings */ ds@1952: case '\'': { ds@1952: int c = *zSql; ds@1952: zSql++; ds@1952: while( *zSql && *zSql!=c ){ zSql++; } ds@1952: if( *zSql==0 ) return 0; ds@1952: token = tkOTHER; ds@1952: break; ds@1952: } ds@1952: default: { ds@1952: int c; ds@1952: if( IdChar((u8)*zSql) ){ ds@1952: /* Keywords and unquoted identifiers */ ds@1952: int nId; ds@1952: for(nId=1; IdChar(zSql[nId]); nId++){} ds@1952: #ifdef SQLITE_OMIT_TRIGGER ds@1952: token = tkOTHER; ds@1952: #else ds@1952: switch( *zSql ){ ds@1952: case 'c': case 'C': { ds@1952: if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){ ds@1952: token = tkCREATE; ds@1952: }else{ ds@1952: token = tkOTHER; ds@1952: } ds@1952: break; ds@1952: } ds@1952: case 't': case 'T': { ds@1952: if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){ ds@1952: token = tkTRIGGER; ds@1952: }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){ ds@1952: token = tkTEMP; ds@1952: }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){ ds@1952: token = tkTEMP; ds@1952: }else{ ds@1952: token = tkOTHER; ds@1952: } ds@1952: break; ds@1952: } ds@1952: case 'e': case 'E': { ds@1952: if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){ ds@1952: token = tkEND; ds@1952: }else ds@1952: #ifndef SQLITE_OMIT_EXPLAIN ds@1952: if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){ ds@1952: token = tkEXPLAIN; ds@1952: }else ds@1952: #endif ds@1952: { ds@1952: token = tkOTHER; ds@1952: } ds@1952: break; ds@1952: } ds@1952: default: { ds@1952: token = tkOTHER; ds@1952: break; ds@1952: } ds@1952: } ds@1952: #endif /* SQLITE_OMIT_TRIGGER */ ds@1952: zSql += nId-1; ds@1952: }else{ ds@1952: /* Operators and special symbols */ ds@1952: token = tkOTHER; ds@1952: } ds@1952: break; ds@1952: } ds@1952: } ds@1952: state = trans[state][token]; ds@1952: zSql++; ds@1952: } ds@1952: return state==0; ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: /* ds@1952: ** This routine is the same as the sqlite3_complete() routine described ds@1952: ** above, except that the parameter is required to be UTF-16 encoded, not ds@1952: ** UTF-8. ds@1952: */ ds@1952: int sqlite3_complete16(const void *zSql){ ds@1952: sqlite3_value *pVal; ds@1952: char const *zSql8; ds@1952: int rc = SQLITE_NOMEM; ds@1952: ds@1952: pVal = sqlite3ValueNew(0); ds@1952: sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC); ds@1952: zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8); ds@1952: if( zSql8 ){ ds@1952: rc = sqlite3_complete(zSql8); ds@1952: } ds@1952: sqlite3ValueFree(pVal); ds@1952: return sqlite3ApiExit(0, rc); ds@1952: } ds@1952: #endif /* SQLITE_OMIT_UTF16 */ ds@1952: #endif /* SQLITE_OMIT_COMPLETE */