blob: c59d7fc82ae2c61f486488ae56da8a722e7985f5 [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**
danielk1977b3bf5562006-01-10 17:58:23 +000016** $Id: prepare.c,v 1.18 2006/01/10 17:58:23 danielk1977 Exp $
danielk1977fa256a32005-05-25 04:11:56 +000017*/
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*/
26static void corruptSchema(InitData *pData, const char *zExtra){
danielk1977e501b892006-01-09 06:29:47 +000027 if( !sqlite3ThreadData()->mallocFailed ){
danielk1977fa256a32005-05-25 04:11:56 +000028 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*/
47int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){
48 InitData *pData = (InitData*)pInit;
49 sqlite3 *db = pData->db;
50 int iDb;
51
danielk1977e501b892006-01-09 06:29:47 +000052 if( sqlite3ThreadData()->mallocFailed ){
danielk1977da184232006-01-05 11:34:32 +000053 return SQLITE_NOMEM;
54 }
55
danielk1977fa256a32005-05-25 04:11:56 +000056 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 if( SQLITE_OK!=rc ){
danielk1977261919c2005-12-06 12:52:59 +000078 if( rc==SQLITE_NOMEM ){
danielk1977e501b892006-01-09 06:29:47 +000079 sqlite3ThreadData()->mallocFailed = 1;
danielk1977261919c2005-12-06 12:52:59 +000080 }else{
81 corruptSchema(pData, zErr);
82 }
danielk1977fa256a32005-05-25 04:11:56 +000083 sqlite3_free(zErr);
84 return rc;
85 }
86 }else{
87 /* If the SQL column is blank it means this is an index that
88 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
89 ** constraint for a CREATE TABLE. The index should have already
90 ** been created when we processed the CREATE TABLE. All we have
91 ** to do here is record the root page number for that index.
92 */
93 Index *pIndex;
94 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
95 if( pIndex==0 || pIndex->tnum!=0 ){
96 /* This can occur if there exists an index on a TEMP table which
97 ** has the same name as another index on a permanent index. Since
98 ** the permanent table is hidden by the TEMP table, we can also
99 ** safely ignore the index on the permanent table.
100 */
101 /* Do Nothing */;
102 }else{
103 pIndex->tnum = atoi(argv[1]);
104 }
105 }
106 return 0;
107}
108
109/*
110** Attempt to read the database schema and initialize internal
111** data structures for a single database file. The index of the
112** database file is given by iDb. iDb==0 is used for the main
113** database. iDb==1 should never be used. iDb>=2 is used for
114** auxiliary databases. Return one of the SQLITE_ error codes to
115** indicate success or failure.
116*/
117static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
118 int rc;
119 BtCursor *curMain;
120 int size;
121 Table *pTab;
drhfdd6e852005-12-16 01:06:16 +0000122 Db *pDb;
danielk1977fa256a32005-05-25 04:11:56 +0000123 char const *azArg[5];
124 char zDbNum[30];
125 int meta[10];
126 InitData initData;
127 char const *zMasterSchema;
128 char const *zMasterName = SCHEMA_TABLE(iDb);
129
130 /*
131 ** The master database table has a structure like this
132 */
133 static const char master_schema[] =
134 "CREATE TABLE sqlite_master(\n"
135 " type text,\n"
136 " name text,\n"
137 " tbl_name text,\n"
138 " rootpage integer,\n"
139 " sql text\n"
140 ")"
141 ;
142#ifndef SQLITE_OMIT_TEMPDB
143 static const char temp_master_schema[] =
144 "CREATE TEMP TABLE sqlite_temp_master(\n"
145 " type text,\n"
146 " name text,\n"
147 " tbl_name text,\n"
148 " rootpage integer,\n"
149 " sql text\n"
150 ")"
151 ;
152#else
153 #define temp_master_schema 0
154#endif
155
156 assert( iDb>=0 && iDb<db->nDb );
157
danielk197714db2662006-01-09 16:12:04 +0000158 assert( db->aDb[iDb].pSchema );
159#if 0
danielk1977da184232006-01-05 11:34:32 +0000160 if( 0==db->aDb[iDb].pSchema ){
danielk197714db2662006-01-09 16:12:04 +0000161 Schema *pS = sqlite3SchemaGet(db->aDb[iDb].pBt);
danielk1977da184232006-01-05 11:34:32 +0000162 db->aDb[iDb].pSchema = pS;
163 if( !pS ){
164 return SQLITE_NOMEM;
165 }else if( pS->file_format!=0 ){
danielk197714db2662006-01-09 16:12:04 +0000166 /* This means that the shared-schema associated with the the btree
167 ** is already open and populated.
168 */
169 if( pS->enc!=ENC(db) ){
170 sqlite3SetString(pzErrMsg, "attached databases must use the same"
171 " text encoding as main database", (char*)0);
172 return SQLITE_ERROR;
173 }
danielk1977da184232006-01-05 11:34:32 +0000174 return SQLITE_OK;
danielk1977da184232006-01-05 11:34:32 +0000175 }
176 }
danielk197714db2662006-01-09 16:12:04 +0000177#endif
danielk1977da184232006-01-05 11:34:32 +0000178
danielk1977fa256a32005-05-25 04:11:56 +0000179 /* zMasterSchema and zInitScript are set to point at the master schema
180 ** and initialisation script appropriate for the database being
181 ** initialised. zMasterName is the name of the master table.
182 */
183 if( !OMIT_TEMPDB && iDb==1 ){
184 zMasterSchema = temp_master_schema;
185 }else{
186 zMasterSchema = master_schema;
187 }
188 zMasterName = SCHEMA_TABLE(iDb);
189
190 /* Construct the schema tables. */
191 sqlite3SafetyOff(db);
192 azArg[0] = zMasterName;
193 azArg[1] = "1";
194 azArg[2] = zMasterSchema;
195 sprintf(zDbNum, "%d", iDb);
196 azArg[3] = zDbNum;
197 azArg[4] = 0;
198 initData.db = db;
199 initData.pzErrMsg = pzErrMsg;
200 rc = sqlite3InitCallback(&initData, 4, (char **)azArg, 0);
201 if( rc!=SQLITE_OK ){
202 sqlite3SafetyOn(db);
203 return rc;
204 }
205 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
206 if( pTab ){
207 pTab->readOnly = 1;
208 }
209 sqlite3SafetyOn(db);
210
211 /* Create a cursor to hold the database open
212 */
drhfdd6e852005-12-16 01:06:16 +0000213 pDb = &db->aDb[iDb];
214 if( pDb->pBt==0 ){
danielk1977fa256a32005-05-25 04:11:56 +0000215 if( !OMIT_TEMPDB && iDb==1 ) DbSetProperty(db, 1, DB_SchemaLoaded);
216 return SQLITE_OK;
217 }
drhfdd6e852005-12-16 01:06:16 +0000218 rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, 0, &curMain);
danielk1977fa256a32005-05-25 04:11:56 +0000219 if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
220 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
221 return rc;
222 }
223
224 /* Get the database meta information.
225 **
226 ** Meta values are as follows:
227 ** meta[0] Schema cookie. Changes with each schema change.
228 ** meta[1] File format of schema layer.
229 ** meta[2] Size of the page cache.
230 ** meta[3] Use freelist if 0. Autovacuum if greater than zero.
231 ** meta[4] Db text encoding. 1:UTF-8 3:UTF-16 LE 4:UTF-16 BE
232 ** meta[5] The user cookie. Used by the application.
233 ** meta[6]
234 ** meta[7]
235 ** meta[8]
236 ** meta[9]
237 **
238 ** Note: The hash defined SQLITE_UTF* symbols in sqliteInt.h correspond to
239 ** the possible values of meta[4].
240 */
241 if( rc==SQLITE_OK ){
242 int i;
243 for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
drhfdd6e852005-12-16 01:06:16 +0000244 rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
danielk1977fa256a32005-05-25 04:11:56 +0000245 }
246 if( rc ){
247 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
248 sqlite3BtreeCloseCursor(curMain);
249 return rc;
250 }
251 }else{
252 memset(meta, 0, sizeof(meta));
253 }
danielk1977da184232006-01-05 11:34:32 +0000254 pDb->pSchema->schema_cookie = meta[0];
danielk1977fa256a32005-05-25 04:11:56 +0000255
256 /* If opening a non-empty database, check the text encoding. For the
257 ** main database, set sqlite3.enc to the encoding of the main database.
258 ** For an attached db, it is an error if the encoding is not the same
259 ** as sqlite3.enc.
260 */
261 if( meta[4] ){ /* text encoding */
262 if( iDb==0 ){
danielk197714db2662006-01-09 16:12:04 +0000263 /* If opening the main database, set ENC(db). */
264 ENC(db) = (u8)meta[4];
danielk1977b3bf5562006-01-10 17:58:23 +0000265 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0);
danielk1977fa256a32005-05-25 04:11:56 +0000266 }else{
danielk197714db2662006-01-09 16:12:04 +0000267 /* If opening an attached database, the encoding much match ENC(db) */
268 if( meta[4]!=ENC(db) ){
danielk1977fa256a32005-05-25 04:11:56 +0000269 sqlite3BtreeCloseCursor(curMain);
270 sqlite3SetString(pzErrMsg, "attached databases must use the same"
271 " text encoding as main database", (char*)0);
272 return SQLITE_ERROR;
273 }
274 }
275 }
danielk197714db2662006-01-09 16:12:04 +0000276 pDb->pSchema->enc = ENC(db);
danielk1977fa256a32005-05-25 04:11:56 +0000277
278 size = meta[2];
279 if( size==0 ){ size = MAX_PAGES; }
danielk197714db2662006-01-09 16:12:04 +0000280 pDb->pSchema->cache_size = size;
281 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
danielk1977fa256a32005-05-25 04:11:56 +0000282
283 /*
284 ** file_format==1 Version 3.0.0.
drhfdd6e852005-12-16 01:06:16 +0000285 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
286 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
drhd946db02005-12-29 19:23:06 +0000287 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
danielk1977fa256a32005-05-25 04:11:56 +0000288 */
danielk1977da184232006-01-05 11:34:32 +0000289 pDb->pSchema->file_format = meta[1];
290 if( pDb->pSchema->file_format==0 ){
291 pDb->pSchema->file_format = 1;
drhfdd6e852005-12-16 01:06:16 +0000292 }
danielk1977da184232006-01-05 11:34:32 +0000293 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
danielk1977fa256a32005-05-25 04:11:56 +0000294 sqlite3BtreeCloseCursor(curMain);
295 sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
296 return SQLITE_ERROR;
297 }
298
danielk1977fa256a32005-05-25 04:11:56 +0000299
300 /* Read the schema information out of the schema tables
301 */
302 assert( db->init.busy );
303 if( rc==SQLITE_EMPTY ){
304 /* For an empty database, there is nothing to read */
305 rc = SQLITE_OK;
306 }else{
307 char *zSql;
308 zSql = sqlite3MPrintf(
309 "SELECT name, rootpage, sql, '%s' FROM '%q'.%s",
310 zDbNum, db->aDb[iDb].zName, zMasterName);
311 sqlite3SafetyOff(db);
312 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
313 sqlite3SafetyOn(db);
314 sqliteFree(zSql);
drh497e4462005-07-23 03:18:40 +0000315#ifndef SQLITE_OMIT_ANALYZE
316 if( rc==SQLITE_OK ){
317 sqlite3AnalysisLoad(db, iDb);
318 }
319#endif
danielk1977fa256a32005-05-25 04:11:56 +0000320 sqlite3BtreeCloseCursor(curMain);
321 }
danielk1977e501b892006-01-09 06:29:47 +0000322 if( sqlite3ThreadData()->mallocFailed ){
danielk1977fa256a32005-05-25 04:11:56 +0000323 sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
324 rc = SQLITE_NOMEM;
325 sqlite3ResetInternalSchema(db, 0);
326 }
327 if( rc==SQLITE_OK ){
328 DbSetProperty(db, iDb, DB_SchemaLoaded);
329 }else{
330 sqlite3ResetInternalSchema(db, iDb);
331 }
332 return rc;
333}
334
335/*
336** Initialize all database files - the main database file, the file
337** used to store temporary tables, and any additional database files
338** created using ATTACH statements. Return a success code. If an
339** error occurs, write an error message into *pzErrMsg.
340**
341** After the database is initialized, the SQLITE_Initialized
342** bit is set in the flags field of the sqlite structure.
343*/
344int sqlite3Init(sqlite3 *db, char **pzErrMsg){
345 int i, rc;
346
347 if( db->init.busy ) return SQLITE_OK;
348 assert( (db->flags & SQLITE_Initialized)==0 );
349 rc = SQLITE_OK;
350 db->init.busy = 1;
351 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
352 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
353 rc = sqlite3InitOne(db, i, pzErrMsg);
354 if( rc ){
355 sqlite3ResetInternalSchema(db, i);
356 }
357 }
358
359 /* Once all the other databases have been initialised, load the schema
360 ** for the TEMP database. This is loaded last, as the TEMP database
361 ** schema may contain references to objects in other databases.
362 */
363#ifndef SQLITE_OMIT_TEMPDB
364 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
365 rc = sqlite3InitOne(db, 1, pzErrMsg);
366 if( rc ){
367 sqlite3ResetInternalSchema(db, 1);
368 }
369 }
370#endif
371
372 db->init.busy = 0;
373 if( rc==SQLITE_OK ){
374 db->flags |= SQLITE_Initialized;
375 sqlite3CommitInternalChanges(db);
376 }
377
378 if( rc!=SQLITE_OK ){
379 db->flags &= ~SQLITE_Initialized;
380 }
381 return rc;
382}
383
384/*
385** This routine is a no-op if the database schema is already initialised.
386** Otherwise, the schema is loaded. An error code is returned.
387*/
388int sqlite3ReadSchema(Parse *pParse){
389 int rc = SQLITE_OK;
390 sqlite3 *db = pParse->db;
391 if( !db->init.busy ){
392 if( (db->flags & SQLITE_Initialized)==0 ){
393 rc = sqlite3Init(db, &pParse->zErrMsg);
394 }
395 }
drh8b3d9902005-08-19 00:14:42 +0000396 assert( rc!=SQLITE_OK || (db->flags & SQLITE_Initialized) || db->init.busy );
danielk1977fa256a32005-05-25 04:11:56 +0000397 if( rc!=SQLITE_OK ){
398 pParse->rc = rc;
399 pParse->nErr++;
400 }
401 return rc;
402}
403
404
405/*
406** Check schema cookies in all databases. If any cookie is out
407** of date, return 0. If all schema cookies are current, return 1.
408*/
409static int schemaIsValid(sqlite3 *db){
410 int iDb;
411 int rc;
412 BtCursor *curTemp;
413 int cookie;
414 int allOk = 1;
415
416 for(iDb=0; allOk && iDb<db->nDb; iDb++){
417 Btree *pBt;
418 pBt = db->aDb[iDb].pBt;
419 if( pBt==0 ) continue;
420 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
421 if( rc==SQLITE_OK ){
422 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
danielk1977da184232006-01-05 11:34:32 +0000423 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].pSchema->schema_cookie ){
danielk1977fa256a32005-05-25 04:11:56 +0000424 allOk = 0;
425 }
426 sqlite3BtreeCloseCursor(curTemp);
427 }
428 }
429 return allOk;
430}
431
432/*
drh198bf392006-01-06 21:52:49 +0000433** Free all resources held by the schema structure. The void* argument points
danielk1977e501b892006-01-09 06:29:47 +0000434** at a Schema struct. This function does not call sqliteFree() on the
drh198bf392006-01-06 21:52:49 +0000435** pointer itself, it just cleans up subsiduary resources (i.e. the contents
436** of the schema hash tables).
437*/
438void sqlite3SchemaFree(void *p){
439 Hash temp1;
440 Hash temp2;
441 HashElem *pElem;
danielk1977e501b892006-01-09 06:29:47 +0000442 Schema *pSchema = (Schema *)p;
drh198bf392006-01-06 21:52:49 +0000443
444 temp1 = pSchema->tblHash;
445 temp2 = pSchema->trigHash;
446 sqlite3HashInit(&pSchema->trigHash, SQLITE_HASH_STRING, 0);
447 sqlite3HashClear(&pSchema->aFKey);
448 sqlite3HashClear(&pSchema->idxHash);
449 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
450 sqlite3DeleteTrigger((Trigger*)sqliteHashData(pElem));
451 }
452 sqlite3HashClear(&temp2);
453 sqlite3HashInit(&pSchema->tblHash, SQLITE_HASH_STRING, 0);
454 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
455 Table *pTab = sqliteHashData(pElem);
456 sqlite3DeleteTable(0, pTab);
457 }
458 sqlite3HashClear(&temp1);
459 pSchema->pSeqTab = 0;
460 pSchema->flags &= ~DB_SchemaLoaded;
461}
462
danielk1977e501b892006-01-09 06:29:47 +0000463Schema *sqlite3SchemaGet(Btree *pBt){
464 Schema * p;
drh198bf392006-01-06 21:52:49 +0000465 if( pBt ){
danielk1977e501b892006-01-09 06:29:47 +0000466 p = (Schema *)sqlite3BtreeSchema(pBt,sizeof(Schema),sqlite3SchemaFree);
drh198bf392006-01-06 21:52:49 +0000467 }else{
danielk1977e501b892006-01-09 06:29:47 +0000468 p = (Schema *)sqliteMalloc(sizeof(Schema));
drh198bf392006-01-06 21:52:49 +0000469 }
470 if( p && 0==p->file_format ){
471 sqlite3HashInit(&p->tblHash, SQLITE_HASH_STRING, 0);
472 sqlite3HashInit(&p->idxHash, SQLITE_HASH_STRING, 0);
473 sqlite3HashInit(&p->trigHash, SQLITE_HASH_STRING, 0);
474 sqlite3HashInit(&p->aFKey, SQLITE_HASH_STRING, 1);
475 }
476 return p;
477}
478
danielk1977e501b892006-01-09 06:29:47 +0000479int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
drh198bf392006-01-06 21:52:49 +0000480 int i = -1000000;
481
482 /* If pSchema is NULL, then return -1000000. This happens when code in
483 ** expr.c is trying to resolve a reference to a transient table (i.e. one
484 ** created by a sub-select). In this case the return value of this
485 ** function should never be used.
486 **
487 ** We return -1000000 instead of the more usual -1 simply because using
488 ** -1000000 as incorrectly using -1000000 index into db->aDb[] is much
489 ** more likely to cause a segfault than -1 (of course there are assert()
490 ** statements too, but it never hurts to play the odds).
491 */
492 if( pSchema ){
493 for(i=0; i<db->nDb; i++){
494 if( db->aDb[i].pSchema==pSchema ){
495 break;
496 }
497 }
498 assert( i>=0 &&i>=0 && i<db->nDb );
499 }
500 return i;
501}
502
503/*
danielk1977fa256a32005-05-25 04:11:56 +0000504** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
505*/
506int sqlite3_prepare(
507 sqlite3 *db, /* Database handle. */
508 const char *zSql, /* UTF-8 encoded SQL statement. */
509 int nBytes, /* Length of zSql in bytes. */
510 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
511 const char** pzTail /* OUT: End of parsed string */
512){
513 Parse sParse;
514 char *zErrMsg = 0;
515 int rc = SQLITE_OK;
danielk1977c87d34d2006-01-06 13:00:28 +0000516 int i;
danielk1977fa256a32005-05-25 04:11:56 +0000517
danielk1977e501b892006-01-09 06:29:47 +0000518 assert( !sqlite3ThreadData()->mallocFailed );
danielk1977fa256a32005-05-25 04:11:56 +0000519
520 assert( ppStmt );
521 *ppStmt = 0;
522 if( sqlite3SafetyOn(db) ){
523 return SQLITE_MISUSE;
524 }
525
danielk1977c87d34d2006-01-06 13:00:28 +0000526 /* If any attached database schemas are locked, do not proceed with
527 ** compilation. Instead return SQLITE_LOCKED immediately.
528 */
529 for(i=0; i<db->nDb; i++) {
530 Btree *pBt = db->aDb[i].pBt;
531 if( pBt && sqlite3BtreeSchemaLocked(pBt) ){
532 const char *zDb = db->aDb[i].zName;
533 sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb);
534 sqlite3SafetyOff(db);
535 return SQLITE_LOCKED;
536 }
537 }
538
danielk1977fa256a32005-05-25 04:11:56 +0000539 memset(&sParse, 0, sizeof(sParse));
540 sParse.db = db;
541 sqlite3RunParser(&sParse, zSql, &zErrMsg);
542
danielk1977e501b892006-01-09 06:29:47 +0000543 if( sqlite3ThreadData()->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +0000544 sParse.rc = SQLITE_NOMEM;
danielk1977fa256a32005-05-25 04:11:56 +0000545 }
546 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
drh4d91a702006-01-04 15:54:36 +0000547 if( sParse.checkSchema && !schemaIsValid(db) ){
danielk1977fa256a32005-05-25 04:11:56 +0000548 sParse.rc = SQLITE_SCHEMA;
549 }
550 if( sParse.rc==SQLITE_SCHEMA ){
551 sqlite3ResetInternalSchema(db, 0);
552 }
553 if( pzTail ) *pzTail = sParse.zTail;
554 rc = sParse.rc;
555
556#ifndef SQLITE_OMIT_EXPLAIN
557 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
drhecc92422005-09-10 16:46:12 +0000558 if( sParse.explain==2 ){
559 sqlite3VdbeSetNumCols(sParse.pVdbe, 3);
560 sqlite3VdbeSetColName(sParse.pVdbe, 0, "order", P3_STATIC);
561 sqlite3VdbeSetColName(sParse.pVdbe, 1, "from", P3_STATIC);
562 sqlite3VdbeSetColName(sParse.pVdbe, 2, "detail", P3_STATIC);
563 }else{
564 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
565 sqlite3VdbeSetColName(sParse.pVdbe, 0, "addr", P3_STATIC);
566 sqlite3VdbeSetColName(sParse.pVdbe, 1, "opcode", P3_STATIC);
567 sqlite3VdbeSetColName(sParse.pVdbe, 2, "p1", P3_STATIC);
568 sqlite3VdbeSetColName(sParse.pVdbe, 3, "p2", P3_STATIC);
569 sqlite3VdbeSetColName(sParse.pVdbe, 4, "p3", P3_STATIC);
570 }
danielk1977fa256a32005-05-25 04:11:56 +0000571 }
572#endif
573
danielk1977fa256a32005-05-25 04:11:56 +0000574 if( sqlite3SafetyOff(db) ){
575 rc = SQLITE_MISUSE;
576 }
577 if( rc==SQLITE_OK ){
578 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
579 }else if( sParse.pVdbe ){
580 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
581 }
582
583 if( zErrMsg ){
584 sqlite3Error(db, rc, "%s", zErrMsg);
585 sqliteFree(zErrMsg);
586 }else{
587 sqlite3Error(db, rc, 0);
588 }
danielk1977261919c2005-12-06 12:52:59 +0000589
590 /* We must check for malloc failure last of all, in case malloc() failed
591 ** inside of the sqlite3Error() call above or something.
592 */
danielk1977e501b892006-01-09 06:29:47 +0000593 if( sqlite3ThreadData()->mallocFailed ){
danielk1977261919c2005-12-06 12:52:59 +0000594 rc = SQLITE_NOMEM;
595 sqlite3Error(db, rc, 0);
596 }
597
danielk19772e588c72005-12-09 14:25:08 +0000598 sqlite3MallocClearFailed();
danielk1977fa256a32005-05-25 04:11:56 +0000599 return rc;
600}
601
602#ifndef SQLITE_OMIT_UTF16
603/*
604** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
605*/
606int sqlite3_prepare16(
607 sqlite3 *db, /* Database handle. */
608 const void *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 void **pzTail /* OUT: End of parsed string */
612){
613 /* This function currently works by first transforming the UTF-16
614 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
615 ** tricky bit is figuring out the pointer to return in *pzTail.
616 */
drhaf9a7c22005-12-15 03:04:10 +0000617 char *zSql8 = 0;
danielk1977c87d34d2006-01-06 13:00:28 +0000618 const char *zTail8 = 0;
danielk1977fa256a32005-05-25 04:11:56 +0000619 int rc;
danielk1977fa256a32005-05-25 04:11:56 +0000620
621 if( sqlite3SafetyCheck(db) ){
622 return SQLITE_MISUSE;
623 }
drhaf9a7c22005-12-15 03:04:10 +0000624 zSql8 = sqlite3utf16to8(zSql, nBytes);
danielk1977fa256a32005-05-25 04:11:56 +0000625 if( !zSql8 ){
626 sqlite3Error(db, SQLITE_NOMEM, 0);
627 return SQLITE_NOMEM;
628 }
629 rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);
630
631 if( zTail8 && pzTail ){
632 /* If sqlite3_prepare returns a tail pointer, we calculate the
633 ** equivalent pointer into the UTF-16 string by counting the unicode
634 ** characters between zSql8 and zTail8, and then returning a pointer
635 ** the same number of characters into the UTF-16 string.
636 */
637 int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8);
638 *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed);
639 }
drhaf9a7c22005-12-15 03:04:10 +0000640 sqliteFree(zSql8);
danielk1977fa256a32005-05-25 04:11:56 +0000641 return rc;
642}
643#endif /* SQLITE_OMIT_UTF16 */