interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / src / complete.c

373 lines · 12269 bytes Raw Blame History
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
30extern 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#ifndef SQLITE_OMIT_TRIGGER
44#define tkEXPLAIN 3
45#define tkCREATE 4
46#define tkTEMP 5
47#define tkTRIGGER 6
48#define tkEND 7
49#endif
50
51/*
52** Return zero if the given SQL string is complete - if all comments,
53** string and blob literals, and quoted identifiers have been closed and
54** if the entire string ends with ";" and possibly with ";END;" if the
55** string is a CREATE TRIGGER statement. A non-zero return indicates
56** that the string is empty or incomplete. Bits of the return value
57** indicate what is missing and is needed to close out the statement.
58**
59** Special handling is require for CREATE TRIGGER statements.
60** Whenever the CREATE TRIGGER keywords are seen, the statement
61** must end with ";END;".
62**
63** Let the return code be a value R. R is split up into various
64** subfields, at byte boundaries:
65**
66** R = 0xwwwwwwww00xxyyzz
67**
68** In other words, zz is the least significant byte, yy is the next
69** most significant byte, xx is the third byte, wwwwwwww is the upper
70** four bytes of the return value.
71**
72** zz == SQLITE_OK Input is complete
73** zz == SQLITE_ERROR Input is incomplete
74** zz == SQLITE_EMPTY Input contains only whitespace
75** zz == SQLITE_MISUSE Input is a NULL pointer
76** zz != 0 New values for zz may be added in the future
77**
78** yy == 0x01 Need a semicolon at the end
79** yy == 0x02 Need "END" and a semicolon
80** yy == 0x03 Need semicolon, "END", and semicolon
81** yy != 0 New values for yy may be added in the future
82**
83** xx == '\'' Incomplete string or blob literal
84** xx == '"' Incomplete quoted identifier
85** xx == '`' Incompelte MySQL-style quoted identifier
86** xx == ']' Incomplete SQLServer-style quoted identifer
87** xx == '-' Incomplete SQL-style comment
88** xx == '/' Incomplete C-style comment
89** xx != 0 New values of xx may be added in the future
90**
91** wwwwwwww Interpret as a signed integer, the number
92** of unmatched "(". Negative means there are
93** more ")" and "(".
94**
95** ((R>>24)&0xff)!=0 New uses for the 4th byte may be added
96** in the future
97**
98** This implementation uses a state machine with 8 states:
99**
100** (0) NOTHING We have not yet seen a non-whitespace character.
101**
102** (1) START At the beginning or end of an SQL statement. This routine
103** returns 1 if it ends in the START state and 0 if it ends
104** in any other state.
105**
106** (2) NORMAL We are in the middle of statement which ends with a single
107** semicolon.
108**
109** (3) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
110** a statement.
111**
112** (4) CREATE The keyword CREATE has been seen at the beginning of a
113** statement, possibly preceded by EXPLAIN and/or followed by
114** TEMP or TEMPORARY
115**
116** (5) TRIGGER We are in the middle of a trigger definition that must be
117** ended by a semicolon, the keyword END, and another semicolon.
118**
119** (6) SEMI We've seen the first semicolon in the ";END;" that occurs at
120** the end of a trigger definition.
121**
122** (7) END We've seen the ";END" of the ";END;" that occurs at the end
123** of a trigger definition.
124**
125** Transitions between states above are determined by tokens extracted
126** from the input. The following tokens are significant:
127**
128** (0) tkSEMI A semicolon.
129** (1) tkWS Whitespace.
130** (2) tkOTHER Any other SQL token.
131** (3) tkEXPLAIN The "explain" keyword.
132** (4) tkCREATE The "create" keyword.
133** (5) tkTEMP The "temp" or "temporary" keyword.
134** (6) tkTRIGGER The "trigger" keyword.
135** (7) tkEND The "end" keyword.
136**
137** Whitespace never causes a state transition and is always ignored.
138** This means that a SQL string of all whitespace is invalid.
139**
140** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
141** to recognize the end of a trigger can be omitted. All we have to do
142** is look for a semicolon that is not part of an string or comment.
143*/
144sqlite3_int64 sqlite3_incomplete(const char *zSql){
145 u8 state = 0; /* Current state, using numbers defined in header comment */
146 u8 token; /* Value of the next token */
147 u8 pending = 0; /* unmatched structure character */
148 int nParen = 0; /* Nested parentheses */
149
150#ifndef SQLITE_OMIT_TRIGGER
151 /* A complex statement machine used to detect the end of a CREATE TRIGGER
152 ** statement. This is the normal case.
153 */
154 static const u8 trans[8][8] = {
155 /* Token: */
156 /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */
157 /* 0 NOTHING: */ { 1, 0, 2, 3, 4, 2, 2, 2, },
158 /* 1 START: */ { 1, 1, 2, 3, 4, 2, 2, 2, },
159 /* 2 NORMAL: */ { 1, 2, 2, 2, 2, 2, 2, 2, },
160 /* 3 EXPLAIN: */ { 1, 3, 3, 2, 4, 2, 2, 2, },
161 /* 4 CREATE: */ { 1, 4, 2, 2, 2, 4, 5, 2, },
162 /* 5 TRIGGER: */ { 6, 5, 5, 5, 5, 5, 5, 5, },
163 /* 6 SEMI: */ { 6, 6, 5, 5, 5, 5, 5, 7, },
164 /* 7 END: */ { 1, 7, 5, 5, 5, 5, 5, 5, },
165 };
166#else
167 /* If triggers are not supported by this compile then the statement machine
168 ** used to detect the end of a statement is much simpler
169 */
170 static const u8 trans[3][3] = {
171 /* Token: */
172 /* State: ** SEMI WS OTHER */
173 /* 0 NOTHING: */ { 1, 0, 2, },
174 /* 1 START: */ { 1, 1, 2, },
175 /* 2 NORMAL: */ { 1, 2, 2, },
176 };
177#endif /* SQLITE_OMIT_TRIGGER */
178 /* Mapping state number to yy value for the return */
179 static const u8 statemap[8] = {
180 /* 0 NOTHING */ 0,
181 /* 1 START */ 0,
182 /* 2 NORMAL */ 1,
183 /* 3 EXPLAIN */ 1,
184 /* 4 CREATE */ 1,
185 /* 5 TRIGGER */ 3,
186 /* 6 SEMI */ 2,
187 /* 7 END */ 1,
188 };
189
190#ifdef SQLITE_ENABLE_API_ARMOR
191 if( zSql==0 ){
192 return SQLITE_MISUSE_BKPT;
193 }
194#endif
195
196 while( *zSql ){
197 switch( *zSql ){
198 case ';': { /* A semicolon */
199 token = tkSEMI;
200 break;
201 }
202 case ' ':
203 case '\r':
204 case '\t':
205 case '\n':
206 case '\f': { /* White space is ignored */
207 token = tkWS;
208 break;
209 }
210 case '/': { /* C-style comments */
211 if( zSql[1]!='*' ){
212 token = tkOTHER;
213 break;
214 }
215 zSql += 2;
216 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
217 if( zSql[0]==0 ){
218 pending = '/';
219 goto incomplete_finish;
220 }
221 zSql++;
222 token = tkWS;
223 break;
224 }
225 case '-': { /* SQL-style comments from "--" to end of line */
226 if( zSql[1]!='-' ){
227 token = tkOTHER;
228 break;
229 }
230 while( *zSql && *zSql!='\n' ){ zSql++; }
231 if( *zSql==0 ){
232 pending = '-';
233 goto incomplete_finish;
234 }
235 token = tkWS;
236 break;
237 }
238 case '[': { /* Microsoft-style identifiers in [...] */
239 zSql++;
240 while( *zSql && *zSql!=']' ){ zSql++; }
241 if( *zSql==0 ){
242 pending = ']';
243 goto incomplete_finish;
244 }
245 token = tkOTHER;
246 break;
247 }
248 case '`': /* Grave-accent quoted symbols used by MySQL */
249 case '"': /* single- and double-quoted strings */
250 case '\'': {
251 int c = *zSql;
252 zSql++;
253 while( *zSql && *zSql!=c ){ zSql++; }
254 if( *zSql==0 ){
255 pending = c;
256 goto incomplete_finish;
257 }
258 token = tkOTHER;
259 break;
260 }
261 case '(': {
262 nParen++;
263 token = tkOTHER;
264 break;
265 }
266 case ')': {
267 nParen--;
268 token = tkOTHER;
269 break;
270 }
271 default: {
272#ifdef SQLITE_EBCDIC
273 unsigned char c;
274#endif
275 if( IdChar((u8)*zSql) ){
276 /* Keywords and unquoted identifiers */
277 int nId;
278 for(nId=1; IdChar(zSql[nId]); nId++){}
279#ifdef SQLITE_OMIT_TRIGGER
280 token = tkOTHER;
281#else
282 switch( *zSql ){
283 case 'c': case 'C': {
284 if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
285 token = tkCREATE;
286 }else{
287 token = tkOTHER;
288 }
289 break;
290 }
291 case 't': case 'T': {
292 if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
293 token = tkTRIGGER;
294 }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
295 token = tkTEMP;
296 }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
297 token = tkTEMP;
298 }else{
299 token = tkOTHER;
300 }
301 break;
302 }
303 case 'e': case 'E': {
304 if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
305 token = tkEND;
306 }else
307#ifndef SQLITE_OMIT_EXPLAIN
308 if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
309 token = tkEXPLAIN;
310 }else
311#endif
312 {
313 token = tkOTHER;
314 }
315 break;
316 }
317 default: {
318 token = tkOTHER;
319 break;
320 }
321 }
322#endif /* SQLITE_OMIT_TRIGGER */
323 zSql += nId-1;
324 }else{
325 /* Operators and special symbols */
326 token = tkOTHER;
327 }
328 break;
329 }
330 }
331 state = trans[state][token];
332 zSql++;
333 }
334incomplete_finish:
335 if( state==0 && pending==0 ) return SQLITE_EMPTY;
336 if( state==1 ) nParen = 0;
337 return (i64)((((u64)nParen)<<32) |
338 ((u64)pending<<16) |
339 ((u64)statemap[state]<<8) |
340 (state>1 || pending!=0));
341}
342int sqlite3_complete(const char *zSql){
343 return sqlite3_incomplete(zSql)==0;
344}
345
346#ifndef SQLITE_OMIT_UTF16
347/*
348** This routine is the same as the sqlite3_complete() routine described
349** above, except that the parameter is required to be UTF-16 encoded, not
350** UTF-8.
351*/
352int sqlite3_complete16(const void *zSql){
353 sqlite3_value *pVal;
354 char const *zSql8;
355 int rc;
356
357#ifndef SQLITE_OMIT_AUTOINIT
358 rc = sqlite3_initialize();
359 if( rc ) return rc;
360#endif
361 pVal = sqlite3ValueNew(0);
362 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
363 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
364 if( zSql8 ){
365 rc = sqlite3_incomplete(zSql8)==0;
366 }else{
367 rc = SQLITE_NOMEM_BKPT;
368 }
369 sqlite3ValueFree(pVal);
370 return rc & 0xff;
371}
372#endif /* SQLITE_OMIT_UTF16 */
373#endif /* SQLITE_OMIT_COMPLETE */