blob: 9087907723b90651e023c3efeef867e2b8966d73 [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**
drhbeae3192001-09-22 18:12:08 +000017** $Id: main.c,v 1.40 2001/09/22 18:12:10 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
drhd78eeee2001-09-13 16:18:53 +000024** database. Each callback contains the following information:
drh28037572000-08-02 13:47:41 +000025**
drhd78eeee2001-09-13 16:18:53 +000026** argv[0] = "meta" or "table" or "index"
drhe3c41372001-09-17 20:25:58 +000027** argv[1] = table or index name or meta statement type.
28** argv[2] = root page number for table or index. NULL for meta.
29** argv[3] = root page number of primary key for tables or NULL.
30** argv[4] = SQL create statement for the table or index
drhd78eeee2001-09-13 16:18:53 +000031**
drh75897232000-05-29 14:26:00 +000032*/
33static int sqliteOpenCb(void *pDb, int argc, char **argv, char **azColName){
34 sqlite *db = (sqlite*)pDb;
35 Parse sParse;
drhd78eeee2001-09-13 16:18:53 +000036 int nErr = 0;
drh75897232000-05-29 14:26:00 +000037
drhe3c41372001-09-17 20:25:58 +000038/* TODO: Do some validity checks on all fields. In particular,
39** make sure fields do not contain NULLs. */
40
41 assert( argc==5 );
drhd78eeee2001-09-13 16:18:53 +000042 switch( argv[0][0] ){
43 case 'm': { /* Meta information */
drh50e5dad2001-09-15 00:57:28 +000044 if( strcmp(argv[1],"file-format")==0 ){
drhe3c41372001-09-17 20:25:58 +000045 db->file_format = atoi(argv[4]);
drh50e5dad2001-09-15 00:57:28 +000046 }else if( strcmp(argv[1],"schema-cookie")==0 ){
drhe3c41372001-09-17 20:25:58 +000047 db->schema_cookie = atoi(argv[4]);
drh50e5dad2001-09-15 00:57:28 +000048 db->next_cookie = db->schema_cookie;
49 }
drhd78eeee2001-09-13 16:18:53 +000050 break;
drh28037572000-08-02 13:47:41 +000051 }
drhd78eeee2001-09-13 16:18:53 +000052 case 'i':
53 case 't': { /* CREATE TABLE and CREATE INDEX statements */
54 memset(&sParse, 0, sizeof(sParse));
55 sParse.db = db;
56 sParse.initFlag = 1;
57 sParse.newTnum = atoi(argv[2]);
drhe3c41372001-09-17 20:25:58 +000058 sParse.newKnum = argv[3] ? atoi(argv[3]) : 0;
59 nErr = sqliteRunParser(&sParse, argv[4], 0);
drhd78eeee2001-09-13 16:18:53 +000060 break;
61 }
62 default: {
63 /* This can not happen! */
64 nErr = 1;
65 assert( nErr==0 );
66 }
drh28037572000-08-02 13:47:41 +000067 }
drh75897232000-05-29 14:26:00 +000068 return nErr;
69}
70
71/*
drh58b95762000-06-02 01:17:37 +000072** Attempt to read the database schema and initialize internal
73** data structures. Return one of the SQLITE_ error codes to
74** indicate success or failure.
drhbed86902000-06-02 13:27:59 +000075**
76** After the database is initialized, the SQLITE_Initialized
77** bit is set in the flags field of the sqlite structure. An
78** attempt is made to initialize the database as soon as it
79** is opened. If that fails (perhaps because another process
80** has the sqlite_master table locked) than another attempt
81** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +000082*/
drh58b95762000-06-02 01:17:37 +000083static int sqliteInit(sqlite *db, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +000084 Vdbe *vdbe;
drh58b95762000-06-02 01:17:37 +000085 int rc;
86
87 /*
88 ** The master database table has a structure like this
89 */
drh75897232000-05-29 14:26:00 +000090 static char master_schema[] =
91 "CREATE TABLE " MASTER_NAME " (\n"
92 " type text,\n"
93 " name text,\n"
94 " tbl_name text,\n"
drhe3c41372001-09-17 20:25:58 +000095 " tnum integer,\n"
96 " knum integer,\n"
drh75897232000-05-29 14:26:00 +000097 " sql text\n"
98 ")"
99 ;
100
101 /* The following program is used to initialize the internal
102 ** structure holding the tables and indexes of the database.
103 ** The database contains a special table named "sqlite_master"
104 ** defined as follows:
105 **
106 ** CREATE TABLE sqlite_master (
drh28037572000-08-02 13:47:41 +0000107 ** type text, -- Either "table" or "index" or "meta"
drh75897232000-05-29 14:26:00 +0000108 ** name text, -- Name of table or index
109 ** tbl_name text, -- Associated table
drhe3c41372001-09-17 20:25:58 +0000110 ** tnum integer, -- The integer page number of root page
111 ** knum integer, -- Root page of primary key, or NULL
drh75897232000-05-29 14:26:00 +0000112 ** sql text -- The CREATE statement for this object
113 ** );
114 **
115 ** The sqlite_master table contains a single entry for each table
drh967e8b72000-06-21 13:59:10 +0000116 ** and each index. The "type" column tells whether the entry is
117 ** a table or index. The "name" column is the name of the object.
drh75897232000-05-29 14:26:00 +0000118 ** The "tbl_name" is the name of the associated table. For tables,
drh967e8b72000-06-21 13:59:10 +0000119 ** the tbl_name column is always the same as name. For indices, the
120 ** tbl_name column contains the name of the table that the index
121 ** indexes. Finally, the "sql" column contains the complete text of
drh75897232000-05-29 14:26:00 +0000122 ** the CREATE TABLE or CREATE INDEX statement that originally created
123 ** the table or index.
124 **
drh28037572000-08-02 13:47:41 +0000125 ** If the "type" column has the value "meta", then the "sql" column
126 ** contains extra information about the database, such as the
127 ** file format version number. All meta information must be processed
128 ** before any tables or indices are constructed.
129 **
drh75897232000-05-29 14:26:00 +0000130 ** The following program invokes its callback on the SQL for each
131 ** table then goes back and invokes the callback on the
132 ** SQL for each index. The callback will invoke the
133 ** parser to build the internal representation of the
134 ** database scheme.
135 */
136 static VdbeOp initProg[] = {
drh5e00f6c2001-09-13 13:46:56 +0000137 { OP_Open, 0, 2, 0},
drhd78eeee2001-09-13 16:18:53 +0000138 { OP_Rewind, 0, 0, 0},
drhe3c41372001-09-17 20:25:58 +0000139 { OP_Next, 0, 13, 0}, /* 2 */
drh5e00f6c2001-09-13 13:46:56 +0000140 { OP_Column, 0, 0, 0},
drh28037572000-08-02 13:47:41 +0000141 { OP_String, 0, 0, "meta"},
drhd78eeee2001-09-13 16:18:53 +0000142 { OP_Ne, 0, 2, 0},
drh5e00f6c2001-09-13 13:46:56 +0000143 { OP_Column, 0, 0, 0},
drhd78eeee2001-09-13 16:18:53 +0000144 { OP_Column, 0, 1, 0},
drhe3c41372001-09-17 20:25:58 +0000145 { OP_Column, 0, 3, 0},
146 { OP_Null, 0, 0, 0},
147 { OP_Column, 0, 5, 0},
148 { OP_Callback, 5, 0, 0},
drhd78eeee2001-09-13 16:18:53 +0000149 { OP_Goto, 0, 2, 0},
drhe3c41372001-09-17 20:25:58 +0000150 { OP_Rewind, 0, 0, 0}, /* 13 */
151 { OP_Next, 0, 25, 0}, /* 14 */
drh5e00f6c2001-09-13 13:46:56 +0000152 { OP_Column, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000153 { OP_String, 0, 0, "table"},
drhe3c41372001-09-17 20:25:58 +0000154 { OP_Ne, 0, 14, 0},
drhd78eeee2001-09-13 16:18:53 +0000155 { OP_Column, 0, 0, 0},
156 { OP_Column, 0, 1, 0},
drhe3c41372001-09-17 20:25:58 +0000157 { OP_Column, 0, 3, 0},
drh5e00f6c2001-09-13 13:46:56 +0000158 { OP_Column, 0, 4, 0},
drhe3c41372001-09-17 20:25:58 +0000159 { OP_Column, 0, 5, 0},
160 { OP_Callback, 5, 0, 0},
161 { OP_Goto, 0, 14, 0},
162 { OP_Rewind, 0, 0, 0}, /* 25 */
163 { OP_Next, 0, 37, 0}, /* 26 */
drh5e00f6c2001-09-13 13:46:56 +0000164 { OP_Column, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000165 { OP_String, 0, 0, "index"},
drhe3c41372001-09-17 20:25:58 +0000166 { OP_Ne, 0, 26, 0},
drhd78eeee2001-09-13 16:18:53 +0000167 { OP_Column, 0, 0, 0},
168 { OP_Column, 0, 1, 0},
drhe3c41372001-09-17 20:25:58 +0000169 { OP_Column, 0, 3, 0},
170 { OP_Null, 0, 0, 0},
171 { OP_Column, 0, 5, 0},
172 { OP_Callback, 5, 0, 0},
173 { OP_Goto, 0, 26, 0},
174 { OP_String, 0, 0, "meta"}, /* 37 */
drh50e5dad2001-09-15 00:57:28 +0000175 { OP_String, 0, 0, "schema-cookie"},
drhe3c41372001-09-17 20:25:58 +0000176 { OP_Null, 0, 0, 0},
177 { OP_Null, 0, 0, 0},
drh50e5dad2001-09-15 00:57:28 +0000178 { OP_ReadCookie,0,0, 0},
drhe3c41372001-09-17 20:25:58 +0000179 { OP_Callback, 5, 0, 0},
drh50e5dad2001-09-15 00:57:28 +0000180 { OP_Close, 0, 0, 0},
drh5e00f6c2001-09-13 13:46:56 +0000181 { OP_Halt, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000182 };
183
drh58b95762000-06-02 01:17:37 +0000184 /* Create a virtual machine to run the initialization program. Run
185 ** the program. The delete the virtual machine.
186 */
drh4c504392000-10-16 22:06:40 +0000187 vdbe = sqliteVdbeCreate(db);
drhd8bc7082000-06-07 23:51:50 +0000188 if( vdbe==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000189 sqliteSetString(pzErrMsg, "out of memory");
190 return SQLITE_NOMEM;
drhd8bc7082000-06-07 23:51:50 +0000191 }
drh58b95762000-06-02 01:17:37 +0000192 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
drh2dfbbca2000-07-28 14:32:48 +0000193 rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg,
194 db->pBusyArg, db->xBusyCallback);
drh58b95762000-06-02 01:17:37 +0000195 sqliteVdbeDelete(vdbe);
drhd78eeee2001-09-13 16:18:53 +0000196 if( rc==SQLITE_OK && db->file_format>1 && db->nTable>0 ){
197 sqliteSetString(pzErrMsg, "unsupported file format", 0);
drh28037572000-08-02 13:47:41 +0000198 rc = SQLITE_ERROR;
199 }
drh58b95762000-06-02 01:17:37 +0000200 if( rc==SQLITE_OK ){
201 Table *pTab;
drhe3c41372001-09-17 20:25:58 +0000202 char *azArg[6];
drhd78eeee2001-09-13 16:18:53 +0000203 azArg[0] = "table";
204 azArg[1] = MASTER_NAME;
205 azArg[2] = "2";
drhe3c41372001-09-17 20:25:58 +0000206 azArg[3] = 0;
207 azArg[4] = master_schema;
208 azArg[5] = 0;
209 sqliteOpenCb(db, 5, azArg, 0);
drh58b95762000-06-02 01:17:37 +0000210 pTab = sqliteFindTable(db, MASTER_NAME);
211 if( pTab ){
212 pTab->readOnly = 1;
213 }
214 db->flags |= SQLITE_Initialized;
drh5e00f6c2001-09-13 13:46:56 +0000215 sqliteCommitInternalChanges(db);
drh58b95762000-06-02 01:17:37 +0000216 }
217 return rc;
218}
219
220/*
drhb217a572000-08-22 13:40:18 +0000221** The version of the library
222*/
drh3d0b5592000-08-22 13:40:51 +0000223const char sqlite_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000224
225/*
drh297ecf12001-04-05 15:57:13 +0000226** Does the library expect data to be encoded as UTF-8 or iso8859? The
227** following global constant always lets us know.
228*/
229#ifdef SQLITE_UTF8
drhfbc3eab2001-04-06 16:13:42 +0000230const char sqlite_encoding[] = "UTF-8";
drh297ecf12001-04-05 15:57:13 +0000231#else
drhfbc3eab2001-04-06 16:13:42 +0000232const char sqlite_encoding[] = "iso8859";
drh297ecf12001-04-05 15:57:13 +0000233#endif
234
235/*
drh58b95762000-06-02 01:17:37 +0000236** Open a new SQLite database. Construct an "sqlite" structure to define
237** the state of this database and return a pointer to that structure.
238**
239** An attempt is made to initialize the in-memory data structures that
240** hold the database schema. But if this fails (because the schema file
241** is locked) then that step is deferred until the first call to
242** sqlite_exec().
243*/
244sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
245 sqlite *db;
246 int rc;
247
248 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000249 db = sqliteMalloc( sizeof(sqlite) );
250 if( pzErrMsg ) *pzErrMsg = 0;
drhdaffd0e2001-04-11 14:28:42 +0000251 if( db==0 ) goto no_mem_on_open;
drhbeae3192001-09-22 18:12:08 +0000252 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
253 sqliteHashInit(&db->idxHash, SQLITE_HASH_STRING, 0);
drh75897232000-05-29 14:26:00 +0000254
255 /* Open the backend database driver */
drha1b351a2001-09-14 16:42:12 +0000256 rc = sqliteBtreeOpen(zFilename, mode, MAX_PAGES, &db->pBe);
drh5e00f6c2001-09-13 13:46:56 +0000257 if( rc!=SQLITE_OK ){
258 switch( rc ){
259 default: {
260 if( pzErrMsg ){
261 sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0);
262 }
263 }
264 }
drh75897232000-05-29 14:26:00 +0000265 sqliteFree(db);
drh5edc3122001-09-13 21:53:09 +0000266 sqliteStrRealloc(pzErrMsg);
drhbe0072d2001-09-13 14:46:09 +0000267 return 0;
drh75897232000-05-29 14:26:00 +0000268 }
269
drh28037572000-08-02 13:47:41 +0000270 /* Assume file format 1 unless the database says otherwise */
271 db->file_format = 1;
272
drh58b95762000-06-02 01:17:37 +0000273 /* Attempt to read the schema */
274 rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000275 if( sqlite_malloc_failed ){
276 goto no_mem_on_open;
277 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
drh58b95762000-06-02 01:17:37 +0000278 sqlite_close(db);
drh5edc3122001-09-13 21:53:09 +0000279 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000280 return 0;
drh4c504392000-10-16 22:06:40 +0000281 }else /* if( pzErrMsg ) */{
drhdaffd0e2001-04-11 14:28:42 +0000282 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000283 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000284 }
drh75897232000-05-29 14:26:00 +0000285 return db;
drhdaffd0e2001-04-11 14:28:42 +0000286
287no_mem_on_open:
288 sqliteSetString(pzErrMsg, "out of memory", 0);
289 sqliteStrRealloc(pzErrMsg);
290 return 0;
drh75897232000-05-29 14:26:00 +0000291}
292
293/*
drh50e5dad2001-09-15 00:57:28 +0000294** Erase all schema information from the schema hash table.
295**
296** The database schema is normally read in once when the database
297** is first opened and stored in a hash table in the sqlite structure.
298** This routine erases the stored schema. This erasure occurs because
299** either the database is being closed or because some other process
300** changed the schema and this process needs to reread it.
drh75897232000-05-29 14:26:00 +0000301*/
drh50e5dad2001-09-15 00:57:28 +0000302static void clearHashTable(sqlite *db){
drhbeae3192001-09-22 18:12:08 +0000303 HashElem *pElem;
304 Hash temp1;
305 temp1 = db->tblHash;
306 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
307 sqliteHashClear(&db->idxHash);
308 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
309 Table *pTbl = sqliteHashData(pElem);
310 sqliteDeleteTable(db, pTbl);
drh75897232000-05-29 14:26:00 +0000311 }
drhbeae3192001-09-22 18:12:08 +0000312 sqliteHashClear(&temp1);
drh50e5dad2001-09-15 00:57:28 +0000313 db->flags &= ~SQLITE_Initialized;
314}
315
316/*
317** Close an existing SQLite database
318*/
319void sqlite_close(sqlite *db){
320 sqliteBtreeClose(db->pBe);
321 clearHashTable(db);
drh75897232000-05-29 14:26:00 +0000322 sqliteFree(db);
323}
324
325/*
326** Return TRUE if the given SQL string ends in a semicolon.
327*/
328int sqlite_complete(const char *zSql){
drh8c82b352000-12-10 18:23:50 +0000329 int isComplete = 0;
330 while( *zSql ){
331 switch( *zSql ){
332 case ';': {
333 isComplete = 1;
drh75897232000-05-29 14:26:00 +0000334 break;
drh8c82b352000-12-10 18:23:50 +0000335 }
336 case ' ':
337 case '\t':
338 case '\n':
339 case '\f': {
drh75897232000-05-29 14:26:00 +0000340 break;
drh8c82b352000-12-10 18:23:50 +0000341 }
342 case '\'': {
343 isComplete = 0;
344 zSql++;
345 while( *zSql && *zSql!='\'' ){ zSql++; }
346 if( *zSql==0 ) return 0;
drh75897232000-05-29 14:26:00 +0000347 break;
drh8c82b352000-12-10 18:23:50 +0000348 }
349 case '"': {
350 isComplete = 0;
351 zSql++;
352 while( *zSql && *zSql!='"' ){ zSql++; }
353 if( *zSql==0 ) return 0;
354 break;
355 }
356 case '-': {
357 if( zSql[1]!='-' ){
358 isComplete = 0;
359 break;
360 }
361 while( *zSql && *zSql!='\n' ){ zSql++; }
362 if( *zSql==0 ) return isComplete;
363 break;
364 }
365 default: {
366 isComplete = 0;
367 break;
368 }
drh75897232000-05-29 14:26:00 +0000369 }
drh8c82b352000-12-10 18:23:50 +0000370 zSql++;
drh75897232000-05-29 14:26:00 +0000371 }
drh8c82b352000-12-10 18:23:50 +0000372 return isComplete;
drh75897232000-05-29 14:26:00 +0000373}
374
375/*
drhbed86902000-06-02 13:27:59 +0000376** Execute SQL code. Return one of the SQLITE_ success/failure
377** codes. Also write an error message into memory obtained from
378** malloc() and make *pzErrMsg point to that message.
379**
380** If the SQL is a query, then for each row in the query result
381** the xCallback() function is called. pArg becomes the first
382** argument to xCallback(). If xCallback=NULL then no callback
383** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000384*/
385int sqlite_exec(
386 sqlite *db, /* The database on which the SQL executes */
387 char *zSql, /* The SQL to be executed */
388 sqlite_callback xCallback, /* Invoke this callback routine */
389 void *pArg, /* First argument to xCallback() */
390 char **pzErrMsg /* Write error messages here */
391){
392 Parse sParse;
drh75897232000-05-29 14:26:00 +0000393
394 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000395 if( (db->flags & SQLITE_Initialized)==0 ){
396 int rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000397 if( rc!=SQLITE_OK ){
398 sqliteStrRealloc(pzErrMsg);
399 return rc;
400 }
drh58b95762000-06-02 01:17:37 +0000401 }
drh75897232000-05-29 14:26:00 +0000402 memset(&sParse, 0, sizeof(sParse));
403 sParse.db = db;
drh5e00f6c2001-09-13 13:46:56 +0000404 sParse.pBe = db->pBe;
drh75897232000-05-29 14:26:00 +0000405 sParse.xCallback = xCallback;
406 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000407 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000408 if( sqlite_malloc_failed ){
409 sqliteSetString(pzErrMsg, "out of memory", 0);
410 sParse.rc = SQLITE_NOMEM;
411 }
412 sqliteStrRealloc(pzErrMsg);
drh50e5dad2001-09-15 00:57:28 +0000413 if( sParse.rc==SQLITE_SCHEMA ){
414 clearHashTable(db);
415 }
drh4c504392000-10-16 22:06:40 +0000416 return sParse.rc;
drh75897232000-05-29 14:26:00 +0000417}
drh2dfbbca2000-07-28 14:32:48 +0000418
419/*
420** This routine implements a busy callback that sleeps and tries
421** again until a timeout value is reached. The timeout value is
422** an integer number of milliseconds passed in as the first
423** argument.
424*/
drhdaffd0e2001-04-11 14:28:42 +0000425static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000426 void *Timeout, /* Maximum amount of time to wait */
427 const char *NotUsed, /* The name of the table that is busy */
428 int count /* Number of times table has been busy */
429){
drh8cfbf082001-09-19 13:22:39 +0000430#if SQLITE_MIN_SLEEP_MS==1
431 int delay = 10;
drh2dfbbca2000-07-28 14:32:48 +0000432 int prior_delay = 0;
433 int timeout = (int)Timeout;
434 int i;
435
436 for(i=1; i<count; i++){
437 prior_delay += delay;
438 delay = delay*2;
drh8cfbf082001-09-19 13:22:39 +0000439 if( delay>=1000 ){
440 delay = 1000;
441 prior_delay += 1000*(count - i - 1);
drh2dfbbca2000-07-28 14:32:48 +0000442 break;
443 }
444 }
445 if( prior_delay + delay > timeout*1000 ){
446 delay = timeout*1000 - prior_delay;
447 if( delay<=0 ) return 0;
448 }
drh8cfbf082001-09-19 13:22:39 +0000449 sqliteOsSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000450 return 1;
451#else
452 int timeout = (int)Timeout;
453 if( (count+1)*1000 > timeout ){
454 return 0;
455 }
drh8cfbf082001-09-19 13:22:39 +0000456 sqliteOsSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000457 return 1;
458#endif
459}
460
461/*
462** This routine sets the busy callback for an Sqlite database to the
463** given callback function with the given argument.
464*/
465void sqlite_busy_handler(
466 sqlite *db,
467 int (*xBusy)(void*,const char*,int),
468 void *pArg
469){
470 db->xBusyCallback = xBusy;
471 db->pBusyArg = pArg;
472}
473
474/*
475** This routine installs a default busy handler that waits for the
476** specified number of milliseconds before returning 0.
477*/
478void sqlite_busy_timeout(sqlite *db, int ms){
479 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000480 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000481 }else{
482 sqlite_busy_handler(db, 0, 0);
483 }
484}
drh4c504392000-10-16 22:06:40 +0000485
486/*
487** Cause any pending operation to stop at its earliest opportunity.
488*/
489void sqlite_interrupt(sqlite *db){
490 db->flags |= SQLITE_Interrupt;
491}