blob: 376fd4e9b6ec9a204c48971a8726ae5a1ee8db1f [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**
drh29278e32007-08-21 10:44:15 +000016** $Id: prepare.c,v 1.55 2007/08/21 10:44:16 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
danielk1977efaaf572006-01-16 11:29:19 +0000465 /* Assert that malloc() has not failed */
drh17435752007-08-16 04:30:38 +0000466 assert( !db->mallocFailed );
danielk1977fa256a32005-05-25 04:11:56 +0000467
468 assert( ppStmt );
469 *ppStmt = 0;
470 if( sqlite3SafetyOn(db) ){
471 return SQLITE_MISUSE;
472 }
473
danielk1977c87d34d2006-01-06 13:00:28 +0000474 /* If any attached database schemas are locked, do not proceed with
475 ** compilation. Instead return SQLITE_LOCKED immediately.
476 */
477 for(i=0; i<db->nDb; i++) {
478 Btree *pBt = db->aDb[i].pBt;
479 if( pBt && sqlite3BtreeSchemaLocked(pBt) ){
480 const char *zDb = db->aDb[i].zName;
481 sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb);
482 sqlite3SafetyOff(db);
483 return SQLITE_LOCKED;
484 }
485 }
486
danielk1977fa256a32005-05-25 04:11:56 +0000487 memset(&sParse, 0, sizeof(sParse));
488 sParse.db = db;
drh9051a422006-01-30 22:35:43 +0000489 if( nBytes>=0 && zSql[nBytes]!=0 ){
drhe5c941b2007-05-08 13:58:26 +0000490 char *zSqlCopy;
491 if( nBytes>SQLITE_MAX_SQL_LENGTH ){
492 return SQLITE_TOOBIG;
493 }
drh17435752007-08-16 04:30:38 +0000494 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
drh276fdbf2007-04-30 21:39:16 +0000495 if( zSqlCopy ){
496 sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
drh17435752007-08-16 04:30:38 +0000497 sqlite3_free(zSqlCopy);
drh276fdbf2007-04-30 21:39:16 +0000498 }
499 sParse.zTail = &zSql[nBytes];
drh9051a422006-01-30 22:35:43 +0000500 }else{
501 sqlite3RunParser(&sParse, zSql, &zErrMsg);
502 }
danielk1977fa256a32005-05-25 04:11:56 +0000503
drh17435752007-08-16 04:30:38 +0000504 if( db->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +0000505 sParse.rc = SQLITE_NOMEM;
danielk1977fa256a32005-05-25 04:11:56 +0000506 }
507 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
drh4d91a702006-01-04 15:54:36 +0000508 if( sParse.checkSchema && !schemaIsValid(db) ){
danielk1977fa256a32005-05-25 04:11:56 +0000509 sParse.rc = SQLITE_SCHEMA;
510 }
511 if( sParse.rc==SQLITE_SCHEMA ){
512 sqlite3ResetInternalSchema(db, 0);
513 }
drh17435752007-08-16 04:30:38 +0000514 if( db->mallocFailed ){
drh344a6272006-06-26 12:50:09 +0000515 sParse.rc = SQLITE_NOMEM;
516 }
drhb900aaf2006-11-09 00:24:53 +0000517 if( pzTail ){
518 *pzTail = sParse.zTail;
519 }
danielk1977fa256a32005-05-25 04:11:56 +0000520 rc = sParse.rc;
521
522#ifndef SQLITE_OMIT_EXPLAIN
523 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
drhecc92422005-09-10 16:46:12 +0000524 if( sParse.explain==2 ){
525 sqlite3VdbeSetNumCols(sParse.pVdbe, 3);
danielk1977955de522006-02-10 02:27:42 +0000526 sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "order", P3_STATIC);
527 sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "from", P3_STATIC);
528 sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "detail", P3_STATIC);
drhecc92422005-09-10 16:46:12 +0000529 }else{
530 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
danielk1977955de522006-02-10 02:27:42 +0000531 sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "addr", P3_STATIC);
532 sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "opcode", P3_STATIC);
533 sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "p1", P3_STATIC);
534 sqlite3VdbeSetColName(sParse.pVdbe, 3, COLNAME_NAME, "p2", P3_STATIC);
535 sqlite3VdbeSetColName(sParse.pVdbe, 4, COLNAME_NAME, "p3", P3_STATIC);
drhecc92422005-09-10 16:46:12 +0000536 }
drhb900aaf2006-11-09 00:24:53 +0000537 }
danielk1977fa256a32005-05-25 04:11:56 +0000538#endif
539
danielk1977fa256a32005-05-25 04:11:56 +0000540 if( sqlite3SafetyOff(db) ){
541 rc = SQLITE_MISUSE;
542 }
danielk19777e29e952007-04-19 11:09:01 +0000543
544 if( saveSqlFlag ){
545 sqlite3VdbeSetSql(sParse.pVdbe, zSql, sParse.zTail - zSql);
546 }
drh17435752007-08-16 04:30:38 +0000547 if( rc!=SQLITE_OK || db->mallocFailed ){
danielk1977fa256a32005-05-25 04:11:56 +0000548 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
danielk19777e29e952007-04-19 11:09:01 +0000549 assert(!(*ppStmt));
550 }else{
551 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
danielk1977fa256a32005-05-25 04:11:56 +0000552 }
553
554 if( zErrMsg ){
555 sqlite3Error(db, rc, "%s", zErrMsg);
drh17435752007-08-16 04:30:38 +0000556 sqlite3_free(zErrMsg);
danielk1977fa256a32005-05-25 04:11:56 +0000557 }else{
558 sqlite3Error(db, rc, 0);
559 }
danielk1977261919c2005-12-06 12:52:59 +0000560
danielk197754f01982006-01-18 15:25:17 +0000561 rc = sqlite3ApiExit(db, rc);
danielk19771e536952007-08-16 10:09:01 +0000562 /* sqlite3ReleaseThreadData(); */
drh4ac285a2006-09-15 07:28:50 +0000563 assert( (rc&db->errMask)==rc );
danielk1977fa256a32005-05-25 04:11:56 +0000564 return rc;
565}
566
drhb900aaf2006-11-09 00:24:53 +0000567/*
568** Rerun the compilation of a statement after a schema change.
569** Return true if the statement was recompiled successfully.
570** Return false if there is an error of some kind.
571*/
572int sqlite3Reprepare(Vdbe *p){
573 int rc;
drh4f0c5872007-03-26 22:05:01 +0000574 sqlite3_stmt *pNew;
drhb900aaf2006-11-09 00:24:53 +0000575 const char *zSql;
576 sqlite3 *db;
577
578 zSql = sqlite3VdbeGetSql(p);
579 if( zSql==0 ){
580 return 0;
581 }
582 db = sqlite3VdbeDb(p);
drh4f0c5872007-03-26 22:05:01 +0000583 rc = sqlite3Prepare(db, zSql, -1, 0, &pNew, 0);
drhb900aaf2006-11-09 00:24:53 +0000584 if( rc ){
585 assert( pNew==0 );
586 return 0;
587 }else{
588 assert( pNew!=0 );
589 }
drh4f0c5872007-03-26 22:05:01 +0000590 sqlite3VdbeSwap((Vdbe*)pNew, p);
591 sqlite3_transfer_bindings(pNew, (sqlite3_stmt*)p);
592 sqlite3VdbeResetStepResult((Vdbe*)pNew);
593 sqlite3VdbeFinalize((Vdbe*)pNew);
drhb900aaf2006-11-09 00:24:53 +0000594 return 1;
595}
596
597
598/*
599** Two versions of the official API. Legacy and new use. In the legacy
600** version, the original SQL text is not saved in the prepared statement
601** and so if a schema change occurs, SQLITE_SCHEMA is returned by
602** sqlite3_step(). In the new version, the original SQL text is retained
603** and the statement is automatically recompiled if an schema change
604** occurs.
605*/
606int sqlite3_prepare(
607 sqlite3 *db, /* Database handle. */
608 const char *zSql, /* UTF-8 encoded SQL statement. */
609 int nBytes, /* Length of zSql in bytes. */
610 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
611 const char **pzTail /* OUT: End of parsed string */
612){
613 return sqlite3Prepare(db,zSql,nBytes,0,ppStmt,pzTail);
614}
615int sqlite3_prepare_v2(
616 sqlite3 *db, /* Database handle. */
617 const char *zSql, /* UTF-8 encoded SQL statement. */
618 int nBytes, /* Length of zSql in bytes. */
619 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
620 const char **pzTail /* OUT: End of parsed string */
621){
622 return sqlite3Prepare(db,zSql,nBytes,1,ppStmt,pzTail);
623}
624
625
danielk1977fa256a32005-05-25 04:11:56 +0000626#ifndef SQLITE_OMIT_UTF16
627/*
628** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
629*/
drhb900aaf2006-11-09 00:24:53 +0000630static int sqlite3Prepare16(
danielk1977fa256a32005-05-25 04:11:56 +0000631 sqlite3 *db, /* Database handle. */
632 const void *zSql, /* UTF-8 encoded SQL statement. */
633 int nBytes, /* Length of zSql in bytes. */
drhb900aaf2006-11-09 00:24:53 +0000634 int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */
danielk1977fa256a32005-05-25 04:11:56 +0000635 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
636 const void **pzTail /* OUT: End of parsed string */
637){
638 /* This function currently works by first transforming the UTF-16
639 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
640 ** tricky bit is figuring out the pointer to return in *pzTail.
641 */
danielk197754f01982006-01-18 15:25:17 +0000642 char *zSql8;
danielk1977c87d34d2006-01-06 13:00:28 +0000643 const char *zTail8 = 0;
danielk197754f01982006-01-18 15:25:17 +0000644 int rc = SQLITE_OK;
danielk1977fa256a32005-05-25 04:11:56 +0000645
646 if( sqlite3SafetyCheck(db) ){
647 return SQLITE_MISUSE;
648 }
danielk19771e536952007-08-16 10:09:01 +0000649 zSql8 = sqlite3Utf16to8(db, zSql, nBytes);
danielk197754f01982006-01-18 15:25:17 +0000650 if( zSql8 ){
drhb900aaf2006-11-09 00:24:53 +0000651 rc = sqlite3Prepare(db, zSql8, -1, saveSqlFlag, ppStmt, &zTail8);
danielk1977fa256a32005-05-25 04:11:56 +0000652 }
danielk1977fa256a32005-05-25 04:11:56 +0000653
654 if( zTail8 && pzTail ){
655 /* If sqlite3_prepare returns a tail pointer, we calculate the
656 ** equivalent pointer into the UTF-16 string by counting the unicode
657 ** characters between zSql8 and zTail8, and then returning a pointer
658 ** the same number of characters into the UTF-16 string.
659 */
drhee858132007-05-08 20:37:38 +0000660 int chars_parsed = sqlite3Utf8CharLen(zSql8, zTail8-zSql8);
661 *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
danielk1977fa256a32005-05-25 04:11:56 +0000662 }
drh17435752007-08-16 04:30:38 +0000663 sqlite3_free(zSql8);
danielk197754f01982006-01-18 15:25:17 +0000664 return sqlite3ApiExit(db, rc);
danielk1977fa256a32005-05-25 04:11:56 +0000665}
drhb900aaf2006-11-09 00:24:53 +0000666
667/*
668** Two versions of the official API. Legacy and new use. In the legacy
669** version, the original SQL text is not saved in the prepared statement
670** and so if a schema change occurs, SQLITE_SCHEMA is returned by
671** sqlite3_step(). In the new version, the original SQL text is retained
672** and the statement is automatically recompiled if an schema change
673** occurs.
674*/
675int sqlite3_prepare16(
676 sqlite3 *db, /* Database handle. */
677 const void *zSql, /* UTF-8 encoded SQL statement. */
678 int nBytes, /* Length of zSql in bytes. */
679 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
680 const void **pzTail /* OUT: End of parsed string */
681){
682 return sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
683}
684int sqlite3_prepare16_v2(
685 sqlite3 *db, /* Database handle. */
686 const void *zSql, /* UTF-8 encoded SQL statement. */
687 int nBytes, /* Length of zSql in bytes. */
688 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
689 const void **pzTail /* OUT: End of parsed string */
690){
691 return sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
692}
693
danielk1977fa256a32005-05-25 04:11:56 +0000694#endif /* SQLITE_OMIT_UTF16 */