sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 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 | */ |
| 16 | #include "sqliteInt.h" |
| 17 | |
| 18 | /* |
| 19 | ** Fill the InitData structure with an error message that indicates |
| 20 | ** that the database is corrupt. |
| 21 | */ |
| 22 | static void corruptSchema( |
| 23 | InitData *pData, /* Initialization context */ |
| 24 | char **azObj, /* Type and name of object being parsed */ |
| 25 | const char *zExtra /* Error information */ |
| 26 | ){ |
| 27 | sqlite3 *db = pData->db; |
| 28 | if( db->mallocFailed ){ |
| 29 | pData->rc = SQLITE_NOMEM_BKPT; |
| 30 | }else if( pData->pzErrMsg[0]!=0 ){ |
| 31 | /* A error message has already been generated. Do not overwrite it */ |
| 32 | }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){ |
| 33 | static const char *azAlterType[] = { |
| 34 | "rename", |
| 35 | "drop column", |
| 36 | "add column", |
| 37 | "drop constraint" |
| 38 | }; |
| 39 | *pData->pzErrMsg = sqlite3MPrintf(db, |
| 40 | "error in %s %s after %s: %s", azObj[0], azObj[1], |
| 41 | azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1], |
| 42 | zExtra |
| 43 | ); |
| 44 | pData->rc = SQLITE_ERROR; |
| 45 | }else if( db->flags & SQLITE_WriteSchema ){ |
| 46 | pData->rc = SQLITE_CORRUPT_BKPT; |
| 47 | }else{ |
| 48 | char *z; |
| 49 | const char *zObj = azObj[1] ? azObj[1] : "?"; |
| 50 | z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj); |
| 51 | if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra); |
| 52 | *pData->pzErrMsg = z; |
| 53 | pData->rc = SQLITE_CORRUPT_BKPT; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | ** Check to see if any sibling index (another index on the same table) |
| 59 | ** of pIndex has the same root page number, and if it does, return true. |
| 60 | ** This would indicate a corrupt schema. |
| 61 | */ |
| 62 | int sqlite3IndexHasDuplicateRootPage(Index *pIndex){ |
| 63 | Index *p; |
| 64 | for(p=pIndex->pTable->pIndex; p; p=p->pNext){ |
| 65 | if( p->tnum==pIndex->tnum && p!=pIndex ) return 1; |
| 66 | } |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | /* forward declaration */ |
| 71 | static int sqlite3Prepare( |
| 72 | sqlite3 *db, /* Database handle. */ |
| 73 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 74 | int nBytes, /* Length of zSql in bytes. */ |
| 75 | u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ |
| 76 | Vdbe *pReprepare, /* VM being reprepared */ |
| 77 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 78 | const char **pzTail /* OUT: End of parsed string */ |
| 79 | ); |
| 80 | |
| 81 | |
| 82 | /* |
| 83 | ** This is the callback routine for the code that initializes the |
| 84 | ** database. See sqlite3Init() below for additional information. |
| 85 | ** This routine is also called from the OP_ParseSchema opcode of the VDBE. |
| 86 | ** |
| 87 | ** Each callback contains the following information: |
| 88 | ** |
| 89 | ** argv[0] = type of object: "table", "index", "trigger", or "view". |
| 90 | ** argv[1] = name of thing being created |
| 91 | ** argv[2] = associated table if an index or trigger |
| 92 | ** argv[3] = root page number for table or index. 0 for trigger or view. |
| 93 | ** argv[4] = SQL text for the CREATE statement. |
| 94 | ** |
| 95 | */ |
| 96 | int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){ |
| 97 | InitData *pData = (InitData*)pInit; |
| 98 | sqlite3 *db = pData->db; |
| 99 | int iDb = pData->iDb; |
| 100 | |
| 101 | assert( argc==5 ); |
| 102 | UNUSED_PARAMETER2(NotUsed, argc); |
| 103 | assert( sqlite3_mutex_held(db->mutex) ); |
| 104 | db->mDbFlags |= DBFLAG_EncodingFixed; |
| 105 | if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ |
| 106 | pData->nInitRow++; |
| 107 | if( db->mallocFailed ){ |
| 108 | corruptSchema(pData, argv, 0); |
| 109 | return 1; |
| 110 | } |
| 111 | |
| 112 | assert( iDb>=0 && iDb<db->nDb ); |
| 113 | assert( db->aDb[iDb].pSchema!=0 ); |
| 114 | if( argv[3]==0 ){ |
| 115 | corruptSchema(pData, argv, 0); |
| 116 | }else if( argv[4] |
| 117 | && 'c'==sqlite3UpperToLower[(unsigned char)argv[4][0]] |
| 118 | && 'r'==sqlite3UpperToLower[(unsigned char)argv[4][1]] ){ |
| 119 | /* Call the parser to process a CREATE TABLE, INDEX or VIEW. |
| 120 | ** But because db->init.busy is set to 1, no VDBE code is generated |
| 121 | ** or executed. All the parser does is build the internal data |
| 122 | ** structures that describe the table, index, or view. |
| 123 | ** |
| 124 | ** No other valid SQL statement, other than the variable CREATE statements, |
| 125 | ** can begin with the letters "C" and "R". Thus, it is not possible run |
| 126 | ** any other kind of statement while parsing the schema, even a corrupt |
| 127 | ** schema. |
| 128 | */ |
| 129 | int rc; |
| 130 | u8 saved_iDb = db->init.iDb; |
| 131 | sqlite3_stmt *pStmt; |
| 132 | const char *zEnd; |
| 133 | TESTONLY(int rcp); /* Return code from sqlite3_prepare() */ |
| 134 | |
| 135 | assert( db->init.busy ); |
| 136 | db->init.iDb = iDb; |
| 137 | |
| 138 | /* We never try to parse a trigger on a read-only database */ |
| 139 | assert( db->aDb[iDb].pBt==0 |
| 140 | || sqlite3BtreeIsReadonly(db->aDb[iDb].pBt)==0 |
| 141 | || sqlite3_stricmp(argv[0],"trigger")!=0 |
| 142 | || CORRUPT_DB ); |
| 143 | |
| 144 | if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0 |
| 145 | || (db->init.newTnum>pData->mxPage && pData->mxPage>0) |
| 146 | ){ |
| 147 | if( sqlite3Config.bExtraSchemaChecks ){ |
| 148 | corruptSchema(pData, argv, "invalid rootpage"); |
| 149 | } |
| 150 | } |
| 151 | db->init.orphanTrigger = 0; |
| 152 | db->init.azInit = (const char**)argv; |
| 153 | pStmt = 0; |
| 154 | TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, &zEnd); |
| 155 | rc = db->errCode; |
| 156 | assert( (rc&0xFF)==(rcp&0xFF) ); |
| 157 | db->init.iDb = saved_iDb; |
| 158 | /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */ |
| 159 | if( SQLITE_OK!=rc ){ |
| 160 | if( db->init.orphanTrigger ){ |
| 161 | assert( iDb==1 ); |
| 162 | }else{ |
| 163 | if( rc > pData->rc ) pData->rc = rc; |
| 164 | if( rc==SQLITE_NOMEM ){ |
| 165 | sqlite3OomFault(db); |
| 166 | }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){ |
| 167 | corruptSchema(pData, argv, sqlite3_errmsg(db)); |
| 168 | } |
| 169 | } |
| 170 | }else if( zEnd[0] ){ |
| 171 | corruptSchema(pData, argv, 0); |
| 172 | } |
| 173 | db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */ |
| 174 | sqlite3_finalize(pStmt); |
| 175 | }else if( argv[1]==0 || (argv[4]!=0 && argv[4][0]!=0) ){ |
| 176 | corruptSchema(pData, argv, 0); |
| 177 | }else{ |
| 178 | /* If the SQL column is blank it means this is an index that |
| 179 | ** was created to be the PRIMARY KEY or to fulfill a UNIQUE |
| 180 | ** constraint for a CREATE TABLE. The index should have already |
| 181 | ** been created when we processed the CREATE TABLE. All we have |
| 182 | ** to do here is record the root page number for that index. |
| 183 | */ |
| 184 | Index *pIndex; |
| 185 | pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName); |
| 186 | if( pIndex==0 ){ |
| 187 | corruptSchema(pData, argv, "orphan index"); |
| 188 | }else |
| 189 | if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0 |
| 190 | || pIndex->tnum<2 |
| 191 | || pIndex->tnum>pData->mxPage |
| 192 | || sqlite3IndexHasDuplicateRootPage(pIndex) |
| 193 | ){ |
| 194 | if( sqlite3Config.bExtraSchemaChecks ){ |
| 195 | corruptSchema(pData, argv, "invalid rootpage"); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | if( pData->pzErrMsg[0]==0 ){ |
| 200 | Schema *pX = db->aDb[iDb].pSchema; |
| 201 | if( pX->tblHash.count + pX->idxHash.count + pX->trigHash.count |
| 202 | > (u32)db->aLimit[SQLITE_LIMIT_SCHEMA] |
| 203 | ){ |
| 204 | *pData->pzErrMsg = sqlite3MPrintf(db, "too many schema objects"); |
| 205 | pData->rc = SQLITE_ERROR; |
| 206 | return 1; |
| 207 | } |
| 208 | } |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | ** Attempt to read the database schema and initialize internal |
| 214 | ** data structures for a single database file. The index of the |
| 215 | ** database file is given by iDb. iDb==0 is used for the main |
| 216 | ** database. iDb==1 should never be used. iDb>=2 is used for |
| 217 | ** auxiliary databases. Return one of the SQLITE_ error codes to |
| 218 | ** indicate success or failure. |
| 219 | */ |
| 220 | int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){ |
| 221 | int rc; |
| 222 | int i; |
| 223 | #ifndef SQLITE_OMIT_DEPRECATED |
| 224 | int size; |
| 225 | #endif |
| 226 | Db *pDb; |
| 227 | char const *azArg[6]; |
| 228 | int meta[5]; |
| 229 | InitData initData; |
| 230 | const char *zSchemaTabName; |
| 231 | int openedTransaction = 0; |
| 232 | int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed); |
| 233 | |
| 234 | assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 ); |
| 235 | assert( iDb>=0 && iDb<db->nDb ); |
| 236 | assert( db->aDb[iDb].pSchema ); |
| 237 | assert( sqlite3_mutex_held(db->mutex) ); |
| 238 | assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); |
| 239 | |
| 240 | db->init.busy = 1 + ((mFlags & INITFLAG_AlterAdd)!=0); |
| 241 | /* ^--- Any non-zero value for init.busy means that we are scanning |
| 242 | ** the sqlite_schema table to build the internal schema representation, |
| 243 | ** rather than running actual CREATE statements. init.busy==2 has the |
| 244 | ** additional meaning that the scan is happening as part of |
| 245 | ** ALTER TABLE ADD COLUMN, which is stricter in its enforcement of |
| 246 | ** function name resolution. */ |
| 247 | |
| 248 | /* Construct the in-memory representation schema tables (sqlite_schema or |
| 249 | ** sqlite_temp_schema) by invoking the parser directly. The appropriate |
| 250 | ** table name will be inserted automatically by the parser so we can just |
| 251 | ** use the abbreviation "x" here. The parser will also automatically tag |
| 252 | ** the schema table as read-only. */ |
| 253 | azArg[0] = "table"; |
| 254 | azArg[1] = zSchemaTabName = SCHEMA_TABLE(iDb); |
| 255 | azArg[2] = azArg[1]; |
| 256 | azArg[3] = "1"; |
| 257 | azArg[4] = "CREATE TABLE x(type text,name text,tbl_name text," |
| 258 | "rootpage int,sql text)"; |
| 259 | azArg[5] = 0; |
| 260 | initData.db = db; |
| 261 | initData.iDb = iDb; |
| 262 | initData.rc = SQLITE_OK; |
| 263 | initData.pzErrMsg = pzErrMsg; |
| 264 | initData.mInitFlags = mFlags; |
| 265 | initData.nInitRow = 0; |
| 266 | initData.mxPage = 0; |
| 267 | sqlite3InitCallback(&initData, 5, (char **)azArg, 0); |
| 268 | db->mDbFlags &= mask; |
| 269 | if( initData.rc ){ |
| 270 | rc = initData.rc; |
| 271 | goto error_out; |
| 272 | } |
| 273 | |
| 274 | /* Create a cursor to hold the database open |
| 275 | */ |
| 276 | pDb = &db->aDb[iDb]; |
| 277 | if( pDb->pBt==0 ){ |
| 278 | assert( iDb==1 ); |
| 279 | DbSetProperty(db, 1, DB_SchemaLoaded); |
| 280 | rc = SQLITE_OK; |
| 281 | goto error_out; |
| 282 | } |
| 283 | |
| 284 | /* If there is not already a read-only (or read-write) transaction opened |
| 285 | ** on the b-tree database, open one now. If a transaction is opened, it |
| 286 | ** will be closed before this function returns. */ |
| 287 | sqlite3BtreeEnter(pDb->pBt); |
| 288 | if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE ){ |
| 289 | rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0); |
| 290 | if( rc!=SQLITE_OK ){ |
| 291 | sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc)); |
| 292 | goto initone_error_out; |
| 293 | } |
| 294 | openedTransaction = 1; |
| 295 | } |
| 296 | |
| 297 | /* Get the database meta information. |
| 298 | ** |
| 299 | ** Meta values are as follows: |
| 300 | ** meta[0] Schema cookie. Changes with each schema change. |
| 301 | ** meta[1] File format of schema layer. |
| 302 | ** meta[2] Size of the page cache. |
| 303 | ** meta[3] Largest rootpage (auto/incr_vacuum mode) |
| 304 | ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE |
| 305 | ** meta[5] User version |
| 306 | ** meta[6] Incremental vacuum mode |
| 307 | ** meta[7] unused |
| 308 | ** meta[8] unused |
| 309 | ** meta[9] unused |
| 310 | ** |
| 311 | ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to |
| 312 | ** the possible values of meta[4]. |
| 313 | */ |
| 314 | for(i=0; i<ArraySize(meta); i++){ |
| 315 | sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]); |
| 316 | } |
| 317 | if( (db->flags & SQLITE_ResetDatabase)!=0 ){ |
| 318 | memset(meta, 0, sizeof(meta)); |
| 319 | } |
| 320 | pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1]; |
| 321 | |
| 322 | /* If opening a non-empty database, check the text encoding. For the |
| 323 | ** main database, set sqlite3.enc to the encoding of the main database. |
| 324 | ** For an attached db, it is an error if the encoding is not the same |
| 325 | ** as sqlite3.enc. |
| 326 | */ |
| 327 | if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */ |
| 328 | if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){ |
| 329 | u8 encoding; |
| 330 | #ifndef SQLITE_OMIT_UTF16 |
| 331 | /* If opening the main database, set ENC(db). */ |
| 332 | encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3; |
| 333 | if( encoding==0 ) encoding = SQLITE_UTF8; |
| 334 | #else |
| 335 | encoding = SQLITE_UTF8; |
| 336 | #endif |
| 337 | sqlite3SetTextEncoding(db, encoding); |
| 338 | }else{ |
| 339 | /* If opening an attached database, the encoding much match ENC(db) */ |
| 340 | if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){ |
| 341 | sqlite3SetString(pzErrMsg, db, "attached databases must use the same" |
| 342 | " text encoding as main database"); |
| 343 | rc = SQLITE_ERROR; |
| 344 | goto initone_error_out; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | pDb->pSchema->enc = ENC(db); |
| 349 | |
| 350 | if( pDb->pSchema->cache_size==0 ){ |
| 351 | #ifndef SQLITE_OMIT_DEPRECATED |
| 352 | size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]); |
| 353 | if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; } |
| 354 | pDb->pSchema->cache_size = size; |
| 355 | #else |
| 356 | pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE; |
| 357 | #endif |
| 358 | sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | ** file_format==1 Version 3.0.0. |
| 363 | ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN |
| 364 | ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults |
| 365 | ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants |
| 366 | */ |
| 367 | pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1]; |
| 368 | if( pDb->pSchema->file_format==0 ){ |
| 369 | pDb->pSchema->file_format = 1; |
| 370 | } |
| 371 | if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){ |
| 372 | sqlite3SetString(pzErrMsg, db, "unsupported file format"); |
| 373 | rc = SQLITE_ERROR; |
| 374 | goto initone_error_out; |
| 375 | } |
| 376 | |
| 377 | /* Ticket #2804: When we open a database in the newer file format, |
| 378 | ** clear the legacy_file_format pragma flag so that a VACUUM will |
| 379 | ** not downgrade the database and thus invalidate any descending |
| 380 | ** indices that the user might have created. |
| 381 | */ |
| 382 | if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){ |
| 383 | db->flags &= ~(u64)SQLITE_LegacyFileFmt; |
| 384 | } |
| 385 | |
| 386 | /* Read the schema information out of the schema tables |
| 387 | */ |
| 388 | assert( db->init.busy ); |
| 389 | initData.mxPage = sqlite3BtreeLastPage(pDb->pBt); |
| 390 | { |
| 391 | char *zSql; |
| 392 | zSql = sqlite3MPrintf(db, |
| 393 | "SELECT*FROM\"%w\".%s %sORDER BY rowid", |
| 394 | db->aDb[iDb].zDbSName, zSchemaTabName, |
| 395 | sqlite3BtreeIsReadonly(db->aDb[iDb].pBt)?"WHERE type<>'trigger'":""); |
| 396 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 397 | { |
| 398 | sqlite3_xauth xAuth; |
| 399 | xAuth = db->xAuth; |
| 400 | db->xAuth = 0; |
| 401 | #endif |
| 402 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
| 403 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 404 | db->xAuth = xAuth; |
| 405 | } |
| 406 | #endif |
| 407 | if( rc==SQLITE_OK ) rc = initData.rc; |
| 408 | sqlite3DbFree(db, zSql); |
| 409 | #ifndef SQLITE_OMIT_ANALYZE |
| 410 | if( rc==SQLITE_OK ){ |
| 411 | sqlite3AnalysisLoad(db, iDb); |
| 412 | } |
| 413 | #endif |
| 414 | } |
| 415 | assert( pDb == &(db->aDb[iDb]) ); |
| 416 | |
| 417 | /* Confirm that TRIGGERs are not parsed on a read-only database */ |
| 418 | assert( sqlite3BtreeIsReadonly(pDb->pBt)==0 |
| 419 | || sqliteHashCount(&pDb->pSchema->trigHash)==0 |
| 420 | || CORRUPT_DB ); |
| 421 | |
| 422 | if( db->mallocFailed ){ |
| 423 | rc = SQLITE_NOMEM_BKPT; |
| 424 | sqlite3ResetAllSchemasOfConnection(db); |
| 425 | pDb = &db->aDb[iDb]; |
| 426 | }else |
| 427 | if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){ |
| 428 | /* Hack: If the SQLITE_NoSchemaError flag is set, then consider |
| 429 | ** the schema loaded, even if errors (other than OOM) occurred. In |
| 430 | ** this situation the current sqlite3_prepare() operation will fail, |
| 431 | ** but the following one will attempt to compile the supplied statement |
| 432 | ** against whatever subset of the schema was loaded before the error |
| 433 | ** occurred. |
| 434 | ** |
| 435 | ** The primary purpose of this is to allow access to the sqlite_schema |
| 436 | ** table even when its contents have been corrupted. |
| 437 | */ |
| 438 | DbSetProperty(db, iDb, DB_SchemaLoaded); |
| 439 | rc = SQLITE_OK; |
| 440 | } |
| 441 | |
| 442 | /* Jump here for an error that occurs after successfully allocating |
| 443 | ** curMain and calling sqlite3BtreeEnter(). For an error that occurs |
| 444 | ** before that point, jump to error_out. |
| 445 | */ |
| 446 | initone_error_out: |
| 447 | if( openedTransaction ){ |
| 448 | sqlite3BtreeCommit(pDb->pBt); |
| 449 | } |
| 450 | sqlite3BtreeLeave(pDb->pBt); |
| 451 | |
| 452 | error_out: |
| 453 | if( rc ){ |
| 454 | if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ |
| 455 | sqlite3OomFault(db); |
| 456 | } |
| 457 | sqlite3ResetOneSchema(db, iDb, 0); |
| 458 | } |
| 459 | db->init.busy = 0; |
| 460 | return rc; |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | ** Initialize all database files - the main database file, the file |
| 465 | ** used to store temporary tables, and any additional database files |
| 466 | ** created using ATTACH statements. Return a success code. If an |
| 467 | ** error occurs, write an error message into *pzErrMsg. |
| 468 | ** |
| 469 | ** After a database is initialized, the DB_SchemaLoaded bit is set |
| 470 | ** bit is set in the flags field of the Db structure. |
| 471 | */ |
| 472 | int sqlite3Init(sqlite3 *db, char **pzErrMsg){ |
| 473 | int i, rc; |
| 474 | int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange); |
| 475 | |
| 476 | assert( sqlite3_mutex_held(db->mutex) ); |
| 477 | assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) ); |
| 478 | assert( db->init.busy==0 ); |
| 479 | ENC(db) = SCHEMA_ENC(db); |
| 480 | assert( db->nDb>0 ); |
| 481 | /* Do the main schema first */ |
| 482 | if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){ |
| 483 | rc = sqlite3InitOne(db, 0, pzErrMsg, 0); |
| 484 | if( rc ) return rc; |
| 485 | } |
| 486 | /* All other schemas after the main schema. The "temp" schema must be last */ |
| 487 | for(i=db->nDb-1; i>0; i--){ |
| 488 | assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) ); |
| 489 | if( !DbHasProperty(db, i, DB_SchemaLoaded) ){ |
| 490 | rc = sqlite3InitOne(db, i, pzErrMsg, 0); |
| 491 | if( rc ) return rc; |
| 492 | } |
| 493 | } |
| 494 | if( commit_internal ){ |
| 495 | sqlite3CommitInternalChanges(db); |
| 496 | } |
| 497 | return SQLITE_OK; |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | ** This routine is a no-op if the database schema is already initialized. |
| 502 | ** Otherwise, the schema is loaded. An error code is returned. |
| 503 | */ |
| 504 | int sqlite3ReadSchema(Parse *pParse){ |
| 505 | int rc = SQLITE_OK; |
| 506 | sqlite3 *db = pParse->db; |
| 507 | assert( sqlite3_mutex_held(db->mutex) ); |
| 508 | if( !db->init.busy ){ |
| 509 | rc = sqlite3Init(db, &pParse->zErrMsg); |
| 510 | if( rc!=SQLITE_OK ){ |
| 511 | pParse->rc = rc; |
| 512 | pParse->nErr++; |
| 513 | }else if( db->noSharedCache ){ |
| 514 | db->mDbFlags |= DBFLAG_SchemaKnownOk; |
| 515 | } |
| 516 | } |
| 517 | return rc; |
| 518 | } |
| 519 | |
| 520 | |
| 521 | /* |
| 522 | ** Check schema cookies in all databases. If any cookie is out |
| 523 | ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies |
| 524 | ** make no changes to pParse->rc. |
| 525 | */ |
| 526 | static void schemaIsValid(Parse *pParse){ |
| 527 | sqlite3 *db = pParse->db; |
| 528 | int iDb; |
| 529 | int rc; |
| 530 | int cookie; |
| 531 | |
| 532 | assert( pParse->checkSchema ); |
| 533 | assert( sqlite3_mutex_held(db->mutex) ); |
| 534 | for(iDb=0; iDb<db->nDb; iDb++){ |
| 535 | int openedTransaction = 0; /* True if a transaction is opened */ |
| 536 | Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */ |
| 537 | if( pBt==0 ) continue; |
| 538 | |
| 539 | /* If there is not already a read-only (or read-write) transaction opened |
| 540 | ** on the b-tree database, open one now. If a transaction is opened, it |
| 541 | ** will be closed immediately after reading the meta-value. */ |
| 542 | if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_NONE ){ |
| 543 | rc = sqlite3BtreeBeginTrans(pBt, 0, 0); |
| 544 | if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ |
| 545 | sqlite3OomFault(db); |
| 546 | pParse->rc = SQLITE_NOMEM; |
| 547 | } |
| 548 | if( rc!=SQLITE_OK ) return; |
| 549 | openedTransaction = 1; |
| 550 | } |
| 551 | |
| 552 | /* Read the schema cookie from the database. If it does not match the |
| 553 | ** value stored as part of the in-memory schema representation, |
| 554 | ** set Parse.rc to SQLITE_SCHEMA. */ |
| 555 | sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie); |
| 556 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 557 | if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){ |
| 558 | if( DbHasProperty(db, iDb, DB_SchemaLoaded) ) pParse->rc = SQLITE_SCHEMA; |
| 559 | sqlite3ResetOneSchema(db, iDb, 0); |
| 560 | } |
| 561 | |
| 562 | /* Close the transaction, if one was opened. */ |
| 563 | if( openedTransaction ){ |
| 564 | sqlite3BtreeCommit(pBt); |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | ** Convert a schema pointer into the iDb index that indicates |
| 571 | ** which database file in db->aDb[] the schema refers to. |
| 572 | ** |
| 573 | ** If the same database is attached more than once, the first |
| 574 | ** attached database is returned. |
| 575 | */ |
| 576 | int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){ |
| 577 | int i = -32768; |
| 578 | |
| 579 | /* If pSchema is NULL, then return -32768. This happens when code in |
| 580 | ** expr.c is trying to resolve a reference to a transient table (i.e. one |
| 581 | ** created by a sub-select). In this case the return value of this |
| 582 | ** function should never be used. |
| 583 | ** |
| 584 | ** We return -32768 instead of the more usual -1 simply because using |
| 585 | ** -32768 as the incorrect index into db->aDb[] is much |
| 586 | ** more likely to cause a segfault than -1 (of course there are assert() |
| 587 | ** statements too, but it never hurts to play the odds) and |
| 588 | ** -32768 will still fit into a 16-bit signed integer. |
| 589 | */ |
| 590 | assert( sqlite3_mutex_held(db->mutex) ); |
| 591 | if( pSchema ){ |
| 592 | for(i=0; 1; i++){ |
| 593 | assert( i<db->nDb ); |
| 594 | if( db->aDb[i].pSchema==pSchema ){ |
| 595 | break; |
| 596 | } |
| 597 | } |
| 598 | assert( i>=0 && i<db->nDb ); |
| 599 | } |
| 600 | return i; |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | ** Free all memory allocations in the pParse object |
| 605 | */ |
| 606 | void sqlite3ParseObjectReset(Parse *pParse){ |
| 607 | sqlite3 *db = pParse->db; |
| 608 | assert( db!=0 ); |
| 609 | assert( db->pParse==pParse ); |
| 610 | assert( pParse->nested==0 ); |
| 611 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 612 | if( pParse->nTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock); |
| 613 | #endif |
| 614 | while( pParse->pCleanup ){ |
| 615 | ParseCleanup *pCleanup = pParse->pCleanup; |
| 616 | pParse->pCleanup = pCleanup->pNext; |
| 617 | pCleanup->xCleanup(db, pCleanup->pPtr); |
| 618 | sqlite3DbNNFreeNN(db, pCleanup); |
| 619 | } |
| 620 | if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel); |
| 621 | if( pParse->pConstExpr ){ |
| 622 | sqlite3ExprListDelete(db, pParse->pConstExpr); |
| 623 | } |
| 624 | assert( db->lookaside.bDisable >= pParse->disableLookaside ); |
| 625 | db->lookaside.bDisable -= pParse->disableLookaside; |
| 626 | db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue; |
| 627 | assert( pParse->db->pParse==pParse ); |
| 628 | db->pParse = pParse->pOuterParse; |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | ** Add a new cleanup operation to a Parser. The cleanup should happen when |
| 633 | ** the parser object is destroyed. But, beware: the cleanup might happen |
| 634 | ** immediately. |
| 635 | ** |
| 636 | ** Use this mechanism for uncommon cleanups. There is a higher setup |
| 637 | ** cost for this mechanism (an extra malloc), so it should not be used |
| 638 | ** for common cleanups that happen on most calls. But for less |
| 639 | ** common cleanups, we save a single NULL-pointer comparison in |
| 640 | ** sqlite3ParseObjectReset(), which reduces the total CPU cycle count. |
| 641 | ** |
| 642 | ** If a memory allocation error occurs, then the cleanup happens immediately. |
| 643 | ** When either SQLITE_DEBUG or SQLITE_COVERAGE_TEST are defined, the |
| 644 | ** pParse->earlyCleanup flag is set in that case. Calling code show verify |
| 645 | ** that test cases exist for which this happens, to guard against possible |
| 646 | ** use-after-free errors following an OOM. The preferred way to do this is |
| 647 | ** to immediately follow the call to this routine with: |
| 648 | ** |
| 649 | ** testcase( pParse->earlyCleanup ); |
| 650 | ** |
| 651 | ** This routine returns a copy of its pPtr input (the third parameter) |
| 652 | ** except if an early cleanup occurs, in which case it returns NULL. So |
| 653 | ** another way to check for early cleanup is to check the return value. |
| 654 | ** Or, stop using the pPtr parameter with this call and use only its |
| 655 | ** return value thereafter. Something like this: |
| 656 | ** |
| 657 | ** pObj = sqlite3ParserAddCleanup(pParse, destructor, pObj); |
| 658 | */ |
| 659 | void *sqlite3ParserAddCleanup( |
| 660 | Parse *pParse, /* Destroy when this Parser finishes */ |
| 661 | void (*xCleanup)(sqlite3*,void*), /* The cleanup routine */ |
| 662 | void *pPtr /* Pointer to object to be cleaned up */ |
| 663 | ){ |
| 664 | ParseCleanup *pCleanup; |
| 665 | if( sqlite3FaultSim(300) ){ |
| 666 | pCleanup = 0; |
| 667 | sqlite3OomFault(pParse->db); |
| 668 | }else{ |
| 669 | pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup)); |
| 670 | } |
| 671 | if( pCleanup ){ |
| 672 | pCleanup->pNext = pParse->pCleanup; |
| 673 | pParse->pCleanup = pCleanup; |
| 674 | pCleanup->pPtr = pPtr; |
| 675 | pCleanup->xCleanup = xCleanup; |
| 676 | }else{ |
| 677 | xCleanup(pParse->db, pPtr); |
| 678 | pPtr = 0; |
| 679 | #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) |
| 680 | pParse->earlyCleanup = 1; |
| 681 | #endif |
| 682 | } |
| 683 | return pPtr; |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | ** Turn bulk memory into a valid Parse object and link that Parse object |
| 688 | ** into database connection db. |
| 689 | ** |
| 690 | ** Call sqlite3ParseObjectReset() to undo this operation. |
| 691 | ** |
| 692 | ** Caution: Do not confuse this routine with sqlite3ParseObjectInit() which |
| 693 | ** is generated by Lemon. |
| 694 | */ |
| 695 | void sqlite3ParseObjectInit(Parse *pParse, sqlite3 *db){ |
| 696 | assert( db!=0 ); |
| 697 | memset(PARSE_HDR(pParse), 0, PARSE_HDR_SZ); |
| 698 | memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ); |
| 699 | assert( db->pParse!=pParse ); |
| 700 | pParse->pOuterParse = db->pParse; |
| 701 | db->pParse = pParse; |
| 702 | pParse->db = db; |
| 703 | if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory"); |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | ** Maximum number of times that we will try again to prepare a statement |
| 708 | ** that returns SQLITE_ERROR_RETRY. |
| 709 | */ |
| 710 | #ifndef SQLITE_MAX_PREPARE_RETRY |
| 711 | # define SQLITE_MAX_PREPARE_RETRY 25 |
| 712 | #endif |
| 713 | |
| 714 | /* |
| 715 | ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. |
| 716 | */ |
| 717 | static int sqlite3Prepare( |
| 718 | sqlite3 *db, /* Database handle. */ |
| 719 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 720 | int nBytes, /* Length of zSql in bytes. */ |
| 721 | u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ |
| 722 | Vdbe *pReprepare, /* VM being reprepared */ |
| 723 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 724 | const char **pzTail /* OUT: End of parsed string */ |
| 725 | ){ |
| 726 | int rc = SQLITE_OK; /* Result code */ |
| 727 | int i; /* Loop counter */ |
| 728 | Parse sParse; /* Parsing context */ |
| 729 | |
| 730 | /* sqlite3ParseObjectInit(&sParse, db); // inlined for performance */ |
| 731 | memset(PARSE_HDR(&sParse), 0, PARSE_HDR_SZ); |
| 732 | memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ); |
| 733 | sParse.pOuterParse = db->pParse; |
| 734 | db->pParse = &sParse; |
| 735 | sParse.db = db; |
| 736 | if( pReprepare ){ |
| 737 | sParse.pReprepare = pReprepare; |
| 738 | sParse.explain = sqlite3_stmt_isexplain((sqlite3_stmt*)pReprepare); |
| 739 | }else{ |
| 740 | assert( sParse.pReprepare==0 ); |
| 741 | } |
| 742 | assert( ppStmt && *ppStmt==0 ); |
| 743 | if( db->mallocFailed ){ |
| 744 | sqlite3ErrorMsg(&sParse, "out of memory"); |
| 745 | db->errCode = rc = SQLITE_NOMEM; |
| 746 | goto end_prepare; |
| 747 | } |
| 748 | assert( sqlite3_mutex_held(db->mutex) ); |
| 749 | |
| 750 | /* For a long-term use prepared statement avoid the use of |
| 751 | ** lookaside memory. |
| 752 | */ |
| 753 | if( prepFlags & SQLITE_PREPARE_PERSISTENT ){ |
| 754 | sParse.disableLookaside++; |
| 755 | DisableLookaside; |
| 756 | } |
| 757 | sParse.prepFlags = prepFlags & 0xff; |
| 758 | |
| 759 | /* Check to verify that it is possible to get a read lock on all |
| 760 | ** database schemas. The inability to get a read lock indicates that |
| 761 | ** some other database connection is holding a write-lock, which in |
| 762 | ** turn means that the other connection has made uncommitted changes |
| 763 | ** to the schema. |
| 764 | ** |
| 765 | ** Were we to proceed and prepare the statement against the uncommitted |
| 766 | ** schema changes and if those schema changes are subsequently rolled |
| 767 | ** back and different changes are made in their place, then when this |
| 768 | ** prepared statement goes to run the schema cookie would fail to detect |
| 769 | ** the schema change. Disaster would follow. |
| 770 | ** |
| 771 | ** This thread is currently holding mutexes on all Btrees (because |
| 772 | ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it |
| 773 | ** is not possible for another thread to start a new schema change |
| 774 | ** while this routine is running. Hence, we do not need to hold |
| 775 | ** locks on the schema, we just need to make sure nobody else is |
| 776 | ** holding them. |
| 777 | ** |
| 778 | ** Note that setting READ_UNCOMMITTED overrides most lock detection, |
| 779 | ** but it does *not* override schema lock detection, so this all still |
| 780 | ** works even if READ_UNCOMMITTED is set. |
| 781 | */ |
| 782 | if( !db->noSharedCache ){ |
| 783 | for(i=0; i<db->nDb; i++) { |
| 784 | Btree *pBt = db->aDb[i].pBt; |
| 785 | if( pBt ){ |
| 786 | assert( sqlite3BtreeHoldsMutex(pBt) ); |
| 787 | rc = sqlite3BtreeSchemaLocked(pBt); |
| 788 | if( rc ){ |
| 789 | const char *zDb = db->aDb[i].zDbSName; |
| 790 | sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb); |
| 791 | testcase( db->flags & SQLITE_ReadUncommit ); |
| 792 | goto end_prepare; |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 799 | if( db->pDisconnect ) sqlite3VtabUnlockList(db); |
| 800 | #endif |
| 801 | |
| 802 | if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){ |
| 803 | char *zSqlCopy; |
| 804 | int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; |
| 805 | testcase( nBytes==mxLen ); |
| 806 | testcase( nBytes==mxLen+1 ); |
| 807 | if( nBytes>mxLen ){ |
| 808 | sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long"); |
| 809 | rc = sqlite3ApiExit(db, SQLITE_TOOBIG); |
| 810 | goto end_prepare; |
| 811 | } |
| 812 | zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes); |
| 813 | if( zSqlCopy ){ |
| 814 | sqlite3RunParser(&sParse, zSqlCopy); |
| 815 | sParse.zTail = &zSql[sParse.zTail-zSqlCopy]; |
| 816 | sqlite3DbFree(db, zSqlCopy); |
| 817 | }else{ |
| 818 | sParse.zTail = &zSql[nBytes]; |
| 819 | } |
| 820 | }else{ |
| 821 | sqlite3RunParser(&sParse, zSql); |
| 822 | } |
| 823 | assert( 0==sParse.nQueryLoop ); |
| 824 | |
| 825 | if( pzTail ){ |
| 826 | *pzTail = sParse.zTail; |
| 827 | } |
| 828 | |
| 829 | if( db->init.busy==0 ){ |
| 830 | sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags); |
| 831 | } |
| 832 | if( db->mallocFailed ){ |
| 833 | sParse.rc = SQLITE_NOMEM_BKPT; |
| 834 | sParse.checkSchema = 0; |
| 835 | } |
| 836 | if( sParse.rc!=SQLITE_OK && sParse.rc!=SQLITE_DONE ){ |
| 837 | if( sParse.checkSchema && db->init.busy==0 ){ |
| 838 | schemaIsValid(&sParse); |
| 839 | } |
| 840 | if( sParse.pVdbe ){ |
| 841 | sqlite3VdbeFinalize(sParse.pVdbe); |
| 842 | } |
| 843 | assert( 0==(*ppStmt) ); |
| 844 | rc = sParse.rc; |
| 845 | if( sParse.zErrMsg ){ |
| 846 | sqlite3ErrorWithMsg(db, rc, "%s", sParse.zErrMsg); |
| 847 | sqlite3DbFree(db, sParse.zErrMsg); |
| 848 | }else{ |
| 849 | sqlite3Error(db, rc); |
| 850 | } |
| 851 | }else{ |
| 852 | assert( sParse.zErrMsg==0 ); |
| 853 | *ppStmt = (sqlite3_stmt*)sParse.pVdbe; |
| 854 | rc = SQLITE_OK; |
| 855 | sqlite3ErrorClear(db); |
| 856 | } |
| 857 | |
| 858 | |
| 859 | /* Delete any TriggerPrg structures allocated while parsing this statement. */ |
| 860 | while( sParse.pTriggerPrg ){ |
| 861 | TriggerPrg *pT = sParse.pTriggerPrg; |
| 862 | sParse.pTriggerPrg = pT->pNext; |
| 863 | sqlite3DbFree(db, pT); |
| 864 | } |
| 865 | |
| 866 | end_prepare: |
| 867 | |
| 868 | sqlite3ParseObjectReset(&sParse); |
| 869 | return rc; |
| 870 | } |
| 871 | static int sqlite3LockAndPrepare( |
| 872 | sqlite3 *db, /* Database handle. */ |
| 873 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 874 | int nBytes, /* Length of zSql in bytes. */ |
| 875 | u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ |
| 876 | Vdbe *pOld, /* VM being reprepared */ |
| 877 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 878 | const char **pzTail /* OUT: End of parsed string */ |
| 879 | ){ |
| 880 | int rc; |
| 881 | int cnt = 0; |
| 882 | |
| 883 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 884 | if( ppStmt==0 ) return SQLITE_MISUSE_BKPT; |
| 885 | #endif |
| 886 | *ppStmt = 0; |
| 887 | if( !sqlite3SafetyCheckOk(db)||zSql==0 ){ |
| 888 | return SQLITE_MISUSE_BKPT; |
| 889 | } |
| 890 | sqlite3_mutex_enter(db->mutex); |
| 891 | sqlite3BtreeEnterAll(db); |
| 892 | do{ |
| 893 | /* Make multiple attempts to compile the SQL, until it either succeeds |
| 894 | ** or encounters a permanent error. A schema problem after one schema |
| 895 | ** reset is considered a permanent error. */ |
| 896 | rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail); |
| 897 | assert( rc==SQLITE_OK || *ppStmt==0 ); |
| 898 | if( rc==SQLITE_OK || db->mallocFailed ) break; |
| 899 | cnt++; |
| 900 | }while( (rc==SQLITE_ERROR_RETRY && ALWAYS(cnt<=SQLITE_MAX_PREPARE_RETRY)) |
| 901 | || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1,0), cnt)==1) ); |
| 902 | sqlite3BtreeLeaveAll(db); |
| 903 | assert( rc!=SQLITE_ERROR_RETRY ); |
| 904 | rc = sqlite3ApiExit(db, rc); |
| 905 | assert( (rc&db->errMask)==rc ); |
| 906 | db->busyHandler.nBusy = 0; |
| 907 | sqlite3_mutex_leave(db->mutex); |
| 908 | assert( rc==SQLITE_OK || (*ppStmt)==0 ); |
| 909 | return rc; |
| 910 | } |
| 911 | |
| 912 | |
| 913 | /* |
| 914 | ** Rerun the compilation of a statement after a schema change. |
| 915 | ** |
| 916 | ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise, |
| 917 | ** if the statement cannot be recompiled because another connection has |
| 918 | ** locked the sqlite3_schema table, return SQLITE_LOCKED. If any other error |
| 919 | ** occurs, return SQLITE_SCHEMA. |
| 920 | */ |
| 921 | int sqlite3Reprepare(Vdbe *p){ |
| 922 | int rc; |
| 923 | sqlite3_stmt *pNew; |
| 924 | const char *zSql; |
| 925 | sqlite3 *db; |
| 926 | u8 prepFlags; |
| 927 | |
| 928 | assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) ); |
| 929 | zSql = sqlite3_sql((sqlite3_stmt *)p); |
| 930 | assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */ |
| 931 | db = sqlite3VdbeDb(p); |
| 932 | assert( sqlite3_mutex_held(db->mutex) ); |
| 933 | prepFlags = sqlite3VdbePrepareFlags(p); |
| 934 | rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0); |
| 935 | if( rc ){ |
| 936 | if( rc==SQLITE_NOMEM ){ |
| 937 | sqlite3OomFault(db); |
| 938 | } |
| 939 | assert( pNew==0 ); |
| 940 | return rc; |
| 941 | }else{ |
| 942 | assert( pNew!=0 ); |
| 943 | } |
| 944 | sqlite3VdbeSwap((Vdbe*)pNew, p); |
| 945 | sqlite3TransferBindings(pNew, (sqlite3_stmt*)p); |
| 946 | sqlite3VdbeResetStepResult((Vdbe*)pNew); |
| 947 | sqlite3VdbeFinalize((Vdbe*)pNew); |
| 948 | return SQLITE_OK; |
| 949 | } |
| 950 | |
| 951 | |
| 952 | /* |
| 953 | ** Two versions of the official API. Legacy and new use. In the legacy |
| 954 | ** version, the original SQL text is not saved in the prepared statement |
| 955 | ** and so if a schema change occurs, SQLITE_SCHEMA is returned by |
| 956 | ** sqlite3_step(). In the new version, the original SQL text is retained |
| 957 | ** and the statement is automatically recompiled if an schema change |
| 958 | ** occurs. |
| 959 | */ |
| 960 | int sqlite3_prepare( |
| 961 | sqlite3 *db, /* Database handle. */ |
| 962 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 963 | int nBytes, /* Length of zSql in bytes. */ |
| 964 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 965 | const char **pzTail /* OUT: End of parsed string */ |
| 966 | ){ |
| 967 | int rc; |
| 968 | rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail); |
| 969 | assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ |
| 970 | return rc; |
| 971 | } |
| 972 | int sqlite3_prepare_v2( |
| 973 | sqlite3 *db, /* Database handle. */ |
| 974 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 975 | int nBytes, /* Length of zSql in bytes. */ |
| 976 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 977 | const char **pzTail /* OUT: End of parsed string */ |
| 978 | ){ |
| 979 | int rc; |
| 980 | /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works |
| 981 | ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags |
| 982 | ** parameter. |
| 983 | ** |
| 984 | ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */ |
| 985 | rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,0, |
| 986 | ppStmt,pzTail); |
| 987 | assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); |
| 988 | return rc; |
| 989 | } |
| 990 | int sqlite3_prepare_v3( |
| 991 | sqlite3 *db, /* Database handle. */ |
| 992 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 993 | int nBytes, /* Length of zSql in bytes. */ |
| 994 | unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ |
| 995 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 996 | const char **pzTail /* OUT: End of parsed string */ |
| 997 | ){ |
| 998 | int rc; |
| 999 | /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from |
| 1000 | ** sqlite3_prepare_v2() only in having the extra prepFlags parameter, |
| 1001 | ** which is a bit array consisting of zero or more of the |
| 1002 | ** SQLITE_PREPARE_* flags. |
| 1003 | ** |
| 1004 | ** Proof by comparison to the implementation of sqlite3_prepare_v2() |
| 1005 | ** directly above. */ |
| 1006 | rc = sqlite3LockAndPrepare(db,zSql,nBytes, |
| 1007 | SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK), |
| 1008 | 0,ppStmt,pzTail); |
| 1009 | assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); |
| 1010 | return rc; |
| 1011 | } |
| 1012 | |
| 1013 | |
| 1014 | #ifndef SQLITE_OMIT_UTF16 |
| 1015 | /* |
| 1016 | ** Compile the UTF-16 encoded SQL statement zSql into a statement handle. |
| 1017 | */ |
| 1018 | static int sqlite3Prepare16( |
| 1019 | sqlite3 *db, /* Database handle. */ |
| 1020 | const void *zSql, /* UTF-16 encoded SQL statement. */ |
| 1021 | int nBytes, /* Length of zSql in bytes. */ |
| 1022 | u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ |
| 1023 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 1024 | const void **pzTail /* OUT: End of parsed string */ |
| 1025 | ){ |
| 1026 | /* This function currently works by first transforming the UTF-16 |
| 1027 | ** encoded string to UTF-8, then invoking sqlite3_prepare(). The |
| 1028 | ** tricky bit is figuring out the pointer to return in *pzTail. |
| 1029 | */ |
| 1030 | char *zSql8; |
| 1031 | const char *zTail8 = 0; |
| 1032 | int rc = SQLITE_OK; |
| 1033 | |
| 1034 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1035 | if( ppStmt==0 ) return SQLITE_MISUSE_BKPT; |
| 1036 | #endif |
| 1037 | *ppStmt = 0; |
| 1038 | if( !sqlite3SafetyCheckOk(db)||zSql==0 ){ |
| 1039 | return SQLITE_MISUSE_BKPT; |
| 1040 | } |
| 1041 | |
| 1042 | /* Make sure nBytes is non-negative and correct. It should be the |
| 1043 | ** number of bytes until the end of the input buffer or until the first |
| 1044 | ** U+0000 character. If the input nBytes is odd, convert it into |
| 1045 | ** an even number. If the input nBytes is negative, then the input |
| 1046 | ** must be terminated by at least one U+0000 character */ |
| 1047 | if( nBytes>=0 ){ |
| 1048 | int sz; |
| 1049 | const char *z = (const char*)zSql; |
| 1050 | for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){} |
| 1051 | nBytes = sz; |
| 1052 | }else{ |
| 1053 | int sz; |
| 1054 | const char *z = (const char*)zSql; |
| 1055 | for(sz=0; z[sz]!=0 || z[sz+1]!=0; sz += 2){} |
| 1056 | nBytes = sz; |
| 1057 | } |
| 1058 | |
| 1059 | sqlite3_mutex_enter(db->mutex); |
| 1060 | zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE); |
| 1061 | if( zSql8 ){ |
| 1062 | rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8); |
| 1063 | } |
| 1064 | |
| 1065 | if( zTail8 && pzTail ){ |
| 1066 | /* If sqlite3_prepare returns a tail pointer, we calculate the |
| 1067 | ** equivalent pointer into the UTF-16 string by counting the unicode |
| 1068 | ** characters between zSql8 and zTail8, and then returning a pointer |
| 1069 | ** the same number of characters into the UTF-16 string. |
| 1070 | */ |
| 1071 | int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8)); |
| 1072 | *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, nBytes, chars_parsed); |
| 1073 | } |
| 1074 | sqlite3DbFree(db, zSql8); |
| 1075 | rc = sqlite3ApiExit(db, rc); |
| 1076 | sqlite3_mutex_leave(db->mutex); |
| 1077 | return rc; |
| 1078 | } |
| 1079 | |
| 1080 | /* |
| 1081 | ** Two versions of the official API. Legacy and new use. In the legacy |
| 1082 | ** version, the original SQL text is not saved in the prepared statement |
| 1083 | ** and so if a schema change occurs, SQLITE_SCHEMA is returned by |
| 1084 | ** sqlite3_step(). In the new version, the original SQL text is retained |
| 1085 | ** and the statement is automatically recompiled if an schema change |
| 1086 | ** occurs. |
| 1087 | */ |
| 1088 | int sqlite3_prepare16( |
| 1089 | sqlite3 *db, /* Database handle. */ |
| 1090 | const void *zSql, /* UTF-16 encoded SQL statement. */ |
| 1091 | int nBytes, /* Length of zSql in bytes. */ |
| 1092 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 1093 | const void **pzTail /* OUT: End of parsed string */ |
| 1094 | ){ |
| 1095 | int rc; |
| 1096 | rc = sqlite3Prepare16(db,zSql,nBytes&~1,0,ppStmt,pzTail); |
| 1097 | assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ |
| 1098 | return rc; |
| 1099 | } |
| 1100 | int sqlite3_prepare16_v2( |
| 1101 | sqlite3 *db, /* Database handle. */ |
| 1102 | const void *zSql, /* UTF-16 encoded SQL statement. */ |
| 1103 | int nBytes, /* Length of zSql in bytes. */ |
| 1104 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 1105 | const void **pzTail /* OUT: End of parsed string */ |
| 1106 | ){ |
| 1107 | int rc; |
| 1108 | rc = sqlite3Prepare16(db,zSql,nBytes&~1,SQLITE_PREPARE_SAVESQL,ppStmt,pzTail); |
| 1109 | assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ |
| 1110 | return rc; |
| 1111 | } |
| 1112 | int sqlite3_prepare16_v3( |
| 1113 | sqlite3 *db, /* Database handle. */ |
| 1114 | const void *zSql, /* UTF-16 encoded SQL statement. */ |
| 1115 | int nBytes, /* Length of zSql in bytes. */ |
| 1116 | unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ |
| 1117 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 1118 | const void **pzTail /* OUT: End of parsed string */ |
| 1119 | ){ |
| 1120 | int rc; |
| 1121 | rc = sqlite3Prepare16(db,zSql,nBytes&~1, |
| 1122 | SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK), |
| 1123 | ppStmt,pzTail); |
| 1124 | assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ |
| 1125 | return rc; |
| 1126 | } |
| 1127 | |
| 1128 | #endif /* SQLITE_OMIT_UTF16 */ |