blob: 4969f3551fc36ec8c432d813406c49998020852a [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**
drh17f71932002-02-21 12:01:27 +000017** $Id: main.c,v 1.61 2002/02/21 12:01:27 drh Exp $
drh75897232000-05-29 14:26:00 +000018*/
19#include "sqliteInt.h"
drh8cfbf082001-09-19 13:22:39 +000020#include "os.h"
drh75897232000-05-29 14:26:00 +000021
22/*
23** This is the callback routine for the code that initializes the
drh382c0242001-10-06 16:33:02 +000024** database. See sqliteInit() below for additional information.
25**
26** Each callback contains the following information:
drh28037572000-08-02 13:47:41 +000027**
drh4a324312001-12-21 14:30:42 +000028** argv[0] = "file-format" or "schema-cookie" or "table" or "index"
drhe3c41372001-09-17 20:25:58 +000029** argv[1] = table or index name or meta statement type.
30** argv[2] = root page number for table or index. NULL for meta.
drhadbca9c2001-09-27 15:11:53 +000031** argv[3] = SQL create statement for the table or index
drhd78eeee2001-09-13 16:18:53 +000032**
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
drh382c0242001-10-06 16:33:02 +000039 /* TODO: Do some validity checks on all fields. In particular,
40 ** make sure fields do not contain NULLs. Otherwise we might core
41 ** when attempting to initialize from a corrupt database file. */
drhe3c41372001-09-17 20:25:58 +000042
drhadbca9c2001-09-27 15:11:53 +000043 assert( argc==4 );
drhd78eeee2001-09-13 16:18:53 +000044 switch( argv[0][0] ){
drh4a324312001-12-21 14:30:42 +000045 case 'f': { /* File format */
46 db->file_format = atoi(argv[3]);
47 break;
48 }
49 case 's': { /* Schema cookie */
50 db->schema_cookie = atoi(argv[3]);
51 db->next_cookie = db->schema_cookie;
drhd78eeee2001-09-13 16:18:53 +000052 break;
drh28037572000-08-02 13:47:41 +000053 }
drh17f71932002-02-21 12:01:27 +000054 case 'v':
drhd78eeee2001-09-13 16:18:53 +000055 case 'i':
drh17f71932002-02-21 12:01:27 +000056 case 't': { /* CREATE TABLE, CREATE INDEX, or CREATE VIEW statements */
drhadbca9c2001-09-27 15:11:53 +000057 if( argv[3] && argv[3][0] ){
drh17f71932002-02-21 12:01:27 +000058 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
drh382c0242001-10-06 16:33:02 +000059 ** But because sParse.initFlag is set to 1, no VDBE code is generated
60 ** or executed. All the parser does is build the internal data
drh17f71932002-02-21 12:01:27 +000061 ** structures that describe the table, index, or view.
drh382c0242001-10-06 16:33:02 +000062 */
drhadbca9c2001-09-27 15:11:53 +000063 memset(&sParse, 0, sizeof(sParse));
64 sParse.db = db;
65 sParse.initFlag = 1;
66 sParse.newTnum = atoi(argv[2]);
drhf5bf0a72001-11-23 00:24:12 +000067 sqliteRunParser(&sParse, argv[3], 0);
drhadbca9c2001-09-27 15:11:53 +000068 }else{
drh382c0242001-10-06 16:33:02 +000069 /* If the SQL column is blank it means this is an index that
70 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
drhaacc5432002-01-06 17:07:40 +000071 ** constraint for a CREATE TABLE. The index should have already
drh382c0242001-10-06 16:33:02 +000072 ** been created when we processed the CREATE TABLE. All we have
drhaacc5432002-01-06 17:07:40 +000073 ** to do here is record the root page number for that index.
drh382c0242001-10-06 16:33:02 +000074 */
drhadbca9c2001-09-27 15:11:53 +000075 Index *pIndex = sqliteFindIndex(db, argv[1]);
76 if( pIndex==0 || pIndex->tnum!=0 ){
drhda9e0342002-01-10 14:31:48 +000077 /* This can occur if there exists an index on a TEMP table which
78 ** has the same name as another index on a permanent index. Since
79 ** the permanent table is hidden by the TEMP table, we can also
80 ** safely ignore the index on the permanent table.
81 */
82 /* Do Nothing */;
drhadbca9c2001-09-27 15:11:53 +000083 }else{
84 pIndex->tnum = atoi(argv[2]);
85 }
86 }
drhd78eeee2001-09-13 16:18:53 +000087 break;
88 }
89 default: {
90 /* This can not happen! */
91 nErr = 1;
92 assert( nErr==0 );
93 }
drh28037572000-08-02 13:47:41 +000094 }
drh75897232000-05-29 14:26:00 +000095 return nErr;
96}
97
98/*
drh58b95762000-06-02 01:17:37 +000099** Attempt to read the database schema and initialize internal
100** data structures. Return one of the SQLITE_ error codes to
101** indicate success or failure.
drhbed86902000-06-02 13:27:59 +0000102**
103** After the database is initialized, the SQLITE_Initialized
104** bit is set in the flags field of the sqlite structure. An
105** attempt is made to initialize the database as soon as it
106** is opened. If that fails (perhaps because another process
107** has the sqlite_master table locked) than another attempt
108** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +0000109*/
drh58b95762000-06-02 01:17:37 +0000110static int sqliteInit(sqlite *db, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +0000111 Vdbe *vdbe;
drh58b95762000-06-02 01:17:37 +0000112 int rc;
113
114 /*
115 ** The master database table has a structure like this
116 */
drh75897232000-05-29 14:26:00 +0000117 static char master_schema[] =
118 "CREATE TABLE " MASTER_NAME " (\n"
119 " type text,\n"
120 " name text,\n"
121 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000122 " rootpage integer,\n"
drh75897232000-05-29 14:26:00 +0000123 " sql text\n"
124 ")"
125 ;
126
drhaacc5432002-01-06 17:07:40 +0000127 /* The following VDBE program is used to initialize the internal
drh75897232000-05-29 14:26:00 +0000128 ** structure holding the tables and indexes of the database.
129 ** The database contains a special table named "sqlite_master"
130 ** defined as follows:
131 **
132 ** CREATE TABLE sqlite_master (
drh28037572000-08-02 13:47:41 +0000133 ** type text, -- Either "table" or "index" or "meta"
drh75897232000-05-29 14:26:00 +0000134 ** name text, -- Name of table or index
135 ** tbl_name text, -- Associated table
drhadbca9c2001-09-27 15:11:53 +0000136 ** rootpage integer, -- The integer page number of root page
drh75897232000-05-29 14:26:00 +0000137 ** sql text -- The CREATE statement for this object
138 ** );
139 **
140 ** The sqlite_master table contains a single entry for each table
drh967e8b72000-06-21 13:59:10 +0000141 ** and each index. The "type" column tells whether the entry is
142 ** a table or index. The "name" column is the name of the object.
drh75897232000-05-29 14:26:00 +0000143 ** The "tbl_name" is the name of the associated table. For tables,
drh967e8b72000-06-21 13:59:10 +0000144 ** the tbl_name column is always the same as name. For indices, the
145 ** tbl_name column contains the name of the table that the index
drh382c0242001-10-06 16:33:02 +0000146 ** indexes. The "rootpage" column holds the number of the root page
147 ** for the b-tree for the table or index. Finally, the "sql" column
148 ** contains the complete text of the CREATE TABLE or CREATE INDEX
149 ** statement that originally created the table or index. If an index
150 ** was created to fulfill a PRIMARY KEY or UNIQUE constraint on a table,
151 ** then the "sql" column is NULL.
drh75897232000-05-29 14:26:00 +0000152 **
drh17f71932002-02-21 12:01:27 +0000153 ** In format 1, entries in the sqlite_master table are in a random
154 ** order. Two passes must be made through the table to initialize
155 ** internal data structures. The first pass reads table definitions
156 ** and the second pass read index definitions. Having two passes
157 ** insures that indices appear after their tables.
158 **
159 ** In format 2, entries appear in chronological order. Only a single
160 ** pass needs to be made through the table since everything will be
161 ** in the write order. VIEWs may only occur in format 2.
drh28037572000-08-02 13:47:41 +0000162 **
drh75897232000-05-29 14:26:00 +0000163 ** The following program invokes its callback on the SQL for each
164 ** table then goes back and invokes the callback on the
165 ** SQL for each index. The callback will invoke the
166 ** parser to build the internal representation of the
167 ** database scheme.
168 */
169 static VdbeOp initProg[] = {
drh17f71932002-02-21 12:01:27 +0000170 /* Send the file format to the callback routine
171 */
drh4a324312001-12-21 14:30:42 +0000172 { OP_Open, 0, 2, 0},
173 { OP_String, 0, 0, "file-format"},
174 { OP_String, 0, 0, 0},
175 { OP_String, 0, 0, 0},
176 { OP_ReadCookie, 0, 1, 0},
177 { OP_Callback, 4, 0, 0},
drh17f71932002-02-21 12:01:27 +0000178
179 /* Send the initial schema cookie to the callback
180 */
drh4a324312001-12-21 14:30:42 +0000181 { OP_String, 0, 0, "schema_cookie"},
182 { OP_String, 0, 0, 0},
183 { OP_String, 0, 0, 0},
184 { OP_ReadCookie, 0, 0, 0},
185 { OP_Callback, 4, 0, 0},
drh17f71932002-02-21 12:01:27 +0000186
187 /* Check the file format. If the format number is 2 or more,
188 ** then do a single pass through the SQLITE_MASTER table. For
189 ** a format number of less than 2, jump forward to a different
190 ** algorithm that makes two passes through the SQLITE_MASTER table,
191 ** once for tables and a second time for indices.
192 */
193 { OP_ReadCookie, 0, 1, 0},
194 { OP_Integer, 2, 0, 0},
195 { OP_Lt, 0, 23, 0},
196
197 /* This is the code for doing a single scan through the SQLITE_MASTER
198 ** table. This code runs for format 2 and greater.
199 */
200 { OP_Rewind, 0, 21, 0},
201 { OP_Column, 0, 0, 0}, /* 15 */
202 { OP_Column, 0, 1, 0},
203 { OP_Column, 0, 3, 0},
204 { OP_Column, 0, 4, 0},
205 { OP_Callback, 4, 0, 0},
206 { OP_Next, 0, 15, 0},
207 { OP_Close, 0, 0, 0}, /* 21 */
208 { OP_Halt, 0, 0, 0},
209
210 /* This is the code for doing two passes through SQLITE_MASTER. This
211 ** code runs for file format 1.
212 */
213 { OP_Rewind, 0, 43, 0}, /* 23 */
214 { OP_Column, 0, 0, 0}, /* 24 */
drh4a324312001-12-21 14:30:42 +0000215 { OP_String, 0, 0, "table"},
drh17f71932002-02-21 12:01:27 +0000216 { OP_Ne, 0, 32, 0},
drh4a324312001-12-21 14:30:42 +0000217 { OP_Column, 0, 0, 0},
218 { OP_Column, 0, 1, 0},
219 { OP_Column, 0, 3, 0},
220 { OP_Column, 0, 4, 0},
221 { OP_Callback, 4, 0, 0},
drh17f71932002-02-21 12:01:27 +0000222 { OP_Next, 0, 24, 0}, /* 32 */
223 { OP_Rewind, 0, 43, 0}, /* 33 */
224 { OP_Column, 0, 0, 0}, /* 34 */
drh4a324312001-12-21 14:30:42 +0000225 { OP_String, 0, 0, "index"},
drh17f71932002-02-21 12:01:27 +0000226 { OP_Ne, 0, 42, 0},
drh4a324312001-12-21 14:30:42 +0000227 { OP_Column, 0, 0, 0},
228 { OP_Column, 0, 1, 0},
229 { OP_Column, 0, 3, 0},
230 { OP_Column, 0, 4, 0},
231 { OP_Callback, 4, 0, 0},
drh17f71932002-02-21 12:01:27 +0000232 { OP_Next, 0, 34, 0}, /* 42 */
233 { OP_Close, 0, 0, 0}, /* 43 */
drh4a324312001-12-21 14:30:42 +0000234 { OP_Halt, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000235 };
236
drh58b95762000-06-02 01:17:37 +0000237 /* Create a virtual machine to run the initialization program. Run
drh382c0242001-10-06 16:33:02 +0000238 ** the program. Then delete the virtual machine.
drh58b95762000-06-02 01:17:37 +0000239 */
drh4c504392000-10-16 22:06:40 +0000240 vdbe = sqliteVdbeCreate(db);
drhd8bc7082000-06-07 23:51:50 +0000241 if( vdbe==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000242 sqliteSetString(pzErrMsg, "out of memory", 0);
drhdaffd0e2001-04-11 14:28:42 +0000243 return SQLITE_NOMEM;
drhd8bc7082000-06-07 23:51:50 +0000244 }
drh58b95762000-06-02 01:17:37 +0000245 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
drh2dfbbca2000-07-28 14:32:48 +0000246 rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg,
247 db->pBusyArg, db->xBusyCallback);
drh58b95762000-06-02 01:17:37 +0000248 sqliteVdbeDelete(vdbe);
drh4a324312001-12-21 14:30:42 +0000249 if( rc==SQLITE_OK && db->nTable==0 ){
drh17f71932002-02-21 12:01:27 +0000250 db->file_format = 2;
drh4a324312001-12-21 14:30:42 +0000251 }
drh17f71932002-02-21 12:01:27 +0000252 if( rc==SQLITE_OK && db->file_format>2 ){
drhd78eeee2001-09-13 16:18:53 +0000253 sqliteSetString(pzErrMsg, "unsupported file format", 0);
drh28037572000-08-02 13:47:41 +0000254 rc = SQLITE_ERROR;
255 }
drhaacc5432002-01-06 17:07:40 +0000256
257 /* The schema for the SQLITE_MASTER table is not stored in the
258 ** database itself. We have to invoke the callback one extra
259 ** time to get it to process the SQLITE_MASTER table defintion.
260 */
drh58b95762000-06-02 01:17:37 +0000261 if( rc==SQLITE_OK ){
262 Table *pTab;
drhe3c41372001-09-17 20:25:58 +0000263 char *azArg[6];
drhd78eeee2001-09-13 16:18:53 +0000264 azArg[0] = "table";
265 azArg[1] = MASTER_NAME;
266 azArg[2] = "2";
drhadbca9c2001-09-27 15:11:53 +0000267 azArg[3] = master_schema;
268 azArg[4] = 0;
269 sqliteOpenCb(db, 4, azArg, 0);
drh58b95762000-06-02 01:17:37 +0000270 pTab = sqliteFindTable(db, MASTER_NAME);
271 if( pTab ){
272 pTab->readOnly = 1;
273 }
274 db->flags |= SQLITE_Initialized;
drh5e00f6c2001-09-13 13:46:56 +0000275 sqliteCommitInternalChanges(db);
drh58b95762000-06-02 01:17:37 +0000276 }
277 return rc;
278}
279
280/*
drhb217a572000-08-22 13:40:18 +0000281** The version of the library
282*/
drh3d0b5592000-08-22 13:40:51 +0000283const char sqlite_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000284
285/*
drh297ecf12001-04-05 15:57:13 +0000286** Does the library expect data to be encoded as UTF-8 or iso8859? The
287** following global constant always lets us know.
288*/
289#ifdef SQLITE_UTF8
drhfbc3eab2001-04-06 16:13:42 +0000290const char sqlite_encoding[] = "UTF-8";
drh297ecf12001-04-05 15:57:13 +0000291#else
drhfbc3eab2001-04-06 16:13:42 +0000292const char sqlite_encoding[] = "iso8859";
drh297ecf12001-04-05 15:57:13 +0000293#endif
294
295/*
drh58b95762000-06-02 01:17:37 +0000296** Open a new SQLite database. Construct an "sqlite" structure to define
297** the state of this database and return a pointer to that structure.
298**
299** An attempt is made to initialize the in-memory data structures that
300** hold the database schema. But if this fails (because the schema file
301** is locked) then that step is deferred until the first call to
302** sqlite_exec().
303*/
304sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
305 sqlite *db;
306 int rc;
307
308 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000309 db = sqliteMalloc( sizeof(sqlite) );
310 if( pzErrMsg ) *pzErrMsg = 0;
drhdaffd0e2001-04-11 14:28:42 +0000311 if( db==0 ) goto no_mem_on_open;
drhbeae3192001-09-22 18:12:08 +0000312 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
313 sqliteHashInit(&db->idxHash, SQLITE_HASH_STRING, 0);
drh74e24cd2002-01-09 03:19:59 +0000314 sqliteHashInit(&db->tblDrop, SQLITE_HASH_POINTER, 0);
315 sqliteHashInit(&db->idxDrop, SQLITE_HASH_POINTER, 0);
drh1c928532002-01-31 15:54:21 +0000316 db->onError = OE_Default;
drh5cf8e8c2002-02-19 22:42:05 +0000317 db->priorNewRowid = 0;
drh75897232000-05-29 14:26:00 +0000318
319 /* Open the backend database driver */
drha1b351a2001-09-14 16:42:12 +0000320 rc = sqliteBtreeOpen(zFilename, mode, MAX_PAGES, &db->pBe);
drh5e00f6c2001-09-13 13:46:56 +0000321 if( rc!=SQLITE_OK ){
322 switch( rc ){
323 default: {
drhaacc5432002-01-06 17:07:40 +0000324 sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0);
drh5e00f6c2001-09-13 13:46:56 +0000325 }
326 }
drh75897232000-05-29 14:26:00 +0000327 sqliteFree(db);
drh5edc3122001-09-13 21:53:09 +0000328 sqliteStrRealloc(pzErrMsg);
drhbe0072d2001-09-13 14:46:09 +0000329 return 0;
drh75897232000-05-29 14:26:00 +0000330 }
331
drh58b95762000-06-02 01:17:37 +0000332 /* Attempt to read the schema */
333 rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000334 if( sqlite_malloc_failed ){
drh6d4abfb2001-10-22 02:58:08 +0000335 sqlite_close(db);
drhdaffd0e2001-04-11 14:28:42 +0000336 goto no_mem_on_open;
337 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
drh58b95762000-06-02 01:17:37 +0000338 sqlite_close(db);
drh5edc3122001-09-13 21:53:09 +0000339 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000340 return 0;
drhaacc5432002-01-06 17:07:40 +0000341 }else if( pzErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +0000342 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000343 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000344 }
drh75897232000-05-29 14:26:00 +0000345 return db;
drhdaffd0e2001-04-11 14:28:42 +0000346
347no_mem_on_open:
348 sqliteSetString(pzErrMsg, "out of memory", 0);
349 sqliteStrRealloc(pzErrMsg);
350 return 0;
drh75897232000-05-29 14:26:00 +0000351}
352
353/*
drhf57b3392001-10-08 13:22:32 +0000354** Erase all schema information from the schema hash table. Except
355** tables that are created using CREATE TEMPORARY TABLE are preserved
drhaacc5432002-01-06 17:07:40 +0000356** if the preserveTemps flag is true.
drh50e5dad2001-09-15 00:57:28 +0000357**
358** The database schema is normally read in once when the database
359** is first opened and stored in a hash table in the sqlite structure.
360** This routine erases the stored schema. This erasure occurs because
361** either the database is being closed or because some other process
362** changed the schema and this process needs to reread it.
drh75897232000-05-29 14:26:00 +0000363*/
drhf57b3392001-10-08 13:22:32 +0000364static void clearHashTable(sqlite *db, int preserveTemps){
drhbeae3192001-09-22 18:12:08 +0000365 HashElem *pElem;
366 Hash temp1;
drh74e24cd2002-01-09 03:19:59 +0000367 assert( sqliteHashFirst(&db->tblDrop)==0 ); /* There can not be uncommitted */
368 assert( sqliteHashFirst(&db->idxDrop)==0 ); /* DROP TABLEs or DROP INDEXs */
drhbeae3192001-09-22 18:12:08 +0000369 temp1 = db->tblHash;
370 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
371 sqliteHashClear(&db->idxHash);
372 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
drhf57b3392001-10-08 13:22:32 +0000373 Table *pTab = sqliteHashData(pElem);
374 if( preserveTemps && pTab->isTemp ){
375 Index *pIdx;
drh6d4abfb2001-10-22 02:58:08 +0000376 int nName = strlen(pTab->zName);
377 Table *pOld = sqliteHashInsert(&db->tblHash, pTab->zName, nName+1, pTab);
378 if( pOld!=0 ){
379 assert( pOld==pTab ); /* Malloc failed on the HashInsert */
380 sqliteDeleteTable(db, pOld);
381 continue;
382 }
drhf57b3392001-10-08 13:22:32 +0000383 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
384 int n = strlen(pIdx->zName)+1;
drh6d4abfb2001-10-22 02:58:08 +0000385 Index *pOldIdx;
386 pOldIdx = sqliteHashInsert(&db->idxHash, pIdx->zName, n, pIdx);
387 if( pOld ){
388 assert( pOldIdx==pIdx );
389 sqliteUnlinkAndDeleteIndex(db, pOldIdx);
390 }
drhf57b3392001-10-08 13:22:32 +0000391 }
392 }else{
393 sqliteDeleteTable(db, pTab);
394 }
drh75897232000-05-29 14:26:00 +0000395 }
drhbeae3192001-09-22 18:12:08 +0000396 sqliteHashClear(&temp1);
drh50e5dad2001-09-15 00:57:28 +0000397 db->flags &= ~SQLITE_Initialized;
398}
399
400/*
drhaf9ff332002-01-16 21:00:27 +0000401** Return the ROWID of the most recent insert
402*/
403int sqlite_last_insert_rowid(sqlite *db){
404 return db->lastRowid;
405}
406
407/*
drh50e5dad2001-09-15 00:57:28 +0000408** Close an existing SQLite database
409*/
410void sqlite_close(sqlite *db){
411 sqliteBtreeClose(db->pBe);
drhf57b3392001-10-08 13:22:32 +0000412 clearHashTable(db, 0);
413 if( db->pBeTemp ){
414 sqliteBtreeClose(db->pBeTemp);
415 }
drh75897232000-05-29 14:26:00 +0000416 sqliteFree(db);
417}
418
419/*
420** Return TRUE if the given SQL string ends in a semicolon.
421*/
422int sqlite_complete(const char *zSql){
drh8c82b352000-12-10 18:23:50 +0000423 int isComplete = 0;
424 while( *zSql ){
425 switch( *zSql ){
426 case ';': {
427 isComplete = 1;
drh75897232000-05-29 14:26:00 +0000428 break;
drh8c82b352000-12-10 18:23:50 +0000429 }
430 case ' ':
431 case '\t':
432 case '\n':
433 case '\f': {
drh75897232000-05-29 14:26:00 +0000434 break;
drh8c82b352000-12-10 18:23:50 +0000435 }
drh969fa7c2002-02-18 18:30:32 +0000436 case '[': {
437 isComplete = 0;
438 zSql++;
439 while( *zSql && *zSql!=']' ){ zSql++; }
440 if( *zSql==0 ) return 0;
441 break;
442 }
drh8c82b352000-12-10 18:23:50 +0000443 case '\'': {
444 isComplete = 0;
445 zSql++;
446 while( *zSql && *zSql!='\'' ){ zSql++; }
447 if( *zSql==0 ) return 0;
drh75897232000-05-29 14:26:00 +0000448 break;
drh8c82b352000-12-10 18:23:50 +0000449 }
450 case '"': {
451 isComplete = 0;
452 zSql++;
453 while( *zSql && *zSql!='"' ){ zSql++; }
454 if( *zSql==0 ) return 0;
455 break;
456 }
457 case '-': {
458 if( zSql[1]!='-' ){
459 isComplete = 0;
460 break;
461 }
462 while( *zSql && *zSql!='\n' ){ zSql++; }
463 if( *zSql==0 ) return isComplete;
464 break;
465 }
466 default: {
467 isComplete = 0;
468 break;
469 }
drh75897232000-05-29 14:26:00 +0000470 }
drh8c82b352000-12-10 18:23:50 +0000471 zSql++;
drh75897232000-05-29 14:26:00 +0000472 }
drh8c82b352000-12-10 18:23:50 +0000473 return isComplete;
drh75897232000-05-29 14:26:00 +0000474}
475
476/*
drhbed86902000-06-02 13:27:59 +0000477** Execute SQL code. Return one of the SQLITE_ success/failure
478** codes. Also write an error message into memory obtained from
479** malloc() and make *pzErrMsg point to that message.
480**
481** If the SQL is a query, then for each row in the query result
482** the xCallback() function is called. pArg becomes the first
483** argument to xCallback(). If xCallback=NULL then no callback
484** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000485*/
486int sqlite_exec(
487 sqlite *db, /* The database on which the SQL executes */
drh9f71c2e2001-11-03 23:57:09 +0000488 const char *zSql, /* The SQL to be executed */
drh75897232000-05-29 14:26:00 +0000489 sqlite_callback xCallback, /* Invoke this callback routine */
490 void *pArg, /* First argument to xCallback() */
491 char **pzErrMsg /* Write error messages here */
492){
493 Parse sParse;
drh75897232000-05-29 14:26:00 +0000494
495 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000496 if( (db->flags & SQLITE_Initialized)==0 ){
497 int rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000498 if( rc!=SQLITE_OK ){
499 sqliteStrRealloc(pzErrMsg);
500 return rc;
501 }
drh58b95762000-06-02 01:17:37 +0000502 }
drh75897232000-05-29 14:26:00 +0000503 memset(&sParse, 0, sizeof(sParse));
504 sParse.db = db;
drh5e00f6c2001-09-13 13:46:56 +0000505 sParse.pBe = db->pBe;
drh75897232000-05-29 14:26:00 +0000506 sParse.xCallback = xCallback;
507 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000508 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000509 if( sqlite_malloc_failed ){
510 sqliteSetString(pzErrMsg, "out of memory", 0);
511 sParse.rc = SQLITE_NOMEM;
drh6d4abfb2001-10-22 02:58:08 +0000512 sqliteBtreeRollback(db->pBe);
513 if( db->pBeTemp ) sqliteBtreeRollback(db->pBeTemp);
514 db->flags &= ~SQLITE_InTrans;
515 clearHashTable(db, 0);
drhdaffd0e2001-04-11 14:28:42 +0000516 }
517 sqliteStrRealloc(pzErrMsg);
drh50e5dad2001-09-15 00:57:28 +0000518 if( sParse.rc==SQLITE_SCHEMA ){
drhf57b3392001-10-08 13:22:32 +0000519 clearHashTable(db, 1);
drh50e5dad2001-09-15 00:57:28 +0000520 }
drh4c504392000-10-16 22:06:40 +0000521 return sParse.rc;
drh75897232000-05-29 14:26:00 +0000522}
drh2dfbbca2000-07-28 14:32:48 +0000523
524/*
525** This routine implements a busy callback that sleeps and tries
526** again until a timeout value is reached. The timeout value is
527** an integer number of milliseconds passed in as the first
528** argument.
529*/
drhdaffd0e2001-04-11 14:28:42 +0000530static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000531 void *Timeout, /* Maximum amount of time to wait */
532 const char *NotUsed, /* The name of the table that is busy */
533 int count /* Number of times table has been busy */
534){
drh8cfbf082001-09-19 13:22:39 +0000535#if SQLITE_MIN_SLEEP_MS==1
536 int delay = 10;
drh2dfbbca2000-07-28 14:32:48 +0000537 int prior_delay = 0;
538 int timeout = (int)Timeout;
539 int i;
540
541 for(i=1; i<count; i++){
542 prior_delay += delay;
543 delay = delay*2;
drh8cfbf082001-09-19 13:22:39 +0000544 if( delay>=1000 ){
545 delay = 1000;
546 prior_delay += 1000*(count - i - 1);
drh2dfbbca2000-07-28 14:32:48 +0000547 break;
548 }
549 }
drh3109e022001-10-09 13:46:01 +0000550 if( prior_delay + delay > timeout ){
551 delay = timeout - prior_delay;
drh2dfbbca2000-07-28 14:32:48 +0000552 if( delay<=0 ) return 0;
553 }
drh8cfbf082001-09-19 13:22:39 +0000554 sqliteOsSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000555 return 1;
556#else
557 int timeout = (int)Timeout;
558 if( (count+1)*1000 > timeout ){
559 return 0;
560 }
drh8cfbf082001-09-19 13:22:39 +0000561 sqliteOsSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000562 return 1;
563#endif
564}
565
566/*
567** This routine sets the busy callback for an Sqlite database to the
568** given callback function with the given argument.
569*/
570void sqlite_busy_handler(
571 sqlite *db,
572 int (*xBusy)(void*,const char*,int),
573 void *pArg
574){
575 db->xBusyCallback = xBusy;
576 db->pBusyArg = pArg;
577}
578
579/*
580** This routine installs a default busy handler that waits for the
581** specified number of milliseconds before returning 0.
582*/
583void sqlite_busy_timeout(sqlite *db, int ms){
584 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000585 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000586 }else{
587 sqlite_busy_handler(db, 0, 0);
588 }
589}
drh4c504392000-10-16 22:06:40 +0000590
591/*
592** Cause any pending operation to stop at its earliest opportunity.
593*/
594void sqlite_interrupt(sqlite *db){
595 db->flags |= SQLITE_Interrupt;
596}
drhfa86c412002-02-02 15:01:15 +0000597
598/*
599** Windows systems should call this routine to free memory that
600** is returned in the in the errmsg parameter of sqlite_open() when
601** SQLite is a DLL. For some reason, it does not work to call free()
602** directly.
603**
604** Note that we need to call free() not sqliteFree() here, since every
605** string that is exported from SQLite should have already passed through
606** sqliteStrRealloc().
607*/
608void sqlite_freemem(void *p){ free(p); }
609
610/*
611** Windows systems need functions to call to return the sqlite_version
612** and sqlite_encoding strings.
613*/
614const char *sqlite_libversion(void){ return sqlite_version; }
615const char *sqlite_libencoding(void){ return sqlite_encoding; }