Copy icons and docs.
[AROS-Contrib.git] / sqlite3 / prepare.c
blob61363ebab0484fc759cd0523abe099c2f3f63d24
1 /*
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.
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.
16 ** $Id$
18 #include "sqliteInt.h"
19 #include "os.h"
20 #include <ctype.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;
50 int iDb;
52 assert( argc==4 );
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);
56 return 1;
58 iDb = atoi(argv[3]);
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.
66 char *zErr;
67 int rc;
68 assert( db->init.busy );
69 db->init.iDb = iDb;
70 db->init.newTnum = atoi(argv[1]);
71 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
72 db->init.iDb = 0;
73 if( SQLITE_OK!=rc ){
74 corruptSchema(pData, zErr);
75 sqlite3_free(zErr);
76 return rc;
78 }else{
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.
85 Index *pIndex;
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.
93 /* Do Nothing */;
94 }else{
95 pIndex->tnum = atoi(argv[1]);
98 return 0;
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){
110 int rc;
111 BtCursor *curMain;
112 int size;
113 Table *pTab;
114 char const *azArg[5];
115 char zDbNum[30];
116 int meta[10];
117 InitData initData;
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"
126 " type text,\n"
127 " name text,\n"
128 " tbl_name text,\n"
129 " rootpage integer,\n"
130 " sql text\n"
133 #ifndef SQLITE_OMIT_TEMPDB
134 static const char temp_master_schema[] =
135 "CREATE TEMP TABLE sqlite_temp_master(\n"
136 " type text,\n"
137 " name text,\n"
138 " tbl_name text,\n"
139 " rootpage integer,\n"
140 " sql text\n"
143 #else
144 #define temp_master_schema 0
145 #endif
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;
155 }else{
156 zMasterSchema = master_schema;
158 zMasterName = SCHEMA_TABLE(iDb);
160 /* Construct the schema tables. */
161 sqlite3SafetyOff(db);
162 azArg[0] = zMasterName;
163 azArg[1] = "1";
164 azArg[2] = zMasterSchema;
165 sprintf(zDbNum, "%d", iDb);
166 azArg[3] = zDbNum;
167 azArg[4] = 0;
168 initData.db = db;
169 initData.pzErrMsg = pzErrMsg;
170 rc = sqlite3InitCallback(&initData, 4, (char **)azArg, 0);
171 if( rc!=SQLITE_OK ){
172 sqlite3SafetyOn(db);
173 return rc;
175 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
176 if( pTab ){
177 pTab->readOnly = 1;
179 sqlite3SafetyOn(db);
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);
185 return SQLITE_OK;
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);
190 return rc;
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.
202 ** meta[6]
203 ** meta[7]
204 ** meta[8]
205 ** meta[9]
207 ** Note: The hash defined SQLITE_UTF* symbols in sqliteInt.h correspond to
208 ** the possible values of meta[4].
210 if( rc==SQLITE_OK ){
211 int i;
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]);
215 if( rc ){
216 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
217 sqlite3BtreeCloseCursor(curMain);
218 return rc;
220 }else{
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
228 ** as sqlite3.enc.
230 if( meta[4] ){ /* text encoding */
231 if( iDb==0 ){
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);
235 }else{
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);
241 return SQLITE_ERROR;
246 size = meta[2];
247 if( size==0 ){ size = MAX_PAGES; }
248 db->aDb[iDb].cache_size = size;
250 if( iDb==0 ){
251 db->file_format = meta[1];
252 if( db->file_format==0 ){
253 /* This happens if the database was initially empty */
254 db->file_format = 1;
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.
261 db->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.
274 if( meta[1]>3 ){
275 sqlite3BtreeCloseCursor(curMain);
276 sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
277 return SQLITE_ERROR;
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 */
287 rc = SQLITE_OK;
288 }else{
289 char *zSql;
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);
295 sqlite3SafetyOn(db);
296 sqliteFree(zSql);
297 sqlite3BtreeCloseCursor(curMain);
299 if( sqlite3_malloc_failed ){
300 sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
301 rc = SQLITE_NOMEM;
302 sqlite3ResetInternalSchema(db, 0);
304 if( rc==SQLITE_OK ){
305 DbSetProperty(db, iDb, DB_SchemaLoaded);
306 }else{
307 sqlite3ResetInternalSchema(db, iDb);
309 return rc;
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){
322 int i, rc;
324 if( db->init.busy ) return SQLITE_OK;
325 assert( (db->flags & SQLITE_Initialized)==0 );
326 rc = SQLITE_OK;
327 db->init.busy = 1;
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);
331 if( rc ){
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);
343 if( rc ){
344 sqlite3ResetInternalSchema(db, 1);
347 #endif
349 db->init.busy = 0;
350 if( rc==SQLITE_OK ){
351 db->flags |= SQLITE_Initialized;
352 sqlite3CommitInternalChanges(db);
355 if( rc!=SQLITE_OK ){
356 db->flags &= ~SQLITE_Initialized;
358 return rc;
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){
366 int rc = SQLITE_OK;
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 );
374 if( rc!=SQLITE_OK ){
375 pParse->rc = rc;
376 pParse->nErr++;
378 return rc;
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){
387 int iDb;
388 int rc;
389 BtCursor *curTemp;
390 int cookie;
391 int allOk = 1;
393 for(iDb=0; allOk && iDb<db->nDb; iDb++){
394 Btree *pBt;
395 pBt = db->aDb[iDb].pBt;
396 if( pBt==0 ) continue;
397 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
398 if( rc==SQLITE_OK ){
399 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
400 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].schema_cookie ){
401 allOk = 0;
403 sqlite3BtreeCloseCursor(curTemp);
406 return allOk;
410 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
412 int sqlite3_prepare(
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 */
419 Parse sParse;
420 STRPTR zErrMsg = NULL;
421 int rc = SQLITE_OK;
423 if( sqlite3_malloc_failed ){
424 return SQLITE_NOMEM;
427 assert( ppStmt );
428 *ppStmt = 0;
429 if( sqlite3SafetyOn(db) ){
430 return SQLITE_MISUSE;
433 memset(&sParse, 0, sizeof(sParse));
434 sParse.db = db;
435 sqlite3RunParser(&sParse, zSql, &zErrMsg);
437 if( sqlite3_malloc_failed ){
438 rc = SQLITE_NOMEM;
439 sqlite3RollbackAll(db);
440 sqlite3ResetInternalSchema(db, 0);
441 db->flags &= ~SQLITE_InTrans;
442 goto prepare_out;
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;
452 rc = sParse.rc;
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);
463 #endif
465 prepare_out:
466 if( sqlite3SafetyOff(db) ){
467 rc = SQLITE_MISUSE;
469 if( rc==SQLITE_OK ){
470 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
471 }else if( sParse.pVdbe ){
472 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
475 if( zErrMsg ){
476 sqlite3Error(db, rc, "%s", zErrMsg);
477 sqliteFree(zErrMsg);
478 }else{
479 sqlite3Error(db, rc, 0);
481 return rc;
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;
501 int rc;
502 sqlite3_value *pTmp;
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);
510 if( !zSql8 ){
511 sqlite3Error(db, SQLITE_NOMEM, 0);
512 return SQLITE_NOMEM;
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);
526 return rc;
528 #endif /* SQLITE_OMIT_UTF16 */