blob: f32850b6c001ad565377bf0f78479a4e268d86e3 [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**
drh7e3b0a02001-04-28 16:52:40 +000029** $Id: main.c,v 1.29 2001/04/28 16:52:42 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"
90 " tbl_name text,\n"
91 " sql text\n"
92 ")"
93 ;
94
95 /* The following program is used to initialize the internal
96 ** structure holding the tables and indexes of the database.
97 ** The database contains a special table named "sqlite_master"
98 ** defined as follows:
99 **
100 ** CREATE TABLE sqlite_master (
drh28037572000-08-02 13:47:41 +0000101 ** type text, -- Either "table" or "index" or "meta"
drh75897232000-05-29 14:26:00 +0000102 ** name text, -- Name of table or index
103 ** tbl_name text, -- Associated table
104 ** sql text -- The CREATE statement for this object
105 ** );
106 **
107 ** The sqlite_master table contains a single entry for each table
drh967e8b72000-06-21 13:59:10 +0000108 ** and each index. The "type" column tells whether the entry is
109 ** a table or index. The "name" column is the name of the object.
drh75897232000-05-29 14:26:00 +0000110 ** The "tbl_name" is the name of the associated table. For tables,
drh967e8b72000-06-21 13:59:10 +0000111 ** the tbl_name column is always the same as name. For indices, the
112 ** tbl_name column contains the name of the table that the index
113 ** indexes. Finally, the "sql" column contains the complete text of
drh75897232000-05-29 14:26:00 +0000114 ** the CREATE TABLE or CREATE INDEX statement that originally created
115 ** the table or index.
116 **
drh28037572000-08-02 13:47:41 +0000117 ** If the "type" column has the value "meta", then the "sql" column
118 ** contains extra information about the database, such as the
119 ** file format version number. All meta information must be processed
120 ** before any tables or indices are constructed.
121 **
drh75897232000-05-29 14:26:00 +0000122 ** The following program invokes its callback on the SQL for each
123 ** table then goes back and invokes the callback on the
124 ** SQL for each index. The callback will invoke the
125 ** parser to build the internal representation of the
126 ** database scheme.
127 */
128 static VdbeOp initProg[] = {
drh338ea132001-02-11 16:56:24 +0000129 { OP_OpenTbl, 0, 0, MASTER_NAME},
drh28037572000-08-02 13:47:41 +0000130 { OP_Next, 0, 9, 0}, /* 1 */
131 { OP_Field, 0, 0, 0},
132 { OP_String, 0, 0, "meta"},
133 { OP_Ne, 0, 1, 0},
134 { OP_Field, 0, 0, 0},
135 { OP_Field, 0, 3, 0},
136 { OP_Callback, 2, 0, 0},
137 { OP_Goto, 0, 1, 0},
138 { OP_Rewind, 0, 0, 0}, /* 9 */
139 { OP_Next, 0, 17, 0}, /* 10 */
drh75897232000-05-29 14:26:00 +0000140 { OP_Field, 0, 0, 0},
141 { OP_String, 0, 0, "table"},
drh28037572000-08-02 13:47:41 +0000142 { OP_Ne, 0, 10, 0},
drh75897232000-05-29 14:26:00 +0000143 { OP_Field, 0, 3, 0},
144 { OP_Callback, 1, 0, 0},
drh28037572000-08-02 13:47:41 +0000145 { OP_Goto, 0, 10, 0},
146 { OP_Rewind, 0, 0, 0}, /* 17 */
147 { OP_Next, 0, 25, 0}, /* 18 */
drh75897232000-05-29 14:26:00 +0000148 { OP_Field, 0, 0, 0},
149 { OP_String, 0, 0, "index"},
drh28037572000-08-02 13:47:41 +0000150 { OP_Ne, 0, 18, 0},
drh75897232000-05-29 14:26:00 +0000151 { OP_Field, 0, 3, 0},
152 { OP_Callback, 1, 0, 0},
drh28037572000-08-02 13:47:41 +0000153 { OP_Goto, 0, 18, 0},
154 { OP_Halt, 0, 0, 0}, /* 25 */
drh75897232000-05-29 14:26:00 +0000155 };
156
drh58b95762000-06-02 01:17:37 +0000157 /* Create a virtual machine to run the initialization program. Run
158 ** the program. The delete the virtual machine.
159 */
drh4c504392000-10-16 22:06:40 +0000160 vdbe = sqliteVdbeCreate(db);
drhd8bc7082000-06-07 23:51:50 +0000161 if( vdbe==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000162 sqliteSetString(pzErrMsg, "out of memory");
163 return SQLITE_NOMEM;
drhd8bc7082000-06-07 23:51:50 +0000164 }
drh58b95762000-06-02 01:17:37 +0000165 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
drh2dfbbca2000-07-28 14:32:48 +0000166 rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg,
167 db->pBusyArg, db->xBusyCallback);
drh58b95762000-06-02 01:17:37 +0000168 sqliteVdbeDelete(vdbe);
drh28037572000-08-02 13:47:41 +0000169 if( rc==SQLITE_OK && db->file_format<2 && db->nTable>0 ){
170 sqliteSetString(pzErrMsg, "obsolete file format", 0);
171 rc = SQLITE_ERROR;
172 }
drh58b95762000-06-02 01:17:37 +0000173 if( rc==SQLITE_OK ){
174 Table *pTab;
175 char *azArg[2];
176 azArg[0] = master_schema;
177 azArg[1] = 0;
178 sqliteOpenCb(db, 1, azArg, 0);
179 pTab = sqliteFindTable(db, MASTER_NAME);
180 if( pTab ){
181 pTab->readOnly = 1;
182 }
183 db->flags |= SQLITE_Initialized;
drh58b95762000-06-02 01:17:37 +0000184 }
185 return rc;
186}
187
188/*
drhb217a572000-08-22 13:40:18 +0000189** The version of the library
190*/
drh3d0b5592000-08-22 13:40:51 +0000191const char sqlite_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000192
193/*
drh297ecf12001-04-05 15:57:13 +0000194** Does the library expect data to be encoded as UTF-8 or iso8859? The
195** following global constant always lets us know.
196*/
197#ifdef SQLITE_UTF8
drhfbc3eab2001-04-06 16:13:42 +0000198const char sqlite_encoding[] = "UTF-8";
drh297ecf12001-04-05 15:57:13 +0000199#else
drhfbc3eab2001-04-06 16:13:42 +0000200const char sqlite_encoding[] = "iso8859";
drh297ecf12001-04-05 15:57:13 +0000201#endif
202
203/*
drh58b95762000-06-02 01:17:37 +0000204** Open a new SQLite database. Construct an "sqlite" structure to define
205** the state of this database and return a pointer to that structure.
206**
207** An attempt is made to initialize the in-memory data structures that
208** hold the database schema. But if this fails (because the schema file
209** is locked) then that step is deferred until the first call to
210** sqlite_exec().
211*/
212sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
213 sqlite *db;
214 int rc;
215
216 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000217 db = sqliteMalloc( sizeof(sqlite) );
218 if( pzErrMsg ) *pzErrMsg = 0;
drhdaffd0e2001-04-11 14:28:42 +0000219 if( db==0 ) goto no_mem_on_open;
drh75897232000-05-29 14:26:00 +0000220
221 /* Open the backend database driver */
222 db->pBe = sqliteDbbeOpen(zFilename, (mode&0222)!=0, mode!=0, pzErrMsg);
223 if( db->pBe==0 ){
224 sqliteFree(db);
drhdaffd0e2001-04-11 14:28:42 +0000225 sqliteStrRealloc(pzErrMsg);
drh75897232000-05-29 14:26:00 +0000226 return 0;
227 }
228
drh28037572000-08-02 13:47:41 +0000229 /* Assume file format 1 unless the database says otherwise */
230 db->file_format = 1;
231
drh58b95762000-06-02 01:17:37 +0000232 /* Attempt to read the schema */
233 rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000234 if( sqlite_malloc_failed ){
235 goto no_mem_on_open;
236 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
drh58b95762000-06-02 01:17:37 +0000237 sqlite_close(db);
238 return 0;
drh4c504392000-10-16 22:06:40 +0000239 }else /* if( pzErrMsg ) */{
drhdaffd0e2001-04-11 14:28:42 +0000240 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000241 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000242 }
drh75897232000-05-29 14:26:00 +0000243 return db;
drhdaffd0e2001-04-11 14:28:42 +0000244
245no_mem_on_open:
246 sqliteSetString(pzErrMsg, "out of memory", 0);
247 sqliteStrRealloc(pzErrMsg);
248 return 0;
drh75897232000-05-29 14:26:00 +0000249}
250
251/*
252** Close an existing SQLite database
253*/
254void sqlite_close(sqlite *db){
255 int i;
drhae85dc82001-01-13 14:34:05 +0000256 db->pBe->x->Close(db->pBe);
drh75897232000-05-29 14:26:00 +0000257 for(i=0; i<N_HASH; i++){
258 Table *pNext, *pList = db->apTblHash[i];
259 db->apTblHash[i] = 0;
260 while( pList ){
261 pNext = pList->pHash;
262 pList->pHash = 0;
263 sqliteDeleteTable(db, pList);
264 pList = pNext;
265 }
266 }
267 sqliteFree(db);
268}
269
270/*
271** Return TRUE if the given SQL string ends in a semicolon.
272*/
273int sqlite_complete(const char *zSql){
drh8c82b352000-12-10 18:23:50 +0000274 int isComplete = 0;
275 while( *zSql ){
276 switch( *zSql ){
277 case ';': {
278 isComplete = 1;
drh75897232000-05-29 14:26:00 +0000279 break;
drh8c82b352000-12-10 18:23:50 +0000280 }
281 case ' ':
282 case '\t':
283 case '\n':
284 case '\f': {
drh75897232000-05-29 14:26:00 +0000285 break;
drh8c82b352000-12-10 18:23:50 +0000286 }
287 case '\'': {
288 isComplete = 0;
289 zSql++;
290 while( *zSql && *zSql!='\'' ){ zSql++; }
291 if( *zSql==0 ) return 0;
drh75897232000-05-29 14:26:00 +0000292 break;
drh8c82b352000-12-10 18:23:50 +0000293 }
294 case '"': {
295 isComplete = 0;
296 zSql++;
297 while( *zSql && *zSql!='"' ){ zSql++; }
298 if( *zSql==0 ) return 0;
299 break;
300 }
301 case '-': {
302 if( zSql[1]!='-' ){
303 isComplete = 0;
304 break;
305 }
306 while( *zSql && *zSql!='\n' ){ zSql++; }
307 if( *zSql==0 ) return isComplete;
308 break;
309 }
310 default: {
311 isComplete = 0;
312 break;
313 }
drh75897232000-05-29 14:26:00 +0000314 }
drh8c82b352000-12-10 18:23:50 +0000315 zSql++;
drh75897232000-05-29 14:26:00 +0000316 }
drh8c82b352000-12-10 18:23:50 +0000317 return isComplete;
drh75897232000-05-29 14:26:00 +0000318}
319
320/*
drhbed86902000-06-02 13:27:59 +0000321** Execute SQL code. Return one of the SQLITE_ success/failure
322** codes. Also write an error message into memory obtained from
323** malloc() and make *pzErrMsg point to that message.
324**
325** If the SQL is a query, then for each row in the query result
326** the xCallback() function is called. pArg becomes the first
327** argument to xCallback(). If xCallback=NULL then no callback
328** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000329*/
330int sqlite_exec(
331 sqlite *db, /* The database on which the SQL executes */
332 char *zSql, /* The SQL to be executed */
333 sqlite_callback xCallback, /* Invoke this callback routine */
334 void *pArg, /* First argument to xCallback() */
335 char **pzErrMsg /* Write error messages here */
336){
337 Parse sParse;
drh75897232000-05-29 14:26:00 +0000338
339 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000340 if( (db->flags & SQLITE_Initialized)==0 ){
341 int rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000342 if( rc!=SQLITE_OK ){
343 sqliteStrRealloc(pzErrMsg);
344 return rc;
345 }
drh58b95762000-06-02 01:17:37 +0000346 }
drh75897232000-05-29 14:26:00 +0000347 memset(&sParse, 0, sizeof(sParse));
348 sParse.db = db;
349 sParse.xCallback = xCallback;
350 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000351 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000352 if( sqlite_malloc_failed ){
353 sqliteSetString(pzErrMsg, "out of memory", 0);
354 sParse.rc = SQLITE_NOMEM;
355 }
356 sqliteStrRealloc(pzErrMsg);
drh4c504392000-10-16 22:06:40 +0000357 return sParse.rc;
drh75897232000-05-29 14:26:00 +0000358}
drh2dfbbca2000-07-28 14:32:48 +0000359
360/*
361** This routine implements a busy callback that sleeps and tries
362** again until a timeout value is reached. The timeout value is
363** an integer number of milliseconds passed in as the first
364** argument.
365*/
drhdaffd0e2001-04-11 14:28:42 +0000366static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000367 void *Timeout, /* Maximum amount of time to wait */
368 const char *NotUsed, /* The name of the table that is busy */
369 int count /* Number of times table has been busy */
370){
drh82ad3832000-07-31 13:38:24 +0000371#if defined(HAVE_USLEEP) && HAVE_USLEEP
drh2dfbbca2000-07-28 14:32:48 +0000372 int delay = 10000;
373 int prior_delay = 0;
374 int timeout = (int)Timeout;
375 int i;
376
377 for(i=1; i<count; i++){
378 prior_delay += delay;
379 delay = delay*2;
380 if( delay>=1000000 ){
381 delay = 1000000;
382 prior_delay += 1000000*(count - i - 1);
383 break;
384 }
385 }
386 if( prior_delay + delay > timeout*1000 ){
387 delay = timeout*1000 - prior_delay;
388 if( delay<=0 ) return 0;
389 }
390 usleep(delay);
391 return 1;
392#else
393 int timeout = (int)Timeout;
394 if( (count+1)*1000 > timeout ){
395 return 0;
396 }
397 sleep(1);
398 return 1;
399#endif
400}
401
402/*
403** This routine sets the busy callback for an Sqlite database to the
404** given callback function with the given argument.
405*/
406void sqlite_busy_handler(
407 sqlite *db,
408 int (*xBusy)(void*,const char*,int),
409 void *pArg
410){
411 db->xBusyCallback = xBusy;
412 db->pBusyArg = pArg;
413}
414
415/*
416** This routine installs a default busy handler that waits for the
417** specified number of milliseconds before returning 0.
418*/
419void sqlite_busy_timeout(sqlite *db, int ms){
420 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000421 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000422 }else{
423 sqlite_busy_handler(db, 0, 0);
424 }
425}
drh4c504392000-10-16 22:06:40 +0000426
427/*
428** Cause any pending operation to stop at its earliest opportunity.
429*/
430void sqlite_interrupt(sqlite *db){
431 db->flags |= SQLITE_Interrupt;
432}