blob: 184c49ff70bfa2a93c87ef4aa861d724d8a204e9 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** Main file for the SQLite library. The routines in this file
13** implement the programmer interface to the library. Routines in
14** other files are for internal use by SQLite and should not be
15** accessed by users of the library.
16**
drhf57b3392001-10-08 13:22:32 +000017** $Id: main.c,v 1.44 2001/10/08 13:22:33 drh Exp $
drh75897232000-05-29 14:26:00 +000018*/
19#include "sqliteInt.h"
drh8cfbf082001-09-19 13:22:39 +000020#include "os.h"
drh75897232000-05-29 14:26:00 +000021
22/*
23** This is the callback routine for the code that initializes the
drh382c0242001-10-06 16:33:02 +000024** database. See sqliteInit() below for additional information.
25**
26** Each callback contains the following information:
drh28037572000-08-02 13:47:41 +000027**
drhd78eeee2001-09-13 16:18:53 +000028** argv[0] = "meta" or "table" or "index"
drhe3c41372001-09-17 20:25:58 +000029** argv[1] = table or index name or meta statement type.
30** argv[2] = root page number for table or index. NULL for meta.
drhadbca9c2001-09-27 15:11:53 +000031** argv[3] = SQL create statement for the table or index
drhd78eeee2001-09-13 16:18:53 +000032**
drh75897232000-05-29 14:26:00 +000033*/
34static int sqliteOpenCb(void *pDb, int argc, char **argv, char **azColName){
35 sqlite *db = (sqlite*)pDb;
36 Parse sParse;
drhd78eeee2001-09-13 16:18:53 +000037 int nErr = 0;
drh75897232000-05-29 14:26:00 +000038
drh382c0242001-10-06 16:33:02 +000039 /* TODO: Do some validity checks on all fields. In particular,
40 ** make sure fields do not contain NULLs. Otherwise we might core
41 ** when attempting to initialize from a corrupt database file. */
drhe3c41372001-09-17 20:25:58 +000042
drhadbca9c2001-09-27 15:11:53 +000043 assert( argc==4 );
drhd78eeee2001-09-13 16:18:53 +000044 switch( argv[0][0] ){
45 case 'm': { /* Meta information */
drh50e5dad2001-09-15 00:57:28 +000046 if( strcmp(argv[1],"file-format")==0 ){
drhadbca9c2001-09-27 15:11:53 +000047 db->file_format = atoi(argv[3]);
drh50e5dad2001-09-15 00:57:28 +000048 }else if( strcmp(argv[1],"schema-cookie")==0 ){
drhadbca9c2001-09-27 15:11:53 +000049 db->schema_cookie = atoi(argv[3]);
drh50e5dad2001-09-15 00:57:28 +000050 db->next_cookie = db->schema_cookie;
51 }
drhd78eeee2001-09-13 16:18:53 +000052 break;
drh28037572000-08-02 13:47:41 +000053 }
drhd78eeee2001-09-13 16:18:53 +000054 case 'i':
55 case 't': { /* CREATE TABLE and CREATE INDEX statements */
drhadbca9c2001-09-27 15:11:53 +000056 if( argv[3] && argv[3][0] ){
drh382c0242001-10-06 16:33:02 +000057 /* Call the parser to process a CREATE TABLE or CREATE INDEX statement.
58 ** But because sParse.initFlag is set to 1, no VDBE code is generated
59 ** or executed. All the parser does is build the internal data
60 ** structures that describe the table or index.
61 */
drhadbca9c2001-09-27 15:11:53 +000062 memset(&sParse, 0, sizeof(sParse));
63 sParse.db = db;
64 sParse.initFlag = 1;
65 sParse.newTnum = atoi(argv[2]);
66 nErr = sqliteRunParser(&sParse, argv[3], 0);
67 }else{
drh382c0242001-10-06 16:33:02 +000068 /* If the SQL column is blank it means this is an index that
69 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
70 ** constraint or a CREATE TABLE. The index should have already
71 ** been created when we processed the CREATE TABLE. All we have
72 ** to do here is record the root page.
73 */
drhadbca9c2001-09-27 15:11:53 +000074 Index *pIndex = sqliteFindIndex(db, argv[1]);
75 if( pIndex==0 || pIndex->tnum!=0 ){
76 nErr++;
77 }else{
78 pIndex->tnum = atoi(argv[2]);
79 }
80 }
drhd78eeee2001-09-13 16:18:53 +000081 break;
82 }
83 default: {
84 /* This can not happen! */
85 nErr = 1;
86 assert( nErr==0 );
87 }
drh28037572000-08-02 13:47:41 +000088 }
drh75897232000-05-29 14:26:00 +000089 return nErr;
90}
91
92/*
drh58b95762000-06-02 01:17:37 +000093** Attempt to read the database schema and initialize internal
94** data structures. Return one of the SQLITE_ error codes to
95** indicate success or failure.
drhbed86902000-06-02 13:27:59 +000096**
97** After the database is initialized, the SQLITE_Initialized
98** bit is set in the flags field of the sqlite structure. An
99** attempt is made to initialize the database as soon as it
100** is opened. If that fails (perhaps because another process
101** has the sqlite_master table locked) than another attempt
102** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +0000103*/
drh58b95762000-06-02 01:17:37 +0000104static int sqliteInit(sqlite *db, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +0000105 Vdbe *vdbe;
drh58b95762000-06-02 01:17:37 +0000106 int rc;
107
108 /*
109 ** The master database table has a structure like this
110 */
drh75897232000-05-29 14:26:00 +0000111 static char master_schema[] =
112 "CREATE TABLE " MASTER_NAME " (\n"
113 " type text,\n"
114 " name text,\n"
115 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000116 " rootpage integer,\n"
drh75897232000-05-29 14:26:00 +0000117 " sql text\n"
118 ")"
119 ;
120
121 /* The following program is used to initialize the internal
122 ** structure holding the tables and indexes of the database.
123 ** The database contains a special table named "sqlite_master"
124 ** defined as follows:
125 **
126 ** CREATE TABLE sqlite_master (
drh28037572000-08-02 13:47:41 +0000127 ** type text, -- Either "table" or "index" or "meta"
drh75897232000-05-29 14:26:00 +0000128 ** name text, -- Name of table or index
129 ** tbl_name text, -- Associated table
drhadbca9c2001-09-27 15:11:53 +0000130 ** rootpage integer, -- The integer page number of root page
drh75897232000-05-29 14:26:00 +0000131 ** sql text -- The CREATE statement for this object
132 ** );
133 **
134 ** The sqlite_master table contains a single entry for each table
drh967e8b72000-06-21 13:59:10 +0000135 ** and each index. The "type" column tells whether the entry is
136 ** a table or index. The "name" column is the name of the object.
drh75897232000-05-29 14:26:00 +0000137 ** The "tbl_name" is the name of the associated table. For tables,
drh967e8b72000-06-21 13:59:10 +0000138 ** the tbl_name column is always the same as name. For indices, the
139 ** tbl_name column contains the name of the table that the index
drh382c0242001-10-06 16:33:02 +0000140 ** indexes. The "rootpage" column holds the number of the root page
141 ** for the b-tree for the table or index. Finally, the "sql" column
142 ** contains the complete text of the CREATE TABLE or CREATE INDEX
143 ** statement that originally created the table or index. If an index
144 ** was created to fulfill a PRIMARY KEY or UNIQUE constraint on a table,
145 ** then the "sql" column is NULL.
drh75897232000-05-29 14:26:00 +0000146 **
drh28037572000-08-02 13:47:41 +0000147 ** If the "type" column has the value "meta", then the "sql" column
148 ** contains extra information about the database, such as the
149 ** file format version number. All meta information must be processed
150 ** before any tables or indices are constructed.
151 **
drh75897232000-05-29 14:26:00 +0000152 ** The following program invokes its callback on the SQL for each
153 ** table then goes back and invokes the callback on the
154 ** SQL for each index. The callback will invoke the
155 ** parser to build the internal representation of the
156 ** database scheme.
157 */
158 static VdbeOp initProg[] = {
drh5e00f6c2001-09-13 13:46:56 +0000159 { OP_Open, 0, 2, 0},
drhd78eeee2001-09-13 16:18:53 +0000160 { OP_Rewind, 0, 0, 0},
drhadbca9c2001-09-27 15:11:53 +0000161 { OP_Next, 0, 12, 0}, /* 2 */
drh5e00f6c2001-09-13 13:46:56 +0000162 { OP_Column, 0, 0, 0},
drh28037572000-08-02 13:47:41 +0000163 { OP_String, 0, 0, "meta"},
drhd78eeee2001-09-13 16:18:53 +0000164 { OP_Ne, 0, 2, 0},
drh5e00f6c2001-09-13 13:46:56 +0000165 { OP_Column, 0, 0, 0},
drhd78eeee2001-09-13 16:18:53 +0000166 { OP_Column, 0, 1, 0},
drhe3c41372001-09-17 20:25:58 +0000167 { OP_Column, 0, 3, 0},
drhadbca9c2001-09-27 15:11:53 +0000168 { OP_Column, 0, 4, 0},
169 { OP_Callback, 4, 0, 0},
drhd78eeee2001-09-13 16:18:53 +0000170 { OP_Goto, 0, 2, 0},
drhadbca9c2001-09-27 15:11:53 +0000171 { OP_Rewind, 0, 0, 0}, /* 12 */
172 { OP_Next, 0, 23, 0}, /* 13 */
drh5e00f6c2001-09-13 13:46:56 +0000173 { OP_Column, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000174 { OP_String, 0, 0, "table"},
drhadbca9c2001-09-27 15:11:53 +0000175 { OP_Ne, 0, 13, 0},
drhd78eeee2001-09-13 16:18:53 +0000176 { OP_Column, 0, 0, 0},
177 { OP_Column, 0, 1, 0},
drhe3c41372001-09-17 20:25:58 +0000178 { OP_Column, 0, 3, 0},
drh5e00f6c2001-09-13 13:46:56 +0000179 { OP_Column, 0, 4, 0},
drhadbca9c2001-09-27 15:11:53 +0000180 { OP_Callback, 4, 0, 0},
181 { OP_Goto, 0, 13, 0},
182 { OP_Rewind, 0, 0, 0}, /* 23 */
183 { OP_Next, 0, 34, 0}, /* 24 */
drh5e00f6c2001-09-13 13:46:56 +0000184 { OP_Column, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000185 { OP_String, 0, 0, "index"},
drhadbca9c2001-09-27 15:11:53 +0000186 { OP_Ne, 0, 24, 0},
drhd78eeee2001-09-13 16:18:53 +0000187 { OP_Column, 0, 0, 0},
188 { OP_Column, 0, 1, 0},
drhe3c41372001-09-17 20:25:58 +0000189 { OP_Column, 0, 3, 0},
drhadbca9c2001-09-27 15:11:53 +0000190 { OP_Column, 0, 4, 0},
191 { OP_Callback, 4, 0, 0},
192 { OP_Goto, 0, 24, 0},
193 { OP_String, 0, 0, "meta"}, /* 34 */
drh50e5dad2001-09-15 00:57:28 +0000194 { OP_String, 0, 0, "schema-cookie"},
drh382c0242001-10-06 16:33:02 +0000195 { OP_String, 0, 0, 0},
drh50e5dad2001-09-15 00:57:28 +0000196 { OP_ReadCookie,0,0, 0},
drhadbca9c2001-09-27 15:11:53 +0000197 { OP_Callback, 4, 0, 0},
drh50e5dad2001-09-15 00:57:28 +0000198 { OP_Close, 0, 0, 0},
drh5e00f6c2001-09-13 13:46:56 +0000199 { OP_Halt, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000200 };
201
drh58b95762000-06-02 01:17:37 +0000202 /* Create a virtual machine to run the initialization program. Run
drh382c0242001-10-06 16:33:02 +0000203 ** the program. Then delete the virtual machine.
drh58b95762000-06-02 01:17:37 +0000204 */
drh4c504392000-10-16 22:06:40 +0000205 vdbe = sqliteVdbeCreate(db);
drhd8bc7082000-06-07 23:51:50 +0000206 if( vdbe==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000207 sqliteSetString(pzErrMsg, "out of memory");
208 return SQLITE_NOMEM;
drhd8bc7082000-06-07 23:51:50 +0000209 }
drh58b95762000-06-02 01:17:37 +0000210 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
drh2dfbbca2000-07-28 14:32:48 +0000211 rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg,
212 db->pBusyArg, db->xBusyCallback);
drh58b95762000-06-02 01:17:37 +0000213 sqliteVdbeDelete(vdbe);
drhd78eeee2001-09-13 16:18:53 +0000214 if( rc==SQLITE_OK && db->file_format>1 && db->nTable>0 ){
215 sqliteSetString(pzErrMsg, "unsupported file format", 0);
drh28037572000-08-02 13:47:41 +0000216 rc = SQLITE_ERROR;
217 }
drh58b95762000-06-02 01:17:37 +0000218 if( rc==SQLITE_OK ){
219 Table *pTab;
drhe3c41372001-09-17 20:25:58 +0000220 char *azArg[6];
drhd78eeee2001-09-13 16:18:53 +0000221 azArg[0] = "table";
222 azArg[1] = MASTER_NAME;
223 azArg[2] = "2";
drhadbca9c2001-09-27 15:11:53 +0000224 azArg[3] = master_schema;
225 azArg[4] = 0;
226 sqliteOpenCb(db, 4, azArg, 0);
drh58b95762000-06-02 01:17:37 +0000227 pTab = sqliteFindTable(db, MASTER_NAME);
228 if( pTab ){
229 pTab->readOnly = 1;
230 }
231 db->flags |= SQLITE_Initialized;
drh5e00f6c2001-09-13 13:46:56 +0000232 sqliteCommitInternalChanges(db);
drh58b95762000-06-02 01:17:37 +0000233 }
234 return rc;
235}
236
237/*
drhb217a572000-08-22 13:40:18 +0000238** The version of the library
239*/
drh3d0b5592000-08-22 13:40:51 +0000240const char sqlite_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000241
242/*
drh297ecf12001-04-05 15:57:13 +0000243** Does the library expect data to be encoded as UTF-8 or iso8859? The
244** following global constant always lets us know.
245*/
246#ifdef SQLITE_UTF8
drhfbc3eab2001-04-06 16:13:42 +0000247const char sqlite_encoding[] = "UTF-8";
drh297ecf12001-04-05 15:57:13 +0000248#else
drhfbc3eab2001-04-06 16:13:42 +0000249const char sqlite_encoding[] = "iso8859";
drh297ecf12001-04-05 15:57:13 +0000250#endif
251
252/*
drh58b95762000-06-02 01:17:37 +0000253** Open a new SQLite database. Construct an "sqlite" structure to define
254** the state of this database and return a pointer to that structure.
255**
256** An attempt is made to initialize the in-memory data structures that
257** hold the database schema. But if this fails (because the schema file
258** is locked) then that step is deferred until the first call to
259** sqlite_exec().
260*/
261sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
262 sqlite *db;
263 int rc;
264
265 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000266 db = sqliteMalloc( sizeof(sqlite) );
267 if( pzErrMsg ) *pzErrMsg = 0;
drhdaffd0e2001-04-11 14:28:42 +0000268 if( db==0 ) goto no_mem_on_open;
drhbeae3192001-09-22 18:12:08 +0000269 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
270 sqliteHashInit(&db->idxHash, SQLITE_HASH_STRING, 0);
drhf3256d22001-09-23 20:17:55 +0000271 db->nextRowid = sqliteRandomInteger(db);
drh75897232000-05-29 14:26:00 +0000272
273 /* Open the backend database driver */
drha1b351a2001-09-14 16:42:12 +0000274 rc = sqliteBtreeOpen(zFilename, mode, MAX_PAGES, &db->pBe);
drh5e00f6c2001-09-13 13:46:56 +0000275 if( rc!=SQLITE_OK ){
276 switch( rc ){
277 default: {
278 if( pzErrMsg ){
279 sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0);
280 }
281 }
282 }
drh75897232000-05-29 14:26:00 +0000283 sqliteFree(db);
drh5edc3122001-09-13 21:53:09 +0000284 sqliteStrRealloc(pzErrMsg);
drhbe0072d2001-09-13 14:46:09 +0000285 return 0;
drh75897232000-05-29 14:26:00 +0000286 }
287
drh28037572000-08-02 13:47:41 +0000288 /* Assume file format 1 unless the database says otherwise */
289 db->file_format = 1;
290
drh58b95762000-06-02 01:17:37 +0000291 /* Attempt to read the schema */
292 rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000293 if( sqlite_malloc_failed ){
294 goto no_mem_on_open;
295 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
drh58b95762000-06-02 01:17:37 +0000296 sqlite_close(db);
drh5edc3122001-09-13 21:53:09 +0000297 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000298 return 0;
drh4c504392000-10-16 22:06:40 +0000299 }else /* if( pzErrMsg ) */{
drhdaffd0e2001-04-11 14:28:42 +0000300 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000301 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000302 }
drh75897232000-05-29 14:26:00 +0000303 return db;
drhdaffd0e2001-04-11 14:28:42 +0000304
305no_mem_on_open:
306 sqliteSetString(pzErrMsg, "out of memory", 0);
307 sqliteStrRealloc(pzErrMsg);
308 return 0;
drh75897232000-05-29 14:26:00 +0000309}
310
311/*
drhf57b3392001-10-08 13:22:32 +0000312** Erase all schema information from the schema hash table. Except
313** tables that are created using CREATE TEMPORARY TABLE are preserved
314** if the preserverTemps flag is true.
drh50e5dad2001-09-15 00:57:28 +0000315**
316** The database schema is normally read in once when the database
317** is first opened and stored in a hash table in the sqlite structure.
318** This routine erases the stored schema. This erasure occurs because
319** either the database is being closed or because some other process
320** changed the schema and this process needs to reread it.
drh75897232000-05-29 14:26:00 +0000321*/
drhf57b3392001-10-08 13:22:32 +0000322static void clearHashTable(sqlite *db, int preserveTemps){
drhbeae3192001-09-22 18:12:08 +0000323 HashElem *pElem;
324 Hash temp1;
325 temp1 = db->tblHash;
326 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
327 sqliteHashClear(&db->idxHash);
328 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
drhf57b3392001-10-08 13:22:32 +0000329 Table *pTab = sqliteHashData(pElem);
330 if( preserveTemps && pTab->isTemp ){
331 Index *pIdx;
332 sqliteHashInsert(&db->tblHash, pTab->zName, strlen(pTab->zName)+1, pTab);
333 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
334 int n = strlen(pIdx->zName)+1;
335 sqliteHashInsert(&db->idxHash, pIdx->zName, n, pIdx);
336 }
337 }else{
338 sqliteDeleteTable(db, pTab);
339 }
drh75897232000-05-29 14:26:00 +0000340 }
drhbeae3192001-09-22 18:12:08 +0000341 sqliteHashClear(&temp1);
drh50e5dad2001-09-15 00:57:28 +0000342 db->flags &= ~SQLITE_Initialized;
343}
344
345/*
346** Close an existing SQLite database
347*/
348void sqlite_close(sqlite *db){
349 sqliteBtreeClose(db->pBe);
drhf57b3392001-10-08 13:22:32 +0000350 clearHashTable(db, 0);
351 if( db->pBeTemp ){
352 sqliteBtreeClose(db->pBeTemp);
353 }
drh75897232000-05-29 14:26:00 +0000354 sqliteFree(db);
355}
356
357/*
358** Return TRUE if the given SQL string ends in a semicolon.
359*/
360int sqlite_complete(const char *zSql){
drh8c82b352000-12-10 18:23:50 +0000361 int isComplete = 0;
362 while( *zSql ){
363 switch( *zSql ){
364 case ';': {
365 isComplete = 1;
drh75897232000-05-29 14:26:00 +0000366 break;
drh8c82b352000-12-10 18:23:50 +0000367 }
368 case ' ':
369 case '\t':
370 case '\n':
371 case '\f': {
drh75897232000-05-29 14:26:00 +0000372 break;
drh8c82b352000-12-10 18:23:50 +0000373 }
374 case '\'': {
375 isComplete = 0;
376 zSql++;
377 while( *zSql && *zSql!='\'' ){ zSql++; }
378 if( *zSql==0 ) return 0;
drh75897232000-05-29 14:26:00 +0000379 break;
drh8c82b352000-12-10 18:23:50 +0000380 }
381 case '"': {
382 isComplete = 0;
383 zSql++;
384 while( *zSql && *zSql!='"' ){ zSql++; }
385 if( *zSql==0 ) return 0;
386 break;
387 }
388 case '-': {
389 if( zSql[1]!='-' ){
390 isComplete = 0;
391 break;
392 }
393 while( *zSql && *zSql!='\n' ){ zSql++; }
394 if( *zSql==0 ) return isComplete;
395 break;
396 }
397 default: {
398 isComplete = 0;
399 break;
400 }
drh75897232000-05-29 14:26:00 +0000401 }
drh8c82b352000-12-10 18:23:50 +0000402 zSql++;
drh75897232000-05-29 14:26:00 +0000403 }
drh8c82b352000-12-10 18:23:50 +0000404 return isComplete;
drh75897232000-05-29 14:26:00 +0000405}
406
407/*
drhbed86902000-06-02 13:27:59 +0000408** Execute SQL code. Return one of the SQLITE_ success/failure
409** codes. Also write an error message into memory obtained from
410** malloc() and make *pzErrMsg point to that message.
411**
412** If the SQL is a query, then for each row in the query result
413** the xCallback() function is called. pArg becomes the first
414** argument to xCallback(). If xCallback=NULL then no callback
415** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000416*/
417int sqlite_exec(
418 sqlite *db, /* The database on which the SQL executes */
419 char *zSql, /* The SQL to be executed */
420 sqlite_callback xCallback, /* Invoke this callback routine */
421 void *pArg, /* First argument to xCallback() */
422 char **pzErrMsg /* Write error messages here */
423){
424 Parse sParse;
drh75897232000-05-29 14:26:00 +0000425
426 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000427 if( (db->flags & SQLITE_Initialized)==0 ){
428 int rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000429 if( rc!=SQLITE_OK ){
430 sqliteStrRealloc(pzErrMsg);
431 return rc;
432 }
drh58b95762000-06-02 01:17:37 +0000433 }
drh75897232000-05-29 14:26:00 +0000434 memset(&sParse, 0, sizeof(sParse));
435 sParse.db = db;
drh5e00f6c2001-09-13 13:46:56 +0000436 sParse.pBe = db->pBe;
drh75897232000-05-29 14:26:00 +0000437 sParse.xCallback = xCallback;
438 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000439 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000440 if( sqlite_malloc_failed ){
441 sqliteSetString(pzErrMsg, "out of memory", 0);
442 sParse.rc = SQLITE_NOMEM;
443 }
444 sqliteStrRealloc(pzErrMsg);
drh50e5dad2001-09-15 00:57:28 +0000445 if( sParse.rc==SQLITE_SCHEMA ){
drhf57b3392001-10-08 13:22:32 +0000446 clearHashTable(db, 1);
drh50e5dad2001-09-15 00:57:28 +0000447 }
drh4c504392000-10-16 22:06:40 +0000448 return sParse.rc;
drh75897232000-05-29 14:26:00 +0000449}
drh2dfbbca2000-07-28 14:32:48 +0000450
451/*
452** This routine implements a busy callback that sleeps and tries
453** again until a timeout value is reached. The timeout value is
454** an integer number of milliseconds passed in as the first
455** argument.
456*/
drhdaffd0e2001-04-11 14:28:42 +0000457static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000458 void *Timeout, /* Maximum amount of time to wait */
459 const char *NotUsed, /* The name of the table that is busy */
460 int count /* Number of times table has been busy */
461){
drh8cfbf082001-09-19 13:22:39 +0000462#if SQLITE_MIN_SLEEP_MS==1
463 int delay = 10;
drh2dfbbca2000-07-28 14:32:48 +0000464 int prior_delay = 0;
465 int timeout = (int)Timeout;
466 int i;
467
468 for(i=1; i<count; i++){
469 prior_delay += delay;
470 delay = delay*2;
drh8cfbf082001-09-19 13:22:39 +0000471 if( delay>=1000 ){
472 delay = 1000;
473 prior_delay += 1000*(count - i - 1);
drh2dfbbca2000-07-28 14:32:48 +0000474 break;
475 }
476 }
477 if( prior_delay + delay > timeout*1000 ){
478 delay = timeout*1000 - prior_delay;
479 if( delay<=0 ) return 0;
480 }
drh8cfbf082001-09-19 13:22:39 +0000481 sqliteOsSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000482 return 1;
483#else
484 int timeout = (int)Timeout;
485 if( (count+1)*1000 > timeout ){
486 return 0;
487 }
drh8cfbf082001-09-19 13:22:39 +0000488 sqliteOsSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000489 return 1;
490#endif
491}
492
493/*
494** This routine sets the busy callback for an Sqlite database to the
495** given callback function with the given argument.
496*/
497void sqlite_busy_handler(
498 sqlite *db,
499 int (*xBusy)(void*,const char*,int),
500 void *pArg
501){
502 db->xBusyCallback = xBusy;
503 db->pBusyArg = pArg;
504}
505
506/*
507** This routine installs a default busy handler that waits for the
508** specified number of milliseconds before returning 0.
509*/
510void sqlite_busy_timeout(sqlite *db, int ms){
511 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000512 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000513 }else{
514 sqlite_busy_handler(db, 0, 0);
515 }
516}
drh4c504392000-10-16 22:06:40 +0000517
518/*
519** Cause any pending operation to stop at its earliest opportunity.
520*/
521void sqlite_interrupt(sqlite *db){
522 db->flags |= SQLITE_Interrupt;
523}