prepare.c 23 KB

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