blob: c882abf0cca43f6b20f775b8c559741e6f261296 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** Main file for the SQLite library. The routines in this file
13** implement the programmer interface to the library. Routines in
14** other files are for internal use by SQLite and should not be
15** accessed by users of the library.
16**
drhb19a2bc2001-09-16 00:13:26 +000017** $Id: main.c,v 1.37 2001/09/16 00:13:27 drh Exp $
drh75897232000-05-29 14:26:00 +000018*/
19#include "sqliteInt.h"
drh7e3b0a02001-04-28 16:52:40 +000020#if defined(HAVE_USLEEP) && HAVE_USLEEP
drh345fda32001-01-15 22:51:08 +000021#include <unistd.h>
drh7e3b0a02001-04-28 16:52:40 +000022#endif
drh75897232000-05-29 14:26:00 +000023
24/*
25** This is the callback routine for the code that initializes the
drhd78eeee2001-09-13 16:18:53 +000026** database. Each callback contains the following information:
drh28037572000-08-02 13:47:41 +000027**
drhd78eeee2001-09-13 16:18:53 +000028** argv[0] = "meta" or "table" or "index"
29** argv[1] = table or index name
30** argv[2] = root page number for table or index
31** argv[3] = SQL create statement for the table or index
32**
drh75897232000-05-29 14:26:00 +000033*/
34static int sqliteOpenCb(void *pDb, int argc, char **argv, char **azColName){
35 sqlite *db = (sqlite*)pDb;
36 Parse sParse;
drhd78eeee2001-09-13 16:18:53 +000037 int nErr = 0;
drh75897232000-05-29 14:26:00 +000038
drhd78eeee2001-09-13 16:18:53 +000039 assert( argc==4 );
40 switch( argv[0][0] ){
41 case 'm': { /* Meta information */
drh50e5dad2001-09-15 00:57:28 +000042 if( strcmp(argv[1],"file-format")==0 ){
43 db->file_format = atoi(argv[3]);
44 }else if( strcmp(argv[1],"schema-cookie")==0 ){
45 db->schema_cookie = atoi(argv[3]);
46 db->next_cookie = db->schema_cookie;
47 }
drhd78eeee2001-09-13 16:18:53 +000048 break;
drh28037572000-08-02 13:47:41 +000049 }
drhd78eeee2001-09-13 16:18:53 +000050 case 'i':
51 case 't': { /* CREATE TABLE and CREATE INDEX statements */
52 memset(&sParse, 0, sizeof(sParse));
53 sParse.db = db;
54 sParse.initFlag = 1;
55 sParse.newTnum = atoi(argv[2]);
56 nErr = sqliteRunParser(&sParse, argv[3], 0);
57 break;
58 }
59 default: {
60 /* This can not happen! */
61 nErr = 1;
62 assert( nErr==0 );
63 }
drh28037572000-08-02 13:47:41 +000064 }
drh75897232000-05-29 14:26:00 +000065 return nErr;
66}
67
68/*
drh58b95762000-06-02 01:17:37 +000069** Attempt to read the database schema and initialize internal
70** data structures. Return one of the SQLITE_ error codes to
71** indicate success or failure.
drhbed86902000-06-02 13:27:59 +000072**
73** After the database is initialized, the SQLITE_Initialized
74** bit is set in the flags field of the sqlite structure. An
75** attempt is made to initialize the database as soon as it
76** is opened. If that fails (perhaps because another process
77** has the sqlite_master table locked) than another attempt
78** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +000079*/
drh58b95762000-06-02 01:17:37 +000080static int sqliteInit(sqlite *db, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +000081 Vdbe *vdbe;
drh58b95762000-06-02 01:17:37 +000082 int rc;
83
84 /*
85 ** The master database table has a structure like this
86 */
drh75897232000-05-29 14:26:00 +000087 static char master_schema[] =
88 "CREATE TABLE " MASTER_NAME " (\n"
89 " type text,\n"
90 " name text,\n"
drh5e00f6c2001-09-13 13:46:56 +000091 " tnum integer,\n"
drh75897232000-05-29 14:26:00 +000092 " tbl_name text,\n"
93 " sql text\n"
94 ")"
95 ;
96
97 /* The following program is used to initialize the internal
98 ** structure holding the tables and indexes of the database.
99 ** The database contains a special table named "sqlite_master"
100 ** defined as follows:
101 **
102 ** CREATE TABLE sqlite_master (
drh28037572000-08-02 13:47:41 +0000103 ** type text, -- Either "table" or "index" or "meta"
drh75897232000-05-29 14:26:00 +0000104 ** name text, -- Name of table or index
drh5e00f6c2001-09-13 13:46:56 +0000105 ** tnum integer, -- The integer page number of root page
drh75897232000-05-29 14:26:00 +0000106 ** tbl_name text, -- Associated table
107 ** sql text -- The CREATE statement for this object
108 ** );
109 **
110 ** The sqlite_master table contains a single entry for each table
drh967e8b72000-06-21 13:59:10 +0000111 ** and each index. The "type" column tells whether the entry is
112 ** a table or index. The "name" column is the name of the object.
drh75897232000-05-29 14:26:00 +0000113 ** The "tbl_name" is the name of the associated table. For tables,
drh967e8b72000-06-21 13:59:10 +0000114 ** the tbl_name column is always the same as name. For indices, the
115 ** tbl_name column contains the name of the table that the index
116 ** indexes. Finally, the "sql" column contains the complete text of
drh75897232000-05-29 14:26:00 +0000117 ** the CREATE TABLE or CREATE INDEX statement that originally created
118 ** the table or index.
119 **
drh28037572000-08-02 13:47:41 +0000120 ** If the "type" column has the value "meta", then the "sql" column
121 ** contains extra information about the database, such as the
122 ** file format version number. All meta information must be processed
123 ** before any tables or indices are constructed.
124 **
drh75897232000-05-29 14:26:00 +0000125 ** The following program invokes its callback on the SQL for each
126 ** table then goes back and invokes the callback on the
127 ** SQL for each index. The callback will invoke the
128 ** parser to build the internal representation of the
129 ** database scheme.
130 */
131 static VdbeOp initProg[] = {
drh5e00f6c2001-09-13 13:46:56 +0000132 { OP_Open, 0, 2, 0},
drhd78eeee2001-09-13 16:18:53 +0000133 { OP_Rewind, 0, 0, 0},
134 { OP_Next, 0, 12, 0}, /* 2 */
drh5e00f6c2001-09-13 13:46:56 +0000135 { OP_Column, 0, 0, 0},
drh28037572000-08-02 13:47:41 +0000136 { OP_String, 0, 0, "meta"},
drhd78eeee2001-09-13 16:18:53 +0000137 { OP_Ne, 0, 2, 0},
drh5e00f6c2001-09-13 13:46:56 +0000138 { OP_Column, 0, 0, 0},
drhd78eeee2001-09-13 16:18:53 +0000139 { OP_Column, 0, 1, 0},
140 { OP_Column, 0, 2, 0},
drh5e00f6c2001-09-13 13:46:56 +0000141 { OP_Column, 0, 4, 0},
drhd78eeee2001-09-13 16:18:53 +0000142 { OP_Callback, 4, 0, 0},
143 { OP_Goto, 0, 2, 0},
144 { OP_Rewind, 0, 0, 0}, /* 12 */
145 { OP_Next, 0, 23, 0}, /* 13 */
drh5e00f6c2001-09-13 13:46:56 +0000146 { OP_Column, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000147 { OP_String, 0, 0, "table"},
drhd78eeee2001-09-13 16:18:53 +0000148 { OP_Ne, 0, 13, 0},
149 { OP_Column, 0, 0, 0},
150 { OP_Column, 0, 1, 0},
151 { OP_Column, 0, 2, 0},
drh5e00f6c2001-09-13 13:46:56 +0000152 { OP_Column, 0, 4, 0},
drhd78eeee2001-09-13 16:18:53 +0000153 { OP_Callback, 4, 0, 0},
154 { OP_Goto, 0, 13, 0},
155 { OP_Rewind, 0, 0, 0}, /* 23 */
156 { OP_Next, 0, 34, 0}, /* 24 */
drh5e00f6c2001-09-13 13:46:56 +0000157 { OP_Column, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000158 { OP_String, 0, 0, "index"},
drhd78eeee2001-09-13 16:18:53 +0000159 { OP_Ne, 0, 24, 0},
160 { OP_Column, 0, 0, 0},
161 { OP_Column, 0, 1, 0},
162 { OP_Column, 0, 2, 0},
drh5e00f6c2001-09-13 13:46:56 +0000163 { OP_Column, 0, 4, 0},
drhd78eeee2001-09-13 16:18:53 +0000164 { OP_Callback, 4, 0, 0},
165 { OP_Goto, 0, 24, 0},
drh50e5dad2001-09-15 00:57:28 +0000166 { OP_String, 0, 0, "meta"}, /* 34 */
167 { OP_String, 0, 0, "schema-cookie"},
168 { OP_String, 0, 0, ""},
169 { OP_ReadCookie,0,0, 0},
170 { OP_Callback, 4, 0, 0},
171 { OP_Close, 0, 0, 0},
drh5e00f6c2001-09-13 13:46:56 +0000172 { OP_Halt, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000173 };
174
drh58b95762000-06-02 01:17:37 +0000175 /* Create a virtual machine to run the initialization program. Run
176 ** the program. The delete the virtual machine.
177 */
drh4c504392000-10-16 22:06:40 +0000178 vdbe = sqliteVdbeCreate(db);
drhd8bc7082000-06-07 23:51:50 +0000179 if( vdbe==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000180 sqliteSetString(pzErrMsg, "out of memory");
181 return SQLITE_NOMEM;
drhd8bc7082000-06-07 23:51:50 +0000182 }
drh58b95762000-06-02 01:17:37 +0000183 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
drh2dfbbca2000-07-28 14:32:48 +0000184 rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg,
185 db->pBusyArg, db->xBusyCallback);
drh58b95762000-06-02 01:17:37 +0000186 sqliteVdbeDelete(vdbe);
drhd78eeee2001-09-13 16:18:53 +0000187 if( rc==SQLITE_OK && db->file_format>1 && db->nTable>0 ){
188 sqliteSetString(pzErrMsg, "unsupported file format", 0);
drh28037572000-08-02 13:47:41 +0000189 rc = SQLITE_ERROR;
190 }
drh58b95762000-06-02 01:17:37 +0000191 if( rc==SQLITE_OK ){
192 Table *pTab;
drhd78eeee2001-09-13 16:18:53 +0000193 char *azArg[5];
194 azArg[0] = "table";
195 azArg[1] = MASTER_NAME;
196 azArg[2] = "2";
197 azArg[3] = master_schema;
198 azArg[4] = 0;
199 sqliteOpenCb(db, 4, azArg, 0);
drh58b95762000-06-02 01:17:37 +0000200 pTab = sqliteFindTable(db, MASTER_NAME);
201 if( pTab ){
202 pTab->readOnly = 1;
203 }
204 db->flags |= SQLITE_Initialized;
drh5e00f6c2001-09-13 13:46:56 +0000205 sqliteCommitInternalChanges(db);
drh58b95762000-06-02 01:17:37 +0000206 }
207 return rc;
208}
209
210/*
drhb217a572000-08-22 13:40:18 +0000211** The version of the library
212*/
drh3d0b5592000-08-22 13:40:51 +0000213const char sqlite_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000214
215/*
drh297ecf12001-04-05 15:57:13 +0000216** Does the library expect data to be encoded as UTF-8 or iso8859? The
217** following global constant always lets us know.
218*/
219#ifdef SQLITE_UTF8
drhfbc3eab2001-04-06 16:13:42 +0000220const char sqlite_encoding[] = "UTF-8";
drh297ecf12001-04-05 15:57:13 +0000221#else
drhfbc3eab2001-04-06 16:13:42 +0000222const char sqlite_encoding[] = "iso8859";
drh297ecf12001-04-05 15:57:13 +0000223#endif
224
225/*
drh58b95762000-06-02 01:17:37 +0000226** Open a new SQLite database. Construct an "sqlite" structure to define
227** the state of this database and return a pointer to that structure.
228**
229** An attempt is made to initialize the in-memory data structures that
230** hold the database schema. But if this fails (because the schema file
231** is locked) then that step is deferred until the first call to
232** sqlite_exec().
233*/
234sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
235 sqlite *db;
236 int rc;
237
238 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000239 db = sqliteMalloc( sizeof(sqlite) );
240 if( pzErrMsg ) *pzErrMsg = 0;
drhdaffd0e2001-04-11 14:28:42 +0000241 if( db==0 ) goto no_mem_on_open;
drh75897232000-05-29 14:26:00 +0000242
243 /* Open the backend database driver */
drha1b351a2001-09-14 16:42:12 +0000244 rc = sqliteBtreeOpen(zFilename, mode, MAX_PAGES, &db->pBe);
drh5e00f6c2001-09-13 13:46:56 +0000245 if( rc!=SQLITE_OK ){
246 switch( rc ){
247 default: {
248 if( pzErrMsg ){
249 sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0);
250 }
251 }
252 }
drh75897232000-05-29 14:26:00 +0000253 sqliteFree(db);
drh5edc3122001-09-13 21:53:09 +0000254 sqliteStrRealloc(pzErrMsg);
drhbe0072d2001-09-13 14:46:09 +0000255 return 0;
drh75897232000-05-29 14:26:00 +0000256 }
257
drh28037572000-08-02 13:47:41 +0000258 /* Assume file format 1 unless the database says otherwise */
259 db->file_format = 1;
260
drh58b95762000-06-02 01:17:37 +0000261 /* Attempt to read the schema */
262 rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000263 if( sqlite_malloc_failed ){
264 goto no_mem_on_open;
265 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
drh58b95762000-06-02 01:17:37 +0000266 sqlite_close(db);
drh5edc3122001-09-13 21:53:09 +0000267 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000268 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/*
drh50e5dad2001-09-15 00:57:28 +0000282** Erase all schema information from the schema hash table.
283**
284** The database schema is normally read in once when the database
285** is first opened and stored in a hash table in the sqlite structure.
286** This routine erases the stored schema. This erasure occurs because
287** either the database is being closed or because some other process
288** changed the schema and this process needs to reread it.
drh75897232000-05-29 14:26:00 +0000289*/
drh50e5dad2001-09-15 00:57:28 +0000290static void clearHashTable(sqlite *db){
drh75897232000-05-29 14:26:00 +0000291 int i;
drh75897232000-05-29 14:26:00 +0000292 for(i=0; i<N_HASH; i++){
293 Table *pNext, *pList = db->apTblHash[i];
294 db->apTblHash[i] = 0;
295 while( pList ){
296 pNext = pList->pHash;
297 pList->pHash = 0;
298 sqliteDeleteTable(db, pList);
299 pList = pNext;
300 }
301 }
drh50e5dad2001-09-15 00:57:28 +0000302 db->flags &= ~SQLITE_Initialized;
303}
304
305/*
306** Close an existing SQLite database
307*/
308void sqlite_close(sqlite *db){
309 sqliteBtreeClose(db->pBe);
310 clearHashTable(db);
drh75897232000-05-29 14:26:00 +0000311 sqliteFree(db);
312}
313
314/*
315** Return TRUE if the given SQL string ends in a semicolon.
316*/
317int sqlite_complete(const char *zSql){
drh8c82b352000-12-10 18:23:50 +0000318 int isComplete = 0;
319 while( *zSql ){
320 switch( *zSql ){
321 case ';': {
322 isComplete = 1;
drh75897232000-05-29 14:26:00 +0000323 break;
drh8c82b352000-12-10 18:23:50 +0000324 }
325 case ' ':
326 case '\t':
327 case '\n':
328 case '\f': {
drh75897232000-05-29 14:26:00 +0000329 break;
drh8c82b352000-12-10 18:23:50 +0000330 }
331 case '\'': {
332 isComplete = 0;
333 zSql++;
334 while( *zSql && *zSql!='\'' ){ zSql++; }
335 if( *zSql==0 ) return 0;
drh75897232000-05-29 14:26:00 +0000336 break;
drh8c82b352000-12-10 18:23:50 +0000337 }
338 case '"': {
339 isComplete = 0;
340 zSql++;
341 while( *zSql && *zSql!='"' ){ zSql++; }
342 if( *zSql==0 ) return 0;
343 break;
344 }
345 case '-': {
346 if( zSql[1]!='-' ){
347 isComplete = 0;
348 break;
349 }
350 while( *zSql && *zSql!='\n' ){ zSql++; }
351 if( *zSql==0 ) return isComplete;
352 break;
353 }
354 default: {
355 isComplete = 0;
356 break;
357 }
drh75897232000-05-29 14:26:00 +0000358 }
drh8c82b352000-12-10 18:23:50 +0000359 zSql++;
drh75897232000-05-29 14:26:00 +0000360 }
drh8c82b352000-12-10 18:23:50 +0000361 return isComplete;
drh75897232000-05-29 14:26:00 +0000362}
363
364/*
drhbed86902000-06-02 13:27:59 +0000365** Execute SQL code. Return one of the SQLITE_ success/failure
366** codes. Also write an error message into memory obtained from
367** malloc() and make *pzErrMsg point to that message.
368**
369** If the SQL is a query, then for each row in the query result
370** the xCallback() function is called. pArg becomes the first
371** argument to xCallback(). If xCallback=NULL then no callback
372** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000373*/
374int sqlite_exec(
375 sqlite *db, /* The database on which the SQL executes */
376 char *zSql, /* The SQL to be executed */
377 sqlite_callback xCallback, /* Invoke this callback routine */
378 void *pArg, /* First argument to xCallback() */
379 char **pzErrMsg /* Write error messages here */
380){
381 Parse sParse;
drh75897232000-05-29 14:26:00 +0000382
383 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000384 if( (db->flags & SQLITE_Initialized)==0 ){
385 int rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000386 if( rc!=SQLITE_OK ){
387 sqliteStrRealloc(pzErrMsg);
388 return rc;
389 }
drh58b95762000-06-02 01:17:37 +0000390 }
drh75897232000-05-29 14:26:00 +0000391 memset(&sParse, 0, sizeof(sParse));
392 sParse.db = db;
drh5e00f6c2001-09-13 13:46:56 +0000393 sParse.pBe = db->pBe;
drh75897232000-05-29 14:26:00 +0000394 sParse.xCallback = xCallback;
395 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000396 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000397 if( sqlite_malloc_failed ){
398 sqliteSetString(pzErrMsg, "out of memory", 0);
399 sParse.rc = SQLITE_NOMEM;
400 }
401 sqliteStrRealloc(pzErrMsg);
drh50e5dad2001-09-15 00:57:28 +0000402 if( sParse.rc==SQLITE_SCHEMA ){
403 clearHashTable(db);
404 }
drh4c504392000-10-16 22:06:40 +0000405 return sParse.rc;
drh75897232000-05-29 14:26:00 +0000406}
drh2dfbbca2000-07-28 14:32:48 +0000407
408/*
409** This routine implements a busy callback that sleeps and tries
410** again until a timeout value is reached. The timeout value is
411** an integer number of milliseconds passed in as the first
412** argument.
413*/
drhdaffd0e2001-04-11 14:28:42 +0000414static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000415 void *Timeout, /* Maximum amount of time to wait */
416 const char *NotUsed, /* The name of the table that is busy */
417 int count /* Number of times table has been busy */
418){
drh82ad3832000-07-31 13:38:24 +0000419#if defined(HAVE_USLEEP) && HAVE_USLEEP
drh2dfbbca2000-07-28 14:32:48 +0000420 int delay = 10000;
421 int prior_delay = 0;
422 int timeout = (int)Timeout;
423 int i;
424
425 for(i=1; i<count; i++){
426 prior_delay += delay;
427 delay = delay*2;
428 if( delay>=1000000 ){
429 delay = 1000000;
430 prior_delay += 1000000*(count - i - 1);
431 break;
432 }
433 }
434 if( prior_delay + delay > timeout*1000 ){
435 delay = timeout*1000 - prior_delay;
436 if( delay<=0 ) return 0;
437 }
438 usleep(delay);
439 return 1;
440#else
441 int timeout = (int)Timeout;
442 if( (count+1)*1000 > timeout ){
443 return 0;
444 }
445 sleep(1);
446 return 1;
447#endif
448}
449
450/*
451** This routine sets the busy callback for an Sqlite database to the
452** given callback function with the given argument.
453*/
454void sqlite_busy_handler(
455 sqlite *db,
456 int (*xBusy)(void*,const char*,int),
457 void *pArg
458){
459 db->xBusyCallback = xBusy;
460 db->pBusyArg = pArg;
461}
462
463/*
464** This routine installs a default busy handler that waits for the
465** specified number of milliseconds before returning 0.
466*/
467void sqlite_busy_timeout(sqlite *db, int ms){
468 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000469 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000470 }else{
471 sqlite_busy_handler(db, 0, 0);
472 }
473}
drh4c504392000-10-16 22:06:40 +0000474
475/*
476** Cause any pending operation to stop at its earliest opportunity.
477*/
478void sqlite_interrupt(sqlite *db){
479 db->flags |= SQLITE_Interrupt;
480}