1*c5c4113dSnw141292
2*c5c4113dSnw141292 #pragma ident "%Z%%M% %I% %E% SMI"
3*c5c4113dSnw141292
4*c5c4113dSnw141292 /*
5*c5c4113dSnw141292 ** 2001 September 15
6*c5c4113dSnw141292 **
7*c5c4113dSnw141292 ** The author disclaims copyright to this source code. In place of
8*c5c4113dSnw141292 ** a legal notice, here is a blessing:
9*c5c4113dSnw141292 **
10*c5c4113dSnw141292 ** May you do good and not evil.
11*c5c4113dSnw141292 ** May you find forgiveness for yourself and forgive others.
12*c5c4113dSnw141292 ** May you share freely, never taking more than you give.
13*c5c4113dSnw141292 **
14*c5c4113dSnw141292 *************************************************************************
15*c5c4113dSnw141292 ** Main file for the SQLite library. The routines in this file
16*c5c4113dSnw141292 ** implement the programmer interface to the library. Routines in
17*c5c4113dSnw141292 ** other files are for internal use by SQLite and should not be
18*c5c4113dSnw141292 ** accessed by users of the library.
19*c5c4113dSnw141292 **
20*c5c4113dSnw141292 ** $Id: main.c,v 1.164.2.2 2004/06/26 14:40:05 drh Exp $
21*c5c4113dSnw141292 */
22*c5c4113dSnw141292 #include "sqliteInt.h"
23*c5c4113dSnw141292 #include "os.h"
24*c5c4113dSnw141292 #include <ctype.h>
25*c5c4113dSnw141292
26*c5c4113dSnw141292 /*
27*c5c4113dSnw141292 ** A pointer to this structure is used to communicate information
28*c5c4113dSnw141292 ** from sqliteInit into the sqliteInitCallback.
29*c5c4113dSnw141292 */
30*c5c4113dSnw141292 typedef struct {
31*c5c4113dSnw141292 sqlite *db; /* The database being initialized */
32*c5c4113dSnw141292 char **pzErrMsg; /* Error message stored here */
33*c5c4113dSnw141292 } InitData;
34*c5c4113dSnw141292
35*c5c4113dSnw141292 /*
36*c5c4113dSnw141292 ** Fill the InitData structure with an error message that indicates
37*c5c4113dSnw141292 ** that the database is corrupt.
38*c5c4113dSnw141292 */
corruptSchema(InitData * pData,const char * zExtra)39*c5c4113dSnw141292 static void corruptSchema(InitData *pData, const char *zExtra){
40*c5c4113dSnw141292 sqliteSetString(pData->pzErrMsg, "malformed database schema",
41*c5c4113dSnw141292 zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0);
42*c5c4113dSnw141292 }
43*c5c4113dSnw141292
44*c5c4113dSnw141292 /*
45*c5c4113dSnw141292 ** This is the callback routine for the code that initializes the
46*c5c4113dSnw141292 ** database. See sqliteInit() below for additional information.
47*c5c4113dSnw141292 **
48*c5c4113dSnw141292 ** Each callback contains the following information:
49*c5c4113dSnw141292 **
50*c5c4113dSnw141292 ** argv[0] = "file-format" or "schema-cookie" or "table" or "index"
51*c5c4113dSnw141292 ** argv[1] = table or index name or meta statement type.
52*c5c4113dSnw141292 ** argv[2] = root page number for table or index. NULL for meta.
53*c5c4113dSnw141292 ** argv[3] = SQL text for a CREATE TABLE or CREATE INDEX statement.
54*c5c4113dSnw141292 ** argv[4] = "1" for temporary files, "0" for main database, "2" or more
55*c5c4113dSnw141292 ** for auxiliary database files.
56*c5c4113dSnw141292 **
57*c5c4113dSnw141292 */
58*c5c4113dSnw141292 static
sqliteInitCallback(void * pInit,int argc,char ** argv,char ** azColName)59*c5c4113dSnw141292 int sqliteInitCallback(void *pInit, int argc, char **argv, char **azColName){
60*c5c4113dSnw141292 InitData *pData = (InitData*)pInit;
61*c5c4113dSnw141292 int nErr = 0;
62*c5c4113dSnw141292
63*c5c4113dSnw141292 assert( argc==5 );
64*c5c4113dSnw141292 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
65*c5c4113dSnw141292 if( argv[0]==0 ){
66*c5c4113dSnw141292 corruptSchema(pData, 0);
67*c5c4113dSnw141292 return 1;
68*c5c4113dSnw141292 }
69*c5c4113dSnw141292 switch( argv[0][0] ){
70*c5c4113dSnw141292 case 'v':
71*c5c4113dSnw141292 case 'i':
72*c5c4113dSnw141292 case 't': { /* CREATE TABLE, CREATE INDEX, or CREATE VIEW statements */
73*c5c4113dSnw141292 sqlite *db = pData->db;
74*c5c4113dSnw141292 if( argv[2]==0 || argv[4]==0 ){
75*c5c4113dSnw141292 corruptSchema(pData, 0);
76*c5c4113dSnw141292 return 1;
77*c5c4113dSnw141292 }
78*c5c4113dSnw141292 if( argv[3] && argv[3][0] ){
79*c5c4113dSnw141292 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
80*c5c4113dSnw141292 ** But because db->init.busy is set to 1, no VDBE code is generated
81*c5c4113dSnw141292 ** or executed. All the parser does is build the internal data
82*c5c4113dSnw141292 ** structures that describe the table, index, or view.
83*c5c4113dSnw141292 */
84*c5c4113dSnw141292 char *zErr;
85*c5c4113dSnw141292 assert( db->init.busy );
86*c5c4113dSnw141292 db->init.iDb = atoi(argv[4]);
87*c5c4113dSnw141292 assert( db->init.iDb>=0 && db->init.iDb<db->nDb );
88*c5c4113dSnw141292 db->init.newTnum = atoi(argv[2]);
89*c5c4113dSnw141292 if( sqlite_exec(db, argv[3], 0, 0, &zErr) ){
90*c5c4113dSnw141292 corruptSchema(pData, zErr);
91*c5c4113dSnw141292 sqlite_freemem(zErr);
92*c5c4113dSnw141292 }
93*c5c4113dSnw141292 db->init.iDb = 0;
94*c5c4113dSnw141292 }else{
95*c5c4113dSnw141292 /* If the SQL column is blank it means this is an index that
96*c5c4113dSnw141292 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
97*c5c4113dSnw141292 ** constraint for a CREATE TABLE. The index should have already
98*c5c4113dSnw141292 ** been created when we processed the CREATE TABLE. All we have
99*c5c4113dSnw141292 ** to do here is record the root page number for that index.
100*c5c4113dSnw141292 */
101*c5c4113dSnw141292 int iDb;
102*c5c4113dSnw141292 Index *pIndex;
103*c5c4113dSnw141292
104*c5c4113dSnw141292 iDb = atoi(argv[4]);
105*c5c4113dSnw141292 assert( iDb>=0 && iDb<db->nDb );
106*c5c4113dSnw141292 pIndex = sqliteFindIndex(db, argv[1], db->aDb[iDb].zName);
107*c5c4113dSnw141292 if( pIndex==0 || pIndex->tnum!=0 ){
108*c5c4113dSnw141292 /* This can occur if there exists an index on a TEMP table which
109*c5c4113dSnw141292 ** has the same name as another index on a permanent index. Since
110*c5c4113dSnw141292 ** the permanent table is hidden by the TEMP table, we can also
111*c5c4113dSnw141292 ** safely ignore the index on the permanent table.
112*c5c4113dSnw141292 */
113*c5c4113dSnw141292 /* Do Nothing */;
114*c5c4113dSnw141292 }else{
115*c5c4113dSnw141292 pIndex->tnum = atoi(argv[2]);
116*c5c4113dSnw141292 }
117*c5c4113dSnw141292 }
118*c5c4113dSnw141292 break;
119*c5c4113dSnw141292 }
120*c5c4113dSnw141292 default: {
121*c5c4113dSnw141292 /* This can not happen! */
122*c5c4113dSnw141292 nErr = 1;
123*c5c4113dSnw141292 assert( nErr==0 );
124*c5c4113dSnw141292 }
125*c5c4113dSnw141292 }
126*c5c4113dSnw141292 return nErr;
127*c5c4113dSnw141292 }
128*c5c4113dSnw141292
129*c5c4113dSnw141292 /*
130*c5c4113dSnw141292 ** This is a callback procedure used to reconstruct a table. The
131*c5c4113dSnw141292 ** name of the table to be reconstructed is passed in as argv[0].
132*c5c4113dSnw141292 **
133*c5c4113dSnw141292 ** This routine is used to automatically upgrade a database from
134*c5c4113dSnw141292 ** format version 1 or 2 to version 3. The correct operation of
135*c5c4113dSnw141292 ** this routine relys on the fact that no indices are used when
136*c5c4113dSnw141292 ** copying a table out to a temporary file.
137*c5c4113dSnw141292 **
138*c5c4113dSnw141292 ** The change from version 2 to version 3 occurred between SQLite
139*c5c4113dSnw141292 ** version 2.5.6 and 2.6.0 on 2002-July-18.
140*c5c4113dSnw141292 */
141*c5c4113dSnw141292 static
upgrade_3_callback(void * pInit,int argc,char ** argv,char ** NotUsed)142*c5c4113dSnw141292 int upgrade_3_callback(void *pInit, int argc, char **argv, char **NotUsed){
143*c5c4113dSnw141292 InitData *pData = (InitData*)pInit;
144*c5c4113dSnw141292 int rc;
145*c5c4113dSnw141292 Table *pTab;
146*c5c4113dSnw141292 Trigger *pTrig;
147*c5c4113dSnw141292 char *zErr = 0;
148*c5c4113dSnw141292
149*c5c4113dSnw141292 pTab = sqliteFindTable(pData->db, argv[0], 0);
150*c5c4113dSnw141292 assert( pTab!=0 );
151*c5c4113dSnw141292 assert( sqliteStrICmp(pTab->zName, argv[0])==0 );
152*c5c4113dSnw141292 if( pTab ){
153*c5c4113dSnw141292 pTrig = pTab->pTrigger;
154*c5c4113dSnw141292 pTab->pTrigger = 0; /* Disable all triggers before rebuilding the table */
155*c5c4113dSnw141292 }
156*c5c4113dSnw141292 rc = sqlite_exec_printf(pData->db,
157*c5c4113dSnw141292 "CREATE TEMP TABLE sqlite_x AS SELECT * FROM '%q'; "
158*c5c4113dSnw141292 "DELETE FROM '%q'; "
159*c5c4113dSnw141292 "INSERT INTO '%q' SELECT * FROM sqlite_x; "
160*c5c4113dSnw141292 "DROP TABLE sqlite_x;",
161*c5c4113dSnw141292 0, 0, &zErr, argv[0], argv[0], argv[0]);
162*c5c4113dSnw141292 if( zErr ){
163*c5c4113dSnw141292 if( *pData->pzErrMsg ) sqlite_freemem(*pData->pzErrMsg);
164*c5c4113dSnw141292 *pData->pzErrMsg = zErr;
165*c5c4113dSnw141292 }
166*c5c4113dSnw141292
167*c5c4113dSnw141292 /* If an error occurred in the SQL above, then the transaction will
168*c5c4113dSnw141292 ** rollback which will delete the internal symbol tables. This will
169*c5c4113dSnw141292 ** cause the structure that pTab points to be deleted. In case that
170*c5c4113dSnw141292 ** happened, we need to refetch pTab.
171*c5c4113dSnw141292 */
172*c5c4113dSnw141292 pTab = sqliteFindTable(pData->db, argv[0], 0);
173*c5c4113dSnw141292 if( pTab ){
174*c5c4113dSnw141292 assert( sqliteStrICmp(pTab->zName, argv[0])==0 );
175*c5c4113dSnw141292 pTab->pTrigger = pTrig; /* Re-enable triggers */
176*c5c4113dSnw141292 }
177*c5c4113dSnw141292 return rc!=SQLITE_OK;
178*c5c4113dSnw141292 }
179*c5c4113dSnw141292
180*c5c4113dSnw141292
181*c5c4113dSnw141292
182*c5c4113dSnw141292 /*
183*c5c4113dSnw141292 ** Attempt to read the database schema and initialize internal
184*c5c4113dSnw141292 ** data structures for a single database file. The index of the
185*c5c4113dSnw141292 ** database file is given by iDb. iDb==0 is used for the main
186*c5c4113dSnw141292 ** database. iDb==1 should never be used. iDb>=2 is used for
187*c5c4113dSnw141292 ** auxiliary databases. Return one of the SQLITE_ error codes to
188*c5c4113dSnw141292 ** indicate success or failure.
189*c5c4113dSnw141292 */
sqliteInitOne(sqlite * db,int iDb,char ** pzErrMsg)190*c5c4113dSnw141292 static int sqliteInitOne(sqlite *db, int iDb, char **pzErrMsg){
191*c5c4113dSnw141292 int rc;
192*c5c4113dSnw141292 BtCursor *curMain;
193*c5c4113dSnw141292 int size;
194*c5c4113dSnw141292 Table *pTab;
195*c5c4113dSnw141292 char const *azArg[6];
196*c5c4113dSnw141292 char zDbNum[30];
197*c5c4113dSnw141292 int meta[SQLITE_N_BTREE_META];
198*c5c4113dSnw141292 InitData initData;
199*c5c4113dSnw141292 char const *zMasterSchema;
200*c5c4113dSnw141292 char const *zMasterName;
201*c5c4113dSnw141292 char *zSql = 0;
202*c5c4113dSnw141292
203*c5c4113dSnw141292 /*
204*c5c4113dSnw141292 ** The master database table has a structure like this
205*c5c4113dSnw141292 */
206*c5c4113dSnw141292 static char master_schema[] =
207*c5c4113dSnw141292 "CREATE TABLE sqlite_master(\n"
208*c5c4113dSnw141292 " type text,\n"
209*c5c4113dSnw141292 " name text,\n"
210*c5c4113dSnw141292 " tbl_name text,\n"
211*c5c4113dSnw141292 " rootpage integer,\n"
212*c5c4113dSnw141292 " sql text\n"
213*c5c4113dSnw141292 ")"
214*c5c4113dSnw141292 ;
215*c5c4113dSnw141292 static char temp_master_schema[] =
216*c5c4113dSnw141292 "CREATE TEMP TABLE sqlite_temp_master(\n"
217*c5c4113dSnw141292 " type text,\n"
218*c5c4113dSnw141292 " name text,\n"
219*c5c4113dSnw141292 " tbl_name text,\n"
220*c5c4113dSnw141292 " rootpage integer,\n"
221*c5c4113dSnw141292 " sql text\n"
222*c5c4113dSnw141292 ")"
223*c5c4113dSnw141292 ;
224*c5c4113dSnw141292
225*c5c4113dSnw141292 assert( iDb>=0 && iDb<db->nDb );
226*c5c4113dSnw141292
227*c5c4113dSnw141292 /* zMasterSchema and zInitScript are set to point at the master schema
228*c5c4113dSnw141292 ** and initialisation script appropriate for the database being
229*c5c4113dSnw141292 ** initialised. zMasterName is the name of the master table.
230*c5c4113dSnw141292 */
231*c5c4113dSnw141292 if( iDb==1 ){
232*c5c4113dSnw141292 zMasterSchema = temp_master_schema;
233*c5c4113dSnw141292 zMasterName = TEMP_MASTER_NAME;
234*c5c4113dSnw141292 }else{
235*c5c4113dSnw141292 zMasterSchema = master_schema;
236*c5c4113dSnw141292 zMasterName = MASTER_NAME;
237*c5c4113dSnw141292 }
238*c5c4113dSnw141292
239*c5c4113dSnw141292 /* Construct the schema table.
240*c5c4113dSnw141292 */
241*c5c4113dSnw141292 sqliteSafetyOff(db);
242*c5c4113dSnw141292 azArg[0] = "table";
243*c5c4113dSnw141292 azArg[1] = zMasterName;
244*c5c4113dSnw141292 azArg[2] = "2";
245*c5c4113dSnw141292 azArg[3] = zMasterSchema;
246*c5c4113dSnw141292 sprintf(zDbNum, "%d", iDb);
247*c5c4113dSnw141292 azArg[4] = zDbNum;
248*c5c4113dSnw141292 azArg[5] = 0;
249*c5c4113dSnw141292 initData.db = db;
250*c5c4113dSnw141292 initData.pzErrMsg = pzErrMsg;
251*c5c4113dSnw141292 sqliteInitCallback(&initData, 5, (char **)azArg, 0);
252*c5c4113dSnw141292 pTab = sqliteFindTable(db, zMasterName, db->aDb[iDb].zName);
253*c5c4113dSnw141292 if( pTab ){
254*c5c4113dSnw141292 pTab->readOnly = 1;
255*c5c4113dSnw141292 }else{
256*c5c4113dSnw141292 return SQLITE_NOMEM;
257*c5c4113dSnw141292 }
258*c5c4113dSnw141292 sqliteSafetyOn(db);
259*c5c4113dSnw141292
260*c5c4113dSnw141292 /* Create a cursor to hold the database open
261*c5c4113dSnw141292 */
262*c5c4113dSnw141292 if( db->aDb[iDb].pBt==0 ) return SQLITE_OK;
263*c5c4113dSnw141292 rc = sqliteBtreeCursor(db->aDb[iDb].pBt, 2, 0, &curMain);
264*c5c4113dSnw141292 if( rc ){
265*c5c4113dSnw141292 sqliteSetString(pzErrMsg, sqlite_error_string(rc), (char*)0);
266*c5c4113dSnw141292 return rc;
267*c5c4113dSnw141292 }
268*c5c4113dSnw141292
269*c5c4113dSnw141292 /* Get the database meta information
270*c5c4113dSnw141292 */
271*c5c4113dSnw141292 rc = sqliteBtreeGetMeta(db->aDb[iDb].pBt, meta);
272*c5c4113dSnw141292 if( rc ){
273*c5c4113dSnw141292 sqliteSetString(pzErrMsg, sqlite_error_string(rc), (char*)0);
274*c5c4113dSnw141292 sqliteBtreeCloseCursor(curMain);
275*c5c4113dSnw141292 return rc;
276*c5c4113dSnw141292 }
277*c5c4113dSnw141292 db->aDb[iDb].schema_cookie = meta[1];
278*c5c4113dSnw141292 if( iDb==0 ){
279*c5c4113dSnw141292 db->next_cookie = meta[1];
280*c5c4113dSnw141292 db->file_format = meta[2];
281*c5c4113dSnw141292 size = meta[3];
282*c5c4113dSnw141292 if( size==0 ){ size = MAX_PAGES; }
283*c5c4113dSnw141292 db->cache_size = size;
284*c5c4113dSnw141292 db->safety_level = meta[4];
285*c5c4113dSnw141292 if( meta[6]>0 && meta[6]<=2 && db->temp_store==0 ){
286*c5c4113dSnw141292 db->temp_store = meta[6];
287*c5c4113dSnw141292 }
288*c5c4113dSnw141292 if( db->safety_level==0 ) db->safety_level = 2;
289*c5c4113dSnw141292
290*c5c4113dSnw141292 /*
291*c5c4113dSnw141292 ** file_format==1 Version 2.1.0.
292*c5c4113dSnw141292 ** file_format==2 Version 2.2.0. Add support for INTEGER PRIMARY KEY.
293*c5c4113dSnw141292 ** file_format==3 Version 2.6.0. Fix empty-string index bug.
294*c5c4113dSnw141292 ** file_format==4 Version 2.7.0. Add support for separate numeric and
295*c5c4113dSnw141292 ** text datatypes.
296*c5c4113dSnw141292 */
297*c5c4113dSnw141292 if( db->file_format==0 ){
298*c5c4113dSnw141292 /* This happens if the database was initially empty */
299*c5c4113dSnw141292 db->file_format = 4;
300*c5c4113dSnw141292 }else if( db->file_format>4 ){
301*c5c4113dSnw141292 sqliteBtreeCloseCursor(curMain);
302*c5c4113dSnw141292 sqliteSetString(pzErrMsg, "unsupported file format", (char*)0);
303*c5c4113dSnw141292 return SQLITE_ERROR;
304*c5c4113dSnw141292 }
305*c5c4113dSnw141292 }else if( iDb!=1 && (db->file_format!=meta[2] || db->file_format<4) ){
306*c5c4113dSnw141292 assert( db->file_format>=4 );
307*c5c4113dSnw141292 if( meta[2]==0 ){
308*c5c4113dSnw141292 sqliteSetString(pzErrMsg, "cannot attach empty database: ",
309*c5c4113dSnw141292 db->aDb[iDb].zName, (char*)0);
310*c5c4113dSnw141292 }else{
311*c5c4113dSnw141292 sqliteSetString(pzErrMsg, "incompatible file format in auxiliary "
312*c5c4113dSnw141292 "database: ", db->aDb[iDb].zName, (char*)0);
313*c5c4113dSnw141292 }
314*c5c4113dSnw141292 sqliteBtreeClose(db->aDb[iDb].pBt);
315*c5c4113dSnw141292 db->aDb[iDb].pBt = 0;
316*c5c4113dSnw141292 return SQLITE_FORMAT;
317*c5c4113dSnw141292 }
318*c5c4113dSnw141292 sqliteBtreeSetCacheSize(db->aDb[iDb].pBt, db->cache_size);
319*c5c4113dSnw141292 sqliteBtreeSetSafetyLevel(db->aDb[iDb].pBt, meta[4]==0 ? 2 : meta[4]);
320*c5c4113dSnw141292
321*c5c4113dSnw141292 /* Read the schema information out of the schema tables
322*c5c4113dSnw141292 */
323*c5c4113dSnw141292 assert( db->init.busy );
324*c5c4113dSnw141292 sqliteSafetyOff(db);
325*c5c4113dSnw141292
326*c5c4113dSnw141292 /* The following SQL will read the schema from the master tables.
327*c5c4113dSnw141292 ** The first version works with SQLite file formats 2 or greater.
328*c5c4113dSnw141292 ** The second version is for format 1 files.
329*c5c4113dSnw141292 **
330*c5c4113dSnw141292 ** Beginning with file format 2, the rowid for new table entries
331*c5c4113dSnw141292 ** (including entries in sqlite_master) is an increasing integer.
332*c5c4113dSnw141292 ** So for file format 2 and later, we can play back sqlite_master
333*c5c4113dSnw141292 ** and all the CREATE statements will appear in the right order.
334*c5c4113dSnw141292 ** But with file format 1, table entries were random and so we
335*c5c4113dSnw141292 ** have to make sure the CREATE TABLEs occur before their corresponding
336*c5c4113dSnw141292 ** CREATE INDEXs. (We don't have to deal with CREATE VIEW or
337*c5c4113dSnw141292 ** CREATE TRIGGER in file format 1 because those constructs did
338*c5c4113dSnw141292 ** not exist then.)
339*c5c4113dSnw141292 */
340*c5c4113dSnw141292 if( db->file_format>=2 ){
341*c5c4113dSnw141292 sqliteSetString(&zSql,
342*c5c4113dSnw141292 "SELECT type, name, rootpage, sql, ", zDbNum, " FROM \"",
343*c5c4113dSnw141292 db->aDb[iDb].zName, "\".", zMasterName, (char*)0);
344*c5c4113dSnw141292 }else{
345*c5c4113dSnw141292 sqliteSetString(&zSql,
346*c5c4113dSnw141292 "SELECT type, name, rootpage, sql, ", zDbNum, " FROM \"",
347*c5c4113dSnw141292 db->aDb[iDb].zName, "\".", zMasterName,
348*c5c4113dSnw141292 " WHERE type IN ('table', 'index')"
349*c5c4113dSnw141292 " ORDER BY CASE type WHEN 'table' THEN 0 ELSE 1 END", (char*)0);
350*c5c4113dSnw141292 }
351*c5c4113dSnw141292 rc = sqlite_exec(db, zSql, sqliteInitCallback, &initData, 0);
352*c5c4113dSnw141292
353*c5c4113dSnw141292 sqliteFree(zSql);
354*c5c4113dSnw141292 sqliteSafetyOn(db);
355*c5c4113dSnw141292 sqliteBtreeCloseCursor(curMain);
356*c5c4113dSnw141292 if( sqlite_malloc_failed ){
357*c5c4113dSnw141292 sqliteSetString(pzErrMsg, "out of memory", (char*)0);
358*c5c4113dSnw141292 rc = SQLITE_NOMEM;
359*c5c4113dSnw141292 sqliteResetInternalSchema(db, 0);
360*c5c4113dSnw141292 }
361*c5c4113dSnw141292 if( rc==SQLITE_OK ){
362*c5c4113dSnw141292 DbSetProperty(db, iDb, DB_SchemaLoaded);
363*c5c4113dSnw141292 }else{
364*c5c4113dSnw141292 sqliteResetInternalSchema(db, iDb);
365*c5c4113dSnw141292 }
366*c5c4113dSnw141292 return rc;
367*c5c4113dSnw141292 }
368*c5c4113dSnw141292
369*c5c4113dSnw141292 /*
370*c5c4113dSnw141292 ** Initialize all database files - the main database file, the file
371*c5c4113dSnw141292 ** used to store temporary tables, and any additional database files
372*c5c4113dSnw141292 ** created using ATTACH statements. Return a success code. If an
373*c5c4113dSnw141292 ** error occurs, write an error message into *pzErrMsg.
374*c5c4113dSnw141292 **
375*c5c4113dSnw141292 ** After the database is initialized, the SQLITE_Initialized
376*c5c4113dSnw141292 ** bit is set in the flags field of the sqlite structure. An
377*c5c4113dSnw141292 ** attempt is made to initialize the database as soon as it
378*c5c4113dSnw141292 ** is opened. If that fails (perhaps because another process
379*c5c4113dSnw141292 ** has the sqlite_master table locked) than another attempt
380*c5c4113dSnw141292 ** is made the first time the database is accessed.
381*c5c4113dSnw141292 */
sqliteInit(sqlite * db,char ** pzErrMsg)382*c5c4113dSnw141292 int sqliteInit(sqlite *db, char **pzErrMsg){
383*c5c4113dSnw141292 int i, rc;
384*c5c4113dSnw141292
385*c5c4113dSnw141292 if( db->init.busy ) return SQLITE_OK;
386*c5c4113dSnw141292 assert( (db->flags & SQLITE_Initialized)==0 );
387*c5c4113dSnw141292 rc = SQLITE_OK;
388*c5c4113dSnw141292 db->init.busy = 1;
389*c5c4113dSnw141292 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
390*c5c4113dSnw141292 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
391*c5c4113dSnw141292 rc = sqliteInitOne(db, i, pzErrMsg);
392*c5c4113dSnw141292 if( rc ){
393*c5c4113dSnw141292 sqliteResetInternalSchema(db, i);
394*c5c4113dSnw141292 }
395*c5c4113dSnw141292 }
396*c5c4113dSnw141292
397*c5c4113dSnw141292 /* Once all the other databases have been initialised, load the schema
398*c5c4113dSnw141292 ** for the TEMP database. This is loaded last, as the TEMP database
399*c5c4113dSnw141292 ** schema may contain references to objects in other databases.
400*c5c4113dSnw141292 */
401*c5c4113dSnw141292 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
402*c5c4113dSnw141292 rc = sqliteInitOne(db, 1, pzErrMsg);
403*c5c4113dSnw141292 if( rc ){
404*c5c4113dSnw141292 sqliteResetInternalSchema(db, 1);
405*c5c4113dSnw141292 }
406*c5c4113dSnw141292 }
407*c5c4113dSnw141292
408*c5c4113dSnw141292 db->init.busy = 0;
409*c5c4113dSnw141292 if( rc==SQLITE_OK ){
410*c5c4113dSnw141292 db->flags |= SQLITE_Initialized;
411*c5c4113dSnw141292 sqliteCommitInternalChanges(db);
412*c5c4113dSnw141292 }
413*c5c4113dSnw141292
414*c5c4113dSnw141292 /* If the database is in formats 1 or 2, then upgrade it to
415*c5c4113dSnw141292 ** version 3. This will reconstruct all indices. If the
416*c5c4113dSnw141292 ** upgrade fails for any reason (ex: out of disk space, database
417*c5c4113dSnw141292 ** is read only, interrupt received, etc.) then fail the init.
418*c5c4113dSnw141292 */
419*c5c4113dSnw141292 if( rc==SQLITE_OK && db->file_format<3 ){
420*c5c4113dSnw141292 char *zErr = 0;
421*c5c4113dSnw141292 InitData initData;
422*c5c4113dSnw141292 int meta[SQLITE_N_BTREE_META];
423*c5c4113dSnw141292
424*c5c4113dSnw141292 db->magic = SQLITE_MAGIC_OPEN;
425*c5c4113dSnw141292 initData.db = db;
426*c5c4113dSnw141292 initData.pzErrMsg = &zErr;
427*c5c4113dSnw141292 db->file_format = 3;
428*c5c4113dSnw141292 rc = sqlite_exec(db,
429*c5c4113dSnw141292 "BEGIN; SELECT name FROM sqlite_master WHERE type='table';",
430*c5c4113dSnw141292 upgrade_3_callback,
431*c5c4113dSnw141292 &initData,
432*c5c4113dSnw141292 &zErr);
433*c5c4113dSnw141292 if( rc==SQLITE_OK ){
434*c5c4113dSnw141292 sqliteBtreeGetMeta(db->aDb[0].pBt, meta);
435*c5c4113dSnw141292 meta[2] = 4;
436*c5c4113dSnw141292 sqliteBtreeUpdateMeta(db->aDb[0].pBt, meta);
437*c5c4113dSnw141292 sqlite_exec(db, "COMMIT", 0, 0, 0);
438*c5c4113dSnw141292 }
439*c5c4113dSnw141292 if( rc!=SQLITE_OK ){
440*c5c4113dSnw141292 sqliteSetString(pzErrMsg,
441*c5c4113dSnw141292 "unable to upgrade database to the version 2.6 format",
442*c5c4113dSnw141292 zErr ? ": " : 0, zErr, (char*)0);
443*c5c4113dSnw141292 }
444*c5c4113dSnw141292 sqlite_freemem(zErr);
445*c5c4113dSnw141292 }
446*c5c4113dSnw141292
447*c5c4113dSnw141292 if( rc!=SQLITE_OK ){
448*c5c4113dSnw141292 db->flags &= ~SQLITE_Initialized;
449*c5c4113dSnw141292 }
450*c5c4113dSnw141292 return rc;
451*c5c4113dSnw141292 }
452*c5c4113dSnw141292
453*c5c4113dSnw141292 /*
454*c5c4113dSnw141292 ** The version of the library
455*c5c4113dSnw141292 */
456*c5c4113dSnw141292 const char rcsid[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $";
457*c5c4113dSnw141292 const char sqlite_version[] = SQLITE_VERSION;
458*c5c4113dSnw141292
459*c5c4113dSnw141292 /*
460*c5c4113dSnw141292 ** Does the library expect data to be encoded as UTF-8 or iso8859? The
461*c5c4113dSnw141292 ** following global constant always lets us know.
462*c5c4113dSnw141292 */
463*c5c4113dSnw141292 #ifdef SQLITE_UTF8
464*c5c4113dSnw141292 const char sqlite_encoding[] = "UTF-8";
465*c5c4113dSnw141292 #else
466*c5c4113dSnw141292 const char sqlite_encoding[] = "iso8859";
467*c5c4113dSnw141292 #endif
468*c5c4113dSnw141292
469*c5c4113dSnw141292 /*
470*c5c4113dSnw141292 ** Open a new SQLite database. Construct an "sqlite" structure to define
471*c5c4113dSnw141292 ** the state of this database and return a pointer to that structure.
472*c5c4113dSnw141292 **
473*c5c4113dSnw141292 ** An attempt is made to initialize the in-memory data structures that
474*c5c4113dSnw141292 ** hold the database schema. But if this fails (because the schema file
475*c5c4113dSnw141292 ** is locked) then that step is deferred until the first call to
476*c5c4113dSnw141292 ** sqlite_exec().
477*c5c4113dSnw141292 */
sqlite_open(const char * zFilename,int mode,char ** pzErrMsg)478*c5c4113dSnw141292 sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
479*c5c4113dSnw141292 sqlite *db;
480*c5c4113dSnw141292 int rc, i;
481*c5c4113dSnw141292
482*c5c4113dSnw141292 /* Allocate the sqlite data structure */
483*c5c4113dSnw141292 db = sqliteMalloc( sizeof(sqlite) );
484*c5c4113dSnw141292 if( pzErrMsg ) *pzErrMsg = 0;
485*c5c4113dSnw141292 if( db==0 ) goto no_mem_on_open;
486*c5c4113dSnw141292 db->onError = OE_Default;
487*c5c4113dSnw141292 db->priorNewRowid = 0;
488*c5c4113dSnw141292 db->magic = SQLITE_MAGIC_BUSY;
489*c5c4113dSnw141292 db->nDb = 2;
490*c5c4113dSnw141292 db->aDb = db->aDbStatic;
491*c5c4113dSnw141292 /* db->flags |= SQLITE_ShortColNames; */
492*c5c4113dSnw141292 sqliteHashInit(&db->aFunc, SQLITE_HASH_STRING, 1);
493*c5c4113dSnw141292 for(i=0; i<db->nDb; i++){
494*c5c4113dSnw141292 sqliteHashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
495*c5c4113dSnw141292 sqliteHashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
496*c5c4113dSnw141292 sqliteHashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);
497*c5c4113dSnw141292 sqliteHashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1);
498*c5c4113dSnw141292 }
499*c5c4113dSnw141292
500*c5c4113dSnw141292 /* Open the backend database driver */
501*c5c4113dSnw141292 if( zFilename[0]==':' && strcmp(zFilename,":memory:")==0 ){
502*c5c4113dSnw141292 db->temp_store = 2;
503*c5c4113dSnw141292 }
504*c5c4113dSnw141292 rc = sqliteBtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt);
505*c5c4113dSnw141292 if( rc!=SQLITE_OK ){
506*c5c4113dSnw141292 switch( rc ){
507*c5c4113dSnw141292 default: {
508*c5c4113dSnw141292 sqliteSetString(pzErrMsg, "unable to open database: ",
509*c5c4113dSnw141292 zFilename, (char*)0);
510*c5c4113dSnw141292 }
511*c5c4113dSnw141292 }
512*c5c4113dSnw141292 sqliteFree(db);
513*c5c4113dSnw141292 sqliteStrRealloc(pzErrMsg);
514*c5c4113dSnw141292 return 0;
515*c5c4113dSnw141292 }
516*c5c4113dSnw141292 db->aDb[0].zName = "main";
517*c5c4113dSnw141292 db->aDb[1].zName = "temp";
518*c5c4113dSnw141292
519*c5c4113dSnw141292 /* Attempt to read the schema */
520*c5c4113dSnw141292 sqliteRegisterBuiltinFunctions(db);
521*c5c4113dSnw141292 rc = sqliteInit(db, pzErrMsg);
522*c5c4113dSnw141292 db->magic = SQLITE_MAGIC_OPEN;
523*c5c4113dSnw141292 if( sqlite_malloc_failed ){
524*c5c4113dSnw141292 sqlite_close(db);
525*c5c4113dSnw141292 goto no_mem_on_open;
526*c5c4113dSnw141292 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
527*c5c4113dSnw141292 sqlite_close(db);
528*c5c4113dSnw141292 sqliteStrRealloc(pzErrMsg);
529*c5c4113dSnw141292 return 0;
530*c5c4113dSnw141292 }else if( pzErrMsg ){
531*c5c4113dSnw141292 sqliteFree(*pzErrMsg);
532*c5c4113dSnw141292 *pzErrMsg = 0;
533*c5c4113dSnw141292 }
534*c5c4113dSnw141292
535*c5c4113dSnw141292 /* Return a pointer to the newly opened database structure */
536*c5c4113dSnw141292 return db;
537*c5c4113dSnw141292
538*c5c4113dSnw141292 no_mem_on_open:
539*c5c4113dSnw141292 sqliteSetString(pzErrMsg, "out of memory", (char*)0);
540*c5c4113dSnw141292 sqliteStrRealloc(pzErrMsg);
541*c5c4113dSnw141292 return 0;
542*c5c4113dSnw141292 }
543*c5c4113dSnw141292
544*c5c4113dSnw141292 /*
545*c5c4113dSnw141292 ** Return the ROWID of the most recent insert
546*c5c4113dSnw141292 */
sqlite_last_insert_rowid(sqlite * db)547*c5c4113dSnw141292 int sqlite_last_insert_rowid(sqlite *db){
548*c5c4113dSnw141292 return db->lastRowid;
549*c5c4113dSnw141292 }
550*c5c4113dSnw141292
551*c5c4113dSnw141292 /*
552*c5c4113dSnw141292 ** Return the number of changes in the most recent call to sqlite_exec().
553*c5c4113dSnw141292 */
sqlite_changes(sqlite * db)554*c5c4113dSnw141292 int sqlite_changes(sqlite *db){
555*c5c4113dSnw141292 return db->nChange;
556*c5c4113dSnw141292 }
557*c5c4113dSnw141292
558*c5c4113dSnw141292 /*
559*c5c4113dSnw141292 ** Return the number of changes produced by the last INSERT, UPDATE, or
560*c5c4113dSnw141292 ** DELETE statement to complete execution. The count does not include
561*c5c4113dSnw141292 ** changes due to SQL statements executed in trigger programs that were
562*c5c4113dSnw141292 ** triggered by that statement
563*c5c4113dSnw141292 */
sqlite_last_statement_changes(sqlite * db)564*c5c4113dSnw141292 int sqlite_last_statement_changes(sqlite *db){
565*c5c4113dSnw141292 return db->lsChange;
566*c5c4113dSnw141292 }
567*c5c4113dSnw141292
568*c5c4113dSnw141292 /*
569*c5c4113dSnw141292 ** Close an existing SQLite database
570*c5c4113dSnw141292 */
sqlite_close(sqlite * db)571*c5c4113dSnw141292 void sqlite_close(sqlite *db){
572*c5c4113dSnw141292 HashElem *i;
573*c5c4113dSnw141292 int j;
574*c5c4113dSnw141292 db->want_to_close = 1;
575*c5c4113dSnw141292 if( sqliteSafetyCheck(db) || sqliteSafetyOn(db) ){
576*c5c4113dSnw141292 /* printf("DID NOT CLOSE\n"); fflush(stdout); */
577*c5c4113dSnw141292 return;
578*c5c4113dSnw141292 }
579*c5c4113dSnw141292 db->magic = SQLITE_MAGIC_CLOSED;
580*c5c4113dSnw141292 for(j=0; j<db->nDb; j++){
581*c5c4113dSnw141292 struct Db *pDb = &db->aDb[j];
582*c5c4113dSnw141292 if( pDb->pBt ){
583*c5c4113dSnw141292 sqliteBtreeClose(pDb->pBt);
584*c5c4113dSnw141292 pDb->pBt = 0;
585*c5c4113dSnw141292 }
586*c5c4113dSnw141292 }
587*c5c4113dSnw141292 sqliteResetInternalSchema(db, 0);
588*c5c4113dSnw141292 assert( db->nDb<=2 );
589*c5c4113dSnw141292 assert( db->aDb==db->aDbStatic );
590*c5c4113dSnw141292 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
591*c5c4113dSnw141292 FuncDef *pFunc, *pNext;
592*c5c4113dSnw141292 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
593*c5c4113dSnw141292 pNext = pFunc->pNext;
594*c5c4113dSnw141292 sqliteFree(pFunc);
595*c5c4113dSnw141292 }
596*c5c4113dSnw141292 }
597*c5c4113dSnw141292 sqliteHashClear(&db->aFunc);
598*c5c4113dSnw141292 sqliteFree(db);
599*c5c4113dSnw141292 }
600*c5c4113dSnw141292
601*c5c4113dSnw141292 /*
602*c5c4113dSnw141292 ** Rollback all database files.
603*c5c4113dSnw141292 */
sqliteRollbackAll(sqlite * db)604*c5c4113dSnw141292 void sqliteRollbackAll(sqlite *db){
605*c5c4113dSnw141292 int i;
606*c5c4113dSnw141292 for(i=0; i<db->nDb; i++){
607*c5c4113dSnw141292 if( db->aDb[i].pBt ){
608*c5c4113dSnw141292 sqliteBtreeRollback(db->aDb[i].pBt);
609*c5c4113dSnw141292 db->aDb[i].inTrans = 0;
610*c5c4113dSnw141292 }
611*c5c4113dSnw141292 }
612*c5c4113dSnw141292 sqliteResetInternalSchema(db, 0);
613*c5c4113dSnw141292 /* sqliteRollbackInternalChanges(db); */
614*c5c4113dSnw141292 }
615*c5c4113dSnw141292
616*c5c4113dSnw141292 /*
617*c5c4113dSnw141292 ** Execute SQL code. Return one of the SQLITE_ success/failure
618*c5c4113dSnw141292 ** codes. Also write an error message into memory obtained from
619*c5c4113dSnw141292 ** malloc() and make *pzErrMsg point to that message.
620*c5c4113dSnw141292 **
621*c5c4113dSnw141292 ** If the SQL is a query, then for each row in the query result
622*c5c4113dSnw141292 ** the xCallback() function is called. pArg becomes the first
623*c5c4113dSnw141292 ** argument to xCallback(). If xCallback=NULL then no callback
624*c5c4113dSnw141292 ** is invoked, even for queries.
625*c5c4113dSnw141292 */
sqlite_exec(sqlite * db,const char * zSql,sqlite_callback xCallback,void * pArg,char ** pzErrMsg)626*c5c4113dSnw141292 int sqlite_exec(
627*c5c4113dSnw141292 sqlite *db, /* The database on which the SQL executes */
628*c5c4113dSnw141292 const char *zSql, /* The SQL to be executed */
629*c5c4113dSnw141292 sqlite_callback xCallback, /* Invoke this callback routine */
630*c5c4113dSnw141292 void *pArg, /* First argument to xCallback() */
631*c5c4113dSnw141292 char **pzErrMsg /* Write error messages here */
632*c5c4113dSnw141292 ){
633*c5c4113dSnw141292 int rc = SQLITE_OK;
634*c5c4113dSnw141292 const char *zLeftover;
635*c5c4113dSnw141292 sqlite_vm *pVm;
636*c5c4113dSnw141292 int nRetry = 0;
637*c5c4113dSnw141292 int nChange = 0;
638*c5c4113dSnw141292 int nCallback;
639*c5c4113dSnw141292
640*c5c4113dSnw141292 if( zSql==0 ) return SQLITE_OK;
641*c5c4113dSnw141292 while( rc==SQLITE_OK && zSql[0] ){
642*c5c4113dSnw141292 pVm = 0;
643*c5c4113dSnw141292 rc = sqlite_compile(db, zSql, &zLeftover, &pVm, pzErrMsg);
644*c5c4113dSnw141292 if( rc!=SQLITE_OK ){
645*c5c4113dSnw141292 assert( pVm==0 || sqlite_malloc_failed );
646*c5c4113dSnw141292 return rc;
647*c5c4113dSnw141292 }
648*c5c4113dSnw141292 if( pVm==0 ){
649*c5c4113dSnw141292 /* This happens if the zSql input contained only whitespace */
650*c5c4113dSnw141292 break;
651*c5c4113dSnw141292 }
652*c5c4113dSnw141292 db->nChange += nChange;
653*c5c4113dSnw141292 nCallback = 0;
654*c5c4113dSnw141292 while(1){
655*c5c4113dSnw141292 int nArg;
656*c5c4113dSnw141292 char **azArg, **azCol;
657*c5c4113dSnw141292 rc = sqlite_step(pVm, &nArg, (const char***)&azArg,(const char***)&azCol);
658*c5c4113dSnw141292 if( rc==SQLITE_ROW ){
659*c5c4113dSnw141292 if( xCallback!=0 && xCallback(pArg, nArg, azArg, azCol) ){
660*c5c4113dSnw141292 sqlite_finalize(pVm, 0);
661*c5c4113dSnw141292 return SQLITE_ABORT;
662*c5c4113dSnw141292 }
663*c5c4113dSnw141292 nCallback++;
664*c5c4113dSnw141292 }else{
665*c5c4113dSnw141292 if( rc==SQLITE_DONE && nCallback==0
666*c5c4113dSnw141292 && (db->flags & SQLITE_NullCallback)!=0 && xCallback!=0 ){
667*c5c4113dSnw141292 xCallback(pArg, nArg, azArg, azCol);
668*c5c4113dSnw141292 }
669*c5c4113dSnw141292 rc = sqlite_finalize(pVm, pzErrMsg);
670*c5c4113dSnw141292 if( rc==SQLITE_SCHEMA && nRetry<2 ){
671*c5c4113dSnw141292 nRetry++;
672*c5c4113dSnw141292 rc = SQLITE_OK;
673*c5c4113dSnw141292 break;
674*c5c4113dSnw141292 }
675*c5c4113dSnw141292 if( db->pVdbe==0 ){
676*c5c4113dSnw141292 nChange = db->nChange;
677*c5c4113dSnw141292 }
678*c5c4113dSnw141292 nRetry = 0;
679*c5c4113dSnw141292 zSql = zLeftover;
680*c5c4113dSnw141292 while( isspace(zSql[0]) ) zSql++;
681*c5c4113dSnw141292 break;
682*c5c4113dSnw141292 }
683*c5c4113dSnw141292 }
684*c5c4113dSnw141292 }
685*c5c4113dSnw141292 return rc;
686*c5c4113dSnw141292 }
687*c5c4113dSnw141292
688*c5c4113dSnw141292
689*c5c4113dSnw141292 /*
690*c5c4113dSnw141292 ** Compile a single statement of SQL into a virtual machine. Return one
691*c5c4113dSnw141292 ** of the SQLITE_ success/failure codes. Also write an error message into
692*c5c4113dSnw141292 ** memory obtained from malloc() and make *pzErrMsg point to that message.
693*c5c4113dSnw141292 */
sqlite_compile(sqlite * db,const char * zSql,const char ** pzTail,sqlite_vm ** ppVm,char ** pzErrMsg)694*c5c4113dSnw141292 int sqlite_compile(
695*c5c4113dSnw141292 sqlite *db, /* The database on which the SQL executes */
696*c5c4113dSnw141292 const char *zSql, /* The SQL to be executed */
697*c5c4113dSnw141292 const char **pzTail, /* OUT: Next statement after the first */
698*c5c4113dSnw141292 sqlite_vm **ppVm, /* OUT: The virtual machine */
699*c5c4113dSnw141292 char **pzErrMsg /* OUT: Write error messages here */
700*c5c4113dSnw141292 ){
701*c5c4113dSnw141292 Parse sParse;
702*c5c4113dSnw141292
703*c5c4113dSnw141292 if( pzErrMsg ) *pzErrMsg = 0;
704*c5c4113dSnw141292 if( sqliteSafetyOn(db) ) goto exec_misuse;
705*c5c4113dSnw141292 if( !db->init.busy ){
706*c5c4113dSnw141292 if( (db->flags & SQLITE_Initialized)==0 ){
707*c5c4113dSnw141292 int rc, cnt = 1;
708*c5c4113dSnw141292 while( (rc = sqliteInit(db, pzErrMsg))==SQLITE_BUSY
709*c5c4113dSnw141292 && db->xBusyCallback
710*c5c4113dSnw141292 && db->xBusyCallback(db->pBusyArg, "", cnt++)!=0 ){}
711*c5c4113dSnw141292 if( rc!=SQLITE_OK ){
712*c5c4113dSnw141292 sqliteStrRealloc(pzErrMsg);
713*c5c4113dSnw141292 sqliteSafetyOff(db);
714*c5c4113dSnw141292 return rc;
715*c5c4113dSnw141292 }
716*c5c4113dSnw141292 if( pzErrMsg ){
717*c5c4113dSnw141292 sqliteFree(*pzErrMsg);
718*c5c4113dSnw141292 *pzErrMsg = 0;
719*c5c4113dSnw141292 }
720*c5c4113dSnw141292 }
721*c5c4113dSnw141292 if( db->file_format<3 ){
722*c5c4113dSnw141292 sqliteSafetyOff(db);
723*c5c4113dSnw141292 sqliteSetString(pzErrMsg, "obsolete database file format", (char*)0);
724*c5c4113dSnw141292 return SQLITE_ERROR;
725*c5c4113dSnw141292 }
726*c5c4113dSnw141292 }
727*c5c4113dSnw141292 assert( (db->flags & SQLITE_Initialized)!=0 || db->init.busy );
728*c5c4113dSnw141292 if( db->pVdbe==0 ){ db->nChange = 0; }
729*c5c4113dSnw141292 memset(&sParse, 0, sizeof(sParse));
730*c5c4113dSnw141292 sParse.db = db;
731*c5c4113dSnw141292 sqliteRunParser(&sParse, zSql, pzErrMsg);
732*c5c4113dSnw141292 if( db->xTrace && !db->init.busy ){
733*c5c4113dSnw141292 /* Trace only the statment that was compiled.
734*c5c4113dSnw141292 ** Make a copy of that part of the SQL string since zSQL is const
735*c5c4113dSnw141292 ** and we must pass a zero terminated string to the trace function
736*c5c4113dSnw141292 ** The copy is unnecessary if the tail pointer is pointing at the
737*c5c4113dSnw141292 ** beginnig or end of the SQL string.
738*c5c4113dSnw141292 */
739*c5c4113dSnw141292 if( sParse.zTail && sParse.zTail!=zSql && *sParse.zTail ){
740*c5c4113dSnw141292 char *tmpSql = sqliteStrNDup(zSql, sParse.zTail - zSql);
741*c5c4113dSnw141292 if( tmpSql ){
742*c5c4113dSnw141292 db->xTrace(db->pTraceArg, tmpSql);
743*c5c4113dSnw141292 free(tmpSql);
744*c5c4113dSnw141292 }else{
745*c5c4113dSnw141292 /* If a memory error occurred during the copy,
746*c5c4113dSnw141292 ** trace entire SQL string and fall through to the
747*c5c4113dSnw141292 ** sqlite_malloc_failed test to report the error.
748*c5c4113dSnw141292 */
749*c5c4113dSnw141292 db->xTrace(db->pTraceArg, zSql);
750*c5c4113dSnw141292 }
751*c5c4113dSnw141292 }else{
752*c5c4113dSnw141292 db->xTrace(db->pTraceArg, zSql);
753*c5c4113dSnw141292 }
754*c5c4113dSnw141292 }
755*c5c4113dSnw141292 if( sqlite_malloc_failed ){
756*c5c4113dSnw141292 sqliteSetString(pzErrMsg, "out of memory", (char*)0);
757*c5c4113dSnw141292 sParse.rc = SQLITE_NOMEM;
758*c5c4113dSnw141292 sqliteRollbackAll(db);
759*c5c4113dSnw141292 sqliteResetInternalSchema(db, 0);
760*c5c4113dSnw141292 db->flags &= ~SQLITE_InTrans;
761*c5c4113dSnw141292 }
762*c5c4113dSnw141292 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
763*c5c4113dSnw141292 if( sParse.rc!=SQLITE_OK && pzErrMsg && *pzErrMsg==0 ){
764*c5c4113dSnw141292 sqliteSetString(pzErrMsg, sqlite_error_string(sParse.rc), (char*)0);
765*c5c4113dSnw141292 }
766*c5c4113dSnw141292 sqliteStrRealloc(pzErrMsg);
767*c5c4113dSnw141292 if( sParse.rc==SQLITE_SCHEMA ){
768*c5c4113dSnw141292 sqliteResetInternalSchema(db, 0);
769*c5c4113dSnw141292 }
770*c5c4113dSnw141292 assert( ppVm );
771*c5c4113dSnw141292 *ppVm = (sqlite_vm*)sParse.pVdbe;
772*c5c4113dSnw141292 if( pzTail ) *pzTail = sParse.zTail;
773*c5c4113dSnw141292 if( sqliteSafetyOff(db) ) goto exec_misuse;
774*c5c4113dSnw141292 return sParse.rc;
775*c5c4113dSnw141292
776*c5c4113dSnw141292 exec_misuse:
777*c5c4113dSnw141292 if( pzErrMsg ){
778*c5c4113dSnw141292 *pzErrMsg = 0;
779*c5c4113dSnw141292 sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), (char*)0);
780*c5c4113dSnw141292 sqliteStrRealloc(pzErrMsg);
781*c5c4113dSnw141292 }
782*c5c4113dSnw141292 return SQLITE_MISUSE;
783*c5c4113dSnw141292 }
784*c5c4113dSnw141292
785*c5c4113dSnw141292
786*c5c4113dSnw141292 /*
787*c5c4113dSnw141292 ** The following routine destroys a virtual machine that is created by
788*c5c4113dSnw141292 ** the sqlite_compile() routine.
789*c5c4113dSnw141292 **
790*c5c4113dSnw141292 ** The integer returned is an SQLITE_ success/failure code that describes
791*c5c4113dSnw141292 ** the result of executing the virtual machine. An error message is
792*c5c4113dSnw141292 ** written into memory obtained from malloc and *pzErrMsg is made to
793*c5c4113dSnw141292 ** point to that error if pzErrMsg is not NULL. The calling routine
794*c5c4113dSnw141292 ** should use sqlite_freemem() to delete the message when it has finished
795*c5c4113dSnw141292 ** with it.
796*c5c4113dSnw141292 */
sqlite_finalize(sqlite_vm * pVm,char ** pzErrMsg)797*c5c4113dSnw141292 int sqlite_finalize(
798*c5c4113dSnw141292 sqlite_vm *pVm, /* The virtual machine to be destroyed */
799*c5c4113dSnw141292 char **pzErrMsg /* OUT: Write error messages here */
800*c5c4113dSnw141292 ){
801*c5c4113dSnw141292 int rc = sqliteVdbeFinalize((Vdbe*)pVm, pzErrMsg);
802*c5c4113dSnw141292 sqliteStrRealloc(pzErrMsg);
803*c5c4113dSnw141292 return rc;
804*c5c4113dSnw141292 }
805*c5c4113dSnw141292
806*c5c4113dSnw141292 /*
807*c5c4113dSnw141292 ** Terminate the current execution of a virtual machine then
808*c5c4113dSnw141292 ** reset the virtual machine back to its starting state so that it
809*c5c4113dSnw141292 ** can be reused. Any error message resulting from the prior execution
810*c5c4113dSnw141292 ** is written into *pzErrMsg. A success code from the prior execution
811*c5c4113dSnw141292 ** is returned.
812*c5c4113dSnw141292 */
sqlite_reset(sqlite_vm * pVm,char ** pzErrMsg)813*c5c4113dSnw141292 int sqlite_reset(
814*c5c4113dSnw141292 sqlite_vm *pVm, /* The virtual machine to be destroyed */
815*c5c4113dSnw141292 char **pzErrMsg /* OUT: Write error messages here */
816*c5c4113dSnw141292 ){
817*c5c4113dSnw141292 int rc = sqliteVdbeReset((Vdbe*)pVm, pzErrMsg);
818*c5c4113dSnw141292 sqliteVdbeMakeReady((Vdbe*)pVm, -1, 0);
819*c5c4113dSnw141292 sqliteStrRealloc(pzErrMsg);
820*c5c4113dSnw141292 return rc;
821*c5c4113dSnw141292 }
822*c5c4113dSnw141292
823*c5c4113dSnw141292 /*
824*c5c4113dSnw141292 ** Return a static string that describes the kind of error specified in the
825*c5c4113dSnw141292 ** argument.
826*c5c4113dSnw141292 */
sqlite_error_string(int rc)827*c5c4113dSnw141292 const char *sqlite_error_string(int rc){
828*c5c4113dSnw141292 const char *z;
829*c5c4113dSnw141292 switch( rc ){
830*c5c4113dSnw141292 case SQLITE_OK: z = "not an error"; break;
831*c5c4113dSnw141292 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
832*c5c4113dSnw141292 case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break;
833*c5c4113dSnw141292 case SQLITE_PERM: z = "access permission denied"; break;
834*c5c4113dSnw141292 case SQLITE_ABORT: z = "callback requested query abort"; break;
835*c5c4113dSnw141292 case SQLITE_BUSY: z = "database is locked"; break;
836*c5c4113dSnw141292 case SQLITE_LOCKED: z = "database table is locked"; break;
837*c5c4113dSnw141292 case SQLITE_NOMEM: z = "out of memory"; break;
838*c5c4113dSnw141292 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
839*c5c4113dSnw141292 case SQLITE_INTERRUPT: z = "interrupted"; break;
840*c5c4113dSnw141292 case SQLITE_IOERR: z = "disk I/O error"; break;
841*c5c4113dSnw141292 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
842*c5c4113dSnw141292 case SQLITE_NOTFOUND: z = "table or record not found"; break;
843*c5c4113dSnw141292 case SQLITE_FULL: z = "database is full"; break;
844*c5c4113dSnw141292 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
845*c5c4113dSnw141292 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
846*c5c4113dSnw141292 case SQLITE_EMPTY: z = "table contains no data"; break;
847*c5c4113dSnw141292 case SQLITE_SCHEMA: z = "database schema has changed"; break;
848*c5c4113dSnw141292 case SQLITE_TOOBIG: z = "too much data for one table row"; break;
849*c5c4113dSnw141292 case SQLITE_CONSTRAINT: z = "constraint failed"; break;
850*c5c4113dSnw141292 case SQLITE_MISMATCH: z = "datatype mismatch"; break;
851*c5c4113dSnw141292 case SQLITE_MISUSE: z = "library routine called out of sequence";break;
852*c5c4113dSnw141292 case SQLITE_NOLFS: z = "kernel lacks large file support"; break;
853*c5c4113dSnw141292 case SQLITE_AUTH: z = "authorization denied"; break;
854*c5c4113dSnw141292 case SQLITE_FORMAT: z = "auxiliary database format error"; break;
855*c5c4113dSnw141292 case SQLITE_RANGE: z = "bind index out of range"; break;
856*c5c4113dSnw141292 case SQLITE_NOTADB: z = "file is encrypted or is not a database";break;
857*c5c4113dSnw141292 default: z = "unknown error"; break;
858*c5c4113dSnw141292 }
859*c5c4113dSnw141292 return z;
860*c5c4113dSnw141292 }
861*c5c4113dSnw141292
862*c5c4113dSnw141292 /*
863*c5c4113dSnw141292 ** This routine implements a busy callback that sleeps and tries
864*c5c4113dSnw141292 ** again until a timeout value is reached. The timeout value is
865*c5c4113dSnw141292 ** an integer number of milliseconds passed in as the first
866*c5c4113dSnw141292 ** argument.
867*c5c4113dSnw141292 */
sqliteDefaultBusyCallback(void * Timeout,const char * NotUsed,int count)868*c5c4113dSnw141292 static int sqliteDefaultBusyCallback(
869*c5c4113dSnw141292 void *Timeout, /* Maximum amount of time to wait */
870*c5c4113dSnw141292 const char *NotUsed, /* The name of the table that is busy */
871*c5c4113dSnw141292 int count /* Number of times table has been busy */
872*c5c4113dSnw141292 ){
873*c5c4113dSnw141292 #if SQLITE_MIN_SLEEP_MS==1
874*c5c4113dSnw141292 static const char delays[] =
875*c5c4113dSnw141292 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 50, 100};
876*c5c4113dSnw141292 static const short int totals[] =
877*c5c4113dSnw141292 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228, 287};
878*c5c4113dSnw141292 # define NDELAY (sizeof(delays)/sizeof(delays[0]))
879*c5c4113dSnw141292 int timeout = (int)(long)Timeout;
880*c5c4113dSnw141292 int delay, prior;
881*c5c4113dSnw141292
882*c5c4113dSnw141292 if( count <= NDELAY ){
883*c5c4113dSnw141292 delay = delays[count-1];
884*c5c4113dSnw141292 prior = totals[count-1];
885*c5c4113dSnw141292 }else{
886*c5c4113dSnw141292 delay = delays[NDELAY-1];
887*c5c4113dSnw141292 prior = totals[NDELAY-1] + delay*(count-NDELAY-1);
888*c5c4113dSnw141292 }
889*c5c4113dSnw141292 if( prior + delay > timeout ){
890*c5c4113dSnw141292 delay = timeout - prior;
891*c5c4113dSnw141292 if( delay<=0 ) return 0;
892*c5c4113dSnw141292 }
893*c5c4113dSnw141292 sqliteOsSleep(delay);
894*c5c4113dSnw141292 return 1;
895*c5c4113dSnw141292 #else
896*c5c4113dSnw141292 int timeout = (int)(long)Timeout;
897*c5c4113dSnw141292 if( (count+1)*1000 > timeout ){
898*c5c4113dSnw141292 return 0;
899*c5c4113dSnw141292 }
900*c5c4113dSnw141292 sqliteOsSleep(1000);
901*c5c4113dSnw141292 return 1;
902*c5c4113dSnw141292 #endif
903*c5c4113dSnw141292 }
904*c5c4113dSnw141292
905*c5c4113dSnw141292 /*
906*c5c4113dSnw141292 ** This routine sets the busy callback for an Sqlite database to the
907*c5c4113dSnw141292 ** given callback function with the given argument.
908*c5c4113dSnw141292 */
sqlite_busy_handler(sqlite * db,int (* xBusy)(void *,const char *,int),void * pArg)909*c5c4113dSnw141292 void sqlite_busy_handler(
910*c5c4113dSnw141292 sqlite *db,
911*c5c4113dSnw141292 int (*xBusy)(void*,const char*,int),
912*c5c4113dSnw141292 void *pArg
913*c5c4113dSnw141292 ){
914*c5c4113dSnw141292 db->xBusyCallback = xBusy;
915*c5c4113dSnw141292 db->pBusyArg = pArg;
916*c5c4113dSnw141292 }
917*c5c4113dSnw141292
918*c5c4113dSnw141292 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
919*c5c4113dSnw141292 /*
920*c5c4113dSnw141292 ** This routine sets the progress callback for an Sqlite database to the
921*c5c4113dSnw141292 ** given callback function with the given argument. The progress callback will
922*c5c4113dSnw141292 ** be invoked every nOps opcodes.
923*c5c4113dSnw141292 */
sqlite_progress_handler(sqlite * db,int nOps,int (* xProgress)(void *),void * pArg)924*c5c4113dSnw141292 void sqlite_progress_handler(
925*c5c4113dSnw141292 sqlite *db,
926*c5c4113dSnw141292 int nOps,
927*c5c4113dSnw141292 int (*xProgress)(void*),
928*c5c4113dSnw141292 void *pArg
929*c5c4113dSnw141292 ){
930*c5c4113dSnw141292 if( nOps>0 ){
931*c5c4113dSnw141292 db->xProgress = xProgress;
932*c5c4113dSnw141292 db->nProgressOps = nOps;
933*c5c4113dSnw141292 db->pProgressArg = pArg;
934*c5c4113dSnw141292 }else{
935*c5c4113dSnw141292 db->xProgress = 0;
936*c5c4113dSnw141292 db->nProgressOps = 0;
937*c5c4113dSnw141292 db->pProgressArg = 0;
938*c5c4113dSnw141292 }
939*c5c4113dSnw141292 }
940*c5c4113dSnw141292 #endif
941*c5c4113dSnw141292
942*c5c4113dSnw141292
943*c5c4113dSnw141292 /*
944*c5c4113dSnw141292 ** This routine installs a default busy handler that waits for the
945*c5c4113dSnw141292 ** specified number of milliseconds before returning 0.
946*c5c4113dSnw141292 */
sqlite_busy_timeout(sqlite * db,int ms)947*c5c4113dSnw141292 void sqlite_busy_timeout(sqlite *db, int ms){
948*c5c4113dSnw141292 if( ms>0 ){
949*c5c4113dSnw141292 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)(long)ms);
950*c5c4113dSnw141292 }else{
951*c5c4113dSnw141292 sqlite_busy_handler(db, 0, 0);
952*c5c4113dSnw141292 }
953*c5c4113dSnw141292 }
954*c5c4113dSnw141292
955*c5c4113dSnw141292 /*
956*c5c4113dSnw141292 ** Cause any pending operation to stop at its earliest opportunity.
957*c5c4113dSnw141292 */
sqlite_interrupt(sqlite * db)958*c5c4113dSnw141292 void sqlite_interrupt(sqlite *db){
959*c5c4113dSnw141292 db->flags |= SQLITE_Interrupt;
960*c5c4113dSnw141292 }
961*c5c4113dSnw141292
962*c5c4113dSnw141292 /*
963*c5c4113dSnw141292 ** Windows systems should call this routine to free memory that
964*c5c4113dSnw141292 ** is returned in the in the errmsg parameter of sqlite_open() when
965*c5c4113dSnw141292 ** SQLite is a DLL. For some reason, it does not work to call free()
966*c5c4113dSnw141292 ** directly.
967*c5c4113dSnw141292 **
968*c5c4113dSnw141292 ** Note that we need to call free() not sqliteFree() here, since every
969*c5c4113dSnw141292 ** string that is exported from SQLite should have already passed through
970*c5c4113dSnw141292 ** sqliteStrRealloc().
971*c5c4113dSnw141292 */
sqlite_freemem(void * p)972*c5c4113dSnw141292 void sqlite_freemem(void *p){ free(p); }
973*c5c4113dSnw141292
974*c5c4113dSnw141292 /*
975*c5c4113dSnw141292 ** Windows systems need functions to call to return the sqlite_version
976*c5c4113dSnw141292 ** and sqlite_encoding strings since they are unable to access constants
977*c5c4113dSnw141292 ** within DLLs.
978*c5c4113dSnw141292 */
sqlite_libversion(void)979*c5c4113dSnw141292 const char *sqlite_libversion(void){ return sqlite_version; }
sqlite_libencoding(void)980*c5c4113dSnw141292 const char *sqlite_libencoding(void){ return sqlite_encoding; }
981*c5c4113dSnw141292
982*c5c4113dSnw141292 /*
983*c5c4113dSnw141292 ** Create new user-defined functions. The sqlite_create_function()
984*c5c4113dSnw141292 ** routine creates a regular function and sqlite_create_aggregate()
985*c5c4113dSnw141292 ** creates an aggregate function.
986*c5c4113dSnw141292 **
987*c5c4113dSnw141292 ** Passing a NULL xFunc argument or NULL xStep and xFinalize arguments
988*c5c4113dSnw141292 ** disables the function. Calling sqlite_create_function() with the
989*c5c4113dSnw141292 ** same name and number of arguments as a prior call to
990*c5c4113dSnw141292 ** sqlite_create_aggregate() disables the prior call to
991*c5c4113dSnw141292 ** sqlite_create_aggregate(), and vice versa.
992*c5c4113dSnw141292 **
993*c5c4113dSnw141292 ** If nArg is -1 it means that this function will accept any number
994*c5c4113dSnw141292 ** of arguments, including 0. The maximum allowed value of nArg is 127.
995*c5c4113dSnw141292 */
sqlite_create_function(sqlite * db,const char * zName,int nArg,void (* xFunc)(sqlite_func *,int,const char **),void * pUserData)996*c5c4113dSnw141292 int sqlite_create_function(
997*c5c4113dSnw141292 sqlite *db, /* Add the function to this database connection */
998*c5c4113dSnw141292 const char *zName, /* Name of the function to add */
999*c5c4113dSnw141292 int nArg, /* Number of arguments */
1000*c5c4113dSnw141292 void (*xFunc)(sqlite_func*,int,const char**), /* The implementation */
1001*c5c4113dSnw141292 void *pUserData /* User data */
1002*c5c4113dSnw141292 ){
1003*c5c4113dSnw141292 FuncDef *p;
1004*c5c4113dSnw141292 int nName;
1005*c5c4113dSnw141292 if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1;
1006*c5c4113dSnw141292 if( nArg<-1 || nArg>127 ) return 1;
1007*c5c4113dSnw141292 nName = strlen(zName);
1008*c5c4113dSnw141292 if( nName>255 ) return 1;
1009*c5c4113dSnw141292 p = sqliteFindFunction(db, zName, nName, nArg, 1);
1010*c5c4113dSnw141292 if( p==0 ) return 1;
1011*c5c4113dSnw141292 p->xFunc = xFunc;
1012*c5c4113dSnw141292 p->xStep = 0;
1013*c5c4113dSnw141292 p->xFinalize = 0;
1014*c5c4113dSnw141292 p->pUserData = pUserData;
1015*c5c4113dSnw141292 return 0;
1016*c5c4113dSnw141292 }
sqlite_create_aggregate(sqlite * db,const char * zName,int nArg,void (* xStep)(sqlite_func *,int,const char **),void (* xFinalize)(sqlite_func *),void * pUserData)1017*c5c4113dSnw141292 int sqlite_create_aggregate(
1018*c5c4113dSnw141292 sqlite *db, /* Add the function to this database connection */
1019*c5c4113dSnw141292 const char *zName, /* Name of the function to add */
1020*c5c4113dSnw141292 int nArg, /* Number of arguments */
1021*c5c4113dSnw141292 void (*xStep)(sqlite_func*,int,const char**), /* The step function */
1022*c5c4113dSnw141292 void (*xFinalize)(sqlite_func*), /* The finalizer */
1023*c5c4113dSnw141292 void *pUserData /* User data */
1024*c5c4113dSnw141292 ){
1025*c5c4113dSnw141292 FuncDef *p;
1026*c5c4113dSnw141292 int nName;
1027*c5c4113dSnw141292 if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1;
1028*c5c4113dSnw141292 if( nArg<-1 || nArg>127 ) return 1;
1029*c5c4113dSnw141292 nName = strlen(zName);
1030*c5c4113dSnw141292 if( nName>255 ) return 1;
1031*c5c4113dSnw141292 p = sqliteFindFunction(db, zName, nName, nArg, 1);
1032*c5c4113dSnw141292 if( p==0 ) return 1;
1033*c5c4113dSnw141292 p->xFunc = 0;
1034*c5c4113dSnw141292 p->xStep = xStep;
1035*c5c4113dSnw141292 p->xFinalize = xFinalize;
1036*c5c4113dSnw141292 p->pUserData = pUserData;
1037*c5c4113dSnw141292 return 0;
1038*c5c4113dSnw141292 }
1039*c5c4113dSnw141292
1040*c5c4113dSnw141292 /*
1041*c5c4113dSnw141292 ** Change the datatype for all functions with a given name. See the
1042*c5c4113dSnw141292 ** header comment for the prototype of this function in sqlite.h for
1043*c5c4113dSnw141292 ** additional information.
1044*c5c4113dSnw141292 */
sqlite_function_type(sqlite * db,const char * zName,int dataType)1045*c5c4113dSnw141292 int sqlite_function_type(sqlite *db, const char *zName, int dataType){
1046*c5c4113dSnw141292 FuncDef *p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, strlen(zName));
1047*c5c4113dSnw141292 while( p ){
1048*c5c4113dSnw141292 p->dataType = dataType;
1049*c5c4113dSnw141292 p = p->pNext;
1050*c5c4113dSnw141292 }
1051*c5c4113dSnw141292 return SQLITE_OK;
1052*c5c4113dSnw141292 }
1053*c5c4113dSnw141292
1054*c5c4113dSnw141292 /*
1055*c5c4113dSnw141292 ** Register a trace function. The pArg from the previously registered trace
1056*c5c4113dSnw141292 ** is returned.
1057*c5c4113dSnw141292 **
1058*c5c4113dSnw141292 ** A NULL trace function means that no tracing is executes. A non-NULL
1059*c5c4113dSnw141292 ** trace is a pointer to a function that is invoked at the start of each
1060*c5c4113dSnw141292 ** sqlite_exec().
1061*c5c4113dSnw141292 */
sqlite_trace(sqlite * db,void (* xTrace)(void *,const char *),void * pArg)1062*c5c4113dSnw141292 void *sqlite_trace(sqlite *db, void (*xTrace)(void*,const char*), void *pArg){
1063*c5c4113dSnw141292 void *pOld = db->pTraceArg;
1064*c5c4113dSnw141292 db->xTrace = xTrace;
1065*c5c4113dSnw141292 db->pTraceArg = pArg;
1066*c5c4113dSnw141292 return pOld;
1067*c5c4113dSnw141292 }
1068*c5c4113dSnw141292
1069*c5c4113dSnw141292 /*** EXPERIMENTAL ***
1070*c5c4113dSnw141292 **
1071*c5c4113dSnw141292 ** Register a function to be invoked when a transaction comments.
1072*c5c4113dSnw141292 ** If either function returns non-zero, then the commit becomes a
1073*c5c4113dSnw141292 ** rollback.
1074*c5c4113dSnw141292 */
sqlite_commit_hook(sqlite * db,int (* xCallback)(void *),void * pArg)1075*c5c4113dSnw141292 void *sqlite_commit_hook(
1076*c5c4113dSnw141292 sqlite *db, /* Attach the hook to this database */
1077*c5c4113dSnw141292 int (*xCallback)(void*), /* Function to invoke on each commit */
1078*c5c4113dSnw141292 void *pArg /* Argument to the function */
1079*c5c4113dSnw141292 ){
1080*c5c4113dSnw141292 void *pOld = db->pCommitArg;
1081*c5c4113dSnw141292 db->xCommitCallback = xCallback;
1082*c5c4113dSnw141292 db->pCommitArg = pArg;
1083*c5c4113dSnw141292 return pOld;
1084*c5c4113dSnw141292 }
1085*c5c4113dSnw141292
1086*c5c4113dSnw141292
1087*c5c4113dSnw141292 /*
1088*c5c4113dSnw141292 ** This routine is called to create a connection to a database BTree
1089*c5c4113dSnw141292 ** driver. If zFilename is the name of a file, then that file is
1090*c5c4113dSnw141292 ** opened and used. If zFilename is the magic name ":memory:" then
1091*c5c4113dSnw141292 ** the database is stored in memory (and is thus forgotten as soon as
1092*c5c4113dSnw141292 ** the connection is closed.) If zFilename is NULL then the database
1093*c5c4113dSnw141292 ** is for temporary use only and is deleted as soon as the connection
1094*c5c4113dSnw141292 ** is closed.
1095*c5c4113dSnw141292 **
1096*c5c4113dSnw141292 ** A temporary database can be either a disk file (that is automatically
1097*c5c4113dSnw141292 ** deleted when the file is closed) or a set of red-black trees held in memory,
1098*c5c4113dSnw141292 ** depending on the values of the TEMP_STORE compile-time macro and the
1099*c5c4113dSnw141292 ** db->temp_store variable, according to the following chart:
1100*c5c4113dSnw141292 **
1101*c5c4113dSnw141292 ** TEMP_STORE db->temp_store Location of temporary database
1102*c5c4113dSnw141292 ** ---------- -------------- ------------------------------
1103*c5c4113dSnw141292 ** 0 any file
1104*c5c4113dSnw141292 ** 1 1 file
1105*c5c4113dSnw141292 ** 1 2 memory
1106*c5c4113dSnw141292 ** 1 0 file
1107*c5c4113dSnw141292 ** 2 1 file
1108*c5c4113dSnw141292 ** 2 2 memory
1109*c5c4113dSnw141292 ** 2 0 memory
1110*c5c4113dSnw141292 ** 3 any memory
1111*c5c4113dSnw141292 */
sqliteBtreeFactory(const sqlite * db,const char * zFilename,int omitJournal,int nCache,Btree ** ppBtree)1112*c5c4113dSnw141292 int sqliteBtreeFactory(
1113*c5c4113dSnw141292 const sqlite *db, /* Main database when opening aux otherwise 0 */
1114*c5c4113dSnw141292 const char *zFilename, /* Name of the file containing the BTree database */
1115*c5c4113dSnw141292 int omitJournal, /* if TRUE then do not journal this file */
1116*c5c4113dSnw141292 int nCache, /* How many pages in the page cache */
1117*c5c4113dSnw141292 Btree **ppBtree){ /* Pointer to new Btree object written here */
1118*c5c4113dSnw141292
1119*c5c4113dSnw141292 assert( ppBtree != 0);
1120*c5c4113dSnw141292
1121*c5c4113dSnw141292 #ifndef SQLITE_OMIT_INMEMORYDB
1122*c5c4113dSnw141292 if( zFilename==0 ){
1123*c5c4113dSnw141292 if (TEMP_STORE == 0) {
1124*c5c4113dSnw141292 /* Always use file based temporary DB */
1125*c5c4113dSnw141292 return sqliteBtreeOpen(0, omitJournal, nCache, ppBtree);
1126*c5c4113dSnw141292 } else if (TEMP_STORE == 1 || TEMP_STORE == 2) {
1127*c5c4113dSnw141292 /* Switch depending on compile-time and/or runtime settings. */
1128*c5c4113dSnw141292 int location = db->temp_store==0 ? TEMP_STORE : db->temp_store;
1129*c5c4113dSnw141292
1130*c5c4113dSnw141292 if (location == 1) {
1131*c5c4113dSnw141292 return sqliteBtreeOpen(zFilename, omitJournal, nCache, ppBtree);
1132*c5c4113dSnw141292 } else {
1133*c5c4113dSnw141292 return sqliteRbtreeOpen(0, 0, 0, ppBtree);
1134*c5c4113dSnw141292 }
1135*c5c4113dSnw141292 } else {
1136*c5c4113dSnw141292 /* Always use in-core DB */
1137*c5c4113dSnw141292 return sqliteRbtreeOpen(0, 0, 0, ppBtree);
1138*c5c4113dSnw141292 }
1139*c5c4113dSnw141292 }else if( zFilename[0]==':' && strcmp(zFilename,":memory:")==0 ){
1140*c5c4113dSnw141292 return sqliteRbtreeOpen(0, 0, 0, ppBtree);
1141*c5c4113dSnw141292 }else
1142*c5c4113dSnw141292 #endif
1143*c5c4113dSnw141292 {
1144*c5c4113dSnw141292 return sqliteBtreeOpen(zFilename, omitJournal, nCache, ppBtree);
1145*c5c4113dSnw141292 }
1146*c5c4113dSnw141292 }
1147