blob: 8704149c992880896b1cc1a4f2c927e29cd9ae03 [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**
drhd78eeee2001-09-13 16:18:53 +000029** $Id: main.c,v 1.33 2001/09/13 16:18:54 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
drhd78eeee2001-09-13 16:18:53 +000038** database. Each callback contains the following information:
drh28037572000-08-02 13:47:41 +000039**
drhd78eeee2001-09-13 16:18:53 +000040** argv[0] = "meta" or "table" or "index"
41** argv[1] = table or index name
42** argv[2] = root page number for table or index
43** argv[3] = SQL create statement for the table or index
44**
drh75897232000-05-29 14:26:00 +000045*/
46static int sqliteOpenCb(void *pDb, int argc, char **argv, char **azColName){
47 sqlite *db = (sqlite*)pDb;
48 Parse sParse;
drhd78eeee2001-09-13 16:18:53 +000049 int nErr = 0;
drh75897232000-05-29 14:26:00 +000050
drhd78eeee2001-09-13 16:18:53 +000051 assert( argc==4 );
52 switch( argv[0][0] ){
53 case 'm': { /* Meta information */
54 sscanf(argv[1],"file format %d",&db->file_format);
55 break;
drh28037572000-08-02 13:47:41 +000056 }
drhd78eeee2001-09-13 16:18:53 +000057 case 'i':
58 case 't': { /* CREATE TABLE and CREATE INDEX statements */
59 memset(&sParse, 0, sizeof(sParse));
60 sParse.db = db;
61 sParse.initFlag = 1;
62 sParse.newTnum = atoi(argv[2]);
63 nErr = sqliteRunParser(&sParse, argv[3], 0);
64 break;
65 }
66 default: {
67 /* This can not happen! */
68 nErr = 1;
69 assert( nErr==0 );
70 }
drh28037572000-08-02 13:47:41 +000071 }
drh75897232000-05-29 14:26:00 +000072 return nErr;
73}
74
75/*
drh58b95762000-06-02 01:17:37 +000076** Attempt to read the database schema and initialize internal
77** data structures. Return one of the SQLITE_ error codes to
78** indicate success or failure.
drhbed86902000-06-02 13:27:59 +000079**
80** After the database is initialized, the SQLITE_Initialized
81** bit is set in the flags field of the sqlite structure. An
82** attempt is made to initialize the database as soon as it
83** is opened. If that fails (perhaps because another process
84** has the sqlite_master table locked) than another attempt
85** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +000086*/
drh58b95762000-06-02 01:17:37 +000087static int sqliteInit(sqlite *db, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +000088 Vdbe *vdbe;
drh58b95762000-06-02 01:17:37 +000089 int rc;
90
91 /*
92 ** The master database table has a structure like this
93 */
drh75897232000-05-29 14:26:00 +000094 static char master_schema[] =
95 "CREATE TABLE " MASTER_NAME " (\n"
96 " type text,\n"
97 " name text,\n"
drh5e00f6c2001-09-13 13:46:56 +000098 " tnum integer,\n"
drh75897232000-05-29 14:26:00 +000099 " tbl_name text,\n"
100 " sql text\n"
101 ")"
102 ;
103
104 /* The following program is used to initialize the internal
105 ** structure holding the tables and indexes of the database.
106 ** The database contains a special table named "sqlite_master"
107 ** defined as follows:
108 **
109 ** CREATE TABLE sqlite_master (
drh28037572000-08-02 13:47:41 +0000110 ** type text, -- Either "table" or "index" or "meta"
drh75897232000-05-29 14:26:00 +0000111 ** name text, -- Name of table or index
drh5e00f6c2001-09-13 13:46:56 +0000112 ** tnum integer, -- The integer page number of root page
drh75897232000-05-29 14:26:00 +0000113 ** tbl_name text, -- Associated table
114 ** sql text -- The CREATE statement for this object
115 ** );
116 **
117 ** The sqlite_master table contains a single entry for each table
drh967e8b72000-06-21 13:59:10 +0000118 ** and each index. The "type" column tells whether the entry is
119 ** a table or index. The "name" column is the name of the object.
drh75897232000-05-29 14:26:00 +0000120 ** The "tbl_name" is the name of the associated table. For tables,
drh967e8b72000-06-21 13:59:10 +0000121 ** the tbl_name column is always the same as name. For indices, the
122 ** tbl_name column contains the name of the table that the index
123 ** indexes. Finally, the "sql" column contains the complete text of
drh75897232000-05-29 14:26:00 +0000124 ** the CREATE TABLE or CREATE INDEX statement that originally created
125 ** the table or index.
126 **
drh28037572000-08-02 13:47:41 +0000127 ** If the "type" column has the value "meta", then the "sql" column
128 ** contains extra information about the database, such as the
129 ** file format version number. All meta information must be processed
130 ** before any tables or indices are constructed.
131 **
drh75897232000-05-29 14:26:00 +0000132 ** The following program invokes its callback on the SQL for each
133 ** table then goes back and invokes the callback on the
134 ** SQL for each index. The callback will invoke the
135 ** parser to build the internal representation of the
136 ** database scheme.
137 */
138 static VdbeOp initProg[] = {
drh5e00f6c2001-09-13 13:46:56 +0000139 { OP_Open, 0, 2, 0},
drhd78eeee2001-09-13 16:18:53 +0000140 { OP_Rewind, 0, 0, 0},
141 { OP_Next, 0, 12, 0}, /* 2 */
drh5e00f6c2001-09-13 13:46:56 +0000142 { OP_Column, 0, 0, 0},
drh28037572000-08-02 13:47:41 +0000143 { OP_String, 0, 0, "meta"},
drhd78eeee2001-09-13 16:18:53 +0000144 { OP_Ne, 0, 2, 0},
drh5e00f6c2001-09-13 13:46:56 +0000145 { OP_Column, 0, 0, 0},
drhd78eeee2001-09-13 16:18:53 +0000146 { OP_Column, 0, 1, 0},
147 { OP_Column, 0, 2, 0},
drh5e00f6c2001-09-13 13:46:56 +0000148 { OP_Column, 0, 4, 0},
drhd78eeee2001-09-13 16:18:53 +0000149 { OP_Callback, 4, 0, 0},
150 { OP_Goto, 0, 2, 0},
151 { OP_Rewind, 0, 0, 0}, /* 12 */
152 { OP_Next, 0, 23, 0}, /* 13 */
drh5e00f6c2001-09-13 13:46:56 +0000153 { OP_Column, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000154 { OP_String, 0, 0, "table"},
drhd78eeee2001-09-13 16:18:53 +0000155 { OP_Ne, 0, 13, 0},
156 { OP_Column, 0, 0, 0},
157 { OP_Column, 0, 1, 0},
158 { OP_Column, 0, 2, 0},
drh5e00f6c2001-09-13 13:46:56 +0000159 { OP_Column, 0, 4, 0},
drhd78eeee2001-09-13 16:18:53 +0000160 { OP_Callback, 4, 0, 0},
161 { OP_Goto, 0, 13, 0},
162 { OP_Rewind, 0, 0, 0}, /* 23 */
163 { OP_Next, 0, 34, 0}, /* 24 */
drh5e00f6c2001-09-13 13:46:56 +0000164 { OP_Column, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000165 { OP_String, 0, 0, "index"},
drhd78eeee2001-09-13 16:18:53 +0000166 { OP_Ne, 0, 24, 0},
167 { OP_Column, 0, 0, 0},
168 { OP_Column, 0, 1, 0},
169 { OP_Column, 0, 2, 0},
drh5e00f6c2001-09-13 13:46:56 +0000170 { OP_Column, 0, 4, 0},
drhd78eeee2001-09-13 16:18:53 +0000171 { OP_Callback, 4, 0, 0},
172 { OP_Goto, 0, 24, 0},
173 { OP_Close, 0, 0, 0}, /* 34 */
drh5e00f6c2001-09-13 13:46:56 +0000174 { OP_Halt, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000175 };
176
drh58b95762000-06-02 01:17:37 +0000177 /* Create a virtual machine to run the initialization program. Run
178 ** the program. The delete the virtual machine.
179 */
drh4c504392000-10-16 22:06:40 +0000180 vdbe = sqliteVdbeCreate(db);
drhd8bc7082000-06-07 23:51:50 +0000181 if( vdbe==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000182 sqliteSetString(pzErrMsg, "out of memory");
183 return SQLITE_NOMEM;
drhd8bc7082000-06-07 23:51:50 +0000184 }
drh58b95762000-06-02 01:17:37 +0000185 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
drh2dfbbca2000-07-28 14:32:48 +0000186 rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg,
187 db->pBusyArg, db->xBusyCallback);
drh58b95762000-06-02 01:17:37 +0000188 sqliteVdbeDelete(vdbe);
drhd78eeee2001-09-13 16:18:53 +0000189 if( rc==SQLITE_OK && db->file_format>1 && db->nTable>0 ){
190 sqliteSetString(pzErrMsg, "unsupported file format", 0);
drh28037572000-08-02 13:47:41 +0000191 rc = SQLITE_ERROR;
192 }
drh58b95762000-06-02 01:17:37 +0000193 if( rc==SQLITE_OK ){
194 Table *pTab;
drhd78eeee2001-09-13 16:18:53 +0000195 char *azArg[5];
196 azArg[0] = "table";
197 azArg[1] = MASTER_NAME;
198 azArg[2] = "2";
199 azArg[3] = master_schema;
200 azArg[4] = 0;
201 sqliteOpenCb(db, 4, azArg, 0);
drh58b95762000-06-02 01:17:37 +0000202 pTab = sqliteFindTable(db, MASTER_NAME);
203 if( pTab ){
204 pTab->readOnly = 1;
205 }
206 db->flags |= SQLITE_Initialized;
drh5e00f6c2001-09-13 13:46:56 +0000207 sqliteCommitInternalChanges(db);
drh58b95762000-06-02 01:17:37 +0000208 }
209 return rc;
210}
211
212/*
drhb217a572000-08-22 13:40:18 +0000213** The version of the library
214*/
drh3d0b5592000-08-22 13:40:51 +0000215const char sqlite_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000216
217/*
drh297ecf12001-04-05 15:57:13 +0000218** Does the library expect data to be encoded as UTF-8 or iso8859? The
219** following global constant always lets us know.
220*/
221#ifdef SQLITE_UTF8
drhfbc3eab2001-04-06 16:13:42 +0000222const char sqlite_encoding[] = "UTF-8";
drh297ecf12001-04-05 15:57:13 +0000223#else
drhfbc3eab2001-04-06 16:13:42 +0000224const char sqlite_encoding[] = "iso8859";
drh297ecf12001-04-05 15:57:13 +0000225#endif
226
227/*
drh58b95762000-06-02 01:17:37 +0000228** Open a new SQLite database. Construct an "sqlite" structure to define
229** the state of this database and return a pointer to that structure.
230**
231** An attempt is made to initialize the in-memory data structures that
232** hold the database schema. But if this fails (because the schema file
233** is locked) then that step is deferred until the first call to
234** sqlite_exec().
235*/
236sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
237 sqlite *db;
238 int rc;
239
240 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000241 db = sqliteMalloc( sizeof(sqlite) );
242 if( pzErrMsg ) *pzErrMsg = 0;
drhdaffd0e2001-04-11 14:28:42 +0000243 if( db==0 ) goto no_mem_on_open;
drh75897232000-05-29 14:26:00 +0000244
245 /* Open the backend database driver */
drh5e00f6c2001-09-13 13:46:56 +0000246 rc = sqliteBtreeOpen(zFilename, mode, 100, &db->pBe);
247 if( rc!=SQLITE_OK ){
248 switch( rc ){
249 default: {
250 if( pzErrMsg ){
251 sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0);
252 }
253 }
254 }
drh75897232000-05-29 14:26:00 +0000255 sqliteFree(db);
drhbe0072d2001-09-13 14:46:09 +0000256 return 0;
drh75897232000-05-29 14:26:00 +0000257 }
258
drh28037572000-08-02 13:47:41 +0000259 /* Assume file format 1 unless the database says otherwise */
260 db->file_format = 1;
261
drh58b95762000-06-02 01:17:37 +0000262 /* Attempt to read the schema */
263 rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000264 if( sqlite_malloc_failed ){
265 goto no_mem_on_open;
266 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
drh58b95762000-06-02 01:17:37 +0000267 sqlite_close(db);
268 return 0;
drh4c504392000-10-16 22:06:40 +0000269 }else /* if( pzErrMsg ) */{
drhdaffd0e2001-04-11 14:28:42 +0000270 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000271 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000272 }
drh75897232000-05-29 14:26:00 +0000273 return db;
drhdaffd0e2001-04-11 14:28:42 +0000274
275no_mem_on_open:
276 sqliteSetString(pzErrMsg, "out of memory", 0);
277 sqliteStrRealloc(pzErrMsg);
278 return 0;
drh75897232000-05-29 14:26:00 +0000279}
280
281/*
282** Close an existing SQLite database
283*/
284void sqlite_close(sqlite *db){
285 int i;
drh5e00f6c2001-09-13 13:46:56 +0000286 sqliteBtreeClose(db->pBe);
drh75897232000-05-29 14:26:00 +0000287 for(i=0; i<N_HASH; i++){
288 Table *pNext, *pList = db->apTblHash[i];
289 db->apTblHash[i] = 0;
290 while( pList ){
291 pNext = pList->pHash;
292 pList->pHash = 0;
293 sqliteDeleteTable(db, pList);
294 pList = pNext;
295 }
296 }
297 sqliteFree(db);
298}
299
300/*
301** Return TRUE if the given SQL string ends in a semicolon.
302*/
303int sqlite_complete(const char *zSql){
drh8c82b352000-12-10 18:23:50 +0000304 int isComplete = 0;
305 while( *zSql ){
306 switch( *zSql ){
307 case ';': {
308 isComplete = 1;
drh75897232000-05-29 14:26:00 +0000309 break;
drh8c82b352000-12-10 18:23:50 +0000310 }
311 case ' ':
312 case '\t':
313 case '\n':
314 case '\f': {
drh75897232000-05-29 14:26:00 +0000315 break;
drh8c82b352000-12-10 18:23:50 +0000316 }
317 case '\'': {
318 isComplete = 0;
319 zSql++;
320 while( *zSql && *zSql!='\'' ){ zSql++; }
321 if( *zSql==0 ) return 0;
drh75897232000-05-29 14:26:00 +0000322 break;
drh8c82b352000-12-10 18:23:50 +0000323 }
324 case '"': {
325 isComplete = 0;
326 zSql++;
327 while( *zSql && *zSql!='"' ){ zSql++; }
328 if( *zSql==0 ) return 0;
329 break;
330 }
331 case '-': {
332 if( zSql[1]!='-' ){
333 isComplete = 0;
334 break;
335 }
336 while( *zSql && *zSql!='\n' ){ zSql++; }
337 if( *zSql==0 ) return isComplete;
338 break;
339 }
340 default: {
341 isComplete = 0;
342 break;
343 }
drh75897232000-05-29 14:26:00 +0000344 }
drh8c82b352000-12-10 18:23:50 +0000345 zSql++;
drh75897232000-05-29 14:26:00 +0000346 }
drh8c82b352000-12-10 18:23:50 +0000347 return isComplete;
drh75897232000-05-29 14:26:00 +0000348}
349
350/*
drhbed86902000-06-02 13:27:59 +0000351** Execute SQL code. Return one of the SQLITE_ success/failure
352** codes. Also write an error message into memory obtained from
353** malloc() and make *pzErrMsg point to that message.
354**
355** If the SQL is a query, then for each row in the query result
356** the xCallback() function is called. pArg becomes the first
357** argument to xCallback(). If xCallback=NULL then no callback
358** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000359*/
360int sqlite_exec(
361 sqlite *db, /* The database on which the SQL executes */
362 char *zSql, /* The SQL to be executed */
363 sqlite_callback xCallback, /* Invoke this callback routine */
364 void *pArg, /* First argument to xCallback() */
365 char **pzErrMsg /* Write error messages here */
366){
367 Parse sParse;
drh75897232000-05-29 14:26:00 +0000368
369 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000370 if( (db->flags & SQLITE_Initialized)==0 ){
371 int rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000372 if( rc!=SQLITE_OK ){
373 sqliteStrRealloc(pzErrMsg);
374 return rc;
375 }
drh58b95762000-06-02 01:17:37 +0000376 }
drh75897232000-05-29 14:26:00 +0000377 memset(&sParse, 0, sizeof(sParse));
378 sParse.db = db;
drh5e00f6c2001-09-13 13:46:56 +0000379 sParse.pBe = db->pBe;
drh75897232000-05-29 14:26:00 +0000380 sParse.xCallback = xCallback;
381 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000382 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000383 if( sqlite_malloc_failed ){
384 sqliteSetString(pzErrMsg, "out of memory", 0);
385 sParse.rc = SQLITE_NOMEM;
386 }
387 sqliteStrRealloc(pzErrMsg);
drh4c504392000-10-16 22:06:40 +0000388 return sParse.rc;
drh75897232000-05-29 14:26:00 +0000389}
drh2dfbbca2000-07-28 14:32:48 +0000390
391/*
392** This routine implements a busy callback that sleeps and tries
393** again until a timeout value is reached. The timeout value is
394** an integer number of milliseconds passed in as the first
395** argument.
396*/
drhdaffd0e2001-04-11 14:28:42 +0000397static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000398 void *Timeout, /* Maximum amount of time to wait */
399 const char *NotUsed, /* The name of the table that is busy */
400 int count /* Number of times table has been busy */
401){
drh82ad3832000-07-31 13:38:24 +0000402#if defined(HAVE_USLEEP) && HAVE_USLEEP
drh2dfbbca2000-07-28 14:32:48 +0000403 int delay = 10000;
404 int prior_delay = 0;
405 int timeout = (int)Timeout;
406 int i;
407
408 for(i=1; i<count; i++){
409 prior_delay += delay;
410 delay = delay*2;
411 if( delay>=1000000 ){
412 delay = 1000000;
413 prior_delay += 1000000*(count - i - 1);
414 break;
415 }
416 }
417 if( prior_delay + delay > timeout*1000 ){
418 delay = timeout*1000 - prior_delay;
419 if( delay<=0 ) return 0;
420 }
421 usleep(delay);
422 return 1;
423#else
424 int timeout = (int)Timeout;
425 if( (count+1)*1000 > timeout ){
426 return 0;
427 }
428 sleep(1);
429 return 1;
430#endif
431}
432
433/*
434** This routine sets the busy callback for an Sqlite database to the
435** given callback function with the given argument.
436*/
437void sqlite_busy_handler(
438 sqlite *db,
439 int (*xBusy)(void*,const char*,int),
440 void *pArg
441){
442 db->xBusyCallback = xBusy;
443 db->pBusyArg = pArg;
444}
445
446/*
447** This routine installs a default busy handler that waits for the
448** specified number of milliseconds before returning 0.
449*/
450void sqlite_busy_timeout(sqlite *db, int ms){
451 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000452 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000453 }else{
454 sqlite_busy_handler(db, 0, 0);
455 }
456}
drh4c504392000-10-16 22:06:40 +0000457
458/*
459** Cause any pending operation to stop at its earliest opportunity.
460*/
461void sqlite_interrupt(sqlite *db){
462 db->flags |= SQLITE_Interrupt;
463}