blob: 9a1d4f3382d3cd9b97a3c0303da4a8caae7228bb [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**
danielk19772e588c72005-12-09 14:25:08 +000016** $Id: prepare.c,v 1.6 2005/12/09 14:25:08 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
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;
57 }
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.
65 */
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 ){
danielk1977261919c2005-12-06 12:52:59 +000074 if( rc==SQLITE_NOMEM ){
75 sqlite3Tsd()->mallocFailed = 1;
76 }else{
77 corruptSchema(pData, zErr);
78 }
danielk1977fa256a32005-05-25 04:11:56 +000079 sqlite3_free(zErr);
80 return rc;
81 }
82 }else{
83 /* If the SQL column is blank it means this is an index that
84 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
85 ** constraint for a CREATE TABLE. The index should have already
86 ** been created when we processed the CREATE TABLE. All we have
87 ** to do here is record the root page number for that index.
88 */
89 Index *pIndex;
90 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
91 if( pIndex==0 || pIndex->tnum!=0 ){
92 /* This can occur if there exists an index on a TEMP table which
93 ** has the same name as another index on a permanent index. Since
94 ** the permanent table is hidden by the TEMP table, we can also
95 ** safely ignore the index on the permanent table.
96 */
97 /* Do Nothing */;
98 }else{
99 pIndex->tnum = atoi(argv[1]);
100 }
101 }
102 return 0;
103}
104
105/*
106** Attempt to read the database schema and initialize internal
107** data structures for a single database file. The index of the
108** database file is given by iDb. iDb==0 is used for the main
109** database. iDb==1 should never be used. iDb>=2 is used for
110** auxiliary databases. Return one of the SQLITE_ error codes to
111** indicate success or failure.
112*/
113static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
114 int rc;
115 BtCursor *curMain;
116 int size;
117 Table *pTab;
118 char const *azArg[5];
119 char zDbNum[30];
120 int meta[10];
121 InitData initData;
122 char const *zMasterSchema;
123 char const *zMasterName = SCHEMA_TABLE(iDb);
124
125 /*
126 ** The master database table has a structure like this
127 */
128 static const char master_schema[] =
129 "CREATE TABLE sqlite_master(\n"
130 " type text,\n"
131 " name text,\n"
132 " tbl_name text,\n"
133 " rootpage integer,\n"
134 " sql text\n"
135 ")"
136 ;
137#ifndef SQLITE_OMIT_TEMPDB
138 static const char temp_master_schema[] =
139 "CREATE TEMP TABLE sqlite_temp_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#else
148 #define temp_master_schema 0
149#endif
150
151 assert( iDb>=0 && iDb<db->nDb );
152
153 /* zMasterSchema and zInitScript are set to point at the master schema
154 ** and initialisation script appropriate for the database being
155 ** initialised. zMasterName is the name of the master table.
156 */
157 if( !OMIT_TEMPDB && iDb==1 ){
158 zMasterSchema = temp_master_schema;
159 }else{
160 zMasterSchema = master_schema;
161 }
162 zMasterName = SCHEMA_TABLE(iDb);
163
164 /* Construct the schema tables. */
165 sqlite3SafetyOff(db);
166 azArg[0] = zMasterName;
167 azArg[1] = "1";
168 azArg[2] = zMasterSchema;
169 sprintf(zDbNum, "%d", iDb);
170 azArg[3] = zDbNum;
171 azArg[4] = 0;
172 initData.db = db;
173 initData.pzErrMsg = pzErrMsg;
174 rc = sqlite3InitCallback(&initData, 4, (char **)azArg, 0);
175 if( rc!=SQLITE_OK ){
176 sqlite3SafetyOn(db);
177 return rc;
178 }
179 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
180 if( pTab ){
181 pTab->readOnly = 1;
182 }
183 sqlite3SafetyOn(db);
184
185 /* Create a cursor to hold the database open
186 */
187 if( db->aDb[iDb].pBt==0 ){
188 if( !OMIT_TEMPDB && iDb==1 ) DbSetProperty(db, 1, DB_SchemaLoaded);
189 return SQLITE_OK;
190 }
191 rc = sqlite3BtreeCursor(db->aDb[iDb].pBt, MASTER_ROOT, 0, 0, 0, &curMain);
192 if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
193 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
194 return rc;
195 }
196
197 /* Get the database meta information.
198 **
199 ** Meta values are as follows:
200 ** meta[0] Schema cookie. Changes with each schema change.
201 ** meta[1] File format of schema layer.
202 ** meta[2] Size of the page cache.
203 ** meta[3] Use freelist if 0. Autovacuum if greater than zero.
204 ** meta[4] Db text encoding. 1:UTF-8 3:UTF-16 LE 4:UTF-16 BE
205 ** meta[5] The user cookie. Used by the application.
206 ** meta[6]
207 ** meta[7]
208 ** meta[8]
209 ** meta[9]
210 **
211 ** Note: The hash defined SQLITE_UTF* symbols in sqliteInt.h correspond to
212 ** the possible values of meta[4].
213 */
214 if( rc==SQLITE_OK ){
215 int i;
216 for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
217 rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, i+1, (u32 *)&meta[i]);
218 }
219 if( rc ){
220 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
221 sqlite3BtreeCloseCursor(curMain);
222 return rc;
223 }
224 }else{
225 memset(meta, 0, sizeof(meta));
226 }
227 db->aDb[iDb].schema_cookie = meta[0];
228
229 /* If opening a non-empty database, check the text encoding. For the
230 ** main database, set sqlite3.enc to the encoding of the main database.
231 ** For an attached db, it is an error if the encoding is not the same
232 ** as sqlite3.enc.
233 */
234 if( meta[4] ){ /* text encoding */
235 if( iDb==0 ){
236 /* If opening the main database, set db->enc. */
237 db->enc = (u8)meta[4];
238 db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0);
239 }else{
240 /* If opening an attached database, the encoding much match db->enc */
241 if( meta[4]!=db->enc ){
242 sqlite3BtreeCloseCursor(curMain);
243 sqlite3SetString(pzErrMsg, "attached databases must use the same"
244 " text encoding as main database", (char*)0);
245 return SQLITE_ERROR;
246 }
247 }
248 }
249
250 size = meta[2];
251 if( size==0 ){ size = MAX_PAGES; }
252 db->aDb[iDb].cache_size = size;
253
254 if( iDb==0 ){
255 db->file_format = meta[1];
256 if( db->file_format==0 ){
257 /* This happens if the database was initially empty */
258 db->file_format = 1;
259 }
260
261 if( db->file_format==2 || db->file_format==3 ){
262 /* File format 2 is treated exactly as file format 1. New
263 ** databases are created with file format 1.
264 */
265 db->file_format = 1;
266 }
267 }
268
269 /*
270 ** file_format==1 Version 3.0.0.
271 ** file_format==2 Version 3.1.3.
272 ** file_format==3 Version 3.1.4.
273 **
274 ** Version 3.0 can only use files with file_format==1. Version 3.1.3
275 ** can read and write files with file_format==1 or file_format==2.
276 ** Version 3.1.4 can read and write file formats 1, 2 and 3.
277 */
278 if( meta[1]>3 ){
279 sqlite3BtreeCloseCursor(curMain);
280 sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
281 return SQLITE_ERROR;
282 }
283
284 sqlite3BtreeSetCacheSize(db->aDb[iDb].pBt, db->aDb[iDb].cache_size);
285
286 /* Read the schema information out of the schema tables
287 */
288 assert( db->init.busy );
289 if( rc==SQLITE_EMPTY ){
290 /* For an empty database, there is nothing to read */
291 rc = SQLITE_OK;
292 }else{
293 char *zSql;
294 zSql = sqlite3MPrintf(
295 "SELECT name, rootpage, sql, '%s' FROM '%q'.%s",
296 zDbNum, db->aDb[iDb].zName, zMasterName);
297 sqlite3SafetyOff(db);
298 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
299 sqlite3SafetyOn(db);
300 sqliteFree(zSql);
drh497e4462005-07-23 03:18:40 +0000301#ifndef SQLITE_OMIT_ANALYZE
302 if( rc==SQLITE_OK ){
303 sqlite3AnalysisLoad(db, iDb);
304 }
305#endif
danielk1977fa256a32005-05-25 04:11:56 +0000306 sqlite3BtreeCloseCursor(curMain);
307 }
danielk1977261919c2005-12-06 12:52:59 +0000308 if( sqlite3Tsd()->mallocFailed ){
danielk1977fa256a32005-05-25 04:11:56 +0000309 sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
310 rc = SQLITE_NOMEM;
311 sqlite3ResetInternalSchema(db, 0);
312 }
313 if( rc==SQLITE_OK ){
314 DbSetProperty(db, iDb, DB_SchemaLoaded);
315 }else{
316 sqlite3ResetInternalSchema(db, iDb);
317 }
318 return rc;
319}
320
321/*
322** Initialize all database files - the main database file, the file
323** used to store temporary tables, and any additional database files
324** created using ATTACH statements. Return a success code. If an
325** error occurs, write an error message into *pzErrMsg.
326**
327** After the database is initialized, the SQLITE_Initialized
328** bit is set in the flags field of the sqlite structure.
329*/
330int sqlite3Init(sqlite3 *db, char **pzErrMsg){
331 int i, rc;
332
333 if( db->init.busy ) return SQLITE_OK;
334 assert( (db->flags & SQLITE_Initialized)==0 );
335 rc = SQLITE_OK;
336 db->init.busy = 1;
337 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
338 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
339 rc = sqlite3InitOne(db, i, pzErrMsg);
340 if( rc ){
341 sqlite3ResetInternalSchema(db, i);
342 }
343 }
344
345 /* Once all the other databases have been initialised, load the schema
346 ** for the TEMP database. This is loaded last, as the TEMP database
347 ** schema may contain references to objects in other databases.
348 */
349#ifndef SQLITE_OMIT_TEMPDB
350 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
351 rc = sqlite3InitOne(db, 1, pzErrMsg);
352 if( rc ){
353 sqlite3ResetInternalSchema(db, 1);
354 }
355 }
356#endif
357
358 db->init.busy = 0;
359 if( rc==SQLITE_OK ){
360 db->flags |= SQLITE_Initialized;
361 sqlite3CommitInternalChanges(db);
362 }
363
364 if( rc!=SQLITE_OK ){
365 db->flags &= ~SQLITE_Initialized;
366 }
367 return rc;
368}
369
370/*
371** This routine is a no-op if the database schema is already initialised.
372** Otherwise, the schema is loaded. An error code is returned.
373*/
374int sqlite3ReadSchema(Parse *pParse){
375 int rc = SQLITE_OK;
376 sqlite3 *db = pParse->db;
377 if( !db->init.busy ){
378 if( (db->flags & SQLITE_Initialized)==0 ){
379 rc = sqlite3Init(db, &pParse->zErrMsg);
380 }
381 }
drh8b3d9902005-08-19 00:14:42 +0000382 assert( rc!=SQLITE_OK || (db->flags & SQLITE_Initialized) || db->init.busy );
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);
409 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].schema_cookie ){
410 allOk = 0;
411 }
412 sqlite3BtreeCloseCursor(curTemp);
413 }
414 }
415 return allOk;
416}
417
418/*
419** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
420*/
421int sqlite3_prepare(
422 sqlite3 *db, /* Database handle. */
423 const char *zSql, /* UTF-8 encoded SQL statement. */
424 int nBytes, /* Length of zSql in bytes. */
425 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
426 const char** pzTail /* OUT: End of parsed string */
427){
428 Parse sParse;
429 char *zErrMsg = 0;
430 int rc = SQLITE_OK;
431
danielk1977261919c2005-12-06 12:52:59 +0000432 assert(!sqlite3Tsd()->mallocFailed);
danielk1977fa256a32005-05-25 04:11:56 +0000433
434 assert( ppStmt );
435 *ppStmt = 0;
436 if( sqlite3SafetyOn(db) ){
437 return SQLITE_MISUSE;
438 }
439
440 memset(&sParse, 0, sizeof(sParse));
441 sParse.db = db;
442 sqlite3RunParser(&sParse, zSql, &zErrMsg);
443
danielk1977261919c2005-12-06 12:52:59 +0000444 if( sqlite3Tsd()->mallocFailed ){
445 sParse.rc = SQLITE_NOMEM;
446#if 0
447 sqlite3RollbackInternalChanges(db);
danielk1977fa256a32005-05-25 04:11:56 +0000448 sqlite3RollbackAll(db);
danielk1977fa256a32005-05-25 04:11:56 +0000449 db->flags &= ~SQLITE_InTrans;
danielk1977261919c2005-12-06 12:52:59 +0000450 db->autoCommit = 1;
451#endif
danielk1977fa256a32005-05-25 04:11:56 +0000452 }
453 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
454 if( sParse.rc!=SQLITE_OK && sParse.checkSchema && !schemaIsValid(db) ){
455 sParse.rc = SQLITE_SCHEMA;
456 }
457 if( sParse.rc==SQLITE_SCHEMA ){
458 sqlite3ResetInternalSchema(db, 0);
459 }
460 if( pzTail ) *pzTail = sParse.zTail;
461 rc = sParse.rc;
462
463#ifndef SQLITE_OMIT_EXPLAIN
464 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
drhecc92422005-09-10 16:46:12 +0000465 if( sParse.explain==2 ){
466 sqlite3VdbeSetNumCols(sParse.pVdbe, 3);
467 sqlite3VdbeSetColName(sParse.pVdbe, 0, "order", P3_STATIC);
468 sqlite3VdbeSetColName(sParse.pVdbe, 1, "from", P3_STATIC);
469 sqlite3VdbeSetColName(sParse.pVdbe, 2, "detail", P3_STATIC);
470 }else{
471 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
472 sqlite3VdbeSetColName(sParse.pVdbe, 0, "addr", P3_STATIC);
473 sqlite3VdbeSetColName(sParse.pVdbe, 1, "opcode", P3_STATIC);
474 sqlite3VdbeSetColName(sParse.pVdbe, 2, "p1", P3_STATIC);
475 sqlite3VdbeSetColName(sParse.pVdbe, 3, "p2", P3_STATIC);
476 sqlite3VdbeSetColName(sParse.pVdbe, 4, "p3", P3_STATIC);
477 }
danielk1977fa256a32005-05-25 04:11:56 +0000478 }
479#endif
480
481prepare_out:
482 if( sqlite3SafetyOff(db) ){
483 rc = SQLITE_MISUSE;
484 }
485 if( rc==SQLITE_OK ){
486 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
487 }else if( sParse.pVdbe ){
488 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
489 }
490
491 if( zErrMsg ){
492 sqlite3Error(db, rc, "%s", zErrMsg);
493 sqliteFree(zErrMsg);
494 }else{
495 sqlite3Error(db, rc, 0);
496 }
danielk1977261919c2005-12-06 12:52:59 +0000497
498 /* We must check for malloc failure last of all, in case malloc() failed
499 ** inside of the sqlite3Error() call above or something.
500 */
501 if( sqlite3Tsd()->mallocFailed ){
502 rc = SQLITE_NOMEM;
503 sqlite3Error(db, rc, 0);
504 }
505
danielk19772e588c72005-12-09 14:25:08 +0000506 sqlite3MallocClearFailed();
danielk1977fa256a32005-05-25 04:11:56 +0000507 return rc;
508}
509
510#ifndef SQLITE_OMIT_UTF16
511/*
512** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
513*/
514int sqlite3_prepare16(
515 sqlite3 *db, /* Database handle. */
516 const void *zSql, /* UTF-8 encoded SQL statement. */
517 int nBytes, /* Length of zSql in bytes. */
518 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
519 const void **pzTail /* OUT: End of parsed string */
520){
521 /* This function currently works by first transforming the UTF-16
522 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
523 ** tricky bit is figuring out the pointer to return in *pzTail.
524 */
525 char const *zSql8 = 0;
526 char const *zTail8 = 0;
527 int rc;
528 sqlite3_value *pTmp;
529
530 if( sqlite3SafetyCheck(db) ){
531 return SQLITE_MISUSE;
532 }
533 pTmp = sqlite3GetTransientValue(db);
534 sqlite3ValueSetStr(pTmp, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
535 zSql8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
536 if( !zSql8 ){
537 sqlite3Error(db, SQLITE_NOMEM, 0);
538 return SQLITE_NOMEM;
539 }
540 rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);
541
542 if( zTail8 && pzTail ){
543 /* If sqlite3_prepare returns a tail pointer, we calculate the
544 ** equivalent pointer into the UTF-16 string by counting the unicode
545 ** characters between zSql8 and zTail8, and then returning a pointer
546 ** the same number of characters into the UTF-16 string.
547 */
548 int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8);
549 *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed);
550 }
551
552 return rc;
553}
554#endif /* SQLITE_OMIT_UTF16 */