blob: 4253ae12514b7a4867e20507ffc5d21a4fc6d038 [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**
drh8e0a2f92002-02-23 23:45:45 +000017** $Id: main.c,v 1.62 2002/02/23 23:45:45 drh Exp $
drh75897232000-05-29 14:26:00 +000018*/
drh8e0a2f92002-02-23 23:45:45 +000019#include <ctype.h>
drh75897232000-05-29 14:26:00 +000020#include "sqliteInt.h"
drh8cfbf082001-09-19 13:22:39 +000021#include "os.h"
drh75897232000-05-29 14:26:00 +000022
23/*
24** This is the callback routine for the code that initializes the
drh382c0242001-10-06 16:33:02 +000025** database. See sqliteInit() below for additional information.
26**
27** Each callback contains the following information:
drh28037572000-08-02 13:47:41 +000028**
drh4a324312001-12-21 14:30:42 +000029** argv[0] = "file-format" or "schema-cookie" or "table" or "index"
drhe3c41372001-09-17 20:25:58 +000030** argv[1] = table or index name or meta statement type.
31** argv[2] = root page number for table or index. NULL for meta.
drhadbca9c2001-09-27 15:11:53 +000032** argv[3] = SQL create statement for the table or index
drhd78eeee2001-09-13 16:18:53 +000033**
drh75897232000-05-29 14:26:00 +000034*/
35static int sqliteOpenCb(void *pDb, int argc, char **argv, char **azColName){
36 sqlite *db = (sqlite*)pDb;
37 Parse sParse;
drhd78eeee2001-09-13 16:18:53 +000038 int nErr = 0;
drh75897232000-05-29 14:26:00 +000039
drh382c0242001-10-06 16:33:02 +000040 /* TODO: Do some validity checks on all fields. In particular,
41 ** make sure fields do not contain NULLs. Otherwise we might core
42 ** when attempting to initialize from a corrupt database file. */
drhe3c41372001-09-17 20:25:58 +000043
drhadbca9c2001-09-27 15:11:53 +000044 assert( argc==4 );
drhd78eeee2001-09-13 16:18:53 +000045 switch( argv[0][0] ){
drh4a324312001-12-21 14:30:42 +000046 case 'f': { /* File format */
47 db->file_format = atoi(argv[3]);
48 break;
49 }
50 case 's': { /* Schema cookie */
51 db->schema_cookie = atoi(argv[3]);
52 db->next_cookie = db->schema_cookie;
drhd78eeee2001-09-13 16:18:53 +000053 break;
drh28037572000-08-02 13:47:41 +000054 }
drh17f71932002-02-21 12:01:27 +000055 case 'v':
drhd78eeee2001-09-13 16:18:53 +000056 case 'i':
drh17f71932002-02-21 12:01:27 +000057 case 't': { /* CREATE TABLE, CREATE INDEX, or CREATE VIEW statements */
drhadbca9c2001-09-27 15:11:53 +000058 if( argv[3] && argv[3][0] ){
drh17f71932002-02-21 12:01:27 +000059 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
drh382c0242001-10-06 16:33:02 +000060 ** But because sParse.initFlag is set to 1, no VDBE code is generated
61 ** or executed. All the parser does is build the internal data
drh17f71932002-02-21 12:01:27 +000062 ** structures that describe the table, index, or view.
drh382c0242001-10-06 16:33:02 +000063 */
drhadbca9c2001-09-27 15:11:53 +000064 memset(&sParse, 0, sizeof(sParse));
65 sParse.db = db;
66 sParse.initFlag = 1;
67 sParse.newTnum = atoi(argv[2]);
drhf5bf0a72001-11-23 00:24:12 +000068 sqliteRunParser(&sParse, argv[3], 0);
drhadbca9c2001-09-27 15:11:53 +000069 }else{
drh382c0242001-10-06 16:33:02 +000070 /* If the SQL column is blank it means this is an index that
71 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
drhaacc5432002-01-06 17:07:40 +000072 ** constraint for a CREATE TABLE. The index should have already
drh382c0242001-10-06 16:33:02 +000073 ** been created when we processed the CREATE TABLE. All we have
drhaacc5432002-01-06 17:07:40 +000074 ** to do here is record the root page number for that index.
drh382c0242001-10-06 16:33:02 +000075 */
drhadbca9c2001-09-27 15:11:53 +000076 Index *pIndex = sqliteFindIndex(db, argv[1]);
77 if( pIndex==0 || pIndex->tnum!=0 ){
drhda9e0342002-01-10 14:31:48 +000078 /* This can occur if there exists an index on a TEMP table which
79 ** has the same name as another index on a permanent index. Since
80 ** the permanent table is hidden by the TEMP table, we can also
81 ** safely ignore the index on the permanent table.
82 */
83 /* Do Nothing */;
drhadbca9c2001-09-27 15:11:53 +000084 }else{
85 pIndex->tnum = atoi(argv[2]);
86 }
87 }
drhd78eeee2001-09-13 16:18:53 +000088 break;
89 }
90 default: {
91 /* This can not happen! */
92 nErr = 1;
93 assert( nErr==0 );
94 }
drh28037572000-08-02 13:47:41 +000095 }
drh75897232000-05-29 14:26:00 +000096 return nErr;
97}
98
99/*
drh58b95762000-06-02 01:17:37 +0000100** Attempt to read the database schema and initialize internal
101** data structures. Return one of the SQLITE_ error codes to
102** indicate success or failure.
drhbed86902000-06-02 13:27:59 +0000103**
104** After the database is initialized, the SQLITE_Initialized
105** bit is set in the flags field of the sqlite structure. An
106** attempt is made to initialize the database as soon as it
107** is opened. If that fails (perhaps because another process
108** has the sqlite_master table locked) than another attempt
109** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +0000110*/
drh58b95762000-06-02 01:17:37 +0000111static int sqliteInit(sqlite *db, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +0000112 Vdbe *vdbe;
drh58b95762000-06-02 01:17:37 +0000113 int rc;
114
115 /*
116 ** The master database table has a structure like this
117 */
drh75897232000-05-29 14:26:00 +0000118 static char master_schema[] =
119 "CREATE TABLE " MASTER_NAME " (\n"
120 " type text,\n"
121 " name text,\n"
122 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000123 " rootpage integer,\n"
drh75897232000-05-29 14:26:00 +0000124 " sql text\n"
125 ")"
126 ;
127
drhaacc5432002-01-06 17:07:40 +0000128 /* The following VDBE program is used to initialize the internal
drh75897232000-05-29 14:26:00 +0000129 ** structure holding the tables and indexes of the database.
130 ** The database contains a special table named "sqlite_master"
131 ** defined as follows:
132 **
133 ** CREATE TABLE sqlite_master (
drh28037572000-08-02 13:47:41 +0000134 ** type text, -- Either "table" or "index" or "meta"
drh75897232000-05-29 14:26:00 +0000135 ** name text, -- Name of table or index
136 ** tbl_name text, -- Associated table
drhadbca9c2001-09-27 15:11:53 +0000137 ** rootpage integer, -- The integer page number of root page
drh75897232000-05-29 14:26:00 +0000138 ** sql text -- The CREATE statement for this object
139 ** );
140 **
141 ** The sqlite_master table contains a single entry for each table
drh967e8b72000-06-21 13:59:10 +0000142 ** and each index. The "type" column tells whether the entry is
143 ** a table or index. The "name" column is the name of the object.
drh75897232000-05-29 14:26:00 +0000144 ** The "tbl_name" is the name of the associated table. For tables,
drh967e8b72000-06-21 13:59:10 +0000145 ** the tbl_name column is always the same as name. For indices, the
146 ** tbl_name column contains the name of the table that the index
drh382c0242001-10-06 16:33:02 +0000147 ** indexes. The "rootpage" column holds the number of the root page
148 ** for the b-tree for the table or index. Finally, the "sql" column
149 ** contains the complete text of the CREATE TABLE or CREATE INDEX
150 ** statement that originally created the table or index. If an index
151 ** was created to fulfill a PRIMARY KEY or UNIQUE constraint on a table,
152 ** then the "sql" column is NULL.
drh75897232000-05-29 14:26:00 +0000153 **
drh17f71932002-02-21 12:01:27 +0000154 ** In format 1, entries in the sqlite_master table are in a random
155 ** order. Two passes must be made through the table to initialize
156 ** internal data structures. The first pass reads table definitions
157 ** and the second pass read index definitions. Having two passes
158 ** insures that indices appear after their tables.
159 **
160 ** In format 2, entries appear in chronological order. Only a single
161 ** pass needs to be made through the table since everything will be
162 ** in the write order. VIEWs may only occur in format 2.
drh28037572000-08-02 13:47:41 +0000163 **
drh75897232000-05-29 14:26:00 +0000164 ** The following program invokes its callback on the SQL for each
165 ** table then goes back and invokes the callback on the
166 ** SQL for each index. The callback will invoke the
167 ** parser to build the internal representation of the
168 ** database scheme.
169 */
170 static VdbeOp initProg[] = {
drh17f71932002-02-21 12:01:27 +0000171 /* Send the file format to the callback routine
172 */
drh4a324312001-12-21 14:30:42 +0000173 { OP_Open, 0, 2, 0},
174 { OP_String, 0, 0, "file-format"},
175 { OP_String, 0, 0, 0},
176 { OP_String, 0, 0, 0},
177 { OP_ReadCookie, 0, 1, 0},
178 { OP_Callback, 4, 0, 0},
drh17f71932002-02-21 12:01:27 +0000179
180 /* Send the initial schema cookie to the callback
181 */
drh4a324312001-12-21 14:30:42 +0000182 { OP_String, 0, 0, "schema_cookie"},
183 { OP_String, 0, 0, 0},
184 { OP_String, 0, 0, 0},
185 { OP_ReadCookie, 0, 0, 0},
186 { OP_Callback, 4, 0, 0},
drh17f71932002-02-21 12:01:27 +0000187
188 /* Check the file format. If the format number is 2 or more,
189 ** then do a single pass through the SQLITE_MASTER table. For
190 ** a format number of less than 2, jump forward to a different
191 ** algorithm that makes two passes through the SQLITE_MASTER table,
192 ** once for tables and a second time for indices.
193 */
194 { OP_ReadCookie, 0, 1, 0},
195 { OP_Integer, 2, 0, 0},
196 { OP_Lt, 0, 23, 0},
197
198 /* This is the code for doing a single scan through the SQLITE_MASTER
199 ** table. This code runs for format 2 and greater.
200 */
201 { OP_Rewind, 0, 21, 0},
202 { OP_Column, 0, 0, 0}, /* 15 */
203 { OP_Column, 0, 1, 0},
204 { OP_Column, 0, 3, 0},
205 { OP_Column, 0, 4, 0},
206 { OP_Callback, 4, 0, 0},
207 { OP_Next, 0, 15, 0},
208 { OP_Close, 0, 0, 0}, /* 21 */
209 { OP_Halt, 0, 0, 0},
210
211 /* This is the code for doing two passes through SQLITE_MASTER. This
212 ** code runs for file format 1.
213 */
214 { OP_Rewind, 0, 43, 0}, /* 23 */
215 { OP_Column, 0, 0, 0}, /* 24 */
drh4a324312001-12-21 14:30:42 +0000216 { OP_String, 0, 0, "table"},
drh17f71932002-02-21 12:01:27 +0000217 { OP_Ne, 0, 32, 0},
drh4a324312001-12-21 14:30:42 +0000218 { OP_Column, 0, 0, 0},
219 { OP_Column, 0, 1, 0},
220 { OP_Column, 0, 3, 0},
221 { OP_Column, 0, 4, 0},
222 { OP_Callback, 4, 0, 0},
drh17f71932002-02-21 12:01:27 +0000223 { OP_Next, 0, 24, 0}, /* 32 */
224 { OP_Rewind, 0, 43, 0}, /* 33 */
225 { OP_Column, 0, 0, 0}, /* 34 */
drh4a324312001-12-21 14:30:42 +0000226 { OP_String, 0, 0, "index"},
drh17f71932002-02-21 12:01:27 +0000227 { OP_Ne, 0, 42, 0},
drh4a324312001-12-21 14:30:42 +0000228 { OP_Column, 0, 0, 0},
229 { OP_Column, 0, 1, 0},
230 { OP_Column, 0, 3, 0},
231 { OP_Column, 0, 4, 0},
232 { OP_Callback, 4, 0, 0},
drh17f71932002-02-21 12:01:27 +0000233 { OP_Next, 0, 34, 0}, /* 42 */
234 { OP_Close, 0, 0, 0}, /* 43 */
drh4a324312001-12-21 14:30:42 +0000235 { OP_Halt, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000236 };
237
drh58b95762000-06-02 01:17:37 +0000238 /* Create a virtual machine to run the initialization program. Run
drh382c0242001-10-06 16:33:02 +0000239 ** the program. Then delete the virtual machine.
drh58b95762000-06-02 01:17:37 +0000240 */
drh4c504392000-10-16 22:06:40 +0000241 vdbe = sqliteVdbeCreate(db);
drhd8bc7082000-06-07 23:51:50 +0000242 if( vdbe==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000243 sqliteSetString(pzErrMsg, "out of memory", 0);
drhdaffd0e2001-04-11 14:28:42 +0000244 return SQLITE_NOMEM;
drhd8bc7082000-06-07 23:51:50 +0000245 }
drh58b95762000-06-02 01:17:37 +0000246 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
drh2dfbbca2000-07-28 14:32:48 +0000247 rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg,
248 db->pBusyArg, db->xBusyCallback);
drh58b95762000-06-02 01:17:37 +0000249 sqliteVdbeDelete(vdbe);
drh4a324312001-12-21 14:30:42 +0000250 if( rc==SQLITE_OK && db->nTable==0 ){
drh17f71932002-02-21 12:01:27 +0000251 db->file_format = 2;
drh4a324312001-12-21 14:30:42 +0000252 }
drh17f71932002-02-21 12:01:27 +0000253 if( rc==SQLITE_OK && db->file_format>2 ){
drhd78eeee2001-09-13 16:18:53 +0000254 sqliteSetString(pzErrMsg, "unsupported file format", 0);
drh28037572000-08-02 13:47:41 +0000255 rc = SQLITE_ERROR;
256 }
drhaacc5432002-01-06 17:07:40 +0000257
258 /* The schema for the SQLITE_MASTER table is not stored in the
259 ** database itself. We have to invoke the callback one extra
260 ** time to get it to process the SQLITE_MASTER table defintion.
261 */
drh58b95762000-06-02 01:17:37 +0000262 if( rc==SQLITE_OK ){
263 Table *pTab;
drhe3c41372001-09-17 20:25:58 +0000264 char *azArg[6];
drhd78eeee2001-09-13 16:18:53 +0000265 azArg[0] = "table";
266 azArg[1] = MASTER_NAME;
267 azArg[2] = "2";
drhadbca9c2001-09-27 15:11:53 +0000268 azArg[3] = master_schema;
269 azArg[4] = 0;
270 sqliteOpenCb(db, 4, azArg, 0);
drh58b95762000-06-02 01:17:37 +0000271 pTab = sqliteFindTable(db, MASTER_NAME);
272 if( pTab ){
273 pTab->readOnly = 1;
274 }
275 db->flags |= SQLITE_Initialized;
drh5e00f6c2001-09-13 13:46:56 +0000276 sqliteCommitInternalChanges(db);
drh58b95762000-06-02 01:17:37 +0000277 }
278 return rc;
279}
280
281/*
drhb217a572000-08-22 13:40:18 +0000282** The version of the library
283*/
drh3d0b5592000-08-22 13:40:51 +0000284const char sqlite_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000285
286/*
drh297ecf12001-04-05 15:57:13 +0000287** Does the library expect data to be encoded as UTF-8 or iso8859? The
288** following global constant always lets us know.
289*/
290#ifdef SQLITE_UTF8
drhfbc3eab2001-04-06 16:13:42 +0000291const char sqlite_encoding[] = "UTF-8";
drh297ecf12001-04-05 15:57:13 +0000292#else
drhfbc3eab2001-04-06 16:13:42 +0000293const char sqlite_encoding[] = "iso8859";
drh297ecf12001-04-05 15:57:13 +0000294#endif
295
296/*
drh8e0a2f92002-02-23 23:45:45 +0000297** Implementation of the upper() and lower() SQL functions.
298*/
299static void upperFunc(void *context, int argc, const char **argv){
300 char *z;
301 int i;
302 if( argc<1 || argv[0]==0 ) return;
303 z = sqlite_set_result_string(context, argv[0], -1);
304 if( z==0 ) return;
305 for(i=0; z[i]; i++){
306 if( islower(z[i]) ) z[i] = toupper(z[i]);
307 }
308}
309static void lowerFunc(void *context, int argc, const char **argv){
310 char *z;
311 int i;
312 if( argc<1 || argv[0]==0 ) return;
313 z = sqlite_set_result_string(context, argv[0], -1);
314 if( z==0 ) return;
315 for(i=0; z[i]; i++){
316 if( isupper(z[i]) ) z[i] = tolower(z[i]);
317 }
318}
319
320/*
drh58b95762000-06-02 01:17:37 +0000321** Open a new SQLite database. Construct an "sqlite" structure to define
322** the state of this database and return a pointer to that structure.
323**
324** An attempt is made to initialize the in-memory data structures that
325** hold the database schema. But if this fails (because the schema file
326** is locked) then that step is deferred until the first call to
327** sqlite_exec().
328*/
329sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
330 sqlite *db;
331 int rc;
332
333 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000334 db = sqliteMalloc( sizeof(sqlite) );
335 if( pzErrMsg ) *pzErrMsg = 0;
drhdaffd0e2001-04-11 14:28:42 +0000336 if( db==0 ) goto no_mem_on_open;
drhbeae3192001-09-22 18:12:08 +0000337 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
338 sqliteHashInit(&db->idxHash, SQLITE_HASH_STRING, 0);
drh74e24cd2002-01-09 03:19:59 +0000339 sqliteHashInit(&db->tblDrop, SQLITE_HASH_POINTER, 0);
340 sqliteHashInit(&db->idxDrop, SQLITE_HASH_POINTER, 0);
drh8e0a2f92002-02-23 23:45:45 +0000341 sqliteHashInit(&db->userFunc, SQLITE_HASH_STRING, 1);
342 sqlite_create_function(db, "upper", 1, upperFunc);
343 sqlite_create_function(db, "lower", 1, lowerFunc);
drh1c928532002-01-31 15:54:21 +0000344 db->onError = OE_Default;
drh5cf8e8c2002-02-19 22:42:05 +0000345 db->priorNewRowid = 0;
drh75897232000-05-29 14:26:00 +0000346
347 /* Open the backend database driver */
drha1b351a2001-09-14 16:42:12 +0000348 rc = sqliteBtreeOpen(zFilename, mode, MAX_PAGES, &db->pBe);
drh5e00f6c2001-09-13 13:46:56 +0000349 if( rc!=SQLITE_OK ){
350 switch( rc ){
351 default: {
drhaacc5432002-01-06 17:07:40 +0000352 sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0);
drh5e00f6c2001-09-13 13:46:56 +0000353 }
354 }
drh75897232000-05-29 14:26:00 +0000355 sqliteFree(db);
drh5edc3122001-09-13 21:53:09 +0000356 sqliteStrRealloc(pzErrMsg);
drhbe0072d2001-09-13 14:46:09 +0000357 return 0;
drh75897232000-05-29 14:26:00 +0000358 }
359
drh58b95762000-06-02 01:17:37 +0000360 /* Attempt to read the schema */
361 rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000362 if( sqlite_malloc_failed ){
drh6d4abfb2001-10-22 02:58:08 +0000363 sqlite_close(db);
drhdaffd0e2001-04-11 14:28:42 +0000364 goto no_mem_on_open;
365 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
drh58b95762000-06-02 01:17:37 +0000366 sqlite_close(db);
drh5edc3122001-09-13 21:53:09 +0000367 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000368 return 0;
drhaacc5432002-01-06 17:07:40 +0000369 }else if( pzErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +0000370 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000371 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000372 }
drh75897232000-05-29 14:26:00 +0000373 return db;
drhdaffd0e2001-04-11 14:28:42 +0000374
375no_mem_on_open:
376 sqliteSetString(pzErrMsg, "out of memory", 0);
377 sqliteStrRealloc(pzErrMsg);
378 return 0;
drh75897232000-05-29 14:26:00 +0000379}
380
381/*
drhf57b3392001-10-08 13:22:32 +0000382** Erase all schema information from the schema hash table. Except
383** tables that are created using CREATE TEMPORARY TABLE are preserved
drhaacc5432002-01-06 17:07:40 +0000384** if the preserveTemps flag is true.
drh50e5dad2001-09-15 00:57:28 +0000385**
386** The database schema is normally read in once when the database
387** is first opened and stored in a hash table in the sqlite structure.
388** This routine erases the stored schema. This erasure occurs because
389** either the database is being closed or because some other process
390** changed the schema and this process needs to reread it.
drh75897232000-05-29 14:26:00 +0000391*/
drhf57b3392001-10-08 13:22:32 +0000392static void clearHashTable(sqlite *db, int preserveTemps){
drhbeae3192001-09-22 18:12:08 +0000393 HashElem *pElem;
394 Hash temp1;
drh74e24cd2002-01-09 03:19:59 +0000395 assert( sqliteHashFirst(&db->tblDrop)==0 ); /* There can not be uncommitted */
396 assert( sqliteHashFirst(&db->idxDrop)==0 ); /* DROP TABLEs or DROP INDEXs */
drhbeae3192001-09-22 18:12:08 +0000397 temp1 = db->tblHash;
398 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
399 sqliteHashClear(&db->idxHash);
400 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
drhf57b3392001-10-08 13:22:32 +0000401 Table *pTab = sqliteHashData(pElem);
402 if( preserveTemps && pTab->isTemp ){
403 Index *pIdx;
drh6d4abfb2001-10-22 02:58:08 +0000404 int nName = strlen(pTab->zName);
405 Table *pOld = sqliteHashInsert(&db->tblHash, pTab->zName, nName+1, pTab);
406 if( pOld!=0 ){
407 assert( pOld==pTab ); /* Malloc failed on the HashInsert */
408 sqliteDeleteTable(db, pOld);
409 continue;
410 }
drhf57b3392001-10-08 13:22:32 +0000411 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
412 int n = strlen(pIdx->zName)+1;
drh6d4abfb2001-10-22 02:58:08 +0000413 Index *pOldIdx;
414 pOldIdx = sqliteHashInsert(&db->idxHash, pIdx->zName, n, pIdx);
415 if( pOld ){
416 assert( pOldIdx==pIdx );
417 sqliteUnlinkAndDeleteIndex(db, pOldIdx);
418 }
drhf57b3392001-10-08 13:22:32 +0000419 }
420 }else{
421 sqliteDeleteTable(db, pTab);
422 }
drh75897232000-05-29 14:26:00 +0000423 }
drhbeae3192001-09-22 18:12:08 +0000424 sqliteHashClear(&temp1);
drh50e5dad2001-09-15 00:57:28 +0000425 db->flags &= ~SQLITE_Initialized;
426}
427
428/*
drhaf9ff332002-01-16 21:00:27 +0000429** Return the ROWID of the most recent insert
430*/
431int sqlite_last_insert_rowid(sqlite *db){
432 return db->lastRowid;
433}
434
435/*
drh50e5dad2001-09-15 00:57:28 +0000436** Close an existing SQLite database
437*/
438void sqlite_close(sqlite *db){
drh8e0a2f92002-02-23 23:45:45 +0000439 HashElem *i;
drh50e5dad2001-09-15 00:57:28 +0000440 sqliteBtreeClose(db->pBe);
drhf57b3392001-10-08 13:22:32 +0000441 clearHashTable(db, 0);
442 if( db->pBeTemp ){
443 sqliteBtreeClose(db->pBeTemp);
444 }
drh8e0a2f92002-02-23 23:45:45 +0000445 for(i=sqliteHashFirst(&db->userFunc); i; i=sqliteHashNext(i)){
446 UserFunc *pFunc, *pNext;
447 for(pFunc = (UserFunc*)sqliteHashData(i); pFunc; pFunc=pNext){
448 pNext = pFunc->pNext;
449 sqliteFree(pFunc);
450 }
451 }
452 sqliteHashClear(&db->userFunc);
drh75897232000-05-29 14:26:00 +0000453 sqliteFree(db);
454}
455
456/*
457** Return TRUE if the given SQL string ends in a semicolon.
458*/
459int sqlite_complete(const char *zSql){
drh8c82b352000-12-10 18:23:50 +0000460 int isComplete = 0;
461 while( *zSql ){
462 switch( *zSql ){
463 case ';': {
464 isComplete = 1;
drh75897232000-05-29 14:26:00 +0000465 break;
drh8c82b352000-12-10 18:23:50 +0000466 }
467 case ' ':
468 case '\t':
469 case '\n':
470 case '\f': {
drh75897232000-05-29 14:26:00 +0000471 break;
drh8c82b352000-12-10 18:23:50 +0000472 }
drh969fa7c2002-02-18 18:30:32 +0000473 case '[': {
474 isComplete = 0;
475 zSql++;
476 while( *zSql && *zSql!=']' ){ zSql++; }
477 if( *zSql==0 ) return 0;
478 break;
479 }
drh8c82b352000-12-10 18:23:50 +0000480 case '\'': {
481 isComplete = 0;
482 zSql++;
483 while( *zSql && *zSql!='\'' ){ zSql++; }
484 if( *zSql==0 ) return 0;
drh75897232000-05-29 14:26:00 +0000485 break;
drh8c82b352000-12-10 18:23:50 +0000486 }
487 case '"': {
488 isComplete = 0;
489 zSql++;
490 while( *zSql && *zSql!='"' ){ zSql++; }
491 if( *zSql==0 ) return 0;
492 break;
493 }
494 case '-': {
495 if( zSql[1]!='-' ){
496 isComplete = 0;
497 break;
498 }
499 while( *zSql && *zSql!='\n' ){ zSql++; }
500 if( *zSql==0 ) return isComplete;
501 break;
502 }
503 default: {
504 isComplete = 0;
505 break;
506 }
drh75897232000-05-29 14:26:00 +0000507 }
drh8c82b352000-12-10 18:23:50 +0000508 zSql++;
drh75897232000-05-29 14:26:00 +0000509 }
drh8c82b352000-12-10 18:23:50 +0000510 return isComplete;
drh75897232000-05-29 14:26:00 +0000511}
512
513/*
drhbed86902000-06-02 13:27:59 +0000514** Execute SQL code. Return one of the SQLITE_ success/failure
515** codes. Also write an error message into memory obtained from
516** malloc() and make *pzErrMsg point to that message.
517**
518** If the SQL is a query, then for each row in the query result
519** the xCallback() function is called. pArg becomes the first
520** argument to xCallback(). If xCallback=NULL then no callback
521** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000522*/
523int sqlite_exec(
524 sqlite *db, /* The database on which the SQL executes */
drh9f71c2e2001-11-03 23:57:09 +0000525 const char *zSql, /* The SQL to be executed */
drh75897232000-05-29 14:26:00 +0000526 sqlite_callback xCallback, /* Invoke this callback routine */
527 void *pArg, /* First argument to xCallback() */
528 char **pzErrMsg /* Write error messages here */
529){
530 Parse sParse;
drh75897232000-05-29 14:26:00 +0000531
532 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000533 if( (db->flags & SQLITE_Initialized)==0 ){
534 int rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000535 if( rc!=SQLITE_OK ){
536 sqliteStrRealloc(pzErrMsg);
537 return rc;
538 }
drh58b95762000-06-02 01:17:37 +0000539 }
drh75897232000-05-29 14:26:00 +0000540 memset(&sParse, 0, sizeof(sParse));
541 sParse.db = db;
drh5e00f6c2001-09-13 13:46:56 +0000542 sParse.pBe = db->pBe;
drh75897232000-05-29 14:26:00 +0000543 sParse.xCallback = xCallback;
544 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000545 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000546 if( sqlite_malloc_failed ){
547 sqliteSetString(pzErrMsg, "out of memory", 0);
548 sParse.rc = SQLITE_NOMEM;
drh6d4abfb2001-10-22 02:58:08 +0000549 sqliteBtreeRollback(db->pBe);
550 if( db->pBeTemp ) sqliteBtreeRollback(db->pBeTemp);
551 db->flags &= ~SQLITE_InTrans;
552 clearHashTable(db, 0);
drhdaffd0e2001-04-11 14:28:42 +0000553 }
554 sqliteStrRealloc(pzErrMsg);
drh50e5dad2001-09-15 00:57:28 +0000555 if( sParse.rc==SQLITE_SCHEMA ){
drhf57b3392001-10-08 13:22:32 +0000556 clearHashTable(db, 1);
drh50e5dad2001-09-15 00:57:28 +0000557 }
drh4c504392000-10-16 22:06:40 +0000558 return sParse.rc;
drh75897232000-05-29 14:26:00 +0000559}
drh2dfbbca2000-07-28 14:32:48 +0000560
561/*
562** This routine implements a busy callback that sleeps and tries
563** again until a timeout value is reached. The timeout value is
564** an integer number of milliseconds passed in as the first
565** argument.
566*/
drhdaffd0e2001-04-11 14:28:42 +0000567static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000568 void *Timeout, /* Maximum amount of time to wait */
569 const char *NotUsed, /* The name of the table that is busy */
570 int count /* Number of times table has been busy */
571){
drh8cfbf082001-09-19 13:22:39 +0000572#if SQLITE_MIN_SLEEP_MS==1
573 int delay = 10;
drh2dfbbca2000-07-28 14:32:48 +0000574 int prior_delay = 0;
575 int timeout = (int)Timeout;
576 int i;
577
578 for(i=1; i<count; i++){
579 prior_delay += delay;
580 delay = delay*2;
drh8cfbf082001-09-19 13:22:39 +0000581 if( delay>=1000 ){
582 delay = 1000;
583 prior_delay += 1000*(count - i - 1);
drh2dfbbca2000-07-28 14:32:48 +0000584 break;
585 }
586 }
drh3109e022001-10-09 13:46:01 +0000587 if( prior_delay + delay > timeout ){
588 delay = timeout - prior_delay;
drh2dfbbca2000-07-28 14:32:48 +0000589 if( delay<=0 ) return 0;
590 }
drh8cfbf082001-09-19 13:22:39 +0000591 sqliteOsSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000592 return 1;
593#else
594 int timeout = (int)Timeout;
595 if( (count+1)*1000 > timeout ){
596 return 0;
597 }
drh8cfbf082001-09-19 13:22:39 +0000598 sqliteOsSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000599 return 1;
600#endif
601}
602
603/*
604** This routine sets the busy callback for an Sqlite database to the
605** given callback function with the given argument.
606*/
607void sqlite_busy_handler(
608 sqlite *db,
609 int (*xBusy)(void*,const char*,int),
610 void *pArg
611){
612 db->xBusyCallback = xBusy;
613 db->pBusyArg = pArg;
614}
615
616/*
617** This routine installs a default busy handler that waits for the
618** specified number of milliseconds before returning 0.
619*/
620void sqlite_busy_timeout(sqlite *db, int ms){
621 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000622 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000623 }else{
624 sqlite_busy_handler(db, 0, 0);
625 }
626}
drh4c504392000-10-16 22:06:40 +0000627
628/*
629** Cause any pending operation to stop at its earliest opportunity.
630*/
631void sqlite_interrupt(sqlite *db){
632 db->flags |= SQLITE_Interrupt;
633}
drhfa86c412002-02-02 15:01:15 +0000634
635/*
636** Windows systems should call this routine to free memory that
637** is returned in the in the errmsg parameter of sqlite_open() when
638** SQLite is a DLL. For some reason, it does not work to call free()
639** directly.
640**
641** Note that we need to call free() not sqliteFree() here, since every
642** string that is exported from SQLite should have already passed through
643** sqliteStrRealloc().
644*/
645void sqlite_freemem(void *p){ free(p); }
646
647/*
648** Windows systems need functions to call to return the sqlite_version
649** and sqlite_encoding strings.
650*/
651const char *sqlite_libversion(void){ return sqlite_version; }
652const char *sqlite_libencoding(void){ return sqlite_encoding; }
drh8e0a2f92002-02-23 23:45:45 +0000653
654/*
655** Create new user-defined functions. The sqlite_create_function()
656** routine creates a regular function and sqlite_create_aggregate()
657** creates an aggregate function.
658**
659** Passing a NULL xFunc argument or NULL xStep and xFinalize arguments
660** disables the function. Calling sqlite_create_function() with the
661** same name and number of arguments as a prior call to
662** sqlite_create_aggregate() disables the prior call to
663** sqlite_create_aggregate(), and vice versa.
664**
665** If nArg is -1 it means that this function will accept any number
666** of arguments, including 0.
667*/
668int sqlite_create_function(
669 sqlite *db, /* Add the function to this database connection */
670 const char *zName, /* Name of the function to add */
671 int nArg, /* Number of arguments */
672 void (*xFunc)(void*,int,const char**) /* Implementation of the function */
673){
674 UserFunc *p;
675 if( db==0 || zName==0 ) return 1;
676 p = sqliteFindUserFunction(db, zName, strlen(zName), nArg, 1);
677 p->xFunc = xFunc;
678 p->xStep = 0;
679 p->xFinalize = 0;
680 return 0;
681}
682int sqlite_create_aggregate(
683 sqlite *db, /* Add the function to this database connection */
684 const char *zName, /* Name of the function to add */
685 int nArg, /* Number of arguments */
686 void *(*xStep)(void*,int,const char**), /* The step function */
687 void (*xFinalize)(void*,void*) /* The finalizer */
688){
689 UserFunc *p;
690 if( db==0 || zName==0 ) return 1;
691 p = sqliteFindUserFunction(db, zName, strlen(zName), nArg, 1);
692 p->xFunc = 0;
693 p->xStep = xStep;
694 p->xFinalize = xFinalize;
695 return 0;
696}