Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Fix deficiencies in sqlite_complete() pointed out by R. Dennis Cote. (CVS 955) |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
54b33a5ed9f7a89435c2f1395a3177e8 |
User & Date: | drh 2003-05-04 17:58:26 |
Context
2003-05-04
| ||
18:30 | Shell command-line parsing enhancements suggested by Mike Hall. (CVS 956) check-in: 5656fe48 user: drh tags: trunk | |
17:58 | Fix deficiencies in sqlite_complete() pointed out by R. Dennis Cote. (CVS 955) check-in: 54b33a5e user: drh tags: trunk | |
07:31 | Added shell command ".databases" to list name and file of open ones. Added several missing shell commands. (CVS 954) check-in: dd57d6ae user: jplyon tags: trunk | |
Changes
Changes to src/main.c.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
|
** ************************************************************************* ** Main file for the SQLite library. The routines in this file ** implement the programmer interface to the library. Routines in ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** ** $Id: main.c,v 1.129 2003/04/29 16:20:46 drh Exp $ */ #include "sqliteInt.h" #include "os.h" #include <ctype.h> /* ** A pointer to this structure is used to communicate information ................................................................................ sqliteFree(pFunc); } } sqliteHashClear(&db->aFunc); sqliteFree(db); } /* ** Return TRUE if the given SQL string ends in a semicolon. ** ** Special handling is require for CREATE TRIGGER statements. ** Whenever the CREATE TRIGGER keywords are seen, the statement ** must end with ";END;". */ int sqlite_complete(const char *zSql){ int isComplete = 1; int requireEnd = 0; int seenText = 0; int seenCreate = 0; while( *zSql ){ switch( *zSql ){ case ';': { isComplete = 1; seenText = 1; seenCreate = 0; break; } case ' ': case '\r': case '\t': case '\n': case '\f': { break; } case '[': { isComplete = 0; seenText = 1; seenCreate = 0; zSql++; while( *zSql && *zSql!=']' ){ zSql++; } if( *zSql==0 ) return 0; break; } case '"': case '\'': { int c = *zSql; isComplete = 0; seenText = 1; seenCreate = 0; zSql++; while( *zSql && *zSql!=c ){ zSql++; } if( *zSql==0 ) return 0; break; } case '/': { if( zSql[1]!='*' ){ isComplete = 0; seenText = 1; seenCreate = 0; break; } zSql += 2; while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; } if( zSql[0]==0 ) return 0; zSql += 2; break; } case '-': { if( zSql[1]!='-' ){ isComplete = 0; seenCreate = 0; break; } while( *zSql && *zSql!='\n' ){ zSql++; } if( *zSql==0 ) return seenText && isComplete && requireEnd==0; break; } case 'c': case 'C': { seenText = 1; if( !isComplete ) break; isComplete = 0; if( sqliteStrNICmp(zSql, "create", 6)!=0 ) break; if( !isspace(zSql[6]) ) break; zSql += 5; seenCreate = 1; while( isspace(zSql[1]) ) zSql++; if( sqliteStrNICmp(&zSql[1],"trigger", 7)!=0 ) break; zSql += 7; requireEnd++; break; } case 't': case 'T': { seenText = 1; if( !seenCreate ) break; seenCreate = 0; isComplete = 0; if( sqliteStrNICmp(zSql, "trigger", 7)!=0 ) break; if( !isspace(zSql[7]) ) break; zSql += 6; requireEnd++; break; } case 'e': case 'E': { seenCreate = 0; seenText = 1; if( !isComplete ) break; isComplete = 0; if( requireEnd==0 ) break; if( sqliteStrNICmp(zSql, "end", 3)!=0 ) break; zSql += 2; while( isspace(zSql[1]) ) zSql++; if( zSql[1]==';' ){ zSql++; isComplete = 1; requireEnd--; } break; } default: { seenCreate = 0; seenText = 1; isComplete = 0; break; } } zSql++; } return /* seenText && */ isComplete && requireEnd==0; } /* ** Rollback all database files. */ void sqliteRollbackAll(sqlite *db){ int i; for(i=0; i<db->nDb; i++){ if( db->aDb[i].pBt ){ |
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
549
550
551
552
553
554
555
556
557
558
559
560
561
562
|
** ************************************************************************* ** Main file for the SQLite library. The routines in this file ** implement the programmer interface to the library. Routines in ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** ** $Id: main.c,v 1.130 2003/05/04 17:58:26 drh Exp $ */ #include "sqliteInt.h" #include "os.h" #include <ctype.h> /* ** A pointer to this structure is used to communicate information ................................................................................ sqliteFree(pFunc); } } sqliteHashClear(&db->aFunc); sqliteFree(db); } /* ** Rollback all database files. */ void sqliteRollbackAll(sqlite *db){ int i; for(i=0; i<db->nDb; i++){ if( db->aDb[i].pBt ){ |
Changes to src/tokenize.c.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
...
492
493
494
495
496
497
498
|
************************************************************************* ** An tokenizer for SQL ** ** This file contains C code that splits an SQL input string up into ** individual tokens and sends those tokens one-by-one over to the ** parser for analysis. ** ** $Id: tokenize.c,v 1.58 2003/04/21 18:48:47 drh Exp $ */ #include "sqliteInt.h" #include "os.h" #include <ctype.h> #include <stdlib.h> /* ................................................................................ pParse->pNewTrigger = 0; } if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){ pParse->rc = SQLITE_ERROR; } return nErr; } |
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
...
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
|
************************************************************************* ** An tokenizer for SQL ** ** This file contains C code that splits an SQL input string up into ** individual tokens and sends those tokens one-by-one over to the ** parser for analysis. ** ** $Id: tokenize.c,v 1.59 2003/05/04 17:58:26 drh Exp $ */ #include "sqliteInt.h" #include "os.h" #include <ctype.h> #include <stdlib.h> /* ................................................................................ pParse->pNewTrigger = 0; } if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){ pParse->rc = SQLITE_ERROR; } return nErr; } /* ** Token types used by the sqlite_complete() routine. See the header ** comments on that procedure for additional information. */ #define tkEXPLAIN 0 #define tkCREATE 1 #define tkTEMP 2 #define tkTRIGGER 3 #define tkEND 4 #define tkSEMI 5 #define tkWS 6 #define tkOTHER 7 /* ** Return TRUE if the given SQL string ends in a semicolon. ** ** Special handling is require for CREATE TRIGGER statements. ** Whenever the CREATE TRIGGER keywords are seen, the statement ** must end with ";END;". ** ** This implementation uses a state machine with 7 states: ** ** (0) START At the beginning or end of an SQL statement. This routine ** returns 1 if it ends in the START state and 0 if it ends ** in any other state. ** ** (1) EXPLAIN The keyword EXPLAIN has been seen at the beginning of ** a statement. ** ** (2) CREATE The keyword CREATE has been seen at the beginning of a ** statement, possibly preceeded by EXPLAIN and/or followed by ** TEMP or TEMPORARY ** ** (3) NORMAL We are in the middle of statement which ends with a single ** semicolon. ** ** (4) TRIGGER We are in the middle of a trigger definition that must be ** ended by a semicolon, the keyword END, and another semicolon. ** ** (5) SEMI We've seen the first semicolon in the ";END;" that occurs at ** the end of a trigger definition. ** ** (6) END We've seen the ";END" of the ";END;" that occurs at the end ** of a trigger difinition. ** ** Transitions between states above are determined by tokens extracted ** from the input. The following tokens are significant: ** ** (0) tkEXPLAIN The "explain" keyword. ** (1) tkCREATE The "create" keyword. ** (2) tkTEMP The "temp" or "temporary" keyword. ** (3) tkTRIGGER The "trigger" keyword. ** (4) tkEND The "end" keyword. ** (5) tkSEMI A semicolon. ** (6) tkWS Whitespace ** (7) tkOTHER Any other SQL token. ** ** Whitespace never causes a state transition and is always ignored. */ int sqlite_complete(const char *zSql){ u8 state = 0; /* Current state, using numbers defined in header comment */ u8 token; /* Value of the next token */ /* The following matrix defines the transition from one state to another ** according to what token is seen. trans[state][token] returns the ** next state. */ static const u8 trans[7][8] = { /* Token: /* State: ** EXPLAIN CREATE TEMP TRIGGER END SEMI WS OTHER */ /* 0 START: */ { 1, 2, 3, 3, 3, 0, 0, 3, }, /* 1 EXPLAIN: */ { 3, 2, 3, 3, 3, 0, 1, 3, }, /* 2 CREATE: */ { 3, 3, 2, 4, 3, 0, 2, 3, }, /* 3 NORMAL: */ { 3, 3, 3, 3, 3, 0, 3, 3, }, /* 4 TRIGGER: */ { 4, 4, 4, 4, 4, 5, 4, 4, }, /* 5 SEMI: */ { 4, 4, 4, 4, 6, 5, 5, 4, }, /* 6 END: */ { 4, 4, 4, 4, 4, 0, 6, 4, }, }; while( *zSql ){ switch( *zSql ){ case ';': { /* A semicolon */ token = tkSEMI; break; } case ' ': case '\r': case '\t': case '\n': case '\f': { /* White space is ignored */ token = tkWS; break; } case '/': { /* C-style comments */ if( zSql[1]!='*' ){ token = tkOTHER; break; } zSql += 2; while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; } if( zSql[0]==0 ) return 0; zSql++; token = tkWS; break; } case '-': { /* SQL-style comments from "--" to end of line */ if( zSql[1]!='-' ){ token = tkOTHER; break; } while( *zSql && *zSql!='\n' ){ zSql++; } if( *zSql==0 ) return state==0; token = tkWS; break; } case '[': { /* Microsoft-style identifiers in [...] */ zSql++; while( *zSql && *zSql!=']' ){ zSql++; } if( *zSql==0 ) return 0; token = tkOTHER; break; } case '"': /* single- and double-quoted strings */ case '\'': { int c = *zSql; zSql++; while( *zSql && *zSql!=c ){ zSql++; } if( *zSql==0 ) return 0; token = tkOTHER; break; } default: { if( isIdChar[*zSql] ){ /* Keywords and unquoted identifiers */ int nId; for(nId=1; isIdChar[zSql[nId]]; nId++){} switch( *zSql ){ case 'c': case 'C': { if( nId==6 && sqliteStrNICmp(zSql, "create", 6)==0 ){ token = tkCREATE; }else{ token = tkOTHER; } break; } case 't': case 'T': { if( nId==7 && sqliteStrNICmp(zSql, "trigger", 7)==0 ){ token = tkTRIGGER; }else if( nId==4 && sqliteStrNICmp(zSql, "temp", 4)==0 ){ token = tkTEMP; }else if( nId==9 && sqliteStrNICmp(zSql, "temporary", 9)==0 ){ token = tkTEMP; }else{ token = tkOTHER; } break; } case 'e': case 'E': { if( nId==3 && sqliteStrNICmp(zSql, "end", 3)==0 ){ token = tkEND; }else if( nId==7 && sqliteStrNICmp(zSql, "explain", 7)==0 ){ token = tkEXPLAIN; }else{ token = tkOTHER; } break; } default: { token = tkOTHER; break; } } zSql += nId-1; }else{ /* Operators and special symbols */ token = tkOTHER; } break; } } state = trans[state][token]; zSql++; } return state==0; } |
Changes to test/main.test.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ... 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 ... 163 164 165 166 167 168 169 170 171 172 173 174 175 176 ... 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this file is exercising the code in main.c. # # $Id: main.test,v 1.13 2003/04/26 02:31:54 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl # Tests of the sqlite_complete() function. # do_test main-1.1 { ................................................................................ do_test main-1.26 { db complete { CREATE -- a comment TRIGGER xyz AFTER DELETE backend BEGIN UPDATE pqr SET a=5; } } {0} do_test main-1.27 { db complete { CREATE -- a comment TRIGGERX xyz AFTER DELETE backend BEGIN UPDATE pqr SET a=5; } } {1} do_test main-1.28 { db complete { CREATE TEMP TRIGGER xyz AFTER DELETE backend BEGIN UPDATE pqr SET a=5; } } {1} do_test main-1.29 { db complete { CREATE TRIGGER xyz AFTER DELETE backend BEGIN UPDATE pqr SET a=5; EXPLAIN select * from xyz; } } {0} ................................................................................ CREATE TABLE /* In comment ; */ } } {0} do_test main-1.31 { db complete { CREATE TABLE /* In comment ; */ hi; } } {1} do_test main-1.32 { db complete { stuff; /* CREATE TABLE multiple lines ................................................................................ multiple lines "*/ of text; } } {1} do_test main-1.35 { db complete {hi /**/ there;} } {1} # Try to open a database with a corrupt database file. # do_test main-2.0 { catch {db close} file delete -force test.db |
| | > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > > > > > |
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ... 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 ... 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 ... 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this file is exercising the code in main.c. # # $Id: main.test,v 1.14 2003/05/04 17:58:27 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl # Tests of the sqlite_complete() function. # do_test main-1.1 { ................................................................................ do_test main-1.26 { db complete { CREATE -- a comment TRIGGER xyz AFTER DELETE backend BEGIN UPDATE pqr SET a=5; } } {0} do_test main-1.27.1 { db complete { CREATE -- a comment TRIGGERX xyz AFTER DELETE backend BEGIN UPDATE pqr SET a=5; } } {1} do_test main-1.27.2 { db complete { CREATE/**/TRIGGER xyz AFTER DELETE backend BEGIN UPDATE pqr SET a=5; } } {0} do_test main-1.27.3 { db complete { /* */ EXPLAIN -- A comment CREATE/**/TRIGGER xyz AFTER DELETE backend BEGIN UPDATE pqr SET a=5; } } {0} do_test main-1.27.4 { db complete { BOGUS token CREATE TRIGGER xyz AFTER DELETE backend BEGIN UPDATE pqr SET a=5; } } {1} do_test main-1.27.5 { db complete { EXPLAIN CREATE TEMP TRIGGER xyz AFTER DELETE backend BEGIN UPDATE pqr SET a=5; } } {0} do_test main-1.28 { db complete { CREATE TEMP TRIGGER xyz AFTER DELETE backend BEGIN UPDATE pqr SET a=5; } } {0} do_test main-1.29 { db complete { CREATE TRIGGER xyz AFTER DELETE backend BEGIN UPDATE pqr SET a=5; EXPLAIN select * from xyz; } } {0} ................................................................................ CREATE TABLE /* In comment ; */ } } {0} do_test main-1.31 { db complete { CREATE TABLE /* In comment ; */ hi; } } {1} do_test main-1.31 { db complete { CREATE TABLE /* In comment ; */; } } {1} do_test main-1.32 { db complete { stuff; /* CREATE TABLE multiple lines ................................................................................ multiple lines "*/ of text; } } {1} do_test main-1.35 { db complete {hi /**/ there;} } {1} do_test main-1.36 { db complete {hi there/***/;} } {1} # Try to open a database with a corrupt database file. # do_test main-2.0 { catch {db close} file delete -force test.db |