blob: cbbe5dc8f7dbb8d9a800ef22a8a660b0e34e1f89 [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**
drha1b351a2001-09-14 16:42:12 +000029** $Id: main.c,v 1.35 2001/09/14 16:42:12 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 */
drha1b351a2001-09-14 16:42:12 +0000246 rc = sqliteBtreeOpen(zFilename, mode, MAX_PAGES, &db->pBe);
drh5e00f6c2001-09-13 13:46:56 +0000247 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);
drh5edc3122001-09-13 21:53:09 +0000256 sqliteStrRealloc(pzErrMsg);
drhbe0072d2001-09-13 14:46:09 +0000257 return 0;
drh75897232000-05-29 14:26:00 +0000258 }
259
drh28037572000-08-02 13:47:41 +0000260 /* Assume file format 1 unless the database says otherwise */
261 db->file_format = 1;
262
drh58b95762000-06-02 01:17:37 +0000263 /* Attempt to read the schema */
264 rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000265 if( sqlite_malloc_failed ){
266 goto no_mem_on_open;
267 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
drh58b95762000-06-02 01:17:37 +0000268 sqlite_close(db);
drh5edc3122001-09-13 21:53:09 +0000269 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000270 return 0;
drh4c504392000-10-16 22:06:40 +0000271 }else /* if( pzErrMsg ) */{
drhdaffd0e2001-04-11 14:28:42 +0000272 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000273 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000274 }
drh75897232000-05-29 14:26:00 +0000275 return db;
drhdaffd0e2001-04-11 14:28:42 +0000276
277no_mem_on_open:
278 sqliteSetString(pzErrMsg, "out of memory", 0);
279 sqliteStrRealloc(pzErrMsg);
280 return 0;
drh75897232000-05-29 14:26:00 +0000281}
282
283/*
284** Close an existing SQLite database
285*/
286void sqlite_close(sqlite *db){
287 int i;
drh5e00f6c2001-09-13 13:46:56 +0000288 sqliteBtreeClose(db->pBe);
drh75897232000-05-29 14:26:00 +0000289 for(i=0; i<N_HASH; i++){
290 Table *pNext, *pList = db->apTblHash[i];
291 db->apTblHash[i] = 0;
292 while( pList ){
293 pNext = pList->pHash;
294 pList->pHash = 0;
295 sqliteDeleteTable(db, pList);
296 pList = pNext;
297 }
298 }
299 sqliteFree(db);
300}
301
302/*
303** Return TRUE if the given SQL string ends in a semicolon.
304*/
305int sqlite_complete(const char *zSql){
drh8c82b352000-12-10 18:23:50 +0000306 int isComplete = 0;
307 while( *zSql ){
308 switch( *zSql ){
309 case ';': {
310 isComplete = 1;
drh75897232000-05-29 14:26:00 +0000311 break;
drh8c82b352000-12-10 18:23:50 +0000312 }
313 case ' ':
314 case '\t':
315 case '\n':
316 case '\f': {
drh75897232000-05-29 14:26:00 +0000317 break;
drh8c82b352000-12-10 18:23:50 +0000318 }
319 case '\'': {
320 isComplete = 0;
321 zSql++;
322 while( *zSql && *zSql!='\'' ){ zSql++; }
323 if( *zSql==0 ) return 0;
drh75897232000-05-29 14:26:00 +0000324 break;
drh8c82b352000-12-10 18:23:50 +0000325 }
326 case '"': {
327 isComplete = 0;
328 zSql++;
329 while( *zSql && *zSql!='"' ){ zSql++; }
330 if( *zSql==0 ) return 0;
331 break;
332 }
333 case '-': {
334 if( zSql[1]!='-' ){
335 isComplete = 0;
336 break;
337 }
338 while( *zSql && *zSql!='\n' ){ zSql++; }
339 if( *zSql==0 ) return isComplete;
340 break;
341 }
342 default: {
343 isComplete = 0;
344 break;
345 }
drh75897232000-05-29 14:26:00 +0000346 }
drh8c82b352000-12-10 18:23:50 +0000347 zSql++;
drh75897232000-05-29 14:26:00 +0000348 }
drh8c82b352000-12-10 18:23:50 +0000349 return isComplete;
drh75897232000-05-29 14:26:00 +0000350}
351
352/*
drhbed86902000-06-02 13:27:59 +0000353** Execute SQL code. Return one of the SQLITE_ success/failure
354** codes. Also write an error message into memory obtained from
355** malloc() and make *pzErrMsg point to that message.
356**
357** If the SQL is a query, then for each row in the query result
358** the xCallback() function is called. pArg becomes the first
359** argument to xCallback(). If xCallback=NULL then no callback
360** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000361*/
362int sqlite_exec(
363 sqlite *db, /* The database on which the SQL executes */
364 char *zSql, /* The SQL to be executed */
365 sqlite_callback xCallback, /* Invoke this callback routine */
366 void *pArg, /* First argument to xCallback() */
367 char **pzErrMsg /* Write error messages here */
368){
369 Parse sParse;
drh75897232000-05-29 14:26:00 +0000370
371 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000372 if( (db->flags & SQLITE_Initialized)==0 ){
373 int rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000374 if( rc!=SQLITE_OK ){
375 sqliteStrRealloc(pzErrMsg);
376 return rc;
377 }
drh58b95762000-06-02 01:17:37 +0000378 }
drh75897232000-05-29 14:26:00 +0000379 memset(&sParse, 0, sizeof(sParse));
380 sParse.db = db;
drh5e00f6c2001-09-13 13:46:56 +0000381 sParse.pBe = db->pBe;
drh75897232000-05-29 14:26:00 +0000382 sParse.xCallback = xCallback;
383 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000384 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000385 if( sqlite_malloc_failed ){
386 sqliteSetString(pzErrMsg, "out of memory", 0);
387 sParse.rc = SQLITE_NOMEM;
388 }
389 sqliteStrRealloc(pzErrMsg);
drh4c504392000-10-16 22:06:40 +0000390 return sParse.rc;
drh75897232000-05-29 14:26:00 +0000391}
drh2dfbbca2000-07-28 14:32:48 +0000392
393/*
394** This routine implements a busy callback that sleeps and tries
395** again until a timeout value is reached. The timeout value is
396** an integer number of milliseconds passed in as the first
397** argument.
398*/
drhdaffd0e2001-04-11 14:28:42 +0000399static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000400 void *Timeout, /* Maximum amount of time to wait */
401 const char *NotUsed, /* The name of the table that is busy */
402 int count /* Number of times table has been busy */
403){
drh82ad3832000-07-31 13:38:24 +0000404#if defined(HAVE_USLEEP) && HAVE_USLEEP
drh2dfbbca2000-07-28 14:32:48 +0000405 int delay = 10000;
406 int prior_delay = 0;
407 int timeout = (int)Timeout;
408 int i;
409
410 for(i=1; i<count; i++){
411 prior_delay += delay;
412 delay = delay*2;
413 if( delay>=1000000 ){
414 delay = 1000000;
415 prior_delay += 1000000*(count - i - 1);
416 break;
417 }
418 }
419 if( prior_delay + delay > timeout*1000 ){
420 delay = timeout*1000 - prior_delay;
421 if( delay<=0 ) return 0;
422 }
423 usleep(delay);
424 return 1;
425#else
426 int timeout = (int)Timeout;
427 if( (count+1)*1000 > timeout ){
428 return 0;
429 }
430 sleep(1);
431 return 1;
432#endif
433}
434
435/*
436** This routine sets the busy callback for an Sqlite database to the
437** given callback function with the given argument.
438*/
439void sqlite_busy_handler(
440 sqlite *db,
441 int (*xBusy)(void*,const char*,int),
442 void *pArg
443){
444 db->xBusyCallback = xBusy;
445 db->pBusyArg = pArg;
446}
447
448/*
449** This routine installs a default busy handler that waits for the
450** specified number of milliseconds before returning 0.
451*/
452void sqlite_busy_timeout(sqlite *db, int ms){
453 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000454 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000455 }else{
456 sqlite_busy_handler(db, 0, 0);
457 }
458}
drh4c504392000-10-16 22:06:40 +0000459
460/*
461** Cause any pending operation to stop at its earliest opportunity.
462*/
463void sqlite_interrupt(sqlite *db){
464 db->flags |= SQLITE_Interrupt;
465}