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.7 2008/06/13 18:24:27 drh 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 #ifndef SQLITE_AMALGAMATION
00028 #ifdef SQLITE_ASCII
00029 extern const char sqlite3IsAsciiIdChar[];
00030 #define IdChar(C)  (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsAsciiIdChar[c-0x20]))
00031 #endif
00032 #ifdef SQLITE_EBCDIC
00033 extern const char sqlite3IsEbcdicIdChar[];
00034 #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
00035 #endif
00036 #endif /* SQLITE_AMALGAMATION */
00037 
00038 
00039 /*
00040 ** Token types used by the sqlite3_complete() routine.  See the header
00041 ** comments on that procedure for additional information.
00042 */
00043 #define tkSEMI    0
00044 #define tkWS      1
00045 #define tkOTHER   2
00046 #define tkEXPLAIN 3
00047 #define tkCREATE  4
00048 #define tkTEMP    5
00049 #define tkTRIGGER 6
00050 #define tkEND     7
00051 
00052 /*
00053 ** Return TRUE if the given SQL string ends in a semicolon.
00054 **
00055 ** Special handling is require for CREATE TRIGGER statements.
00056 ** Whenever the CREATE TRIGGER keywords are seen, the statement
00057 ** must end with ";END;".
00058 **
00059 ** This implementation uses a state machine with 7 states:
00060 **
00061 **   (0) START     At the beginning or end of an SQL statement.  This routine
00062 **                 returns 1 if it ends in the START state and 0 if it ends
00063 **                 in any other state.
00064 **
00065 **   (1) NORMAL    We are in the middle of statement which ends with a single
00066 **                 semicolon.
00067 **
00068 **   (2) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of 
00069 **                 a statement.
00070 **
00071 **   (3) CREATE    The keyword CREATE has been seen at the beginning of a
00072 **                 statement, possibly preceeded by EXPLAIN and/or followed by
00073 **                 TEMP or TEMPORARY
00074 **
00075 **   (4) TRIGGER   We are in the middle of a trigger definition that must be
00076 **                 ended by a semicolon, the keyword END, and another semicolon.
00077 **
00078 **   (5) SEMI      We've seen the first semicolon in the ";END;" that occurs at
00079 **                 the end of a trigger definition.
00080 **
00081 **   (6) END       We've seen the ";END" of the ";END;" that occurs at the end
00082 **                 of a trigger difinition.
00083 **
00084 ** Transitions between states above are determined by tokens extracted
00085 ** from the input.  The following tokens are significant:
00086 **
00087 **   (0) tkSEMI      A semicolon.
00088 **   (1) tkWS        Whitespace
00089 **   (2) tkOTHER     Any other SQL token.
00090 **   (3) tkEXPLAIN   The "explain" keyword.
00091 **   (4) tkCREATE    The "create" keyword.
00092 **   (5) tkTEMP      The "temp" or "temporary" keyword.
00093 **   (6) tkTRIGGER   The "trigger" keyword.
00094 **   (7) tkEND       The "end" keyword.
00095 **
00096 ** Whitespace never causes a state transition and is always ignored.
00097 **
00098 ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
00099 ** to recognize the end of a trigger can be omitted.  All we have to do
00100 ** is look for a semicolon that is not part of an string or comment.
00101 */
00102 int sqlite3_complete(const char *zSql){
00103   u8 state = 0;   /* Current state, using numbers defined in header comment */
00104   u8 token;       /* Value of the next token */
00105 
00106 #ifndef SQLITE_OMIT_TRIGGER
00107   /* A complex statement machine used to detect the end of a CREATE TRIGGER
00108   ** statement.  This is the normal case.
00109   */
00110   static const u8 trans[7][8] = {
00111                      /* Token:                                                */
00112      /* State:       **  SEMI  WS  OTHER EXPLAIN  CREATE  TEMP  TRIGGER  END  */
00113      /* 0   START: */ {    0,  0,     1,      2,      3,    1,       1,   1,  },
00114      /* 1  NORMAL: */ {    0,  1,     1,      1,      1,    1,       1,   1,  },
00115      /* 2 EXPLAIN: */ {    0,  2,     1,      1,      3,    1,       1,   1,  },
00116      /* 3  CREATE: */ {    0,  3,     1,      1,      1,    3,       4,   1,  },
00117      /* 4 TRIGGER: */ {    5,  4,     4,      4,      4,    4,       4,   4,  },
00118      /* 5    SEMI: */ {    5,  5,     4,      4,      4,    4,       4,   6,  },
00119      /* 6     END: */ {    0,  6,     4,      4,      4,    4,       4,   4,  },
00120   };
00121 #else
00122   /* If triggers are not suppored by this compile then the statement machine
00123   ** used to detect the end of a statement is much simplier
00124   */
00125   static const u8 trans[2][3] = {
00126                      /* Token:           */
00127      /* State:       **  SEMI  WS  OTHER */
00128      /* 0   START: */ {    0,  0,     1, },
00129      /* 1  NORMAL: */ {    0,  1,     1, },
00130   };
00131 #endif /* SQLITE_OMIT_TRIGGER */
00132 
00133   while( *zSql ){
00134     switch( *zSql ){
00135       case ';': {  /* A semicolon */
00136         token = tkSEMI;
00137         break;
00138       }
00139       case ' ':
00140       case '\r':
00141       case '\t':
00142       case '\n':
00143       case '\f': {  /* White space is ignored */
00144         token = tkWS;
00145         break;
00146       }
00147       case '/': {   /* C-style comments */
00148         if( zSql[1]!='*' ){
00149           token = tkOTHER;
00150           break;
00151         }
00152         zSql += 2;
00153         while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
00154         if( zSql[0]==0 ) return 0;
00155         zSql++;
00156         token = tkWS;
00157         break;
00158       }
00159       case '-': {   /* SQL-style comments from "--" to end of line */
00160         if( zSql[1]!='-' ){
00161           token = tkOTHER;
00162           break;
00163         }
00164         while( *zSql && *zSql!='\n' ){ zSql++; }
00165         if( *zSql==0 ) return state==0;
00166         token = tkWS;
00167         break;
00168       }
00169       case '[': {   /* Microsoft-style identifiers in [...] */
00170         zSql++;
00171         while( *zSql && *zSql!=']' ){ zSql++; }
00172         if( *zSql==0 ) return 0;
00173         token = tkOTHER;
00174         break;
00175       }
00176       case '`':     /* Grave-accent quoted symbols used by MySQL */
00177       case '"':     /* single- and double-quoted strings */
00178       case '\'': {
00179         int c = *zSql;
00180         zSql++;
00181         while( *zSql && *zSql!=c ){ zSql++; }
00182         if( *zSql==0 ) return 0;
00183         token = tkOTHER;
00184         break;
00185       }
00186       default: {
00187         int c;
00188         if( IdChar((u8)*zSql) ){
00189           /* Keywords and unquoted identifiers */
00190           int nId;
00191           for(nId=1; IdChar(zSql[nId]); nId++){}
00192 #ifdef SQLITE_OMIT_TRIGGER
00193           token = tkOTHER;
00194 #else
00195           switch( *zSql ){
00196             case 'c': case 'C': {
00197               if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
00198                 token = tkCREATE;
00199               }else{
00200                 token = tkOTHER;
00201               }
00202               break;
00203             }
00204             case 't': case 'T': {
00205               if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
00206                 token = tkTRIGGER;
00207               }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
00208                 token = tkTEMP;
00209               }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
00210                 token = tkTEMP;
00211               }else{
00212                 token = tkOTHER;
00213               }
00214               break;
00215             }
00216             case 'e':  case 'E': {
00217               if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
00218                 token = tkEND;
00219               }else
00220 #ifndef SQLITE_OMIT_EXPLAIN
00221               if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
00222                 token = tkEXPLAIN;
00223               }else
00224 #endif
00225               {
00226                 token = tkOTHER;
00227               }
00228               break;
00229             }
00230             default: {
00231               token = tkOTHER;
00232               break;
00233             }
00234           }
00235 #endif /* SQLITE_OMIT_TRIGGER */
00236           zSql += nId-1;
00237         }else{
00238           /* Operators and special symbols */
00239           token = tkOTHER;
00240         }
00241         break;
00242       }
00243     }
00244     state = trans[state][token];
00245     zSql++;
00246   }
00247   return state==0;
00248 }
00249 
00250 #ifndef SQLITE_OMIT_UTF16
00251 /*
00252 ** This routine is the same as the sqlite3_complete() routine described
00253 ** above, except that the parameter is required to be UTF-16 encoded, not
00254 ** UTF-8.
00255 */
00256 int sqlite3_complete16(const void *zSql){
00257   sqlite3_value *pVal;
00258   char const *zSql8;
00259   int rc = SQLITE_NOMEM;
00260 
00261 #ifndef SQLITE_OMIT_AUTOINIT
00262   rc = sqlite3_initialize();
00263   if( rc ) return rc;
00264 #endif
00265   pVal = sqlite3ValueNew(0);
00266   sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
00267   zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
00268   if( zSql8 ){
00269     rc = sqlite3_complete(zSql8);
00270   }else{
00271     rc = SQLITE_NOMEM;
00272   }
00273   sqlite3ValueFree(pVal);
00274   return sqlite3ApiExit(0, rc);
00275 }
00276 #endif /* SQLITE_OMIT_UTF16 */
00277 #endif /* SQLITE_OMIT_COMPLETE */

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:52 2011 by Doxygen 1.6.1