blob: 2301915f4356e4c19258bcd024388e481802faa9 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
2** Copyright (c) 1999, 2000 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
24** Main file for the SQLite library. The routines in this file
25** implement the programmer interface to the library. Routines in
26** other files are for internal use by SQLite and should not be
27** accessed by users of the library.
28**
drh5b2fd562001-09-13 15:21:31 +000029** $Id: main.c,v 1.32 2001/09/13 15:21:32 drh Exp $
drh75897232000-05-29 14:26:00 +000030*/
31#include "sqliteInt.h"
drh7e3b0a02001-04-28 16:52:40 +000032#if defined(HAVE_USLEEP) && HAVE_USLEEP
drh345fda32001-01-15 22:51:08 +000033#include <unistd.h>
drh7e3b0a02001-04-28 16:52:40 +000034#endif
drh75897232000-05-29 14:26:00 +000035
36/*
37** This is the callback routine for the code that initializes the
38** database. Each callback contains text of a CREATE TABLE or
39** CREATE INDEX statement that must be parsed to yield the internal
40** structures that describe the tables.
drh28037572000-08-02 13:47:41 +000041**
42** This callback is also called with argc==2 when there is meta
43** information in the sqlite_master file. The meta information is
44** contained in argv[1]. Typical meta information is the file format
45** version.
drh75897232000-05-29 14:26:00 +000046*/
47static int sqliteOpenCb(void *pDb, int argc, char **argv, char **azColName){
48 sqlite *db = (sqlite*)pDb;
49 Parse sParse;
50 int nErr;
drh75897232000-05-29 14:26:00 +000051
drh28037572000-08-02 13:47:41 +000052 if( argc==2 ){
53 if( sscanf(argv[1],"file format %d",&db->file_format)==1 ){
54 return 0;
55 }
56 /* Unknown meta information. Ignore it. */
57 return 0;
58 }
drh75897232000-05-29 14:26:00 +000059 if( argc!=1 ) return 0;
60 memset(&sParse, 0, sizeof(sParse));
61 sParse.db = db;
62 sParse.initFlag = 1;
drhd1dedb82000-06-05 02:07:04 +000063 nErr = sqliteRunParser(&sParse, argv[0], 0);
drh75897232000-05-29 14:26:00 +000064 return nErr;
65}
66
67/*
drh58b95762000-06-02 01:17:37 +000068** Attempt to read the database schema and initialize internal
69** data structures. Return one of the SQLITE_ error codes to
70** indicate success or failure.
drhbed86902000-06-02 13:27:59 +000071**
72** After the database is initialized, the SQLITE_Initialized
73** bit is set in the flags field of the sqlite structure. An
74** attempt is made to initialize the database as soon as it
75** is opened. If that fails (perhaps because another process
76** has the sqlite_master table locked) than another attempt
77** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +000078*/
drh58b95762000-06-02 01:17:37 +000079static int sqliteInit(sqlite *db, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +000080 Vdbe *vdbe;
drh58b95762000-06-02 01:17:37 +000081 int rc;
82
83 /*
84 ** The master database table has a structure like this
85 */
drh75897232000-05-29 14:26:00 +000086 static char master_schema[] =
87 "CREATE TABLE " MASTER_NAME " (\n"
88 " type text,\n"
89 " name text,\n"
drh5e00f6c2001-09-13 13:46:56 +000090 " tnum integer,\n"
drh75897232000-05-29 14:26:00 +000091 " tbl_name text,\n"
92 " sql text\n"
93 ")"
94 ;
95
96 /* The following program is used to initialize the internal
97 ** structure holding the tables and indexes of the database.
98 ** The database contains a special table named "sqlite_master"
99 ** defined as follows:
100 **
101 ** CREATE TABLE sqlite_master (
drh28037572000-08-02 13:47:41 +0000102 ** type text, -- Either "table" or "index" or "meta"
drh75897232000-05-29 14:26:00 +0000103 ** name text, -- Name of table or index
drh5e00f6c2001-09-13 13:46:56 +0000104 ** tnum integer, -- The integer page number of root page
drh75897232000-05-29 14:26:00 +0000105 ** tbl_name text, -- Associated table
106 ** sql text -- The CREATE statement for this object
107 ** );
108 **
109 ** The sqlite_master table contains a single entry for each table
drh967e8b72000-06-21 13:59:10 +0000110 ** and each index. The "type" column tells whether the entry is
111 ** a table or index. The "name" column is the name of the object.
drh75897232000-05-29 14:26:00 +0000112 ** The "tbl_name" is the name of the associated table. For tables,
drh967e8b72000-06-21 13:59:10 +0000113 ** the tbl_name column is always the same as name. For indices, the
114 ** tbl_name column contains the name of the table that the index
115 ** indexes. Finally, the "sql" column contains the complete text of
drh75897232000-05-29 14:26:00 +0000116 ** the CREATE TABLE or CREATE INDEX statement that originally created
117 ** the table or index.
118 **
drh28037572000-08-02 13:47:41 +0000119 ** If the "type" column has the value "meta", then the "sql" column
120 ** contains extra information about the database, such as the
121 ** file format version number. All meta information must be processed
122 ** before any tables or indices are constructed.
123 **
drh75897232000-05-29 14:26:00 +0000124 ** The following program invokes its callback on the SQL for each
125 ** table then goes back and invokes the callback on the
126 ** SQL for each index. The callback will invoke the
127 ** parser to build the internal representation of the
128 ** database scheme.
129 */
130 static VdbeOp initProg[] = {
drh5e00f6c2001-09-13 13:46:56 +0000131 { OP_Open, 0, 2, 0},
drh28037572000-08-02 13:47:41 +0000132 { OP_Next, 0, 9, 0}, /* 1 */
drh5e00f6c2001-09-13 13:46:56 +0000133 { OP_Column, 0, 0, 0},
drh28037572000-08-02 13:47:41 +0000134 { OP_String, 0, 0, "meta"},
135 { OP_Ne, 0, 1, 0},
drh5e00f6c2001-09-13 13:46:56 +0000136 { OP_Column, 0, 0, 0},
137 { OP_Column, 0, 4, 0},
drh28037572000-08-02 13:47:41 +0000138 { OP_Callback, 2, 0, 0},
139 { OP_Goto, 0, 1, 0},
140 { OP_Rewind, 0, 0, 0}, /* 9 */
141 { OP_Next, 0, 17, 0}, /* 10 */
drh5e00f6c2001-09-13 13:46:56 +0000142 { OP_Column, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000143 { OP_String, 0, 0, "table"},
drh28037572000-08-02 13:47:41 +0000144 { OP_Ne, 0, 10, 0},
drh5e00f6c2001-09-13 13:46:56 +0000145 { OP_Column, 0, 4, 0},
drh75897232000-05-29 14:26:00 +0000146 { OP_Callback, 1, 0, 0},
drh28037572000-08-02 13:47:41 +0000147 { OP_Goto, 0, 10, 0},
148 { OP_Rewind, 0, 0, 0}, /* 17 */
149 { OP_Next, 0, 25, 0}, /* 18 */
drh5e00f6c2001-09-13 13:46:56 +0000150 { OP_Column, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000151 { OP_String, 0, 0, "index"},
drh28037572000-08-02 13:47:41 +0000152 { OP_Ne, 0, 18, 0},
drh5e00f6c2001-09-13 13:46:56 +0000153 { OP_Column, 0, 4, 0},
drh75897232000-05-29 14:26:00 +0000154 { OP_Callback, 1, 0, 0},
drh28037572000-08-02 13:47:41 +0000155 { OP_Goto, 0, 18, 0},
drh5e00f6c2001-09-13 13:46:56 +0000156 { OP_Close, 2, 0, 0}, /* 25 */
157 { OP_Halt, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000158 };
159
drh58b95762000-06-02 01:17:37 +0000160 /* Create a virtual machine to run the initialization program. Run
161 ** the program. The delete the virtual machine.
162 */
drh4c504392000-10-16 22:06:40 +0000163 vdbe = sqliteVdbeCreate(db);
drhd8bc7082000-06-07 23:51:50 +0000164 if( vdbe==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000165 sqliteSetString(pzErrMsg, "out of memory");
166 return SQLITE_NOMEM;
drhd8bc7082000-06-07 23:51:50 +0000167 }
drh58b95762000-06-02 01:17:37 +0000168 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
drh2dfbbca2000-07-28 14:32:48 +0000169 rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg,
170 db->pBusyArg, db->xBusyCallback);
drh58b95762000-06-02 01:17:37 +0000171 sqliteVdbeDelete(vdbe);
drh28037572000-08-02 13:47:41 +0000172 if( rc==SQLITE_OK && db->file_format<2 && db->nTable>0 ){
173 sqliteSetString(pzErrMsg, "obsolete file format", 0);
174 rc = SQLITE_ERROR;
175 }
drh58b95762000-06-02 01:17:37 +0000176 if( rc==SQLITE_OK ){
177 Table *pTab;
178 char *azArg[2];
179 azArg[0] = master_schema;
180 azArg[1] = 0;
181 sqliteOpenCb(db, 1, azArg, 0);
182 pTab = sqliteFindTable(db, MASTER_NAME);
183 if( pTab ){
184 pTab->readOnly = 1;
drh5b2fd562001-09-13 15:21:31 +0000185 pTab->tnum = 2;
drh58b95762000-06-02 01:17:37 +0000186 }
187 db->flags |= SQLITE_Initialized;
drh5e00f6c2001-09-13 13:46:56 +0000188 sqliteCommitInternalChanges(db);
drh58b95762000-06-02 01:17:37 +0000189 }
190 return rc;
191}
192
193/*
drhb217a572000-08-22 13:40:18 +0000194** The version of the library
195*/
drh3d0b5592000-08-22 13:40:51 +0000196const char sqlite_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000197
198/*
drh297ecf12001-04-05 15:57:13 +0000199** Does the library expect data to be encoded as UTF-8 or iso8859? The
200** following global constant always lets us know.
201*/
202#ifdef SQLITE_UTF8
drhfbc3eab2001-04-06 16:13:42 +0000203const char sqlite_encoding[] = "UTF-8";
drh297ecf12001-04-05 15:57:13 +0000204#else
drhfbc3eab2001-04-06 16:13:42 +0000205const char sqlite_encoding[] = "iso8859";
drh297ecf12001-04-05 15:57:13 +0000206#endif
207
208/*
drh58b95762000-06-02 01:17:37 +0000209** Open a new SQLite database. Construct an "sqlite" structure to define
210** the state of this database and return a pointer to that structure.
211**
212** An attempt is made to initialize the in-memory data structures that
213** hold the database schema. But if this fails (because the schema file
214** is locked) then that step is deferred until the first call to
215** sqlite_exec().
216*/
217sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
218 sqlite *db;
219 int rc;
220
221 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000222 db = sqliteMalloc( sizeof(sqlite) );
223 if( pzErrMsg ) *pzErrMsg = 0;
drhdaffd0e2001-04-11 14:28:42 +0000224 if( db==0 ) goto no_mem_on_open;
drh75897232000-05-29 14:26:00 +0000225
226 /* Open the backend database driver */
drh5e00f6c2001-09-13 13:46:56 +0000227 rc = sqliteBtreeOpen(zFilename, mode, 100, &db->pBe);
228 if( rc!=SQLITE_OK ){
229 switch( rc ){
230 default: {
231 if( pzErrMsg ){
232 sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0);
233 }
234 }
235 }
drh75897232000-05-29 14:26:00 +0000236 sqliteFree(db);
drhbe0072d2001-09-13 14:46:09 +0000237 return 0;
drh75897232000-05-29 14:26:00 +0000238 }
239
drh28037572000-08-02 13:47:41 +0000240 /* Assume file format 1 unless the database says otherwise */
241 db->file_format = 1;
242
drh58b95762000-06-02 01:17:37 +0000243 /* Attempt to read the schema */
244 rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000245 if( sqlite_malloc_failed ){
246 goto no_mem_on_open;
247 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
drh58b95762000-06-02 01:17:37 +0000248 sqlite_close(db);
249 return 0;
drh4c504392000-10-16 22:06:40 +0000250 }else /* if( pzErrMsg ) */{
drhdaffd0e2001-04-11 14:28:42 +0000251 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000252 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000253 }
drh75897232000-05-29 14:26:00 +0000254 return db;
drhdaffd0e2001-04-11 14:28:42 +0000255
256no_mem_on_open:
257 sqliteSetString(pzErrMsg, "out of memory", 0);
258 sqliteStrRealloc(pzErrMsg);
259 return 0;
drh75897232000-05-29 14:26:00 +0000260}
261
262/*
263** Close an existing SQLite database
264*/
265void sqlite_close(sqlite *db){
266 int i;
drh5e00f6c2001-09-13 13:46:56 +0000267 sqliteBtreeClose(db->pBe);
drh75897232000-05-29 14:26:00 +0000268 for(i=0; i<N_HASH; i++){
269 Table *pNext, *pList = db->apTblHash[i];
270 db->apTblHash[i] = 0;
271 while( pList ){
272 pNext = pList->pHash;
273 pList->pHash = 0;
274 sqliteDeleteTable(db, pList);
275 pList = pNext;
276 }
277 }
278 sqliteFree(db);
279}
280
281/*
282** Return TRUE if the given SQL string ends in a semicolon.
283*/
284int sqlite_complete(const char *zSql){
drh8c82b352000-12-10 18:23:50 +0000285 int isComplete = 0;
286 while( *zSql ){
287 switch( *zSql ){
288 case ';': {
289 isComplete = 1;
drh75897232000-05-29 14:26:00 +0000290 break;
drh8c82b352000-12-10 18:23:50 +0000291 }
292 case ' ':
293 case '\t':
294 case '\n':
295 case '\f': {
drh75897232000-05-29 14:26:00 +0000296 break;
drh8c82b352000-12-10 18:23:50 +0000297 }
298 case '\'': {
299 isComplete = 0;
300 zSql++;
301 while( *zSql && *zSql!='\'' ){ zSql++; }
302 if( *zSql==0 ) return 0;
drh75897232000-05-29 14:26:00 +0000303 break;
drh8c82b352000-12-10 18:23:50 +0000304 }
305 case '"': {
306 isComplete = 0;
307 zSql++;
308 while( *zSql && *zSql!='"' ){ zSql++; }
309 if( *zSql==0 ) return 0;
310 break;
311 }
312 case '-': {
313 if( zSql[1]!='-' ){
314 isComplete = 0;
315 break;
316 }
317 while( *zSql && *zSql!='\n' ){ zSql++; }
318 if( *zSql==0 ) return isComplete;
319 break;
320 }
321 default: {
322 isComplete = 0;
323 break;
324 }
drh75897232000-05-29 14:26:00 +0000325 }
drh8c82b352000-12-10 18:23:50 +0000326 zSql++;
drh75897232000-05-29 14:26:00 +0000327 }
drh8c82b352000-12-10 18:23:50 +0000328 return isComplete;
drh75897232000-05-29 14:26:00 +0000329}
330
331/*
drhbed86902000-06-02 13:27:59 +0000332** Execute SQL code. Return one of the SQLITE_ success/failure
333** codes. Also write an error message into memory obtained from
334** malloc() and make *pzErrMsg point to that message.
335**
336** If the SQL is a query, then for each row in the query result
337** the xCallback() function is called. pArg becomes the first
338** argument to xCallback(). If xCallback=NULL then no callback
339** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000340*/
341int sqlite_exec(
342 sqlite *db, /* The database on which the SQL executes */
343 char *zSql, /* The SQL to be executed */
344 sqlite_callback xCallback, /* Invoke this callback routine */
345 void *pArg, /* First argument to xCallback() */
346 char **pzErrMsg /* Write error messages here */
347){
348 Parse sParse;
drh75897232000-05-29 14:26:00 +0000349
350 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000351 if( (db->flags & SQLITE_Initialized)==0 ){
352 int rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000353 if( rc!=SQLITE_OK ){
354 sqliteStrRealloc(pzErrMsg);
355 return rc;
356 }
drh58b95762000-06-02 01:17:37 +0000357 }
drh75897232000-05-29 14:26:00 +0000358 memset(&sParse, 0, sizeof(sParse));
359 sParse.db = db;
drh5e00f6c2001-09-13 13:46:56 +0000360 sParse.pBe = db->pBe;
drh75897232000-05-29 14:26:00 +0000361 sParse.xCallback = xCallback;
362 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000363 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000364 if( sqlite_malloc_failed ){
365 sqliteSetString(pzErrMsg, "out of memory", 0);
366 sParse.rc = SQLITE_NOMEM;
367 }
368 sqliteStrRealloc(pzErrMsg);
drh4c504392000-10-16 22:06:40 +0000369 return sParse.rc;
drh75897232000-05-29 14:26:00 +0000370}
drh2dfbbca2000-07-28 14:32:48 +0000371
372/*
373** This routine implements a busy callback that sleeps and tries
374** again until a timeout value is reached. The timeout value is
375** an integer number of milliseconds passed in as the first
376** argument.
377*/
drhdaffd0e2001-04-11 14:28:42 +0000378static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000379 void *Timeout, /* Maximum amount of time to wait */
380 const char *NotUsed, /* The name of the table that is busy */
381 int count /* Number of times table has been busy */
382){
drh82ad3832000-07-31 13:38:24 +0000383#if defined(HAVE_USLEEP) && HAVE_USLEEP
drh2dfbbca2000-07-28 14:32:48 +0000384 int delay = 10000;
385 int prior_delay = 0;
386 int timeout = (int)Timeout;
387 int i;
388
389 for(i=1; i<count; i++){
390 prior_delay += delay;
391 delay = delay*2;
392 if( delay>=1000000 ){
393 delay = 1000000;
394 prior_delay += 1000000*(count - i - 1);
395 break;
396 }
397 }
398 if( prior_delay + delay > timeout*1000 ){
399 delay = timeout*1000 - prior_delay;
400 if( delay<=0 ) return 0;
401 }
402 usleep(delay);
403 return 1;
404#else
405 int timeout = (int)Timeout;
406 if( (count+1)*1000 > timeout ){
407 return 0;
408 }
409 sleep(1);
410 return 1;
411#endif
412}
413
414/*
415** This routine sets the busy callback for an Sqlite database to the
416** given callback function with the given argument.
417*/
418void sqlite_busy_handler(
419 sqlite *db,
420 int (*xBusy)(void*,const char*,int),
421 void *pArg
422){
423 db->xBusyCallback = xBusy;
424 db->pBusyArg = pArg;
425}
426
427/*
428** This routine installs a default busy handler that waits for the
429** specified number of milliseconds before returning 0.
430*/
431void sqlite_busy_timeout(sqlite *db, int ms){
432 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000433 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000434 }else{
435 sqlite_busy_handler(db, 0, 0);
436 }
437}
drh4c504392000-10-16 22:06:40 +0000438
439/*
440** Cause any pending operation to stop at its earliest opportunity.
441*/
442void sqlite_interrupt(sqlite *db){
443 db->flags |= SQLITE_Interrupt;
444}