blob: c66f0e8c825c5b444b29db9365b26ac3a1cac791 [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**
drh247be432002-05-10 05:44:55 +000017** $Id: main.c,v 1.70 2002/05/10 05:44:56 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] ){
drh603240c2002-03-05 01:11:12 +000045 case 'c': { /* Recommended pager cache size */
46 int size = atoi(argv[3]);
drhcd61c282002-03-06 22:01:34 +000047 if( size==0 ){ size = MAX_PAGES; }
48 db->cache_size = size;
49 sqliteBtreeSetCacheSize(db->pBe, size);
drh603240c2002-03-05 01:11:12 +000050 break;
51 }
drh4a324312001-12-21 14:30:42 +000052 case 'f': { /* File format */
53 db->file_format = atoi(argv[3]);
54 break;
55 }
56 case 's': { /* Schema cookie */
57 db->schema_cookie = atoi(argv[3]);
58 db->next_cookie = db->schema_cookie;
drhd78eeee2001-09-13 16:18:53 +000059 break;
drh28037572000-08-02 13:47:41 +000060 }
drh17f71932002-02-21 12:01:27 +000061 case 'v':
drhd78eeee2001-09-13 16:18:53 +000062 case 'i':
drh17f71932002-02-21 12:01:27 +000063 case 't': { /* CREATE TABLE, CREATE INDEX, or CREATE VIEW statements */
drhadbca9c2001-09-27 15:11:53 +000064 if( argv[3] && argv[3][0] ){
drh17f71932002-02-21 12:01:27 +000065 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
drh382c0242001-10-06 16:33:02 +000066 ** But because sParse.initFlag is set to 1, no VDBE code is generated
67 ** or executed. All the parser does is build the internal data
drh17f71932002-02-21 12:01:27 +000068 ** structures that describe the table, index, or view.
drh382c0242001-10-06 16:33:02 +000069 */
drhadbca9c2001-09-27 15:11:53 +000070 memset(&sParse, 0, sizeof(sParse));
71 sParse.db = db;
72 sParse.initFlag = 1;
73 sParse.newTnum = atoi(argv[2]);
drhf5bf0a72001-11-23 00:24:12 +000074 sqliteRunParser(&sParse, argv[3], 0);
drhadbca9c2001-09-27 15:11:53 +000075 }else{
drh382c0242001-10-06 16:33:02 +000076 /* If the SQL column is blank it means this is an index that
77 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
drhaacc5432002-01-06 17:07:40 +000078 ** constraint for a CREATE TABLE. The index should have already
drh382c0242001-10-06 16:33:02 +000079 ** been created when we processed the CREATE TABLE. All we have
drhaacc5432002-01-06 17:07:40 +000080 ** to do here is record the root page number for that index.
drh382c0242001-10-06 16:33:02 +000081 */
drhadbca9c2001-09-27 15:11:53 +000082 Index *pIndex = sqliteFindIndex(db, argv[1]);
83 if( pIndex==0 || pIndex->tnum!=0 ){
drhda9e0342002-01-10 14:31:48 +000084 /* This can occur if there exists an index on a TEMP table which
85 ** has the same name as another index on a permanent index. Since
86 ** the permanent table is hidden by the TEMP table, we can also
87 ** safely ignore the index on the permanent table.
88 */
89 /* Do Nothing */;
drhadbca9c2001-09-27 15:11:53 +000090 }else{
91 pIndex->tnum = atoi(argv[2]);
92 }
93 }
drhd78eeee2001-09-13 16:18:53 +000094 break;
95 }
96 default: {
97 /* This can not happen! */
98 nErr = 1;
99 assert( nErr==0 );
100 }
drh28037572000-08-02 13:47:41 +0000101 }
drh75897232000-05-29 14:26:00 +0000102 return nErr;
103}
104
105/*
drh58b95762000-06-02 01:17:37 +0000106** Attempt to read the database schema and initialize internal
107** data structures. Return one of the SQLITE_ error codes to
108** indicate success or failure.
drhbed86902000-06-02 13:27:59 +0000109**
110** After the database is initialized, the SQLITE_Initialized
111** bit is set in the flags field of the sqlite structure. An
112** attempt is made to initialize the database as soon as it
113** is opened. If that fails (perhaps because another process
114** has the sqlite_master table locked) than another attempt
115** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +0000116*/
drh58b95762000-06-02 01:17:37 +0000117static int sqliteInit(sqlite *db, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +0000118 Vdbe *vdbe;
drh58b95762000-06-02 01:17:37 +0000119 int rc;
120
121 /*
122 ** The master database table has a structure like this
123 */
drh75897232000-05-29 14:26:00 +0000124 static char master_schema[] =
125 "CREATE TABLE " MASTER_NAME " (\n"
126 " type text,\n"
127 " name text,\n"
128 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000129 " rootpage integer,\n"
drh75897232000-05-29 14:26:00 +0000130 " sql text\n"
131 ")"
132 ;
133
drhaacc5432002-01-06 17:07:40 +0000134 /* The following VDBE program is used to initialize the internal
drh75897232000-05-29 14:26:00 +0000135 ** structure holding the tables and indexes of the database.
136 ** The database contains a special table named "sqlite_master"
137 ** defined as follows:
138 **
139 ** CREATE TABLE sqlite_master (
drh28037572000-08-02 13:47:41 +0000140 ** type text, -- Either "table" or "index" or "meta"
drh75897232000-05-29 14:26:00 +0000141 ** name text, -- Name of table or index
142 ** tbl_name text, -- Associated table
drhadbca9c2001-09-27 15:11:53 +0000143 ** rootpage integer, -- The integer page number of root page
drh75897232000-05-29 14:26:00 +0000144 ** sql text -- The CREATE statement for this object
145 ** );
146 **
147 ** The sqlite_master table contains a single entry for each table
drh967e8b72000-06-21 13:59:10 +0000148 ** and each index. The "type" column tells whether the entry is
149 ** a table or index. The "name" column is the name of the object.
drh75897232000-05-29 14:26:00 +0000150 ** The "tbl_name" is the name of the associated table. For tables,
drh967e8b72000-06-21 13:59:10 +0000151 ** the tbl_name column is always the same as name. For indices, the
152 ** tbl_name column contains the name of the table that the index
drh382c0242001-10-06 16:33:02 +0000153 ** indexes. The "rootpage" column holds the number of the root page
154 ** for the b-tree for the table or index. Finally, the "sql" column
155 ** contains the complete text of the CREATE TABLE or CREATE INDEX
156 ** statement that originally created the table or index. If an index
157 ** was created to fulfill a PRIMARY KEY or UNIQUE constraint on a table,
158 ** then the "sql" column is NULL.
drh75897232000-05-29 14:26:00 +0000159 **
drh17f71932002-02-21 12:01:27 +0000160 ** In format 1, entries in the sqlite_master table are in a random
161 ** order. Two passes must be made through the table to initialize
162 ** internal data structures. The first pass reads table definitions
163 ** and the second pass read index definitions. Having two passes
164 ** insures that indices appear after their tables.
165 **
166 ** In format 2, entries appear in chronological order. Only a single
167 ** pass needs to be made through the table since everything will be
168 ** in the write order. VIEWs may only occur in format 2.
drh28037572000-08-02 13:47:41 +0000169 **
drh75897232000-05-29 14:26:00 +0000170 ** The following program invokes its callback on the SQL for each
171 ** table then goes back and invokes the callback on the
172 ** SQL for each index. The callback will invoke the
173 ** parser to build the internal representation of the
174 ** database scheme.
175 */
176 static VdbeOp initProg[] = {
drh17f71932002-02-21 12:01:27 +0000177 /* Send the file format to the callback routine
178 */
drh4a324312001-12-21 14:30:42 +0000179 { OP_Open, 0, 2, 0},
180 { OP_String, 0, 0, "file-format"},
181 { OP_String, 0, 0, 0},
182 { OP_String, 0, 0, 0},
183 { OP_ReadCookie, 0, 1, 0},
184 { OP_Callback, 4, 0, 0},
drh17f71932002-02-21 12:01:27 +0000185
drh603240c2002-03-05 01:11:12 +0000186 /* Send the recommended pager cache size to the callback routine
187 */
188 { OP_String, 0, 0, "cache-size"},
189 { OP_String, 0, 0, 0},
190 { OP_String, 0, 0, 0},
191 { OP_ReadCookie, 0, 2, 0},
192 { OP_Callback, 4, 0, 0},
193
drh17f71932002-02-21 12:01:27 +0000194 /* Send the initial schema cookie to the callback
195 */
drh4a324312001-12-21 14:30:42 +0000196 { OP_String, 0, 0, "schema_cookie"},
197 { OP_String, 0, 0, 0},
198 { OP_String, 0, 0, 0},
199 { OP_ReadCookie, 0, 0, 0},
200 { OP_Callback, 4, 0, 0},
drh17f71932002-02-21 12:01:27 +0000201
202 /* Check the file format. If the format number is 2 or more,
203 ** then do a single pass through the SQLITE_MASTER table. For
204 ** a format number of less than 2, jump forward to a different
205 ** algorithm that makes two passes through the SQLITE_MASTER table,
206 ** once for tables and a second time for indices.
207 */
208 { OP_ReadCookie, 0, 1, 0},
209 { OP_Integer, 2, 0, 0},
drh603240c2002-03-05 01:11:12 +0000210 { OP_Lt, 0, 28, 0},
drh17f71932002-02-21 12:01:27 +0000211
212 /* This is the code for doing a single scan through the SQLITE_MASTER
213 ** table. This code runs for format 2 and greater.
214 */
drh603240c2002-03-05 01:11:12 +0000215 { OP_Rewind, 0, 26, 0},
216 { OP_Column, 0, 0, 0}, /* 20 */
drh17f71932002-02-21 12:01:27 +0000217 { OP_Column, 0, 1, 0},
218 { OP_Column, 0, 3, 0},
219 { OP_Column, 0, 4, 0},
220 { OP_Callback, 4, 0, 0},
drh603240c2002-03-05 01:11:12 +0000221 { OP_Next, 0, 20, 0},
222 { OP_Close, 0, 0, 0}, /* 26 */
drh17f71932002-02-21 12:01:27 +0000223 { OP_Halt, 0, 0, 0},
224
225 /* This is the code for doing two passes through SQLITE_MASTER. This
226 ** code runs for file format 1.
227 */
drh603240c2002-03-05 01:11:12 +0000228 { OP_Rewind, 0, 48, 0}, /* 28 */
229 { OP_Column, 0, 0, 0}, /* 29 */
drh4a324312001-12-21 14:30:42 +0000230 { OP_String, 0, 0, "table"},
drh603240c2002-03-05 01:11:12 +0000231 { OP_Ne, 0, 37, 0},
drh4a324312001-12-21 14:30:42 +0000232 { OP_Column, 0, 0, 0},
233 { OP_Column, 0, 1, 0},
234 { OP_Column, 0, 3, 0},
235 { OP_Column, 0, 4, 0},
236 { OP_Callback, 4, 0, 0},
drh603240c2002-03-05 01:11:12 +0000237 { OP_Next, 0, 29, 0}, /* 37 */
238 { OP_Rewind, 0, 48, 0}, /* 38 */
239 { OP_Column, 0, 0, 0}, /* 39 */
drh4a324312001-12-21 14:30:42 +0000240 { OP_String, 0, 0, "index"},
drh603240c2002-03-05 01:11:12 +0000241 { OP_Ne, 0, 47, 0},
drh4a324312001-12-21 14:30:42 +0000242 { OP_Column, 0, 0, 0},
243 { OP_Column, 0, 1, 0},
244 { OP_Column, 0, 3, 0},
245 { OP_Column, 0, 4, 0},
246 { OP_Callback, 4, 0, 0},
drh603240c2002-03-05 01:11:12 +0000247 { OP_Next, 0, 39, 0}, /* 47 */
248 { OP_Close, 0, 0, 0}, /* 48 */
drh4a324312001-12-21 14:30:42 +0000249 { OP_Halt, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000250 };
251
drh58b95762000-06-02 01:17:37 +0000252 /* Create a virtual machine to run the initialization program. Run
drh382c0242001-10-06 16:33:02 +0000253 ** the program. Then delete the virtual machine.
drh58b95762000-06-02 01:17:37 +0000254 */
drh4c504392000-10-16 22:06:40 +0000255 vdbe = sqliteVdbeCreate(db);
drhd8bc7082000-06-07 23:51:50 +0000256 if( vdbe==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000257 sqliteSetString(pzErrMsg, "out of memory", 0);
drhdaffd0e2001-04-11 14:28:42 +0000258 return SQLITE_NOMEM;
drhd8bc7082000-06-07 23:51:50 +0000259 }
drh58b95762000-06-02 01:17:37 +0000260 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
drh2dfbbca2000-07-28 14:32:48 +0000261 rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg,
262 db->pBusyArg, db->xBusyCallback);
drh58b95762000-06-02 01:17:37 +0000263 sqliteVdbeDelete(vdbe);
drh4a324312001-12-21 14:30:42 +0000264 if( rc==SQLITE_OK && db->nTable==0 ){
drh17f71932002-02-21 12:01:27 +0000265 db->file_format = 2;
drh4a324312001-12-21 14:30:42 +0000266 }
drh17f71932002-02-21 12:01:27 +0000267 if( rc==SQLITE_OK && db->file_format>2 ){
drhd78eeee2001-09-13 16:18:53 +0000268 sqliteSetString(pzErrMsg, "unsupported file format", 0);
drh28037572000-08-02 13:47:41 +0000269 rc = SQLITE_ERROR;
270 }
drhaacc5432002-01-06 17:07:40 +0000271
272 /* The schema for the SQLITE_MASTER table is not stored in the
273 ** database itself. We have to invoke the callback one extra
274 ** time to get it to process the SQLITE_MASTER table defintion.
275 */
drh58b95762000-06-02 01:17:37 +0000276 if( rc==SQLITE_OK ){
277 Table *pTab;
drhe3c41372001-09-17 20:25:58 +0000278 char *azArg[6];
drhd78eeee2001-09-13 16:18:53 +0000279 azArg[0] = "table";
280 azArg[1] = MASTER_NAME;
281 azArg[2] = "2";
drhadbca9c2001-09-27 15:11:53 +0000282 azArg[3] = master_schema;
283 azArg[4] = 0;
284 sqliteOpenCb(db, 4, azArg, 0);
drh58b95762000-06-02 01:17:37 +0000285 pTab = sqliteFindTable(db, MASTER_NAME);
286 if( pTab ){
287 pTab->readOnly = 1;
288 }
289 db->flags |= SQLITE_Initialized;
drh5e00f6c2001-09-13 13:46:56 +0000290 sqliteCommitInternalChanges(db);
drh58b95762000-06-02 01:17:37 +0000291 }
292 return rc;
293}
294
295/*
drhb217a572000-08-22 13:40:18 +0000296** The version of the library
297*/
drh3d0b5592000-08-22 13:40:51 +0000298const char sqlite_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000299
300/*
drh297ecf12001-04-05 15:57:13 +0000301** Does the library expect data to be encoded as UTF-8 or iso8859? The
302** following global constant always lets us know.
303*/
304#ifdef SQLITE_UTF8
drhfbc3eab2001-04-06 16:13:42 +0000305const char sqlite_encoding[] = "UTF-8";
drh297ecf12001-04-05 15:57:13 +0000306#else
drhfbc3eab2001-04-06 16:13:42 +0000307const char sqlite_encoding[] = "iso8859";
drh297ecf12001-04-05 15:57:13 +0000308#endif
309
310/*
drh58b95762000-06-02 01:17:37 +0000311** Open a new SQLite database. Construct an "sqlite" structure to define
312** the state of this database and return a pointer to that structure.
313**
314** An attempt is made to initialize the in-memory data structures that
315** hold the database schema. But if this fails (because the schema file
316** is locked) then that step is deferred until the first call to
317** sqlite_exec().
318*/
319sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
320 sqlite *db;
321 int rc;
322
323 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000324 db = sqliteMalloc( sizeof(sqlite) );
325 if( pzErrMsg ) *pzErrMsg = 0;
drhdaffd0e2001-04-11 14:28:42 +0000326 if( db==0 ) goto no_mem_on_open;
drhbeae3192001-09-22 18:12:08 +0000327 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
328 sqliteHashInit(&db->idxHash, SQLITE_HASH_STRING, 0);
drh74e24cd2002-01-09 03:19:59 +0000329 sqliteHashInit(&db->tblDrop, SQLITE_HASH_POINTER, 0);
330 sqliteHashInit(&db->idxDrop, SQLITE_HASH_POINTER, 0);
drh0bce8352002-02-28 00:41:10 +0000331 sqliteHashInit(&db->aFunc, SQLITE_HASH_STRING, 1);
drhdc04c582002-02-24 01:55:15 +0000332 sqliteRegisterBuildinFunctions(db);
drh1c928532002-01-31 15:54:21 +0000333 db->onError = OE_Default;
drh5cf8e8c2002-02-19 22:42:05 +0000334 db->priorNewRowid = 0;
drh247be432002-05-10 05:44:55 +0000335 db->magic = SQLITE_MAGIC_BUSY;
drh75897232000-05-29 14:26:00 +0000336
337 /* Open the backend database driver */
drha1b351a2001-09-14 16:42:12 +0000338 rc = sqliteBtreeOpen(zFilename, mode, MAX_PAGES, &db->pBe);
drh5e00f6c2001-09-13 13:46:56 +0000339 if( rc!=SQLITE_OK ){
340 switch( rc ){
341 default: {
drhaacc5432002-01-06 17:07:40 +0000342 sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0);
drh5e00f6c2001-09-13 13:46:56 +0000343 }
344 }
drh75897232000-05-29 14:26:00 +0000345 sqliteFree(db);
drh5edc3122001-09-13 21:53:09 +0000346 sqliteStrRealloc(pzErrMsg);
drhbe0072d2001-09-13 14:46:09 +0000347 return 0;
drh75897232000-05-29 14:26:00 +0000348 }
349
drh58b95762000-06-02 01:17:37 +0000350 /* Attempt to read the schema */
351 rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000352 if( sqlite_malloc_failed ){
drh6d4abfb2001-10-22 02:58:08 +0000353 sqlite_close(db);
drhdaffd0e2001-04-11 14:28:42 +0000354 goto no_mem_on_open;
355 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
drh58b95762000-06-02 01:17:37 +0000356 sqlite_close(db);
drh5edc3122001-09-13 21:53:09 +0000357 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000358 return 0;
drhaacc5432002-01-06 17:07:40 +0000359 }else if( pzErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +0000360 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000361 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000362 }
drh247be432002-05-10 05:44:55 +0000363 db->magic = SQLITE_MAGIC_OPEN;
drh75897232000-05-29 14:26:00 +0000364 return db;
drhdaffd0e2001-04-11 14:28:42 +0000365
366no_mem_on_open:
367 sqliteSetString(pzErrMsg, "out of memory", 0);
368 sqliteStrRealloc(pzErrMsg);
369 return 0;
drh75897232000-05-29 14:26:00 +0000370}
371
372/*
drhf57b3392001-10-08 13:22:32 +0000373** Erase all schema information from the schema hash table. Except
374** tables that are created using CREATE TEMPORARY TABLE are preserved
drhaacc5432002-01-06 17:07:40 +0000375** if the preserveTemps flag is true.
drh50e5dad2001-09-15 00:57:28 +0000376**
377** The database schema is normally read in once when the database
378** is first opened and stored in a hash table in the sqlite structure.
379** This routine erases the stored schema. This erasure occurs because
380** either the database is being closed or because some other process
381** changed the schema and this process needs to reread it.
drh75897232000-05-29 14:26:00 +0000382*/
drhf57b3392001-10-08 13:22:32 +0000383static void clearHashTable(sqlite *db, int preserveTemps){
drhbeae3192001-09-22 18:12:08 +0000384 HashElem *pElem;
385 Hash temp1;
drh74e24cd2002-01-09 03:19:59 +0000386 assert( sqliteHashFirst(&db->tblDrop)==0 ); /* There can not be uncommitted */
387 assert( sqliteHashFirst(&db->idxDrop)==0 ); /* DROP TABLEs or DROP INDEXs */
drhbeae3192001-09-22 18:12:08 +0000388 temp1 = db->tblHash;
389 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
390 sqliteHashClear(&db->idxHash);
391 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
drhf57b3392001-10-08 13:22:32 +0000392 Table *pTab = sqliteHashData(pElem);
393 if( preserveTemps && pTab->isTemp ){
394 Index *pIdx;
drh6d4abfb2001-10-22 02:58:08 +0000395 int nName = strlen(pTab->zName);
396 Table *pOld = sqliteHashInsert(&db->tblHash, pTab->zName, nName+1, pTab);
397 if( pOld!=0 ){
398 assert( pOld==pTab ); /* Malloc failed on the HashInsert */
399 sqliteDeleteTable(db, pOld);
400 continue;
401 }
drhf57b3392001-10-08 13:22:32 +0000402 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
403 int n = strlen(pIdx->zName)+1;
drh6d4abfb2001-10-22 02:58:08 +0000404 Index *pOldIdx;
405 pOldIdx = sqliteHashInsert(&db->idxHash, pIdx->zName, n, pIdx);
406 if( pOld ){
407 assert( pOldIdx==pIdx );
408 sqliteUnlinkAndDeleteIndex(db, pOldIdx);
409 }
drhf57b3392001-10-08 13:22:32 +0000410 }
411 }else{
412 sqliteDeleteTable(db, pTab);
413 }
drh75897232000-05-29 14:26:00 +0000414 }
drhbeae3192001-09-22 18:12:08 +0000415 sqliteHashClear(&temp1);
drh50e5dad2001-09-15 00:57:28 +0000416 db->flags &= ~SQLITE_Initialized;
417}
418
419/*
drhaf9ff332002-01-16 21:00:27 +0000420** Return the ROWID of the most recent insert
421*/
422int sqlite_last_insert_rowid(sqlite *db){
423 return db->lastRowid;
424}
425
426/*
drhc8d30ac2002-04-12 10:08:59 +0000427** Return the number of changes in the most recent call to sqlite_exec().
428*/
429int sqlite_changes(sqlite *db){
430 return db->nChange;
431}
432
433/*
drh50e5dad2001-09-15 00:57:28 +0000434** Close an existing SQLite database
435*/
436void sqlite_close(sqlite *db){
drh8e0a2f92002-02-23 23:45:45 +0000437 HashElem *i;
drh247be432002-05-10 05:44:55 +0000438 if( sqliteSafetyOn(db) ){ return; }
439 db->magic = SQLITE_MAGIC_CLOSED;
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 }
drh0bce8352002-02-28 00:41:10 +0000445 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
446 FuncDef *pFunc, *pNext;
447 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
drh8e0a2f92002-02-23 23:45:45 +0000448 pNext = pFunc->pNext;
449 sqliteFree(pFunc);
450 }
451 }
drh0bce8352002-02-28 00:41:10 +0000452 sqliteHashClear(&db->aFunc);
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;
drh247be432002-05-10 05:44:55 +0000533 if( sqliteSafetyOn(db) ){ return SQLITE_MISUSE; }
drh58b95762000-06-02 01:17:37 +0000534 if( (db->flags & SQLITE_Initialized)==0 ){
535 int rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000536 if( rc!=SQLITE_OK ){
537 sqliteStrRealloc(pzErrMsg);
drh247be432002-05-10 05:44:55 +0000538 sqliteSafetyOff(db);
drhdaffd0e2001-04-11 14:28:42 +0000539 return rc;
540 }
drh58b95762000-06-02 01:17:37 +0000541 }
drhc8d30ac2002-04-12 10:08:59 +0000542 if( db->recursionDepth==0 ){ db->nChange = 0; }
543 db->recursionDepth++;
drh75897232000-05-29 14:26:00 +0000544 memset(&sParse, 0, sizeof(sParse));
545 sParse.db = db;
drh5e00f6c2001-09-13 13:46:56 +0000546 sParse.pBe = db->pBe;
drh75897232000-05-29 14:26:00 +0000547 sParse.xCallback = xCallback;
548 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000549 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000550 if( sqlite_malloc_failed ){
551 sqliteSetString(pzErrMsg, "out of memory", 0);
552 sParse.rc = SQLITE_NOMEM;
drh6d4abfb2001-10-22 02:58:08 +0000553 sqliteBtreeRollback(db->pBe);
554 if( db->pBeTemp ) sqliteBtreeRollback(db->pBeTemp);
555 db->flags &= ~SQLITE_InTrans;
556 clearHashTable(db, 0);
drhdaffd0e2001-04-11 14:28:42 +0000557 }
558 sqliteStrRealloc(pzErrMsg);
drh50e5dad2001-09-15 00:57:28 +0000559 if( sParse.rc==SQLITE_SCHEMA ){
drhf57b3392001-10-08 13:22:32 +0000560 clearHashTable(db, 1);
drh50e5dad2001-09-15 00:57:28 +0000561 }
drhc8d30ac2002-04-12 10:08:59 +0000562 db->recursionDepth--;
drh247be432002-05-10 05:44:55 +0000563 if( sqliteSafetyOff(db) ){ sParse.rc = SQLITE_MISUSE; }
drh4c504392000-10-16 22:06:40 +0000564 return sParse.rc;
drh75897232000-05-29 14:26:00 +0000565}
drh2dfbbca2000-07-28 14:32:48 +0000566
567/*
drh247be432002-05-10 05:44:55 +0000568** Change the magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
569** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
570** when this routine is called.
571**
572** This routine is a attempt to detect if two threads attempt
573** to use the same sqlite* pointer at the same time. There is a
574** race condition so it is possible that the error is not detected.
575** But usually the problem will be seen. The result will be an
576** error which can be used to debugging the application that is
577** using SQLite incorrectly.
578*/
579int sqliteSafetyOn(sqlite *db){
580 if( db->magic==SQLITE_MAGIC_OPEN ){
581 db->magic = SQLITE_MAGIC_BUSY;
582 return 0;
583 }else{
584 db->magic = SQLITE_MAGIC_ERROR;
585 db->flags |= SQLITE_Interrupt;
586 return 1;
587 }
588}
589
590/*
591** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
592** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
593** when this routine is called.
594*/
595int sqliteSafetyOff(sqlite *db){
596 if( db->magic==SQLITE_MAGIC_BUSY ){
597 db->magic = SQLITE_MAGIC_OPEN;
598 return 0;
599 }else{
600 db->magic = SQLITE_MAGIC_ERROR;
601 db->flags |= SQLITE_Interrupt;
602 return 1;
603 }
604}
605
606/*
drh2dfbbca2000-07-28 14:32:48 +0000607** This routine implements a busy callback that sleeps and tries
608** again until a timeout value is reached. The timeout value is
609** an integer number of milliseconds passed in as the first
610** argument.
611*/
drhdaffd0e2001-04-11 14:28:42 +0000612static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000613 void *Timeout, /* Maximum amount of time to wait */
614 const char *NotUsed, /* The name of the table that is busy */
615 int count /* Number of times table has been busy */
616){
drh8cfbf082001-09-19 13:22:39 +0000617#if SQLITE_MIN_SLEEP_MS==1
618 int delay = 10;
drh2dfbbca2000-07-28 14:32:48 +0000619 int prior_delay = 0;
620 int timeout = (int)Timeout;
621 int i;
622
623 for(i=1; i<count; i++){
624 prior_delay += delay;
625 delay = delay*2;
drh8cfbf082001-09-19 13:22:39 +0000626 if( delay>=1000 ){
627 delay = 1000;
628 prior_delay += 1000*(count - i - 1);
drh2dfbbca2000-07-28 14:32:48 +0000629 break;
630 }
631 }
drh3109e022001-10-09 13:46:01 +0000632 if( prior_delay + delay > timeout ){
633 delay = timeout - prior_delay;
drh2dfbbca2000-07-28 14:32:48 +0000634 if( delay<=0 ) return 0;
635 }
drh8cfbf082001-09-19 13:22:39 +0000636 sqliteOsSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000637 return 1;
638#else
639 int timeout = (int)Timeout;
640 if( (count+1)*1000 > timeout ){
641 return 0;
642 }
drh8cfbf082001-09-19 13:22:39 +0000643 sqliteOsSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000644 return 1;
645#endif
646}
647
648/*
649** This routine sets the busy callback for an Sqlite database to the
650** given callback function with the given argument.
651*/
652void sqlite_busy_handler(
653 sqlite *db,
654 int (*xBusy)(void*,const char*,int),
655 void *pArg
656){
657 db->xBusyCallback = xBusy;
658 db->pBusyArg = pArg;
659}
660
661/*
662** This routine installs a default busy handler that waits for the
663** specified number of milliseconds before returning 0.
664*/
665void sqlite_busy_timeout(sqlite *db, int ms){
666 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000667 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000668 }else{
669 sqlite_busy_handler(db, 0, 0);
670 }
671}
drh4c504392000-10-16 22:06:40 +0000672
673/*
674** Cause any pending operation to stop at its earliest opportunity.
675*/
676void sqlite_interrupt(sqlite *db){
677 db->flags |= SQLITE_Interrupt;
678}
drhfa86c412002-02-02 15:01:15 +0000679
680/*
681** Windows systems should call this routine to free memory that
682** is returned in the in the errmsg parameter of sqlite_open() when
683** SQLite is a DLL. For some reason, it does not work to call free()
684** directly.
685**
686** Note that we need to call free() not sqliteFree() here, since every
687** string that is exported from SQLite should have already passed through
688** sqliteStrRealloc().
689*/
690void sqlite_freemem(void *p){ free(p); }
691
692/*
693** Windows systems need functions to call to return the sqlite_version
694** and sqlite_encoding strings.
695*/
696const char *sqlite_libversion(void){ return sqlite_version; }
697const char *sqlite_libencoding(void){ return sqlite_encoding; }
drh8e0a2f92002-02-23 23:45:45 +0000698
699/*
700** Create new user-defined functions. The sqlite_create_function()
701** routine creates a regular function and sqlite_create_aggregate()
702** creates an aggregate function.
703**
704** Passing a NULL xFunc argument or NULL xStep and xFinalize arguments
705** disables the function. Calling sqlite_create_function() with the
706** same name and number of arguments as a prior call to
707** sqlite_create_aggregate() disables the prior call to
708** sqlite_create_aggregate(), and vice versa.
709**
710** If nArg is -1 it means that this function will accept any number
711** of arguments, including 0.
712*/
713int sqlite_create_function(
714 sqlite *db, /* Add the function to this database connection */
715 const char *zName, /* Name of the function to add */
716 int nArg, /* Number of arguments */
drh1350b032002-02-27 19:00:20 +0000717 void (*xFunc)(sqlite_func*,int,const char**), /* The implementation */
718 void *pUserData /* User data */
drh8e0a2f92002-02-23 23:45:45 +0000719){
drh0bce8352002-02-28 00:41:10 +0000720 FuncDef *p;
drh8e0a2f92002-02-23 23:45:45 +0000721 if( db==0 || zName==0 ) return 1;
drh0bce8352002-02-28 00:41:10 +0000722 p = sqliteFindFunction(db, zName, strlen(zName), nArg, 1);
drh4e0f9952002-02-27 01:53:13 +0000723 if( p==0 ) return 1;
drh8e0a2f92002-02-23 23:45:45 +0000724 p->xFunc = xFunc;
725 p->xStep = 0;
726 p->xFinalize = 0;
drh1350b032002-02-27 19:00:20 +0000727 p->pUserData = pUserData;
drh8e0a2f92002-02-23 23:45:45 +0000728 return 0;
729}
730int sqlite_create_aggregate(
731 sqlite *db, /* Add the function to this database connection */
732 const char *zName, /* Name of the function to add */
733 int nArg, /* Number of arguments */
drh1350b032002-02-27 19:00:20 +0000734 void (*xStep)(sqlite_func*,int,const char**), /* The step function */
735 void (*xFinalize)(sqlite_func*), /* The finalizer */
736 void *pUserData /* User data */
drh8e0a2f92002-02-23 23:45:45 +0000737){
drh0bce8352002-02-28 00:41:10 +0000738 FuncDef *p;
drh8e0a2f92002-02-23 23:45:45 +0000739 if( db==0 || zName==0 ) return 1;
drh0bce8352002-02-28 00:41:10 +0000740 p = sqliteFindFunction(db, zName, strlen(zName), nArg, 1);
drh4e0f9952002-02-27 01:53:13 +0000741 if( p==0 ) return 1;
drh8e0a2f92002-02-23 23:45:45 +0000742 p->xFunc = 0;
743 p->xStep = xStep;
744 p->xFinalize = xFinalize;
drh1350b032002-02-27 19:00:20 +0000745 p->pUserData = pUserData;
drh8e0a2f92002-02-23 23:45:45 +0000746 return 0;
747}