annotate sqlite/prepare.c @ 1571:0b69bc0eb2d2 trunk

[svn] - if uri == NULL, then continue looking for URIs in the XSPF playlist
author nenolod
date Fri, 11 Aug 2006 01:17:05 -0700
parents b6b61becdf4e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1434
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
2 ** 2005 May 25
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
3 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
4 ** The author disclaims copyright to this source code. In place of
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
5 ** a legal notice, here is a blessing:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
6 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
7 ** May you do good and not evil.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
8 ** May you find forgiveness for yourself and forgive others.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
9 ** May you share freely, never taking more than you give.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
10 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
11 *************************************************************************
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
12 ** This file contains the implementation of the sqlite3_prepare()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
13 ** interface, and routines that contribute to loading the database schema
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
14 ** from disk.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
15 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
16 ** $Id: prepare.c,v 1.34 2006/05/23 23:22:29 drh Exp $
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
17 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
18 #include "sqliteInt.h"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
19 #include "os.h"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
20 #include <ctype.h>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
21
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
22 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
23 ** Fill the InitData structure with an error message that indicates
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
24 ** that the database is corrupt.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
25 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
26 static void corruptSchema(InitData *pData, const char *zExtra){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
27 if( !sqlite3MallocFailed() ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
28 sqlite3SetString(pData->pzErrMsg, "malformed database schema",
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
29 zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
30 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
31 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
32
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
33 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
34 ** This is the callback routine for the code that initializes the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
35 ** database. See sqlite3Init() below for additional information.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
36 ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
37 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
38 ** Each callback contains the following information:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
39 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
40 ** argv[0] = name of thing being created
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
41 ** argv[1] = root page number for table or index. NULL for trigger or view.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
42 ** argv[2] = SQL text for the CREATE statement.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
43 ** argv[3] = "1" for temporary files, "0" for main database, "2" or more
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
44 ** for auxiliary database files.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
45 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
46 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
47 int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
48 InitData *pData = (InitData*)pInit;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
49 sqlite3 *db = pData->db;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
50 int iDb;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
51
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
52 if( sqlite3MallocFailed() ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
53 return SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
54 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
55
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
56 assert( argc==4 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
57 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
58 if( argv[1]==0 || argv[3]==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
59 corruptSchema(pData, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
60 return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
61 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
62 iDb = atoi(argv[3]);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
63 assert( iDb>=0 && iDb<db->nDb );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
64 if( argv[2] && argv[2][0] ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
65 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
66 ** But because db->init.busy is set to 1, no VDBE code is generated
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
67 ** or executed. All the parser does is build the internal data
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
68 ** structures that describe the table, index, or view.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
69 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
70 char *zErr;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
71 int rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
72 assert( db->init.busy );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
73 db->init.iDb = iDb;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
74 db->init.newTnum = atoi(argv[1]);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
75 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
76 db->init.iDb = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
77 assert( rc!=SQLITE_OK || zErr==0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
78 if( SQLITE_OK!=rc ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
79 if( rc==SQLITE_NOMEM ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
80 sqlite3FailedMalloc();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
81 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
82 corruptSchema(pData, zErr);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
83 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
84 sqlite3_free(zErr);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
85 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
86 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
87 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
88 /* If the SQL column is blank it means this is an index that
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
89 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
90 ** constraint for a CREATE TABLE. The index should have already
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
91 ** been created when we processed the CREATE TABLE. All we have
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
92 ** to do here is record the root page number for that index.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
93 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
94 Index *pIndex;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
95 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
96 if( pIndex==0 || pIndex->tnum!=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
97 /* This can occur if there exists an index on a TEMP table which
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
98 ** has the same name as another index on a permanent index. Since
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
99 ** the permanent table is hidden by the TEMP table, we can also
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
100 ** safely ignore the index on the permanent table.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
101 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
102 /* Do Nothing */;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
103 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
104 pIndex->tnum = atoi(argv[1]);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
105 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
106 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
107 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
108 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
109
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
110 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
111 ** Attempt to read the database schema and initialize internal
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
112 ** data structures for a single database file. The index of the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
113 ** database file is given by iDb. iDb==0 is used for the main
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
114 ** database. iDb==1 should never be used. iDb>=2 is used for
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
115 ** auxiliary databases. Return one of the SQLITE_ error codes to
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
116 ** indicate success or failure.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
117 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
118 static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
119 int rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
120 BtCursor *curMain;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
121 int size;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
122 Table *pTab;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
123 Db *pDb;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
124 char const *azArg[5];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
125 char zDbNum[30];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
126 int meta[10];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
127 InitData initData;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
128 char const *zMasterSchema;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
129 char const *zMasterName = SCHEMA_TABLE(iDb);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
130
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
131 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
132 ** The master database table has a structure like this
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
133 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
134 static const char master_schema[] =
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
135 "CREATE TABLE sqlite_master(\n"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
136 " type text,\n"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
137 " name text,\n"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
138 " tbl_name text,\n"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
139 " rootpage integer,\n"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
140 " sql text\n"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
141 ")"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
142 ;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
143 #ifndef SQLITE_OMIT_TEMPDB
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
144 static const char temp_master_schema[] =
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
145 "CREATE TEMP TABLE sqlite_temp_master(\n"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
146 " type text,\n"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
147 " name text,\n"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
148 " tbl_name text,\n"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
149 " rootpage integer,\n"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
150 " sql text\n"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
151 ")"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
152 ;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
153 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
154 #define temp_master_schema 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
155 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
156
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
157 assert( iDb>=0 && iDb<db->nDb );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
158 assert( db->aDb[iDb].pSchema );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
159
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
160 /* zMasterSchema and zInitScript are set to point at the master schema
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
161 ** and initialisation script appropriate for the database being
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
162 ** initialised. zMasterName is the name of the master table.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
163 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
164 if( !OMIT_TEMPDB && iDb==1 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
165 zMasterSchema = temp_master_schema;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
166 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
167 zMasterSchema = master_schema;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
168 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
169 zMasterName = SCHEMA_TABLE(iDb);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
170
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
171 /* Construct the schema tables. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
172 sqlite3SafetyOff(db);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
173 azArg[0] = zMasterName;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
174 azArg[1] = "1";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
175 azArg[2] = zMasterSchema;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
176 sprintf(zDbNum, "%d", iDb);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
177 azArg[3] = zDbNum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
178 azArg[4] = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
179 initData.db = db;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
180 initData.pzErrMsg = pzErrMsg;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
181 rc = sqlite3InitCallback(&initData, 4, (char **)azArg, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
182 if( rc!=SQLITE_OK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
183 sqlite3SafetyOn(db);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
184 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
185 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
186 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
187 if( pTab ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
188 pTab->readOnly = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
189 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
190 sqlite3SafetyOn(db);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
191
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
192 /* Create a cursor to hold the database open
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
193 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
194 pDb = &db->aDb[iDb];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
195 if( pDb->pBt==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
196 if( !OMIT_TEMPDB && iDb==1 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
197 DbSetProperty(db, 1, DB_SchemaLoaded);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
198 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
199 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
200 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
201 rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, 0, &curMain);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
202 if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
203 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
204 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
205 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
206
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
207 /* Get the database meta information.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
208 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
209 ** Meta values are as follows:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
210 ** meta[0] Schema cookie. Changes with each schema change.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
211 ** meta[1] File format of schema layer.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
212 ** meta[2] Size of the page cache.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
213 ** meta[3] Use freelist if 0. Autovacuum if greater than zero.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
214 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
215 ** meta[5] The user cookie. Used by the application.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
216 ** meta[6]
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
217 ** meta[7]
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
218 ** meta[8]
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
219 ** meta[9]
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
220 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
221 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
222 ** the possible values of meta[4].
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
223 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
224 if( rc==SQLITE_OK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
225 int i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
226 for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
227 rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
228 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
229 if( rc ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
230 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
231 sqlite3BtreeCloseCursor(curMain);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
232 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
233 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
234 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
235 memset(meta, 0, sizeof(meta));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
236 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
237 pDb->pSchema->schema_cookie = meta[0];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
238
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
239 /* If opening a non-empty database, check the text encoding. For the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
240 ** main database, set sqlite3.enc to the encoding of the main database.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
241 ** For an attached db, it is an error if the encoding is not the same
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
242 ** as sqlite3.enc.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
243 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
244 if( meta[4] ){ /* text encoding */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
245 if( iDb==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
246 /* If opening the main database, set ENC(db). */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
247 ENC(db) = (u8)meta[4];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
248 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
249 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
250 /* If opening an attached database, the encoding much match ENC(db) */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
251 if( meta[4]!=ENC(db) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
252 sqlite3BtreeCloseCursor(curMain);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
253 sqlite3SetString(pzErrMsg, "attached databases must use the same"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
254 " text encoding as main database", (char*)0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
255 return SQLITE_ERROR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
256 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
257 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
258 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
259 DbSetProperty(db, iDb, DB_Empty);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
260 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
261 pDb->pSchema->enc = ENC(db);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
262
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
263 size = meta[2];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
264 if( size==0 ){ size = MAX_PAGES; }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
265 pDb->pSchema->cache_size = size;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
266 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
267
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
268 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
269 ** file_format==1 Version 3.0.0.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
270 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
271 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
272 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
273 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
274 pDb->pSchema->file_format = meta[1];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
275 if( pDb->pSchema->file_format==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
276 pDb->pSchema->file_format = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
277 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
278 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
279 sqlite3BtreeCloseCursor(curMain);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
280 sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
281 return SQLITE_ERROR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
282 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
283
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
284
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
285 /* Read the schema information out of the schema tables
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
286 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
287 assert( db->init.busy );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
288 if( rc==SQLITE_EMPTY ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
289 /* For an empty database, there is nothing to read */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
290 rc = SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
291 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
292 char *zSql;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
293 zSql = sqlite3MPrintf(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
294 "SELECT name, rootpage, sql, '%s' FROM '%q'.%s",
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
295 zDbNum, db->aDb[iDb].zName, zMasterName);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
296 sqlite3SafetyOff(db);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
297 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
298 sqlite3SafetyOn(db);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
299 sqliteFree(zSql);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
300 #ifndef SQLITE_OMIT_ANALYZE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
301 if( rc==SQLITE_OK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
302 sqlite3AnalysisLoad(db, iDb);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
303 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
304 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
305 sqlite3BtreeCloseCursor(curMain);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
306 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
307 if( sqlite3MallocFailed() ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
308 /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
309 rc = SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
310 sqlite3ResetInternalSchema(db, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
311 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
312 if( rc==SQLITE_OK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
313 DbSetProperty(db, iDb, DB_SchemaLoaded);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
314 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
315 sqlite3ResetInternalSchema(db, iDb);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
316 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
317 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
318 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
319
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
320 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
321 ** Initialize all database files - the main database file, the file
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
322 ** used to store temporary tables, and any additional database files
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
323 ** created using ATTACH statements. Return a success code. If an
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
324 ** error occurs, write an error message into *pzErrMsg.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
325 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
326 ** After a database is initialized, the DB_SchemaLoaded bit is set
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
327 ** bit is set in the flags field of the Db structure. If the database
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
328 ** file was of zero-length, then the DB_Empty flag is also set.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
329 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
330 int sqlite3Init(sqlite3 *db, char **pzErrMsg){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
331 int i, rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
332 int called_initone = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
333
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
334 if( db->init.busy ) return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
335 rc = SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
336 db->init.busy = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
337 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
338 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
339 rc = sqlite3InitOne(db, i, pzErrMsg);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
340 if( rc ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
341 sqlite3ResetInternalSchema(db, i);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
342 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
343 called_initone = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
344 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
345
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
346 /* Once all the other databases have been initialised, load the schema
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
347 ** for the TEMP database. This is loaded last, as the TEMP database
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
348 ** schema may contain references to objects in other databases.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
349 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
350 #ifndef SQLITE_OMIT_TEMPDB
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
351 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
352 rc = sqlite3InitOne(db, 1, pzErrMsg);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
353 if( rc ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
354 sqlite3ResetInternalSchema(db, 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
355 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
356 called_initone = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
357 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
358 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
359
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
360 db->init.busy = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
361 if( rc==SQLITE_OK && called_initone ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
362 sqlite3CommitInternalChanges(db);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
363 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
364
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
365 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
366 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
367
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
368 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
369 ** This routine is a no-op if the database schema is already initialised.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
370 ** Otherwise, the schema is loaded. An error code is returned.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
371 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
372 int sqlite3ReadSchema(Parse *pParse){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
373 int rc = SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
374 sqlite3 *db = pParse->db;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
375 if( !db->init.busy ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
376 rc = sqlite3Init(db, &pParse->zErrMsg);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
377 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
378 if( rc!=SQLITE_OK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
379 pParse->rc = rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
380 pParse->nErr++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
381 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
382 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
383 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
384
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
385
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
386 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
387 ** Check schema cookies in all databases. If any cookie is out
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
388 ** of date, return 0. If all schema cookies are current, return 1.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
389 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
390 static int schemaIsValid(sqlite3 *db){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
391 int iDb;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
392 int rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
393 BtCursor *curTemp;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
394 int cookie;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
395 int allOk = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
396
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
397 for(iDb=0; allOk && iDb<db->nDb; iDb++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
398 Btree *pBt;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
399 pBt = db->aDb[iDb].pBt;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
400 if( pBt==0 ) continue;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
401 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
402 if( rc==SQLITE_OK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
403 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
404 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].pSchema->schema_cookie ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
405 allOk = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
406 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
407 sqlite3BtreeCloseCursor(curTemp);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
408 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
409 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
410 return allOk;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
411 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
412
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
413 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
414 ** Convert a schema pointer into the iDb index that indicates
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
415 ** which database file in db->aDb[] the schema refers to.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
416 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
417 ** If the same database is attached more than once, the first
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
418 ** attached database is returned.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
419 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
420 int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
421 int i = -1000000;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
422
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
423 /* If pSchema is NULL, then return -1000000. This happens when code in
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
424 ** expr.c is trying to resolve a reference to a transient table (i.e. one
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
425 ** created by a sub-select). In this case the return value of this
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
426 ** function should never be used.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
427 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
428 ** We return -1000000 instead of the more usual -1 simply because using
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
429 ** -1000000 as incorrectly using -1000000 index into db->aDb[] is much
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
430 ** more likely to cause a segfault than -1 (of course there are assert()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
431 ** statements too, but it never hurts to play the odds).
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
432 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
433 if( pSchema ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
434 for(i=0; i<db->nDb; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
435 if( db->aDb[i].pSchema==pSchema ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
436 break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
437 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
438 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
439 assert( i>=0 &&i>=0 && i<db->nDb );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
440 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
441 return i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
442 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
443
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
444 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
445 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
446 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
447 int sqlite3_prepare(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
448 sqlite3 *db, /* Database handle. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
449 const char *zSql, /* UTF-8 encoded SQL statement. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
450 int nBytes, /* Length of zSql in bytes. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
451 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
452 const char** pzTail /* OUT: End of parsed string */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
453 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
454 Parse sParse;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
455 char *zErrMsg = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
456 int rc = SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
457 int i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
458
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
459 /* Assert that malloc() has not failed */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
460 assert( !sqlite3MallocFailed() );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
461
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
462 assert( ppStmt );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
463 *ppStmt = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
464 if( sqlite3SafetyOn(db) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
465 return SQLITE_MISUSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
466 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
467
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
468 /* If any attached database schemas are locked, do not proceed with
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
469 ** compilation. Instead return SQLITE_LOCKED immediately.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
470 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
471 for(i=0; i<db->nDb; i++) {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
472 Btree *pBt = db->aDb[i].pBt;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
473 if( pBt && sqlite3BtreeSchemaLocked(pBt) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
474 const char *zDb = db->aDb[i].zName;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
475 sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
476 sqlite3SafetyOff(db);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
477 return SQLITE_LOCKED;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
478 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
479 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
480
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
481 memset(&sParse, 0, sizeof(sParse));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
482 sParse.db = db;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
483 if( nBytes>=0 && zSql[nBytes]!=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
484 char *zSqlCopy = sqlite3StrNDup(zSql, nBytes);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
485 sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
486 sParse.zTail += zSql - zSqlCopy;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
487 sqliteFree(zSqlCopy);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
488 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
489 sqlite3RunParser(&sParse, zSql, &zErrMsg);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
490 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
491
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
492 if( sqlite3MallocFailed() ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
493 sParse.rc = SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
494 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
495 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
496 if( sParse.checkSchema && !schemaIsValid(db) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
497 sParse.rc = SQLITE_SCHEMA;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
498 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
499 if( sParse.rc==SQLITE_SCHEMA ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
500 sqlite3ResetInternalSchema(db, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
501 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
502 if( pzTail ) *pzTail = sParse.zTail;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
503 rc = sParse.rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
504
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
505 #ifndef SQLITE_OMIT_EXPLAIN
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
506 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
507 if( sParse.explain==2 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
508 sqlite3VdbeSetNumCols(sParse.pVdbe, 3);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
509 sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "order", P3_STATIC);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
510 sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "from", P3_STATIC);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
511 sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "detail", P3_STATIC);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
512 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
513 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
514 sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "addr", P3_STATIC);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
515 sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "opcode", P3_STATIC);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
516 sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "p1", P3_STATIC);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
517 sqlite3VdbeSetColName(sParse.pVdbe, 3, COLNAME_NAME, "p2", P3_STATIC);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
518 sqlite3VdbeSetColName(sParse.pVdbe, 4, COLNAME_NAME, "p3", P3_STATIC);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
519 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
520 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
521 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
522
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
523 if( sqlite3SafetyOff(db) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
524 rc = SQLITE_MISUSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
525 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
526 if( rc==SQLITE_OK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
527 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
528 }else if( sParse.pVdbe ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
529 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
530 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
531
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
532 if( zErrMsg ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
533 sqlite3Error(db, rc, "%s", zErrMsg);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
534 sqliteFree(zErrMsg);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
535 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
536 sqlite3Error(db, rc, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
537 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
538
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
539 rc = sqlite3ApiExit(db, rc);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
540 sqlite3ReleaseThreadData();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
541 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
542 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
543
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
544 #ifndef SQLITE_OMIT_UTF16
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
545 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
546 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
547 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
548 int sqlite3_prepare16(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
549 sqlite3 *db, /* Database handle. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
550 const void *zSql, /* UTF-8 encoded SQL statement. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
551 int nBytes, /* Length of zSql in bytes. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
552 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
553 const void **pzTail /* OUT: End of parsed string */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
554 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
555 /* This function currently works by first transforming the UTF-16
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
556 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
557 ** tricky bit is figuring out the pointer to return in *pzTail.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
558 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
559 char *zSql8;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
560 const char *zTail8 = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
561 int rc = SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
562
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
563 if( sqlite3SafetyCheck(db) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
564 return SQLITE_MISUSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
565 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
566 zSql8 = sqlite3utf16to8(zSql, nBytes);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
567 if( zSql8 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
568 rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
569 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
570
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
571 if( zTail8 && pzTail ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
572 /* If sqlite3_prepare returns a tail pointer, we calculate the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
573 ** equivalent pointer into the UTF-16 string by counting the unicode
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
574 ** characters between zSql8 and zTail8, and then returning a pointer
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
575 ** the same number of characters into the UTF-16 string.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
576 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
577 int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
578 *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
579 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
580 sqliteFree(zSql8);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
581 return sqlite3ApiExit(db, rc);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
582 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
583 #endif /* SQLITE_OMIT_UTF16 */