blob: 8345b2a399776ef8c8a3b05cfaf9846686a05348 [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**
drhbe0072d2001-09-13 14:46:09 +000029** $Id: main.c,v 1.31 2001/09/13 14:46:10 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;
185 }
186 db->flags |= SQLITE_Initialized;
drh5e00f6c2001-09-13 13:46:56 +0000187 sqliteCommitInternalChanges(db);
drh58b95762000-06-02 01:17:37 +0000188 }
189 return rc;
190}
191
192/*
drhb217a572000-08-22 13:40:18 +0000193** The version of the library
194*/
drh3d0b5592000-08-22 13:40:51 +0000195const char sqlite_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000196
197/*
drh297ecf12001-04-05 15:57:13 +0000198** Does the library expect data to be encoded as UTF-8 or iso8859? The
199** following global constant always lets us know.
200*/
201#ifdef SQLITE_UTF8
drhfbc3eab2001-04-06 16:13:42 +0000202const char sqlite_encoding[] = "UTF-8";
drh297ecf12001-04-05 15:57:13 +0000203#else
drhfbc3eab2001-04-06 16:13:42 +0000204const char sqlite_encoding[] = "iso8859";
drh297ecf12001-04-05 15:57:13 +0000205#endif
206
207/*
drh58b95762000-06-02 01:17:37 +0000208** Open a new SQLite database. Construct an "sqlite" structure to define
209** the state of this database and return a pointer to that structure.
210**
211** An attempt is made to initialize the in-memory data structures that
212** hold the database schema. But if this fails (because the schema file
213** is locked) then that step is deferred until the first call to
214** sqlite_exec().
215*/
216sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
217 sqlite *db;
218 int rc;
219
220 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000221 db = sqliteMalloc( sizeof(sqlite) );
222 if( pzErrMsg ) *pzErrMsg = 0;
drhdaffd0e2001-04-11 14:28:42 +0000223 if( db==0 ) goto no_mem_on_open;
drh75897232000-05-29 14:26:00 +0000224
225 /* Open the backend database driver */
drh5e00f6c2001-09-13 13:46:56 +0000226 rc = sqliteBtreeOpen(zFilename, mode, 100, &db->pBe);
227 if( rc!=SQLITE_OK ){
228 switch( rc ){
229 default: {
230 if( pzErrMsg ){
231 sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0);
232 }
233 }
234 }
drh75897232000-05-29 14:26:00 +0000235 sqliteFree(db);
drhbe0072d2001-09-13 14:46:09 +0000236 return 0;
drh75897232000-05-29 14:26:00 +0000237 }
238
drh28037572000-08-02 13:47:41 +0000239 /* Assume file format 1 unless the database says otherwise */
240 db->file_format = 1;
241
drh58b95762000-06-02 01:17:37 +0000242 /* Attempt to read the schema */
243 rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000244 if( sqlite_malloc_failed ){
245 goto no_mem_on_open;
246 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
drh58b95762000-06-02 01:17:37 +0000247 sqlite_close(db);
248 return 0;
drh4c504392000-10-16 22:06:40 +0000249 }else /* if( pzErrMsg ) */{
drhdaffd0e2001-04-11 14:28:42 +0000250 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000251 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000252 }
drh75897232000-05-29 14:26:00 +0000253 return db;
drhdaffd0e2001-04-11 14:28:42 +0000254
255no_mem_on_open:
256 sqliteSetString(pzErrMsg, "out of memory", 0);
257 sqliteStrRealloc(pzErrMsg);
258 return 0;
drh75897232000-05-29 14:26:00 +0000259}
260
261/*
262** Close an existing SQLite database
263*/
264void sqlite_close(sqlite *db){
265 int i;
drh5e00f6c2001-09-13 13:46:56 +0000266 sqliteBtreeClose(db->pBe);
drh75897232000-05-29 14:26:00 +0000267 for(i=0; i<N_HASH; i++){
268 Table *pNext, *pList = db->apTblHash[i];
269 db->apTblHash[i] = 0;
270 while( pList ){
271 pNext = pList->pHash;
272 pList->pHash = 0;
273 sqliteDeleteTable(db, pList);
274 pList = pNext;
275 }
276 }
277 sqliteFree(db);
278}
279
280/*
281** Return TRUE if the given SQL string ends in a semicolon.
282*/
283int sqlite_complete(const char *zSql){
drh8c82b352000-12-10 18:23:50 +0000284 int isComplete = 0;
285 while( *zSql ){
286 switch( *zSql ){
287 case ';': {
288 isComplete = 1;
drh75897232000-05-29 14:26:00 +0000289 break;
drh8c82b352000-12-10 18:23:50 +0000290 }
291 case ' ':
292 case '\t':
293 case '\n':
294 case '\f': {
drh75897232000-05-29 14:26:00 +0000295 break;
drh8c82b352000-12-10 18:23:50 +0000296 }
297 case '\'': {
298 isComplete = 0;
299 zSql++;
300 while( *zSql && *zSql!='\'' ){ zSql++; }
301 if( *zSql==0 ) return 0;
drh75897232000-05-29 14:26:00 +0000302 break;
drh8c82b352000-12-10 18:23:50 +0000303 }
304 case '"': {
305 isComplete = 0;
306 zSql++;
307 while( *zSql && *zSql!='"' ){ zSql++; }
308 if( *zSql==0 ) return 0;
309 break;
310 }
311 case '-': {
312 if( zSql[1]!='-' ){
313 isComplete = 0;
314 break;
315 }
316 while( *zSql && *zSql!='\n' ){ zSql++; }
317 if( *zSql==0 ) return isComplete;
318 break;
319 }
320 default: {
321 isComplete = 0;
322 break;
323 }
drh75897232000-05-29 14:26:00 +0000324 }
drh8c82b352000-12-10 18:23:50 +0000325 zSql++;
drh75897232000-05-29 14:26:00 +0000326 }
drh8c82b352000-12-10 18:23:50 +0000327 return isComplete;
drh75897232000-05-29 14:26:00 +0000328}
329
330/*
drhbed86902000-06-02 13:27:59 +0000331** Execute SQL code. Return one of the SQLITE_ success/failure
332** codes. Also write an error message into memory obtained from
333** malloc() and make *pzErrMsg point to that message.
334**
335** If the SQL is a query, then for each row in the query result
336** the xCallback() function is called. pArg becomes the first
337** argument to xCallback(). If xCallback=NULL then no callback
338** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000339*/
340int sqlite_exec(
341 sqlite *db, /* The database on which the SQL executes */
342 char *zSql, /* The SQL to be executed */
343 sqlite_callback xCallback, /* Invoke this callback routine */
344 void *pArg, /* First argument to xCallback() */
345 char **pzErrMsg /* Write error messages here */
346){
347 Parse sParse;
drh75897232000-05-29 14:26:00 +0000348
349 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000350 if( (db->flags & SQLITE_Initialized)==0 ){
351 int rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000352 if( rc!=SQLITE_OK ){
353 sqliteStrRealloc(pzErrMsg);
354 return rc;
355 }
drh58b95762000-06-02 01:17:37 +0000356 }
drh75897232000-05-29 14:26:00 +0000357 memset(&sParse, 0, sizeof(sParse));
358 sParse.db = db;
drh5e00f6c2001-09-13 13:46:56 +0000359 sParse.pBe = db->pBe;
drh75897232000-05-29 14:26:00 +0000360 sParse.xCallback = xCallback;
361 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000362 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000363 if( sqlite_malloc_failed ){
364 sqliteSetString(pzErrMsg, "out of memory", 0);
365 sParse.rc = SQLITE_NOMEM;
366 }
367 sqliteStrRealloc(pzErrMsg);
drh4c504392000-10-16 22:06:40 +0000368 return sParse.rc;
drh75897232000-05-29 14:26:00 +0000369}
drh2dfbbca2000-07-28 14:32:48 +0000370
371/*
372** This routine implements a busy callback that sleeps and tries
373** again until a timeout value is reached. The timeout value is
374** an integer number of milliseconds passed in as the first
375** argument.
376*/
drhdaffd0e2001-04-11 14:28:42 +0000377static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000378 void *Timeout, /* Maximum amount of time to wait */
379 const char *NotUsed, /* The name of the table that is busy */
380 int count /* Number of times table has been busy */
381){
drh82ad3832000-07-31 13:38:24 +0000382#if defined(HAVE_USLEEP) && HAVE_USLEEP
drh2dfbbca2000-07-28 14:32:48 +0000383 int delay = 10000;
384 int prior_delay = 0;
385 int timeout = (int)Timeout;
386 int i;
387
388 for(i=1; i<count; i++){
389 prior_delay += delay;
390 delay = delay*2;
391 if( delay>=1000000 ){
392 delay = 1000000;
393 prior_delay += 1000000*(count - i - 1);
394 break;
395 }
396 }
397 if( prior_delay + delay > timeout*1000 ){
398 delay = timeout*1000 - prior_delay;
399 if( delay<=0 ) return 0;
400 }
401 usleep(delay);
402 return 1;
403#else
404 int timeout = (int)Timeout;
405 if( (count+1)*1000 > timeout ){
406 return 0;
407 }
408 sleep(1);
409 return 1;
410#endif
411}
412
413/*
414** This routine sets the busy callback for an Sqlite database to the
415** given callback function with the given argument.
416*/
417void sqlite_busy_handler(
418 sqlite *db,
419 int (*xBusy)(void*,const char*,int),
420 void *pArg
421){
422 db->xBusyCallback = xBusy;
423 db->pBusyArg = pArg;
424}
425
426/*
427** This routine installs a default busy handler that waits for the
428** specified number of milliseconds before returning 0.
429*/
430void sqlite_busy_timeout(sqlite *db, int ms){
431 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000432 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000433 }else{
434 sqlite_busy_handler(db, 0, 0);
435 }
436}
drh4c504392000-10-16 22:06:40 +0000437
438/*
439** Cause any pending operation to stop at its earliest opportunity.
440*/
441void sqlite_interrupt(sqlite *db){
442 db->flags |= SQLITE_Interrupt;
443}