4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** This file contains the implementation of the sqlite3_prepare()
13 ** interface, and routines that contribute to loading the database schema
18 #include "sqliteInt.h"
23 ** Fill the InitData structure with an error message that indicates
24 ** that the database is corrupt.
26 static void corruptSchema(InitData
*pData
, const char *zExtra
){
27 if( !sqlite3_malloc_failed
){
28 sqlite3SetString(pData
->pzErrMsg
, "malformed database schema",
29 zExtra
!=0 && zExtra
[0]!=0 ? " - " : (char*)0, zExtra
, (char*)0);
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.
38 ** Each callback contains the following information:
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.
47 int sqlite3InitCallback(void *pInit
, int argc
, char **argv
, char **azColName
){
48 InitData
*pData
= (InitData
*)pInit
;
49 sqlite3
*db
= pData
->db
;
53 if( argv
==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
54 if( argv
[1]==0 || argv
[3]==0 ){
55 corruptSchema(pData
, 0);
59 assert( iDb
>=0 && iDb
<db
->nDb
);
60 if( argv
[2] && argv
[2][0] ){
61 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
62 ** But because db->init.busy is set to 1, no VDBE code is generated
63 ** or executed. All the parser does is build the internal data
64 ** structures that describe the table, index, or view.
68 assert( db
->init
.busy
);
70 db
->init
.newTnum
= atoi(argv
[1]);
71 rc
= sqlite3_exec(db
, argv
[2], 0, 0, &zErr
);
74 corruptSchema(pData
, zErr
);
79 /* If the SQL column is blank it means this is an index that
80 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
81 ** constraint for a CREATE TABLE. The index should have already
82 ** been created when we processed the CREATE TABLE. All we have
83 ** to do here is record the root page number for that index.
86 pIndex
= sqlite3FindIndex(db
, argv
[0], db
->aDb
[iDb
].zName
);
87 if( pIndex
==0 || pIndex
->tnum
!=0 ){
88 /* This can occur if there exists an index on a TEMP table which
89 ** has the same name as another index on a permanent index. Since
90 ** the permanent table is hidden by the TEMP table, we can also
91 ** safely ignore the index on the permanent table.
95 pIndex
->tnum
= atoi(argv
[1]);
102 ** Attempt to read the database schema and initialize internal
103 ** data structures for a single database file. The index of the
104 ** database file is given by iDb. iDb==0 is used for the main
105 ** database. iDb==1 should never be used. iDb>=2 is used for
106 ** auxiliary databases. Return one of the SQLITE_ error codes to
107 ** indicate success or failure.
109 static int sqlite3InitOne(sqlite3
*db
, int iDb
, STRPTR
*pzErrMsg
){
114 char const *azArg
[5];
118 char const *zMasterSchema
;
119 char const *zMasterName
= SCHEMA_TABLE(iDb
);
122 ** The master database table has a structure like this
124 static const char master_schema
[] =
125 "CREATE TABLE sqlite_master(\n"
129 " rootpage integer,\n"
133 #ifndef SQLITE_OMIT_TEMPDB
134 static const char temp_master_schema
[] =
135 "CREATE TEMP TABLE sqlite_temp_master(\n"
139 " rootpage integer,\n"
144 #define temp_master_schema 0
147 assert( iDb
>=0 && iDb
<db
->nDb
);
149 /* zMasterSchema and zInitScript are set to point at the master schema
150 ** and initialisation script appropriate for the database being
151 ** initialised. zMasterName is the name of the master table.
153 if( !OMIT_TEMPDB
&& iDb
==1 ){
154 zMasterSchema
= temp_master_schema
;
156 zMasterSchema
= master_schema
;
158 zMasterName
= SCHEMA_TABLE(iDb
);
160 /* Construct the schema tables. */
161 sqlite3SafetyOff(db
);
162 azArg
[0] = zMasterName
;
164 azArg
[2] = zMasterSchema
;
165 sprintf(zDbNum
, "%d", iDb
);
169 initData
.pzErrMsg
= pzErrMsg
;
170 rc
= sqlite3InitCallback(&initData
, 4, (char **)azArg
, 0);
175 pTab
= sqlite3FindTable(db
, zMasterName
, db
->aDb
[iDb
].zName
);
181 /* Create a cursor to hold the database open
183 if( db
->aDb
[iDb
].pBt
==0 ){
184 if( !OMIT_TEMPDB
&& iDb
==1 ) DbSetProperty(db
, 1, DB_SchemaLoaded
);
187 rc
= sqlite3BtreeCursor(db
->aDb
[iDb
].pBt
, MASTER_ROOT
, 0, 0, 0, &curMain
);
188 if( rc
!=SQLITE_OK
&& rc
!=SQLITE_EMPTY
){
189 sqlite3SetString(pzErrMsg
, sqlite3ErrStr(rc
), (char*)0);
193 /* Get the database meta information.
195 ** Meta values are as follows:
196 ** meta[0] Schema cookie. Changes with each schema change.
197 ** meta[1] File format of schema layer.
198 ** meta[2] Size of the page cache.
199 ** meta[3] Use freelist if 0. Autovacuum if greater than zero.
200 ** meta[4] Db text encoding. 1:UTF-8 3:UTF-16 LE 4:UTF-16 BE
201 ** meta[5] The user cookie. Used by the application.
207 ** Note: The hash defined SQLITE_UTF* symbols in sqliteInt.h correspond to
208 ** the possible values of meta[4].
212 for(i
=0; rc
==SQLITE_OK
&& i
<sizeof(meta
)/sizeof(meta
[0]); i
++){
213 rc
= sqlite3BtreeGetMeta(db
->aDb
[iDb
].pBt
, i
+1, (u32
*)&meta
[i
]);
216 sqlite3SetString(pzErrMsg
, sqlite3ErrStr(rc
), (char*)0);
217 sqlite3BtreeCloseCursor(curMain
);
221 memset(meta
, 0, sizeof(meta
));
223 db
->aDb
[iDb
].schema_cookie
= meta
[0];
225 /* If opening a non-empty database, check the text encoding. For the
226 ** main database, set sqlite3.enc to the encoding of the main database.
227 ** For an attached db, it is an error if the encoding is not the same
230 if( meta
[4] ){ /* text encoding */
232 /* If opening the main database, set db->enc. */
233 db
->enc
= (u8
)meta
[4];
234 db
->pDfltColl
= sqlite3FindCollSeq(db
, db
->enc
, "BINARY", 6, 0);
236 /* If opening an attached database, the encoding much match db->enc */
237 if( meta
[4]!=db
->enc
){
238 sqlite3BtreeCloseCursor(curMain
);
239 sqlite3SetString(pzErrMsg
, "attached databases must use the same"
240 " text encoding as main database", (char*)0);
247 if( size
==0 ){ size
= MAX_PAGES
; }
248 db
->aDb
[iDb
].cache_size
= size
;
251 db
->file_format
= meta
[1];
252 if( db
->file_format
==0 ){
253 /* This happens if the database was initially empty */
257 if( db
->file_format
==2 || db
->file_format
==3 ){
258 /* File format 2 is treated exactly as file format 1. New
259 ** databases are created with file format 1.
266 ** file_format==1 Version 3.0.0.
267 ** file_format==2 Version 3.1.3.
268 ** file_format==3 Version 3.1.4.
270 ** Version 3.0 can only use files with file_format==1. Version 3.1.3
271 ** can read and write files with file_format==1 or file_format==2.
272 ** Version 3.1.4 can read and write file formats 1, 2 and 3.
275 sqlite3BtreeCloseCursor(curMain
);
276 sqlite3SetString(pzErrMsg
, "unsupported file format", (char*)0);
280 sqlite3BtreeSetCacheSize(db
->aDb
[iDb
].pBt
, db
->aDb
[iDb
].cache_size
);
282 /* Read the schema information out of the schema tables
284 assert( db
->init
.busy
);
285 if( rc
==SQLITE_EMPTY
){
286 /* For an empty database, there is nothing to read */
290 zSql
= sqlite3MPrintf(
291 "SELECT name, rootpage, sql, '%s' FROM '%q'.%s",
292 zDbNum
, db
->aDb
[iDb
].zName
, zMasterName
);
293 sqlite3SafetyOff(db
);
294 rc
= sqlite3_exec(db
, zSql
, sqlite3InitCallback
, &initData
, 0);
297 sqlite3BtreeCloseCursor(curMain
);
299 if( sqlite3_malloc_failed
){
300 sqlite3SetString(pzErrMsg
, "out of memory", (char*)0);
302 sqlite3ResetInternalSchema(db
, 0);
305 DbSetProperty(db
, iDb
, DB_SchemaLoaded
);
307 sqlite3ResetInternalSchema(db
, iDb
);
313 ** Initialize all database files - the main database file, the file
314 ** used to store temporary tables, and any additional database files
315 ** created using ATTACH statements. Return a success code. If an
316 ** error occurs, write an error message into *pzErrMsg.
318 ** After the database is initialized, the SQLITE_Initialized
319 ** bit is set in the flags field of the sqlite structure.
321 int sqlite3Init(sqlite3
*db
, STRPTR
*pzErrMsg
){
324 if( db
->init
.busy
) return SQLITE_OK
;
325 assert( (db
->flags
& SQLITE_Initialized
)==0 );
328 for(i
=0; rc
==SQLITE_OK
&& i
<db
->nDb
; i
++){
329 if( DbHasProperty(db
, i
, DB_SchemaLoaded
) || i
==1 ) continue;
330 rc
= sqlite3InitOne(db
, i
, pzErrMsg
);
332 sqlite3ResetInternalSchema(db
, i
);
336 /* Once all the other databases have been initialised, load the schema
337 ** for the TEMP database. This is loaded last, as the TEMP database
338 ** schema may contain references to objects in other databases.
340 #ifndef SQLITE_OMIT_TEMPDB
341 if( rc
==SQLITE_OK
&& db
->nDb
>1 && !DbHasProperty(db
, 1, DB_SchemaLoaded
) ){
342 rc
= sqlite3InitOne(db
, 1, pzErrMsg
);
344 sqlite3ResetInternalSchema(db
, 1);
351 db
->flags
|= SQLITE_Initialized
;
352 sqlite3CommitInternalChanges(db
);
356 db
->flags
&= ~SQLITE_Initialized
;
362 ** This routine is a no-op if the database schema is already initialised.
363 ** Otherwise, the schema is loaded. An error code is returned.
365 int sqlite3ReadSchema(Parse
*pParse
){
367 sqlite3
*db
= pParse
->db
;
368 if( !db
->init
.busy
){
369 if( (db
->flags
& SQLITE_Initialized
)==0 ){
370 rc
= sqlite3Init(db
, &pParse
->zErrMsg
);
373 assert( rc
!=SQLITE_OK
|| (db
->flags
& SQLITE_Initialized
)||db
->init
.busy
);
383 ** Check schema cookies in all databases. If any cookie is out
384 ** of date, return 0. If all schema cookies are current, return 1.
386 static int schemaIsValid(sqlite3
*db
){
393 for(iDb
=0; allOk
&& iDb
<db
->nDb
; iDb
++){
395 pBt
= db
->aDb
[iDb
].pBt
;
396 if( pBt
==0 ) continue;
397 rc
= sqlite3BtreeCursor(pBt
, MASTER_ROOT
, 0, 0, 0, &curTemp
);
399 rc
= sqlite3BtreeGetMeta(pBt
, 1, (u32
*)&cookie
);
400 if( rc
==SQLITE_OK
&& cookie
!=db
->aDb
[iDb
].schema_cookie
){
403 sqlite3BtreeCloseCursor(curTemp
);
410 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
413 sqlite3
*db
, /* Database handle. */
414 const char *zSql
, /* UTF-8 encoded SQL statement. */
415 int nBytes
, /* Length of zSql in bytes. */
416 sqlite3_stmt
**ppStmt
, /* OUT: A pointer to the prepared statement */
417 const char** pzTail
/* OUT: End of parsed string */
420 STRPTR zErrMsg
= NULL
;
423 if( sqlite3_malloc_failed
){
429 if( sqlite3SafetyOn(db
) ){
430 return SQLITE_MISUSE
;
433 memset(&sParse
, 0, sizeof(sParse
));
435 sqlite3RunParser(&sParse
, zSql
, &zErrMsg
);
437 if( sqlite3_malloc_failed
){
439 sqlite3RollbackAll(db
);
440 sqlite3ResetInternalSchema(db
, 0);
441 db
->flags
&= ~SQLITE_InTrans
;
444 if( sParse
.rc
==SQLITE_DONE
) sParse
.rc
= SQLITE_OK
;
445 if( sParse
.rc
!=SQLITE_OK
&& sParse
.checkSchema
&& !schemaIsValid(db
) ){
446 sParse
.rc
= SQLITE_SCHEMA
;
448 if( sParse
.rc
==SQLITE_SCHEMA
){
449 sqlite3ResetInternalSchema(db
, 0);
451 if( pzTail
) *pzTail
= sParse
.zTail
;
454 #ifndef SQLITE_OMIT_EXPLAIN
455 if( rc
==SQLITE_OK
&& sParse
.pVdbe
&& sParse
.explain
){
456 sqlite3VdbeSetNumCols(sParse
.pVdbe
, 5);
457 sqlite3VdbeSetColName(sParse
.pVdbe
, 0, "addr", P3_STATIC
);
458 sqlite3VdbeSetColName(sParse
.pVdbe
, 1, "opcode", P3_STATIC
);
459 sqlite3VdbeSetColName(sParse
.pVdbe
, 2, "p1", P3_STATIC
);
460 sqlite3VdbeSetColName(sParse
.pVdbe
, 3, "p2", P3_STATIC
);
461 sqlite3VdbeSetColName(sParse
.pVdbe
, 4, "p3", P3_STATIC
);
466 if( sqlite3SafetyOff(db
) ){
470 *ppStmt
= (sqlite3_stmt
*)sParse
.pVdbe
;
471 }else if( sParse
.pVdbe
){
472 sqlite3_finalize((sqlite3_stmt
*)sParse
.pVdbe
);
476 sqlite3Error(db
, rc
, "%s", zErrMsg
);
479 sqlite3Error(db
, rc
, 0);
484 #ifndef SQLITE_OMIT_UTF16
486 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
488 int sqlite3_prepare16(
489 sqlite3
*db
, /* Database handle. */
490 const void *zSql
, /* UTF-8 encoded SQL statement. */
491 int nBytes
, /* Length of zSql in bytes. */
492 sqlite3_stmt
**ppStmt
, /* OUT: A pointer to the prepared statement */
493 const void **pzTail
/* OUT: End of parsed string */
495 /* This function currently works by first transforming the UTF-16
496 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
497 ** tricky bit is figuring out the pointer to return in *pzTail.
499 char const *zSql8
= 0;
500 char const *zTail8
= 0;
504 if( sqlite3SafetyCheck(db
) ){
505 return SQLITE_MISUSE
;
507 pTmp
= sqlite3GetTransientValue(db
);
508 sqlite3ValueSetStr(pTmp
, -1, zSql
, SQLITE_UTF16NATIVE
, SQLITE_STATIC
);
509 zSql8
= sqlite3ValueText(pTmp
, SQLITE_UTF8
);
511 sqlite3Error(db
, SQLITE_NOMEM
, 0);
514 rc
= sqlite3_prepare(db
, zSql8
, -1, ppStmt
, &zTail8
);
516 if( zTail8
&& pzTail
){
517 /* If sqlite3_prepare returns a tail pointer, we calculate the
518 ** equivalent pointer into the UTF-16 string by counting the unicode
519 ** characters between zSql8 and zTail8, and then returning a pointer
520 ** the same number of characters into the UTF-16 string.
522 int chars_parsed
= sqlite3utf8CharLen(zSql8
, zTail8
-zSql8
);
523 *pzTail
= (u8
*)zSql
+ sqlite3utf16ByteLen(zSql
, chars_parsed
);
528 #endif /* SQLITE_OMIT_UTF16 */