blob: e3863e2131562ad730293057f565b9b642d33709 [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**
drh50e5dad2001-09-15 00:57:28 +000029** $Id: main.c,v 1.36 2001/09/15 00:57:29 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 */
drh50e5dad2001-09-15 00:57:28 +000054 if( strcmp(argv[1],"file-format")==0 ){
55 db->file_format = atoi(argv[3]);
56 }else if( strcmp(argv[1],"schema-cookie")==0 ){
57 db->schema_cookie = atoi(argv[3]);
58 db->next_cookie = db->schema_cookie;
59 }
drhd78eeee2001-09-13 16:18:53 +000060 break;
drh28037572000-08-02 13:47:41 +000061 }
drhd78eeee2001-09-13 16:18:53 +000062 case 'i':
63 case 't': { /* CREATE TABLE and CREATE INDEX statements */
64 memset(&sParse, 0, sizeof(sParse));
65 sParse.db = db;
66 sParse.initFlag = 1;
67 sParse.newTnum = atoi(argv[2]);
68 nErr = sqliteRunParser(&sParse, argv[3], 0);
69 break;
70 }
71 default: {
72 /* This can not happen! */
73 nErr = 1;
74 assert( nErr==0 );
75 }
drh28037572000-08-02 13:47:41 +000076 }
drh75897232000-05-29 14:26:00 +000077 return nErr;
78}
79
80/*
drh58b95762000-06-02 01:17:37 +000081** Attempt to read the database schema and initialize internal
82** data structures. Return one of the SQLITE_ error codes to
83** indicate success or failure.
drhbed86902000-06-02 13:27:59 +000084**
85** After the database is initialized, the SQLITE_Initialized
86** bit is set in the flags field of the sqlite structure. An
87** attempt is made to initialize the database as soon as it
88** is opened. If that fails (perhaps because another process
89** has the sqlite_master table locked) than another attempt
90** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +000091*/
drh58b95762000-06-02 01:17:37 +000092static int sqliteInit(sqlite *db, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +000093 Vdbe *vdbe;
drh58b95762000-06-02 01:17:37 +000094 int rc;
95
96 /*
97 ** The master database table has a structure like this
98 */
drh75897232000-05-29 14:26:00 +000099 static char master_schema[] =
100 "CREATE TABLE " MASTER_NAME " (\n"
101 " type text,\n"
102 " name text,\n"
drh5e00f6c2001-09-13 13:46:56 +0000103 " tnum integer,\n"
drh75897232000-05-29 14:26:00 +0000104 " tbl_name text,\n"
105 " sql text\n"
106 ")"
107 ;
108
109 /* The following program is used to initialize the internal
110 ** structure holding the tables and indexes of the database.
111 ** The database contains a special table named "sqlite_master"
112 ** defined as follows:
113 **
114 ** CREATE TABLE sqlite_master (
drh28037572000-08-02 13:47:41 +0000115 ** type text, -- Either "table" or "index" or "meta"
drh75897232000-05-29 14:26:00 +0000116 ** name text, -- Name of table or index
drh5e00f6c2001-09-13 13:46:56 +0000117 ** tnum integer, -- The integer page number of root page
drh75897232000-05-29 14:26:00 +0000118 ** tbl_name text, -- Associated table
119 ** sql text -- The CREATE statement for this object
120 ** );
121 **
122 ** The sqlite_master table contains a single entry for each table
drh967e8b72000-06-21 13:59:10 +0000123 ** and each index. The "type" column tells whether the entry is
124 ** a table or index. The "name" column is the name of the object.
drh75897232000-05-29 14:26:00 +0000125 ** The "tbl_name" is the name of the associated table. For tables,
drh967e8b72000-06-21 13:59:10 +0000126 ** the tbl_name column is always the same as name. For indices, the
127 ** tbl_name column contains the name of the table that the index
128 ** indexes. Finally, the "sql" column contains the complete text of
drh75897232000-05-29 14:26:00 +0000129 ** the CREATE TABLE or CREATE INDEX statement that originally created
130 ** the table or index.
131 **
drh28037572000-08-02 13:47:41 +0000132 ** If the "type" column has the value "meta", then the "sql" column
133 ** contains extra information about the database, such as the
134 ** file format version number. All meta information must be processed
135 ** before any tables or indices are constructed.
136 **
drh75897232000-05-29 14:26:00 +0000137 ** The following program invokes its callback on the SQL for each
138 ** table then goes back and invokes the callback on the
139 ** SQL for each index. The callback will invoke the
140 ** parser to build the internal representation of the
141 ** database scheme.
142 */
143 static VdbeOp initProg[] = {
drh5e00f6c2001-09-13 13:46:56 +0000144 { OP_Open, 0, 2, 0},
drhd78eeee2001-09-13 16:18:53 +0000145 { OP_Rewind, 0, 0, 0},
146 { OP_Next, 0, 12, 0}, /* 2 */
drh5e00f6c2001-09-13 13:46:56 +0000147 { OP_Column, 0, 0, 0},
drh28037572000-08-02 13:47:41 +0000148 { OP_String, 0, 0, "meta"},
drhd78eeee2001-09-13 16:18:53 +0000149 { OP_Ne, 0, 2, 0},
drh5e00f6c2001-09-13 13:46:56 +0000150 { OP_Column, 0, 0, 0},
drhd78eeee2001-09-13 16:18:53 +0000151 { OP_Column, 0, 1, 0},
152 { OP_Column, 0, 2, 0},
drh5e00f6c2001-09-13 13:46:56 +0000153 { OP_Column, 0, 4, 0},
drhd78eeee2001-09-13 16:18:53 +0000154 { OP_Callback, 4, 0, 0},
155 { OP_Goto, 0, 2, 0},
156 { OP_Rewind, 0, 0, 0}, /* 12 */
157 { OP_Next, 0, 23, 0}, /* 13 */
drh5e00f6c2001-09-13 13:46:56 +0000158 { OP_Column, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000159 { OP_String, 0, 0, "table"},
drhd78eeee2001-09-13 16:18:53 +0000160 { OP_Ne, 0, 13, 0},
161 { OP_Column, 0, 0, 0},
162 { OP_Column, 0, 1, 0},
163 { OP_Column, 0, 2, 0},
drh5e00f6c2001-09-13 13:46:56 +0000164 { OP_Column, 0, 4, 0},
drhd78eeee2001-09-13 16:18:53 +0000165 { OP_Callback, 4, 0, 0},
166 { OP_Goto, 0, 13, 0},
167 { OP_Rewind, 0, 0, 0}, /* 23 */
168 { OP_Next, 0, 34, 0}, /* 24 */
drh5e00f6c2001-09-13 13:46:56 +0000169 { OP_Column, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000170 { OP_String, 0, 0, "index"},
drhd78eeee2001-09-13 16:18:53 +0000171 { OP_Ne, 0, 24, 0},
172 { OP_Column, 0, 0, 0},
173 { OP_Column, 0, 1, 0},
174 { OP_Column, 0, 2, 0},
drh5e00f6c2001-09-13 13:46:56 +0000175 { OP_Column, 0, 4, 0},
drhd78eeee2001-09-13 16:18:53 +0000176 { OP_Callback, 4, 0, 0},
177 { OP_Goto, 0, 24, 0},
drh50e5dad2001-09-15 00:57:28 +0000178 { OP_String, 0, 0, "meta"}, /* 34 */
179 { OP_String, 0, 0, "schema-cookie"},
180 { OP_String, 0, 0, ""},
181 { OP_ReadCookie,0,0, 0},
182 { OP_Callback, 4, 0, 0},
183 { OP_Close, 0, 0, 0},
drh5e00f6c2001-09-13 13:46:56 +0000184 { OP_Halt, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000185 };
186
drh58b95762000-06-02 01:17:37 +0000187 /* Create a virtual machine to run the initialization program. Run
188 ** the program. The delete the virtual machine.
189 */
drh4c504392000-10-16 22:06:40 +0000190 vdbe = sqliteVdbeCreate(db);
drhd8bc7082000-06-07 23:51:50 +0000191 if( vdbe==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000192 sqliteSetString(pzErrMsg, "out of memory");
193 return SQLITE_NOMEM;
drhd8bc7082000-06-07 23:51:50 +0000194 }
drh58b95762000-06-02 01:17:37 +0000195 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
drh2dfbbca2000-07-28 14:32:48 +0000196 rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg,
197 db->pBusyArg, db->xBusyCallback);
drh58b95762000-06-02 01:17:37 +0000198 sqliteVdbeDelete(vdbe);
drhd78eeee2001-09-13 16:18:53 +0000199 if( rc==SQLITE_OK && db->file_format>1 && db->nTable>0 ){
200 sqliteSetString(pzErrMsg, "unsupported file format", 0);
drh28037572000-08-02 13:47:41 +0000201 rc = SQLITE_ERROR;
202 }
drh58b95762000-06-02 01:17:37 +0000203 if( rc==SQLITE_OK ){
204 Table *pTab;
drhd78eeee2001-09-13 16:18:53 +0000205 char *azArg[5];
206 azArg[0] = "table";
207 azArg[1] = MASTER_NAME;
208 azArg[2] = "2";
209 azArg[3] = master_schema;
210 azArg[4] = 0;
211 sqliteOpenCb(db, 4, azArg, 0);
drh58b95762000-06-02 01:17:37 +0000212 pTab = sqliteFindTable(db, MASTER_NAME);
213 if( pTab ){
214 pTab->readOnly = 1;
215 }
216 db->flags |= SQLITE_Initialized;
drh5e00f6c2001-09-13 13:46:56 +0000217 sqliteCommitInternalChanges(db);
drh58b95762000-06-02 01:17:37 +0000218 }
219 return rc;
220}
221
222/*
drhb217a572000-08-22 13:40:18 +0000223** The version of the library
224*/
drh3d0b5592000-08-22 13:40:51 +0000225const char sqlite_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000226
227/*
drh297ecf12001-04-05 15:57:13 +0000228** Does the library expect data to be encoded as UTF-8 or iso8859? The
229** following global constant always lets us know.
230*/
231#ifdef SQLITE_UTF8
drhfbc3eab2001-04-06 16:13:42 +0000232const char sqlite_encoding[] = "UTF-8";
drh297ecf12001-04-05 15:57:13 +0000233#else
drhfbc3eab2001-04-06 16:13:42 +0000234const char sqlite_encoding[] = "iso8859";
drh297ecf12001-04-05 15:57:13 +0000235#endif
236
237/*
drh58b95762000-06-02 01:17:37 +0000238** Open a new SQLite database. Construct an "sqlite" structure to define
239** the state of this database and return a pointer to that structure.
240**
241** An attempt is made to initialize the in-memory data structures that
242** hold the database schema. But if this fails (because the schema file
243** is locked) then that step is deferred until the first call to
244** sqlite_exec().
245*/
246sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
247 sqlite *db;
248 int rc;
249
250 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000251 db = sqliteMalloc( sizeof(sqlite) );
252 if( pzErrMsg ) *pzErrMsg = 0;
drhdaffd0e2001-04-11 14:28:42 +0000253 if( db==0 ) goto no_mem_on_open;
drh75897232000-05-29 14:26:00 +0000254
255 /* Open the backend database driver */
drha1b351a2001-09-14 16:42:12 +0000256 rc = sqliteBtreeOpen(zFilename, mode, MAX_PAGES, &db->pBe);
drh5e00f6c2001-09-13 13:46:56 +0000257 if( rc!=SQLITE_OK ){
258 switch( rc ){
259 default: {
260 if( pzErrMsg ){
261 sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0);
262 }
263 }
264 }
drh75897232000-05-29 14:26:00 +0000265 sqliteFree(db);
drh5edc3122001-09-13 21:53:09 +0000266 sqliteStrRealloc(pzErrMsg);
drhbe0072d2001-09-13 14:46:09 +0000267 return 0;
drh75897232000-05-29 14:26:00 +0000268 }
269
drh28037572000-08-02 13:47:41 +0000270 /* Assume file format 1 unless the database says otherwise */
271 db->file_format = 1;
272
drh58b95762000-06-02 01:17:37 +0000273 /* Attempt to read the schema */
274 rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000275 if( sqlite_malloc_failed ){
276 goto no_mem_on_open;
277 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
drh58b95762000-06-02 01:17:37 +0000278 sqlite_close(db);
drh5edc3122001-09-13 21:53:09 +0000279 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000280 return 0;
drh4c504392000-10-16 22:06:40 +0000281 }else /* if( pzErrMsg ) */{
drhdaffd0e2001-04-11 14:28:42 +0000282 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000283 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000284 }
drh75897232000-05-29 14:26:00 +0000285 return db;
drhdaffd0e2001-04-11 14:28:42 +0000286
287no_mem_on_open:
288 sqliteSetString(pzErrMsg, "out of memory", 0);
289 sqliteStrRealloc(pzErrMsg);
290 return 0;
drh75897232000-05-29 14:26:00 +0000291}
292
293/*
drh50e5dad2001-09-15 00:57:28 +0000294** Erase all schema information from the schema hash table.
295**
296** The database schema is normally read in once when the database
297** is first opened and stored in a hash table in the sqlite structure.
298** This routine erases the stored schema. This erasure occurs because
299** either the database is being closed or because some other process
300** changed the schema and this process needs to reread it.
drh75897232000-05-29 14:26:00 +0000301*/
drh50e5dad2001-09-15 00:57:28 +0000302static void clearHashTable(sqlite *db){
drh75897232000-05-29 14:26:00 +0000303 int i;
drh75897232000-05-29 14:26:00 +0000304 for(i=0; i<N_HASH; i++){
305 Table *pNext, *pList = db->apTblHash[i];
306 db->apTblHash[i] = 0;
307 while( pList ){
308 pNext = pList->pHash;
309 pList->pHash = 0;
310 sqliteDeleteTable(db, pList);
311 pList = pNext;
312 }
313 }
drh50e5dad2001-09-15 00:57:28 +0000314 db->flags &= ~SQLITE_Initialized;
315}
316
317/*
318** Close an existing SQLite database
319*/
320void sqlite_close(sqlite *db){
321 sqliteBtreeClose(db->pBe);
322 clearHashTable(db);
drh75897232000-05-29 14:26:00 +0000323 sqliteFree(db);
324}
325
326/*
327** Return TRUE if the given SQL string ends in a semicolon.
328*/
329int sqlite_complete(const char *zSql){
drh8c82b352000-12-10 18:23:50 +0000330 int isComplete = 0;
331 while( *zSql ){
332 switch( *zSql ){
333 case ';': {
334 isComplete = 1;
drh75897232000-05-29 14:26:00 +0000335 break;
drh8c82b352000-12-10 18:23:50 +0000336 }
337 case ' ':
338 case '\t':
339 case '\n':
340 case '\f': {
drh75897232000-05-29 14:26:00 +0000341 break;
drh8c82b352000-12-10 18:23:50 +0000342 }
343 case '\'': {
344 isComplete = 0;
345 zSql++;
346 while( *zSql && *zSql!='\'' ){ zSql++; }
347 if( *zSql==0 ) return 0;
drh75897232000-05-29 14:26:00 +0000348 break;
drh8c82b352000-12-10 18:23:50 +0000349 }
350 case '"': {
351 isComplete = 0;
352 zSql++;
353 while( *zSql && *zSql!='"' ){ zSql++; }
354 if( *zSql==0 ) return 0;
355 break;
356 }
357 case '-': {
358 if( zSql[1]!='-' ){
359 isComplete = 0;
360 break;
361 }
362 while( *zSql && *zSql!='\n' ){ zSql++; }
363 if( *zSql==0 ) return isComplete;
364 break;
365 }
366 default: {
367 isComplete = 0;
368 break;
369 }
drh75897232000-05-29 14:26:00 +0000370 }
drh8c82b352000-12-10 18:23:50 +0000371 zSql++;
drh75897232000-05-29 14:26:00 +0000372 }
drh8c82b352000-12-10 18:23:50 +0000373 return isComplete;
drh75897232000-05-29 14:26:00 +0000374}
375
376/*
drhbed86902000-06-02 13:27:59 +0000377** Execute SQL code. Return one of the SQLITE_ success/failure
378** codes. Also write an error message into memory obtained from
379** malloc() and make *pzErrMsg point to that message.
380**
381** If the SQL is a query, then for each row in the query result
382** the xCallback() function is called. pArg becomes the first
383** argument to xCallback(). If xCallback=NULL then no callback
384** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000385*/
386int sqlite_exec(
387 sqlite *db, /* The database on which the SQL executes */
388 char *zSql, /* The SQL to be executed */
389 sqlite_callback xCallback, /* Invoke this callback routine */
390 void *pArg, /* First argument to xCallback() */
391 char **pzErrMsg /* Write error messages here */
392){
393 Parse sParse;
drh75897232000-05-29 14:26:00 +0000394
395 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000396 if( (db->flags & SQLITE_Initialized)==0 ){
397 int rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000398 if( rc!=SQLITE_OK ){
399 sqliteStrRealloc(pzErrMsg);
400 return rc;
401 }
drh58b95762000-06-02 01:17:37 +0000402 }
drh75897232000-05-29 14:26:00 +0000403 memset(&sParse, 0, sizeof(sParse));
404 sParse.db = db;
drh5e00f6c2001-09-13 13:46:56 +0000405 sParse.pBe = db->pBe;
drh75897232000-05-29 14:26:00 +0000406 sParse.xCallback = xCallback;
407 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000408 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000409 if( sqlite_malloc_failed ){
410 sqliteSetString(pzErrMsg, "out of memory", 0);
411 sParse.rc = SQLITE_NOMEM;
412 }
413 sqliteStrRealloc(pzErrMsg);
drh50e5dad2001-09-15 00:57:28 +0000414 if( sParse.rc==SQLITE_SCHEMA ){
415 clearHashTable(db);
416 }
drh4c504392000-10-16 22:06:40 +0000417 return sParse.rc;
drh75897232000-05-29 14:26:00 +0000418}
drh2dfbbca2000-07-28 14:32:48 +0000419
420/*
421** This routine implements a busy callback that sleeps and tries
422** again until a timeout value is reached. The timeout value is
423** an integer number of milliseconds passed in as the first
424** argument.
425*/
drhdaffd0e2001-04-11 14:28:42 +0000426static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000427 void *Timeout, /* Maximum amount of time to wait */
428 const char *NotUsed, /* The name of the table that is busy */
429 int count /* Number of times table has been busy */
430){
drh82ad3832000-07-31 13:38:24 +0000431#if defined(HAVE_USLEEP) && HAVE_USLEEP
drh2dfbbca2000-07-28 14:32:48 +0000432 int delay = 10000;
433 int prior_delay = 0;
434 int timeout = (int)Timeout;
435 int i;
436
437 for(i=1; i<count; i++){
438 prior_delay += delay;
439 delay = delay*2;
440 if( delay>=1000000 ){
441 delay = 1000000;
442 prior_delay += 1000000*(count - i - 1);
443 break;
444 }
445 }
446 if( prior_delay + delay > timeout*1000 ){
447 delay = timeout*1000 - prior_delay;
448 if( delay<=0 ) return 0;
449 }
450 usleep(delay);
451 return 1;
452#else
453 int timeout = (int)Timeout;
454 if( (count+1)*1000 > timeout ){
455 return 0;
456 }
457 sleep(1);
458 return 1;
459#endif
460}
461
462/*
463** This routine sets the busy callback for an Sqlite database to the
464** given callback function with the given argument.
465*/
466void sqlite_busy_handler(
467 sqlite *db,
468 int (*xBusy)(void*,const char*,int),
469 void *pArg
470){
471 db->xBusyCallback = xBusy;
472 db->pBusyArg = pArg;
473}
474
475/*
476** This routine installs a default busy handler that waits for the
477** specified number of milliseconds before returning 0.
478*/
479void sqlite_busy_timeout(sqlite *db, int ms){
480 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000481 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000482 }else{
483 sqlite_busy_handler(db, 0, 0);
484 }
485}
drh4c504392000-10-16 22:06:40 +0000486
487/*
488** Cause any pending operation to stop at its earliest opportunity.
489*/
490void sqlite_interrupt(sqlite *db){
491 db->flags |= SQLITE_Interrupt;
492}