blob: 24355db7285bb7d5464541aa9a0285a0acca3210 [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**
drh7c68d602000-10-11 19:28:51 +000029** $Id: main.c,v 1.19 2000/10/11 19:28:52 drh Exp $
drh75897232000-05-29 14:26:00 +000030*/
31#include "sqliteInt.h"
32
33/*
34** This is the callback routine for the code that initializes the
35** database. Each callback contains text of a CREATE TABLE or
36** CREATE INDEX statement that must be parsed to yield the internal
37** structures that describe the tables.
drh28037572000-08-02 13:47:41 +000038**
39** This callback is also called with argc==2 when there is meta
40** information in the sqlite_master file. The meta information is
41** contained in argv[1]. Typical meta information is the file format
42** version.
drh75897232000-05-29 14:26:00 +000043*/
44static int sqliteOpenCb(void *pDb, int argc, char **argv, char **azColName){
45 sqlite *db = (sqlite*)pDb;
46 Parse sParse;
47 int nErr;
drh75897232000-05-29 14:26:00 +000048
drh28037572000-08-02 13:47:41 +000049 if( argc==2 ){
50 if( sscanf(argv[1],"file format %d",&db->file_format)==1 ){
51 return 0;
52 }
53 /* Unknown meta information. Ignore it. */
54 return 0;
55 }
drh75897232000-05-29 14:26:00 +000056 if( argc!=1 ) return 0;
57 memset(&sParse, 0, sizeof(sParse));
58 sParse.db = db;
59 sParse.initFlag = 1;
drhd1dedb82000-06-05 02:07:04 +000060 nErr = sqliteRunParser(&sParse, argv[0], 0);
drh75897232000-05-29 14:26:00 +000061 return nErr;
62}
63
64/*
drh58b95762000-06-02 01:17:37 +000065** Attempt to read the database schema and initialize internal
66** data structures. Return one of the SQLITE_ error codes to
67** indicate success or failure.
drhbed86902000-06-02 13:27:59 +000068**
69** After the database is initialized, the SQLITE_Initialized
70** bit is set in the flags field of the sqlite structure. An
71** attempt is made to initialize the database as soon as it
72** is opened. If that fails (perhaps because another process
73** has the sqlite_master table locked) than another attempt
74** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +000075*/
drh58b95762000-06-02 01:17:37 +000076static int sqliteInit(sqlite *db, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +000077 Vdbe *vdbe;
drh58b95762000-06-02 01:17:37 +000078 int rc;
79
80 /*
81 ** The master database table has a structure like this
82 */
drh75897232000-05-29 14:26:00 +000083 static char master_schema[] =
84 "CREATE TABLE " MASTER_NAME " (\n"
85 " type text,\n"
86 " name text,\n"
87 " tbl_name text,\n"
88 " sql text\n"
89 ")"
90 ;
91
92 /* The following program is used to initialize the internal
93 ** structure holding the tables and indexes of the database.
94 ** The database contains a special table named "sqlite_master"
95 ** defined as follows:
96 **
97 ** CREATE TABLE sqlite_master (
drh28037572000-08-02 13:47:41 +000098 ** type text, -- Either "table" or "index" or "meta"
drh75897232000-05-29 14:26:00 +000099 ** name text, -- Name of table or index
100 ** tbl_name text, -- Associated table
101 ** sql text -- The CREATE statement for this object
102 ** );
103 **
104 ** The sqlite_master table contains a single entry for each table
drh967e8b72000-06-21 13:59:10 +0000105 ** and each index. The "type" column tells whether the entry is
106 ** a table or index. The "name" column is the name of the object.
drh75897232000-05-29 14:26:00 +0000107 ** The "tbl_name" is the name of the associated table. For tables,
drh967e8b72000-06-21 13:59:10 +0000108 ** the tbl_name column is always the same as name. For indices, the
109 ** tbl_name column contains the name of the table that the index
110 ** indexes. Finally, the "sql" column contains the complete text of
drh75897232000-05-29 14:26:00 +0000111 ** the CREATE TABLE or CREATE INDEX statement that originally created
112 ** the table or index.
113 **
drh28037572000-08-02 13:47:41 +0000114 ** If the "type" column has the value "meta", then the "sql" column
115 ** contains extra information about the database, such as the
116 ** file format version number. All meta information must be processed
117 ** before any tables or indices are constructed.
118 **
drh75897232000-05-29 14:26:00 +0000119 ** The following program invokes its callback on the SQL for each
120 ** table then goes back and invokes the callback on the
121 ** SQL for each index. The callback will invoke the
122 ** parser to build the internal representation of the
123 ** database scheme.
124 */
125 static VdbeOp initProg[] = {
drheec553b2000-06-02 01:51:20 +0000126 { OP_Open, 0, 0, MASTER_NAME},
drh28037572000-08-02 13:47:41 +0000127 { OP_Next, 0, 9, 0}, /* 1 */
128 { OP_Field, 0, 0, 0},
129 { OP_String, 0, 0, "meta"},
130 { OP_Ne, 0, 1, 0},
131 { OP_Field, 0, 0, 0},
132 { OP_Field, 0, 3, 0},
133 { OP_Callback, 2, 0, 0},
134 { OP_Goto, 0, 1, 0},
135 { OP_Rewind, 0, 0, 0}, /* 9 */
136 { OP_Next, 0, 17, 0}, /* 10 */
drh75897232000-05-29 14:26:00 +0000137 { OP_Field, 0, 0, 0},
138 { OP_String, 0, 0, "table"},
drh28037572000-08-02 13:47:41 +0000139 { OP_Ne, 0, 10, 0},
drh75897232000-05-29 14:26:00 +0000140 { OP_Field, 0, 3, 0},
141 { OP_Callback, 1, 0, 0},
drh28037572000-08-02 13:47:41 +0000142 { OP_Goto, 0, 10, 0},
143 { OP_Rewind, 0, 0, 0}, /* 17 */
144 { OP_Next, 0, 25, 0}, /* 18 */
drh75897232000-05-29 14:26:00 +0000145 { OP_Field, 0, 0, 0},
146 { OP_String, 0, 0, "index"},
drh28037572000-08-02 13:47:41 +0000147 { OP_Ne, 0, 18, 0},
drh75897232000-05-29 14:26:00 +0000148 { OP_Field, 0, 3, 0},
149 { OP_Callback, 1, 0, 0},
drh28037572000-08-02 13:47:41 +0000150 { OP_Goto, 0, 18, 0},
151 { OP_Halt, 0, 0, 0}, /* 25 */
drh75897232000-05-29 14:26:00 +0000152 };
153
drh58b95762000-06-02 01:17:37 +0000154 /* Create a virtual machine to run the initialization program. Run
155 ** the program. The delete the virtual machine.
156 */
157 vdbe = sqliteVdbeCreate(db->pBe);
drhd8bc7082000-06-07 23:51:50 +0000158 if( vdbe==0 ){
159 sqliteSetString(pzErrMsg, "out of memory",0);
160 return 1;
161 }
drh58b95762000-06-02 01:17:37 +0000162 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
drh2dfbbca2000-07-28 14:32:48 +0000163 rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg,
164 db->pBusyArg, db->xBusyCallback);
drh58b95762000-06-02 01:17:37 +0000165 sqliteVdbeDelete(vdbe);
drh28037572000-08-02 13:47:41 +0000166 if( rc==SQLITE_OK && db->file_format<2 && db->nTable>0 ){
167 sqliteSetString(pzErrMsg, "obsolete file format", 0);
168 rc = SQLITE_ERROR;
169 }
drh58b95762000-06-02 01:17:37 +0000170 if( rc==SQLITE_OK ){
171 Table *pTab;
172 char *azArg[2];
173 azArg[0] = master_schema;
174 azArg[1] = 0;
175 sqliteOpenCb(db, 1, azArg, 0);
176 pTab = sqliteFindTable(db, MASTER_NAME);
177 if( pTab ){
178 pTab->readOnly = 1;
179 }
180 db->flags |= SQLITE_Initialized;
181 }else{
182 sqliteStrRealloc(pzErrMsg);
183 }
184 return rc;
185}
186
187/*
drhb217a572000-08-22 13:40:18 +0000188** The version of the library
189*/
drh3d0b5592000-08-22 13:40:51 +0000190const char sqlite_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000191
192/*
drh58b95762000-06-02 01:17:37 +0000193** Open a new SQLite database. Construct an "sqlite" structure to define
194** the state of this database and return a pointer to that structure.
195**
196** An attempt is made to initialize the in-memory data structures that
197** hold the database schema. But if this fails (because the schema file
198** is locked) then that step is deferred until the first call to
199** sqlite_exec().
200*/
201sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
202 sqlite *db;
203 int rc;
204
205 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000206 db = sqliteMalloc( sizeof(sqlite) );
207 if( pzErrMsg ) *pzErrMsg = 0;
208 if( db==0 ){
209 sqliteSetString(pzErrMsg, "out of memory", 0);
drhc3c2fc92000-05-31 22:58:39 +0000210 sqliteStrRealloc(pzErrMsg);
drh75897232000-05-29 14:26:00 +0000211 return 0;
212 }
213
214 /* Open the backend database driver */
215 db->pBe = sqliteDbbeOpen(zFilename, (mode&0222)!=0, mode!=0, pzErrMsg);
216 if( db->pBe==0 ){
drh5974a302000-06-07 14:42:26 +0000217 sqliteStrRealloc(pzErrMsg);
drh75897232000-05-29 14:26:00 +0000218 sqliteFree(db);
219 return 0;
220 }
221
drh28037572000-08-02 13:47:41 +0000222 /* Assume file format 1 unless the database says otherwise */
223 db->file_format = 1;
224
drh58b95762000-06-02 01:17:37 +0000225 /* Attempt to read the schema */
226 rc = sqliteInit(db, pzErrMsg);
227 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
228 sqlite_close(db);
229 return 0;
drhbed86902000-06-02 13:27:59 +0000230 }else{
drhcc85b412000-06-07 15:11:27 +0000231 free(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000232 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000233 }
drh75897232000-05-29 14:26:00 +0000234 return db;
235}
236
237/*
238** Close an existing SQLite database
239*/
240void sqlite_close(sqlite *db){
241 int i;
242 sqliteDbbeClose(db->pBe);
243 for(i=0; i<N_HASH; i++){
244 Table *pNext, *pList = db->apTblHash[i];
245 db->apTblHash[i] = 0;
246 while( pList ){
247 pNext = pList->pHash;
248 pList->pHash = 0;
249 sqliteDeleteTable(db, pList);
250 pList = pNext;
251 }
252 }
253 sqliteFree(db);
254}
255
256/*
257** Return TRUE if the given SQL string ends in a semicolon.
258*/
259int sqlite_complete(const char *zSql){
260 int i;
261 int lastWasSemi = 0;
262
263 i = 0;
264 while( i>=0 && zSql[i]!=0 ){
265 int tokenType;
266 int n;
267
268 n = sqliteGetToken(&zSql[i], &tokenType);
269 switch( tokenType ){
270 case TK_SPACE:
271 case TK_COMMENT:
272 break;
273 case TK_SEMI:
274 lastWasSemi = 1;
275 break;
276 default:
277 lastWasSemi = 0;
278 break;
279 }
280 i += n;
281 }
282 return lastWasSemi;
283}
284
285/*
drhbed86902000-06-02 13:27:59 +0000286** Execute SQL code. Return one of the SQLITE_ success/failure
287** codes. Also write an error message into memory obtained from
288** malloc() and make *pzErrMsg point to that message.
289**
290** If the SQL is a query, then for each row in the query result
291** the xCallback() function is called. pArg becomes the first
292** argument to xCallback(). If xCallback=NULL then no callback
293** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000294*/
295int sqlite_exec(
296 sqlite *db, /* The database on which the SQL executes */
297 char *zSql, /* The SQL to be executed */
298 sqlite_callback xCallback, /* Invoke this callback routine */
299 void *pArg, /* First argument to xCallback() */
300 char **pzErrMsg /* Write error messages here */
301){
302 Parse sParse;
drh58b95762000-06-02 01:17:37 +0000303 int rc;
drh75897232000-05-29 14:26:00 +0000304
305 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000306 if( (db->flags & SQLITE_Initialized)==0 ){
307 int rc = sqliteInit(db, pzErrMsg);
308 if( rc!=SQLITE_OK ) return rc;
309 }
drh75897232000-05-29 14:26:00 +0000310 memset(&sParse, 0, sizeof(sParse));
311 sParse.db = db;
312 sParse.xCallback = xCallback;
313 sParse.pArg = pArg;
drh58b95762000-06-02 01:17:37 +0000314 rc = sqliteRunParser(&sParse, zSql, pzErrMsg);
drhc3c2fc92000-05-31 22:58:39 +0000315 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000316 return rc;
drh75897232000-05-29 14:26:00 +0000317}
drh2dfbbca2000-07-28 14:32:48 +0000318
319/*
320** This routine implements a busy callback that sleeps and tries
321** again until a timeout value is reached. The timeout value is
322** an integer number of milliseconds passed in as the first
323** argument.
324*/
325static int sqlite_default_busy_callback(
326 void *Timeout, /* Maximum amount of time to wait */
327 const char *NotUsed, /* The name of the table that is busy */
328 int count /* Number of times table has been busy */
329){
drh82ad3832000-07-31 13:38:24 +0000330#if defined(HAVE_USLEEP) && HAVE_USLEEP
drh2dfbbca2000-07-28 14:32:48 +0000331 int delay = 10000;
332 int prior_delay = 0;
333 int timeout = (int)Timeout;
334 int i;
335
336 for(i=1; i<count; i++){
337 prior_delay += delay;
338 delay = delay*2;
339 if( delay>=1000000 ){
340 delay = 1000000;
341 prior_delay += 1000000*(count - i - 1);
342 break;
343 }
344 }
345 if( prior_delay + delay > timeout*1000 ){
346 delay = timeout*1000 - prior_delay;
347 if( delay<=0 ) return 0;
348 }
349 usleep(delay);
350 return 1;
351#else
352 int timeout = (int)Timeout;
353 if( (count+1)*1000 > timeout ){
354 return 0;
355 }
356 sleep(1);
357 return 1;
358#endif
359}
360
361/*
362** This routine sets the busy callback for an Sqlite database to the
363** given callback function with the given argument.
364*/
365void sqlite_busy_handler(
366 sqlite *db,
367 int (*xBusy)(void*,const char*,int),
368 void *pArg
369){
370 db->xBusyCallback = xBusy;
371 db->pBusyArg = pArg;
372}
373
374/*
375** This routine installs a default busy handler that waits for the
376** specified number of milliseconds before returning 0.
377*/
378void sqlite_busy_timeout(sqlite *db, int ms){
379 if( ms>0 ){
380 sqlite_busy_handler(db, sqlite_default_busy_callback, (void*)ms);
381 }else{
382 sqlite_busy_handler(db, 0, 0);
383 }
384}