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