Back to index

lightning-sunbird  0.9+nobinonly
complete.c
Go to the documentation of this file.
00001 /*
00002 ** 2001 September 15
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 ** An tokenizer for SQL
00013 **
00014 ** This file contains C code that implements the sqlite3_complete() API.
00015 ** This code used to be part of the tokenizer.c source file.  But by
00016 ** separating it out, the code will be automatically omitted from
00017 ** static links that do not use it.
00018 **
00019 ** $Id: complete.c,v 1.1.2.4 2006/05/22 19:12:32 brettw%gmail.com Exp $
00020 */
00021 #include "sqliteInt.h"
00022 #ifndef SQLITE_OMIT_COMPLETE
00023 
00024 /*
00025 ** This is defined in tokenize.c.  We just have to import the definition.
00026 */
00027 extern const char sqlite3IsIdChar[];
00028 #define IdChar(C)  (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsIdChar[c-0x20]))
00029 
00030 
00031 /*
00032 ** Token types used by the sqlite3_complete() routine.  See the header
00033 ** comments on that procedure for additional information.
00034 */
00035 #define tkSEMI    0
00036 #define tkWS      1
00037 #define tkOTHER   2
00038 #define tkEXPLAIN 3
00039 #define tkCREATE  4
00040 #define tkTEMP    5
00041 #define tkTRIGGER 6
00042 #define tkEND     7
00043 
00044 /*
00045 ** Return TRUE if the given SQL string ends in a semicolon.
00046 **
00047 ** Special handling is require for CREATE TRIGGER statements.
00048 ** Whenever the CREATE TRIGGER keywords are seen, the statement
00049 ** must end with ";END;".
00050 **
00051 ** This implementation uses a state machine with 7 states:
00052 **
00053 **   (0) START     At the beginning or end of an SQL statement.  This routine
00054 **                 returns 1 if it ends in the START state and 0 if it ends
00055 **                 in any other state.
00056 **
00057 **   (1) NORMAL    We are in the middle of statement which ends with a single
00058 **                 semicolon.
00059 **
00060 **   (2) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of 
00061 **                 a statement.
00062 **
00063 **   (3) CREATE    The keyword CREATE has been seen at the beginning of a
00064 **                 statement, possibly preceeded by EXPLAIN and/or followed by
00065 **                 TEMP or TEMPORARY
00066 **
00067 **   (4) TRIGGER   We are in the middle of a trigger definition that must be
00068 **                 ended by a semicolon, the keyword END, and another semicolon.
00069 **
00070 **   (5) SEMI      We've seen the first semicolon in the ";END;" that occurs at
00071 **                 the end of a trigger definition.
00072 **
00073 **   (6) END       We've seen the ";END" of the ";END;" that occurs at the end
00074 **                 of a trigger difinition.
00075 **
00076 ** Transitions between states above are determined by tokens extracted
00077 ** from the input.  The following tokens are significant:
00078 **
00079 **   (0) tkSEMI      A semicolon.
00080 **   (1) tkWS        Whitespace
00081 **   (2) tkOTHER     Any other SQL token.
00082 **   (3) tkEXPLAIN   The "explain" keyword.
00083 **   (4) tkCREATE    The "create" keyword.
00084 **   (5) tkTEMP      The "temp" or "temporary" keyword.
00085 **   (6) tkTRIGGER   The "trigger" keyword.
00086 **   (7) tkEND       The "end" keyword.
00087 **
00088 ** Whitespace never causes a state transition and is always ignored.
00089 **
00090 ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
00091 ** to recognize the end of a trigger can be omitted.  All we have to do
00092 ** is look for a semicolon that is not part of an string or comment.
00093 */
00094 int sqlite3_complete(const char *zSql){
00095   u8 state = 0;   /* Current state, using numbers defined in header comment */
00096   u8 token;       /* Value of the next token */
00097 
00098 #ifndef SQLITE_OMIT_TRIGGER
00099   /* A complex statement machine used to detect the end of a CREATE TRIGGER
00100   ** statement.  This is the normal case.
00101   */
00102   static const u8 trans[7][8] = {
00103                      /* Token:                                                */
00104      /* State:       **  SEMI  WS  OTHER EXPLAIN  CREATE  TEMP  TRIGGER  END  */
00105      /* 0   START: */ {    0,  0,     1,      2,      3,    1,       1,   1,  },
00106      /* 1  NORMAL: */ {    0,  1,     1,      1,      1,    1,       1,   1,  },
00107      /* 2 EXPLAIN: */ {    0,  2,     1,      1,      3,    1,       1,   1,  },
00108      /* 3  CREATE: */ {    0,  3,     1,      1,      1,    3,       4,   1,  },
00109      /* 4 TRIGGER: */ {    5,  4,     4,      4,      4,    4,       4,   4,  },
00110      /* 5    SEMI: */ {    5,  5,     4,      4,      4,    4,       4,   6,  },
00111      /* 6     END: */ {    0,  6,     4,      4,      4,    4,       4,   4,  },
00112   };
00113 #else
00114   /* If triggers are not suppored by this compile then the statement machine
00115   ** used to detect the end of a statement is much simplier
00116   */
00117   static const u8 trans[2][3] = {
00118                      /* Token:           */
00119      /* State:       **  SEMI  WS  OTHER */
00120      /* 0   START: */ {    0,  0,     1, },
00121      /* 1  NORMAL: */ {    0,  1,     1, },
00122   };
00123 #endif /* SQLITE_OMIT_TRIGGER */
00124 
00125   while( *zSql ){
00126     switch( *zSql ){
00127       case ';': {  /* A semicolon */
00128         token = tkSEMI;
00129         break;
00130       }
00131       case ' ':
00132       case '\r':
00133       case '\t':
00134       case '\n':
00135       case '\f': {  /* White space is ignored */
00136         token = tkWS;
00137         break;
00138       }
00139       case '/': {   /* C-style comments */
00140         if( zSql[1]!='*' ){
00141           token = tkOTHER;
00142           break;
00143         }
00144         zSql += 2;
00145         while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
00146         if( zSql[0]==0 ) return 0;
00147         zSql++;
00148         token = tkWS;
00149         break;
00150       }
00151       case '-': {   /* SQL-style comments from "--" to end of line */
00152         if( zSql[1]!='-' ){
00153           token = tkOTHER;
00154           break;
00155         }
00156         while( *zSql && *zSql!='\n' ){ zSql++; }
00157         if( *zSql==0 ) return state==0;
00158         token = tkWS;
00159         break;
00160       }
00161       case '[': {   /* Microsoft-style identifiers in [...] */
00162         zSql++;
00163         while( *zSql && *zSql!=']' ){ zSql++; }
00164         if( *zSql==0 ) return 0;
00165         token = tkOTHER;
00166         break;
00167       }
00168       case '`':     /* Grave-accent quoted symbols used by MySQL */
00169       case '"':     /* single- and double-quoted strings */
00170       case '\'': {
00171         int c = *zSql;
00172         zSql++;
00173         while( *zSql && *zSql!=c ){ zSql++; }
00174         if( *zSql==0 ) return 0;
00175         token = tkOTHER;
00176         break;
00177       }
00178       default: {
00179         int c;
00180         if( IdChar((u8)*zSql) ){
00181           /* Keywords and unquoted identifiers */
00182           int nId;
00183           for(nId=1; IdChar(zSql[nId]); nId++){}
00184 #ifdef SQLITE_OMIT_TRIGGER
00185           token = tkOTHER;
00186 #else
00187           switch( *zSql ){
00188             case 'c': case 'C': {
00189               if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
00190                 token = tkCREATE;
00191               }else{
00192                 token = tkOTHER;
00193               }
00194               break;
00195             }
00196             case 't': case 'T': {
00197               if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
00198                 token = tkTRIGGER;
00199               }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
00200                 token = tkTEMP;
00201               }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
00202                 token = tkTEMP;
00203               }else{
00204                 token = tkOTHER;
00205               }
00206               break;
00207             }
00208             case 'e':  case 'E': {
00209               if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
00210                 token = tkEND;
00211               }else
00212 #ifndef SQLITE_OMIT_EXPLAIN
00213               if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
00214                 token = tkEXPLAIN;
00215               }else
00216 #endif
00217               {
00218                 token = tkOTHER;
00219               }
00220               break;
00221             }
00222             default: {
00223               token = tkOTHER;
00224               break;
00225             }
00226           }
00227 #endif /* SQLITE_OMIT_TRIGGER */
00228           zSql += nId-1;
00229         }else{
00230           /* Operators and special symbols */
00231           token = tkOTHER;
00232         }
00233         break;
00234       }
00235     }
00236     state = trans[state][token];
00237     zSql++;
00238   }
00239   return state==0;
00240 }
00241 
00242 #ifndef SQLITE_OMIT_UTF16
00243 /*
00244 ** This routine is the same as the sqlite3_complete() routine described
00245 ** above, except that the parameter is required to be UTF-16 encoded, not
00246 ** UTF-8.
00247 */
00248 int sqlite3_complete16(const void *zSql){
00249   sqlite3_value *pVal;
00250   char const *zSql8;
00251   int rc = 0;
00252 
00253   pVal = sqlite3ValueNew();
00254   sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
00255   zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
00256   if( zSql8 ){
00257     rc = sqlite3_complete(zSql8);
00258   }
00259   sqlite3ValueFree(pVal);
00260   return sqlite3ApiExit(0, rc);
00261 }
00262 #endif /* SQLITE_OMIT_UTF16 */
00263 #endif /* SQLITE_OMIT_COMPLETE */