blob: c755d6b93ec487bdd184ac87a6bc731572e6ad2a [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**
danielk1977955de522006-02-10 02:27:42 +000016** $Id: prepare.c,v 1.31 2006/02/10 02:27:43 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){
danielk19779e128002006-01-18 16:51:35 +000027 if( !sqlite3MallocFailed() ){
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
danielk19779e128002006-01-18 16:51:35 +000052 if( sqlite3MallocFailed() ){
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 ){
danielk19779e128002006-01-18 16:51:35 +000079 sqlite3FailedMalloc();
danielk1977261919c2005-12-06 12:52:59 +000080 }else{
danielk19779e128002006-01-18 16:51:35 +000081 corruptSchema(pData, zErr);
danielk1977261919c2005-12-06 12:52:59 +000082 }
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 );
danielk197714db2662006-01-09 16:12:04 +0000157 assert( db->aDb[iDb].pSchema );
danielk1977da184232006-01-05 11:34:32 +0000158
danielk1977fa256a32005-05-25 04:11:56 +0000159 /* zMasterSchema and zInitScript are set to point at the master schema
160 ** and initialisation script appropriate for the database being
161 ** initialised. zMasterName is the name of the master table.
162 */
163 if( !OMIT_TEMPDB && iDb==1 ){
164 zMasterSchema = temp_master_schema;
165 }else{
166 zMasterSchema = master_schema;
167 }
168 zMasterName = SCHEMA_TABLE(iDb);
169
170 /* Construct the schema tables. */
171 sqlite3SafetyOff(db);
172 azArg[0] = zMasterName;
173 azArg[1] = "1";
174 azArg[2] = zMasterSchema;
175 sprintf(zDbNum, "%d", iDb);
176 azArg[3] = zDbNum;
177 azArg[4] = 0;
178 initData.db = db;
179 initData.pzErrMsg = pzErrMsg;
180 rc = sqlite3InitCallback(&initData, 4, (char **)azArg, 0);
181 if( rc!=SQLITE_OK ){
182 sqlite3SafetyOn(db);
183 return rc;
184 }
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.
213 ** meta[4] Db text encoding. 1:UTF-8 3:UTF-16 LE 4:UTF-16 BE
214 ** meta[5] The user cookie. Used by the application.
215 ** meta[6]
216 ** 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];
263 if( size==0 ){ size = MAX_PAGES; }
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;
292 zSql = sqlite3MPrintf(
293 "SELECT name, rootpage, sql, '%s' FROM '%q'.%s",
294 zDbNum, db->aDb[iDb].zName, zMasterName);
295 sqlite3SafetyOff(db);
296 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
297 sqlite3SafetyOn(db);
298 sqliteFree(zSql);
drh497e4462005-07-23 03:18:40 +0000299#ifndef SQLITE_OMIT_ANALYZE
300 if( rc==SQLITE_OK ){
301 sqlite3AnalysisLoad(db, iDb);
302 }
303#endif
danielk1977fa256a32005-05-25 04:11:56 +0000304 sqlite3BtreeCloseCursor(curMain);
305 }
danielk19779e128002006-01-18 16:51:35 +0000306 if( sqlite3MallocFailed() ){
307 /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */
danielk1977fa256a32005-05-25 04:11:56 +0000308 rc = SQLITE_NOMEM;
309 sqlite3ResetInternalSchema(db, 0);
310 }
311 if( rc==SQLITE_OK ){
312 DbSetProperty(db, iDb, DB_SchemaLoaded);
313 }else{
314 sqlite3ResetInternalSchema(db, iDb);
315 }
316 return rc;
317}
318
319/*
320** Initialize all database files - the main database file, the file
321** used to store temporary tables, and any additional database files
322** created using ATTACH statements. Return a success code. If an
323** error occurs, write an error message into *pzErrMsg.
324**
danielk1977e7259292006-01-13 06:33:23 +0000325** After a database is initialized, the DB_SchemaLoaded bit is set
326** bit is set in the flags field of the Db structure. If the database
327** file was of zero-length, then the DB_Empty flag is also set.
danielk1977fa256a32005-05-25 04:11:56 +0000328*/
329int sqlite3Init(sqlite3 *db, char **pzErrMsg){
330 int i, rc;
danielk1977b82e7ed2006-01-11 14:09:31 +0000331 int called_initone = 0;
danielk1977fa256a32005-05-25 04:11:56 +0000332
333 if( db->init.busy ) return SQLITE_OK;
danielk1977fa256a32005-05-25 04:11:56 +0000334 rc = SQLITE_OK;
335 db->init.busy = 1;
336 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
337 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
338 rc = sqlite3InitOne(db, i, pzErrMsg);
339 if( rc ){
340 sqlite3ResetInternalSchema(db, i);
341 }
danielk1977b82e7ed2006-01-11 14:09:31 +0000342 called_initone = 1;
danielk1977fa256a32005-05-25 04:11:56 +0000343 }
344
345 /* Once all the other databases have been initialised, load the schema
346 ** for the TEMP database. This is loaded last, as the TEMP database
347 ** schema may contain references to objects in other databases.
348 */
349#ifndef SQLITE_OMIT_TEMPDB
350 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
351 rc = sqlite3InitOne(db, 1, pzErrMsg);
352 if( rc ){
353 sqlite3ResetInternalSchema(db, 1);
354 }
danielk1977b82e7ed2006-01-11 14:09:31 +0000355 called_initone = 1;
danielk1977fa256a32005-05-25 04:11:56 +0000356 }
357#endif
358
359 db->init.busy = 0;
danielk1977b82e7ed2006-01-11 14:09:31 +0000360 if( rc==SQLITE_OK && called_initone ){
danielk1977fa256a32005-05-25 04:11:56 +0000361 sqlite3CommitInternalChanges(db);
362 }
363
danielk1977b82e7ed2006-01-11 14:09:31 +0000364 return rc;
danielk1977fa256a32005-05-25 04:11:56 +0000365}
366
367/*
368** This routine is a no-op if the database schema is already initialised.
369** Otherwise, the schema is loaded. An error code is returned.
370*/
371int sqlite3ReadSchema(Parse *pParse){
372 int rc = SQLITE_OK;
373 sqlite3 *db = pParse->db;
374 if( !db->init.busy ){
danielk1977b82e7ed2006-01-11 14:09:31 +0000375 rc = sqlite3Init(db, &pParse->zErrMsg);
danielk1977fa256a32005-05-25 04:11:56 +0000376 }
danielk1977fa256a32005-05-25 04:11:56 +0000377 if( rc!=SQLITE_OK ){
378 pParse->rc = rc;
379 pParse->nErr++;
380 }
381 return rc;
382}
383
384
385/*
386** Check schema cookies in all databases. If any cookie is out
387** of date, return 0. If all schema cookies are current, return 1.
388*/
389static int schemaIsValid(sqlite3 *db){
390 int iDb;
391 int rc;
392 BtCursor *curTemp;
393 int cookie;
394 int allOk = 1;
395
396 for(iDb=0; allOk && iDb<db->nDb; iDb++){
397 Btree *pBt;
398 pBt = db->aDb[iDb].pBt;
399 if( pBt==0 ) continue;
400 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
401 if( rc==SQLITE_OK ){
402 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
danielk1977da184232006-01-05 11:34:32 +0000403 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].pSchema->schema_cookie ){
danielk1977fa256a32005-05-25 04:11:56 +0000404 allOk = 0;
405 }
406 sqlite3BtreeCloseCursor(curTemp);
407 }
408 }
409 return allOk;
410}
411
412/*
drh198bf392006-01-06 21:52:49 +0000413** Free all resources held by the schema structure. The void* argument points
danielk1977e501b892006-01-09 06:29:47 +0000414** at a Schema struct. This function does not call sqliteFree() on the
drh198bf392006-01-06 21:52:49 +0000415** pointer itself, it just cleans up subsiduary resources (i.e. the contents
416** of the schema hash tables).
417*/
418void sqlite3SchemaFree(void *p){
419 Hash temp1;
420 Hash temp2;
421 HashElem *pElem;
danielk1977e501b892006-01-09 06:29:47 +0000422 Schema *pSchema = (Schema *)p;
drh198bf392006-01-06 21:52:49 +0000423
424 temp1 = pSchema->tblHash;
425 temp2 = pSchema->trigHash;
426 sqlite3HashInit(&pSchema->trigHash, SQLITE_HASH_STRING, 0);
427 sqlite3HashClear(&pSchema->aFKey);
428 sqlite3HashClear(&pSchema->idxHash);
429 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
430 sqlite3DeleteTrigger((Trigger*)sqliteHashData(pElem));
431 }
432 sqlite3HashClear(&temp2);
433 sqlite3HashInit(&pSchema->tblHash, SQLITE_HASH_STRING, 0);
434 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
435 Table *pTab = sqliteHashData(pElem);
436 sqlite3DeleteTable(0, pTab);
437 }
438 sqlite3HashClear(&temp1);
439 pSchema->pSeqTab = 0;
440 pSchema->flags &= ~DB_SchemaLoaded;
441}
442
drhf248e212006-01-25 22:50:38 +0000443/*
444** Find and return the schema associated with a BTree. Create
445** a new one if necessary.
446*/
danielk1977e501b892006-01-09 06:29:47 +0000447Schema *sqlite3SchemaGet(Btree *pBt){
448 Schema * p;
drh198bf392006-01-06 21:52:49 +0000449 if( pBt ){
danielk1977e501b892006-01-09 06:29:47 +0000450 p = (Schema *)sqlite3BtreeSchema(pBt,sizeof(Schema),sqlite3SchemaFree);
drh198bf392006-01-06 21:52:49 +0000451 }else{
danielk1977e501b892006-01-09 06:29:47 +0000452 p = (Schema *)sqliteMalloc(sizeof(Schema));
drh198bf392006-01-06 21:52:49 +0000453 }
454 if( p && 0==p->file_format ){
455 sqlite3HashInit(&p->tblHash, SQLITE_HASH_STRING, 0);
456 sqlite3HashInit(&p->idxHash, SQLITE_HASH_STRING, 0);
457 sqlite3HashInit(&p->trigHash, SQLITE_HASH_STRING, 0);
458 sqlite3HashInit(&p->aFKey, SQLITE_HASH_STRING, 1);
459 }
460 return p;
461}
462
drhf248e212006-01-25 22:50:38 +0000463/*
464** Convert a schema pointer into the iDb index that indicates
465** which database file in db->aDb[] the schema refers to.
466**
467** If the same database is attached more than once, the first
468** attached database is returned.
469*/
danielk1977e501b892006-01-09 06:29:47 +0000470int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
drh198bf392006-01-06 21:52:49 +0000471 int i = -1000000;
472
473 /* If pSchema is NULL, then return -1000000. This happens when code in
474 ** expr.c is trying to resolve a reference to a transient table (i.e. one
475 ** created by a sub-select). In this case the return value of this
476 ** function should never be used.
477 **
478 ** We return -1000000 instead of the more usual -1 simply because using
479 ** -1000000 as incorrectly using -1000000 index into db->aDb[] is much
480 ** more likely to cause a segfault than -1 (of course there are assert()
481 ** statements too, but it never hurts to play the odds).
482 */
483 if( pSchema ){
484 for(i=0; i<db->nDb; i++){
485 if( db->aDb[i].pSchema==pSchema ){
486 break;
487 }
488 }
489 assert( i>=0 &&i>=0 && i<db->nDb );
490 }
491 return i;
492}
493
494/*
danielk1977fa256a32005-05-25 04:11:56 +0000495** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
496*/
497int sqlite3_prepare(
498 sqlite3 *db, /* Database handle. */
499 const char *zSql, /* UTF-8 encoded SQL statement. */
500 int nBytes, /* Length of zSql in bytes. */
501 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
502 const char** pzTail /* OUT: End of parsed string */
503){
504 Parse sParse;
505 char *zErrMsg = 0;
506 int rc = SQLITE_OK;
danielk1977c87d34d2006-01-06 13:00:28 +0000507 int i;
danielk1977fa256a32005-05-25 04:11:56 +0000508
danielk1977efaaf572006-01-16 11:29:19 +0000509 /* Assert that malloc() has not failed */
danielk19779e128002006-01-18 16:51:35 +0000510 assert( !sqlite3MallocFailed() );
danielk1977fa256a32005-05-25 04:11:56 +0000511
512 assert( ppStmt );
513 *ppStmt = 0;
514 if( sqlite3SafetyOn(db) ){
515 return SQLITE_MISUSE;
516 }
517
danielk1977c87d34d2006-01-06 13:00:28 +0000518 /* If any attached database schemas are locked, do not proceed with
519 ** compilation. Instead return SQLITE_LOCKED immediately.
520 */
521 for(i=0; i<db->nDb; i++) {
522 Btree *pBt = db->aDb[i].pBt;
523 if( pBt && sqlite3BtreeSchemaLocked(pBt) ){
524 const char *zDb = db->aDb[i].zName;
525 sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb);
526 sqlite3SafetyOff(db);
527 return SQLITE_LOCKED;
528 }
529 }
530
danielk1977fa256a32005-05-25 04:11:56 +0000531 memset(&sParse, 0, sizeof(sParse));
532 sParse.db = db;
drh9051a422006-01-30 22:35:43 +0000533 if( nBytes>=0 && zSql[nBytes]!=0 ){
534 char *zSqlCopy = sqlite3StrNDup(zSql, nBytes);
535 sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
536 sParse.zTail += zSql - zSqlCopy;
537 sqliteFree(zSqlCopy);
538 }else{
539 sqlite3RunParser(&sParse, zSql, &zErrMsg);
540 }
danielk1977fa256a32005-05-25 04:11:56 +0000541
danielk19779e128002006-01-18 16:51:35 +0000542 if( sqlite3MallocFailed() ){
danielk1977261919c2005-12-06 12:52:59 +0000543 sParse.rc = SQLITE_NOMEM;
danielk1977fa256a32005-05-25 04:11:56 +0000544 }
545 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
drh4d91a702006-01-04 15:54:36 +0000546 if( sParse.checkSchema && !schemaIsValid(db) ){
danielk1977fa256a32005-05-25 04:11:56 +0000547 sParse.rc = SQLITE_SCHEMA;
548 }
549 if( sParse.rc==SQLITE_SCHEMA ){
550 sqlite3ResetInternalSchema(db, 0);
551 }
552 if( pzTail ) *pzTail = sParse.zTail;
553 rc = sParse.rc;
554
555#ifndef SQLITE_OMIT_EXPLAIN
556 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
drhecc92422005-09-10 16:46:12 +0000557 if( sParse.explain==2 ){
558 sqlite3VdbeSetNumCols(sParse.pVdbe, 3);
danielk1977955de522006-02-10 02:27:42 +0000559 sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "order", P3_STATIC);
560 sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "from", P3_STATIC);
561 sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "detail", P3_STATIC);
drhecc92422005-09-10 16:46:12 +0000562 }else{
563 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
danielk1977955de522006-02-10 02:27:42 +0000564 sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "addr", P3_STATIC);
565 sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "opcode", P3_STATIC);
566 sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "p1", P3_STATIC);
567 sqlite3VdbeSetColName(sParse.pVdbe, 3, COLNAME_NAME, "p2", P3_STATIC);
568 sqlite3VdbeSetColName(sParse.pVdbe, 4, COLNAME_NAME, "p3", P3_STATIC);
drhecc92422005-09-10 16:46:12 +0000569 }
danielk1977fa256a32005-05-25 04:11:56 +0000570 }
571#endif
572
danielk1977fa256a32005-05-25 04:11:56 +0000573 if( sqlite3SafetyOff(db) ){
574 rc = SQLITE_MISUSE;
575 }
576 if( rc==SQLITE_OK ){
577 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
578 }else if( sParse.pVdbe ){
579 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
580 }
581
582 if( zErrMsg ){
583 sqlite3Error(db, rc, "%s", zErrMsg);
584 sqliteFree(zErrMsg);
585 }else{
586 sqlite3Error(db, rc, 0);
587 }
danielk1977261919c2005-12-06 12:52:59 +0000588
danielk197754f01982006-01-18 15:25:17 +0000589 rc = sqlite3ApiExit(db, rc);
drh4b494d62006-01-12 12:43:36 +0000590 sqlite3ReleaseThreadData();
danielk1977fa256a32005-05-25 04:11:56 +0000591 return rc;
592}
593
594#ifndef SQLITE_OMIT_UTF16
595/*
596** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
597*/
598int sqlite3_prepare16(
599 sqlite3 *db, /* Database handle. */
600 const void *zSql, /* UTF-8 encoded SQL statement. */
601 int nBytes, /* Length of zSql in bytes. */
602 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
603 const void **pzTail /* OUT: End of parsed string */
604){
605 /* This function currently works by first transforming the UTF-16
606 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
607 ** tricky bit is figuring out the pointer to return in *pzTail.
608 */
danielk197754f01982006-01-18 15:25:17 +0000609 char *zSql8;
danielk1977c87d34d2006-01-06 13:00:28 +0000610 const char *zTail8 = 0;
danielk197754f01982006-01-18 15:25:17 +0000611 int rc = SQLITE_OK;
danielk1977fa256a32005-05-25 04:11:56 +0000612
613 if( sqlite3SafetyCheck(db) ){
614 return SQLITE_MISUSE;
615 }
drhaf9a7c22005-12-15 03:04:10 +0000616 zSql8 = sqlite3utf16to8(zSql, nBytes);
danielk197754f01982006-01-18 15:25:17 +0000617 if( zSql8 ){
618 rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);
danielk1977fa256a32005-05-25 04:11:56 +0000619 }
danielk1977fa256a32005-05-25 04:11:56 +0000620
621 if( zTail8 && pzTail ){
622 /* If sqlite3_prepare returns a tail pointer, we calculate the
623 ** equivalent pointer into the UTF-16 string by counting the unicode
624 ** characters between zSql8 and zTail8, and then returning a pointer
625 ** the same number of characters into the UTF-16 string.
626 */
627 int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8);
628 *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed);
629 }
drhaf9a7c22005-12-15 03:04:10 +0000630 sqliteFree(zSql8);
danielk197754f01982006-01-18 15:25:17 +0000631 return sqlite3ApiExit(db, rc);
danielk1977fa256a32005-05-25 04:11:56 +0000632}
633#endif /* SQLITE_OMIT_UTF16 */