blob: b1233a98f8083452c1c7b986c8b055b047454866 [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**
drh2dfbbca2000-07-28 14:32:48 +000029** $Id: main.c,v 1.14 2000/07/28 14:32:49 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.
38*/
39static int sqliteOpenCb(void *pDb, int argc, char **argv, char **azColName){
40 sqlite *db = (sqlite*)pDb;
41 Parse sParse;
42 int nErr;
drh75897232000-05-29 14:26:00 +000043
44 if( argc!=1 ) return 0;
45 memset(&sParse, 0, sizeof(sParse));
46 sParse.db = db;
47 sParse.initFlag = 1;
drhd1dedb82000-06-05 02:07:04 +000048 nErr = sqliteRunParser(&sParse, argv[0], 0);
drh75897232000-05-29 14:26:00 +000049 return nErr;
50}
51
52/*
drh58b95762000-06-02 01:17:37 +000053** Attempt to read the database schema and initialize internal
54** data structures. Return one of the SQLITE_ error codes to
55** indicate success or failure.
drhbed86902000-06-02 13:27:59 +000056**
57** After the database is initialized, the SQLITE_Initialized
58** bit is set in the flags field of the sqlite structure. An
59** attempt is made to initialize the database as soon as it
60** is opened. If that fails (perhaps because another process
61** has the sqlite_master table locked) than another attempt
62** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +000063*/
drh58b95762000-06-02 01:17:37 +000064static int sqliteInit(sqlite *db, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +000065 Vdbe *vdbe;
drh58b95762000-06-02 01:17:37 +000066 int rc;
67
68 /*
69 ** The master database table has a structure like this
70 */
drh75897232000-05-29 14:26:00 +000071 static char master_schema[] =
72 "CREATE TABLE " MASTER_NAME " (\n"
73 " type text,\n"
74 " name text,\n"
75 " tbl_name text,\n"
76 " sql text\n"
77 ")"
78 ;
79
80 /* The following program is used to initialize the internal
81 ** structure holding the tables and indexes of the database.
82 ** The database contains a special table named "sqlite_master"
83 ** defined as follows:
84 **
85 ** CREATE TABLE sqlite_master (
86 ** type text, -- Either "table" or "index"
87 ** name text, -- Name of table or index
88 ** tbl_name text, -- Associated table
89 ** sql text -- The CREATE statement for this object
90 ** );
91 **
92 ** The sqlite_master table contains a single entry for each table
drh967e8b72000-06-21 13:59:10 +000093 ** and each index. The "type" column tells whether the entry is
94 ** a table or index. The "name" column is the name of the object.
drh75897232000-05-29 14:26:00 +000095 ** The "tbl_name" is the name of the associated table. For tables,
drh967e8b72000-06-21 13:59:10 +000096 ** the tbl_name column is always the same as name. For indices, the
97 ** tbl_name column contains the name of the table that the index
98 ** indexes. Finally, the "sql" column contains the complete text of
drh75897232000-05-29 14:26:00 +000099 ** the CREATE TABLE or CREATE INDEX statement that originally created
100 ** the table or index.
101 **
102 ** The following program invokes its callback on the SQL for each
103 ** table then goes back and invokes the callback on the
104 ** SQL for each index. The callback will invoke the
105 ** parser to build the internal representation of the
106 ** database scheme.
107 */
108 static VdbeOp initProg[] = {
drheec553b2000-06-02 01:51:20 +0000109 { OP_Open, 0, 0, MASTER_NAME},
drh75897232000-05-29 14:26:00 +0000110 { OP_Next, 0, 8, 0}, /* 1 */
111 { OP_Field, 0, 0, 0},
112 { OP_String, 0, 0, "table"},
113 { OP_Ne, 0, 1, 0},
114 { OP_Field, 0, 3, 0},
115 { OP_Callback, 1, 0, 0},
116 { OP_Goto, 0, 1, 0},
117 { OP_Rewind, 0, 0, 0}, /* 8 */
118 { OP_Next, 0, 16, 0}, /* 9 */
119 { OP_Field, 0, 0, 0},
120 { OP_String, 0, 0, "index"},
121 { OP_Ne, 0, 9, 0},
122 { OP_Field, 0, 3, 0},
123 { OP_Callback, 1, 0, 0},
124 { OP_Goto, 0, 9, 0},
125 { OP_Halt, 0, 0, 0}, /* 16 */
126 };
127
drh58b95762000-06-02 01:17:37 +0000128 /* Create a virtual machine to run the initialization program. Run
129 ** the program. The delete the virtual machine.
130 */
131 vdbe = sqliteVdbeCreate(db->pBe);
drhd8bc7082000-06-07 23:51:50 +0000132 if( vdbe==0 ){
133 sqliteSetString(pzErrMsg, "out of memory",0);
134 return 1;
135 }
drh58b95762000-06-02 01:17:37 +0000136 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
drh2dfbbca2000-07-28 14:32:48 +0000137 rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg,
138 db->pBusyArg, db->xBusyCallback);
drh58b95762000-06-02 01:17:37 +0000139 sqliteVdbeDelete(vdbe);
140 if( rc==SQLITE_OK ){
141 Table *pTab;
142 char *azArg[2];
143 azArg[0] = master_schema;
144 azArg[1] = 0;
145 sqliteOpenCb(db, 1, azArg, 0);
146 pTab = sqliteFindTable(db, MASTER_NAME);
147 if( pTab ){
148 pTab->readOnly = 1;
149 }
150 db->flags |= SQLITE_Initialized;
151 }else{
152 sqliteStrRealloc(pzErrMsg);
153 }
154 return rc;
155}
156
157/*
158** Open a new SQLite database. Construct an "sqlite" structure to define
159** the state of this database and return a pointer to that structure.
160**
161** An attempt is made to initialize the in-memory data structures that
162** hold the database schema. But if this fails (because the schema file
163** is locked) then that step is deferred until the first call to
164** sqlite_exec().
165*/
166sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
167 sqlite *db;
168 int rc;
169
170 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000171 db = sqliteMalloc( sizeof(sqlite) );
172 if( pzErrMsg ) *pzErrMsg = 0;
173 if( db==0 ){
174 sqliteSetString(pzErrMsg, "out of memory", 0);
drhc3c2fc92000-05-31 22:58:39 +0000175 sqliteStrRealloc(pzErrMsg);
drh75897232000-05-29 14:26:00 +0000176 return 0;
177 }
178
179 /* Open the backend database driver */
180 db->pBe = sqliteDbbeOpen(zFilename, (mode&0222)!=0, mode!=0, pzErrMsg);
181 if( db->pBe==0 ){
drh5974a302000-06-07 14:42:26 +0000182 sqliteStrRealloc(pzErrMsg);
drh75897232000-05-29 14:26:00 +0000183 sqliteFree(db);
184 return 0;
185 }
186
drh58b95762000-06-02 01:17:37 +0000187 /* Attempt to read the schema */
188 rc = sqliteInit(db, pzErrMsg);
189 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
190 sqlite_close(db);
191 return 0;
drhbed86902000-06-02 13:27:59 +0000192 }else{
drhcc85b412000-06-07 15:11:27 +0000193 free(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000194 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000195 }
drh75897232000-05-29 14:26:00 +0000196 return db;
197}
198
199/*
200** Close an existing SQLite database
201*/
202void sqlite_close(sqlite *db){
203 int i;
204 sqliteDbbeClose(db->pBe);
205 for(i=0; i<N_HASH; i++){
206 Table *pNext, *pList = db->apTblHash[i];
207 db->apTblHash[i] = 0;
208 while( pList ){
209 pNext = pList->pHash;
210 pList->pHash = 0;
211 sqliteDeleteTable(db, pList);
212 pList = pNext;
213 }
214 }
215 sqliteFree(db);
216}
217
218/*
219** Return TRUE if the given SQL string ends in a semicolon.
220*/
221int sqlite_complete(const char *zSql){
222 int i;
223 int lastWasSemi = 0;
224
225 i = 0;
226 while( i>=0 && zSql[i]!=0 ){
227 int tokenType;
228 int n;
229
230 n = sqliteGetToken(&zSql[i], &tokenType);
231 switch( tokenType ){
232 case TK_SPACE:
233 case TK_COMMENT:
234 break;
235 case TK_SEMI:
236 lastWasSemi = 1;
237 break;
238 default:
239 lastWasSemi = 0;
240 break;
241 }
242 i += n;
243 }
244 return lastWasSemi;
245}
246
247/*
drhbed86902000-06-02 13:27:59 +0000248** Execute SQL code. Return one of the SQLITE_ success/failure
249** codes. Also write an error message into memory obtained from
250** malloc() and make *pzErrMsg point to that message.
251**
252** If the SQL is a query, then for each row in the query result
253** the xCallback() function is called. pArg becomes the first
254** argument to xCallback(). If xCallback=NULL then no callback
255** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000256*/
257int sqlite_exec(
258 sqlite *db, /* The database on which the SQL executes */
259 char *zSql, /* The SQL to be executed */
260 sqlite_callback xCallback, /* Invoke this callback routine */
261 void *pArg, /* First argument to xCallback() */
262 char **pzErrMsg /* Write error messages here */
263){
264 Parse sParse;
drh58b95762000-06-02 01:17:37 +0000265 int rc;
drh75897232000-05-29 14:26:00 +0000266
267 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000268 if( (db->flags & SQLITE_Initialized)==0 ){
269 int rc = sqliteInit(db, pzErrMsg);
270 if( rc!=SQLITE_OK ) return rc;
271 }
drh75897232000-05-29 14:26:00 +0000272 memset(&sParse, 0, sizeof(sParse));
273 sParse.db = db;
274 sParse.xCallback = xCallback;
275 sParse.pArg = pArg;
drh58b95762000-06-02 01:17:37 +0000276 rc = sqliteRunParser(&sParse, zSql, pzErrMsg);
drhc3c2fc92000-05-31 22:58:39 +0000277 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000278 return rc;
drh75897232000-05-29 14:26:00 +0000279}
drh2dfbbca2000-07-28 14:32:48 +0000280
281/*
282** This routine implements a busy callback that sleeps and tries
283** again until a timeout value is reached. The timeout value is
284** an integer number of milliseconds passed in as the first
285** argument.
286*/
287static int sqlite_default_busy_callback(
288 void *Timeout, /* Maximum amount of time to wait */
289 const char *NotUsed, /* The name of the table that is busy */
290 int count /* Number of times table has been busy */
291){
292 int rc;
293#ifdef HAVE_USLEEP
294 int delay = 10000;
295 int prior_delay = 0;
296 int timeout = (int)Timeout;
297 int i;
298
299 for(i=1; i<count; i++){
300 prior_delay += delay;
301 delay = delay*2;
302 if( delay>=1000000 ){
303 delay = 1000000;
304 prior_delay += 1000000*(count - i - 1);
305 break;
306 }
307 }
308 if( prior_delay + delay > timeout*1000 ){
309 delay = timeout*1000 - prior_delay;
310 if( delay<=0 ) return 0;
311 }
312 usleep(delay);
313 return 1;
314#else
315 int timeout = (int)Timeout;
316 if( (count+1)*1000 > timeout ){
317 return 0;
318 }
319 sleep(1);
320 return 1;
321#endif
322}
323
324/*
325** This routine sets the busy callback for an Sqlite database to the
326** given callback function with the given argument.
327*/
328void sqlite_busy_handler(
329 sqlite *db,
330 int (*xBusy)(void*,const char*,int),
331 void *pArg
332){
333 db->xBusyCallback = xBusy;
334 db->pBusyArg = pArg;
335}
336
337/*
338** This routine installs a default busy handler that waits for the
339** specified number of milliseconds before returning 0.
340*/
341void sqlite_busy_timeout(sqlite *db, int ms){
342 if( ms>0 ){
343 sqlite_busy_handler(db, sqlite_default_busy_callback, (void*)ms);
344 }else{
345 sqlite_busy_handler(db, 0, 0);
346 }
347}