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 | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame^] | 16 | ** $Id: prepare.c,v 1.37 2006/07/26 13:43:31 drh Exp $ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 17 | */ |
| 18 | #include "sqliteInt.h" |
| 19 | #include "os.h" |
| 20 | #include <ctype.h> |
| 21 | |
| 22 | /* |
| 23 | ** Fill the InitData structure with an error message that indicates |
| 24 | ** that the database is corrupt. |
| 25 | */ |
| 26 | static void corruptSchema(InitData *pData, const char *zExtra){ |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 27 | if( !sqlite3MallocFailed() ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 28 | sqlite3SetString(pData->pzErrMsg, "malformed database schema", |
| 29 | zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0); |
| 30 | } |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame^] | 31 | pData->rc = SQLITE_CORRUPT; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /* |
| 35 | ** This is the callback routine for the code that initializes the |
| 36 | ** database. See sqlite3Init() below for additional information. |
| 37 | ** This routine is also called from the OP_ParseSchema opcode of the VDBE. |
| 38 | ** |
| 39 | ** Each callback contains the following information: |
| 40 | ** |
| 41 | ** argv[0] = name of thing being created |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 42 | ** 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] | 43 | ** argv[2] = SQL text for the CREATE statement. |
| 44 | ** argv[3] = "1" for temporary files, "0" for main database, "2" or more |
| 45 | ** for auxiliary database files. |
| 46 | ** |
| 47 | */ |
| 48 | int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){ |
| 49 | InitData *pData = (InitData*)pInit; |
| 50 | sqlite3 *db = pData->db; |
| 51 | int iDb; |
| 52 | |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame^] | 53 | pData->rc = SQLITE_OK; |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 54 | if( sqlite3MallocFailed() ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 55 | return SQLITE_NOMEM; |
| 56 | } |
| 57 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 58 | assert( argc==4 ); |
| 59 | if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ |
| 60 | if( argv[1]==0 || argv[3]==0 ){ |
| 61 | corruptSchema(pData, 0); |
| 62 | return 1; |
| 63 | } |
| 64 | iDb = atoi(argv[3]); |
| 65 | assert( iDb>=0 && iDb<db->nDb ); |
| 66 | if( argv[2] && argv[2][0] ){ |
| 67 | /* Call the parser to process a CREATE TABLE, INDEX or VIEW. |
| 68 | ** But because db->init.busy is set to 1, no VDBE code is generated |
| 69 | ** or executed. All the parser does is build the internal data |
| 70 | ** structures that describe the table, index, or view. |
| 71 | */ |
| 72 | char *zErr; |
| 73 | int rc; |
| 74 | assert( db->init.busy ); |
| 75 | db->init.iDb = iDb; |
| 76 | db->init.newTnum = atoi(argv[1]); |
| 77 | rc = sqlite3_exec(db, argv[2], 0, 0, &zErr); |
| 78 | db->init.iDb = 0; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 79 | assert( rc!=SQLITE_OK || zErr==0 ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 80 | if( SQLITE_OK!=rc ){ |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame^] | 81 | pData->rc = rc; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 82 | if( rc==SQLITE_NOMEM ){ |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 83 | sqlite3FailedMalloc(); |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame^] | 84 | }else if( rc!=SQLITE_INTERRUPT ){ |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 85 | corruptSchema(pData, zErr); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 86 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 87 | sqlite3_free(zErr); |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame^] | 88 | return 1; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 89 | } |
| 90 | }else{ |
| 91 | /* If the SQL column is blank it means this is an index that |
| 92 | ** was created to be the PRIMARY KEY or to fulfill a UNIQUE |
| 93 | ** constraint for a CREATE TABLE. The index should have already |
| 94 | ** been created when we processed the CREATE TABLE. All we have |
| 95 | ** to do here is record the root page number for that index. |
| 96 | */ |
| 97 | Index *pIndex; |
| 98 | pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName); |
| 99 | if( pIndex==0 || pIndex->tnum!=0 ){ |
| 100 | /* This can occur if there exists an index on a TEMP table which |
| 101 | ** has the same name as another index on a permanent index. Since |
| 102 | ** the permanent table is hidden by the TEMP table, we can also |
| 103 | ** safely ignore the index on the permanent table. |
| 104 | */ |
| 105 | /* Do Nothing */; |
| 106 | }else{ |
| 107 | pIndex->tnum = atoi(argv[1]); |
| 108 | } |
| 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | ** Attempt to read the database schema and initialize internal |
| 115 | ** data structures for a single database file. The index of the |
| 116 | ** database file is given by iDb. iDb==0 is used for the main |
| 117 | ** database. iDb==1 should never be used. iDb>=2 is used for |
| 118 | ** auxiliary databases. Return one of the SQLITE_ error codes to |
| 119 | ** indicate success or failure. |
| 120 | */ |
| 121 | static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ |
| 122 | int rc; |
| 123 | BtCursor *curMain; |
| 124 | int size; |
| 125 | Table *pTab; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 126 | Db *pDb; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 127 | char const *azArg[5]; |
| 128 | char zDbNum[30]; |
| 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 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 162 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 163 | /* zMasterSchema and zInitScript are set to point at the master schema |
| 164 | ** and initialisation script appropriate for the database being |
| 165 | ** initialised. zMasterName is the name of the master table. |
| 166 | */ |
| 167 | if( !OMIT_TEMPDB && iDb==1 ){ |
| 168 | zMasterSchema = temp_master_schema; |
| 169 | }else{ |
| 170 | zMasterSchema = master_schema; |
| 171 | } |
| 172 | zMasterName = SCHEMA_TABLE(iDb); |
| 173 | |
| 174 | /* Construct the schema tables. */ |
| 175 | sqlite3SafetyOff(db); |
| 176 | azArg[0] = zMasterName; |
| 177 | azArg[1] = "1"; |
| 178 | azArg[2] = zMasterSchema; |
| 179 | sprintf(zDbNum, "%d", iDb); |
| 180 | azArg[3] = zDbNum; |
| 181 | azArg[4] = 0; |
| 182 | initData.db = db; |
| 183 | initData.pzErrMsg = pzErrMsg; |
| 184 | rc = sqlite3InitCallback(&initData, 4, (char **)azArg, 0); |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame^] | 185 | if( rc ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 186 | sqlite3SafetyOn(db); |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame^] | 187 | return initData.rc; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 188 | } |
| 189 | pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName); |
| 190 | if( pTab ){ |
| 191 | pTab->readOnly = 1; |
| 192 | } |
| 193 | sqlite3SafetyOn(db); |
| 194 | |
| 195 | /* Create a cursor to hold the database open |
| 196 | */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 197 | pDb = &db->aDb[iDb]; |
| 198 | if( pDb->pBt==0 ){ |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 199 | if( !OMIT_TEMPDB && iDb==1 ){ |
| 200 | DbSetProperty(db, 1, DB_SchemaLoaded); |
| 201 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 202 | return SQLITE_OK; |
| 203 | } |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 204 | rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, 0, &curMain); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 205 | if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){ |
| 206 | sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0); |
| 207 | return rc; |
| 208 | } |
| 209 | |
| 210 | /* Get the database meta information. |
| 211 | ** |
| 212 | ** Meta values are as follows: |
| 213 | ** meta[0] Schema cookie. Changes with each schema change. |
| 214 | ** meta[1] File format of schema layer. |
| 215 | ** meta[2] Size of the page cache. |
| 216 | ** meta[3] Use freelist if 0. Autovacuum if greater than zero. |
drh | 8159a35 | 2006-05-23 23:22:29 +0000 | [diff] [blame] | 217 | ** 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] | 218 | ** meta[5] The user cookie. Used by the application. |
| 219 | ** meta[6] |
| 220 | ** meta[7] |
| 221 | ** meta[8] |
| 222 | ** meta[9] |
| 223 | ** |
drh | f248e21 | 2006-01-25 22:50:38 +0000 | [diff] [blame] | 224 | ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 225 | ** the possible values of meta[4]. |
| 226 | */ |
| 227 | if( rc==SQLITE_OK ){ |
| 228 | int i; |
| 229 | for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 230 | rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 231 | } |
| 232 | if( rc ){ |
| 233 | sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0); |
| 234 | sqlite3BtreeCloseCursor(curMain); |
| 235 | return rc; |
| 236 | } |
| 237 | }else{ |
| 238 | memset(meta, 0, sizeof(meta)); |
| 239 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 240 | pDb->pSchema->schema_cookie = meta[0]; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 241 | |
| 242 | /* If opening a non-empty database, check the text encoding. For the |
| 243 | ** main database, set sqlite3.enc to the encoding of the main database. |
| 244 | ** For an attached db, it is an error if the encoding is not the same |
| 245 | ** as sqlite3.enc. |
| 246 | */ |
| 247 | if( meta[4] ){ /* text encoding */ |
| 248 | if( iDb==0 ){ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 249 | /* If opening the main database, set ENC(db). */ |
| 250 | ENC(db) = (u8)meta[4]; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 251 | db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 252 | }else{ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 253 | /* If opening an attached database, the encoding much match ENC(db) */ |
| 254 | if( meta[4]!=ENC(db) ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 255 | sqlite3BtreeCloseCursor(curMain); |
| 256 | sqlite3SetString(pzErrMsg, "attached databases must use the same" |
| 257 | " text encoding as main database", (char*)0); |
| 258 | return SQLITE_ERROR; |
| 259 | } |
| 260 | } |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 261 | }else{ |
| 262 | DbSetProperty(db, iDb, DB_Empty); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 263 | } |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 264 | pDb->pSchema->enc = ENC(db); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 265 | |
| 266 | size = meta[2]; |
| 267 | if( size==0 ){ size = MAX_PAGES; } |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 268 | pDb->pSchema->cache_size = size; |
| 269 | sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 270 | |
| 271 | /* |
| 272 | ** file_format==1 Version 3.0.0. |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 273 | ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN |
| 274 | ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 275 | ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 276 | */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 277 | pDb->pSchema->file_format = meta[1]; |
| 278 | if( pDb->pSchema->file_format==0 ){ |
| 279 | pDb->pSchema->file_format = 1; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 280 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 281 | if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 282 | sqlite3BtreeCloseCursor(curMain); |
| 283 | sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0); |
| 284 | return SQLITE_ERROR; |
| 285 | } |
| 286 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 287 | |
| 288 | /* Read the schema information out of the schema tables |
| 289 | */ |
| 290 | assert( db->init.busy ); |
| 291 | if( rc==SQLITE_EMPTY ){ |
| 292 | /* For an empty database, there is nothing to read */ |
| 293 | rc = SQLITE_OK; |
| 294 | }else{ |
| 295 | char *zSql; |
| 296 | zSql = sqlite3MPrintf( |
| 297 | "SELECT name, rootpage, sql, '%s' FROM '%q'.%s", |
| 298 | zDbNum, db->aDb[iDb].zName, zMasterName); |
| 299 | sqlite3SafetyOff(db); |
| 300 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame^] | 301 | if( rc==SQLITE_ABORT ) rc = initData.rc; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 302 | sqlite3SafetyOn(db); |
| 303 | sqliteFree(zSql); |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 304 | #ifndef SQLITE_OMIT_ANALYZE |
| 305 | if( rc==SQLITE_OK ){ |
| 306 | sqlite3AnalysisLoad(db, iDb); |
| 307 | } |
| 308 | #endif |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 309 | sqlite3BtreeCloseCursor(curMain); |
| 310 | } |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 311 | if( sqlite3MallocFailed() ){ |
| 312 | /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 313 | rc = SQLITE_NOMEM; |
| 314 | sqlite3ResetInternalSchema(db, 0); |
| 315 | } |
| 316 | if( rc==SQLITE_OK ){ |
| 317 | DbSetProperty(db, iDb, DB_SchemaLoaded); |
| 318 | }else{ |
| 319 | sqlite3ResetInternalSchema(db, iDb); |
| 320 | } |
| 321 | return rc; |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | ** Initialize all database files - the main database file, the file |
| 326 | ** used to store temporary tables, and any additional database files |
| 327 | ** created using ATTACH statements. Return a success code. If an |
| 328 | ** error occurs, write an error message into *pzErrMsg. |
| 329 | ** |
danielk1977 | e725929 | 2006-01-13 06:33:23 +0000 | [diff] [blame] | 330 | ** After a database is initialized, the DB_SchemaLoaded bit is set |
| 331 | ** bit is set in the flags field of the Db structure. If the database |
| 332 | ** file was of zero-length, then the DB_Empty flag is also set. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 333 | */ |
| 334 | int sqlite3Init(sqlite3 *db, char **pzErrMsg){ |
| 335 | int i, rc; |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 336 | int called_initone = 0; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 337 | |
| 338 | if( db->init.busy ) return SQLITE_OK; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 339 | rc = SQLITE_OK; |
| 340 | db->init.busy = 1; |
| 341 | for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
| 342 | if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue; |
| 343 | rc = sqlite3InitOne(db, i, pzErrMsg); |
| 344 | if( rc ){ |
| 345 | sqlite3ResetInternalSchema(db, i); |
| 346 | } |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 347 | called_initone = 1; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /* Once all the other databases have been initialised, load the schema |
| 351 | ** for the TEMP database. This is loaded last, as the TEMP database |
| 352 | ** schema may contain references to objects in other databases. |
| 353 | */ |
| 354 | #ifndef SQLITE_OMIT_TEMPDB |
| 355 | if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){ |
| 356 | rc = sqlite3InitOne(db, 1, pzErrMsg); |
| 357 | if( rc ){ |
| 358 | sqlite3ResetInternalSchema(db, 1); |
| 359 | } |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 360 | called_initone = 1; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 361 | } |
| 362 | #endif |
| 363 | |
| 364 | db->init.busy = 0; |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 365 | if( rc==SQLITE_OK && called_initone ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 366 | sqlite3CommitInternalChanges(db); |
| 367 | } |
| 368 | |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 369 | return rc; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* |
| 373 | ** This routine is a no-op if the database schema is already initialised. |
| 374 | ** Otherwise, the schema is loaded. An error code is returned. |
| 375 | */ |
| 376 | int sqlite3ReadSchema(Parse *pParse){ |
| 377 | int rc = SQLITE_OK; |
| 378 | sqlite3 *db = pParse->db; |
| 379 | if( !db->init.busy ){ |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 380 | rc = sqlite3Init(db, &pParse->zErrMsg); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 381 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 382 | if( rc!=SQLITE_OK ){ |
| 383 | pParse->rc = rc; |
| 384 | pParse->nErr++; |
| 385 | } |
| 386 | return rc; |
| 387 | } |
| 388 | |
| 389 | |
| 390 | /* |
| 391 | ** Check schema cookies in all databases. If any cookie is out |
| 392 | ** of date, return 0. If all schema cookies are current, return 1. |
| 393 | */ |
| 394 | static int schemaIsValid(sqlite3 *db){ |
| 395 | int iDb; |
| 396 | int rc; |
| 397 | BtCursor *curTemp; |
| 398 | int cookie; |
| 399 | int allOk = 1; |
| 400 | |
| 401 | for(iDb=0; allOk && iDb<db->nDb; iDb++){ |
| 402 | Btree *pBt; |
| 403 | pBt = db->aDb[iDb].pBt; |
| 404 | if( pBt==0 ) continue; |
| 405 | rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp); |
| 406 | if( rc==SQLITE_OK ){ |
| 407 | rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 408 | if( rc==SQLITE_OK && cookie!=db->aDb[iDb].pSchema->schema_cookie ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 409 | allOk = 0; |
| 410 | } |
| 411 | sqlite3BtreeCloseCursor(curTemp); |
| 412 | } |
| 413 | } |
| 414 | return allOk; |
| 415 | } |
| 416 | |
| 417 | /* |
drh | f248e21 | 2006-01-25 22:50:38 +0000 | [diff] [blame] | 418 | ** Convert a schema pointer into the iDb index that indicates |
| 419 | ** which database file in db->aDb[] the schema refers to. |
| 420 | ** |
| 421 | ** If the same database is attached more than once, the first |
| 422 | ** attached database is returned. |
| 423 | */ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 424 | int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){ |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 425 | int i = -1000000; |
| 426 | |
| 427 | /* If pSchema is NULL, then return -1000000. This happens when code in |
| 428 | ** expr.c is trying to resolve a reference to a transient table (i.e. one |
| 429 | ** created by a sub-select). In this case the return value of this |
| 430 | ** function should never be used. |
| 431 | ** |
| 432 | ** We return -1000000 instead of the more usual -1 simply because using |
| 433 | ** -1000000 as incorrectly using -1000000 index into db->aDb[] is much |
| 434 | ** more likely to cause a segfault than -1 (of course there are assert() |
| 435 | ** statements too, but it never hurts to play the odds). |
| 436 | */ |
| 437 | if( pSchema ){ |
| 438 | for(i=0; i<db->nDb; i++){ |
| 439 | if( db->aDb[i].pSchema==pSchema ){ |
| 440 | break; |
| 441 | } |
| 442 | } |
| 443 | assert( i>=0 &&i>=0 && i<db->nDb ); |
| 444 | } |
| 445 | return i; |
| 446 | } |
| 447 | |
| 448 | /* |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 449 | ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. |
| 450 | */ |
| 451 | int sqlite3_prepare( |
| 452 | sqlite3 *db, /* Database handle. */ |
| 453 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 454 | int nBytes, /* Length of zSql in bytes. */ |
| 455 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 456 | const char** pzTail /* OUT: End of parsed string */ |
| 457 | ){ |
| 458 | Parse sParse; |
| 459 | char *zErrMsg = 0; |
| 460 | int rc = SQLITE_OK; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 461 | int i; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 462 | |
danielk1977 | efaaf57 | 2006-01-16 11:29:19 +0000 | [diff] [blame] | 463 | /* Assert that malloc() has not failed */ |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 464 | assert( !sqlite3MallocFailed() ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 465 | |
| 466 | assert( ppStmt ); |
| 467 | *ppStmt = 0; |
| 468 | if( sqlite3SafetyOn(db) ){ |
| 469 | return SQLITE_MISUSE; |
| 470 | } |
| 471 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 472 | /* If any attached database schemas are locked, do not proceed with |
| 473 | ** compilation. Instead return SQLITE_LOCKED immediately. |
| 474 | */ |
| 475 | for(i=0; i<db->nDb; i++) { |
| 476 | Btree *pBt = db->aDb[i].pBt; |
| 477 | if( pBt && sqlite3BtreeSchemaLocked(pBt) ){ |
| 478 | const char *zDb = db->aDb[i].zName; |
| 479 | sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb); |
| 480 | sqlite3SafetyOff(db); |
| 481 | return SQLITE_LOCKED; |
| 482 | } |
| 483 | } |
| 484 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 485 | memset(&sParse, 0, sizeof(sParse)); |
| 486 | sParse.db = db; |
drh | 9051a42 | 2006-01-30 22:35:43 +0000 | [diff] [blame] | 487 | if( nBytes>=0 && zSql[nBytes]!=0 ){ |
| 488 | char *zSqlCopy = sqlite3StrNDup(zSql, nBytes); |
| 489 | sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg); |
| 490 | sParse.zTail += zSql - zSqlCopy; |
| 491 | sqliteFree(zSqlCopy); |
| 492 | }else{ |
| 493 | sqlite3RunParser(&sParse, zSql, &zErrMsg); |
| 494 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 495 | |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 496 | if( sqlite3MallocFailed() ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 497 | sParse.rc = SQLITE_NOMEM; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 498 | } |
| 499 | if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK; |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 500 | if( sParse.checkSchema && !schemaIsValid(db) ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 501 | sParse.rc = SQLITE_SCHEMA; |
| 502 | } |
| 503 | if( sParse.rc==SQLITE_SCHEMA ){ |
| 504 | sqlite3ResetInternalSchema(db, 0); |
| 505 | } |
drh | 344a627 | 2006-06-26 12:50:09 +0000 | [diff] [blame] | 506 | if( sqlite3MallocFailed() ){ |
| 507 | sParse.rc = SQLITE_NOMEM; |
| 508 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 509 | if( pzTail ) *pzTail = sParse.zTail; |
| 510 | rc = sParse.rc; |
| 511 | |
| 512 | #ifndef SQLITE_OMIT_EXPLAIN |
| 513 | if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){ |
drh | ecc9242 | 2005-09-10 16:46:12 +0000 | [diff] [blame] | 514 | if( sParse.explain==2 ){ |
| 515 | sqlite3VdbeSetNumCols(sParse.pVdbe, 3); |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 516 | sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "order", P3_STATIC); |
| 517 | sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "from", P3_STATIC); |
| 518 | sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "detail", P3_STATIC); |
drh | ecc9242 | 2005-09-10 16:46:12 +0000 | [diff] [blame] | 519 | }else{ |
| 520 | sqlite3VdbeSetNumCols(sParse.pVdbe, 5); |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 521 | sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "addr", P3_STATIC); |
| 522 | sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "opcode", P3_STATIC); |
| 523 | sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "p1", P3_STATIC); |
| 524 | sqlite3VdbeSetColName(sParse.pVdbe, 3, COLNAME_NAME, "p2", P3_STATIC); |
| 525 | sqlite3VdbeSetColName(sParse.pVdbe, 4, COLNAME_NAME, "p3", P3_STATIC); |
drh | ecc9242 | 2005-09-10 16:46:12 +0000 | [diff] [blame] | 526 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 527 | } |
| 528 | #endif |
| 529 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 530 | if( sqlite3SafetyOff(db) ){ |
| 531 | rc = SQLITE_MISUSE; |
| 532 | } |
| 533 | if( rc==SQLITE_OK ){ |
| 534 | *ppStmt = (sqlite3_stmt*)sParse.pVdbe; |
| 535 | }else if( sParse.pVdbe ){ |
| 536 | sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe); |
| 537 | } |
| 538 | |
| 539 | if( zErrMsg ){ |
| 540 | sqlite3Error(db, rc, "%s", zErrMsg); |
| 541 | sqliteFree(zErrMsg); |
| 542 | }else{ |
| 543 | sqlite3Error(db, rc, 0); |
| 544 | } |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 545 | |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 546 | rc = sqlite3ApiExit(db, rc); |
drh | 4b494d6 | 2006-01-12 12:43:36 +0000 | [diff] [blame] | 547 | sqlite3ReleaseThreadData(); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 548 | return rc; |
| 549 | } |
| 550 | |
| 551 | #ifndef SQLITE_OMIT_UTF16 |
| 552 | /* |
| 553 | ** Compile the UTF-16 encoded SQL statement zSql into a statement handle. |
| 554 | */ |
| 555 | int sqlite3_prepare16( |
| 556 | sqlite3 *db, /* Database handle. */ |
| 557 | const void *zSql, /* UTF-8 encoded SQL statement. */ |
| 558 | int nBytes, /* Length of zSql in bytes. */ |
| 559 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 560 | const void **pzTail /* OUT: End of parsed string */ |
| 561 | ){ |
| 562 | /* This function currently works by first transforming the UTF-16 |
| 563 | ** encoded string to UTF-8, then invoking sqlite3_prepare(). The |
| 564 | ** tricky bit is figuring out the pointer to return in *pzTail. |
| 565 | */ |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 566 | char *zSql8; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 567 | const char *zTail8 = 0; |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 568 | int rc = SQLITE_OK; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 569 | |
| 570 | if( sqlite3SafetyCheck(db) ){ |
| 571 | return SQLITE_MISUSE; |
| 572 | } |
drh | af9a7c2 | 2005-12-15 03:04:10 +0000 | [diff] [blame] | 573 | zSql8 = sqlite3utf16to8(zSql, nBytes); |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 574 | if( zSql8 ){ |
| 575 | rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 576 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 577 | |
| 578 | if( zTail8 && pzTail ){ |
| 579 | /* If sqlite3_prepare returns a tail pointer, we calculate the |
| 580 | ** equivalent pointer into the UTF-16 string by counting the unicode |
| 581 | ** characters between zSql8 and zTail8, and then returning a pointer |
| 582 | ** the same number of characters into the UTF-16 string. |
| 583 | */ |
| 584 | int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8); |
| 585 | *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed); |
| 586 | } |
drh | af9a7c2 | 2005-12-15 03:04:10 +0000 | [diff] [blame] | 587 | sqliteFree(zSql8); |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 588 | return sqlite3ApiExit(db, rc); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 589 | } |
| 590 | #endif /* SQLITE_OMIT_UTF16 */ |