blob: 50d03e835671700f8012ae7ea64eb9558d8ffa0a [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**
drh8b3d9902005-08-19 00:14:42 +000016** $Id: prepare.c,v 1.3 2005/08/19 00:14:42 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){
27 if( !sqlite3_malloc_failed ){
28 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 ){
74 corruptSchema(pData, zErr);
75 sqlite3_free(zErr);
76 return rc;
77 }
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.
84 */
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.
92 */
93 /* Do Nothing */;
94 }else{
95 pIndex->tnum = atoi(argv[1]);
96 }
97 }
98 return 0;
99}
100
101/*
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.
108*/
109static int sqlite3InitOne(sqlite3 *db, int iDb, char **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);
120
121 /*
122 ** The master database table has a structure like this
123 */
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"
131 ")"
132 ;
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"
141 ")"
142 ;
143#else
144 #define temp_master_schema 0
145#endif
146
147 assert( iDb>=0 && iDb<db->nDb );
148
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.
152 */
153 if( !OMIT_TEMPDB && iDb==1 ){
154 zMasterSchema = temp_master_schema;
155 }else{
156 zMasterSchema = master_schema;
157 }
158 zMasterName = SCHEMA_TABLE(iDb);
159
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;
174 }
175 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
176 if( pTab ){
177 pTab->readOnly = 1;
178 }
179 sqlite3SafetyOn(db);
180
181 /* Create a cursor to hold the database open
182 */
183 if( db->aDb[iDb].pBt==0 ){
184 if( !OMIT_TEMPDB && iDb==1 ) DbSetProperty(db, 1, DB_SchemaLoaded);
185 return SQLITE_OK;
186 }
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;
191 }
192
193 /* Get the database meta information.
194 **
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]
206 **
207 ** Note: The hash defined SQLITE_UTF* symbols in sqliteInt.h correspond to
208 ** the possible values of meta[4].
209 */
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]);
214 }
215 if( rc ){
216 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
217 sqlite3BtreeCloseCursor(curMain);
218 return rc;
219 }
220 }else{
221 memset(meta, 0, sizeof(meta));
222 }
223 db->aDb[iDb].schema_cookie = meta[0];
224
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.
229 */
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;
242 }
243 }
244 }
245
246 size = meta[2];
247 if( size==0 ){ size = MAX_PAGES; }
248 db->aDb[iDb].cache_size = size;
249
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;
255 }
256
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.
260 */
261 db->file_format = 1;
262 }
263 }
264
265 /*
266 ** file_format==1 Version 3.0.0.
267 ** file_format==2 Version 3.1.3.
268 ** file_format==3 Version 3.1.4.
269 **
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.
273 */
274 if( meta[1]>3 ){
275 sqlite3BtreeCloseCursor(curMain);
276 sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
277 return SQLITE_ERROR;
278 }
279
280 sqlite3BtreeSetCacheSize(db->aDb[iDb].pBt, db->aDb[iDb].cache_size);
281
282 /* Read the schema information out of the schema tables
283 */
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);
drh497e4462005-07-23 03:18:40 +0000297#ifndef SQLITE_OMIT_ANALYZE
298 if( rc==SQLITE_OK ){
299 sqlite3AnalysisLoad(db, iDb);
300 }
301#endif
danielk1977fa256a32005-05-25 04:11:56 +0000302 sqlite3BtreeCloseCursor(curMain);
303 }
304 if( sqlite3_malloc_failed ){
305 sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
306 rc = SQLITE_NOMEM;
307 sqlite3ResetInternalSchema(db, 0);
308 }
309 if( rc==SQLITE_OK ){
310 DbSetProperty(db, iDb, DB_SchemaLoaded);
311 }else{
312 sqlite3ResetInternalSchema(db, iDb);
313 }
314 return rc;
315}
316
317/*
318** Initialize all database files - the main database file, the file
319** used to store temporary tables, and any additional database files
320** created using ATTACH statements. Return a success code. If an
321** error occurs, write an error message into *pzErrMsg.
322**
323** After the database is initialized, the SQLITE_Initialized
324** bit is set in the flags field of the sqlite structure.
325*/
326int sqlite3Init(sqlite3 *db, char **pzErrMsg){
327 int i, rc;
328
329 if( db->init.busy ) return SQLITE_OK;
330 assert( (db->flags & SQLITE_Initialized)==0 );
331 rc = SQLITE_OK;
332 db->init.busy = 1;
333 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
334 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
335 rc = sqlite3InitOne(db, i, pzErrMsg);
336 if( rc ){
337 sqlite3ResetInternalSchema(db, i);
338 }
339 }
340
341 /* Once all the other databases have been initialised, load the schema
342 ** for the TEMP database. This is loaded last, as the TEMP database
343 ** schema may contain references to objects in other databases.
344 */
345#ifndef SQLITE_OMIT_TEMPDB
346 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
347 rc = sqlite3InitOne(db, 1, pzErrMsg);
348 if( rc ){
349 sqlite3ResetInternalSchema(db, 1);
350 }
351 }
352#endif
353
354 db->init.busy = 0;
355 if( rc==SQLITE_OK ){
356 db->flags |= SQLITE_Initialized;
357 sqlite3CommitInternalChanges(db);
358 }
359
360 if( rc!=SQLITE_OK ){
361 db->flags &= ~SQLITE_Initialized;
362 }
363 return rc;
364}
365
366/*
367** This routine is a no-op if the database schema is already initialised.
368** Otherwise, the schema is loaded. An error code is returned.
369*/
370int sqlite3ReadSchema(Parse *pParse){
371 int rc = SQLITE_OK;
372 sqlite3 *db = pParse->db;
373 if( !db->init.busy ){
374 if( (db->flags & SQLITE_Initialized)==0 ){
375 rc = sqlite3Init(db, &pParse->zErrMsg);
376 }
377 }
drh8b3d9902005-08-19 00:14:42 +0000378 assert( rc!=SQLITE_OK || (db->flags & SQLITE_Initialized) || db->init.busy );
danielk1977fa256a32005-05-25 04:11:56 +0000379 if( rc!=SQLITE_OK ){
380 pParse->rc = rc;
381 pParse->nErr++;
382 }
383 return rc;
384}
385
386
387/*
388** Check schema cookies in all databases. If any cookie is out
389** of date, return 0. If all schema cookies are current, return 1.
390*/
391static int schemaIsValid(sqlite3 *db){
392 int iDb;
393 int rc;
394 BtCursor *curTemp;
395 int cookie;
396 int allOk = 1;
397
398 for(iDb=0; allOk && iDb<db->nDb; iDb++){
399 Btree *pBt;
400 pBt = db->aDb[iDb].pBt;
401 if( pBt==0 ) continue;
402 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
403 if( rc==SQLITE_OK ){
404 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
405 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].schema_cookie ){
406 allOk = 0;
407 }
408 sqlite3BtreeCloseCursor(curTemp);
409 }
410 }
411 return allOk;
412}
413
414/*
415** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
416*/
417int sqlite3_prepare(
418 sqlite3 *db, /* Database handle. */
419 const char *zSql, /* UTF-8 encoded SQL statement. */
420 int nBytes, /* Length of zSql in bytes. */
421 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
422 const char** pzTail /* OUT: End of parsed string */
423){
424 Parse sParse;
425 char *zErrMsg = 0;
426 int rc = SQLITE_OK;
427
428 if( sqlite3_malloc_failed ){
429 return SQLITE_NOMEM;
430 }
431
432 assert( ppStmt );
433 *ppStmt = 0;
434 if( sqlite3SafetyOn(db) ){
435 return SQLITE_MISUSE;
436 }
437
438 memset(&sParse, 0, sizeof(sParse));
439 sParse.db = db;
440 sqlite3RunParser(&sParse, zSql, &zErrMsg);
441
442 if( sqlite3_malloc_failed ){
443 rc = SQLITE_NOMEM;
444 sqlite3RollbackAll(db);
445 sqlite3ResetInternalSchema(db, 0);
446 db->flags &= ~SQLITE_InTrans;
447 goto prepare_out;
448 }
449 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
450 if( sParse.rc!=SQLITE_OK && sParse.checkSchema && !schemaIsValid(db) ){
451 sParse.rc = SQLITE_SCHEMA;
452 }
453 if( sParse.rc==SQLITE_SCHEMA ){
454 sqlite3ResetInternalSchema(db, 0);
455 }
456 if( pzTail ) *pzTail = sParse.zTail;
457 rc = sParse.rc;
458
459#ifndef SQLITE_OMIT_EXPLAIN
460 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
461 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
462 sqlite3VdbeSetColName(sParse.pVdbe, 0, "addr", P3_STATIC);
463 sqlite3VdbeSetColName(sParse.pVdbe, 1, "opcode", P3_STATIC);
464 sqlite3VdbeSetColName(sParse.pVdbe, 2, "p1", P3_STATIC);
465 sqlite3VdbeSetColName(sParse.pVdbe, 3, "p2", P3_STATIC);
466 sqlite3VdbeSetColName(sParse.pVdbe, 4, "p3", P3_STATIC);
467 }
468#endif
469
470prepare_out:
471 if( sqlite3SafetyOff(db) ){
472 rc = SQLITE_MISUSE;
473 }
474 if( rc==SQLITE_OK ){
475 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
476 }else if( sParse.pVdbe ){
477 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
478 }
479
480 if( zErrMsg ){
481 sqlite3Error(db, rc, "%s", zErrMsg);
482 sqliteFree(zErrMsg);
483 }else{
484 sqlite3Error(db, rc, 0);
485 }
486 return rc;
487}
488
489#ifndef SQLITE_OMIT_UTF16
490/*
491** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
492*/
493int sqlite3_prepare16(
494 sqlite3 *db, /* Database handle. */
495 const void *zSql, /* UTF-8 encoded SQL statement. */
496 int nBytes, /* Length of zSql in bytes. */
497 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
498 const void **pzTail /* OUT: End of parsed string */
499){
500 /* This function currently works by first transforming the UTF-16
501 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
502 ** tricky bit is figuring out the pointer to return in *pzTail.
503 */
504 char const *zSql8 = 0;
505 char const *zTail8 = 0;
506 int rc;
507 sqlite3_value *pTmp;
508
509 if( sqlite3SafetyCheck(db) ){
510 return SQLITE_MISUSE;
511 }
512 pTmp = sqlite3GetTransientValue(db);
513 sqlite3ValueSetStr(pTmp, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
514 zSql8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
515 if( !zSql8 ){
516 sqlite3Error(db, SQLITE_NOMEM, 0);
517 return SQLITE_NOMEM;
518 }
519 rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);
520
521 if( zTail8 && pzTail ){
522 /* If sqlite3_prepare returns a tail pointer, we calculate the
523 ** equivalent pointer into the UTF-16 string by counting the unicode
524 ** characters between zSql8 and zTail8, and then returning a pointer
525 ** the same number of characters into the UTF-16 string.
526 */
527 int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8);
528 *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed);
529 }
530
531 return rc;
532}
533#endif /* SQLITE_OMIT_UTF16 */