danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2005 May 25 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains the implementation of the sqlite3_prepare() |
| 13 | ** interface, and routines that contribute to loading the database schema |
| 14 | ** from disk. |
| 15 | ** |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 16 | ** $Id: prepare.c,v 1.75 2008/01/23 03:03:05 drh Exp $ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 17 | */ |
| 18 | #include "sqliteInt.h" |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 19 | #include <ctype.h> |
| 20 | |
| 21 | /* |
| 22 | ** Fill the InitData structure with an error message that indicates |
| 23 | ** that the database is corrupt. |
| 24 | */ |
| 25 | static void corruptSchema(InitData *pData, const char *zExtra){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 26 | if( !pData->db->mallocFailed ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 27 | sqlite3SetString(pData->pzErrMsg, "malformed database schema", |
| 28 | zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0); |
| 29 | } |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 30 | pData->rc = SQLITE_CORRUPT; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | /* |
| 34 | ** This is the callback routine for the code that initializes the |
| 35 | ** database. See sqlite3Init() below for additional information. |
| 36 | ** This routine is also called from the OP_ParseSchema opcode of the VDBE. |
| 37 | ** |
| 38 | ** Each callback contains the following information: |
| 39 | ** |
| 40 | ** argv[0] = name of thing being created |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 41 | ** argv[1] = root page number for table or index. 0 for trigger or view. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 42 | ** argv[2] = SQL text for the CREATE statement. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 43 | ** |
| 44 | */ |
| 45 | int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){ |
| 46 | InitData *pData = (InitData*)pInit; |
| 47 | sqlite3 *db = pData->db; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 48 | int iDb = pData->iDb; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 49 | |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 50 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 51 | pData->rc = SQLITE_OK; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 52 | DbClearProperty(db, iDb, DB_Empty); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 53 | if( db->mallocFailed ){ |
drh | fe5a816 | 2006-08-12 13:28:23 +0000 | [diff] [blame] | 54 | corruptSchema(pData, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 55 | return SQLITE_NOMEM; |
| 56 | } |
| 57 | |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 58 | assert( argc==3 ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 59 | if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 60 | if( argv[1]==0 ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 61 | corruptSchema(pData, 0); |
| 62 | return 1; |
| 63 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 64 | assert( iDb>=0 && iDb<db->nDb ); |
| 65 | if( argv[2] && argv[2][0] ){ |
| 66 | /* Call the parser to process a CREATE TABLE, INDEX or VIEW. |
| 67 | ** But because db->init.busy is set to 1, no VDBE code is generated |
| 68 | ** or executed. All the parser does is build the internal data |
| 69 | ** structures that describe the table, index, or view. |
| 70 | */ |
| 71 | char *zErr; |
| 72 | int rc; |
| 73 | assert( db->init.busy ); |
| 74 | db->init.iDb = iDb; |
| 75 | db->init.newTnum = atoi(argv[1]); |
| 76 | rc = sqlite3_exec(db, argv[2], 0, 0, &zErr); |
| 77 | db->init.iDb = 0; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 78 | assert( rc!=SQLITE_OK || zErr==0 ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 79 | if( SQLITE_OK!=rc ){ |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 80 | pData->rc = rc; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 81 | if( rc==SQLITE_NOMEM ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 82 | db->mallocFailed = 1; |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 83 | }else if( rc!=SQLITE_INTERRUPT ){ |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 84 | corruptSchema(pData, zErr); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 85 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 86 | sqlite3_free(zErr); |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 87 | return 1; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 88 | } |
drh | 02cd2b8 | 2008-01-22 19:34:27 +0000 | [diff] [blame] | 89 | }else if( argv[0]==0 ){ |
drh | c046f40 | 2008-01-22 16:35:36 +0000 | [diff] [blame] | 90 | corruptSchema(pData, 0); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 91 | }else{ |
| 92 | /* If the SQL column is blank it means this is an index that |
| 93 | ** was created to be the PRIMARY KEY or to fulfill a UNIQUE |
| 94 | ** constraint for a CREATE TABLE. The index should have already |
| 95 | ** been created when we processed the CREATE TABLE. All we have |
| 96 | ** to do here is record the root page number for that index. |
| 97 | */ |
| 98 | Index *pIndex; |
| 99 | pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName); |
| 100 | if( pIndex==0 || pIndex->tnum!=0 ){ |
| 101 | /* This can occur if there exists an index on a TEMP table which |
| 102 | ** has the same name as another index on a permanent index. Since |
| 103 | ** the permanent table is hidden by the TEMP table, we can also |
| 104 | ** safely ignore the index on the permanent table. |
| 105 | */ |
| 106 | /* Do Nothing */; |
| 107 | }else{ |
| 108 | pIndex->tnum = atoi(argv[1]); |
| 109 | } |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | ** Attempt to read the database schema and initialize internal |
| 116 | ** data structures for a single database file. The index of the |
| 117 | ** database file is given by iDb. iDb==0 is used for the main |
| 118 | ** database. iDb==1 should never be used. iDb>=2 is used for |
| 119 | ** auxiliary databases. Return one of the SQLITE_ error codes to |
| 120 | ** indicate success or failure. |
| 121 | */ |
| 122 | static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ |
| 123 | int rc; |
| 124 | BtCursor *curMain; |
| 125 | int size; |
| 126 | Table *pTab; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 127 | Db *pDb; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 128 | char const *azArg[4]; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 129 | int meta[10]; |
| 130 | InitData initData; |
| 131 | char const *zMasterSchema; |
| 132 | char const *zMasterName = SCHEMA_TABLE(iDb); |
| 133 | |
| 134 | /* |
| 135 | ** The master database table has a structure like this |
| 136 | */ |
| 137 | static const char master_schema[] = |
| 138 | "CREATE TABLE sqlite_master(\n" |
| 139 | " type text,\n" |
| 140 | " name text,\n" |
| 141 | " tbl_name text,\n" |
| 142 | " rootpage integer,\n" |
| 143 | " sql text\n" |
| 144 | ")" |
| 145 | ; |
| 146 | #ifndef SQLITE_OMIT_TEMPDB |
| 147 | static const char temp_master_schema[] = |
| 148 | "CREATE TEMP TABLE sqlite_temp_master(\n" |
| 149 | " type text,\n" |
| 150 | " name text,\n" |
| 151 | " tbl_name text,\n" |
| 152 | " rootpage integer,\n" |
| 153 | " sql text\n" |
| 154 | ")" |
| 155 | ; |
| 156 | #else |
| 157 | #define temp_master_schema 0 |
| 158 | #endif |
| 159 | |
| 160 | assert( iDb>=0 && iDb<db->nDb ); |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 161 | assert( db->aDb[iDb].pSchema ); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 162 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | 4eab8b7 | 2007-12-27 15:12:16 +0000 | [diff] [blame] | 163 | assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 164 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 165 | /* zMasterSchema and zInitScript are set to point at the master schema |
| 166 | ** and initialisation script appropriate for the database being |
| 167 | ** initialised. zMasterName is the name of the master table. |
| 168 | */ |
| 169 | if( !OMIT_TEMPDB && iDb==1 ){ |
| 170 | zMasterSchema = temp_master_schema; |
| 171 | }else{ |
| 172 | zMasterSchema = master_schema; |
| 173 | } |
| 174 | zMasterName = SCHEMA_TABLE(iDb); |
| 175 | |
| 176 | /* Construct the schema tables. */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 177 | azArg[0] = zMasterName; |
| 178 | azArg[1] = "1"; |
| 179 | azArg[2] = zMasterSchema; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 180 | azArg[3] = 0; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 181 | initData.db = db; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 182 | initData.iDb = iDb; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 183 | initData.pzErrMsg = pzErrMsg; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 184 | (void)sqlite3SafetyOff(db); |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 185 | rc = sqlite3InitCallback(&initData, 3, (char **)azArg, 0); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 186 | (void)sqlite3SafetyOn(db); |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 187 | if( rc ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 188 | rc = initData.rc; |
| 189 | goto error_out; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 190 | } |
| 191 | pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName); |
| 192 | if( pTab ){ |
| 193 | pTab->readOnly = 1; |
| 194 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 195 | |
| 196 | /* Create a cursor to hold the database open |
| 197 | */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 198 | pDb = &db->aDb[iDb]; |
| 199 | if( pDb->pBt==0 ){ |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 200 | if( !OMIT_TEMPDB && iDb==1 ){ |
| 201 | DbSetProperty(db, 1, DB_SchemaLoaded); |
| 202 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 203 | return SQLITE_OK; |
| 204 | } |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 205 | sqlite3BtreeEnter(pDb->pBt); |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 206 | rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, 0, &curMain); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 207 | if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){ |
| 208 | sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 209 | sqlite3BtreeLeave(pDb->pBt); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 210 | goto error_out; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* Get the database meta information. |
| 214 | ** |
| 215 | ** Meta values are as follows: |
| 216 | ** meta[0] Schema cookie. Changes with each schema change. |
| 217 | ** meta[1] File format of schema layer. |
| 218 | ** meta[2] Size of the page cache. |
| 219 | ** meta[3] Use freelist if 0. Autovacuum if greater than zero. |
drh | 8159a35 | 2006-05-23 23:22:29 +0000 | [diff] [blame] | 220 | ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 221 | ** meta[5] The user cookie. Used by the application. |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 222 | ** meta[6] Incremental-vacuum flag. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 223 | ** meta[7] |
| 224 | ** meta[8] |
| 225 | ** meta[9] |
| 226 | ** |
drh | f248e21 | 2006-01-25 22:50:38 +0000 | [diff] [blame] | 227 | ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 228 | ** the possible values of meta[4]. |
| 229 | */ |
| 230 | if( rc==SQLITE_OK ){ |
| 231 | int i; |
| 232 | for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 233 | rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 234 | } |
| 235 | if( rc ){ |
| 236 | sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0); |
| 237 | sqlite3BtreeCloseCursor(curMain); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 238 | sqlite3BtreeLeave(pDb->pBt); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 239 | goto error_out; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 240 | } |
| 241 | }else{ |
| 242 | memset(meta, 0, sizeof(meta)); |
| 243 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 244 | pDb->pSchema->schema_cookie = meta[0]; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 245 | |
| 246 | /* If opening a non-empty database, check the text encoding. For the |
| 247 | ** main database, set sqlite3.enc to the encoding of the main database. |
| 248 | ** For an attached db, it is an error if the encoding is not the same |
| 249 | ** as sqlite3.enc. |
| 250 | */ |
| 251 | if( meta[4] ){ /* text encoding */ |
| 252 | if( iDb==0 ){ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 253 | /* If opening the main database, set ENC(db). */ |
| 254 | ENC(db) = (u8)meta[4]; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 255 | db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 256 | }else{ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 257 | /* If opening an attached database, the encoding much match ENC(db) */ |
| 258 | if( meta[4]!=ENC(db) ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 259 | sqlite3BtreeCloseCursor(curMain); |
| 260 | sqlite3SetString(pzErrMsg, "attached databases must use the same" |
| 261 | " text encoding as main database", (char*)0); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 262 | sqlite3BtreeLeave(pDb->pBt); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 263 | return SQLITE_ERROR; |
| 264 | } |
| 265 | } |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 266 | }else{ |
| 267 | DbSetProperty(db, iDb, DB_Empty); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 268 | } |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 269 | pDb->pSchema->enc = ENC(db); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 270 | |
| 271 | size = meta[2]; |
drh | c797d4d | 2007-05-08 01:08:49 +0000 | [diff] [blame] | 272 | if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; } |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 273 | if( size<0 ) size = -size; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 274 | pDb->pSchema->cache_size = size; |
| 275 | sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 276 | |
| 277 | /* |
| 278 | ** file_format==1 Version 3.0.0. |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 279 | ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN |
| 280 | ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 281 | ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 282 | */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 283 | pDb->pSchema->file_format = meta[1]; |
| 284 | if( pDb->pSchema->file_format==0 ){ |
| 285 | pDb->pSchema->file_format = 1; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 286 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 287 | if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 288 | sqlite3BtreeCloseCursor(curMain); |
| 289 | sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 290 | sqlite3BtreeLeave(pDb->pBt); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 291 | return SQLITE_ERROR; |
| 292 | } |
| 293 | |
drh | 4aa2bfe | 2007-11-28 13:43:16 +0000 | [diff] [blame] | 294 | /* Ticket #2804: When we open a database in the newer file format, |
| 295 | ** clear the legacy_file_format pragma flag so that a VACUUM will |
| 296 | ** not downgrade the database and thus invalidate any descending |
| 297 | ** indices that the user might have created. |
| 298 | */ |
| 299 | if( iDb==0 && meta[1]>=4 ){ |
| 300 | db->flags &= ~SQLITE_LegacyFileFmt; |
| 301 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 302 | |
| 303 | /* Read the schema information out of the schema tables |
| 304 | */ |
| 305 | assert( db->init.busy ); |
| 306 | if( rc==SQLITE_EMPTY ){ |
| 307 | /* For an empty database, there is nothing to read */ |
| 308 | rc = SQLITE_OK; |
| 309 | }else{ |
| 310 | char *zSql; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 311 | zSql = sqlite3MPrintf(db, |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 312 | "SELECT name, rootpage, sql FROM '%q'.%s", |
| 313 | db->aDb[iDb].zName, zMasterName); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 314 | (void)sqlite3SafetyOff(db); |
drh | a6d0ffc | 2007-10-12 20:42:28 +0000 | [diff] [blame] | 315 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 316 | { |
| 317 | int (*xAuth)(void*,int,const char*,const char*,const char*,const char*); |
| 318 | xAuth = db->xAuth; |
| 319 | db->xAuth = 0; |
| 320 | #endif |
| 321 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
| 322 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 323 | db->xAuth = xAuth; |
| 324 | } |
| 325 | #endif |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 326 | if( rc==SQLITE_ABORT ) rc = initData.rc; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 327 | (void)sqlite3SafetyOn(db); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 328 | sqlite3_free(zSql); |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 329 | #ifndef SQLITE_OMIT_ANALYZE |
| 330 | if( rc==SQLITE_OK ){ |
| 331 | sqlite3AnalysisLoad(db, iDb); |
| 332 | } |
| 333 | #endif |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 334 | sqlite3BtreeCloseCursor(curMain); |
| 335 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 336 | if( db->mallocFailed ){ |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 337 | /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 338 | rc = SQLITE_NOMEM; |
| 339 | sqlite3ResetInternalSchema(db, 0); |
| 340 | } |
danielk1977 | 34c68fb | 2007-03-14 15:37:04 +0000 | [diff] [blame] | 341 | if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){ |
| 342 | /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider |
| 343 | ** the schema loaded, even if errors occured. In this situation the |
| 344 | ** current sqlite3_prepare() operation will fail, but the following one |
| 345 | ** will attempt to compile the supplied statement against whatever subset |
| 346 | ** of the schema was loaded before the error occured. The primary |
| 347 | ** purpose of this is to allow access to the sqlite_master table |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 348 | ** even when its contents have been corrupted. |
danielk1977 | 34c68fb | 2007-03-14 15:37:04 +0000 | [diff] [blame] | 349 | */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 350 | DbSetProperty(db, iDb, DB_SchemaLoaded); |
danielk1977 | 34c68fb | 2007-03-14 15:37:04 +0000 | [diff] [blame] | 351 | rc = SQLITE_OK; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 352 | } |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 353 | sqlite3BtreeLeave(pDb->pBt); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 354 | |
| 355 | error_out: |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 356 | if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 357 | db->mallocFailed = 1; |
| 358 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 359 | return rc; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | ** Initialize all database files - the main database file, the file |
| 364 | ** used to store temporary tables, and any additional database files |
| 365 | ** created using ATTACH statements. Return a success code. If an |
| 366 | ** error occurs, write an error message into *pzErrMsg. |
| 367 | ** |
danielk1977 | e725929 | 2006-01-13 06:33:23 +0000 | [diff] [blame] | 368 | ** After a database is initialized, the DB_SchemaLoaded bit is set |
| 369 | ** bit is set in the flags field of the Db structure. If the database |
| 370 | ** file was of zero-length, then the DB_Empty flag is also set. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 371 | */ |
| 372 | int sqlite3Init(sqlite3 *db, char **pzErrMsg){ |
| 373 | int i, rc; |
danielk1977 | 5814c1a | 2007-08-13 14:41:19 +0000 | [diff] [blame] | 374 | int commit_internal = !(db->flags&SQLITE_InternChanges); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 375 | |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 376 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 377 | if( db->init.busy ) return SQLITE_OK; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 378 | rc = SQLITE_OK; |
| 379 | db->init.busy = 1; |
| 380 | for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
| 381 | if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue; |
| 382 | rc = sqlite3InitOne(db, i, pzErrMsg); |
| 383 | if( rc ){ |
| 384 | sqlite3ResetInternalSchema(db, i); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /* Once all the other databases have been initialised, load the schema |
| 389 | ** for the TEMP database. This is loaded last, as the TEMP database |
| 390 | ** schema may contain references to objects in other databases. |
| 391 | */ |
| 392 | #ifndef SQLITE_OMIT_TEMPDB |
| 393 | if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){ |
| 394 | rc = sqlite3InitOne(db, 1, pzErrMsg); |
| 395 | if( rc ){ |
| 396 | sqlite3ResetInternalSchema(db, 1); |
| 397 | } |
| 398 | } |
| 399 | #endif |
| 400 | |
| 401 | db->init.busy = 0; |
danielk1977 | 5814c1a | 2007-08-13 14:41:19 +0000 | [diff] [blame] | 402 | if( rc==SQLITE_OK && commit_internal ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 403 | sqlite3CommitInternalChanges(db); |
| 404 | } |
| 405 | |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 406 | return rc; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /* |
| 410 | ** This routine is a no-op if the database schema is already initialised. |
| 411 | ** Otherwise, the schema is loaded. An error code is returned. |
| 412 | */ |
| 413 | int sqlite3ReadSchema(Parse *pParse){ |
| 414 | int rc = SQLITE_OK; |
| 415 | sqlite3 *db = pParse->db; |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 416 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 417 | if( !db->init.busy ){ |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 418 | rc = sqlite3Init(db, &pParse->zErrMsg); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 419 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 420 | if( rc!=SQLITE_OK ){ |
| 421 | pParse->rc = rc; |
| 422 | pParse->nErr++; |
| 423 | } |
| 424 | return rc; |
| 425 | } |
| 426 | |
| 427 | |
| 428 | /* |
| 429 | ** Check schema cookies in all databases. If any cookie is out |
| 430 | ** of date, return 0. If all schema cookies are current, return 1. |
| 431 | */ |
| 432 | static int schemaIsValid(sqlite3 *db){ |
| 433 | int iDb; |
| 434 | int rc; |
| 435 | BtCursor *curTemp; |
| 436 | int cookie; |
| 437 | int allOk = 1; |
| 438 | |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 439 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 440 | for(iDb=0; allOk && iDb<db->nDb; iDb++){ |
| 441 | Btree *pBt; |
| 442 | pBt = db->aDb[iDb].pBt; |
| 443 | if( pBt==0 ) continue; |
| 444 | rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp); |
| 445 | if( rc==SQLITE_OK ){ |
| 446 | rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 447 | if( rc==SQLITE_OK && cookie!=db->aDb[iDb].pSchema->schema_cookie ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 448 | allOk = 0; |
| 449 | } |
| 450 | sqlite3BtreeCloseCursor(curTemp); |
| 451 | } |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 452 | if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 453 | db->mallocFailed = 1; |
| 454 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 455 | } |
| 456 | return allOk; |
| 457 | } |
| 458 | |
| 459 | /* |
drh | f248e21 | 2006-01-25 22:50:38 +0000 | [diff] [blame] | 460 | ** Convert a schema pointer into the iDb index that indicates |
| 461 | ** which database file in db->aDb[] the schema refers to. |
| 462 | ** |
| 463 | ** If the same database is attached more than once, the first |
| 464 | ** attached database is returned. |
| 465 | */ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 466 | int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){ |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 467 | int i = -1000000; |
| 468 | |
| 469 | /* If pSchema is NULL, then return -1000000. This happens when code in |
| 470 | ** expr.c is trying to resolve a reference to a transient table (i.e. one |
| 471 | ** created by a sub-select). In this case the return value of this |
| 472 | ** function should never be used. |
| 473 | ** |
| 474 | ** We return -1000000 instead of the more usual -1 simply because using |
| 475 | ** -1000000 as incorrectly using -1000000 index into db->aDb[] is much |
| 476 | ** more likely to cause a segfault than -1 (of course there are assert() |
| 477 | ** statements too, but it never hurts to play the odds). |
| 478 | */ |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 479 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 480 | if( pSchema ){ |
| 481 | for(i=0; i<db->nDb; i++){ |
| 482 | if( db->aDb[i].pSchema==pSchema ){ |
| 483 | break; |
| 484 | } |
| 485 | } |
| 486 | assert( i>=0 &&i>=0 && i<db->nDb ); |
| 487 | } |
| 488 | return i; |
| 489 | } |
| 490 | |
| 491 | /* |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 492 | ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. |
| 493 | */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 494 | int sqlite3Prepare( |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 495 | sqlite3 *db, /* Database handle. */ |
| 496 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 497 | int nBytes, /* Length of zSql in bytes. */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 498 | int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 499 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 500 | const char **pzTail /* OUT: End of parsed string */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 501 | ){ |
| 502 | Parse sParse; |
| 503 | char *zErrMsg = 0; |
| 504 | int rc = SQLITE_OK; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 505 | int i; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 506 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 507 | assert( ppStmt ); |
| 508 | *ppStmt = 0; |
| 509 | if( sqlite3SafetyOn(db) ){ |
| 510 | return SQLITE_MISUSE; |
| 511 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 512 | assert( !db->mallocFailed ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 513 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 514 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 515 | /* If any attached database schemas are locked, do not proceed with |
| 516 | ** compilation. Instead return SQLITE_LOCKED immediately. |
| 517 | */ |
| 518 | for(i=0; i<db->nDb; i++) { |
| 519 | Btree *pBt = db->aDb[i].pBt; |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 520 | if( pBt ){ |
| 521 | int rc; |
| 522 | rc = sqlite3BtreeSchemaLocked(pBt); |
| 523 | if( rc ){ |
| 524 | const char *zDb = db->aDb[i].zName; |
| 525 | sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 526 | (void)sqlite3SafetyOff(db); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 527 | return SQLITE_LOCKED; |
| 528 | } |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 532 | memset(&sParse, 0, sizeof(sParse)); |
| 533 | sParse.db = db; |
drh | 9051a42 | 2006-01-30 22:35:43 +0000 | [diff] [blame] | 534 | if( nBytes>=0 && zSql[nBytes]!=0 ){ |
drh | e5c941b | 2007-05-08 13:58:26 +0000 | [diff] [blame] | 535 | char *zSqlCopy; |
drh | be69bf6 | 2007-12-18 17:50:33 +0000 | [diff] [blame] | 536 | if( SQLITE_MAX_SQL_LENGTH>0 && nBytes>SQLITE_MAX_SQL_LENGTH ){ |
| 537 | sqlite3Error(db, SQLITE_TOOBIG, "statement too long"); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 538 | (void)sqlite3SafetyOff(db); |
drh | e5c941b | 2007-05-08 13:58:26 +0000 | [diff] [blame] | 539 | return SQLITE_TOOBIG; |
| 540 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 541 | zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes); |
drh | 276fdbf | 2007-04-30 21:39:16 +0000 | [diff] [blame] | 542 | if( zSqlCopy ){ |
| 543 | sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 544 | sqlite3_free(zSqlCopy); |
drh | 276fdbf | 2007-04-30 21:39:16 +0000 | [diff] [blame] | 545 | } |
| 546 | sParse.zTail = &zSql[nBytes]; |
drh | 9051a42 | 2006-01-30 22:35:43 +0000 | [diff] [blame] | 547 | }else{ |
| 548 | sqlite3RunParser(&sParse, zSql, &zErrMsg); |
| 549 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 550 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 551 | if( db->mallocFailed ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 552 | sParse.rc = SQLITE_NOMEM; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 553 | } |
| 554 | if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK; |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 555 | if( sParse.checkSchema && !schemaIsValid(db) ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 556 | sParse.rc = SQLITE_SCHEMA; |
| 557 | } |
| 558 | if( sParse.rc==SQLITE_SCHEMA ){ |
| 559 | sqlite3ResetInternalSchema(db, 0); |
| 560 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 561 | if( db->mallocFailed ){ |
drh | 344a627 | 2006-06-26 12:50:09 +0000 | [diff] [blame] | 562 | sParse.rc = SQLITE_NOMEM; |
| 563 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 564 | if( pzTail ){ |
| 565 | *pzTail = sParse.zTail; |
| 566 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 567 | rc = sParse.rc; |
| 568 | |
| 569 | #ifndef SQLITE_OMIT_EXPLAIN |
| 570 | if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){ |
drh | ecc9242 | 2005-09-10 16:46:12 +0000 | [diff] [blame] | 571 | if( sParse.explain==2 ){ |
| 572 | sqlite3VdbeSetNumCols(sParse.pVdbe, 3); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 573 | sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "order", P4_STATIC); |
| 574 | sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "from", P4_STATIC); |
| 575 | sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "detail", P4_STATIC); |
drh | ecc9242 | 2005-09-10 16:46:12 +0000 | [diff] [blame] | 576 | }else{ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 577 | sqlite3VdbeSetNumCols(sParse.pVdbe, 8); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 578 | sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "addr", P4_STATIC); |
| 579 | sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "opcode", P4_STATIC); |
| 580 | sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "p1", P4_STATIC); |
| 581 | sqlite3VdbeSetColName(sParse.pVdbe, 3, COLNAME_NAME, "p2", P4_STATIC); |
| 582 | sqlite3VdbeSetColName(sParse.pVdbe, 4, COLNAME_NAME, "p3", P4_STATIC); |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 583 | sqlite3VdbeSetColName(sParse.pVdbe, 5, COLNAME_NAME, "p4", P4_STATIC); |
| 584 | sqlite3VdbeSetColName(sParse.pVdbe, 6, COLNAME_NAME, "p5", P4_STATIC); |
| 585 | sqlite3VdbeSetColName(sParse.pVdbe, 7, COLNAME_NAME, "comment",P4_STATIC); |
drh | ecc9242 | 2005-09-10 16:46:12 +0000 | [diff] [blame] | 586 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 587 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 588 | #endif |
| 589 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 590 | if( sqlite3SafetyOff(db) ){ |
| 591 | rc = SQLITE_MISUSE; |
| 592 | } |
danielk1977 | 7e29e95 | 2007-04-19 11:09:01 +0000 | [diff] [blame] | 593 | |
| 594 | if( saveSqlFlag ){ |
| 595 | sqlite3VdbeSetSql(sParse.pVdbe, zSql, sParse.zTail - zSql); |
| 596 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 597 | if( rc!=SQLITE_OK || db->mallocFailed ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 598 | sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe); |
danielk1977 | 7e29e95 | 2007-04-19 11:09:01 +0000 | [diff] [blame] | 599 | assert(!(*ppStmt)); |
| 600 | }else{ |
| 601 | *ppStmt = (sqlite3_stmt*)sParse.pVdbe; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | if( zErrMsg ){ |
| 605 | sqlite3Error(db, rc, "%s", zErrMsg); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 606 | sqlite3_free(zErrMsg); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 607 | }else{ |
| 608 | sqlite3Error(db, rc, 0); |
| 609 | } |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 610 | |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 611 | rc = sqlite3ApiExit(db, rc); |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 612 | assert( (rc&db->errMask)==rc ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 613 | return rc; |
| 614 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 615 | static int sqlite3LockAndPrepare( |
| 616 | sqlite3 *db, /* Database handle. */ |
| 617 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 618 | int nBytes, /* Length of zSql in bytes. */ |
| 619 | int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ |
| 620 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 621 | const char **pzTail /* OUT: End of parsed string */ |
| 622 | ){ |
| 623 | int rc; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 624 | if( !sqlite3SafetyCheckOk(db) ){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 625 | return SQLITE_MISUSE; |
| 626 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 627 | sqlite3_mutex_enter(db->mutex); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 628 | sqlite3BtreeEnterAll(db); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 629 | rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, ppStmt, pzTail); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 630 | sqlite3BtreeLeaveAll(db); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 631 | sqlite3_mutex_leave(db->mutex); |
| 632 | return rc; |
| 633 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 634 | |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 635 | /* |
| 636 | ** Rerun the compilation of a statement after a schema change. |
| 637 | ** Return true if the statement was recompiled successfully. |
| 638 | ** Return false if there is an error of some kind. |
| 639 | */ |
| 640 | int sqlite3Reprepare(Vdbe *p){ |
| 641 | int rc; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 642 | sqlite3_stmt *pNew; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 643 | const char *zSql; |
| 644 | sqlite3 *db; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 645 | |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 646 | assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) ); |
danielk1977 | d0e2a85 | 2007-11-14 06:48:48 +0000 | [diff] [blame] | 647 | zSql = sqlite3_sql((sqlite3_stmt *)p); |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 648 | assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 649 | db = sqlite3VdbeDb(p); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 650 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 651 | rc = sqlite3LockAndPrepare(db, zSql, -1, 0, &pNew, 0); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 652 | if( rc ){ |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 653 | if( rc==SQLITE_NOMEM ){ |
| 654 | db->mallocFailed = 1; |
| 655 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 656 | assert( pNew==0 ); |
| 657 | return 0; |
| 658 | }else{ |
| 659 | assert( pNew!=0 ); |
| 660 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 661 | sqlite3VdbeSwap((Vdbe*)pNew, p); |
| 662 | sqlite3_transfer_bindings(pNew, (sqlite3_stmt*)p); |
| 663 | sqlite3VdbeResetStepResult((Vdbe*)pNew); |
| 664 | sqlite3VdbeFinalize((Vdbe*)pNew); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 665 | return 1; |
| 666 | } |
| 667 | |
| 668 | |
| 669 | /* |
| 670 | ** Two versions of the official API. Legacy and new use. In the legacy |
| 671 | ** version, the original SQL text is not saved in the prepared statement |
| 672 | ** and so if a schema change occurs, SQLITE_SCHEMA is returned by |
| 673 | ** sqlite3_step(). In the new version, the original SQL text is retained |
| 674 | ** and the statement is automatically recompiled if an schema change |
| 675 | ** occurs. |
| 676 | */ |
| 677 | int sqlite3_prepare( |
| 678 | sqlite3 *db, /* Database handle. */ |
| 679 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 680 | int nBytes, /* Length of zSql in bytes. */ |
| 681 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 682 | const char **pzTail /* OUT: End of parsed string */ |
| 683 | ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 684 | return sqlite3LockAndPrepare(db,zSql,nBytes,0,ppStmt,pzTail); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 685 | } |
| 686 | int sqlite3_prepare_v2( |
| 687 | sqlite3 *db, /* Database handle. */ |
| 688 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 689 | int nBytes, /* Length of zSql in bytes. */ |
| 690 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 691 | const char **pzTail /* OUT: End of parsed string */ |
| 692 | ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 693 | return sqlite3LockAndPrepare(db,zSql,nBytes,1,ppStmt,pzTail); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 697 | #ifndef SQLITE_OMIT_UTF16 |
| 698 | /* |
| 699 | ** Compile the UTF-16 encoded SQL statement zSql into a statement handle. |
| 700 | */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 701 | static int sqlite3Prepare16( |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 702 | sqlite3 *db, /* Database handle. */ |
| 703 | const void *zSql, /* UTF-8 encoded SQL statement. */ |
| 704 | int nBytes, /* Length of zSql in bytes. */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 705 | int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 706 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 707 | const void **pzTail /* OUT: End of parsed string */ |
| 708 | ){ |
| 709 | /* This function currently works by first transforming the UTF-16 |
| 710 | ** encoded string to UTF-8, then invoking sqlite3_prepare(). The |
| 711 | ** tricky bit is figuring out the pointer to return in *pzTail. |
| 712 | */ |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 713 | char *zSql8; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 714 | const char *zTail8 = 0; |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 715 | int rc = SQLITE_OK; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 716 | |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 717 | if( !sqlite3SafetyCheckOk(db) ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 718 | return SQLITE_MISUSE; |
| 719 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 720 | sqlite3_mutex_enter(db->mutex); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 721 | zSql8 = sqlite3Utf16to8(db, zSql, nBytes); |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 722 | if( zSql8 ){ |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 723 | rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, ppStmt, &zTail8); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 724 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 725 | |
| 726 | if( zTail8 && pzTail ){ |
| 727 | /* If sqlite3_prepare returns a tail pointer, we calculate the |
| 728 | ** equivalent pointer into the UTF-16 string by counting the unicode |
| 729 | ** characters between zSql8 and zTail8, and then returning a pointer |
| 730 | ** the same number of characters into the UTF-16 string. |
| 731 | */ |
drh | ee85813 | 2007-05-08 20:37:38 +0000 | [diff] [blame] | 732 | int chars_parsed = sqlite3Utf8CharLen(zSql8, zTail8-zSql8); |
| 733 | *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 734 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 735 | sqlite3_free(zSql8); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 736 | rc = sqlite3ApiExit(db, rc); |
| 737 | sqlite3_mutex_leave(db->mutex); |
| 738 | return rc; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 739 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 740 | |
| 741 | /* |
| 742 | ** Two versions of the official API. Legacy and new use. In the legacy |
| 743 | ** version, the original SQL text is not saved in the prepared statement |
| 744 | ** and so if a schema change occurs, SQLITE_SCHEMA is returned by |
| 745 | ** sqlite3_step(). In the new version, the original SQL text is retained |
| 746 | ** and the statement is automatically recompiled if an schema change |
| 747 | ** occurs. |
| 748 | */ |
| 749 | int sqlite3_prepare16( |
| 750 | sqlite3 *db, /* Database handle. */ |
| 751 | const void *zSql, /* UTF-8 encoded SQL statement. */ |
| 752 | int nBytes, /* Length of zSql in bytes. */ |
| 753 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 754 | const void **pzTail /* OUT: End of parsed string */ |
| 755 | ){ |
| 756 | return sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail); |
| 757 | } |
| 758 | int sqlite3_prepare16_v2( |
| 759 | sqlite3 *db, /* Database handle. */ |
| 760 | const void *zSql, /* UTF-8 encoded SQL statement. */ |
| 761 | int nBytes, /* Length of zSql in bytes. */ |
| 762 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 763 | const void **pzTail /* OUT: End of parsed string */ |
| 764 | ){ |
| 765 | return sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail); |
| 766 | } |
| 767 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 768 | #endif /* SQLITE_OMIT_UTF16 */ |