blob: 7e52c87871124413a3dc439a7694e6f50b0921e5 [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**
drh4ac285a2006-09-15 07:28:50 +000016** $Id: prepare.c,v 1.39 2006/09/15 07:28:50 drh 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 }
drh15ca1df2006-07-26 13:43:30 +000031 pData->rc = SQLITE_CORRUPT;
danielk1977fa256a32005-05-25 04:11:56 +000032}
33
34/*
35** This is the callback routine for the code that initializes the
36** database. See sqlite3Init() below for additional information.
37** This routine is also called from the OP_ParseSchema opcode of the VDBE.
38**
39** Each callback contains the following information:
40**
41** argv[0] = name of thing being created
danielk197778efaba2006-06-12 06:09:17 +000042** argv[1] = root page number for table or index. 0 for trigger or view.
danielk1977fa256a32005-05-25 04:11:56 +000043** argv[2] = SQL text for the CREATE statement.
44** argv[3] = "1" for temporary files, "0" for main database, "2" or more
45** for auxiliary database files.
46**
47*/
48int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){
49 InitData *pData = (InitData*)pInit;
50 sqlite3 *db = pData->db;
51 int iDb;
52
drh15ca1df2006-07-26 13:43:30 +000053 pData->rc = SQLITE_OK;
danielk19779e128002006-01-18 16:51:35 +000054 if( sqlite3MallocFailed() ){
drhfe5a8162006-08-12 13:28:23 +000055 corruptSchema(pData, 0);
danielk1977da184232006-01-05 11:34:32 +000056 return SQLITE_NOMEM;
57 }
58
danielk1977fa256a32005-05-25 04:11:56 +000059 assert( argc==4 );
60 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
61 if( argv[1]==0 || argv[3]==0 ){
62 corruptSchema(pData, 0);
63 return 1;
64 }
65 iDb = atoi(argv[3]);
66 assert( iDb>=0 && iDb<db->nDb );
67 if( argv[2] && argv[2][0] ){
68 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
69 ** But because db->init.busy is set to 1, no VDBE code is generated
70 ** or executed. All the parser does is build the internal data
71 ** structures that describe the table, index, or view.
72 */
73 char *zErr;
74 int rc;
75 assert( db->init.busy );
76 db->init.iDb = iDb;
77 db->init.newTnum = atoi(argv[1]);
78 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
79 db->init.iDb = 0;
drh43617e92006-03-06 20:55:46 +000080 assert( rc!=SQLITE_OK || zErr==0 );
danielk1977fa256a32005-05-25 04:11:56 +000081 if( SQLITE_OK!=rc ){
drh15ca1df2006-07-26 13:43:30 +000082 pData->rc = rc;
danielk1977261919c2005-12-06 12:52:59 +000083 if( rc==SQLITE_NOMEM ){
danielk19779e128002006-01-18 16:51:35 +000084 sqlite3FailedMalloc();
drh15ca1df2006-07-26 13:43:30 +000085 }else if( rc!=SQLITE_INTERRUPT ){
danielk19779e128002006-01-18 16:51:35 +000086 corruptSchema(pData, zErr);
danielk1977261919c2005-12-06 12:52:59 +000087 }
danielk1977fa256a32005-05-25 04:11:56 +000088 sqlite3_free(zErr);
drh15ca1df2006-07-26 13:43:30 +000089 return 1;
danielk1977fa256a32005-05-25 04:11:56 +000090 }
91 }else{
92 /* If the SQL column is blank it means this is an index that
93 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
94 ** constraint for a CREATE TABLE. The index should have already
95 ** been created when we processed the CREATE TABLE. All we have
96 ** to do here is record the root page number for that index.
97 */
98 Index *pIndex;
99 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
100 if( pIndex==0 || pIndex->tnum!=0 ){
101 /* This can occur if there exists an index on a TEMP table which
102 ** has the same name as another index on a permanent index. Since
103 ** the permanent table is hidden by the TEMP table, we can also
104 ** safely ignore the index on the permanent table.
105 */
106 /* Do Nothing */;
107 }else{
108 pIndex->tnum = atoi(argv[1]);
109 }
110 }
111 return 0;
112}
113
114/*
115** Attempt to read the database schema and initialize internal
116** data structures for a single database file. The index of the
117** database file is given by iDb. iDb==0 is used for the main
118** database. iDb==1 should never be used. iDb>=2 is used for
119** auxiliary databases. Return one of the SQLITE_ error codes to
120** indicate success or failure.
121*/
122static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
123 int rc;
124 BtCursor *curMain;
125 int size;
126 Table *pTab;
drhfdd6e852005-12-16 01:06:16 +0000127 Db *pDb;
danielk1977fa256a32005-05-25 04:11:56 +0000128 char const *azArg[5];
129 char zDbNum[30];
130 int meta[10];
131 InitData initData;
132 char const *zMasterSchema;
133 char const *zMasterName = SCHEMA_TABLE(iDb);
134
135 /*
136 ** The master database table has a structure like this
137 */
138 static const char master_schema[] =
139 "CREATE TABLE sqlite_master(\n"
140 " type text,\n"
141 " name text,\n"
142 " tbl_name text,\n"
143 " rootpage integer,\n"
144 " sql text\n"
145 ")"
146 ;
147#ifndef SQLITE_OMIT_TEMPDB
148 static const char temp_master_schema[] =
149 "CREATE TEMP TABLE sqlite_temp_master(\n"
150 " type text,\n"
151 " name text,\n"
152 " tbl_name text,\n"
153 " rootpage integer,\n"
154 " sql text\n"
155 ")"
156 ;
157#else
158 #define temp_master_schema 0
159#endif
160
161 assert( iDb>=0 && iDb<db->nDb );
danielk197714db2662006-01-09 16:12:04 +0000162 assert( db->aDb[iDb].pSchema );
danielk1977da184232006-01-05 11:34:32 +0000163
danielk1977fa256a32005-05-25 04:11:56 +0000164 /* zMasterSchema and zInitScript are set to point at the master schema
165 ** and initialisation script appropriate for the database being
166 ** initialised. zMasterName is the name of the master table.
167 */
168 if( !OMIT_TEMPDB && iDb==1 ){
169 zMasterSchema = temp_master_schema;
170 }else{
171 zMasterSchema = master_schema;
172 }
173 zMasterName = SCHEMA_TABLE(iDb);
174
175 /* Construct the schema tables. */
176 sqlite3SafetyOff(db);
177 azArg[0] = zMasterName;
178 azArg[1] = "1";
179 azArg[2] = zMasterSchema;
180 sprintf(zDbNum, "%d", iDb);
181 azArg[3] = zDbNum;
182 azArg[4] = 0;
183 initData.db = db;
184 initData.pzErrMsg = pzErrMsg;
185 rc = sqlite3InitCallback(&initData, 4, (char **)azArg, 0);
drh15ca1df2006-07-26 13:43:30 +0000186 if( rc ){
danielk1977fa256a32005-05-25 04:11:56 +0000187 sqlite3SafetyOn(db);
drh15ca1df2006-07-26 13:43:30 +0000188 return initData.rc;
danielk1977fa256a32005-05-25 04:11:56 +0000189 }
190 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
191 if( pTab ){
192 pTab->readOnly = 1;
193 }
194 sqlite3SafetyOn(db);
195
196 /* Create a cursor to hold the database open
197 */
drhfdd6e852005-12-16 01:06:16 +0000198 pDb = &db->aDb[iDb];
199 if( pDb->pBt==0 ){
danielk1977b82e7ed2006-01-11 14:09:31 +0000200 if( !OMIT_TEMPDB && iDb==1 ){
201 DbSetProperty(db, 1, DB_SchemaLoaded);
202 }
danielk1977fa256a32005-05-25 04:11:56 +0000203 return SQLITE_OK;
204 }
drhfdd6e852005-12-16 01:06:16 +0000205 rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, 0, &curMain);
danielk1977fa256a32005-05-25 04:11:56 +0000206 if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
207 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
208 return rc;
209 }
210
211 /* Get the database meta information.
212 **
213 ** Meta values are as follows:
214 ** meta[0] Schema cookie. Changes with each schema change.
215 ** meta[1] File format of schema layer.
216 ** meta[2] Size of the page cache.
217 ** meta[3] Use freelist if 0. Autovacuum if greater than zero.
drh8159a352006-05-23 23:22:29 +0000218 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
danielk1977fa256a32005-05-25 04:11:56 +0000219 ** meta[5] The user cookie. Used by the application.
220 ** meta[6]
221 ** meta[7]
222 ** meta[8]
223 ** meta[9]
224 **
drhf248e212006-01-25 22:50:38 +0000225 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
danielk1977fa256a32005-05-25 04:11:56 +0000226 ** the possible values of meta[4].
227 */
228 if( rc==SQLITE_OK ){
229 int i;
230 for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
drhfdd6e852005-12-16 01:06:16 +0000231 rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
danielk1977fa256a32005-05-25 04:11:56 +0000232 }
233 if( rc ){
234 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
235 sqlite3BtreeCloseCursor(curMain);
236 return rc;
237 }
238 }else{
239 memset(meta, 0, sizeof(meta));
240 }
danielk1977da184232006-01-05 11:34:32 +0000241 pDb->pSchema->schema_cookie = meta[0];
danielk1977fa256a32005-05-25 04:11:56 +0000242
243 /* If opening a non-empty database, check the text encoding. For the
244 ** main database, set sqlite3.enc to the encoding of the main database.
245 ** For an attached db, it is an error if the encoding is not the same
246 ** as sqlite3.enc.
247 */
248 if( meta[4] ){ /* text encoding */
249 if( iDb==0 ){
danielk197714db2662006-01-09 16:12:04 +0000250 /* If opening the main database, set ENC(db). */
251 ENC(db) = (u8)meta[4];
danielk1977b3bf5562006-01-10 17:58:23 +0000252 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0);
danielk1977fa256a32005-05-25 04:11:56 +0000253 }else{
danielk197714db2662006-01-09 16:12:04 +0000254 /* If opening an attached database, the encoding much match ENC(db) */
255 if( meta[4]!=ENC(db) ){
danielk1977fa256a32005-05-25 04:11:56 +0000256 sqlite3BtreeCloseCursor(curMain);
257 sqlite3SetString(pzErrMsg, "attached databases must use the same"
258 " text encoding as main database", (char*)0);
259 return SQLITE_ERROR;
260 }
261 }
danielk1977b82e7ed2006-01-11 14:09:31 +0000262 }else{
263 DbSetProperty(db, iDb, DB_Empty);
danielk1977fa256a32005-05-25 04:11:56 +0000264 }
danielk197714db2662006-01-09 16:12:04 +0000265 pDb->pSchema->enc = ENC(db);
danielk1977fa256a32005-05-25 04:11:56 +0000266
267 size = meta[2];
268 if( size==0 ){ size = MAX_PAGES; }
danielk197714db2662006-01-09 16:12:04 +0000269 pDb->pSchema->cache_size = size;
270 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
danielk1977fa256a32005-05-25 04:11:56 +0000271
272 /*
273 ** file_format==1 Version 3.0.0.
drhfdd6e852005-12-16 01:06:16 +0000274 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
275 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
drhd946db02005-12-29 19:23:06 +0000276 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
danielk1977fa256a32005-05-25 04:11:56 +0000277 */
danielk1977da184232006-01-05 11:34:32 +0000278 pDb->pSchema->file_format = meta[1];
279 if( pDb->pSchema->file_format==0 ){
280 pDb->pSchema->file_format = 1;
drhfdd6e852005-12-16 01:06:16 +0000281 }
danielk1977da184232006-01-05 11:34:32 +0000282 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
danielk1977fa256a32005-05-25 04:11:56 +0000283 sqlite3BtreeCloseCursor(curMain);
284 sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
285 return SQLITE_ERROR;
286 }
287
danielk1977fa256a32005-05-25 04:11:56 +0000288
289 /* Read the schema information out of the schema tables
290 */
291 assert( db->init.busy );
292 if( rc==SQLITE_EMPTY ){
293 /* For an empty database, there is nothing to read */
294 rc = SQLITE_OK;
295 }else{
296 char *zSql;
297 zSql = sqlite3MPrintf(
298 "SELECT name, rootpage, sql, '%s' FROM '%q'.%s",
299 zDbNum, db->aDb[iDb].zName, zMasterName);
300 sqlite3SafetyOff(db);
301 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
drh15ca1df2006-07-26 13:43:30 +0000302 if( rc==SQLITE_ABORT ) rc = initData.rc;
danielk1977fa256a32005-05-25 04:11:56 +0000303 sqlite3SafetyOn(db);
304 sqliteFree(zSql);
drh497e4462005-07-23 03:18:40 +0000305#ifndef SQLITE_OMIT_ANALYZE
306 if( rc==SQLITE_OK ){
307 sqlite3AnalysisLoad(db, iDb);
308 }
309#endif
danielk1977fa256a32005-05-25 04:11:56 +0000310 sqlite3BtreeCloseCursor(curMain);
311 }
danielk19779e128002006-01-18 16:51:35 +0000312 if( sqlite3MallocFailed() ){
313 /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */
danielk1977fa256a32005-05-25 04:11:56 +0000314 rc = SQLITE_NOMEM;
315 sqlite3ResetInternalSchema(db, 0);
316 }
317 if( rc==SQLITE_OK ){
318 DbSetProperty(db, iDb, DB_SchemaLoaded);
319 }else{
320 sqlite3ResetInternalSchema(db, iDb);
321 }
322 return rc;
323}
324
325/*
326** Initialize all database files - the main database file, the file
327** used to store temporary tables, and any additional database files
328** created using ATTACH statements. Return a success code. If an
329** error occurs, write an error message into *pzErrMsg.
330**
danielk1977e7259292006-01-13 06:33:23 +0000331** After a database is initialized, the DB_SchemaLoaded bit is set
332** bit is set in the flags field of the Db structure. If the database
333** file was of zero-length, then the DB_Empty flag is also set.
danielk1977fa256a32005-05-25 04:11:56 +0000334*/
335int sqlite3Init(sqlite3 *db, char **pzErrMsg){
336 int i, rc;
danielk1977b82e7ed2006-01-11 14:09:31 +0000337 int called_initone = 0;
danielk1977fa256a32005-05-25 04:11:56 +0000338
339 if( db->init.busy ) return SQLITE_OK;
danielk1977fa256a32005-05-25 04:11:56 +0000340 rc = SQLITE_OK;
341 db->init.busy = 1;
342 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
343 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
344 rc = sqlite3InitOne(db, i, pzErrMsg);
345 if( rc ){
346 sqlite3ResetInternalSchema(db, i);
347 }
danielk1977b82e7ed2006-01-11 14:09:31 +0000348 called_initone = 1;
danielk1977fa256a32005-05-25 04:11:56 +0000349 }
350
351 /* Once all the other databases have been initialised, load the schema
352 ** for the TEMP database. This is loaded last, as the TEMP database
353 ** schema may contain references to objects in other databases.
354 */
355#ifndef SQLITE_OMIT_TEMPDB
356 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
357 rc = sqlite3InitOne(db, 1, pzErrMsg);
358 if( rc ){
359 sqlite3ResetInternalSchema(db, 1);
360 }
danielk1977b82e7ed2006-01-11 14:09:31 +0000361 called_initone = 1;
danielk1977fa256a32005-05-25 04:11:56 +0000362 }
363#endif
364
365 db->init.busy = 0;
danielk1977b82e7ed2006-01-11 14:09:31 +0000366 if( rc==SQLITE_OK && called_initone ){
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*/
452int sqlite3_prepare(
453 sqlite3 *db, /* Database handle. */
454 const char *zSql, /* UTF-8 encoded SQL statement. */
455 int nBytes, /* Length of zSql in bytes. */
456 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
457 const char** pzTail /* OUT: End of parsed string */
458){
459 Parse sParse;
460 char *zErrMsg = 0;
461 int rc = SQLITE_OK;
danielk1977c87d34d2006-01-06 13:00:28 +0000462 int i;
danielk1977fa256a32005-05-25 04:11:56 +0000463
danielk1977efaaf572006-01-16 11:29:19 +0000464 /* Assert that malloc() has not failed */
danielk19779e128002006-01-18 16:51:35 +0000465 assert( !sqlite3MallocFailed() );
danielk1977fa256a32005-05-25 04:11:56 +0000466
467 assert( ppStmt );
468 *ppStmt = 0;
469 if( sqlite3SafetyOn(db) ){
470 return SQLITE_MISUSE;
471 }
472
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 ){
489 char *zSqlCopy = sqlite3StrNDup(zSql, nBytes);
490 sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
491 sParse.zTail += zSql - zSqlCopy;
492 sqliteFree(zSqlCopy);
493 }else{
494 sqlite3RunParser(&sParse, zSql, &zErrMsg);
495 }
danielk1977fa256a32005-05-25 04:11:56 +0000496
danielk19779e128002006-01-18 16:51:35 +0000497 if( sqlite3MallocFailed() ){
danielk1977261919c2005-12-06 12:52:59 +0000498 sParse.rc = SQLITE_NOMEM;
danielk1977fa256a32005-05-25 04:11:56 +0000499 }
500 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
drh4d91a702006-01-04 15:54:36 +0000501 if( sParse.checkSchema && !schemaIsValid(db) ){
danielk1977fa256a32005-05-25 04:11:56 +0000502 sParse.rc = SQLITE_SCHEMA;
503 }
504 if( sParse.rc==SQLITE_SCHEMA ){
505 sqlite3ResetInternalSchema(db, 0);
506 }
drh344a6272006-06-26 12:50:09 +0000507 if( sqlite3MallocFailed() ){
508 sParse.rc = SQLITE_NOMEM;
509 }
danielk1977fa256a32005-05-25 04:11:56 +0000510 if( pzTail ) *pzTail = sParse.zTail;
511 rc = sParse.rc;
512
513#ifndef SQLITE_OMIT_EXPLAIN
514 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
drhecc92422005-09-10 16:46:12 +0000515 if( sParse.explain==2 ){
516 sqlite3VdbeSetNumCols(sParse.pVdbe, 3);
danielk1977955de522006-02-10 02:27:42 +0000517 sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "order", P3_STATIC);
518 sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "from", P3_STATIC);
519 sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "detail", P3_STATIC);
drhecc92422005-09-10 16:46:12 +0000520 }else{
521 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
danielk1977955de522006-02-10 02:27:42 +0000522 sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "addr", P3_STATIC);
523 sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "opcode", P3_STATIC);
524 sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "p1", P3_STATIC);
525 sqlite3VdbeSetColName(sParse.pVdbe, 3, COLNAME_NAME, "p2", P3_STATIC);
526 sqlite3VdbeSetColName(sParse.pVdbe, 4, COLNAME_NAME, "p3", P3_STATIC);
drhecc92422005-09-10 16:46:12 +0000527 }
danielk1977fa256a32005-05-25 04:11:56 +0000528 }
529#endif
530
danielk1977fa256a32005-05-25 04:11:56 +0000531 if( sqlite3SafetyOff(db) ){
532 rc = SQLITE_MISUSE;
533 }
534 if( rc==SQLITE_OK ){
535 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
536 }else if( sParse.pVdbe ){
537 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
538 }
539
540 if( zErrMsg ){
541 sqlite3Error(db, rc, "%s", zErrMsg);
542 sqliteFree(zErrMsg);
543 }else{
544 sqlite3Error(db, rc, 0);
545 }
danielk1977261919c2005-12-06 12:52:59 +0000546
danielk197754f01982006-01-18 15:25:17 +0000547 rc = sqlite3ApiExit(db, rc);
drh4b494d62006-01-12 12:43:36 +0000548 sqlite3ReleaseThreadData();
drh4ac285a2006-09-15 07:28:50 +0000549 assert( (rc&db->errMask)==rc );
danielk1977fa256a32005-05-25 04:11:56 +0000550 return rc;
551}
552
553#ifndef SQLITE_OMIT_UTF16
554/*
555** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
556*/
557int sqlite3_prepare16(
558 sqlite3 *db, /* Database handle. */
559 const void *zSql, /* UTF-8 encoded SQL statement. */
560 int nBytes, /* Length of zSql in bytes. */
561 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
562 const void **pzTail /* OUT: End of parsed string */
563){
564 /* This function currently works by first transforming the UTF-16
565 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
566 ** tricky bit is figuring out the pointer to return in *pzTail.
567 */
danielk197754f01982006-01-18 15:25:17 +0000568 char *zSql8;
danielk1977c87d34d2006-01-06 13:00:28 +0000569 const char *zTail8 = 0;
danielk197754f01982006-01-18 15:25:17 +0000570 int rc = SQLITE_OK;
danielk1977fa256a32005-05-25 04:11:56 +0000571
572 if( sqlite3SafetyCheck(db) ){
573 return SQLITE_MISUSE;
574 }
drhaf9a7c22005-12-15 03:04:10 +0000575 zSql8 = sqlite3utf16to8(zSql, nBytes);
danielk197754f01982006-01-18 15:25:17 +0000576 if( zSql8 ){
577 rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);
danielk1977fa256a32005-05-25 04:11:56 +0000578 }
danielk1977fa256a32005-05-25 04:11:56 +0000579
580 if( zTail8 && pzTail ){
581 /* If sqlite3_prepare returns a tail pointer, we calculate the
582 ** equivalent pointer into the UTF-16 string by counting the unicode
583 ** characters between zSql8 and zTail8, and then returning a pointer
584 ** the same number of characters into the UTF-16 string.
585 */
586 int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8);
587 *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed);
588 }
drhaf9a7c22005-12-15 03:04:10 +0000589 sqliteFree(zSql8);
danielk197754f01982006-01-18 15:25:17 +0000590 return sqlite3ApiExit(db, rc);
danielk1977fa256a32005-05-25 04:11:56 +0000591}
592#endif /* SQLITE_OMIT_UTF16 */