1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-30 19:03:16 +03:00

Modified statement machine in sqlite3_complete() to return 0 on empty string.

Added/updated tests for same.  Ticket 356c885b0b.

FossilOrigin-Name: 76eca7295cf7df4bef013af6c8c37251300cd383
This commit is contained in:
shaneh
2009-12-17 22:17:38 +00:00
parent 4e7b32f37f
commit c7d526e92b
4 changed files with 315 additions and 289 deletions

View File

@ -1,5 +1,5 @@
C Fixed\ssome\sTCL\stest\scases\sto\swork\sif\sSQLITE_OMIT_TRIGGER\sis\sdefined. C Modified\sstatement\smachine\sin\ssqlite3_complete()\sto\sreturn\s0\son\sempty\sstring.\s\s\nAdded/updated\stests\sfor\ssame.\s\sTicket\s356c885b0b.
D 2009-12-17T22:12:52 D 2009-12-17T22:17:39
F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
F Makefile.in c5827ead754ab32b9585487177c93bb00b9497b3 F Makefile.in c5827ead754ab32b9585487177c93bb00b9497b3
F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@ -113,7 +113,7 @@ F src/btree.h 7944a9dac59eb3e541aad45fd2747f1051e7c63d
F src/btreeInt.h 54f4245decd0409ea52cf9aee422d3d761d7ac10 F src/btreeInt.h 54f4245decd0409ea52cf9aee422d3d761d7ac10
F src/build.c 5a18b6846e99cb923008a8c888d3fd520f0bea52 F src/build.c 5a18b6846e99cb923008a8c888d3fd520f0bea52
F src/callback.c 908f3e0172c3d4058f4ca0acd42c637c52e9669f F src/callback.c 908f3e0172c3d4058f4ca0acd42c637c52e9669f
F src/complete.c 417df1ef5ea798532bb6290b0cc4265fef82980a F src/complete.c 4c8a742c4a4a6d9c835912648f5c8f032ea36c7b
F src/date.c a79c0a8f219370b972e320741f995a3bef9df33f F src/date.c a79c0a8f219370b972e320741f995a3bef9df33f
F src/delete.c 8b8afb9cd7783d573eae55a3f4208bc0637a2bb8 F src/delete.c 8b8afb9cd7783d573eae55a3f4208bc0637a2bb8
F src/expr.c 50385ed51f1cd7f1ab289629cd0f87d5b2fcca52 F src/expr.c 50385ed51f1cd7f1ab289629cd0f87d5b2fcca52
@ -468,7 +468,7 @@ F test/lock5.test 6b1f78f09ad1522843dad571b76b321e6f439bf7
F test/lock6.test 862aa71e97b288d6b3f92ba3313f51bd0b003776 F test/lock6.test 862aa71e97b288d6b3f92ba3313f51bd0b003776
F test/lock7.test 64006c84c1c616657e237c7ad6532b765611cf64 F test/lock7.test 64006c84c1c616657e237c7ad6532b765611cf64
F test/lookaside.test 1dd350dc6dff015c47c07fcc5a727a72fc5bae02 F test/lookaside.test 1dd350dc6dff015c47c07fcc5a727a72fc5bae02
F test/main.test 347ab987f16167858781383427476b33dc69fdb7 F test/main.test 2be2352ac77ac5b238c6337a5469aeeef57677e6
F test/make-where7.tcl 05c16b5d4f5d6512881dfec560cb793915932ef9 F test/make-where7.tcl 05c16b5d4f5d6512881dfec560cb793915932ef9
F test/malloc.test d23580e15c33ee0353717129421b077541e910dc F test/malloc.test d23580e15c33ee0353717129421b077541e910dc
F test/malloc3.test 4bc57f850b212f706f3e1b37c4eced1d5a727cd1 F test/malloc3.test 4bc57f850b212f706f3e1b37c4eced1d5a727cd1
@ -781,7 +781,7 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
P 29e3c8da1bd3971215036e5f5cfa5b25c6caa81f P 85e3c73497d72d09becf728efe81041277ca1730
R 821d3650108711607e08c70b0cbff634 R 441a9b5eb3dd9b55570d6d08e1a1ee71
U shaneh U shaneh
Z 39fa2349d7d22ddb3b872ed12521d5b0 Z 240b5a79be1f4714cd0b02c34f1cf679

View File

@ -1 +1 @@
85e3c73497d72d09becf728efe81041277ca1730 76eca7295cf7df4bef013af6c8c37251300cd383

View File

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

View File

@ -25,21 +25,40 @@ ifcapable {complete} {
do_test main-1.1 { do_test main-1.1 {
db complete {This is a test} db complete {This is a test}
} {0} } {0}
do_test main-1.2 { do_test main-1.2.0 {
db complete { db complete {
} }
} {1} } {0}
do_test main-1.3 { do_test main-1.2.1 {
db complete {}
} {0}
do_test main-1.3.0 {
db complete { db complete {
-- a comment ; -- a comment ;
} }
} {1} } {0}
do_test main-1.4 { do_test main-1.3.1 {
db complete {
/* a comment ; */
}
} {0}
do_test main-1.4.0 {
db complete { db complete {
-- a comment ; -- a comment ;
; ;
} }
} {1} } {1}
do_test main-1.4.1 {
db complete {
/* a comment ; */
;
}
} {1}
do_test main-1.4.2 {
db complete {
/* a comment ; */ ;
}
} {1}
do_test main-1.5 { do_test main-1.5 {
db complete {DROP TABLE 'xyz;} db complete {DROP TABLE 'xyz;}
} {0} } {0}