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