| 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 ** |
| 11 ************************************************************************* | 11 ************************************************************************* |
| 12 ** This file contains the implementation of the sqlite3_prepare() | 12 ** This file contains the implementation of the sqlite3_prepare() |
| 13 ** interface, and routines that contribute to loading the database schema | 13 ** interface, and routines that contribute to loading the database schema |
| 14 ** from disk. | 14 ** from disk. |
| 15 */ | 15 */ |
| 16 #include "sqliteInt.h" | 16 #include "sqliteInt.h" |
| 17 | 17 |
| 18 /* | 18 /* |
| 19 ** Fill the InitData structure with an error message that indicates | 19 ** Fill the InitData structure with an error message that indicates |
| 20 ** that the database is corrupt. | 20 ** that the database is corrupt. |
| 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 if( zObj==0 ) zObj = "?"; | 30 if( zObj==0 ) zObj = "?"; |
| 30 sqlite3SetString(pData->pzErrMsg, db, | 31 z = sqlite3_mprintf("malformed database schema (%s)", zObj); |
| 31 "malformed database schema (%s)", zObj); | 32 if( z && zExtra ) z = sqlite3_mprintf("%z - %s", z, zExtra); |
| 32 if( zExtra ){ | 33 sqlite3DbFree(db, *pData->pzErrMsg); |
| 33 *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg, | 34 *pData->pzErrMsg = z; |
| 34 "%s - %s", *pData->pzErrMsg, zExtra); | 35 if( z==0 ) db->mallocFailed = 1; |
| 35 } | |
| 36 } | 36 } |
| 37 pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT; | 37 pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT; |
| 38 } | 38 } |
| 39 | 39 |
| 40 /* | 40 /* |
| 41 ** This is the callback routine for the code that initializes the | 41 ** This is the callback routine for the code that initializes the |
| 42 ** database. See sqlite3Init() below for additional information. | 42 ** database. See sqlite3Init() below for additional information. |
| 43 ** This routine is also called from the OP_ParseSchema opcode of the VDBE. | 43 ** This routine is also called from the OP_ParseSchema opcode of the VDBE. |
| 44 ** | 44 ** |
| 45 ** Each callback contains the following information: | 45 ** Each callback contains the following information: |
| (...skipping 14 matching lines...) Expand all Loading... |
| 60 DbClearProperty(db, iDb, DB_Empty); | 60 DbClearProperty(db, iDb, DB_Empty); |
| 61 if( db->mallocFailed ){ | 61 if( db->mallocFailed ){ |
| 62 corruptSchema(pData, argv[0], 0); | 62 corruptSchema(pData, argv[0], 0); |
| 63 return 1; | 63 return 1; |
| 64 } | 64 } |
| 65 | 65 |
| 66 assert( iDb>=0 && iDb<db->nDb ); | 66 assert( iDb>=0 && iDb<db->nDb ); |
| 67 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ | 67 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ |
| 68 if( argv[1]==0 ){ | 68 if( argv[1]==0 ){ |
| 69 corruptSchema(pData, argv[0], 0); | 69 corruptSchema(pData, argv[0], 0); |
| 70 }else if( argv[2] && argv[2][0] ){ | 70 }else if( sqlite3_strnicmp(argv[2],"create ",7)==0 ){ |
| 71 /* Call the parser to process a CREATE TABLE, INDEX or VIEW. | 71 /* 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 | 72 ** 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 | 73 ** or executed. All the parser does is build the internal data |
| 74 ** structures that describe the table, index, or view. | 74 ** structures that describe the table, index, or view. |
| 75 */ | 75 */ |
| 76 int rc; | 76 int rc; |
| 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 ); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 91 }else{ | 91 }else{ |
| 92 pData->rc = rc; | 92 pData->rc = rc; |
| 93 if( rc==SQLITE_NOMEM ){ | 93 if( rc==SQLITE_NOMEM ){ |
| 94 db->mallocFailed = 1; | 94 db->mallocFailed = 1; |
| 95 }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){ | 95 }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){ |
| 96 corruptSchema(pData, argv[0], sqlite3_errmsg(db)); | 96 corruptSchema(pData, argv[0], sqlite3_errmsg(db)); |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 } | 99 } |
| 100 sqlite3_finalize(pStmt); | 100 sqlite3_finalize(pStmt); |
| 101 }else if( argv[0]==0 ){ | 101 }else if( argv[0]==0 || (argv[2]!=0 && argv[2][0]!=0) ){ |
| 102 corruptSchema(pData, 0, 0); | 102 corruptSchema(pData, argv[0], 0); |
| 103 }else{ | 103 }else{ |
| 104 /* If the SQL column is blank it means this is an index that | 104 /* 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 | 105 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE |
| 106 ** constraint for a CREATE TABLE. The index should have already | 106 ** constraint for a CREATE TABLE. The index should have already |
| 107 ** been created when we processed the CREATE TABLE. All we have | 107 ** been created when we processed the CREATE TABLE. All we have |
| 108 ** to do here is record the root page number for that index. | 108 ** to do here is record the root page number for that index. |
| 109 */ | 109 */ |
| 110 Index *pIndex; | 110 Index *pIndex; |
| 111 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName); | 111 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName); |
| 112 if( pIndex==0 ){ | 112 if( pIndex==0 ){ |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 return SQLITE_OK; | 217 return SQLITE_OK; |
| 218 } | 218 } |
| 219 | 219 |
| 220 /* If there is not already a read-only (or read-write) transaction opened | 220 /* If there is not already a read-only (or read-write) transaction opened |
| 221 ** on the b-tree database, open one now. If a transaction is opened, it | 221 ** on the b-tree database, open one now. If a transaction is opened, it |
| 222 ** will be closed before this function returns. */ | 222 ** will be closed before this function returns. */ |
| 223 sqlite3BtreeEnter(pDb->pBt); | 223 sqlite3BtreeEnter(pDb->pBt); |
| 224 if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){ | 224 if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){ |
| 225 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0); | 225 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0); |
| 226 if( rc!=SQLITE_OK ){ | 226 if( rc!=SQLITE_OK ){ |
| 227 sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc)); | 227 sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc)); |
| 228 goto initone_error_out; | 228 goto initone_error_out; |
| 229 } | 229 } |
| 230 openedTransaction = 1; | 230 openedTransaction = 1; |
| 231 } | 231 } |
| 232 | 232 |
| 233 /* Get the database meta information. | 233 /* Get the database meta information. |
| 234 ** | 234 ** |
| 235 ** Meta values are as follows: | 235 ** Meta values are as follows: |
| 236 ** meta[0] Schema cookie. Changes with each schema change. | 236 ** meta[0] Schema cookie. Changes with each schema change. |
| 237 ** meta[1] File format of schema layer. | 237 ** meta[1] File format of schema layer. |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 ** | 387 ** |
| 388 ** After a database is initialized, the DB_SchemaLoaded bit is set | 388 ** After a database is initialized, the DB_SchemaLoaded bit is set |
| 389 ** 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 |
| 390 ** 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. |
| 391 */ | 391 */ |
| 392 int sqlite3Init(sqlite3 *db, char **pzErrMsg){ | 392 int sqlite3Init(sqlite3 *db, char **pzErrMsg){ |
| 393 int i, rc; | 393 int i, rc; |
| 394 int commit_internal = !(db->flags&SQLITE_InternChanges); | 394 int commit_internal = !(db->flags&SQLITE_InternChanges); |
| 395 | 395 |
| 396 assert( sqlite3_mutex_held(db->mutex) ); | 396 assert( sqlite3_mutex_held(db->mutex) ); |
| 397 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) ); |
| 397 assert( db->init.busy==0 ); | 398 assert( db->init.busy==0 ); |
| 398 rc = SQLITE_OK; | 399 rc = SQLITE_OK; |
| 399 db->init.busy = 1; | 400 db->init.busy = 1; |
| 401 ENC(db) = SCHEMA_ENC(db); |
| 400 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ | 402 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
| 401 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue; | 403 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue; |
| 402 rc = sqlite3InitOne(db, i, pzErrMsg); | 404 rc = sqlite3InitOne(db, i, pzErrMsg); |
| 403 if( rc ){ | 405 if( rc ){ |
| 404 sqlite3ResetOneSchema(db, i); | 406 sqlite3ResetOneSchema(db, i); |
| 405 } | 407 } |
| 406 } | 408 } |
| 407 | 409 |
| 408 /* Once all the other databases have been initialized, load the schema | 410 /* Once all the other databases have been initialized, load the schema |
| 409 ** for the TEMP database. This is loaded last, as the TEMP database | 411 ** for the TEMP database. This is loaded last, as the TEMP database |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 702 static int sqlite3LockAndPrepare( | 704 static int sqlite3LockAndPrepare( |
| 703 sqlite3 *db, /* Database handle. */ | 705 sqlite3 *db, /* Database handle. */ |
| 704 const char *zSql, /* UTF-8 encoded SQL statement. */ | 706 const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 705 int nBytes, /* Length of zSql in bytes. */ | 707 int nBytes, /* Length of zSql in bytes. */ |
| 706 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ | 708 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ |
| 707 Vdbe *pOld, /* VM being reprepared */ | 709 Vdbe *pOld, /* VM being reprepared */ |
| 708 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ | 710 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 709 const char **pzTail /* OUT: End of parsed string */ | 711 const char **pzTail /* OUT: End of parsed string */ |
| 710 ){ | 712 ){ |
| 711 int rc; | 713 int rc; |
| 712 assert( ppStmt!=0 ); | 714 |
| 715 #ifdef SQLITE_ENABLE_API_ARMOR |
| 716 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT; |
| 717 #endif |
| 713 *ppStmt = 0; | 718 *ppStmt = 0; |
| 714 if( !sqlite3SafetyCheckOk(db) ){ | 719 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){ |
| 715 return SQLITE_MISUSE_BKPT; | 720 return SQLITE_MISUSE_BKPT; |
| 716 } | 721 } |
| 717 sqlite3_mutex_enter(db->mutex); | 722 sqlite3_mutex_enter(db->mutex); |
| 718 sqlite3BtreeEnterAll(db); | 723 sqlite3BtreeEnterAll(db); |
| 719 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); | 724 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); |
| 720 if( rc==SQLITE_SCHEMA ){ | 725 if( rc==SQLITE_SCHEMA ){ |
| 721 sqlite3_finalize(*ppStmt); | 726 sqlite3_finalize(*ppStmt); |
| 722 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); | 727 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); |
| 723 } | 728 } |
| 724 sqlite3BtreeLeaveAll(db); | 729 sqlite3BtreeLeaveAll(db); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 811 const void **pzTail /* OUT: End of parsed string */ | 816 const void **pzTail /* OUT: End of parsed string */ |
| 812 ){ | 817 ){ |
| 813 /* This function currently works by first transforming the UTF-16 | 818 /* This function currently works by first transforming the UTF-16 |
| 814 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The | 819 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The |
| 815 ** tricky bit is figuring out the pointer to return in *pzTail. | 820 ** tricky bit is figuring out the pointer to return in *pzTail. |
| 816 */ | 821 */ |
| 817 char *zSql8; | 822 char *zSql8; |
| 818 const char *zTail8 = 0; | 823 const char *zTail8 = 0; |
| 819 int rc = SQLITE_OK; | 824 int rc = SQLITE_OK; |
| 820 | 825 |
| 821 assert( ppStmt ); | 826 #ifdef SQLITE_ENABLE_API_ARMOR |
| 827 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT; |
| 828 #endif |
| 822 *ppStmt = 0; | 829 *ppStmt = 0; |
| 823 if( !sqlite3SafetyCheckOk(db) ){ | 830 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){ |
| 824 return SQLITE_MISUSE_BKPT; | 831 return SQLITE_MISUSE_BKPT; |
| 825 } | 832 } |
| 826 if( nBytes>=0 ){ | 833 if( nBytes>=0 ){ |
| 827 int sz; | 834 int sz; |
| 828 const char *z = (const char*)zSql; | 835 const char *z = (const char*)zSql; |
| 829 for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){} | 836 for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){} |
| 830 nBytes = sz; | 837 nBytes = sz; |
| 831 } | 838 } |
| 832 sqlite3_mutex_enter(db->mutex); | 839 sqlite3_mutex_enter(db->mutex); |
| 833 zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE); | 840 zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 877 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ | 884 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 878 const void **pzTail /* OUT: End of parsed string */ | 885 const void **pzTail /* OUT: End of parsed string */ |
| 879 ){ | 886 ){ |
| 880 int rc; | 887 int rc; |
| 881 rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail); | 888 rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail); |
| 882 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ | 889 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ |
| 883 return rc; | 890 return rc; |
| 884 } | 891 } |
| 885 | 892 |
| 886 #endif /* SQLITE_OMIT_UTF16 */ | 893 #endif /* SQLITE_OMIT_UTF16 */ |
| OLD | NEW |