blob: 68cd5a7fd04a97c131b9fb11471b90bbbaeb0d64 [file] [log] [blame]
danielk1977fa256a32005-05-25 04:11:56 +00001/*
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**
drh27641702007-08-22 02:56:42 +000016** $Id: prepare.c,v 1.58 2007/08/22 02:56:44 drh Exp $
danielk1977fa256a32005-05-25 04:11:56 +000017*/
18#include "sqliteInt.h"
danielk1977fa256a32005-05-25 04:11:56 +000019#include <ctype.h>
20
21/*
22** Fill the InitData structure with an error message that indicates
23** that the database is corrupt.
24*/
25static void corruptSchema(InitData *pData, const char *zExtra){
drh17435752007-08-16 04:30:38 +000026 if( !pData->db->mallocFailed ){
danielk1977fa256a32005-05-25 04:11:56 +000027 sqlite3SetString(pData->pzErrMsg, "malformed database schema",
28 zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0);
29 }
drh15ca1df2006-07-26 13:43:30 +000030 pData->rc = SQLITE_CORRUPT;
danielk1977fa256a32005-05-25 04:11:56 +000031}
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
danielk197778efaba2006-06-12 06:09:17 +000041** argv[1] = root page number for table or index. 0 for trigger or view.
danielk1977fa256a32005-05-25 04:11:56 +000042** argv[2] = SQL text for the CREATE statement.
danielk1977fa256a32005-05-25 04:11:56 +000043**
44*/
45int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){
46 InitData *pData = (InitData*)pInit;
47 sqlite3 *db = pData->db;
drhece3c722006-09-23 20:36:01 +000048 int iDb = pData->iDb;
danielk1977fa256a32005-05-25 04:11:56 +000049
drh15ca1df2006-07-26 13:43:30 +000050 pData->rc = SQLITE_OK;
drhece3c722006-09-23 20:36:01 +000051 DbClearProperty(db, iDb, DB_Empty);
drh17435752007-08-16 04:30:38 +000052 if( db->mallocFailed ){
drhfe5a8162006-08-12 13:28:23 +000053 corruptSchema(pData, 0);
danielk1977da184232006-01-05 11:34:32 +000054 return SQLITE_NOMEM;
55 }
56
drhece3c722006-09-23 20:36:01 +000057 assert( argc==3 );
danielk1977fa256a32005-05-25 04:11:56 +000058 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
drhece3c722006-09-23 20:36:01 +000059 if( argv[1]==0 ){
danielk1977fa256a32005-05-25 04:11:56 +000060 corruptSchema(pData, 0);
61 return 1;
62 }
danielk1977fa256a32005-05-25 04:11:56 +000063 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;
drh43617e92006-03-06 20:55:46 +000077 assert( rc!=SQLITE_OK || zErr==0 );
danielk1977fa256a32005-05-25 04:11:56 +000078 if( SQLITE_OK!=rc ){
drh15ca1df2006-07-26 13:43:30 +000079 pData->rc = rc;
danielk1977261919c2005-12-06 12:52:59 +000080 if( rc==SQLITE_NOMEM ){
drh17435752007-08-16 04:30:38 +000081 db->mallocFailed = 1;
drh15ca1df2006-07-26 13:43:30 +000082 }else if( rc!=SQLITE_INTERRUPT ){
danielk19779e128002006-01-18 16:51:35 +000083 corruptSchema(pData, zErr);
danielk1977261919c2005-12-06 12:52:59 +000084 }
danielk1977fa256a32005-05-25 04:11:56 +000085 sqlite3_free(zErr);
drh15ca1df2006-07-26 13:43:30 +000086 return 1;
danielk1977fa256a32005-05-25 04:11:56 +000087 }
88 }else{
89 /* If the SQL column is blank it means this is an index that
90 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
91 ** constraint for a CREATE TABLE. The index should have already
92 ** been created when we processed the CREATE TABLE. All we have
93 ** to do here is record the root page number for that index.
94 */
95 Index *pIndex;
96 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
97 if( pIndex==0 || pIndex->tnum!=0 ){
98 /* This can occur if there exists an index on a TEMP table which
99 ** has the same name as another index on a permanent index. Since
100 ** the permanent table is hidden by the TEMP table, we can also
101 ** safely ignore the index on the permanent table.
102 */
103 /* Do Nothing */;
104 }else{
105 pIndex->tnum = atoi(argv[1]);
106 }
107 }
108 return 0;
109}
110
111/*
112** Attempt to read the database schema and initialize internal
113** data structures for a single database file. The index of the
114** database file is given by iDb. iDb==0 is used for the main
115** database. iDb==1 should never be used. iDb>=2 is used for
116** auxiliary databases. Return one of the SQLITE_ error codes to
117** indicate success or failure.
118*/
119static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
120 int rc;
121 BtCursor *curMain;
122 int size;
123 Table *pTab;
drhfdd6e852005-12-16 01:06:16 +0000124 Db *pDb;
drhece3c722006-09-23 20:36:01 +0000125 char const *azArg[4];
danielk1977fa256a32005-05-25 04:11:56 +0000126 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 );
danielk197714db2662006-01-09 16:12:04 +0000158 assert( db->aDb[iDb].pSchema );
danielk1977da184232006-01-05 11:34:32 +0000159
danielk1977fa256a32005-05-25 04:11:56 +0000160 /* 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;
drhece3c722006-09-23 20:36:01 +0000176 azArg[3] = 0;
danielk1977fa256a32005-05-25 04:11:56 +0000177 initData.db = db;
drhece3c722006-09-23 20:36:01 +0000178 initData.iDb = iDb;
danielk1977fa256a32005-05-25 04:11:56 +0000179 initData.pzErrMsg = pzErrMsg;
drhece3c722006-09-23 20:36:01 +0000180 rc = sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
drh15ca1df2006-07-26 13:43:30 +0000181 if( rc ){
danielk1977fa256a32005-05-25 04:11:56 +0000182 sqlite3SafetyOn(db);
drh15ca1df2006-07-26 13:43:30 +0000183 return initData.rc;
danielk1977fa256a32005-05-25 04:11:56 +0000184 }
185 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
186 if( pTab ){
187 pTab->readOnly = 1;
188 }
189 sqlite3SafetyOn(db);
190
191 /* Create a cursor to hold the database open
192 */
drhfdd6e852005-12-16 01:06:16 +0000193 pDb = &db->aDb[iDb];
194 if( pDb->pBt==0 ){
danielk1977b82e7ed2006-01-11 14:09:31 +0000195 if( !OMIT_TEMPDB && iDb==1 ){
196 DbSetProperty(db, 1, DB_SchemaLoaded);
197 }
danielk1977fa256a32005-05-25 04:11:56 +0000198 return SQLITE_OK;
199 }
drhfdd6e852005-12-16 01:06:16 +0000200 rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, 0, &curMain);
danielk1977fa256a32005-05-25 04:11:56 +0000201 if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
202 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
203 return rc;
204 }
205
206 /* Get the database meta information.
207 **
208 ** Meta values are as follows:
209 ** meta[0] Schema cookie. Changes with each schema change.
210 ** meta[1] File format of schema layer.
211 ** meta[2] Size of the page cache.
212 ** meta[3] Use freelist if 0. Autovacuum if greater than zero.
drh8159a352006-05-23 23:22:29 +0000213 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
danielk1977fa256a32005-05-25 04:11:56 +0000214 ** meta[5] The user cookie. Used by the application.
danielk1977418899a2007-06-24 10:14:00 +0000215 ** meta[6] Incremental-vacuum flag.
danielk1977fa256a32005-05-25 04:11:56 +0000216 ** meta[7]
217 ** meta[8]
218 ** meta[9]
219 **
drhf248e212006-01-25 22:50:38 +0000220 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
danielk1977fa256a32005-05-25 04:11:56 +0000221 ** the possible values of meta[4].
222 */
223 if( rc==SQLITE_OK ){
224 int i;
225 for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
drhfdd6e852005-12-16 01:06:16 +0000226 rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
danielk1977fa256a32005-05-25 04:11:56 +0000227 }
228 if( rc ){
229 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
230 sqlite3BtreeCloseCursor(curMain);
231 return rc;
232 }
233 }else{
234 memset(meta, 0, sizeof(meta));
235 }
danielk1977da184232006-01-05 11:34:32 +0000236 pDb->pSchema->schema_cookie = meta[0];
danielk1977fa256a32005-05-25 04:11:56 +0000237
238 /* If opening a non-empty database, check the text encoding. For the
239 ** main database, set sqlite3.enc to the encoding of the main database.
240 ** For an attached db, it is an error if the encoding is not the same
241 ** as sqlite3.enc.
242 */
243 if( meta[4] ){ /* text encoding */
244 if( iDb==0 ){
danielk197714db2662006-01-09 16:12:04 +0000245 /* If opening the main database, set ENC(db). */
246 ENC(db) = (u8)meta[4];
danielk1977b3bf5562006-01-10 17:58:23 +0000247 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0);
danielk1977fa256a32005-05-25 04:11:56 +0000248 }else{
danielk197714db2662006-01-09 16:12:04 +0000249 /* If opening an attached database, the encoding much match ENC(db) */
250 if( meta[4]!=ENC(db) ){
danielk1977fa256a32005-05-25 04:11:56 +0000251 sqlite3BtreeCloseCursor(curMain);
252 sqlite3SetString(pzErrMsg, "attached databases must use the same"
253 " text encoding as main database", (char*)0);
254 return SQLITE_ERROR;
255 }
256 }
danielk1977b82e7ed2006-01-11 14:09:31 +0000257 }else{
258 DbSetProperty(db, iDb, DB_Empty);
danielk1977fa256a32005-05-25 04:11:56 +0000259 }
danielk197714db2662006-01-09 16:12:04 +0000260 pDb->pSchema->enc = ENC(db);
danielk1977fa256a32005-05-25 04:11:56 +0000261
262 size = meta[2];
drhc797d4d2007-05-08 01:08:49 +0000263 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
danielk197714db2662006-01-09 16:12:04 +0000264 pDb->pSchema->cache_size = size;
265 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
danielk1977fa256a32005-05-25 04:11:56 +0000266
267 /*
268 ** file_format==1 Version 3.0.0.
drhfdd6e852005-12-16 01:06:16 +0000269 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
270 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
drhd946db02005-12-29 19:23:06 +0000271 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
danielk1977fa256a32005-05-25 04:11:56 +0000272 */
danielk1977da184232006-01-05 11:34:32 +0000273 pDb->pSchema->file_format = meta[1];
274 if( pDb->pSchema->file_format==0 ){
275 pDb->pSchema->file_format = 1;
drhfdd6e852005-12-16 01:06:16 +0000276 }
danielk1977da184232006-01-05 11:34:32 +0000277 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
danielk1977fa256a32005-05-25 04:11:56 +0000278 sqlite3BtreeCloseCursor(curMain);
279 sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
280 return SQLITE_ERROR;
281 }
282
danielk1977fa256a32005-05-25 04:11:56 +0000283
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;
danielk19771e536952007-08-16 10:09:01 +0000292 zSql = sqlite3MPrintf(db,
drhece3c722006-09-23 20:36:01 +0000293 "SELECT name, rootpage, sql FROM '%q'.%s",
294 db->aDb[iDb].zName, zMasterName);
danielk1977fa256a32005-05-25 04:11:56 +0000295 sqlite3SafetyOff(db);
296 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
drh15ca1df2006-07-26 13:43:30 +0000297 if( rc==SQLITE_ABORT ) rc = initData.rc;
danielk1977fa256a32005-05-25 04:11:56 +0000298 sqlite3SafetyOn(db);
drh17435752007-08-16 04:30:38 +0000299 sqlite3_free(zSql);
drh497e4462005-07-23 03:18:40 +0000300#ifndef SQLITE_OMIT_ANALYZE
301 if( rc==SQLITE_OK ){
302 sqlite3AnalysisLoad(db, iDb);
303 }
304#endif
danielk1977fa256a32005-05-25 04:11:56 +0000305 sqlite3BtreeCloseCursor(curMain);
306 }
drh17435752007-08-16 04:30:38 +0000307 if( db->mallocFailed ){
danielk19779e128002006-01-18 16:51:35 +0000308 /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */
danielk1977fa256a32005-05-25 04:11:56 +0000309 rc = SQLITE_NOMEM;
310 sqlite3ResetInternalSchema(db, 0);
311 }
danielk197734c68fb2007-03-14 15:37:04 +0000312 if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
313 /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
314 ** the schema loaded, even if errors occured. In this situation the
315 ** current sqlite3_prepare() operation will fail, but the following one
316 ** will attempt to compile the supplied statement against whatever subset
317 ** of the schema was loaded before the error occured. The primary
318 ** purpose of this is to allow access to the sqlite_master table
319 ** even when it's contents have been corrupted.
320 */
danielk1977fa256a32005-05-25 04:11:56 +0000321 DbSetProperty(db, iDb, DB_SchemaLoaded);
danielk197734c68fb2007-03-14 15:37:04 +0000322 rc = SQLITE_OK;
danielk1977fa256a32005-05-25 04:11:56 +0000323 }
324 return rc;
325}
326
327/*
328** Initialize all database files - the main database file, the file
329** used to store temporary tables, and any additional database files
330** created using ATTACH statements. Return a success code. If an
331** error occurs, write an error message into *pzErrMsg.
332**
danielk1977e7259292006-01-13 06:33:23 +0000333** After a database is initialized, the DB_SchemaLoaded bit is set
334** bit is set in the flags field of the Db structure. If the database
335** file was of zero-length, then the DB_Empty flag is also set.
danielk1977fa256a32005-05-25 04:11:56 +0000336*/
337int sqlite3Init(sqlite3 *db, char **pzErrMsg){
338 int i, rc;
danielk19775814c1a2007-08-13 14:41:19 +0000339 int commit_internal = !(db->flags&SQLITE_InternChanges);
danielk1977fa256a32005-05-25 04:11:56 +0000340
341 if( db->init.busy ) return SQLITE_OK;
danielk1977fa256a32005-05-25 04:11:56 +0000342 rc = SQLITE_OK;
343 db->init.busy = 1;
344 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
345 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
346 rc = sqlite3InitOne(db, i, pzErrMsg);
347 if( rc ){
348 sqlite3ResetInternalSchema(db, i);
349 }
350 }
351
352 /* Once all the other databases have been initialised, load the schema
353 ** for the TEMP database. This is loaded last, as the TEMP database
354 ** schema may contain references to objects in other databases.
355 */
356#ifndef SQLITE_OMIT_TEMPDB
357 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
358 rc = sqlite3InitOne(db, 1, pzErrMsg);
359 if( rc ){
360 sqlite3ResetInternalSchema(db, 1);
361 }
362 }
363#endif
364
365 db->init.busy = 0;
danielk19775814c1a2007-08-13 14:41:19 +0000366 if( rc==SQLITE_OK && commit_internal ){
danielk1977fa256a32005-05-25 04:11:56 +0000367 sqlite3CommitInternalChanges(db);
368 }
369
danielk1977b82e7ed2006-01-11 14:09:31 +0000370 return rc;
danielk1977fa256a32005-05-25 04:11:56 +0000371}
372
373/*
374** This routine is a no-op if the database schema is already initialised.
375** Otherwise, the schema is loaded. An error code is returned.
376*/
377int sqlite3ReadSchema(Parse *pParse){
378 int rc = SQLITE_OK;
379 sqlite3 *db = pParse->db;
380 if( !db->init.busy ){
danielk1977b82e7ed2006-01-11 14:09:31 +0000381 rc = sqlite3Init(db, &pParse->zErrMsg);
danielk1977fa256a32005-05-25 04:11:56 +0000382 }
danielk1977fa256a32005-05-25 04:11:56 +0000383 if( rc!=SQLITE_OK ){
384 pParse->rc = rc;
385 pParse->nErr++;
386 }
387 return rc;
388}
389
390
391/*
392** Check schema cookies in all databases. If any cookie is out
393** of date, return 0. If all schema cookies are current, return 1.
394*/
395static int schemaIsValid(sqlite3 *db){
396 int iDb;
397 int rc;
398 BtCursor *curTemp;
399 int cookie;
400 int allOk = 1;
401
402 for(iDb=0; allOk && iDb<db->nDb; iDb++){
403 Btree *pBt;
404 pBt = db->aDb[iDb].pBt;
405 if( pBt==0 ) continue;
406 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
407 if( rc==SQLITE_OK ){
408 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
danielk1977da184232006-01-05 11:34:32 +0000409 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].pSchema->schema_cookie ){
danielk1977fa256a32005-05-25 04:11:56 +0000410 allOk = 0;
411 }
412 sqlite3BtreeCloseCursor(curTemp);
413 }
414 }
415 return allOk;
416}
417
418/*
drhf248e212006-01-25 22:50:38 +0000419** Convert a schema pointer into the iDb index that indicates
420** which database file in db->aDb[] the schema refers to.
421**
422** If the same database is attached more than once, the first
423** attached database is returned.
424*/
danielk1977e501b892006-01-09 06:29:47 +0000425int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
drh198bf392006-01-06 21:52:49 +0000426 int i = -1000000;
427
428 /* If pSchema is NULL, then return -1000000. This happens when code in
429 ** expr.c is trying to resolve a reference to a transient table (i.e. one
430 ** created by a sub-select). In this case the return value of this
431 ** function should never be used.
432 **
433 ** We return -1000000 instead of the more usual -1 simply because using
434 ** -1000000 as incorrectly using -1000000 index into db->aDb[] is much
435 ** more likely to cause a segfault than -1 (of course there are assert()
436 ** statements too, but it never hurts to play the odds).
437 */
438 if( pSchema ){
439 for(i=0; i<db->nDb; i++){
440 if( db->aDb[i].pSchema==pSchema ){
441 break;
442 }
443 }
444 assert( i>=0 &&i>=0 && i<db->nDb );
445 }
446 return i;
447}
448
449/*
danielk1977fa256a32005-05-25 04:11:56 +0000450** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
451*/
drhb900aaf2006-11-09 00:24:53 +0000452int sqlite3Prepare(
danielk1977fa256a32005-05-25 04:11:56 +0000453 sqlite3 *db, /* Database handle. */
454 const char *zSql, /* UTF-8 encoded SQL statement. */
455 int nBytes, /* Length of zSql in bytes. */
drhb900aaf2006-11-09 00:24:53 +0000456 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */
danielk1977fa256a32005-05-25 04:11:56 +0000457 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
drhb900aaf2006-11-09 00:24:53 +0000458 const char **pzTail /* OUT: End of parsed string */
danielk1977fa256a32005-05-25 04:11:56 +0000459){
460 Parse sParse;
461 char *zErrMsg = 0;
462 int rc = SQLITE_OK;
danielk1977c87d34d2006-01-06 13:00:28 +0000463 int i;
danielk1977fa256a32005-05-25 04:11:56 +0000464
danielk1977fa256a32005-05-25 04:11:56 +0000465 assert( ppStmt );
466 *ppStmt = 0;
467 if( sqlite3SafetyOn(db) ){
468 return SQLITE_MISUSE;
469 }
drh86f8c192007-08-22 00:39:19 +0000470 assert( !db->mallocFailed );
drhb21c8cd2007-08-21 19:33:56 +0000471 assert( sqlite3_mutex_held(db->mutex) );
danielk1977fa256a32005-05-25 04:11:56 +0000472
danielk1977c87d34d2006-01-06 13:00:28 +0000473 /* If any attached database schemas are locked, do not proceed with
474 ** compilation. Instead return SQLITE_LOCKED immediately.
475 */
476 for(i=0; i<db->nDb; i++) {
477 Btree *pBt = db->aDb[i].pBt;
478 if( pBt && sqlite3BtreeSchemaLocked(pBt) ){
479 const char *zDb = db->aDb[i].zName;
480 sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb);
481 sqlite3SafetyOff(db);
482 return SQLITE_LOCKED;
483 }
484 }
485
danielk1977fa256a32005-05-25 04:11:56 +0000486 memset(&sParse, 0, sizeof(sParse));
487 sParse.db = db;
drh9051a422006-01-30 22:35:43 +0000488 if( nBytes>=0 && zSql[nBytes]!=0 ){
drhe5c941b2007-05-08 13:58:26 +0000489 char *zSqlCopy;
490 if( nBytes>SQLITE_MAX_SQL_LENGTH ){
491 return SQLITE_TOOBIG;
492 }
drh17435752007-08-16 04:30:38 +0000493 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
drh276fdbf2007-04-30 21:39:16 +0000494 if( zSqlCopy ){
495 sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
drh17435752007-08-16 04:30:38 +0000496 sqlite3_free(zSqlCopy);
drh276fdbf2007-04-30 21:39:16 +0000497 }
498 sParse.zTail = &zSql[nBytes];
drh9051a422006-01-30 22:35:43 +0000499 }else{
500 sqlite3RunParser(&sParse, zSql, &zErrMsg);
501 }
danielk1977fa256a32005-05-25 04:11:56 +0000502
drh17435752007-08-16 04:30:38 +0000503 if( db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +0000504 sParse.rc = SQLITE_NOMEM;
danielk1977fa256a32005-05-25 04:11:56 +0000505 }
506 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
drh4d91a702006-01-04 15:54:36 +0000507 if( sParse.checkSchema && !schemaIsValid(db) ){
danielk1977fa256a32005-05-25 04:11:56 +0000508 sParse.rc = SQLITE_SCHEMA;
509 }
510 if( sParse.rc==SQLITE_SCHEMA ){
511 sqlite3ResetInternalSchema(db, 0);
512 }
drh17435752007-08-16 04:30:38 +0000513 if( db->mallocFailed ){
drh344a6272006-06-26 12:50:09 +0000514 sParse.rc = SQLITE_NOMEM;
515 }
drhb900aaf2006-11-09 00:24:53 +0000516 if( pzTail ){
517 *pzTail = sParse.zTail;
518 }
danielk1977fa256a32005-05-25 04:11:56 +0000519 rc = sParse.rc;
520
521#ifndef SQLITE_OMIT_EXPLAIN
522 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
drhecc92422005-09-10 16:46:12 +0000523 if( sParse.explain==2 ){
524 sqlite3VdbeSetNumCols(sParse.pVdbe, 3);
danielk1977955de522006-02-10 02:27:42 +0000525 sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "order", P3_STATIC);
526 sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "from", P3_STATIC);
527 sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "detail", P3_STATIC);
drhecc92422005-09-10 16:46:12 +0000528 }else{
529 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
danielk1977955de522006-02-10 02:27:42 +0000530 sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "addr", P3_STATIC);
531 sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "opcode", P3_STATIC);
532 sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "p1", P3_STATIC);
533 sqlite3VdbeSetColName(sParse.pVdbe, 3, COLNAME_NAME, "p2", P3_STATIC);
534 sqlite3VdbeSetColName(sParse.pVdbe, 4, COLNAME_NAME, "p3", P3_STATIC);
drhecc92422005-09-10 16:46:12 +0000535 }
drhb900aaf2006-11-09 00:24:53 +0000536 }
danielk1977fa256a32005-05-25 04:11:56 +0000537#endif
538
danielk1977fa256a32005-05-25 04:11:56 +0000539 if( sqlite3SafetyOff(db) ){
540 rc = SQLITE_MISUSE;
541 }
danielk19777e29e952007-04-19 11:09:01 +0000542
543 if( saveSqlFlag ){
544 sqlite3VdbeSetSql(sParse.pVdbe, zSql, sParse.zTail - zSql);
545 }
drh17435752007-08-16 04:30:38 +0000546 if( rc!=SQLITE_OK || db->mallocFailed ){
danielk1977fa256a32005-05-25 04:11:56 +0000547 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
danielk19777e29e952007-04-19 11:09:01 +0000548 assert(!(*ppStmt));
549 }else{
550 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
danielk1977fa256a32005-05-25 04:11:56 +0000551 }
552
553 if( zErrMsg ){
554 sqlite3Error(db, rc, "%s", zErrMsg);
drh17435752007-08-16 04:30:38 +0000555 sqlite3_free(zErrMsg);
danielk1977fa256a32005-05-25 04:11:56 +0000556 }else{
557 sqlite3Error(db, rc, 0);
558 }
danielk1977261919c2005-12-06 12:52:59 +0000559
danielk197754f01982006-01-18 15:25:17 +0000560 rc = sqlite3ApiExit(db, rc);
danielk19771e536952007-08-16 10:09:01 +0000561 /* sqlite3ReleaseThreadData(); */
drh4ac285a2006-09-15 07:28:50 +0000562 assert( (rc&db->errMask)==rc );
danielk1977fa256a32005-05-25 04:11:56 +0000563 return rc;
564}
drhb21c8cd2007-08-21 19:33:56 +0000565static int sqlite3LockAndPrepare(
566 sqlite3 *db, /* Database handle. */
567 const char *zSql, /* UTF-8 encoded SQL statement. */
568 int nBytes, /* Length of zSql in bytes. */
569 int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */
570 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
571 const char **pzTail /* OUT: End of parsed string */
572){
573 int rc;
drh27641702007-08-22 02:56:42 +0000574 if( sqlite3SafetyCheck(db) ){
575 return SQLITE_MISUSE;
576 }
drhb21c8cd2007-08-21 19:33:56 +0000577 sqlite3_mutex_enter(db->mutex);
578 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, ppStmt, pzTail);
579 sqlite3_mutex_leave(db->mutex);
580 return rc;
581}
danielk1977fa256a32005-05-25 04:11:56 +0000582
drhb900aaf2006-11-09 00:24:53 +0000583/*
584** Rerun the compilation of a statement after a schema change.
585** Return true if the statement was recompiled successfully.
586** Return false if there is an error of some kind.
587*/
588int sqlite3Reprepare(Vdbe *p){
589 int rc;
drh4f0c5872007-03-26 22:05:01 +0000590 sqlite3_stmt *pNew;
drhb900aaf2006-11-09 00:24:53 +0000591 const char *zSql;
592 sqlite3 *db;
drhb21c8cd2007-08-21 19:33:56 +0000593
drhb900aaf2006-11-09 00:24:53 +0000594 zSql = sqlite3VdbeGetSql(p);
595 if( zSql==0 ){
596 return 0;
597 }
598 db = sqlite3VdbeDb(p);
drhb21c8cd2007-08-21 19:33:56 +0000599 assert( sqlite3_mutex_held(db->mutex) );
drh4f0c5872007-03-26 22:05:01 +0000600 rc = sqlite3Prepare(db, zSql, -1, 0, &pNew, 0);
drhb900aaf2006-11-09 00:24:53 +0000601 if( rc ){
602 assert( pNew==0 );
603 return 0;
604 }else{
605 assert( pNew!=0 );
606 }
drh4f0c5872007-03-26 22:05:01 +0000607 sqlite3VdbeSwap((Vdbe*)pNew, p);
608 sqlite3_transfer_bindings(pNew, (sqlite3_stmt*)p);
609 sqlite3VdbeResetStepResult((Vdbe*)pNew);
610 sqlite3VdbeFinalize((Vdbe*)pNew);
drhb900aaf2006-11-09 00:24:53 +0000611 return 1;
612}
613
614
615/*
616** Two versions of the official API. Legacy and new use. In the legacy
617** version, the original SQL text is not saved in the prepared statement
618** and so if a schema change occurs, SQLITE_SCHEMA is returned by
619** sqlite3_step(). In the new version, the original SQL text is retained
620** and the statement is automatically recompiled if an schema change
621** occurs.
622*/
623int sqlite3_prepare(
624 sqlite3 *db, /* Database handle. */
625 const char *zSql, /* UTF-8 encoded SQL statement. */
626 int nBytes, /* Length of zSql in bytes. */
627 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
628 const char **pzTail /* OUT: End of parsed string */
629){
drhb21c8cd2007-08-21 19:33:56 +0000630 return sqlite3LockAndPrepare(db,zSql,nBytes,0,ppStmt,pzTail);
drhb900aaf2006-11-09 00:24:53 +0000631}
632int sqlite3_prepare_v2(
633 sqlite3 *db, /* Database handle. */
634 const char *zSql, /* UTF-8 encoded SQL statement. */
635 int nBytes, /* Length of zSql in bytes. */
636 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
637 const char **pzTail /* OUT: End of parsed string */
638){
drhb21c8cd2007-08-21 19:33:56 +0000639 return sqlite3LockAndPrepare(db,zSql,nBytes,1,ppStmt,pzTail);
drhb900aaf2006-11-09 00:24:53 +0000640}
641
642
danielk1977fa256a32005-05-25 04:11:56 +0000643#ifndef SQLITE_OMIT_UTF16
644/*
645** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
646*/
drhb900aaf2006-11-09 00:24:53 +0000647static int sqlite3Prepare16(
danielk1977fa256a32005-05-25 04:11:56 +0000648 sqlite3 *db, /* Database handle. */
649 const void *zSql, /* UTF-8 encoded SQL statement. */
650 int nBytes, /* Length of zSql in bytes. */
drhb900aaf2006-11-09 00:24:53 +0000651 int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */
danielk1977fa256a32005-05-25 04:11:56 +0000652 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
653 const void **pzTail /* OUT: End of parsed string */
654){
655 /* This function currently works by first transforming the UTF-16
656 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
657 ** tricky bit is figuring out the pointer to return in *pzTail.
658 */
danielk197754f01982006-01-18 15:25:17 +0000659 char *zSql8;
danielk1977c87d34d2006-01-06 13:00:28 +0000660 const char *zTail8 = 0;
danielk197754f01982006-01-18 15:25:17 +0000661 int rc = SQLITE_OK;
danielk1977fa256a32005-05-25 04:11:56 +0000662
663 if( sqlite3SafetyCheck(db) ){
664 return SQLITE_MISUSE;
665 }
drhb21c8cd2007-08-21 19:33:56 +0000666 sqlite3_mutex_enter(db->mutex);
danielk19771e536952007-08-16 10:09:01 +0000667 zSql8 = sqlite3Utf16to8(db, zSql, nBytes);
danielk197754f01982006-01-18 15:25:17 +0000668 if( zSql8 ){
drhb900aaf2006-11-09 00:24:53 +0000669 rc = sqlite3Prepare(db, zSql8, -1, saveSqlFlag, ppStmt, &zTail8);
danielk1977fa256a32005-05-25 04:11:56 +0000670 }
danielk1977fa256a32005-05-25 04:11:56 +0000671
672 if( zTail8 && pzTail ){
673 /* If sqlite3_prepare returns a tail pointer, we calculate the
674 ** equivalent pointer into the UTF-16 string by counting the unicode
675 ** characters between zSql8 and zTail8, and then returning a pointer
676 ** the same number of characters into the UTF-16 string.
677 */
drhee858132007-05-08 20:37:38 +0000678 int chars_parsed = sqlite3Utf8CharLen(zSql8, zTail8-zSql8);
679 *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
danielk1977fa256a32005-05-25 04:11:56 +0000680 }
drh17435752007-08-16 04:30:38 +0000681 sqlite3_free(zSql8);
drhb21c8cd2007-08-21 19:33:56 +0000682 rc = sqlite3ApiExit(db, rc);
683 sqlite3_mutex_leave(db->mutex);
684 return rc;
danielk1977fa256a32005-05-25 04:11:56 +0000685}
drhb900aaf2006-11-09 00:24:53 +0000686
687/*
688** Two versions of the official API. Legacy and new use. In the legacy
689** version, the original SQL text is not saved in the prepared statement
690** and so if a schema change occurs, SQLITE_SCHEMA is returned by
691** sqlite3_step(). In the new version, the original SQL text is retained
692** and the statement is automatically recompiled if an schema change
693** occurs.
694*/
695int sqlite3_prepare16(
696 sqlite3 *db, /* Database handle. */
697 const void *zSql, /* UTF-8 encoded SQL statement. */
698 int nBytes, /* Length of zSql in bytes. */
699 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
700 const void **pzTail /* OUT: End of parsed string */
701){
702 return sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
703}
704int sqlite3_prepare16_v2(
705 sqlite3 *db, /* Database handle. */
706 const void *zSql, /* UTF-8 encoded SQL statement. */
707 int nBytes, /* Length of zSql in bytes. */
708 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
709 const void **pzTail /* OUT: End of parsed string */
710){
711 return sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
712}
713
danielk1977fa256a32005-05-25 04:11:56 +0000714#endif /* SQLITE_OMIT_UTF16 */