blob: 5d65dc023dbe3543a13bd2294934b25056b2fc29 [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**
drh15ca1df2006-07-26 13:43:30 +000016** $Id: prepare.c,v 1.37 2006/07/26 13:43:31 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() ){
danielk1977da184232006-01-05 11:34:32 +000055 return SQLITE_NOMEM;
56 }
57
danielk1977fa256a32005-05-25 04:11:56 +000058 assert( argc==4 );
59 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
60 if( argv[1]==0 || argv[3]==0 ){
61 corruptSchema(pData, 0);
62 return 1;
63 }
64 iDb = atoi(argv[3]);
65 assert( iDb>=0 && iDb<db->nDb );
66 if( argv[2] && argv[2][0] ){
67 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
68 ** But because db->init.busy is set to 1, no VDBE code is generated
69 ** or executed. All the parser does is build the internal data
70 ** structures that describe the table, index, or view.
71 */
72 char *zErr;
73 int rc;
74 assert( db->init.busy );
75 db->init.iDb = iDb;
76 db->init.newTnum = atoi(argv[1]);
77 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
78 db->init.iDb = 0;
drh43617e92006-03-06 20:55:46 +000079 assert( rc!=SQLITE_OK || zErr==0 );
danielk1977fa256a32005-05-25 04:11:56 +000080 if( SQLITE_OK!=rc ){
drh15ca1df2006-07-26 13:43:30 +000081 pData->rc = rc;
danielk1977261919c2005-12-06 12:52:59 +000082 if( rc==SQLITE_NOMEM ){
danielk19779e128002006-01-18 16:51:35 +000083 sqlite3FailedMalloc();
drh15ca1df2006-07-26 13:43:30 +000084 }else if( rc!=SQLITE_INTERRUPT ){
danielk19779e128002006-01-18 16:51:35 +000085 corruptSchema(pData, zErr);
danielk1977261919c2005-12-06 12:52:59 +000086 }
danielk1977fa256a32005-05-25 04:11:56 +000087 sqlite3_free(zErr);
drh15ca1df2006-07-26 13:43:30 +000088 return 1;
danielk1977fa256a32005-05-25 04:11:56 +000089 }
90 }else{
91 /* If the SQL column is blank it means this is an index that
92 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
93 ** constraint for a CREATE TABLE. The index should have already
94 ** been created when we processed the CREATE TABLE. All we have
95 ** to do here is record the root page number for that index.
96 */
97 Index *pIndex;
98 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
99 if( pIndex==0 || pIndex->tnum!=0 ){
100 /* This can occur if there exists an index on a TEMP table which
101 ** has the same name as another index on a permanent index. Since
102 ** the permanent table is hidden by the TEMP table, we can also
103 ** safely ignore the index on the permanent table.
104 */
105 /* Do Nothing */;
106 }else{
107 pIndex->tnum = atoi(argv[1]);
108 }
109 }
110 return 0;
111}
112
113/*
114** Attempt to read the database schema and initialize internal
115** data structures for a single database file. The index of the
116** database file is given by iDb. iDb==0 is used for the main
117** database. iDb==1 should never be used. iDb>=2 is used for
118** auxiliary databases. Return one of the SQLITE_ error codes to
119** indicate success or failure.
120*/
121static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
122 int rc;
123 BtCursor *curMain;
124 int size;
125 Table *pTab;
drhfdd6e852005-12-16 01:06:16 +0000126 Db *pDb;
danielk1977fa256a32005-05-25 04:11:56 +0000127 char const *azArg[5];
128 char zDbNum[30];
129 int meta[10];
130 InitData initData;
131 char const *zMasterSchema;
132 char const *zMasterName = SCHEMA_TABLE(iDb);
133
134 /*
135 ** The master database table has a structure like this
136 */
137 static const char master_schema[] =
138 "CREATE TABLE sqlite_master(\n"
139 " type text,\n"
140 " name text,\n"
141 " tbl_name text,\n"
142 " rootpage integer,\n"
143 " sql text\n"
144 ")"
145 ;
146#ifndef SQLITE_OMIT_TEMPDB
147 static const char temp_master_schema[] =
148 "CREATE TEMP TABLE sqlite_temp_master(\n"
149 " type text,\n"
150 " name text,\n"
151 " tbl_name text,\n"
152 " rootpage integer,\n"
153 " sql text\n"
154 ")"
155 ;
156#else
157 #define temp_master_schema 0
158#endif
159
160 assert( iDb>=0 && iDb<db->nDb );
danielk197714db2662006-01-09 16:12:04 +0000161 assert( db->aDb[iDb].pSchema );
danielk1977da184232006-01-05 11:34:32 +0000162
danielk1977fa256a32005-05-25 04:11:56 +0000163 /* zMasterSchema and zInitScript are set to point at the master schema
164 ** and initialisation script appropriate for the database being
165 ** initialised. zMasterName is the name of the master table.
166 */
167 if( !OMIT_TEMPDB && iDb==1 ){
168 zMasterSchema = temp_master_schema;
169 }else{
170 zMasterSchema = master_schema;
171 }
172 zMasterName = SCHEMA_TABLE(iDb);
173
174 /* Construct the schema tables. */
175 sqlite3SafetyOff(db);
176 azArg[0] = zMasterName;
177 azArg[1] = "1";
178 azArg[2] = zMasterSchema;
179 sprintf(zDbNum, "%d", iDb);
180 azArg[3] = zDbNum;
181 azArg[4] = 0;
182 initData.db = db;
183 initData.pzErrMsg = pzErrMsg;
184 rc = sqlite3InitCallback(&initData, 4, (char **)azArg, 0);
drh15ca1df2006-07-26 13:43:30 +0000185 if( rc ){
danielk1977fa256a32005-05-25 04:11:56 +0000186 sqlite3SafetyOn(db);
drh15ca1df2006-07-26 13:43:30 +0000187 return initData.rc;
danielk1977fa256a32005-05-25 04:11:56 +0000188 }
189 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
190 if( pTab ){
191 pTab->readOnly = 1;
192 }
193 sqlite3SafetyOn(db);
194
195 /* Create a cursor to hold the database open
196 */
drhfdd6e852005-12-16 01:06:16 +0000197 pDb = &db->aDb[iDb];
198 if( pDb->pBt==0 ){
danielk1977b82e7ed2006-01-11 14:09:31 +0000199 if( !OMIT_TEMPDB && iDb==1 ){
200 DbSetProperty(db, 1, DB_SchemaLoaded);
201 }
danielk1977fa256a32005-05-25 04:11:56 +0000202 return SQLITE_OK;
203 }
drhfdd6e852005-12-16 01:06:16 +0000204 rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, 0, &curMain);
danielk1977fa256a32005-05-25 04:11:56 +0000205 if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
206 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
207 return rc;
208 }
209
210 /* Get the database meta information.
211 **
212 ** Meta values are as follows:
213 ** meta[0] Schema cookie. Changes with each schema change.
214 ** meta[1] File format of schema layer.
215 ** meta[2] Size of the page cache.
216 ** meta[3] Use freelist if 0. Autovacuum if greater than zero.
drh8159a352006-05-23 23:22:29 +0000217 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
danielk1977fa256a32005-05-25 04:11:56 +0000218 ** meta[5] The user cookie. Used by the application.
219 ** meta[6]
220 ** meta[7]
221 ** meta[8]
222 ** meta[9]
223 **
drhf248e212006-01-25 22:50:38 +0000224 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
danielk1977fa256a32005-05-25 04:11:56 +0000225 ** the possible values of meta[4].
226 */
227 if( rc==SQLITE_OK ){
228 int i;
229 for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
drhfdd6e852005-12-16 01:06:16 +0000230 rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
danielk1977fa256a32005-05-25 04:11:56 +0000231 }
232 if( rc ){
233 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
234 sqlite3BtreeCloseCursor(curMain);
235 return rc;
236 }
237 }else{
238 memset(meta, 0, sizeof(meta));
239 }
danielk1977da184232006-01-05 11:34:32 +0000240 pDb->pSchema->schema_cookie = meta[0];
danielk1977fa256a32005-05-25 04:11:56 +0000241
242 /* If opening a non-empty database, check the text encoding. For the
243 ** main database, set sqlite3.enc to the encoding of the main database.
244 ** For an attached db, it is an error if the encoding is not the same
245 ** as sqlite3.enc.
246 */
247 if( meta[4] ){ /* text encoding */
248 if( iDb==0 ){
danielk197714db2662006-01-09 16:12:04 +0000249 /* If opening the main database, set ENC(db). */
250 ENC(db) = (u8)meta[4];
danielk1977b3bf5562006-01-10 17:58:23 +0000251 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0);
danielk1977fa256a32005-05-25 04:11:56 +0000252 }else{
danielk197714db2662006-01-09 16:12:04 +0000253 /* If opening an attached database, the encoding much match ENC(db) */
254 if( meta[4]!=ENC(db) ){
danielk1977fa256a32005-05-25 04:11:56 +0000255 sqlite3BtreeCloseCursor(curMain);
256 sqlite3SetString(pzErrMsg, "attached databases must use the same"
257 " text encoding as main database", (char*)0);
258 return SQLITE_ERROR;
259 }
260 }
danielk1977b82e7ed2006-01-11 14:09:31 +0000261 }else{
262 DbSetProperty(db, iDb, DB_Empty);
danielk1977fa256a32005-05-25 04:11:56 +0000263 }
danielk197714db2662006-01-09 16:12:04 +0000264 pDb->pSchema->enc = ENC(db);
danielk1977fa256a32005-05-25 04:11:56 +0000265
266 size = meta[2];
267 if( size==0 ){ size = MAX_PAGES; }
danielk197714db2662006-01-09 16:12:04 +0000268 pDb->pSchema->cache_size = size;
269 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
danielk1977fa256a32005-05-25 04:11:56 +0000270
271 /*
272 ** file_format==1 Version 3.0.0.
drhfdd6e852005-12-16 01:06:16 +0000273 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
274 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
drhd946db02005-12-29 19:23:06 +0000275 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
danielk1977fa256a32005-05-25 04:11:56 +0000276 */
danielk1977da184232006-01-05 11:34:32 +0000277 pDb->pSchema->file_format = meta[1];
278 if( pDb->pSchema->file_format==0 ){
279 pDb->pSchema->file_format = 1;
drhfdd6e852005-12-16 01:06:16 +0000280 }
danielk1977da184232006-01-05 11:34:32 +0000281 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
danielk1977fa256a32005-05-25 04:11:56 +0000282 sqlite3BtreeCloseCursor(curMain);
283 sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
284 return SQLITE_ERROR;
285 }
286
danielk1977fa256a32005-05-25 04:11:56 +0000287
288 /* Read the schema information out of the schema tables
289 */
290 assert( db->init.busy );
291 if( rc==SQLITE_EMPTY ){
292 /* For an empty database, there is nothing to read */
293 rc = SQLITE_OK;
294 }else{
295 char *zSql;
296 zSql = sqlite3MPrintf(
297 "SELECT name, rootpage, sql, '%s' FROM '%q'.%s",
298 zDbNum, db->aDb[iDb].zName, zMasterName);
299 sqlite3SafetyOff(db);
300 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
drh15ca1df2006-07-26 13:43:30 +0000301 if( rc==SQLITE_ABORT ) rc = initData.rc;
danielk1977fa256a32005-05-25 04:11:56 +0000302 sqlite3SafetyOn(db);
303 sqliteFree(zSql);
drh497e4462005-07-23 03:18:40 +0000304#ifndef SQLITE_OMIT_ANALYZE
305 if( rc==SQLITE_OK ){
306 sqlite3AnalysisLoad(db, iDb);
307 }
308#endif
danielk1977fa256a32005-05-25 04:11:56 +0000309 sqlite3BtreeCloseCursor(curMain);
310 }
danielk19779e128002006-01-18 16:51:35 +0000311 if( sqlite3MallocFailed() ){
312 /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */
danielk1977fa256a32005-05-25 04:11:56 +0000313 rc = SQLITE_NOMEM;
314 sqlite3ResetInternalSchema(db, 0);
315 }
316 if( rc==SQLITE_OK ){
317 DbSetProperty(db, iDb, DB_SchemaLoaded);
318 }else{
319 sqlite3ResetInternalSchema(db, iDb);
320 }
321 return rc;
322}
323
324/*
325** Initialize all database files - the main database file, the file
326** used to store temporary tables, and any additional database files
327** created using ATTACH statements. Return a success code. If an
328** error occurs, write an error message into *pzErrMsg.
329**
danielk1977e7259292006-01-13 06:33:23 +0000330** After a database is initialized, the DB_SchemaLoaded bit is set
331** bit is set in the flags field of the Db structure. If the database
332** file was of zero-length, then the DB_Empty flag is also set.
danielk1977fa256a32005-05-25 04:11:56 +0000333*/
334int sqlite3Init(sqlite3 *db, char **pzErrMsg){
335 int i, rc;
danielk1977b82e7ed2006-01-11 14:09:31 +0000336 int called_initone = 0;
danielk1977fa256a32005-05-25 04:11:56 +0000337
338 if( db->init.busy ) return SQLITE_OK;
danielk1977fa256a32005-05-25 04:11:56 +0000339 rc = SQLITE_OK;
340 db->init.busy = 1;
341 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
342 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
343 rc = sqlite3InitOne(db, i, pzErrMsg);
344 if( rc ){
345 sqlite3ResetInternalSchema(db, i);
346 }
danielk1977b82e7ed2006-01-11 14:09:31 +0000347 called_initone = 1;
danielk1977fa256a32005-05-25 04:11:56 +0000348 }
349
350 /* Once all the other databases have been initialised, load the schema
351 ** for the TEMP database. This is loaded last, as the TEMP database
352 ** schema may contain references to objects in other databases.
353 */
354#ifndef SQLITE_OMIT_TEMPDB
355 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
356 rc = sqlite3InitOne(db, 1, pzErrMsg);
357 if( rc ){
358 sqlite3ResetInternalSchema(db, 1);
359 }
danielk1977b82e7ed2006-01-11 14:09:31 +0000360 called_initone = 1;
danielk1977fa256a32005-05-25 04:11:56 +0000361 }
362#endif
363
364 db->init.busy = 0;
danielk1977b82e7ed2006-01-11 14:09:31 +0000365 if( rc==SQLITE_OK && called_initone ){
danielk1977fa256a32005-05-25 04:11:56 +0000366 sqlite3CommitInternalChanges(db);
367 }
368
danielk1977b82e7ed2006-01-11 14:09:31 +0000369 return rc;
danielk1977fa256a32005-05-25 04:11:56 +0000370}
371
372/*
373** This routine is a no-op if the database schema is already initialised.
374** Otherwise, the schema is loaded. An error code is returned.
375*/
376int sqlite3ReadSchema(Parse *pParse){
377 int rc = SQLITE_OK;
378 sqlite3 *db = pParse->db;
379 if( !db->init.busy ){
danielk1977b82e7ed2006-01-11 14:09:31 +0000380 rc = sqlite3Init(db, &pParse->zErrMsg);
danielk1977fa256a32005-05-25 04:11:56 +0000381 }
danielk1977fa256a32005-05-25 04:11:56 +0000382 if( rc!=SQLITE_OK ){
383 pParse->rc = rc;
384 pParse->nErr++;
385 }
386 return rc;
387}
388
389
390/*
391** Check schema cookies in all databases. If any cookie is out
392** of date, return 0. If all schema cookies are current, return 1.
393*/
394static int schemaIsValid(sqlite3 *db){
395 int iDb;
396 int rc;
397 BtCursor *curTemp;
398 int cookie;
399 int allOk = 1;
400
401 for(iDb=0; allOk && iDb<db->nDb; iDb++){
402 Btree *pBt;
403 pBt = db->aDb[iDb].pBt;
404 if( pBt==0 ) continue;
405 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
406 if( rc==SQLITE_OK ){
407 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
danielk1977da184232006-01-05 11:34:32 +0000408 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].pSchema->schema_cookie ){
danielk1977fa256a32005-05-25 04:11:56 +0000409 allOk = 0;
410 }
411 sqlite3BtreeCloseCursor(curTemp);
412 }
413 }
414 return allOk;
415}
416
417/*
drhf248e212006-01-25 22:50:38 +0000418** Convert a schema pointer into the iDb index that indicates
419** which database file in db->aDb[] the schema refers to.
420**
421** If the same database is attached more than once, the first
422** attached database is returned.
423*/
danielk1977e501b892006-01-09 06:29:47 +0000424int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
drh198bf392006-01-06 21:52:49 +0000425 int i = -1000000;
426
427 /* If pSchema is NULL, then return -1000000. This happens when code in
428 ** expr.c is trying to resolve a reference to a transient table (i.e. one
429 ** created by a sub-select). In this case the return value of this
430 ** function should never be used.
431 **
432 ** We return -1000000 instead of the more usual -1 simply because using
433 ** -1000000 as incorrectly using -1000000 index into db->aDb[] is much
434 ** more likely to cause a segfault than -1 (of course there are assert()
435 ** statements too, but it never hurts to play the odds).
436 */
437 if( pSchema ){
438 for(i=0; i<db->nDb; i++){
439 if( db->aDb[i].pSchema==pSchema ){
440 break;
441 }
442 }
443 assert( i>=0 &&i>=0 && i<db->nDb );
444 }
445 return i;
446}
447
448/*
danielk1977fa256a32005-05-25 04:11:56 +0000449** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
450*/
451int sqlite3_prepare(
452 sqlite3 *db, /* Database handle. */
453 const char *zSql, /* UTF-8 encoded SQL statement. */
454 int nBytes, /* Length of zSql in bytes. */
455 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
456 const char** pzTail /* OUT: End of parsed string */
457){
458 Parse sParse;
459 char *zErrMsg = 0;
460 int rc = SQLITE_OK;
danielk1977c87d34d2006-01-06 13:00:28 +0000461 int i;
danielk1977fa256a32005-05-25 04:11:56 +0000462
danielk1977efaaf572006-01-16 11:29:19 +0000463 /* Assert that malloc() has not failed */
danielk19779e128002006-01-18 16:51:35 +0000464 assert( !sqlite3MallocFailed() );
danielk1977fa256a32005-05-25 04:11:56 +0000465
466 assert( ppStmt );
467 *ppStmt = 0;
468 if( sqlite3SafetyOn(db) ){
469 return SQLITE_MISUSE;
470 }
471
danielk1977c87d34d2006-01-06 13:00:28 +0000472 /* If any attached database schemas are locked, do not proceed with
473 ** compilation. Instead return SQLITE_LOCKED immediately.
474 */
475 for(i=0; i<db->nDb; i++) {
476 Btree *pBt = db->aDb[i].pBt;
477 if( pBt && sqlite3BtreeSchemaLocked(pBt) ){
478 const char *zDb = db->aDb[i].zName;
479 sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb);
480 sqlite3SafetyOff(db);
481 return SQLITE_LOCKED;
482 }
483 }
484
danielk1977fa256a32005-05-25 04:11:56 +0000485 memset(&sParse, 0, sizeof(sParse));
486 sParse.db = db;
drh9051a422006-01-30 22:35:43 +0000487 if( nBytes>=0 && zSql[nBytes]!=0 ){
488 char *zSqlCopy = sqlite3StrNDup(zSql, nBytes);
489 sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
490 sParse.zTail += zSql - zSqlCopy;
491 sqliteFree(zSqlCopy);
492 }else{
493 sqlite3RunParser(&sParse, zSql, &zErrMsg);
494 }
danielk1977fa256a32005-05-25 04:11:56 +0000495
danielk19779e128002006-01-18 16:51:35 +0000496 if( sqlite3MallocFailed() ){
danielk1977261919c2005-12-06 12:52:59 +0000497 sParse.rc = SQLITE_NOMEM;
danielk1977fa256a32005-05-25 04:11:56 +0000498 }
499 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
drh4d91a702006-01-04 15:54:36 +0000500 if( sParse.checkSchema && !schemaIsValid(db) ){
danielk1977fa256a32005-05-25 04:11:56 +0000501 sParse.rc = SQLITE_SCHEMA;
502 }
503 if( sParse.rc==SQLITE_SCHEMA ){
504 sqlite3ResetInternalSchema(db, 0);
505 }
drh344a6272006-06-26 12:50:09 +0000506 if( sqlite3MallocFailed() ){
507 sParse.rc = SQLITE_NOMEM;
508 }
danielk1977fa256a32005-05-25 04:11:56 +0000509 if( pzTail ) *pzTail = sParse.zTail;
510 rc = sParse.rc;
511
512#ifndef SQLITE_OMIT_EXPLAIN
513 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
drhecc92422005-09-10 16:46:12 +0000514 if( sParse.explain==2 ){
515 sqlite3VdbeSetNumCols(sParse.pVdbe, 3);
danielk1977955de522006-02-10 02:27:42 +0000516 sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "order", P3_STATIC);
517 sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "from", P3_STATIC);
518 sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "detail", P3_STATIC);
drhecc92422005-09-10 16:46:12 +0000519 }else{
520 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
danielk1977955de522006-02-10 02:27:42 +0000521 sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "addr", P3_STATIC);
522 sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "opcode", P3_STATIC);
523 sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "p1", P3_STATIC);
524 sqlite3VdbeSetColName(sParse.pVdbe, 3, COLNAME_NAME, "p2", P3_STATIC);
525 sqlite3VdbeSetColName(sParse.pVdbe, 4, COLNAME_NAME, "p3", P3_STATIC);
drhecc92422005-09-10 16:46:12 +0000526 }
danielk1977fa256a32005-05-25 04:11:56 +0000527 }
528#endif
529
danielk1977fa256a32005-05-25 04:11:56 +0000530 if( sqlite3SafetyOff(db) ){
531 rc = SQLITE_MISUSE;
532 }
533 if( rc==SQLITE_OK ){
534 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
535 }else if( sParse.pVdbe ){
536 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
537 }
538
539 if( zErrMsg ){
540 sqlite3Error(db, rc, "%s", zErrMsg);
541 sqliteFree(zErrMsg);
542 }else{
543 sqlite3Error(db, rc, 0);
544 }
danielk1977261919c2005-12-06 12:52:59 +0000545
danielk197754f01982006-01-18 15:25:17 +0000546 rc = sqlite3ApiExit(db, rc);
drh4b494d62006-01-12 12:43:36 +0000547 sqlite3ReleaseThreadData();
danielk1977fa256a32005-05-25 04:11:56 +0000548 return rc;
549}
550
551#ifndef SQLITE_OMIT_UTF16
552/*
553** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
554*/
555int sqlite3_prepare16(
556 sqlite3 *db, /* Database handle. */
557 const void *zSql, /* UTF-8 encoded SQL statement. */
558 int nBytes, /* Length of zSql in bytes. */
559 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
560 const void **pzTail /* OUT: End of parsed string */
561){
562 /* This function currently works by first transforming the UTF-16
563 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
564 ** tricky bit is figuring out the pointer to return in *pzTail.
565 */
danielk197754f01982006-01-18 15:25:17 +0000566 char *zSql8;
danielk1977c87d34d2006-01-06 13:00:28 +0000567 const char *zTail8 = 0;
danielk197754f01982006-01-18 15:25:17 +0000568 int rc = SQLITE_OK;
danielk1977fa256a32005-05-25 04:11:56 +0000569
570 if( sqlite3SafetyCheck(db) ){
571 return SQLITE_MISUSE;
572 }
drhaf9a7c22005-12-15 03:04:10 +0000573 zSql8 = sqlite3utf16to8(zSql, nBytes);
danielk197754f01982006-01-18 15:25:17 +0000574 if( zSql8 ){
575 rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);
danielk1977fa256a32005-05-25 04:11:56 +0000576 }
danielk1977fa256a32005-05-25 04:11:56 +0000577
578 if( zTail8 && pzTail ){
579 /* If sqlite3_prepare returns a tail pointer, we calculate the
580 ** equivalent pointer into the UTF-16 string by counting the unicode
581 ** characters between zSql8 and zTail8, and then returning a pointer
582 ** the same number of characters into the UTF-16 string.
583 */
584 int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8);
585 *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed);
586 }
drhaf9a7c22005-12-15 03:04:10 +0000587 sqliteFree(zSql8);
danielk197754f01982006-01-18 15:25:17 +0000588 return sqlite3ApiExit(db, rc);
danielk1977fa256a32005-05-25 04:11:56 +0000589}
590#endif /* SQLITE_OMIT_UTF16 */