blob: 431aacea6eaf75afd5c3dc05713d4c9a9d8a2d9e [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**
drhdaffd0e2001-04-11 14:28:42 +000029** $Id: main.c,v 1.28 2001/04/11 14:28:42 drh Exp $
drh75897232000-05-29 14:26:00 +000030*/
31#include "sqliteInt.h"
drh345fda32001-01-15 22:51:08 +000032#include <unistd.h>
drh75897232000-05-29 14:26:00 +000033
34/*
35** This is the callback routine for the code that initializes the
36** database. Each callback contains text of a CREATE TABLE or
37** CREATE INDEX statement that must be parsed to yield the internal
38** structures that describe the tables.
drh28037572000-08-02 13:47:41 +000039**
40** This callback is also called with argc==2 when there is meta
41** information in the sqlite_master file. The meta information is
42** contained in argv[1]. Typical meta information is the file format
43** version.
drh75897232000-05-29 14:26:00 +000044*/
45static int sqliteOpenCb(void *pDb, int argc, char **argv, char **azColName){
46 sqlite *db = (sqlite*)pDb;
47 Parse sParse;
48 int nErr;
drh75897232000-05-29 14:26:00 +000049
drh28037572000-08-02 13:47:41 +000050 if( argc==2 ){
51 if( sscanf(argv[1],"file format %d",&db->file_format)==1 ){
52 return 0;
53 }
54 /* Unknown meta information. Ignore it. */
55 return 0;
56 }
drh75897232000-05-29 14:26:00 +000057 if( argc!=1 ) return 0;
58 memset(&sParse, 0, sizeof(sParse));
59 sParse.db = db;
60 sParse.initFlag = 1;
drhd1dedb82000-06-05 02:07:04 +000061 nErr = sqliteRunParser(&sParse, argv[0], 0);
drh75897232000-05-29 14:26:00 +000062 return nErr;
63}
64
65/*
drh58b95762000-06-02 01:17:37 +000066** Attempt to read the database schema and initialize internal
67** data structures. Return one of the SQLITE_ error codes to
68** indicate success or failure.
drhbed86902000-06-02 13:27:59 +000069**
70** After the database is initialized, the SQLITE_Initialized
71** bit is set in the flags field of the sqlite structure. An
72** attempt is made to initialize the database as soon as it
73** is opened. If that fails (perhaps because another process
74** has the sqlite_master table locked) than another attempt
75** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +000076*/
drh58b95762000-06-02 01:17:37 +000077static int sqliteInit(sqlite *db, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +000078 Vdbe *vdbe;
drh58b95762000-06-02 01:17:37 +000079 int rc;
80
81 /*
82 ** The master database table has a structure like this
83 */
drh75897232000-05-29 14:26:00 +000084 static char master_schema[] =
85 "CREATE TABLE " MASTER_NAME " (\n"
86 " type text,\n"
87 " name text,\n"
88 " tbl_name text,\n"
89 " sql text\n"
90 ")"
91 ;
92
93 /* The following program is used to initialize the internal
94 ** structure holding the tables and indexes of the database.
95 ** The database contains a special table named "sqlite_master"
96 ** defined as follows:
97 **
98 ** CREATE TABLE sqlite_master (
drh28037572000-08-02 13:47:41 +000099 ** type text, -- Either "table" or "index" or "meta"
drh75897232000-05-29 14:26:00 +0000100 ** name text, -- Name of table or index
101 ** tbl_name text, -- Associated table
102 ** sql text -- The CREATE statement for this object
103 ** );
104 **
105 ** The sqlite_master table contains a single entry for each table
drh967e8b72000-06-21 13:59:10 +0000106 ** and each index. The "type" column tells whether the entry is
107 ** a table or index. The "name" column is the name of the object.
drh75897232000-05-29 14:26:00 +0000108 ** The "tbl_name" is the name of the associated table. For tables,
drh967e8b72000-06-21 13:59:10 +0000109 ** the tbl_name column is always the same as name. For indices, the
110 ** tbl_name column contains the name of the table that the index
111 ** indexes. Finally, the "sql" column contains the complete text of
drh75897232000-05-29 14:26:00 +0000112 ** the CREATE TABLE or CREATE INDEX statement that originally created
113 ** the table or index.
114 **
drh28037572000-08-02 13:47:41 +0000115 ** If the "type" column has the value "meta", then the "sql" column
116 ** contains extra information about the database, such as the
117 ** file format version number. All meta information must be processed
118 ** before any tables or indices are constructed.
119 **
drh75897232000-05-29 14:26:00 +0000120 ** The following program invokes its callback on the SQL for each
121 ** table then goes back and invokes the callback on the
122 ** SQL for each index. The callback will invoke the
123 ** parser to build the internal representation of the
124 ** database scheme.
125 */
126 static VdbeOp initProg[] = {
drh338ea132001-02-11 16:56:24 +0000127 { OP_OpenTbl, 0, 0, MASTER_NAME},
drh28037572000-08-02 13:47:41 +0000128 { OP_Next, 0, 9, 0}, /* 1 */
129 { OP_Field, 0, 0, 0},
130 { OP_String, 0, 0, "meta"},
131 { OP_Ne, 0, 1, 0},
132 { OP_Field, 0, 0, 0},
133 { OP_Field, 0, 3, 0},
134 { OP_Callback, 2, 0, 0},
135 { OP_Goto, 0, 1, 0},
136 { OP_Rewind, 0, 0, 0}, /* 9 */
137 { OP_Next, 0, 17, 0}, /* 10 */
drh75897232000-05-29 14:26:00 +0000138 { OP_Field, 0, 0, 0},
139 { OP_String, 0, 0, "table"},
drh28037572000-08-02 13:47:41 +0000140 { OP_Ne, 0, 10, 0},
drh75897232000-05-29 14:26:00 +0000141 { OP_Field, 0, 3, 0},
142 { OP_Callback, 1, 0, 0},
drh28037572000-08-02 13:47:41 +0000143 { OP_Goto, 0, 10, 0},
144 { OP_Rewind, 0, 0, 0}, /* 17 */
145 { OP_Next, 0, 25, 0}, /* 18 */
drh75897232000-05-29 14:26:00 +0000146 { OP_Field, 0, 0, 0},
147 { OP_String, 0, 0, "index"},
drh28037572000-08-02 13:47:41 +0000148 { OP_Ne, 0, 18, 0},
drh75897232000-05-29 14:26:00 +0000149 { OP_Field, 0, 3, 0},
150 { OP_Callback, 1, 0, 0},
drh28037572000-08-02 13:47:41 +0000151 { OP_Goto, 0, 18, 0},
152 { OP_Halt, 0, 0, 0}, /* 25 */
drh75897232000-05-29 14:26:00 +0000153 };
154
drh58b95762000-06-02 01:17:37 +0000155 /* Create a virtual machine to run the initialization program. Run
156 ** the program. The delete the virtual machine.
157 */
drh4c504392000-10-16 22:06:40 +0000158 vdbe = sqliteVdbeCreate(db);
drhd8bc7082000-06-07 23:51:50 +0000159 if( vdbe==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000160 sqliteSetString(pzErrMsg, "out of memory");
161 return SQLITE_NOMEM;
drhd8bc7082000-06-07 23:51:50 +0000162 }
drh58b95762000-06-02 01:17:37 +0000163 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
drh2dfbbca2000-07-28 14:32:48 +0000164 rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg,
165 db->pBusyArg, db->xBusyCallback);
drh58b95762000-06-02 01:17:37 +0000166 sqliteVdbeDelete(vdbe);
drh28037572000-08-02 13:47:41 +0000167 if( rc==SQLITE_OK && db->file_format<2 && db->nTable>0 ){
168 sqliteSetString(pzErrMsg, "obsolete file format", 0);
169 rc = SQLITE_ERROR;
170 }
drh58b95762000-06-02 01:17:37 +0000171 if( rc==SQLITE_OK ){
172 Table *pTab;
173 char *azArg[2];
174 azArg[0] = master_schema;
175 azArg[1] = 0;
176 sqliteOpenCb(db, 1, azArg, 0);
177 pTab = sqliteFindTable(db, MASTER_NAME);
178 if( pTab ){
179 pTab->readOnly = 1;
180 }
181 db->flags |= SQLITE_Initialized;
drh58b95762000-06-02 01:17:37 +0000182 }
183 return rc;
184}
185
186/*
drhb217a572000-08-22 13:40:18 +0000187** The version of the library
188*/
drh3d0b5592000-08-22 13:40:51 +0000189const char sqlite_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000190
191/*
drh297ecf12001-04-05 15:57:13 +0000192** Does the library expect data to be encoded as UTF-8 or iso8859? The
193** following global constant always lets us know.
194*/
195#ifdef SQLITE_UTF8
drhfbc3eab2001-04-06 16:13:42 +0000196const char sqlite_encoding[] = "UTF-8";
drh297ecf12001-04-05 15:57:13 +0000197#else
drhfbc3eab2001-04-06 16:13:42 +0000198const char sqlite_encoding[] = "iso8859";
drh297ecf12001-04-05 15:57:13 +0000199#endif
200
201/*
drh58b95762000-06-02 01:17:37 +0000202** Open a new SQLite database. Construct an "sqlite" structure to define
203** the state of this database and return a pointer to that structure.
204**
205** An attempt is made to initialize the in-memory data structures that
206** hold the database schema. But if this fails (because the schema file
207** is locked) then that step is deferred until the first call to
208** sqlite_exec().
209*/
210sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
211 sqlite *db;
212 int rc;
213
214 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000215 db = sqliteMalloc( sizeof(sqlite) );
216 if( pzErrMsg ) *pzErrMsg = 0;
drhdaffd0e2001-04-11 14:28:42 +0000217 if( db==0 ) goto no_mem_on_open;
drh75897232000-05-29 14:26:00 +0000218
219 /* Open the backend database driver */
220 db->pBe = sqliteDbbeOpen(zFilename, (mode&0222)!=0, mode!=0, pzErrMsg);
221 if( db->pBe==0 ){
222 sqliteFree(db);
drhdaffd0e2001-04-11 14:28:42 +0000223 sqliteStrRealloc(pzErrMsg);
drh75897232000-05-29 14:26:00 +0000224 return 0;
225 }
226
drh28037572000-08-02 13:47:41 +0000227 /* Assume file format 1 unless the database says otherwise */
228 db->file_format = 1;
229
drh58b95762000-06-02 01:17:37 +0000230 /* Attempt to read the schema */
231 rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000232 if( sqlite_malloc_failed ){
233 goto no_mem_on_open;
234 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
drh58b95762000-06-02 01:17:37 +0000235 sqlite_close(db);
236 return 0;
drh4c504392000-10-16 22:06:40 +0000237 }else /* if( pzErrMsg ) */{
drhdaffd0e2001-04-11 14:28:42 +0000238 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000239 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000240 }
drh75897232000-05-29 14:26:00 +0000241 return db;
drhdaffd0e2001-04-11 14:28:42 +0000242
243no_mem_on_open:
244 sqliteSetString(pzErrMsg, "out of memory", 0);
245 sqliteStrRealloc(pzErrMsg);
246 return 0;
drh75897232000-05-29 14:26:00 +0000247}
248
249/*
250** Close an existing SQLite database
251*/
252void sqlite_close(sqlite *db){
253 int i;
drhae85dc82001-01-13 14:34:05 +0000254 db->pBe->x->Close(db->pBe);
drh75897232000-05-29 14:26:00 +0000255 for(i=0; i<N_HASH; i++){
256 Table *pNext, *pList = db->apTblHash[i];
257 db->apTblHash[i] = 0;
258 while( pList ){
259 pNext = pList->pHash;
260 pList->pHash = 0;
261 sqliteDeleteTable(db, pList);
262 pList = pNext;
263 }
264 }
265 sqliteFree(db);
266}
267
268/*
269** Return TRUE if the given SQL string ends in a semicolon.
270*/
271int sqlite_complete(const char *zSql){
drh8c82b352000-12-10 18:23:50 +0000272 int isComplete = 0;
273 while( *zSql ){
274 switch( *zSql ){
275 case ';': {
276 isComplete = 1;
drh75897232000-05-29 14:26:00 +0000277 break;
drh8c82b352000-12-10 18:23:50 +0000278 }
279 case ' ':
280 case '\t':
281 case '\n':
282 case '\f': {
drh75897232000-05-29 14:26:00 +0000283 break;
drh8c82b352000-12-10 18:23:50 +0000284 }
285 case '\'': {
286 isComplete = 0;
287 zSql++;
288 while( *zSql && *zSql!='\'' ){ zSql++; }
289 if( *zSql==0 ) return 0;
drh75897232000-05-29 14:26:00 +0000290 break;
drh8c82b352000-12-10 18:23:50 +0000291 }
292 case '"': {
293 isComplete = 0;
294 zSql++;
295 while( *zSql && *zSql!='"' ){ zSql++; }
296 if( *zSql==0 ) return 0;
297 break;
298 }
299 case '-': {
300 if( zSql[1]!='-' ){
301 isComplete = 0;
302 break;
303 }
304 while( *zSql && *zSql!='\n' ){ zSql++; }
305 if( *zSql==0 ) return isComplete;
306 break;
307 }
308 default: {
309 isComplete = 0;
310 break;
311 }
drh75897232000-05-29 14:26:00 +0000312 }
drh8c82b352000-12-10 18:23:50 +0000313 zSql++;
drh75897232000-05-29 14:26:00 +0000314 }
drh8c82b352000-12-10 18:23:50 +0000315 return isComplete;
drh75897232000-05-29 14:26:00 +0000316}
317
318/*
drhbed86902000-06-02 13:27:59 +0000319** Execute SQL code. Return one of the SQLITE_ success/failure
320** codes. Also write an error message into memory obtained from
321** malloc() and make *pzErrMsg point to that message.
322**
323** If the SQL is a query, then for each row in the query result
324** the xCallback() function is called. pArg becomes the first
325** argument to xCallback(). If xCallback=NULL then no callback
326** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000327*/
328int sqlite_exec(
329 sqlite *db, /* The database on which the SQL executes */
330 char *zSql, /* The SQL to be executed */
331 sqlite_callback xCallback, /* Invoke this callback routine */
332 void *pArg, /* First argument to xCallback() */
333 char **pzErrMsg /* Write error messages here */
334){
335 Parse sParse;
drh75897232000-05-29 14:26:00 +0000336
337 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000338 if( (db->flags & SQLITE_Initialized)==0 ){
339 int rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000340 if( rc!=SQLITE_OK ){
341 sqliteStrRealloc(pzErrMsg);
342 return rc;
343 }
drh58b95762000-06-02 01:17:37 +0000344 }
drh75897232000-05-29 14:26:00 +0000345 memset(&sParse, 0, sizeof(sParse));
346 sParse.db = db;
347 sParse.xCallback = xCallback;
348 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000349 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000350 if( sqlite_malloc_failed ){
351 sqliteSetString(pzErrMsg, "out of memory", 0);
352 sParse.rc = SQLITE_NOMEM;
353 }
354 sqliteStrRealloc(pzErrMsg);
drh4c504392000-10-16 22:06:40 +0000355 return sParse.rc;
drh75897232000-05-29 14:26:00 +0000356}
drh2dfbbca2000-07-28 14:32:48 +0000357
358/*
359** This routine implements a busy callback that sleeps and tries
360** again until a timeout value is reached. The timeout value is
361** an integer number of milliseconds passed in as the first
362** argument.
363*/
drhdaffd0e2001-04-11 14:28:42 +0000364static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000365 void *Timeout, /* Maximum amount of time to wait */
366 const char *NotUsed, /* The name of the table that is busy */
367 int count /* Number of times table has been busy */
368){
drh82ad3832000-07-31 13:38:24 +0000369#if defined(HAVE_USLEEP) && HAVE_USLEEP
drh2dfbbca2000-07-28 14:32:48 +0000370 int delay = 10000;
371 int prior_delay = 0;
372 int timeout = (int)Timeout;
373 int i;
374
375 for(i=1; i<count; i++){
376 prior_delay += delay;
377 delay = delay*2;
378 if( delay>=1000000 ){
379 delay = 1000000;
380 prior_delay += 1000000*(count - i - 1);
381 break;
382 }
383 }
384 if( prior_delay + delay > timeout*1000 ){
385 delay = timeout*1000 - prior_delay;
386 if( delay<=0 ) return 0;
387 }
388 usleep(delay);
389 return 1;
390#else
391 int timeout = (int)Timeout;
392 if( (count+1)*1000 > timeout ){
393 return 0;
394 }
395 sleep(1);
396 return 1;
397#endif
398}
399
400/*
401** This routine sets the busy callback for an Sqlite database to the
402** given callback function with the given argument.
403*/
404void sqlite_busy_handler(
405 sqlite *db,
406 int (*xBusy)(void*,const char*,int),
407 void *pArg
408){
409 db->xBusyCallback = xBusy;
410 db->pBusyArg = pArg;
411}
412
413/*
414** This routine installs a default busy handler that waits for the
415** specified number of milliseconds before returning 0.
416*/
417void sqlite_busy_timeout(sqlite *db, int ms){
418 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000419 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000420 }else{
421 sqlite_busy_handler(db, 0, 0);
422 }
423}
drh4c504392000-10-16 22:06:40 +0000424
425/*
426** Cause any pending operation to stop at its earliest opportunity.
427*/
428void sqlite_interrupt(sqlite *db){
429 db->flags |= SQLITE_Interrupt;
430}