Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: third_party/sqlite/src/src/prepare.c

Issue 2751253002: [sql] Import SQLite 3.17.0. (Closed)
Patch Set: also clang on Linux i386 Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/sqlite/src/src/pragma.c ('k') | third_party/sqlite/src/src/printf.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 ** 2005 May 25 2 ** 2005 May 25
3 ** 3 **
4 ** The author disclaims copyright to this source code. In place of 4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing: 5 ** a legal notice, here is a blessing:
6 ** 6 **
7 ** May you do good and not evil. 7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others. 8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give. 9 ** May you share freely, never taking more than you give.
10 ** 10 **
(...skipping 10 matching lines...) Expand all
21 */ 21 */
22 static void corruptSchema( 22 static void corruptSchema(
23 InitData *pData, /* Initialization context */ 23 InitData *pData, /* Initialization context */
24 const char *zObj, /* Object being parsed at the point of error */ 24 const char *zObj, /* Object being parsed at the point of error */
25 const char *zExtra /* Error information */ 25 const char *zExtra /* Error information */
26 ){ 26 ){
27 sqlite3 *db = pData->db; 27 sqlite3 *db = pData->db;
28 if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){ 28 if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
29 char *z; 29 char *z;
30 if( zObj==0 ) zObj = "?"; 30 if( zObj==0 ) zObj = "?";
31 z = sqlite3_mprintf("malformed database schema (%s)", zObj); 31 z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
32 if( z && zExtra ) z = sqlite3_mprintf("%z - %s", z, zExtra); 32 if( zExtra ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
33 sqlite3DbFree(db, *pData->pzErrMsg); 33 sqlite3DbFree(db, *pData->pzErrMsg);
34 *pData->pzErrMsg = z; 34 *pData->pzErrMsg = z;
35 if( z==0 ) db->mallocFailed = 1;
36 } 35 }
37 pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT; 36 pData->rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_CORRUPT_BKPT;
38 } 37 }
39 38
40 /* 39 /*
41 ** This is the callback routine for the code that initializes the 40 ** This is the callback routine for the code that initializes the
42 ** database. See sqlite3Init() below for additional information. 41 ** database. See sqlite3Init() below for additional information.
43 ** This routine is also called from the OP_ParseSchema opcode of the VDBE. 42 ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
44 ** 43 **
45 ** Each callback contains the following information: 44 ** Each callback contains the following information:
46 ** 45 **
47 ** argv[0] = name of thing being created 46 ** argv[0] = name of thing being created
(...skipping 19 matching lines...) Expand all
67 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ 66 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
68 if( argv[1]==0 ){ 67 if( argv[1]==0 ){
69 corruptSchema(pData, argv[0], 0); 68 corruptSchema(pData, argv[0], 0);
70 }else if( sqlite3_strnicmp(argv[2],"create ",7)==0 ){ 69 }else if( sqlite3_strnicmp(argv[2],"create ",7)==0 ){
71 /* Call the parser to process a CREATE TABLE, INDEX or VIEW. 70 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
72 ** But because db->init.busy is set to 1, no VDBE code is generated 71 ** But because db->init.busy is set to 1, no VDBE code is generated
73 ** or executed. All the parser does is build the internal data 72 ** or executed. All the parser does is build the internal data
74 ** structures that describe the table, index, or view. 73 ** structures that describe the table, index, or view.
75 */ 74 */
76 int rc; 75 int rc;
76 u8 saved_iDb = db->init.iDb;
77 sqlite3_stmt *pStmt; 77 sqlite3_stmt *pStmt;
78 TESTONLY(int rcp); /* Return code from sqlite3_prepare() */ 78 TESTONLY(int rcp); /* Return code from sqlite3_prepare() */
79 79
80 assert( db->init.busy ); 80 assert( db->init.busy );
81 db->init.iDb = iDb; 81 db->init.iDb = iDb;
82 db->init.newTnum = sqlite3Atoi(argv[1]); 82 db->init.newTnum = sqlite3Atoi(argv[1]);
83 db->init.orphanTrigger = 0; 83 db->init.orphanTrigger = 0;
84 TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0); 84 TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
85 rc = db->errCode; 85 rc = db->errCode;
86 assert( (rc&0xFF)==(rcp&0xFF) ); 86 assert( (rc&0xFF)==(rcp&0xFF) );
87 db->init.iDb = 0; 87 db->init.iDb = saved_iDb;
88 assert( saved_iDb==0 || (db->flags & SQLITE_Vacuum)!=0 );
88 if( SQLITE_OK!=rc ){ 89 if( SQLITE_OK!=rc ){
89 if( db->init.orphanTrigger ){ 90 if( db->init.orphanTrigger ){
90 assert( iDb==1 ); 91 assert( iDb==1 );
91 }else{ 92 }else{
92 pData->rc = rc; 93 pData->rc = rc;
93 if( rc==SQLITE_NOMEM ){ 94 if( rc==SQLITE_NOMEM ){
94 db->mallocFailed = 1; 95 sqlite3OomFault(db);
95 }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){ 96 }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
96 corruptSchema(pData, argv[0], sqlite3_errmsg(db)); 97 corruptSchema(pData, argv[0], sqlite3_errmsg(db));
97 } 98 }
98 } 99 }
99 } 100 }
100 sqlite3_finalize(pStmt); 101 sqlite3_finalize(pStmt);
101 }else if( argv[0]==0 || (argv[2]!=0 && argv[2][0]!=0) ){ 102 }else if( argv[0]==0 || (argv[2]!=0 && argv[2][0]!=0) ){
102 corruptSchema(pData, argv[0], 0); 103 corruptSchema(pData, argv[0], 0);
103 }else{ 104 }else{
104 /* If the SQL column is blank it means this is an index that 105 /* If the SQL column is blank it means this is an index that
105 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE 106 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
106 ** constraint for a CREATE TABLE. The index should have already 107 ** constraint for a CREATE TABLE. The index should have already
107 ** been created when we processed the CREATE TABLE. All we have 108 ** been created when we processed the CREATE TABLE. All we have
108 ** to do here is record the root page number for that index. 109 ** to do here is record the root page number for that index.
109 */ 110 */
110 Index *pIndex; 111 Index *pIndex;
111 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName); 112 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zDbSName);
112 if( pIndex==0 ){ 113 if( pIndex==0 ){
113 /* This can occur if there exists an index on a TEMP table which 114 /* This can occur if there exists an index on a TEMP table which
114 ** has the same name as another index on a permanent index. Since 115 ** has the same name as another index on a permanent index. Since
115 ** the permanent table is hidden by the TEMP table, we can also 116 ** the permanent table is hidden by the TEMP table, we can also
116 ** safely ignore the index on the permanent table. 117 ** safely ignore the index on the permanent table.
117 */ 118 */
118 /* Do Nothing */; 119 /* Do Nothing */;
119 }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){ 120 }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
120 corruptSchema(pData, argv[0], "invalid rootpage"); 121 corruptSchema(pData, argv[0], "invalid rootpage");
121 } 122 }
122 } 123 }
123 return 0; 124 return 0;
124 } 125 }
125 126
126 /* 127 /*
127 ** Attempt to read the database schema and initialize internal 128 ** Attempt to read the database schema and initialize internal
128 ** data structures for a single database file. The index of the 129 ** data structures for a single database file. The index of the
129 ** database file is given by iDb. iDb==0 is used for the main 130 ** database file is given by iDb. iDb==0 is used for the main
130 ** database. iDb==1 should never be used. iDb>=2 is used for 131 ** database. iDb==1 should never be used. iDb>=2 is used for
131 ** auxiliary databases. Return one of the SQLITE_ error codes to 132 ** auxiliary databases. Return one of the SQLITE_ error codes to
132 ** indicate success or failure. 133 ** indicate success or failure.
133 */ 134 */
134 static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ 135 static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
135 int rc; 136 int rc;
136 int i; 137 int i;
137 #ifndef SQLITE_OMIT_DEPRECATED 138 #ifndef SQLITE_OMIT_DEPRECATED
138 int size; 139 int size;
139 #endif 140 #endif
140 Table *pTab;
141 Db *pDb; 141 Db *pDb;
142 char const *azArg[4]; 142 char const *azArg[4];
143 int meta[5]; 143 int meta[5];
144 InitData initData; 144 InitData initData;
145 char const *zMasterSchema; 145 const char *zMasterName;
146 char const *zMasterName;
147 int openedTransaction = 0; 146 int openedTransaction = 0;
148 147
149 /*
150 ** The master database table has a structure like this
151 */
152 static const char master_schema[] =
153 "CREATE TABLE sqlite_master(\n"
154 " type text,\n"
155 " name text,\n"
156 " tbl_name text,\n"
157 " rootpage integer,\n"
158 " sql text\n"
159 ")"
160 ;
161 #ifndef SQLITE_OMIT_TEMPDB
162 static const char temp_master_schema[] =
163 "CREATE TEMP TABLE sqlite_temp_master(\n"
164 " type text,\n"
165 " name text,\n"
166 " tbl_name text,\n"
167 " rootpage integer,\n"
168 " sql text\n"
169 ")"
170 ;
171 #else
172 #define temp_master_schema 0
173 #endif
174
175 assert( iDb>=0 && iDb<db->nDb ); 148 assert( iDb>=0 && iDb<db->nDb );
176 assert( db->aDb[iDb].pSchema ); 149 assert( db->aDb[iDb].pSchema );
177 assert( sqlite3_mutex_held(db->mutex) ); 150 assert( sqlite3_mutex_held(db->mutex) );
178 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); 151 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
179 152
180 /* zMasterSchema and zInitScript are set to point at the master schema 153 /* Construct the in-memory representation schema tables (sqlite_master or
181 ** and initialisation script appropriate for the database being 154 ** sqlite_temp_master) by invoking the parser directly. The appropriate
182 ** initialized. zMasterName is the name of the master table. 155 ** table name will be inserted automatically by the parser so we can just
183 */ 156 ** use the abbreviation "x" here. The parser will also automatically tag
184 if( !OMIT_TEMPDB && iDb==1 ){ 157 ** the schema table as read-only. */
185 zMasterSchema = temp_master_schema; 158 azArg[0] = zMasterName = SCHEMA_TABLE(iDb);
186 }else{
187 zMasterSchema = master_schema;
188 }
189 zMasterName = SCHEMA_TABLE(iDb);
190
191 /* Construct the schema tables. */
192 azArg[0] = zMasterName;
193 azArg[1] = "1"; 159 azArg[1] = "1";
194 azArg[2] = zMasterSchema; 160 azArg[2] = "CREATE TABLE x(type text,name text,tbl_name text,"
161 "rootpage integer,sql text)";
195 azArg[3] = 0; 162 azArg[3] = 0;
196 initData.db = db; 163 initData.db = db;
197 initData.iDb = iDb; 164 initData.iDb = iDb;
198 initData.rc = SQLITE_OK; 165 initData.rc = SQLITE_OK;
199 initData.pzErrMsg = pzErrMsg; 166 initData.pzErrMsg = pzErrMsg;
200 sqlite3InitCallback(&initData, 3, (char **)azArg, 0); 167 sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
201 if( initData.rc ){ 168 if( initData.rc ){
202 rc = initData.rc; 169 rc = initData.rc;
203 goto error_out; 170 goto error_out;
204 } 171 }
205 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
206 if( ALWAYS(pTab) ){
207 pTab->tabFlags |= TF_Readonly;
208 }
209 172
210 /* Create a cursor to hold the database open 173 /* Create a cursor to hold the database open
211 */ 174 */
212 pDb = &db->aDb[iDb]; 175 pDb = &db->aDb[iDb];
213 if( pDb->pBt==0 ){ 176 if( pDb->pBt==0 ){
214 if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){ 177 if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
215 DbSetProperty(db, 1, DB_SchemaLoaded); 178 DbSetProperty(db, 1, DB_SchemaLoaded);
216 } 179 }
217 return SQLITE_OK; 180 return SQLITE_OK;
218 } 181 }
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){ 280 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
318 db->flags &= ~SQLITE_LegacyFileFmt; 281 db->flags &= ~SQLITE_LegacyFileFmt;
319 } 282 }
320 283
321 /* Read the schema information out of the schema tables 284 /* Read the schema information out of the schema tables
322 */ 285 */
323 assert( db->init.busy ); 286 assert( db->init.busy );
324 { 287 {
325 char *zSql; 288 char *zSql;
326 zSql = sqlite3MPrintf(db, 289 zSql = sqlite3MPrintf(db,
327 "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid", 290 "SELECT name, rootpage, sql FROM \"%w\".%s ORDER BY rowid",
328 db->aDb[iDb].zName, zMasterName); 291 db->aDb[iDb].zDbSName, zMasterName);
329 #ifndef SQLITE_OMIT_AUTHORIZATION 292 #ifndef SQLITE_OMIT_AUTHORIZATION
330 { 293 {
331 sqlite3_xauth xAuth; 294 sqlite3_xauth xAuth;
332 xAuth = db->xAuth; 295 xAuth = db->xAuth;
333 db->xAuth = 0; 296 db->xAuth = 0;
334 #endif 297 #endif
335 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); 298 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
336 #ifndef SQLITE_OMIT_AUTHORIZATION 299 #ifndef SQLITE_OMIT_AUTHORIZATION
337 db->xAuth = xAuth; 300 db->xAuth = xAuth;
338 } 301 }
339 #endif 302 #endif
340 if( rc==SQLITE_OK ) rc = initData.rc; 303 if( rc==SQLITE_OK ) rc = initData.rc;
341 sqlite3DbFree(db, zSql); 304 sqlite3DbFree(db, zSql);
342 #ifndef SQLITE_OMIT_ANALYZE 305 #ifndef SQLITE_OMIT_ANALYZE
343 if( rc==SQLITE_OK ){ 306 if( rc==SQLITE_OK ){
344 sqlite3AnalysisLoad(db, iDb); 307 sqlite3AnalysisLoad(db, iDb);
345 } 308 }
346 #endif 309 #endif
347 } 310 }
348 if( db->mallocFailed ){ 311 if( db->mallocFailed ){
349 rc = SQLITE_NOMEM; 312 rc = SQLITE_NOMEM_BKPT;
350 sqlite3ResetAllSchemasOfConnection(db); 313 sqlite3ResetAllSchemasOfConnection(db);
351 } 314 }
352 if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){ 315 if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
353 /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider 316 /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
354 ** the schema loaded, even if errors occurred. In this situation the 317 ** the schema loaded, even if errors occurred. In this situation the
355 ** current sqlite3_prepare() operation will fail, but the following one 318 ** current sqlite3_prepare() operation will fail, but the following one
356 ** will attempt to compile the supplied statement against whatever subset 319 ** will attempt to compile the supplied statement against whatever subset
357 ** of the schema was loaded before the error occurred. The primary 320 ** of the schema was loaded before the error occurred. The primary
358 ** purpose of this is to allow access to the sqlite_master table 321 ** purpose of this is to allow access to the sqlite_master table
359 ** even when its contents have been corrupted. 322 ** even when its contents have been corrupted.
360 */ 323 */
361 DbSetProperty(db, iDb, DB_SchemaLoaded); 324 DbSetProperty(db, iDb, DB_SchemaLoaded);
362 rc = SQLITE_OK; 325 rc = SQLITE_OK;
363 } 326 }
364 327
365 /* Jump here for an error that occurs after successfully allocating 328 /* Jump here for an error that occurs after successfully allocating
366 ** curMain and calling sqlite3BtreeEnter(). For an error that occurs 329 ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
367 ** before that point, jump to error_out. 330 ** before that point, jump to error_out.
368 */ 331 */
369 initone_error_out: 332 initone_error_out:
370 if( openedTransaction ){ 333 if( openedTransaction ){
371 sqlite3BtreeCommit(pDb->pBt); 334 sqlite3BtreeCommit(pDb->pBt);
372 } 335 }
373 sqlite3BtreeLeave(pDb->pBt); 336 sqlite3BtreeLeave(pDb->pBt);
374 337
375 error_out: 338 error_out:
376 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ 339 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
377 db->mallocFailed = 1; 340 sqlite3OomFault(db);
378 } 341 }
379 return rc; 342 return rc;
380 } 343 }
381 344
382 /* 345 /*
383 ** Initialize all database files - the main database file, the file 346 ** Initialize all database files - the main database file, the file
384 ** used to store temporary tables, and any additional database files 347 ** used to store temporary tables, and any additional database files
385 ** created using ATTACH statements. Return a success code. If an 348 ** created using ATTACH statements. Return a success code. If an
386 ** error occurs, write an error message into *pzErrMsg. 349 ** error occurs, write an error message into *pzErrMsg.
387 ** 350 **
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 int openedTransaction = 0; /* True if a transaction is opened */ 428 int openedTransaction = 0; /* True if a transaction is opened */
466 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */ 429 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
467 if( pBt==0 ) continue; 430 if( pBt==0 ) continue;
468 431
469 /* If there is not already a read-only (or read-write) transaction opened 432 /* If there is not already a read-only (or read-write) transaction opened
470 ** on the b-tree database, open one now. If a transaction is opened, it 433 ** on the b-tree database, open one now. If a transaction is opened, it
471 ** will be closed immediately after reading the meta-value. */ 434 ** will be closed immediately after reading the meta-value. */
472 if( !sqlite3BtreeIsInReadTrans(pBt) ){ 435 if( !sqlite3BtreeIsInReadTrans(pBt) ){
473 rc = sqlite3BtreeBeginTrans(pBt, 0); 436 rc = sqlite3BtreeBeginTrans(pBt, 0);
474 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ 437 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
475 db->mallocFailed = 1; 438 sqlite3OomFault(db);
476 } 439 }
477 if( rc!=SQLITE_OK ) return; 440 if( rc!=SQLITE_OK ) return;
478 openedTransaction = 1; 441 openedTransaction = 1;
479 } 442 }
480 443
481 /* Read the schema cookie from the database. If it does not match the 444 /* Read the schema cookie from the database. If it does not match the
482 ** value stored as part of the in-memory schema representation, 445 ** value stored as part of the in-memory schema representation,
483 ** set Parse.rc to SQLITE_SCHEMA. */ 446 ** set Parse.rc to SQLITE_SCHEMA. */
484 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie); 447 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
485 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 448 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 } 491 }
529 492
530 /* 493 /*
531 ** Free all memory allocations in the pParse object 494 ** Free all memory allocations in the pParse object
532 */ 495 */
533 void sqlite3ParserReset(Parse *pParse){ 496 void sqlite3ParserReset(Parse *pParse){
534 if( pParse ){ 497 if( pParse ){
535 sqlite3 *db = pParse->db; 498 sqlite3 *db = pParse->db;
536 sqlite3DbFree(db, pParse->aLabel); 499 sqlite3DbFree(db, pParse->aLabel);
537 sqlite3ExprListDelete(db, pParse->pConstExpr); 500 sqlite3ExprListDelete(db, pParse->pConstExpr);
501 if( db ){
502 assert( db->lookaside.bDisable >= pParse->disableLookaside );
503 db->lookaside.bDisable -= pParse->disableLookaside;
504 }
505 pParse->disableLookaside = 0;
538 } 506 }
539 } 507 }
540 508
541 /* 509 /*
542 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. 510 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
543 */ 511 */
544 static int sqlite3Prepare( 512 static int sqlite3Prepare(
545 sqlite3 *db, /* Database handle. */ 513 sqlite3 *db, /* Database handle. */
546 const char *zSql, /* UTF-8 encoded SQL statement. */ 514 const char *zSql, /* UTF-8 encoded SQL statement. */
547 int nBytes, /* Length of zSql in bytes. */ 515 int nBytes, /* Length of zSql in bytes. */
548 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ 516 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */
549 Vdbe *pReprepare, /* VM being reprepared */ 517 Vdbe *pReprepare, /* VM being reprepared */
550 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ 518 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
551 const char **pzTail /* OUT: End of parsed string */ 519 const char **pzTail /* OUT: End of parsed string */
552 ){ 520 ){
553 Parse *pParse; /* Parsing context */
554 char *zErrMsg = 0; /* Error message */ 521 char *zErrMsg = 0; /* Error message */
555 int rc = SQLITE_OK; /* Result code */ 522 int rc = SQLITE_OK; /* Result code */
556 int i; /* Loop counter */ 523 int i; /* Loop counter */
524 Parse sParse; /* Parsing context */
557 525
558 /* Allocate the parsing context */ 526 memset(&sParse, 0, PARSE_HDR_SZ);
559 pParse = sqlite3StackAllocZero(db, sizeof(*pParse)); 527 memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ);
560 if( pParse==0 ){ 528 sParse.pReprepare = pReprepare;
561 rc = SQLITE_NOMEM;
562 goto end_prepare;
563 }
564 pParse->pReprepare = pReprepare;
565 assert( ppStmt && *ppStmt==0 ); 529 assert( ppStmt && *ppStmt==0 );
566 assert( !db->mallocFailed ); 530 /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
567 assert( sqlite3_mutex_held(db->mutex) ); 531 assert( sqlite3_mutex_held(db->mutex) );
568 532
569 /* Check to verify that it is possible to get a read lock on all 533 /* Check to verify that it is possible to get a read lock on all
570 ** database schemas. The inability to get a read lock indicates that 534 ** database schemas. The inability to get a read lock indicates that
571 ** some other database connection is holding a write-lock, which in 535 ** some other database connection is holding a write-lock, which in
572 ** turn means that the other connection has made uncommitted changes 536 ** turn means that the other connection has made uncommitted changes
573 ** to the schema. 537 ** to the schema.
574 ** 538 **
575 ** Were we to proceed and prepare the statement against the uncommitted 539 ** Were we to proceed and prepare the statement against the uncommitted
576 ** schema changes and if those schema changes are subsequently rolled 540 ** schema changes and if those schema changes are subsequently rolled
(...skipping 11 matching lines...) Expand all
588 ** Note that setting READ_UNCOMMITTED overrides most lock detection, 552 ** Note that setting READ_UNCOMMITTED overrides most lock detection,
589 ** but it does *not* override schema lock detection, so this all still 553 ** but it does *not* override schema lock detection, so this all still
590 ** works even if READ_UNCOMMITTED is set. 554 ** works even if READ_UNCOMMITTED is set.
591 */ 555 */
592 for(i=0; i<db->nDb; i++) { 556 for(i=0; i<db->nDb; i++) {
593 Btree *pBt = db->aDb[i].pBt; 557 Btree *pBt = db->aDb[i].pBt;
594 if( pBt ){ 558 if( pBt ){
595 assert( sqlite3BtreeHoldsMutex(pBt) ); 559 assert( sqlite3BtreeHoldsMutex(pBt) );
596 rc = sqlite3BtreeSchemaLocked(pBt); 560 rc = sqlite3BtreeSchemaLocked(pBt);
597 if( rc ){ 561 if( rc ){
598 const char *zDb = db->aDb[i].zName; 562 const char *zDb = db->aDb[i].zDbSName;
599 sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb); 563 sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
600 testcase( db->flags & SQLITE_ReadUncommitted ); 564 testcase( db->flags & SQLITE_ReadUncommitted );
601 goto end_prepare; 565 goto end_prepare;
602 } 566 }
603 } 567 }
604 } 568 }
605 569
606 sqlite3VtabUnlockList(db); 570 sqlite3VtabUnlockList(db);
607 571
608 pParse->db = db; 572 sParse.db = db;
609 pParse->nQueryLoop = 0; /* Logarithmic, so 0 really means 1 */
610 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){ 573 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
611 char *zSqlCopy; 574 char *zSqlCopy;
612 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; 575 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
613 testcase( nBytes==mxLen ); 576 testcase( nBytes==mxLen );
614 testcase( nBytes==mxLen+1 ); 577 testcase( nBytes==mxLen+1 );
615 if( nBytes>mxLen ){ 578 if( nBytes>mxLen ){
616 sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long"); 579 sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long");
617 rc = sqlite3ApiExit(db, SQLITE_TOOBIG); 580 rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
618 goto end_prepare; 581 goto end_prepare;
619 } 582 }
620 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes); 583 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
621 if( zSqlCopy ){ 584 if( zSqlCopy ){
622 sqlite3RunParser(pParse, zSqlCopy, &zErrMsg); 585 sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
586 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
623 sqlite3DbFree(db, zSqlCopy); 587 sqlite3DbFree(db, zSqlCopy);
624 pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
625 }else{ 588 }else{
626 pParse->zTail = &zSql[nBytes]; 589 sParse.zTail = &zSql[nBytes];
627 } 590 }
628 }else{ 591 }else{
629 sqlite3RunParser(pParse, zSql, &zErrMsg); 592 sqlite3RunParser(&sParse, zSql, &zErrMsg);
630 } 593 }
631 assert( 0==pParse->nQueryLoop ); 594 assert( 0==sParse.nQueryLoop );
632 595
633 if( db->mallocFailed ){ 596 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
634 pParse->rc = SQLITE_NOMEM; 597 if( sParse.checkSchema ){
635 } 598 schemaIsValid(&sParse);
636 if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
637 if( pParse->checkSchema ){
638 schemaIsValid(pParse);
639 } 599 }
640 if( db->mallocFailed ){ 600 if( db->mallocFailed ){
641 pParse->rc = SQLITE_NOMEM; 601 sParse.rc = SQLITE_NOMEM_BKPT;
642 } 602 }
643 if( pzTail ){ 603 if( pzTail ){
644 *pzTail = pParse->zTail; 604 *pzTail = sParse.zTail;
645 } 605 }
646 rc = pParse->rc; 606 rc = sParse.rc;
647 607
648 #ifndef SQLITE_OMIT_EXPLAIN 608 #ifndef SQLITE_OMIT_EXPLAIN
649 if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){ 609 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
650 static const char * const azColName[] = { 610 static const char * const azColName[] = {
651 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment", 611 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
652 "selectid", "order", "from", "detail" 612 "selectid", "order", "from", "detail"
653 }; 613 };
654 int iFirst, mx; 614 int iFirst, mx;
655 if( pParse->explain==2 ){ 615 if( sParse.explain==2 ){
656 sqlite3VdbeSetNumCols(pParse->pVdbe, 4); 616 sqlite3VdbeSetNumCols(sParse.pVdbe, 4);
657 iFirst = 8; 617 iFirst = 8;
658 mx = 12; 618 mx = 12;
659 }else{ 619 }else{
660 sqlite3VdbeSetNumCols(pParse->pVdbe, 8); 620 sqlite3VdbeSetNumCols(sParse.pVdbe, 8);
661 iFirst = 0; 621 iFirst = 0;
662 mx = 8; 622 mx = 8;
663 } 623 }
664 for(i=iFirst; i<mx; i++){ 624 for(i=iFirst; i<mx; i++){
665 sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME, 625 sqlite3VdbeSetColName(sParse.pVdbe, i-iFirst, COLNAME_NAME,
666 azColName[i], SQLITE_STATIC); 626 azColName[i], SQLITE_STATIC);
667 } 627 }
668 } 628 }
669 #endif 629 #endif
670 630
671 if( db->init.busy==0 ){ 631 if( db->init.busy==0 ){
672 Vdbe *pVdbe = pParse->pVdbe; 632 Vdbe *pVdbe = sParse.pVdbe;
673 sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag); 633 sqlite3VdbeSetSql(pVdbe, zSql, (int)(sParse.zTail-zSql), saveSqlFlag);
674 } 634 }
675 if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){ 635 if( sParse.pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
676 sqlite3VdbeFinalize(pParse->pVdbe); 636 sqlite3VdbeFinalize(sParse.pVdbe);
677 assert(!(*ppStmt)); 637 assert(!(*ppStmt));
678 }else{ 638 }else{
679 *ppStmt = (sqlite3_stmt*)pParse->pVdbe; 639 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
680 } 640 }
681 641
682 if( zErrMsg ){ 642 if( zErrMsg ){
683 sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg); 643 sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg);
684 sqlite3DbFree(db, zErrMsg); 644 sqlite3DbFree(db, zErrMsg);
685 }else{ 645 }else{
686 sqlite3Error(db, rc); 646 sqlite3Error(db, rc);
687 } 647 }
688 648
689 /* Delete any TriggerPrg structures allocated while parsing this statement. */ 649 /* Delete any TriggerPrg structures allocated while parsing this statement. */
690 while( pParse->pTriggerPrg ){ 650 while( sParse.pTriggerPrg ){
691 TriggerPrg *pT = pParse->pTriggerPrg; 651 TriggerPrg *pT = sParse.pTriggerPrg;
692 pParse->pTriggerPrg = pT->pNext; 652 sParse.pTriggerPrg = pT->pNext;
693 sqlite3DbFree(db, pT); 653 sqlite3DbFree(db, pT);
694 } 654 }
695 655
696 end_prepare: 656 end_prepare:
697 657
698 sqlite3ParserReset(pParse); 658 sqlite3ParserReset(&sParse);
699 sqlite3StackFree(db, pParse);
700 rc = sqlite3ApiExit(db, rc); 659 rc = sqlite3ApiExit(db, rc);
701 assert( (rc&db->errMask)==rc ); 660 assert( (rc&db->errMask)==rc );
702 return rc; 661 return rc;
703 } 662 }
704 static int sqlite3LockAndPrepare( 663 static int sqlite3LockAndPrepare(
705 sqlite3 *db, /* Database handle. */ 664 sqlite3 *db, /* Database handle. */
706 const char *zSql, /* UTF-8 encoded SQL statement. */ 665 const char *zSql, /* UTF-8 encoded SQL statement. */
707 int nBytes, /* Length of zSql in bytes. */ 666 int nBytes, /* Length of zSql in bytes. */
708 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ 667 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */
709 Vdbe *pOld, /* VM being reprepared */ 668 Vdbe *pOld, /* VM being reprepared */
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
747 sqlite3 *db; 706 sqlite3 *db;
748 707
749 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) ); 708 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
750 zSql = sqlite3_sql((sqlite3_stmt *)p); 709 zSql = sqlite3_sql((sqlite3_stmt *)p);
751 assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */ 710 assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */
752 db = sqlite3VdbeDb(p); 711 db = sqlite3VdbeDb(p);
753 assert( sqlite3_mutex_held(db->mutex) ); 712 assert( sqlite3_mutex_held(db->mutex) );
754 rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0); 713 rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
755 if( rc ){ 714 if( rc ){
756 if( rc==SQLITE_NOMEM ){ 715 if( rc==SQLITE_NOMEM ){
757 db->mallocFailed = 1; 716 sqlite3OomFault(db);
758 } 717 }
759 assert( pNew==0 ); 718 assert( pNew==0 );
760 return rc; 719 return rc;
761 }else{ 720 }else{
762 assert( pNew!=0 ); 721 assert( pNew!=0 );
763 } 722 }
764 sqlite3VdbeSwap((Vdbe*)pNew, p); 723 sqlite3VdbeSwap((Vdbe*)pNew, p);
765 sqlite3TransferBindings(pNew, (sqlite3_stmt*)p); 724 sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
766 sqlite3VdbeResetStepResult((Vdbe*)pNew); 725 sqlite3VdbeResetStepResult((Vdbe*)pNew);
767 sqlite3VdbeFinalize((Vdbe*)pNew); 726 sqlite3VdbeFinalize((Vdbe*)pNew);
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
884 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ 843 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
885 const void **pzTail /* OUT: End of parsed string */ 844 const void **pzTail /* OUT: End of parsed string */
886 ){ 845 ){
887 int rc; 846 int rc;
888 rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail); 847 rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
889 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ 848 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
890 return rc; 849 return rc;
891 } 850 }
892 851
893 #endif /* SQLITE_OMIT_UTF16 */ 852 #endif /* SQLITE_OMIT_UTF16 */
OLDNEW
« no previous file with comments | « third_party/sqlite/src/src/pragma.c ('k') | third_party/sqlite/src/src/printf.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698