blob: 84614c5626c105a7135e7fcc07725fc718cf2426 [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**
drhe0140fc2002-06-16 18:21:44 +000017** $Id: main.c,v 1.80 2002/06/16 18:21:44 drh Exp $
drh75897232000-05-29 14:26:00 +000018*/
19#include "sqliteInt.h"
drh8cfbf082001-09-19 13:22:39 +000020#include "os.h"
drhce9079c2002-05-15 14:17:44 +000021#include <ctype.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*/
drhe0140fc2002-06-16 18:21:44 +000035int sqliteInitCallback(void *pDb, int argc, char **argv, char **azColName){
drh75897232000-05-29 14:26:00 +000036 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] ){
drh603240c2002-03-05 01:11:12 +000046 case 'c': { /* Recommended pager cache size */
47 int size = atoi(argv[3]);
drhcd61c282002-03-06 22:01:34 +000048 if( size==0 ){ size = MAX_PAGES; }
49 db->cache_size = size;
50 sqliteBtreeSetCacheSize(db->pBe, size);
drh603240c2002-03-05 01:11:12 +000051 break;
52 }
drh4a324312001-12-21 14:30:42 +000053 case 'f': { /* File format */
54 db->file_format = atoi(argv[3]);
55 break;
56 }
57 case 's': { /* Schema cookie */
58 db->schema_cookie = atoi(argv[3]);
59 db->next_cookie = db->schema_cookie;
drhd78eeee2001-09-13 16:18:53 +000060 break;
drh28037572000-08-02 13:47:41 +000061 }
drh17f71932002-02-21 12:01:27 +000062 case 'v':
drhd78eeee2001-09-13 16:18:53 +000063 case 'i':
drh17f71932002-02-21 12:01:27 +000064 case 't': { /* CREATE TABLE, CREATE INDEX, or CREATE VIEW statements */
drhadbca9c2001-09-27 15:11:53 +000065 if( argv[3] && argv[3][0] ){
drh17f71932002-02-21 12:01:27 +000066 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
drh382c0242001-10-06 16:33:02 +000067 ** But because sParse.initFlag is set to 1, no VDBE code is generated
68 ** or executed. All the parser does is build the internal data
drh17f71932002-02-21 12:01:27 +000069 ** structures that describe the table, index, or view.
drh382c0242001-10-06 16:33:02 +000070 */
drhadbca9c2001-09-27 15:11:53 +000071 memset(&sParse, 0, sizeof(sParse));
72 sParse.db = db;
73 sParse.initFlag = 1;
74 sParse.newTnum = atoi(argv[2]);
drhf5bf0a72001-11-23 00:24:12 +000075 sqliteRunParser(&sParse, argv[3], 0);
drhadbca9c2001-09-27 15:11:53 +000076 }else{
drh382c0242001-10-06 16:33:02 +000077 /* If the SQL column is blank it means this is an index that
78 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
drhaacc5432002-01-06 17:07:40 +000079 ** constraint for a CREATE TABLE. The index should have already
drh382c0242001-10-06 16:33:02 +000080 ** been created when we processed the CREATE TABLE. All we have
drhaacc5432002-01-06 17:07:40 +000081 ** to do here is record the root page number for that index.
drh382c0242001-10-06 16:33:02 +000082 */
drhadbca9c2001-09-27 15:11:53 +000083 Index *pIndex = sqliteFindIndex(db, argv[1]);
84 if( pIndex==0 || pIndex->tnum!=0 ){
drhda9e0342002-01-10 14:31:48 +000085 /* This can occur if there exists an index on a TEMP table which
86 ** has the same name as another index on a permanent index. Since
87 ** the permanent table is hidden by the TEMP table, we can also
88 ** safely ignore the index on the permanent table.
89 */
90 /* Do Nothing */;
drhadbca9c2001-09-27 15:11:53 +000091 }else{
92 pIndex->tnum = atoi(argv[2]);
93 }
94 }
drhd78eeee2001-09-13 16:18:53 +000095 break;
96 }
97 default: {
98 /* This can not happen! */
99 nErr = 1;
100 assert( nErr==0 );
101 }
drh28037572000-08-02 13:47:41 +0000102 }
drh75897232000-05-29 14:26:00 +0000103 return nErr;
104}
105
106/*
drh58b95762000-06-02 01:17:37 +0000107** Attempt to read the database schema and initialize internal
108** data structures. Return one of the SQLITE_ error codes to
109** indicate success or failure.
drhbed86902000-06-02 13:27:59 +0000110**
111** After the database is initialized, the SQLITE_Initialized
112** bit is set in the flags field of the sqlite structure. An
113** attempt is made to initialize the database as soon as it
114** is opened. If that fails (perhaps because another process
115** has the sqlite_master table locked) than another attempt
116** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +0000117*/
drh58b95762000-06-02 01:17:37 +0000118static int sqliteInit(sqlite *db, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +0000119 Vdbe *vdbe;
drh58b95762000-06-02 01:17:37 +0000120 int rc;
121
122 /*
123 ** The master database table has a structure like this
124 */
drh75897232000-05-29 14:26:00 +0000125 static char master_schema[] =
126 "CREATE TABLE " MASTER_NAME " (\n"
127 " type text,\n"
128 " name text,\n"
129 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000130 " rootpage integer,\n"
drh75897232000-05-29 14:26:00 +0000131 " sql text\n"
132 ")"
133 ;
134
drhaacc5432002-01-06 17:07:40 +0000135 /* The following VDBE program is used to initialize the internal
drh75897232000-05-29 14:26:00 +0000136 ** structure holding the tables and indexes of the database.
137 ** The database contains a special table named "sqlite_master"
138 ** defined as follows:
139 **
140 ** CREATE TABLE sqlite_master (
drh28037572000-08-02 13:47:41 +0000141 ** type text, -- Either "table" or "index" or "meta"
drh75897232000-05-29 14:26:00 +0000142 ** name text, -- Name of table or index
143 ** tbl_name text, -- Associated table
drhadbca9c2001-09-27 15:11:53 +0000144 ** rootpage integer, -- The integer page number of root page
drh75897232000-05-29 14:26:00 +0000145 ** sql text -- The CREATE statement for this object
146 ** );
147 **
148 ** The sqlite_master table contains a single entry for each table
drh967e8b72000-06-21 13:59:10 +0000149 ** and each index. The "type" column tells whether the entry is
150 ** a table or index. The "name" column is the name of the object.
drh75897232000-05-29 14:26:00 +0000151 ** The "tbl_name" is the name of the associated table. For tables,
drh967e8b72000-06-21 13:59:10 +0000152 ** the tbl_name column is always the same as name. For indices, the
153 ** tbl_name column contains the name of the table that the index
drh382c0242001-10-06 16:33:02 +0000154 ** indexes. The "rootpage" column holds the number of the root page
155 ** for the b-tree for the table or index. Finally, the "sql" column
156 ** contains the complete text of the CREATE TABLE or CREATE INDEX
157 ** statement that originally created the table or index. If an index
158 ** was created to fulfill a PRIMARY KEY or UNIQUE constraint on a table,
159 ** then the "sql" column is NULL.
drh75897232000-05-29 14:26:00 +0000160 **
drh17f71932002-02-21 12:01:27 +0000161 ** In format 1, entries in the sqlite_master table are in a random
162 ** order. Two passes must be made through the table to initialize
163 ** internal data structures. The first pass reads table definitions
164 ** and the second pass read index definitions. Having two passes
165 ** insures that indices appear after their tables.
166 **
167 ** In format 2, entries appear in chronological order. Only a single
168 ** pass needs to be made through the table since everything will be
169 ** in the write order. VIEWs may only occur in format 2.
drh28037572000-08-02 13:47:41 +0000170 **
drh75897232000-05-29 14:26:00 +0000171 ** The following program invokes its callback on the SQL for each
172 ** table then goes back and invokes the callback on the
173 ** SQL for each index. The callback will invoke the
174 ** parser to build the internal representation of the
175 ** database scheme.
176 */
177 static VdbeOp initProg[] = {
drh17f71932002-02-21 12:01:27 +0000178 /* Send the file format to the callback routine
179 */
drh4a324312001-12-21 14:30:42 +0000180 { OP_Open, 0, 2, 0},
181 { OP_String, 0, 0, "file-format"},
182 { OP_String, 0, 0, 0},
183 { OP_String, 0, 0, 0},
184 { OP_ReadCookie, 0, 1, 0},
185 { OP_Callback, 4, 0, 0},
drh17f71932002-02-21 12:01:27 +0000186
drh603240c2002-03-05 01:11:12 +0000187 /* Send the recommended pager cache size to the callback routine
188 */
189 { OP_String, 0, 0, "cache-size"},
190 { OP_String, 0, 0, 0},
191 { OP_String, 0, 0, 0},
192 { OP_ReadCookie, 0, 2, 0},
193 { OP_Callback, 4, 0, 0},
194
drh17f71932002-02-21 12:01:27 +0000195 /* Send the initial schema cookie to the callback
196 */
drh4a324312001-12-21 14:30:42 +0000197 { OP_String, 0, 0, "schema_cookie"},
198 { OP_String, 0, 0, 0},
199 { OP_String, 0, 0, 0},
200 { OP_ReadCookie, 0, 0, 0},
201 { OP_Callback, 4, 0, 0},
drh17f71932002-02-21 12:01:27 +0000202
203 /* Check the file format. If the format number is 2 or more,
204 ** then do a single pass through the SQLITE_MASTER table. For
205 ** a format number of less than 2, jump forward to a different
206 ** algorithm that makes two passes through the SQLITE_MASTER table,
207 ** once for tables and a second time for indices.
208 */
209 { OP_ReadCookie, 0, 1, 0},
210 { OP_Integer, 2, 0, 0},
drh603240c2002-03-05 01:11:12 +0000211 { OP_Lt, 0, 28, 0},
drh17f71932002-02-21 12:01:27 +0000212
213 /* This is the code for doing a single scan through the SQLITE_MASTER
214 ** table. This code runs for format 2 and greater.
215 */
drh603240c2002-03-05 01:11:12 +0000216 { OP_Rewind, 0, 26, 0},
217 { OP_Column, 0, 0, 0}, /* 20 */
drh17f71932002-02-21 12:01:27 +0000218 { OP_Column, 0, 1, 0},
219 { OP_Column, 0, 3, 0},
220 { OP_Column, 0, 4, 0},
221 { OP_Callback, 4, 0, 0},
drh603240c2002-03-05 01:11:12 +0000222 { OP_Next, 0, 20, 0},
223 { OP_Close, 0, 0, 0}, /* 26 */
drh17f71932002-02-21 12:01:27 +0000224 { OP_Halt, 0, 0, 0},
225
226 /* This is the code for doing two passes through SQLITE_MASTER. This
227 ** code runs for file format 1.
228 */
drh603240c2002-03-05 01:11:12 +0000229 { OP_Rewind, 0, 48, 0}, /* 28 */
230 { OP_Column, 0, 0, 0}, /* 29 */
drh4a324312001-12-21 14:30:42 +0000231 { OP_String, 0, 0, "table"},
drh603240c2002-03-05 01:11:12 +0000232 { OP_Ne, 0, 37, 0},
drh4a324312001-12-21 14:30:42 +0000233 { OP_Column, 0, 0, 0},
234 { OP_Column, 0, 1, 0},
235 { OP_Column, 0, 3, 0},
236 { OP_Column, 0, 4, 0},
237 { OP_Callback, 4, 0, 0},
drh603240c2002-03-05 01:11:12 +0000238 { OP_Next, 0, 29, 0}, /* 37 */
239 { OP_Rewind, 0, 48, 0}, /* 38 */
240 { OP_Column, 0, 0, 0}, /* 39 */
drh4a324312001-12-21 14:30:42 +0000241 { OP_String, 0, 0, "index"},
drh603240c2002-03-05 01:11:12 +0000242 { OP_Ne, 0, 47, 0},
drh4a324312001-12-21 14:30:42 +0000243 { OP_Column, 0, 0, 0},
244 { OP_Column, 0, 1, 0},
245 { OP_Column, 0, 3, 0},
246 { OP_Column, 0, 4, 0},
247 { OP_Callback, 4, 0, 0},
drh603240c2002-03-05 01:11:12 +0000248 { OP_Next, 0, 39, 0}, /* 47 */
249 { OP_Close, 0, 0, 0}, /* 48 */
drh4a324312001-12-21 14:30:42 +0000250 { OP_Halt, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000251 };
252
drh58b95762000-06-02 01:17:37 +0000253 /* Create a virtual machine to run the initialization program. Run
drh382c0242001-10-06 16:33:02 +0000254 ** the program. Then delete the virtual machine.
drh58b95762000-06-02 01:17:37 +0000255 */
drh4c504392000-10-16 22:06:40 +0000256 vdbe = sqliteVdbeCreate(db);
drhd8bc7082000-06-07 23:51:50 +0000257 if( vdbe==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000258 sqliteSetString(pzErrMsg, "out of memory", 0);
drhdaffd0e2001-04-11 14:28:42 +0000259 return SQLITE_NOMEM;
drhd8bc7082000-06-07 23:51:50 +0000260 }
drh58b95762000-06-02 01:17:37 +0000261 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
drhe0140fc2002-06-16 18:21:44 +0000262 rc = sqliteVdbeExec(vdbe, sqliteInitCallback, db, pzErrMsg,
drh2dfbbca2000-07-28 14:32:48 +0000263 db->pBusyArg, db->xBusyCallback);
drh58b95762000-06-02 01:17:37 +0000264 sqliteVdbeDelete(vdbe);
drh4a324312001-12-21 14:30:42 +0000265 if( rc==SQLITE_OK && db->nTable==0 ){
drh17f71932002-02-21 12:01:27 +0000266 db->file_format = 2;
drh4a324312001-12-21 14:30:42 +0000267 }
drh17f71932002-02-21 12:01:27 +0000268 if( rc==SQLITE_OK && db->file_format>2 ){
drhd78eeee2001-09-13 16:18:53 +0000269 sqliteSetString(pzErrMsg, "unsupported file format", 0);
drh28037572000-08-02 13:47:41 +0000270 rc = SQLITE_ERROR;
271 }
drhaacc5432002-01-06 17:07:40 +0000272
273 /* The schema for the SQLITE_MASTER table is not stored in the
274 ** database itself. We have to invoke the callback one extra
275 ** time to get it to process the SQLITE_MASTER table defintion.
276 */
drh58b95762000-06-02 01:17:37 +0000277 if( rc==SQLITE_OK ){
278 Table *pTab;
drhe3c41372001-09-17 20:25:58 +0000279 char *azArg[6];
drhd78eeee2001-09-13 16:18:53 +0000280 azArg[0] = "table";
281 azArg[1] = MASTER_NAME;
282 azArg[2] = "2";
drhadbca9c2001-09-27 15:11:53 +0000283 azArg[3] = master_schema;
284 azArg[4] = 0;
drhe0140fc2002-06-16 18:21:44 +0000285 sqliteInitCallback(db, 4, azArg, 0);
drh58b95762000-06-02 01:17:37 +0000286 pTab = sqliteFindTable(db, MASTER_NAME);
287 if( pTab ){
288 pTab->readOnly = 1;
289 }
290 db->flags |= SQLITE_Initialized;
drh5e00f6c2001-09-13 13:46:56 +0000291 sqliteCommitInternalChanges(db);
drh58b95762000-06-02 01:17:37 +0000292 }
293 return rc;
294}
295
296/*
drhb217a572000-08-22 13:40:18 +0000297** The version of the library
298*/
drh3d0b5592000-08-22 13:40:51 +0000299const char sqlite_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000300
301/*
drh297ecf12001-04-05 15:57:13 +0000302** Does the library expect data to be encoded as UTF-8 or iso8859? The
303** following global constant always lets us know.
304*/
305#ifdef SQLITE_UTF8
drhfbc3eab2001-04-06 16:13:42 +0000306const char sqlite_encoding[] = "UTF-8";
drh297ecf12001-04-05 15:57:13 +0000307#else
drhfbc3eab2001-04-06 16:13:42 +0000308const char sqlite_encoding[] = "iso8859";
drh297ecf12001-04-05 15:57:13 +0000309#endif
310
311/*
drh58b95762000-06-02 01:17:37 +0000312** Open a new SQLite database. Construct an "sqlite" structure to define
313** the state of this database and return a pointer to that structure.
314**
315** An attempt is made to initialize the in-memory data structures that
316** hold the database schema. But if this fails (because the schema file
317** is locked) then that step is deferred until the first call to
318** sqlite_exec().
319*/
320sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
321 sqlite *db;
322 int rc;
323
324 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000325 db = sqliteMalloc( sizeof(sqlite) );
326 if( pzErrMsg ) *pzErrMsg = 0;
drhdaffd0e2001-04-11 14:28:42 +0000327 if( db==0 ) goto no_mem_on_open;
drhbeae3192001-09-22 18:12:08 +0000328 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
329 sqliteHashInit(&db->idxHash, SQLITE_HASH_STRING, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000330 sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0);
331 sqliteHashInit(&db->trigDrop, SQLITE_HASH_STRING, 0);
drh74e24cd2002-01-09 03:19:59 +0000332 sqliteHashInit(&db->tblDrop, SQLITE_HASH_POINTER, 0);
333 sqliteHashInit(&db->idxDrop, SQLITE_HASH_POINTER, 0);
drh0bce8352002-02-28 00:41:10 +0000334 sqliteHashInit(&db->aFunc, SQLITE_HASH_STRING, 1);
drh28f4b682002-06-09 10:14:18 +0000335 sqliteRegisterBuiltinFunctions(db);
drh1c928532002-01-31 15:54:21 +0000336 db->onError = OE_Default;
drh5cf8e8c2002-02-19 22:42:05 +0000337 db->priorNewRowid = 0;
drh247be432002-05-10 05:44:55 +0000338 db->magic = SQLITE_MAGIC_BUSY;
drh75897232000-05-29 14:26:00 +0000339
340 /* Open the backend database driver */
drha1b351a2001-09-14 16:42:12 +0000341 rc = sqliteBtreeOpen(zFilename, mode, MAX_PAGES, &db->pBe);
drh5e00f6c2001-09-13 13:46:56 +0000342 if( rc!=SQLITE_OK ){
343 switch( rc ){
344 default: {
drhaacc5432002-01-06 17:07:40 +0000345 sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0);
drh5e00f6c2001-09-13 13:46:56 +0000346 }
347 }
drh75897232000-05-29 14:26:00 +0000348 sqliteFree(db);
drh5edc3122001-09-13 21:53:09 +0000349 sqliteStrRealloc(pzErrMsg);
drhbe0072d2001-09-13 14:46:09 +0000350 return 0;
drh75897232000-05-29 14:26:00 +0000351 }
352
drh58b95762000-06-02 01:17:37 +0000353 /* Attempt to read the schema */
354 rc = sqliteInit(db, pzErrMsg);
drhc67980b2002-06-14 20:54:14 +0000355 db->magic = SQLITE_MAGIC_OPEN;
drhdaffd0e2001-04-11 14:28:42 +0000356 if( sqlite_malloc_failed ){
drh6d4abfb2001-10-22 02:58:08 +0000357 sqlite_close(db);
drhdaffd0e2001-04-11 14:28:42 +0000358 goto no_mem_on_open;
359 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
drh58b95762000-06-02 01:17:37 +0000360 sqlite_close(db);
drh5edc3122001-09-13 21:53:09 +0000361 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000362 return 0;
drhaacc5432002-01-06 17:07:40 +0000363 }else if( pzErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +0000364 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000365 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000366 }
drh75897232000-05-29 14:26:00 +0000367 return db;
drhdaffd0e2001-04-11 14:28:42 +0000368
369no_mem_on_open:
370 sqliteSetString(pzErrMsg, "out of memory", 0);
371 sqliteStrRealloc(pzErrMsg);
372 return 0;
drh75897232000-05-29 14:26:00 +0000373}
374
375/*
drhf57b3392001-10-08 13:22:32 +0000376** Erase all schema information from the schema hash table. Except
377** tables that are created using CREATE TEMPORARY TABLE are preserved
drhaacc5432002-01-06 17:07:40 +0000378** if the preserveTemps flag is true.
drh50e5dad2001-09-15 00:57:28 +0000379**
380** The database schema is normally read in once when the database
381** is first opened and stored in a hash table in the sqlite structure.
382** This routine erases the stored schema. This erasure occurs because
383** either the database is being closed or because some other process
384** changed the schema and this process needs to reread it.
drh75897232000-05-29 14:26:00 +0000385*/
drhf57b3392001-10-08 13:22:32 +0000386static void clearHashTable(sqlite *db, int preserveTemps){
drhbeae3192001-09-22 18:12:08 +0000387 HashElem *pElem;
388 Hash temp1;
danielk1977c3f9bad2002-05-15 08:30:12 +0000389 Hash temp2;
drhe4697f52002-05-23 02:09:03 +0000390
391 /* Make sure there are no uncommited DROPs */
392 assert( sqliteHashFirst(&db->tblDrop)==0 || sqlite_malloc_failed );
393 assert( sqliteHashFirst(&db->idxDrop)==0 || sqlite_malloc_failed );
394 assert( sqliteHashFirst(&db->trigDrop)==0 || sqlite_malloc_failed );
drhbeae3192001-09-22 18:12:08 +0000395 temp1 = db->tblHash;
danielk1977c3f9bad2002-05-15 08:30:12 +0000396 temp2 = db->trigHash;
397 sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0);
drhbeae3192001-09-22 18:12:08 +0000398 sqliteHashClear(&db->idxHash);
danielk1977c3f9bad2002-05-15 08:30:12 +0000399
danielk1977f29ce552002-05-19 23:43:12 +0000400 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
danielk1977c3f9bad2002-05-15 08:30:12 +0000401 Trigger * pTrigger = sqliteHashData(pElem);
402 Table *pTab = sqliteFindTable(db, pTrigger->table);
403 assert(pTab);
drh0b4efed2002-05-23 13:15:37 +0000404 if( pTab->isTemp && preserveTemps ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000405 sqliteHashInsert(&db->trigHash, pTrigger->name, strlen(pTrigger->name),
drh9adf9ac2002-05-15 11:44:13 +0000406 pTrigger);
danielk1977f29ce552002-05-19 23:43:12 +0000407 }else{
danielk1977c3f9bad2002-05-15 08:30:12 +0000408 sqliteDeleteTrigger(pTrigger);
409 }
410 }
411 sqliteHashClear(&temp2);
412
413 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
414
drhbeae3192001-09-22 18:12:08 +0000415 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
drhf57b3392001-10-08 13:22:32 +0000416 Table *pTab = sqliteHashData(pElem);
417 if( preserveTemps && pTab->isTemp ){
418 Index *pIdx;
drh6d4abfb2001-10-22 02:58:08 +0000419 int nName = strlen(pTab->zName);
420 Table *pOld = sqliteHashInsert(&db->tblHash, pTab->zName, nName+1, pTab);
421 if( pOld!=0 ){
422 assert( pOld==pTab ); /* Malloc failed on the HashInsert */
423 sqliteDeleteTable(db, pOld);
424 continue;
425 }
drhf57b3392001-10-08 13:22:32 +0000426 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
427 int n = strlen(pIdx->zName)+1;
drh6d4abfb2001-10-22 02:58:08 +0000428 Index *pOldIdx;
429 pOldIdx = sqliteHashInsert(&db->idxHash, pIdx->zName, n, pIdx);
430 if( pOld ){
431 assert( pOldIdx==pIdx );
432 sqliteUnlinkAndDeleteIndex(db, pOldIdx);
433 }
drhf57b3392001-10-08 13:22:32 +0000434 }
435 }else{
436 sqliteDeleteTable(db, pTab);
437 }
drh75897232000-05-29 14:26:00 +0000438 }
drhbeae3192001-09-22 18:12:08 +0000439 sqliteHashClear(&temp1);
drh50e5dad2001-09-15 00:57:28 +0000440 db->flags &= ~SQLITE_Initialized;
441}
442
443/*
drhaf9ff332002-01-16 21:00:27 +0000444** Return the ROWID of the most recent insert
445*/
446int sqlite_last_insert_rowid(sqlite *db){
447 return db->lastRowid;
448}
449
450/*
drhc8d30ac2002-04-12 10:08:59 +0000451** Return the number of changes in the most recent call to sqlite_exec().
452*/
453int sqlite_changes(sqlite *db){
454 return db->nChange;
455}
456
457/*
drh50e5dad2001-09-15 00:57:28 +0000458** Close an existing SQLite database
459*/
460void sqlite_close(sqlite *db){
drh8e0a2f92002-02-23 23:45:45 +0000461 HashElem *i;
drhc22bd472002-05-10 13:14:07 +0000462 if( sqliteSafetyCheck(db) || sqliteSafetyOn(db) ){ return; }
drh247be432002-05-10 05:44:55 +0000463 db->magic = SQLITE_MAGIC_CLOSED;
drh50e5dad2001-09-15 00:57:28 +0000464 sqliteBtreeClose(db->pBe);
drhf57b3392001-10-08 13:22:32 +0000465 clearHashTable(db, 0);
466 if( db->pBeTemp ){
467 sqliteBtreeClose(db->pBeTemp);
468 }
drh0bce8352002-02-28 00:41:10 +0000469 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
470 FuncDef *pFunc, *pNext;
471 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
drh8e0a2f92002-02-23 23:45:45 +0000472 pNext = pFunc->pNext;
473 sqliteFree(pFunc);
474 }
475 }
drh0bce8352002-02-28 00:41:10 +0000476 sqliteHashClear(&db->aFunc);
drh75897232000-05-29 14:26:00 +0000477 sqliteFree(db);
478}
479
480/*
481** Return TRUE if the given SQL string ends in a semicolon.
drhce9079c2002-05-15 14:17:44 +0000482**
483** Special handling is require for CREATE TRIGGER statements.
484** Whenever the CREATE TRIGGER keywords are seen, the statement
485** must end with ";END;".
drh75897232000-05-29 14:26:00 +0000486*/
487int sqlite_complete(const char *zSql){
drhce9079c2002-05-15 14:17:44 +0000488 int isComplete = 1;
489 int requireEnd = 0;
490 int seenText = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000491 int seenCreate = 0;
drh8c82b352000-12-10 18:23:50 +0000492 while( *zSql ){
493 switch( *zSql ){
494 case ';': {
495 isComplete = 1;
drhce9079c2002-05-15 14:17:44 +0000496 seenText = 1;
497 seenCreate = 0;
drh75897232000-05-29 14:26:00 +0000498 break;
drh8c82b352000-12-10 18:23:50 +0000499 }
500 case ' ':
501 case '\t':
502 case '\n':
503 case '\f': {
drh75897232000-05-29 14:26:00 +0000504 break;
drh8c82b352000-12-10 18:23:50 +0000505 }
drh969fa7c2002-02-18 18:30:32 +0000506 case '[': {
507 isComplete = 0;
drhce9079c2002-05-15 14:17:44 +0000508 seenText = 1;
509 seenCreate = 0;
drh969fa7c2002-02-18 18:30:32 +0000510 zSql++;
511 while( *zSql && *zSql!=']' ){ zSql++; }
512 if( *zSql==0 ) return 0;
513 break;
514 }
drhce9079c2002-05-15 14:17:44 +0000515 case '"':
drh8c82b352000-12-10 18:23:50 +0000516 case '\'': {
drhce9079c2002-05-15 14:17:44 +0000517 int c = *zSql;
drh8c82b352000-12-10 18:23:50 +0000518 isComplete = 0;
drhce9079c2002-05-15 14:17:44 +0000519 seenText = 1;
520 seenCreate = 0;
drh8c82b352000-12-10 18:23:50 +0000521 zSql++;
drhce9079c2002-05-15 14:17:44 +0000522 while( *zSql && *zSql!=c ){ zSql++; }
drh8c82b352000-12-10 18:23:50 +0000523 if( *zSql==0 ) return 0;
524 break;
525 }
526 case '-': {
527 if( zSql[1]!='-' ){
528 isComplete = 0;
drhce9079c2002-05-15 14:17:44 +0000529 seenCreate = 0;
drh8c82b352000-12-10 18:23:50 +0000530 break;
531 }
532 while( *zSql && *zSql!='\n' ){ zSql++; }
drhce9079c2002-05-15 14:17:44 +0000533 if( *zSql==0 ) return seenText && isComplete && requireEnd==0;
534 break;
535 }
536 case 'c':
537 case 'C': {
538 seenText = 1;
539 if( !isComplete ) break;
540 isComplete = 0;
541 if( sqliteStrNICmp(zSql, "create", 6)!=0 ) break;
542 if( !isspace(zSql[6]) ) break;
543 zSql += 5;
544 seenCreate = 1;
545 while( isspace(zSql[1]) ) zSql++;
546 if( sqliteStrNICmp(&zSql[1],"trigger", 7)!=0 ) break;
547 zSql += 7;
548 requireEnd++;
549 break;
550 }
551 case 't':
552 case 'T': {
553 seenText = 1;
554 if( !seenCreate ) break;
555 seenCreate = 0;
556 isComplete = 0;
557 if( sqliteStrNICmp(zSql, "trigger", 7)!=0 ) break;
558 if( !isspace(zSql[7]) ) break;
559 zSql += 6;
560 requireEnd++;
561 break;
562 }
563 case 'e':
564 case 'E': {
565 seenCreate = 0;
566 seenText = 1;
567 if( !isComplete ) break;
568 isComplete = 0;
569 if( requireEnd==0 ) break;
570 if( sqliteStrNICmp(zSql, "end", 3)!=0 ) break;
571 zSql += 2;
572 while( isspace(zSql[1]) ) zSql++;
573 if( zSql[1]==';' ){
574 zSql++;
575 isComplete = 1;
576 requireEnd--;
577 }
drh8c82b352000-12-10 18:23:50 +0000578 break;
drh9adf9ac2002-05-15 11:44:13 +0000579 }
drh8c82b352000-12-10 18:23:50 +0000580 default: {
drhce9079c2002-05-15 14:17:44 +0000581 seenCreate = 0;
582 seenText = 1;
drh8c82b352000-12-10 18:23:50 +0000583 isComplete = 0;
584 break;
585 }
drh75897232000-05-29 14:26:00 +0000586 }
drh8c82b352000-12-10 18:23:50 +0000587 zSql++;
drh75897232000-05-29 14:26:00 +0000588 }
drhce9079c2002-05-15 14:17:44 +0000589 return seenText && isComplete && requireEnd==0;
drh75897232000-05-29 14:26:00 +0000590}
591
592/*
drhbed86902000-06-02 13:27:59 +0000593** Execute SQL code. Return one of the SQLITE_ success/failure
594** codes. Also write an error message into memory obtained from
595** malloc() and make *pzErrMsg point to that message.
596**
597** If the SQL is a query, then for each row in the query result
598** the xCallback() function is called. pArg becomes the first
599** argument to xCallback(). If xCallback=NULL then no callback
600** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000601*/
602int sqlite_exec(
603 sqlite *db, /* The database on which the SQL executes */
drh9f71c2e2001-11-03 23:57:09 +0000604 const char *zSql, /* The SQL to be executed */
drh75897232000-05-29 14:26:00 +0000605 sqlite_callback xCallback, /* Invoke this callback routine */
606 void *pArg, /* First argument to xCallback() */
607 char **pzErrMsg /* Write error messages here */
608){
609 Parse sParse;
drh75897232000-05-29 14:26:00 +0000610
611 if( pzErrMsg ) *pzErrMsg = 0;
drhc22bd472002-05-10 13:14:07 +0000612 if( sqliteSafetyOn(db) ) goto exec_misuse;
drh58b95762000-06-02 01:17:37 +0000613 if( (db->flags & SQLITE_Initialized)==0 ){
614 int rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000615 if( rc!=SQLITE_OK ){
616 sqliteStrRealloc(pzErrMsg);
drh247be432002-05-10 05:44:55 +0000617 sqliteSafetyOff(db);
drhdaffd0e2001-04-11 14:28:42 +0000618 return rc;
619 }
drh58b95762000-06-02 01:17:37 +0000620 }
drhc8d30ac2002-04-12 10:08:59 +0000621 if( db->recursionDepth==0 ){ db->nChange = 0; }
622 db->recursionDepth++;
drh75897232000-05-29 14:26:00 +0000623 memset(&sParse, 0, sizeof(sParse));
624 sParse.db = db;
drh5e00f6c2001-09-13 13:46:56 +0000625 sParse.pBe = db->pBe;
drh75897232000-05-29 14:26:00 +0000626 sParse.xCallback = xCallback;
627 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000628 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000629 if( sqlite_malloc_failed ){
630 sqliteSetString(pzErrMsg, "out of memory", 0);
631 sParse.rc = SQLITE_NOMEM;
drh6d4abfb2001-10-22 02:58:08 +0000632 sqliteBtreeRollback(db->pBe);
633 if( db->pBeTemp ) sqliteBtreeRollback(db->pBeTemp);
634 db->flags &= ~SQLITE_InTrans;
635 clearHashTable(db, 0);
drhdaffd0e2001-04-11 14:28:42 +0000636 }
637 sqliteStrRealloc(pzErrMsg);
drh50e5dad2001-09-15 00:57:28 +0000638 if( sParse.rc==SQLITE_SCHEMA ){
drhf57b3392001-10-08 13:22:32 +0000639 clearHashTable(db, 1);
drh50e5dad2001-09-15 00:57:28 +0000640 }
drhc8d30ac2002-04-12 10:08:59 +0000641 db->recursionDepth--;
drhc22bd472002-05-10 13:14:07 +0000642 if( sqliteSafetyOff(db) ) goto exec_misuse;
drh4c504392000-10-16 22:06:40 +0000643 return sParse.rc;
drhc22bd472002-05-10 13:14:07 +0000644
645exec_misuse:
646 if( pzErrMsg ){
647 *pzErrMsg = 0;
648 sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), 0);
649 sqliteStrRealloc(pzErrMsg);
650 }
651 return SQLITE_MISUSE;
drh75897232000-05-29 14:26:00 +0000652}
drh2dfbbca2000-07-28 14:32:48 +0000653
654/*
drhc22bd472002-05-10 13:14:07 +0000655** Return a static string that describes the kind of error specified in the
656** argument.
drh247be432002-05-10 05:44:55 +0000657*/
drhc22bd472002-05-10 13:14:07 +0000658const char *sqlite_error_string(int rc){
659 const char *z;
660 switch( rc ){
661 case SQLITE_OK: z = "not an error"; break;
662 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
663 case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break;
664 case SQLITE_PERM: z = "access permission denied"; break;
665 case SQLITE_ABORT: z = "callback requested query abort"; break;
666 case SQLITE_BUSY: z = "database is locked"; break;
667 case SQLITE_LOCKED: z = "database table is locked"; break;
668 case SQLITE_NOMEM: z = "out of memory"; break;
669 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
670 case SQLITE_INTERRUPT: z = "interrupted"; break;
671 case SQLITE_IOERR: z = "disk I/O error"; break;
672 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
673 case SQLITE_NOTFOUND: z = "table or record not found"; break;
674 case SQLITE_FULL: z = "database is full"; break;
675 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
676 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
677 case SQLITE_EMPTY: z = "table contains no data"; break;
678 case SQLITE_SCHEMA: z = "database schema has changed"; break;
679 case SQLITE_TOOBIG: z = "too much data for one table row"; break;
680 case SQLITE_CONSTRAINT: z = "constraint failed"; break;
681 case SQLITE_MISMATCH: z = "datatype mismatch"; break;
682 case SQLITE_MISUSE: z = "library routine called out of sequence";break;
683 default: z = "unknown error"; break;
drh247be432002-05-10 05:44:55 +0000684 }
drhc22bd472002-05-10 13:14:07 +0000685 return z;
drh247be432002-05-10 05:44:55 +0000686}
687
688/*
drh2dfbbca2000-07-28 14:32:48 +0000689** This routine implements a busy callback that sleeps and tries
690** again until a timeout value is reached. The timeout value is
691** an integer number of milliseconds passed in as the first
692** argument.
693*/
drhdaffd0e2001-04-11 14:28:42 +0000694static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000695 void *Timeout, /* Maximum amount of time to wait */
696 const char *NotUsed, /* The name of the table that is busy */
697 int count /* Number of times table has been busy */
698){
drh8cfbf082001-09-19 13:22:39 +0000699#if SQLITE_MIN_SLEEP_MS==1
700 int delay = 10;
drh2dfbbca2000-07-28 14:32:48 +0000701 int prior_delay = 0;
702 int timeout = (int)Timeout;
703 int i;
704
705 for(i=1; i<count; i++){
706 prior_delay += delay;
707 delay = delay*2;
drh8cfbf082001-09-19 13:22:39 +0000708 if( delay>=1000 ){
709 delay = 1000;
710 prior_delay += 1000*(count - i - 1);
drh2dfbbca2000-07-28 14:32:48 +0000711 break;
712 }
713 }
drh3109e022001-10-09 13:46:01 +0000714 if( prior_delay + delay > timeout ){
715 delay = timeout - prior_delay;
drh2dfbbca2000-07-28 14:32:48 +0000716 if( delay<=0 ) return 0;
717 }
drh8cfbf082001-09-19 13:22:39 +0000718 sqliteOsSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000719 return 1;
720#else
721 int timeout = (int)Timeout;
722 if( (count+1)*1000 > timeout ){
723 return 0;
724 }
drh8cfbf082001-09-19 13:22:39 +0000725 sqliteOsSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000726 return 1;
727#endif
728}
729
730/*
731** This routine sets the busy callback for an Sqlite database to the
732** given callback function with the given argument.
733*/
734void sqlite_busy_handler(
735 sqlite *db,
736 int (*xBusy)(void*,const char*,int),
737 void *pArg
738){
739 db->xBusyCallback = xBusy;
740 db->pBusyArg = pArg;
741}
742
743/*
744** This routine installs a default busy handler that waits for the
745** specified number of milliseconds before returning 0.
746*/
747void sqlite_busy_timeout(sqlite *db, int ms){
748 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000749 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000750 }else{
751 sqlite_busy_handler(db, 0, 0);
752 }
753}
drh4c504392000-10-16 22:06:40 +0000754
755/*
756** Cause any pending operation to stop at its earliest opportunity.
757*/
758void sqlite_interrupt(sqlite *db){
759 db->flags |= SQLITE_Interrupt;
760}
drhfa86c412002-02-02 15:01:15 +0000761
762/*
763** Windows systems should call this routine to free memory that
764** is returned in the in the errmsg parameter of sqlite_open() when
765** SQLite is a DLL. For some reason, it does not work to call free()
766** directly.
767**
768** Note that we need to call free() not sqliteFree() here, since every
769** string that is exported from SQLite should have already passed through
770** sqliteStrRealloc().
771*/
772void sqlite_freemem(void *p){ free(p); }
773
774/*
775** Windows systems need functions to call to return the sqlite_version
776** and sqlite_encoding strings.
777*/
778const char *sqlite_libversion(void){ return sqlite_version; }
779const char *sqlite_libencoding(void){ return sqlite_encoding; }
drh8e0a2f92002-02-23 23:45:45 +0000780
781/*
782** Create new user-defined functions. The sqlite_create_function()
783** routine creates a regular function and sqlite_create_aggregate()
784** creates an aggregate function.
785**
786** Passing a NULL xFunc argument or NULL xStep and xFinalize arguments
787** disables the function. Calling sqlite_create_function() with the
788** same name and number of arguments as a prior call to
789** sqlite_create_aggregate() disables the prior call to
790** sqlite_create_aggregate(), and vice versa.
791**
792** If nArg is -1 it means that this function will accept any number
793** of arguments, including 0.
794*/
795int sqlite_create_function(
796 sqlite *db, /* Add the function to this database connection */
797 const char *zName, /* Name of the function to add */
798 int nArg, /* Number of arguments */
drh1350b032002-02-27 19:00:20 +0000799 void (*xFunc)(sqlite_func*,int,const char**), /* The implementation */
800 void *pUserData /* User data */
drh8e0a2f92002-02-23 23:45:45 +0000801){
drh0bce8352002-02-28 00:41:10 +0000802 FuncDef *p;
drhc22bd472002-05-10 13:14:07 +0000803 if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1;
drh0bce8352002-02-28 00:41:10 +0000804 p = sqliteFindFunction(db, zName, strlen(zName), nArg, 1);
drh4e0f9952002-02-27 01:53:13 +0000805 if( p==0 ) return 1;
drh8e0a2f92002-02-23 23:45:45 +0000806 p->xFunc = xFunc;
807 p->xStep = 0;
808 p->xFinalize = 0;
drh1350b032002-02-27 19:00:20 +0000809 p->pUserData = pUserData;
drh8e0a2f92002-02-23 23:45:45 +0000810 return 0;
811}
812int sqlite_create_aggregate(
813 sqlite *db, /* Add the function to this database connection */
814 const char *zName, /* Name of the function to add */
815 int nArg, /* Number of arguments */
drh1350b032002-02-27 19:00:20 +0000816 void (*xStep)(sqlite_func*,int,const char**), /* The step function */
817 void (*xFinalize)(sqlite_func*), /* The finalizer */
818 void *pUserData /* User data */
drh8e0a2f92002-02-23 23:45:45 +0000819){
drh0bce8352002-02-28 00:41:10 +0000820 FuncDef *p;
drhc22bd472002-05-10 13:14:07 +0000821 if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1;
drh0bce8352002-02-28 00:41:10 +0000822 p = sqliteFindFunction(db, zName, strlen(zName), nArg, 1);
drh4e0f9952002-02-27 01:53:13 +0000823 if( p==0 ) return 1;
drh8e0a2f92002-02-23 23:45:45 +0000824 p->xFunc = 0;
825 p->xStep = xStep;
826 p->xFinalize = xFinalize;
drh1350b032002-02-27 19:00:20 +0000827 p->pUserData = pUserData;
drh8e0a2f92002-02-23 23:45:45 +0000828 return 0;
829}