blob: 564aff53b8941c01e4f273c2a84cd87068731e1f [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**
danielk1977c87d34d2006-01-06 13:00:28 +000016** $Id: prepare.c,v 1.14 2006/01/06 13:00:30 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){
danielk1977261919c2005-12-06 12:52:59 +000027 if( !sqlite3Tsd()->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
danielk1977da184232006-01-05 11:34:32 +000052 if( sqlite3Tsd()->mallocFailed ){
53 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 ){
79 sqlite3Tsd()->mallocFailed = 1;
80 }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
danielk1977da184232006-01-05 11:34:32 +0000158 if( 0==db->aDb[iDb].pSchema ){
159 DbSchema *pS = sqlite3BtreeSchema(db->aDb[iDb].pBt, sizeof(DbSchema),
160 sqlite3SchemaFree);
161 db->aDb[iDb].pSchema = pS;
162 if( !pS ){
163 return SQLITE_NOMEM;
164 }else if( pS->file_format!=0 ){
165 return SQLITE_OK;
166 }else{
167 sqlite3HashInit(&pS->tblHash, SQLITE_HASH_STRING, 0);
168 sqlite3HashInit(&pS->idxHash, SQLITE_HASH_STRING, 0);
169 sqlite3HashInit(&pS->trigHash, SQLITE_HASH_STRING, 0);
170 sqlite3HashInit(&pS->aFKey, SQLITE_HASH_STRING, 1);
171 }
172 }
173
danielk1977fa256a32005-05-25 04:11:56 +0000174 /* zMasterSchema and zInitScript are set to point at the master schema
175 ** and initialisation script appropriate for the database being
176 ** initialised. zMasterName is the name of the master table.
177 */
178 if( !OMIT_TEMPDB && iDb==1 ){
179 zMasterSchema = temp_master_schema;
180 }else{
181 zMasterSchema = master_schema;
182 }
183 zMasterName = SCHEMA_TABLE(iDb);
184
185 /* Construct the schema tables. */
186 sqlite3SafetyOff(db);
187 azArg[0] = zMasterName;
188 azArg[1] = "1";
189 azArg[2] = zMasterSchema;
190 sprintf(zDbNum, "%d", iDb);
191 azArg[3] = zDbNum;
192 azArg[4] = 0;
193 initData.db = db;
194 initData.pzErrMsg = pzErrMsg;
195 rc = sqlite3InitCallback(&initData, 4, (char **)azArg, 0);
196 if( rc!=SQLITE_OK ){
197 sqlite3SafetyOn(db);
198 return rc;
199 }
200 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
201 if( pTab ){
202 pTab->readOnly = 1;
203 }
204 sqlite3SafetyOn(db);
205
206 /* Create a cursor to hold the database open
207 */
drhfdd6e852005-12-16 01:06:16 +0000208 pDb = &db->aDb[iDb];
209 if( pDb->pBt==0 ){
danielk1977fa256a32005-05-25 04:11:56 +0000210 if( !OMIT_TEMPDB && iDb==1 ) DbSetProperty(db, 1, DB_SchemaLoaded);
211 return SQLITE_OK;
212 }
drhfdd6e852005-12-16 01:06:16 +0000213 rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, 0, &curMain);
danielk1977fa256a32005-05-25 04:11:56 +0000214 if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
215 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
216 return rc;
217 }
218
219 /* Get the database meta information.
220 **
221 ** Meta values are as follows:
222 ** meta[0] Schema cookie. Changes with each schema change.
223 ** meta[1] File format of schema layer.
224 ** meta[2] Size of the page cache.
225 ** meta[3] Use freelist if 0. Autovacuum if greater than zero.
226 ** meta[4] Db text encoding. 1:UTF-8 3:UTF-16 LE 4:UTF-16 BE
227 ** meta[5] The user cookie. Used by the application.
228 ** meta[6]
229 ** meta[7]
230 ** meta[8]
231 ** meta[9]
232 **
233 ** Note: The hash defined SQLITE_UTF* symbols in sqliteInt.h correspond to
234 ** the possible values of meta[4].
235 */
236 if( rc==SQLITE_OK ){
237 int i;
238 for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
drhfdd6e852005-12-16 01:06:16 +0000239 rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
danielk1977fa256a32005-05-25 04:11:56 +0000240 }
241 if( rc ){
242 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
243 sqlite3BtreeCloseCursor(curMain);
244 return rc;
245 }
246 }else{
247 memset(meta, 0, sizeof(meta));
248 }
danielk1977da184232006-01-05 11:34:32 +0000249 pDb->pSchema->schema_cookie = meta[0];
danielk1977fa256a32005-05-25 04:11:56 +0000250
251 /* If opening a non-empty database, check the text encoding. For the
252 ** main database, set sqlite3.enc to the encoding of the main database.
253 ** For an attached db, it is an error if the encoding is not the same
254 ** as sqlite3.enc.
255 */
256 if( meta[4] ){ /* text encoding */
257 if( iDb==0 ){
258 /* If opening the main database, set db->enc. */
259 db->enc = (u8)meta[4];
260 db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0);
261 }else{
262 /* If opening an attached database, the encoding much match db->enc */
263 if( meta[4]!=db->enc ){
264 sqlite3BtreeCloseCursor(curMain);
265 sqlite3SetString(pzErrMsg, "attached databases must use the same"
266 " text encoding as main database", (char*)0);
267 return SQLITE_ERROR;
268 }
269 }
270 }
271
272 size = meta[2];
273 if( size==0 ){ size = MAX_PAGES; }
drhfdd6e852005-12-16 01:06:16 +0000274 pDb->cache_size = size;
275 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->cache_size);
danielk1977fa256a32005-05-25 04:11:56 +0000276
277 /*
278 ** file_format==1 Version 3.0.0.
drhfdd6e852005-12-16 01:06:16 +0000279 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
280 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
drhd946db02005-12-29 19:23:06 +0000281 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
danielk1977fa256a32005-05-25 04:11:56 +0000282 */
danielk1977da184232006-01-05 11:34:32 +0000283 pDb->pSchema->file_format = meta[1];
284 if( pDb->pSchema->file_format==0 ){
285 pDb->pSchema->file_format = 1;
drhfdd6e852005-12-16 01:06:16 +0000286 }
danielk1977da184232006-01-05 11:34:32 +0000287 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
danielk1977fa256a32005-05-25 04:11:56 +0000288 sqlite3BtreeCloseCursor(curMain);
289 sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
290 return SQLITE_ERROR;
291 }
292
danielk1977fa256a32005-05-25 04:11:56 +0000293
294 /* Read the schema information out of the schema tables
295 */
296 assert( db->init.busy );
297 if( rc==SQLITE_EMPTY ){
298 /* For an empty database, there is nothing to read */
299 rc = SQLITE_OK;
300 }else{
301 char *zSql;
302 zSql = sqlite3MPrintf(
303 "SELECT name, rootpage, sql, '%s' FROM '%q'.%s",
304 zDbNum, db->aDb[iDb].zName, zMasterName);
305 sqlite3SafetyOff(db);
306 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
307 sqlite3SafetyOn(db);
308 sqliteFree(zSql);
drh497e4462005-07-23 03:18:40 +0000309#ifndef SQLITE_OMIT_ANALYZE
310 if( rc==SQLITE_OK ){
311 sqlite3AnalysisLoad(db, iDb);
312 }
313#endif
danielk1977fa256a32005-05-25 04:11:56 +0000314 sqlite3BtreeCloseCursor(curMain);
315 }
danielk1977261919c2005-12-06 12:52:59 +0000316 if( sqlite3Tsd()->mallocFailed ){
danielk1977fa256a32005-05-25 04:11:56 +0000317 sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
318 rc = SQLITE_NOMEM;
319 sqlite3ResetInternalSchema(db, 0);
320 }
321 if( rc==SQLITE_OK ){
322 DbSetProperty(db, iDb, DB_SchemaLoaded);
323 }else{
324 sqlite3ResetInternalSchema(db, iDb);
325 }
326 return rc;
327}
328
329/*
330** Initialize all database files - the main database file, the file
331** used to store temporary tables, and any additional database files
332** created using ATTACH statements. Return a success code. If an
333** error occurs, write an error message into *pzErrMsg.
334**
335** After the database is initialized, the SQLITE_Initialized
336** bit is set in the flags field of the sqlite structure.
337*/
338int sqlite3Init(sqlite3 *db, char **pzErrMsg){
339 int i, rc;
340
341 if( db->init.busy ) return SQLITE_OK;
342 assert( (db->flags & SQLITE_Initialized)==0 );
343 rc = SQLITE_OK;
344 db->init.busy = 1;
345 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
346 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
347 rc = sqlite3InitOne(db, i, pzErrMsg);
348 if( rc ){
349 sqlite3ResetInternalSchema(db, i);
350 }
351 }
352
353 /* Once all the other databases have been initialised, load the schema
354 ** for the TEMP database. This is loaded last, as the TEMP database
355 ** schema may contain references to objects in other databases.
356 */
357#ifndef SQLITE_OMIT_TEMPDB
358 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
359 rc = sqlite3InitOne(db, 1, pzErrMsg);
360 if( rc ){
361 sqlite3ResetInternalSchema(db, 1);
362 }
363 }
364#endif
365
366 db->init.busy = 0;
367 if( rc==SQLITE_OK ){
368 db->flags |= SQLITE_Initialized;
369 sqlite3CommitInternalChanges(db);
370 }
371
372 if( rc!=SQLITE_OK ){
373 db->flags &= ~SQLITE_Initialized;
374 }
375 return rc;
376}
377
378/*
379** This routine is a no-op if the database schema is already initialised.
380** Otherwise, the schema is loaded. An error code is returned.
381*/
382int sqlite3ReadSchema(Parse *pParse){
383 int rc = SQLITE_OK;
384 sqlite3 *db = pParse->db;
385 if( !db->init.busy ){
386 if( (db->flags & SQLITE_Initialized)==0 ){
387 rc = sqlite3Init(db, &pParse->zErrMsg);
388 }
389 }
drh8b3d9902005-08-19 00:14:42 +0000390 assert( rc!=SQLITE_OK || (db->flags & SQLITE_Initialized) || db->init.busy );
danielk1977fa256a32005-05-25 04:11:56 +0000391 if( rc!=SQLITE_OK ){
392 pParse->rc = rc;
393 pParse->nErr++;
394 }
395 return rc;
396}
397
398
399/*
400** Check schema cookies in all databases. If any cookie is out
401** of date, return 0. If all schema cookies are current, return 1.
402*/
403static int schemaIsValid(sqlite3 *db){
404 int iDb;
405 int rc;
406 BtCursor *curTemp;
407 int cookie;
408 int allOk = 1;
409
410 for(iDb=0; allOk && iDb<db->nDb; iDb++){
411 Btree *pBt;
412 pBt = db->aDb[iDb].pBt;
413 if( pBt==0 ) continue;
414 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
415 if( rc==SQLITE_OK ){
416 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
danielk1977da184232006-01-05 11:34:32 +0000417 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].pSchema->schema_cookie ){
danielk1977fa256a32005-05-25 04:11:56 +0000418 allOk = 0;
419 }
420 sqlite3BtreeCloseCursor(curTemp);
421 }
422 }
423 return allOk;
424}
425
426/*
427** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
428*/
429int sqlite3_prepare(
430 sqlite3 *db, /* Database handle. */
431 const char *zSql, /* UTF-8 encoded SQL statement. */
432 int nBytes, /* Length of zSql in bytes. */
433 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
434 const char** pzTail /* OUT: End of parsed string */
435){
436 Parse sParse;
437 char *zErrMsg = 0;
438 int rc = SQLITE_OK;
danielk1977c87d34d2006-01-06 13:00:28 +0000439 int i;
danielk1977fa256a32005-05-25 04:11:56 +0000440
danielk1977c87d34d2006-01-06 13:00:28 +0000441 assert( !sqlite3Tsd()->mallocFailed );
danielk1977fa256a32005-05-25 04:11:56 +0000442
443 assert( ppStmt );
444 *ppStmt = 0;
445 if( sqlite3SafetyOn(db) ){
446 return SQLITE_MISUSE;
447 }
448
danielk1977c87d34d2006-01-06 13:00:28 +0000449 /* If any attached database schemas are locked, do not proceed with
450 ** compilation. Instead return SQLITE_LOCKED immediately.
451 */
452 for(i=0; i<db->nDb; i++) {
453 Btree *pBt = db->aDb[i].pBt;
454 if( pBt && sqlite3BtreeSchemaLocked(pBt) ){
455 const char *zDb = db->aDb[i].zName;
456 sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb);
457 sqlite3SafetyOff(db);
458 return SQLITE_LOCKED;
459 }
460 }
461
danielk1977fa256a32005-05-25 04:11:56 +0000462 memset(&sParse, 0, sizeof(sParse));
463 sParse.db = db;
464 sqlite3RunParser(&sParse, zSql, &zErrMsg);
465
danielk1977261919c2005-12-06 12:52:59 +0000466 if( sqlite3Tsd()->mallocFailed ){
467 sParse.rc = SQLITE_NOMEM;
danielk1977fa256a32005-05-25 04:11:56 +0000468 }
469 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
drh4d91a702006-01-04 15:54:36 +0000470 if( sParse.checkSchema && !schemaIsValid(db) ){
danielk1977fa256a32005-05-25 04:11:56 +0000471 sParse.rc = SQLITE_SCHEMA;
472 }
473 if( sParse.rc==SQLITE_SCHEMA ){
474 sqlite3ResetInternalSchema(db, 0);
475 }
476 if( pzTail ) *pzTail = sParse.zTail;
477 rc = sParse.rc;
478
479#ifndef SQLITE_OMIT_EXPLAIN
480 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
drhecc92422005-09-10 16:46:12 +0000481 if( sParse.explain==2 ){
482 sqlite3VdbeSetNumCols(sParse.pVdbe, 3);
483 sqlite3VdbeSetColName(sParse.pVdbe, 0, "order", P3_STATIC);
484 sqlite3VdbeSetColName(sParse.pVdbe, 1, "from", P3_STATIC);
485 sqlite3VdbeSetColName(sParse.pVdbe, 2, "detail", P3_STATIC);
486 }else{
487 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
488 sqlite3VdbeSetColName(sParse.pVdbe, 0, "addr", P3_STATIC);
489 sqlite3VdbeSetColName(sParse.pVdbe, 1, "opcode", P3_STATIC);
490 sqlite3VdbeSetColName(sParse.pVdbe, 2, "p1", P3_STATIC);
491 sqlite3VdbeSetColName(sParse.pVdbe, 3, "p2", P3_STATIC);
492 sqlite3VdbeSetColName(sParse.pVdbe, 4, "p3", P3_STATIC);
493 }
danielk1977fa256a32005-05-25 04:11:56 +0000494 }
495#endif
496
danielk1977fa256a32005-05-25 04:11:56 +0000497 if( sqlite3SafetyOff(db) ){
498 rc = SQLITE_MISUSE;
499 }
500 if( rc==SQLITE_OK ){
501 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
502 }else if( sParse.pVdbe ){
503 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
504 }
505
506 if( zErrMsg ){
507 sqlite3Error(db, rc, "%s", zErrMsg);
508 sqliteFree(zErrMsg);
509 }else{
510 sqlite3Error(db, rc, 0);
511 }
danielk1977261919c2005-12-06 12:52:59 +0000512
513 /* We must check for malloc failure last of all, in case malloc() failed
514 ** inside of the sqlite3Error() call above or something.
515 */
516 if( sqlite3Tsd()->mallocFailed ){
517 rc = SQLITE_NOMEM;
518 sqlite3Error(db, rc, 0);
519 }
520
danielk19772e588c72005-12-09 14:25:08 +0000521 sqlite3MallocClearFailed();
danielk1977fa256a32005-05-25 04:11:56 +0000522 return rc;
523}
524
525#ifndef SQLITE_OMIT_UTF16
526/*
527** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
528*/
529int sqlite3_prepare16(
530 sqlite3 *db, /* Database handle. */
531 const void *zSql, /* UTF-8 encoded SQL statement. */
532 int nBytes, /* Length of zSql in bytes. */
533 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
534 const void **pzTail /* OUT: End of parsed string */
535){
536 /* This function currently works by first transforming the UTF-16
537 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
538 ** tricky bit is figuring out the pointer to return in *pzTail.
539 */
drhaf9a7c22005-12-15 03:04:10 +0000540 char *zSql8 = 0;
danielk1977c87d34d2006-01-06 13:00:28 +0000541 const char *zTail8 = 0;
danielk1977fa256a32005-05-25 04:11:56 +0000542 int rc;
danielk1977fa256a32005-05-25 04:11:56 +0000543
544 if( sqlite3SafetyCheck(db) ){
545 return SQLITE_MISUSE;
546 }
drhaf9a7c22005-12-15 03:04:10 +0000547 zSql8 = sqlite3utf16to8(zSql, nBytes);
danielk1977fa256a32005-05-25 04:11:56 +0000548 if( !zSql8 ){
549 sqlite3Error(db, SQLITE_NOMEM, 0);
550 return SQLITE_NOMEM;
551 }
552 rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);
553
554 if( zTail8 && pzTail ){
555 /* If sqlite3_prepare returns a tail pointer, we calculate the
556 ** equivalent pointer into the UTF-16 string by counting the unicode
557 ** characters between zSql8 and zTail8, and then returning a pointer
558 ** the same number of characters into the UTF-16 string.
559 */
560 int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8);
561 *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed);
562 }
drhaf9a7c22005-12-15 03:04:10 +0000563 sqliteFree(zSql8);
danielk1977fa256a32005-05-25 04:11:56 +0000564 return rc;
565}
566#endif /* SQLITE_OMIT_UTF16 */