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