OLD | NEW |
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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 ** Attempt to read the database schema and initialize internal | 127 ** Attempt to read the database schema and initialize internal |
128 ** data structures for a single database file. The index of the | 128 ** 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 | 129 ** 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 | 130 ** database. iDb==1 should never be used. iDb>=2 is used for |
131 ** auxiliary databases. Return one of the SQLITE_ error codes to | 131 ** auxiliary databases. Return one of the SQLITE_ error codes to |
132 ** indicate success or failure. | 132 ** indicate success or failure. |
133 */ | 133 */ |
134 static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ | 134 static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ |
135 int rc; | 135 int rc; |
136 int i; | 136 int i; |
| 137 #ifndef SQLITE_OMIT_DEPRECATED |
137 int size; | 138 int size; |
| 139 #endif |
138 Table *pTab; | 140 Table *pTab; |
139 Db *pDb; | 141 Db *pDb; |
140 char const *azArg[4]; | 142 char const *azArg[4]; |
141 int meta[5]; | 143 int meta[5]; |
142 InitData initData; | 144 InitData initData; |
143 char const *zMasterSchema; | 145 char const *zMasterSchema; |
144 char const *zMasterName; | 146 char const *zMasterName; |
145 int openedTransaction = 0; | 147 int openedTransaction = 0; |
146 | 148 |
147 /* | 149 /* |
(...skipping 22 matching lines...) Expand all Loading... |
170 #define temp_master_schema 0 | 172 #define temp_master_schema 0 |
171 #endif | 173 #endif |
172 | 174 |
173 assert( iDb>=0 && iDb<db->nDb ); | 175 assert( iDb>=0 && iDb<db->nDb ); |
174 assert( db->aDb[iDb].pSchema ); | 176 assert( db->aDb[iDb].pSchema ); |
175 assert( sqlite3_mutex_held(db->mutex) ); | 177 assert( sqlite3_mutex_held(db->mutex) ); |
176 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); | 178 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); |
177 | 179 |
178 /* zMasterSchema and zInitScript are set to point at the master schema | 180 /* zMasterSchema and zInitScript are set to point at the master schema |
179 ** and initialisation script appropriate for the database being | 181 ** and initialisation script appropriate for the database being |
180 ** initialised. zMasterName is the name of the master table. | 182 ** initialized. zMasterName is the name of the master table. |
181 */ | 183 */ |
182 if( !OMIT_TEMPDB && iDb==1 ){ | 184 if( !OMIT_TEMPDB && iDb==1 ){ |
183 zMasterSchema = temp_master_schema; | 185 zMasterSchema = temp_master_schema; |
184 }else{ | 186 }else{ |
185 zMasterSchema = master_schema; | 187 zMasterSchema = master_schema; |
186 } | 188 } |
187 zMasterName = SCHEMA_TABLE(iDb); | 189 zMasterName = SCHEMA_TABLE(iDb); |
188 | 190 |
189 /* Construct the schema tables. */ | 191 /* Construct the schema tables. */ |
190 azArg[0] = zMasterName; | 192 azArg[0] = zMasterName; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 } | 252 } |
251 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1]; | 253 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1]; |
252 | 254 |
253 /* If opening a non-empty database, check the text encoding. For the | 255 /* If opening a non-empty database, check the text encoding. For the |
254 ** main database, set sqlite3.enc to the encoding of the main database. | 256 ** main database, set sqlite3.enc to the encoding of the main database. |
255 ** For an attached db, it is an error if the encoding is not the same | 257 ** For an attached db, it is an error if the encoding is not the same |
256 ** as sqlite3.enc. | 258 ** as sqlite3.enc. |
257 */ | 259 */ |
258 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */ | 260 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */ |
259 if( iDb==0 ){ | 261 if( iDb==0 ){ |
| 262 #ifndef SQLITE_OMIT_UTF16 |
260 u8 encoding; | 263 u8 encoding; |
261 /* If opening the main database, set ENC(db). */ | 264 /* If opening the main database, set ENC(db). */ |
262 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3; | 265 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3; |
263 if( encoding==0 ) encoding = SQLITE_UTF8; | 266 if( encoding==0 ) encoding = SQLITE_UTF8; |
264 ENC(db) = encoding; | 267 ENC(db) = encoding; |
265 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0); | 268 #else |
| 269 ENC(db) = SQLITE_UTF8; |
| 270 #endif |
266 }else{ | 271 }else{ |
267 /* If opening an attached database, the encoding much match ENC(db) */ | 272 /* If opening an attached database, the encoding much match ENC(db) */ |
268 if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){ | 273 if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){ |
269 sqlite3SetString(pzErrMsg, db, "attached databases must use the same" | 274 sqlite3SetString(pzErrMsg, db, "attached databases must use the same" |
270 " text encoding as main database"); | 275 " text encoding as main database"); |
271 rc = SQLITE_ERROR; | 276 rc = SQLITE_ERROR; |
272 goto initone_error_out; | 277 goto initone_error_out; |
273 } | 278 } |
274 } | 279 } |
275 }else{ | 280 }else{ |
276 DbSetProperty(db, iDb, DB_Empty); | 281 DbSetProperty(db, iDb, DB_Empty); |
277 } | 282 } |
278 pDb->pSchema->enc = ENC(db); | 283 pDb->pSchema->enc = ENC(db); |
279 | 284 |
280 if( pDb->pSchema->cache_size==0 ){ | 285 if( pDb->pSchema->cache_size==0 ){ |
| 286 #ifndef SQLITE_OMIT_DEPRECATED |
281 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]); | 287 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]); |
282 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; } | 288 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; } |
283 pDb->pSchema->cache_size = size; | 289 pDb->pSchema->cache_size = size; |
| 290 #else |
| 291 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE; |
| 292 #endif |
284 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); | 293 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); |
285 } | 294 } |
286 | 295 |
287 /* | 296 /* |
288 ** file_format==1 Version 3.0.0. | 297 ** file_format==1 Version 3.0.0. |
289 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN | 298 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN |
290 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults | 299 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults |
291 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants | 300 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants |
292 */ | 301 */ |
293 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1]; | 302 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1]; |
(...skipping 18 matching lines...) Expand all Loading... |
312 /* Read the schema information out of the schema tables | 321 /* Read the schema information out of the schema tables |
313 */ | 322 */ |
314 assert( db->init.busy ); | 323 assert( db->init.busy ); |
315 { | 324 { |
316 char *zSql; | 325 char *zSql; |
317 zSql = sqlite3MPrintf(db, | 326 zSql = sqlite3MPrintf(db, |
318 "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid", | 327 "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid", |
319 db->aDb[iDb].zName, zMasterName); | 328 db->aDb[iDb].zName, zMasterName); |
320 #ifndef SQLITE_OMIT_AUTHORIZATION | 329 #ifndef SQLITE_OMIT_AUTHORIZATION |
321 { | 330 { |
322 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*); | 331 sqlite3_xauth xAuth; |
323 xAuth = db->xAuth; | 332 xAuth = db->xAuth; |
324 db->xAuth = 0; | 333 db->xAuth = 0; |
325 #endif | 334 #endif |
326 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); | 335 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
327 #ifndef SQLITE_OMIT_AUTHORIZATION | 336 #ifndef SQLITE_OMIT_AUTHORIZATION |
328 db->xAuth = xAuth; | 337 db->xAuth = xAuth; |
329 } | 338 } |
330 #endif | 339 #endif |
331 if( rc==SQLITE_OK ) rc = initData.rc; | 340 if( rc==SQLITE_OK ) rc = initData.rc; |
332 sqlite3DbFree(db, zSql); | 341 sqlite3DbFree(db, zSql); |
333 #ifndef SQLITE_OMIT_ANALYZE | 342 #ifndef SQLITE_OMIT_ANALYZE |
334 if( rc==SQLITE_OK ){ | 343 if( rc==SQLITE_OK ){ |
335 sqlite3AnalysisLoad(db, iDb); | 344 sqlite3AnalysisLoad(db, iDb); |
336 } | 345 } |
337 #endif | 346 #endif |
338 } | 347 } |
339 if( db->mallocFailed ){ | 348 if( db->mallocFailed ){ |
340 rc = SQLITE_NOMEM; | 349 rc = SQLITE_NOMEM; |
341 sqlite3ResetInternalSchema(db, -1); | 350 sqlite3ResetAllSchemasOfConnection(db); |
342 } | 351 } |
343 if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){ | 352 if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){ |
344 /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider | 353 /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider |
345 ** the schema loaded, even if errors occurred. In this situation the | 354 ** the schema loaded, even if errors occurred. In this situation the |
346 ** current sqlite3_prepare() operation will fail, but the following one | 355 ** current sqlite3_prepare() operation will fail, but the following one |
347 ** will attempt to compile the supplied statement against whatever subset | 356 ** will attempt to compile the supplied statement against whatever subset |
348 ** of the schema was loaded before the error occurred. The primary | 357 ** of the schema was loaded before the error occurred. The primary |
349 ** purpose of this is to allow access to the sqlite_master table | 358 ** purpose of this is to allow access to the sqlite_master table |
350 ** even when its contents have been corrupted. | 359 ** even when its contents have been corrupted. |
351 */ | 360 */ |
(...skipping 26 matching lines...) Expand all Loading... |
378 ** | 387 ** |
379 ** After a database is initialized, the DB_SchemaLoaded bit is set | 388 ** After a database is initialized, the DB_SchemaLoaded bit is set |
380 ** bit is set in the flags field of the Db structure. If the database | 389 ** bit is set in the flags field of the Db structure. If the database |
381 ** file was of zero-length, then the DB_Empty flag is also set. | 390 ** file was of zero-length, then the DB_Empty flag is also set. |
382 */ | 391 */ |
383 int sqlite3Init(sqlite3 *db, char **pzErrMsg){ | 392 int sqlite3Init(sqlite3 *db, char **pzErrMsg){ |
384 int i, rc; | 393 int i, rc; |
385 int commit_internal = !(db->flags&SQLITE_InternChanges); | 394 int commit_internal = !(db->flags&SQLITE_InternChanges); |
386 | 395 |
387 assert( sqlite3_mutex_held(db->mutex) ); | 396 assert( sqlite3_mutex_held(db->mutex) ); |
| 397 assert( db->init.busy==0 ); |
388 rc = SQLITE_OK; | 398 rc = SQLITE_OK; |
389 db->init.busy = 1; | 399 db->init.busy = 1; |
390 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ | 400 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
391 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue; | 401 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue; |
392 rc = sqlite3InitOne(db, i, pzErrMsg); | 402 rc = sqlite3InitOne(db, i, pzErrMsg); |
393 if( rc ){ | 403 if( rc ){ |
394 sqlite3ResetInternalSchema(db, i); | 404 sqlite3ResetOneSchema(db, i); |
395 } | 405 } |
396 } | 406 } |
397 | 407 |
398 /* Once all the other databases have been initialised, load the schema | 408 /* Once all the other databases have been initialized, load the schema |
399 ** for the TEMP database. This is loaded last, as the TEMP database | 409 ** for the TEMP database. This is loaded last, as the TEMP database |
400 ** schema may contain references to objects in other databases. | 410 ** schema may contain references to objects in other databases. |
401 */ | 411 */ |
402 #ifndef SQLITE_OMIT_TEMPDB | 412 #ifndef SQLITE_OMIT_TEMPDB |
403 if( rc==SQLITE_OK && ALWAYS(db->nDb>1) | 413 assert( db->nDb>1 ); |
404 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){ | 414 if( rc==SQLITE_OK && !DbHasProperty(db, 1, DB_SchemaLoaded) ){ |
405 rc = sqlite3InitOne(db, 1, pzErrMsg); | 415 rc = sqlite3InitOne(db, 1, pzErrMsg); |
406 if( rc ){ | 416 if( rc ){ |
407 sqlite3ResetInternalSchema(db, 1); | 417 sqlite3ResetOneSchema(db, 1); |
408 } | 418 } |
409 } | 419 } |
410 #endif | 420 #endif |
411 | 421 |
412 db->init.busy = 0; | 422 db->init.busy = 0; |
413 if( rc==SQLITE_OK && commit_internal ){ | 423 if( rc==SQLITE_OK && commit_internal ){ |
414 sqlite3CommitInternalChanges(db); | 424 sqlite3CommitInternalChanges(db); |
415 } | 425 } |
416 | 426 |
417 return rc; | 427 return rc; |
418 } | 428 } |
419 | 429 |
420 /* | 430 /* |
421 ** This routine is a no-op if the database schema is already initialised. | 431 ** This routine is a no-op if the database schema is already initialized. |
422 ** Otherwise, the schema is loaded. An error code is returned. | 432 ** Otherwise, the schema is loaded. An error code is returned. |
423 */ | 433 */ |
424 int sqlite3ReadSchema(Parse *pParse){ | 434 int sqlite3ReadSchema(Parse *pParse){ |
425 int rc = SQLITE_OK; | 435 int rc = SQLITE_OK; |
426 sqlite3 *db = pParse->db; | 436 sqlite3 *db = pParse->db; |
427 assert( sqlite3_mutex_held(db->mutex) ); | 437 assert( sqlite3_mutex_held(db->mutex) ); |
428 if( !db->init.busy ){ | 438 if( !db->init.busy ){ |
429 rc = sqlite3Init(db, &pParse->zErrMsg); | 439 rc = sqlite3Init(db, &pParse->zErrMsg); |
430 } | 440 } |
431 if( rc!=SQLITE_OK ){ | 441 if( rc!=SQLITE_OK ){ |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 if( rc!=SQLITE_OK ) return; | 475 if( rc!=SQLITE_OK ) return; |
466 openedTransaction = 1; | 476 openedTransaction = 1; |
467 } | 477 } |
468 | 478 |
469 /* Read the schema cookie from the database. If it does not match the | 479 /* Read the schema cookie from the database. If it does not match the |
470 ** value stored as part of the in-memory schema representation, | 480 ** value stored as part of the in-memory schema representation, |
471 ** set Parse.rc to SQLITE_SCHEMA. */ | 481 ** set Parse.rc to SQLITE_SCHEMA. */ |
472 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie); | 482 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie); |
473 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); | 483 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
474 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){ | 484 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){ |
475 sqlite3ResetInternalSchema(db, iDb); | 485 sqlite3ResetOneSchema(db, iDb); |
476 pParse->rc = SQLITE_SCHEMA; | 486 pParse->rc = SQLITE_SCHEMA; |
477 } | 487 } |
478 | 488 |
479 /* Close the transaction, if one was opened. */ | 489 /* Close the transaction, if one was opened. */ |
480 if( openedTransaction ){ | 490 if( openedTransaction ){ |
481 sqlite3BtreeCommit(pBt); | 491 sqlite3BtreeCommit(pBt); |
482 } | 492 } |
483 } | 493 } |
484 } | 494 } |
485 | 495 |
(...skipping 23 matching lines...) Expand all Loading... |
509 if( db->aDb[i].pSchema==pSchema ){ | 519 if( db->aDb[i].pSchema==pSchema ){ |
510 break; | 520 break; |
511 } | 521 } |
512 } | 522 } |
513 assert( i>=0 && i<db->nDb ); | 523 assert( i>=0 && i<db->nDb ); |
514 } | 524 } |
515 return i; | 525 return i; |
516 } | 526 } |
517 | 527 |
518 /* | 528 /* |
| 529 ** Free all memory allocations in the pParse object |
| 530 */ |
| 531 void sqlite3ParserReset(Parse *pParse){ |
| 532 if( pParse ){ |
| 533 sqlite3 *db = pParse->db; |
| 534 sqlite3DbFree(db, pParse->aLabel); |
| 535 sqlite3ExprListDelete(db, pParse->pConstExpr); |
| 536 } |
| 537 } |
| 538 |
| 539 /* |
519 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. | 540 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. |
520 */ | 541 */ |
521 static int sqlite3Prepare( | 542 static int sqlite3Prepare( |
522 sqlite3 *db, /* Database handle. */ | 543 sqlite3 *db, /* Database handle. */ |
523 const char *zSql, /* UTF-8 encoded SQL statement. */ | 544 const char *zSql, /* UTF-8 encoded SQL statement. */ |
524 int nBytes, /* Length of zSql in bytes. */ | 545 int nBytes, /* Length of zSql in bytes. */ |
525 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ | 546 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ |
526 Vdbe *pReprepare, /* VM being reprepared */ | 547 Vdbe *pReprepare, /* VM being reprepared */ |
527 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ | 548 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
528 const char **pzTail /* OUT: End of parsed string */ | 549 const char **pzTail /* OUT: End of parsed string */ |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 ** but it does *not* override schema lock detection, so this all still | 587 ** but it does *not* override schema lock detection, so this all still |
567 ** works even if READ_UNCOMMITTED is set. | 588 ** works even if READ_UNCOMMITTED is set. |
568 */ | 589 */ |
569 for(i=0; i<db->nDb; i++) { | 590 for(i=0; i<db->nDb; i++) { |
570 Btree *pBt = db->aDb[i].pBt; | 591 Btree *pBt = db->aDb[i].pBt; |
571 if( pBt ){ | 592 if( pBt ){ |
572 assert( sqlite3BtreeHoldsMutex(pBt) ); | 593 assert( sqlite3BtreeHoldsMutex(pBt) ); |
573 rc = sqlite3BtreeSchemaLocked(pBt); | 594 rc = sqlite3BtreeSchemaLocked(pBt); |
574 if( rc ){ | 595 if( rc ){ |
575 const char *zDb = db->aDb[i].zName; | 596 const char *zDb = db->aDb[i].zName; |
576 sqlite3Error(db, rc, "database schema is locked: %s", zDb); | 597 sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb); |
577 testcase( db->flags & SQLITE_ReadUncommitted ); | 598 testcase( db->flags & SQLITE_ReadUncommitted ); |
578 goto end_prepare; | 599 goto end_prepare; |
579 } | 600 } |
580 } | 601 } |
581 } | 602 } |
582 | 603 |
583 sqlite3VtabUnlockList(db); | 604 sqlite3VtabUnlockList(db); |
584 | 605 |
585 pParse->db = db; | 606 pParse->db = db; |
586 pParse->nQueryLoop = (double)1; | 607 pParse->nQueryLoop = 0; /* Logarithmic, so 0 really means 1 */ |
587 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){ | 608 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){ |
588 char *zSqlCopy; | 609 char *zSqlCopy; |
589 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; | 610 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; |
590 testcase( nBytes==mxLen ); | 611 testcase( nBytes==mxLen ); |
591 testcase( nBytes==mxLen+1 ); | 612 testcase( nBytes==mxLen+1 ); |
592 if( nBytes>mxLen ){ | 613 if( nBytes>mxLen ){ |
593 sqlite3Error(db, SQLITE_TOOBIG, "statement too long"); | 614 sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long"); |
594 rc = sqlite3ApiExit(db, SQLITE_TOOBIG); | 615 rc = sqlite3ApiExit(db, SQLITE_TOOBIG); |
595 goto end_prepare; | 616 goto end_prepare; |
596 } | 617 } |
597 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes); | 618 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes); |
598 if( zSqlCopy ){ | 619 if( zSqlCopy ){ |
599 sqlite3RunParser(pParse, zSqlCopy, &zErrMsg); | 620 sqlite3RunParser(pParse, zSqlCopy, &zErrMsg); |
600 sqlite3DbFree(db, zSqlCopy); | 621 sqlite3DbFree(db, zSqlCopy); |
601 pParse->zTail = &zSql[pParse->zTail-zSqlCopy]; | 622 pParse->zTail = &zSql[pParse->zTail-zSqlCopy]; |
602 }else{ | 623 }else{ |
603 pParse->zTail = &zSql[nBytes]; | 624 pParse->zTail = &zSql[nBytes]; |
604 } | 625 } |
605 }else{ | 626 }else{ |
606 sqlite3RunParser(pParse, zSql, &zErrMsg); | 627 sqlite3RunParser(pParse, zSql, &zErrMsg); |
607 } | 628 } |
608 assert( 1==(int)pParse->nQueryLoop ); | 629 assert( 0==pParse->nQueryLoop ); |
609 | 630 |
610 if( db->mallocFailed ){ | 631 if( db->mallocFailed ){ |
611 pParse->rc = SQLITE_NOMEM; | 632 pParse->rc = SQLITE_NOMEM; |
612 } | 633 } |
613 if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK; | 634 if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK; |
614 if( pParse->checkSchema ){ | 635 if( pParse->checkSchema ){ |
615 schemaIsValid(pParse); | 636 schemaIsValid(pParse); |
616 } | 637 } |
617 if( db->mallocFailed ){ | 638 if( db->mallocFailed ){ |
618 pParse->rc = SQLITE_NOMEM; | 639 pParse->rc = SQLITE_NOMEM; |
(...skipping 19 matching lines...) Expand all Loading... |
638 iFirst = 0; | 659 iFirst = 0; |
639 mx = 8; | 660 mx = 8; |
640 } | 661 } |
641 for(i=iFirst; i<mx; i++){ | 662 for(i=iFirst; i<mx; i++){ |
642 sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME, | 663 sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME, |
643 azColName[i], SQLITE_STATIC); | 664 azColName[i], SQLITE_STATIC); |
644 } | 665 } |
645 } | 666 } |
646 #endif | 667 #endif |
647 | 668 |
648 assert( db->init.busy==0 || saveSqlFlag==0 ); | |
649 if( db->init.busy==0 ){ | 669 if( db->init.busy==0 ){ |
650 Vdbe *pVdbe = pParse->pVdbe; | 670 Vdbe *pVdbe = pParse->pVdbe; |
651 sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag); | 671 sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag); |
652 } | 672 } |
653 if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){ | 673 if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){ |
654 sqlite3VdbeFinalize(pParse->pVdbe); | 674 sqlite3VdbeFinalize(pParse->pVdbe); |
655 assert(!(*ppStmt)); | 675 assert(!(*ppStmt)); |
656 }else{ | 676 }else{ |
657 *ppStmt = (sqlite3_stmt*)pParse->pVdbe; | 677 *ppStmt = (sqlite3_stmt*)pParse->pVdbe; |
658 } | 678 } |
659 | 679 |
660 if( zErrMsg ){ | 680 if( zErrMsg ){ |
661 sqlite3Error(db, rc, "%s", zErrMsg); | 681 sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg); |
662 sqlite3DbFree(db, zErrMsg); | 682 sqlite3DbFree(db, zErrMsg); |
663 }else{ | 683 }else{ |
664 sqlite3Error(db, rc, 0); | 684 sqlite3Error(db, rc); |
665 } | 685 } |
666 | 686 |
667 /* Delete any TriggerPrg structures allocated while parsing this statement. */ | 687 /* Delete any TriggerPrg structures allocated while parsing this statement. */ |
668 while( pParse->pTriggerPrg ){ | 688 while( pParse->pTriggerPrg ){ |
669 TriggerPrg *pT = pParse->pTriggerPrg; | 689 TriggerPrg *pT = pParse->pTriggerPrg; |
670 pParse->pTriggerPrg = pT->pNext; | 690 pParse->pTriggerPrg = pT->pNext; |
671 sqlite3DbFree(db, pT); | 691 sqlite3DbFree(db, pT); |
672 } | 692 } |
673 | 693 |
674 end_prepare: | 694 end_prepare: |
675 | 695 |
| 696 sqlite3ParserReset(pParse); |
676 sqlite3StackFree(db, pParse); | 697 sqlite3StackFree(db, pParse); |
677 rc = sqlite3ApiExit(db, rc); | 698 rc = sqlite3ApiExit(db, rc); |
678 assert( (rc&db->errMask)==rc ); | 699 assert( (rc&db->errMask)==rc ); |
679 return rc; | 700 return rc; |
680 } | 701 } |
681 static int sqlite3LockAndPrepare( | 702 static int sqlite3LockAndPrepare( |
682 sqlite3 *db, /* Database handle. */ | 703 sqlite3 *db, /* Database handle. */ |
683 const char *zSql, /* UTF-8 encoded SQL statement. */ | 704 const char *zSql, /* UTF-8 encoded SQL statement. */ |
684 int nBytes, /* Length of zSql in bytes. */ | 705 int nBytes, /* Length of zSql in bytes. */ |
685 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ | 706 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ |
686 Vdbe *pOld, /* VM being reprepared */ | 707 Vdbe *pOld, /* VM being reprepared */ |
687 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ | 708 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
688 const char **pzTail /* OUT: End of parsed string */ | 709 const char **pzTail /* OUT: End of parsed string */ |
689 ){ | 710 ){ |
690 int rc; | 711 int rc; |
691 assert( ppStmt!=0 ); | 712 assert( ppStmt!=0 ); |
692 *ppStmt = 0; | 713 *ppStmt = 0; |
693 if( !sqlite3SafetyCheckOk(db) ){ | 714 if( !sqlite3SafetyCheckOk(db) ){ |
694 return SQLITE_MISUSE_BKPT; | 715 return SQLITE_MISUSE_BKPT; |
695 } | 716 } |
696 sqlite3_mutex_enter(db->mutex); | 717 sqlite3_mutex_enter(db->mutex); |
697 sqlite3BtreeEnterAll(db); | 718 sqlite3BtreeEnterAll(db); |
698 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); | 719 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); |
699 if( rc==SQLITE_SCHEMA ){ | 720 if( rc==SQLITE_SCHEMA ){ |
700 sqlite3_finalize(*ppStmt); | 721 sqlite3_finalize(*ppStmt); |
701 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); | 722 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); |
702 } | 723 } |
703 sqlite3BtreeLeaveAll(db); | 724 sqlite3BtreeLeaveAll(db); |
704 sqlite3_mutex_leave(db->mutex); | 725 sqlite3_mutex_leave(db->mutex); |
| 726 assert( rc==SQLITE_OK || *ppStmt==0 ); |
705 return rc; | 727 return rc; |
706 } | 728 } |
707 | 729 |
708 /* | 730 /* |
709 ** Rerun the compilation of a statement after a schema change. | 731 ** Rerun the compilation of a statement after a schema change. |
710 ** | 732 ** |
711 ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise, | 733 ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise, |
712 ** if the statement cannot be recompiled because another connection has | 734 ** if the statement cannot be recompiled because another connection has |
713 ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error | 735 ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error |
714 ** occurs, return SQLITE_SCHEMA. | 736 ** occurs, return SQLITE_SCHEMA. |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
794 */ | 816 */ |
795 char *zSql8; | 817 char *zSql8; |
796 const char *zTail8 = 0; | 818 const char *zTail8 = 0; |
797 int rc = SQLITE_OK; | 819 int rc = SQLITE_OK; |
798 | 820 |
799 assert( ppStmt ); | 821 assert( ppStmt ); |
800 *ppStmt = 0; | 822 *ppStmt = 0; |
801 if( !sqlite3SafetyCheckOk(db) ){ | 823 if( !sqlite3SafetyCheckOk(db) ){ |
802 return SQLITE_MISUSE_BKPT; | 824 return SQLITE_MISUSE_BKPT; |
803 } | 825 } |
| 826 if( nBytes>=0 ){ |
| 827 int sz; |
| 828 const char *z = (const char*)zSql; |
| 829 for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){} |
| 830 nBytes = sz; |
| 831 } |
804 sqlite3_mutex_enter(db->mutex); | 832 sqlite3_mutex_enter(db->mutex); |
805 zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE); | 833 zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE); |
806 if( zSql8 ){ | 834 if( zSql8 ){ |
807 rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8); | 835 rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8); |
808 } | 836 } |
809 | 837 |
810 if( zTail8 && pzTail ){ | 838 if( zTail8 && pzTail ){ |
811 /* If sqlite3_prepare returns a tail pointer, we calculate the | 839 /* If sqlite3_prepare returns a tail pointer, we calculate the |
812 ** equivalent pointer into the UTF-16 string by counting the unicode | 840 ** equivalent pointer into the UTF-16 string by counting the unicode |
813 ** characters between zSql8 and zTail8, and then returning a pointer | 841 ** characters between zSql8 and zTail8, and then returning a pointer |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
849 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ | 877 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
850 const void **pzTail /* OUT: End of parsed string */ | 878 const void **pzTail /* OUT: End of parsed string */ |
851 ){ | 879 ){ |
852 int rc; | 880 int rc; |
853 rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail); | 881 rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail); |
854 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ | 882 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ |
855 return rc; | 883 return rc; |
856 } | 884 } |
857 | 885 |
858 #endif /* SQLITE_OMIT_UTF16 */ | 886 #endif /* SQLITE_OMIT_UTF16 */ |
OLD | NEW |