blob: a5e1be6455fdc201f9d5403e814b340e8a8c2acb [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**
drhc9b84a12002-06-20 11:36:48 +000017** $Id: main.c,v 1.81 2002/06/20 11:36:49 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 */
drhc9b84a12002-06-20 11:36:48 +000054 /*
55 ** file_format==1 Version 2.1.0.
56 ** file_format==2 Version 2.2.0. Integer primary key.
57 ** file_format==3 Version 2.6.0. Separate text and numeric datatypes.
58 */
drh4a324312001-12-21 14:30:42 +000059 db->file_format = atoi(argv[3]);
60 break;
61 }
62 case 's': { /* Schema cookie */
63 db->schema_cookie = atoi(argv[3]);
64 db->next_cookie = db->schema_cookie;
drhd78eeee2001-09-13 16:18:53 +000065 break;
drh28037572000-08-02 13:47:41 +000066 }
drh17f71932002-02-21 12:01:27 +000067 case 'v':
drhd78eeee2001-09-13 16:18:53 +000068 case 'i':
drh17f71932002-02-21 12:01:27 +000069 case 't': { /* CREATE TABLE, CREATE INDEX, or CREATE VIEW statements */
drhadbca9c2001-09-27 15:11:53 +000070 if( argv[3] && argv[3][0] ){
drh17f71932002-02-21 12:01:27 +000071 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
drh382c0242001-10-06 16:33:02 +000072 ** But because sParse.initFlag is set to 1, no VDBE code is generated
73 ** or executed. All the parser does is build the internal data
drh17f71932002-02-21 12:01:27 +000074 ** structures that describe the table, index, or view.
drh382c0242001-10-06 16:33:02 +000075 */
drhadbca9c2001-09-27 15:11:53 +000076 memset(&sParse, 0, sizeof(sParse));
77 sParse.db = db;
78 sParse.initFlag = 1;
79 sParse.newTnum = atoi(argv[2]);
drhf5bf0a72001-11-23 00:24:12 +000080 sqliteRunParser(&sParse, argv[3], 0);
drhadbca9c2001-09-27 15:11:53 +000081 }else{
drh382c0242001-10-06 16:33:02 +000082 /* If the SQL column is blank it means this is an index that
83 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
drhaacc5432002-01-06 17:07:40 +000084 ** constraint for a CREATE TABLE. The index should have already
drh382c0242001-10-06 16:33:02 +000085 ** been created when we processed the CREATE TABLE. All we have
drhaacc5432002-01-06 17:07:40 +000086 ** to do here is record the root page number for that index.
drh382c0242001-10-06 16:33:02 +000087 */
drhadbca9c2001-09-27 15:11:53 +000088 Index *pIndex = sqliteFindIndex(db, argv[1]);
89 if( pIndex==0 || pIndex->tnum!=0 ){
drhda9e0342002-01-10 14:31:48 +000090 /* This can occur if there exists an index on a TEMP table which
91 ** has the same name as another index on a permanent index. Since
92 ** the permanent table is hidden by the TEMP table, we can also
93 ** safely ignore the index on the permanent table.
94 */
95 /* Do Nothing */;
drhadbca9c2001-09-27 15:11:53 +000096 }else{
97 pIndex->tnum = atoi(argv[2]);
98 }
99 }
drhd78eeee2001-09-13 16:18:53 +0000100 break;
101 }
102 default: {
103 /* This can not happen! */
104 nErr = 1;
105 assert( nErr==0 );
106 }
drh28037572000-08-02 13:47:41 +0000107 }
drh75897232000-05-29 14:26:00 +0000108 return nErr;
109}
110
111/*
drh58b95762000-06-02 01:17:37 +0000112** Attempt to read the database schema and initialize internal
113** data structures. Return one of the SQLITE_ error codes to
114** indicate success or failure.
drhbed86902000-06-02 13:27:59 +0000115**
116** After the database is initialized, the SQLITE_Initialized
117** bit is set in the flags field of the sqlite structure. An
118** attempt is made to initialize the database as soon as it
119** is opened. If that fails (perhaps because another process
120** has the sqlite_master table locked) than another attempt
121** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +0000122*/
drh58b95762000-06-02 01:17:37 +0000123static int sqliteInit(sqlite *db, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +0000124 Vdbe *vdbe;
drh58b95762000-06-02 01:17:37 +0000125 int rc;
126
127 /*
128 ** The master database table has a structure like this
129 */
drh75897232000-05-29 14:26:00 +0000130 static char master_schema[] =
131 "CREATE TABLE " MASTER_NAME " (\n"
132 " type text,\n"
133 " name text,\n"
134 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000135 " rootpage integer,\n"
drh75897232000-05-29 14:26:00 +0000136 " sql text\n"
137 ")"
138 ;
139
drhaacc5432002-01-06 17:07:40 +0000140 /* The following VDBE program is used to initialize the internal
drh75897232000-05-29 14:26:00 +0000141 ** structure holding the tables and indexes of the database.
142 ** The database contains a special table named "sqlite_master"
143 ** defined as follows:
144 **
145 ** CREATE TABLE sqlite_master (
drh28037572000-08-02 13:47:41 +0000146 ** type text, -- Either "table" or "index" or "meta"
drh75897232000-05-29 14:26:00 +0000147 ** name text, -- Name of table or index
148 ** tbl_name text, -- Associated table
drhadbca9c2001-09-27 15:11:53 +0000149 ** rootpage integer, -- The integer page number of root page
drh75897232000-05-29 14:26:00 +0000150 ** sql text -- The CREATE statement for this object
151 ** );
152 **
153 ** The sqlite_master table contains a single entry for each table
drh967e8b72000-06-21 13:59:10 +0000154 ** and each index. The "type" column tells whether the entry is
155 ** a table or index. The "name" column is the name of the object.
drh75897232000-05-29 14:26:00 +0000156 ** The "tbl_name" is the name of the associated table. For tables,
drh967e8b72000-06-21 13:59:10 +0000157 ** the tbl_name column is always the same as name. For indices, the
158 ** tbl_name column contains the name of the table that the index
drh382c0242001-10-06 16:33:02 +0000159 ** indexes. The "rootpage" column holds the number of the root page
160 ** for the b-tree for the table or index. Finally, the "sql" column
161 ** contains the complete text of the CREATE TABLE or CREATE INDEX
162 ** statement that originally created the table or index. If an index
163 ** was created to fulfill a PRIMARY KEY or UNIQUE constraint on a table,
164 ** then the "sql" column is NULL.
drh75897232000-05-29 14:26:00 +0000165 **
drh17f71932002-02-21 12:01:27 +0000166 ** In format 1, entries in the sqlite_master table are in a random
167 ** order. Two passes must be made through the table to initialize
168 ** internal data structures. The first pass reads table definitions
169 ** and the second pass read index definitions. Having two passes
170 ** insures that indices appear after their tables.
171 **
172 ** In format 2, entries appear in chronological order. Only a single
173 ** pass needs to be made through the table since everything will be
174 ** in the write order. VIEWs may only occur in format 2.
drh28037572000-08-02 13:47:41 +0000175 **
drh75897232000-05-29 14:26:00 +0000176 ** The following program invokes its callback on the SQL for each
177 ** table then goes back and invokes the callback on the
178 ** SQL for each index. The callback will invoke the
179 ** parser to build the internal representation of the
180 ** database scheme.
181 */
182 static VdbeOp initProg[] = {
drh17f71932002-02-21 12:01:27 +0000183 /* Send the file format to the callback routine
184 */
drh4a324312001-12-21 14:30:42 +0000185 { OP_Open, 0, 2, 0},
186 { OP_String, 0, 0, "file-format"},
187 { OP_String, 0, 0, 0},
188 { OP_String, 0, 0, 0},
189 { OP_ReadCookie, 0, 1, 0},
190 { OP_Callback, 4, 0, 0},
drh17f71932002-02-21 12:01:27 +0000191
drh603240c2002-03-05 01:11:12 +0000192 /* Send the recommended pager cache size to the callback routine
193 */
194 { OP_String, 0, 0, "cache-size"},
195 { OP_String, 0, 0, 0},
196 { OP_String, 0, 0, 0},
197 { OP_ReadCookie, 0, 2, 0},
198 { OP_Callback, 4, 0, 0},
199
drh17f71932002-02-21 12:01:27 +0000200 /* Send the initial schema cookie to the callback
201 */
drh4a324312001-12-21 14:30:42 +0000202 { OP_String, 0, 0, "schema_cookie"},
203 { OP_String, 0, 0, 0},
204 { OP_String, 0, 0, 0},
205 { OP_ReadCookie, 0, 0, 0},
206 { OP_Callback, 4, 0, 0},
drh17f71932002-02-21 12:01:27 +0000207
208 /* Check the file format. If the format number is 2 or more,
209 ** then do a single pass through the SQLITE_MASTER table. For
210 ** a format number of less than 2, jump forward to a different
211 ** algorithm that makes two passes through the SQLITE_MASTER table,
212 ** once for tables and a second time for indices.
213 */
214 { OP_ReadCookie, 0, 1, 0},
215 { OP_Integer, 2, 0, 0},
drh603240c2002-03-05 01:11:12 +0000216 { OP_Lt, 0, 28, 0},
drh17f71932002-02-21 12:01:27 +0000217
218 /* This is the code for doing a single scan through the SQLITE_MASTER
219 ** table. This code runs for format 2 and greater.
220 */
drh603240c2002-03-05 01:11:12 +0000221 { OP_Rewind, 0, 26, 0},
222 { OP_Column, 0, 0, 0}, /* 20 */
drh17f71932002-02-21 12:01:27 +0000223 { OP_Column, 0, 1, 0},
224 { OP_Column, 0, 3, 0},
225 { OP_Column, 0, 4, 0},
226 { OP_Callback, 4, 0, 0},
drh603240c2002-03-05 01:11:12 +0000227 { OP_Next, 0, 20, 0},
228 { OP_Close, 0, 0, 0}, /* 26 */
drh17f71932002-02-21 12:01:27 +0000229 { OP_Halt, 0, 0, 0},
230
231 /* This is the code for doing two passes through SQLITE_MASTER. This
232 ** code runs for file format 1.
233 */
drh603240c2002-03-05 01:11:12 +0000234 { OP_Rewind, 0, 48, 0}, /* 28 */
235 { OP_Column, 0, 0, 0}, /* 29 */
drh4a324312001-12-21 14:30:42 +0000236 { OP_String, 0, 0, "table"},
drh603240c2002-03-05 01:11:12 +0000237 { OP_Ne, 0, 37, 0},
drh4a324312001-12-21 14:30:42 +0000238 { OP_Column, 0, 0, 0},
239 { OP_Column, 0, 1, 0},
240 { OP_Column, 0, 3, 0},
241 { OP_Column, 0, 4, 0},
242 { OP_Callback, 4, 0, 0},
drh603240c2002-03-05 01:11:12 +0000243 { OP_Next, 0, 29, 0}, /* 37 */
244 { OP_Rewind, 0, 48, 0}, /* 38 */
245 { OP_Column, 0, 0, 0}, /* 39 */
drh4a324312001-12-21 14:30:42 +0000246 { OP_String, 0, 0, "index"},
drh603240c2002-03-05 01:11:12 +0000247 { OP_Ne, 0, 47, 0},
drh4a324312001-12-21 14:30:42 +0000248 { OP_Column, 0, 0, 0},
249 { OP_Column, 0, 1, 0},
250 { OP_Column, 0, 3, 0},
251 { OP_Column, 0, 4, 0},
252 { OP_Callback, 4, 0, 0},
drh603240c2002-03-05 01:11:12 +0000253 { OP_Next, 0, 39, 0}, /* 47 */
254 { OP_Close, 0, 0, 0}, /* 48 */
drh4a324312001-12-21 14:30:42 +0000255 { OP_Halt, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000256 };
257
drh58b95762000-06-02 01:17:37 +0000258 /* Create a virtual machine to run the initialization program. Run
drh382c0242001-10-06 16:33:02 +0000259 ** the program. Then delete the virtual machine.
drh58b95762000-06-02 01:17:37 +0000260 */
drh4c504392000-10-16 22:06:40 +0000261 vdbe = sqliteVdbeCreate(db);
drhd8bc7082000-06-07 23:51:50 +0000262 if( vdbe==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000263 sqliteSetString(pzErrMsg, "out of memory", 0);
drhdaffd0e2001-04-11 14:28:42 +0000264 return SQLITE_NOMEM;
drhd8bc7082000-06-07 23:51:50 +0000265 }
drh58b95762000-06-02 01:17:37 +0000266 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
drhe0140fc2002-06-16 18:21:44 +0000267 rc = sqliteVdbeExec(vdbe, sqliteInitCallback, db, pzErrMsg,
drh2dfbbca2000-07-28 14:32:48 +0000268 db->pBusyArg, db->xBusyCallback);
drh58b95762000-06-02 01:17:37 +0000269 sqliteVdbeDelete(vdbe);
drh4a324312001-12-21 14:30:42 +0000270 if( rc==SQLITE_OK && db->nTable==0 ){
drh17f71932002-02-21 12:01:27 +0000271 db->file_format = 2;
drh4a324312001-12-21 14:30:42 +0000272 }
drh17f71932002-02-21 12:01:27 +0000273 if( rc==SQLITE_OK && db->file_format>2 ){
drhd78eeee2001-09-13 16:18:53 +0000274 sqliteSetString(pzErrMsg, "unsupported file format", 0);
drh28037572000-08-02 13:47:41 +0000275 rc = SQLITE_ERROR;
276 }
drhaacc5432002-01-06 17:07:40 +0000277
278 /* The schema for the SQLITE_MASTER table is not stored in the
279 ** database itself. We have to invoke the callback one extra
280 ** time to get it to process the SQLITE_MASTER table defintion.
281 */
drh58b95762000-06-02 01:17:37 +0000282 if( rc==SQLITE_OK ){
283 Table *pTab;
drhe3c41372001-09-17 20:25:58 +0000284 char *azArg[6];
drhd78eeee2001-09-13 16:18:53 +0000285 azArg[0] = "table";
286 azArg[1] = MASTER_NAME;
287 azArg[2] = "2";
drhadbca9c2001-09-27 15:11:53 +0000288 azArg[3] = master_schema;
289 azArg[4] = 0;
drhe0140fc2002-06-16 18:21:44 +0000290 sqliteInitCallback(db, 4, azArg, 0);
drh58b95762000-06-02 01:17:37 +0000291 pTab = sqliteFindTable(db, MASTER_NAME);
292 if( pTab ){
293 pTab->readOnly = 1;
294 }
295 db->flags |= SQLITE_Initialized;
drh5e00f6c2001-09-13 13:46:56 +0000296 sqliteCommitInternalChanges(db);
drh58b95762000-06-02 01:17:37 +0000297 }
298 return rc;
299}
300
301/*
drhb217a572000-08-22 13:40:18 +0000302** The version of the library
303*/
drh3d0b5592000-08-22 13:40:51 +0000304const char sqlite_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000305
306/*
drh297ecf12001-04-05 15:57:13 +0000307** Does the library expect data to be encoded as UTF-8 or iso8859? The
308** following global constant always lets us know.
309*/
310#ifdef SQLITE_UTF8
drhfbc3eab2001-04-06 16:13:42 +0000311const char sqlite_encoding[] = "UTF-8";
drh297ecf12001-04-05 15:57:13 +0000312#else
drhfbc3eab2001-04-06 16:13:42 +0000313const char sqlite_encoding[] = "iso8859";
drh297ecf12001-04-05 15:57:13 +0000314#endif
315
316/*
drh58b95762000-06-02 01:17:37 +0000317** Open a new SQLite database. Construct an "sqlite" structure to define
318** the state of this database and return a pointer to that structure.
319**
320** An attempt is made to initialize the in-memory data structures that
321** hold the database schema. But if this fails (because the schema file
322** is locked) then that step is deferred until the first call to
323** sqlite_exec().
324*/
325sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
326 sqlite *db;
327 int rc;
328
329 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000330 db = sqliteMalloc( sizeof(sqlite) );
331 if( pzErrMsg ) *pzErrMsg = 0;
drhdaffd0e2001-04-11 14:28:42 +0000332 if( db==0 ) goto no_mem_on_open;
drhbeae3192001-09-22 18:12:08 +0000333 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
334 sqliteHashInit(&db->idxHash, SQLITE_HASH_STRING, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000335 sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0);
336 sqliteHashInit(&db->trigDrop, SQLITE_HASH_STRING, 0);
drh74e24cd2002-01-09 03:19:59 +0000337 sqliteHashInit(&db->tblDrop, SQLITE_HASH_POINTER, 0);
338 sqliteHashInit(&db->idxDrop, SQLITE_HASH_POINTER, 0);
drh0bce8352002-02-28 00:41:10 +0000339 sqliteHashInit(&db->aFunc, SQLITE_HASH_STRING, 1);
drh28f4b682002-06-09 10:14:18 +0000340 sqliteRegisterBuiltinFunctions(db);
drh1c928532002-01-31 15:54:21 +0000341 db->onError = OE_Default;
drh5cf8e8c2002-02-19 22:42:05 +0000342 db->priorNewRowid = 0;
drh247be432002-05-10 05:44:55 +0000343 db->magic = SQLITE_MAGIC_BUSY;
drh75897232000-05-29 14:26:00 +0000344
345 /* Open the backend database driver */
drha1b351a2001-09-14 16:42:12 +0000346 rc = sqliteBtreeOpen(zFilename, mode, MAX_PAGES, &db->pBe);
drh5e00f6c2001-09-13 13:46:56 +0000347 if( rc!=SQLITE_OK ){
348 switch( rc ){
349 default: {
drhaacc5432002-01-06 17:07:40 +0000350 sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0);
drh5e00f6c2001-09-13 13:46:56 +0000351 }
352 }
drh75897232000-05-29 14:26:00 +0000353 sqliteFree(db);
drh5edc3122001-09-13 21:53:09 +0000354 sqliteStrRealloc(pzErrMsg);
drhbe0072d2001-09-13 14:46:09 +0000355 return 0;
drh75897232000-05-29 14:26:00 +0000356 }
357
drh58b95762000-06-02 01:17:37 +0000358 /* Attempt to read the schema */
359 rc = sqliteInit(db, pzErrMsg);
drhc67980b2002-06-14 20:54:14 +0000360 db->magic = SQLITE_MAGIC_OPEN;
drhdaffd0e2001-04-11 14:28:42 +0000361 if( sqlite_malloc_failed ){
drh6d4abfb2001-10-22 02:58:08 +0000362 sqlite_close(db);
drhdaffd0e2001-04-11 14:28:42 +0000363 goto no_mem_on_open;
364 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
drh58b95762000-06-02 01:17:37 +0000365 sqlite_close(db);
drh5edc3122001-09-13 21:53:09 +0000366 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000367 return 0;
drhaacc5432002-01-06 17:07:40 +0000368 }else if( pzErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +0000369 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000370 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000371 }
drh75897232000-05-29 14:26:00 +0000372 return db;
drhdaffd0e2001-04-11 14:28:42 +0000373
374no_mem_on_open:
375 sqliteSetString(pzErrMsg, "out of memory", 0);
376 sqliteStrRealloc(pzErrMsg);
377 return 0;
drh75897232000-05-29 14:26:00 +0000378}
379
380/*
drhf57b3392001-10-08 13:22:32 +0000381** Erase all schema information from the schema hash table. Except
382** tables that are created using CREATE TEMPORARY TABLE are preserved
drhaacc5432002-01-06 17:07:40 +0000383** if the preserveTemps flag is true.
drh50e5dad2001-09-15 00:57:28 +0000384**
385** The database schema is normally read in once when the database
386** is first opened and stored in a hash table in the sqlite structure.
387** This routine erases the stored schema. This erasure occurs because
388** either the database is being closed or because some other process
389** changed the schema and this process needs to reread it.
drh75897232000-05-29 14:26:00 +0000390*/
drhf57b3392001-10-08 13:22:32 +0000391static void clearHashTable(sqlite *db, int preserveTemps){
drhbeae3192001-09-22 18:12:08 +0000392 HashElem *pElem;
393 Hash temp1;
danielk1977c3f9bad2002-05-15 08:30:12 +0000394 Hash temp2;
drhe4697f52002-05-23 02:09:03 +0000395
396 /* Make sure there are no uncommited DROPs */
397 assert( sqliteHashFirst(&db->tblDrop)==0 || sqlite_malloc_failed );
398 assert( sqliteHashFirst(&db->idxDrop)==0 || sqlite_malloc_failed );
399 assert( sqliteHashFirst(&db->trigDrop)==0 || sqlite_malloc_failed );
drhbeae3192001-09-22 18:12:08 +0000400 temp1 = db->tblHash;
danielk1977c3f9bad2002-05-15 08:30:12 +0000401 temp2 = db->trigHash;
402 sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0);
drhbeae3192001-09-22 18:12:08 +0000403 sqliteHashClear(&db->idxHash);
danielk1977c3f9bad2002-05-15 08:30:12 +0000404
danielk1977f29ce552002-05-19 23:43:12 +0000405 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
danielk1977c3f9bad2002-05-15 08:30:12 +0000406 Trigger * pTrigger = sqliteHashData(pElem);
407 Table *pTab = sqliteFindTable(db, pTrigger->table);
408 assert(pTab);
drh0b4efed2002-05-23 13:15:37 +0000409 if( pTab->isTemp && preserveTemps ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000410 sqliteHashInsert(&db->trigHash, pTrigger->name, strlen(pTrigger->name),
drh9adf9ac2002-05-15 11:44:13 +0000411 pTrigger);
danielk1977f29ce552002-05-19 23:43:12 +0000412 }else{
danielk1977c3f9bad2002-05-15 08:30:12 +0000413 sqliteDeleteTrigger(pTrigger);
414 }
415 }
416 sqliteHashClear(&temp2);
417
418 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
419
drhbeae3192001-09-22 18:12:08 +0000420 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
drhf57b3392001-10-08 13:22:32 +0000421 Table *pTab = sqliteHashData(pElem);
422 if( preserveTemps && pTab->isTemp ){
423 Index *pIdx;
drh6d4abfb2001-10-22 02:58:08 +0000424 int nName = strlen(pTab->zName);
425 Table *pOld = sqliteHashInsert(&db->tblHash, pTab->zName, nName+1, pTab);
426 if( pOld!=0 ){
427 assert( pOld==pTab ); /* Malloc failed on the HashInsert */
428 sqliteDeleteTable(db, pOld);
429 continue;
430 }
drhf57b3392001-10-08 13:22:32 +0000431 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
432 int n = strlen(pIdx->zName)+1;
drh6d4abfb2001-10-22 02:58:08 +0000433 Index *pOldIdx;
434 pOldIdx = sqliteHashInsert(&db->idxHash, pIdx->zName, n, pIdx);
435 if( pOld ){
436 assert( pOldIdx==pIdx );
437 sqliteUnlinkAndDeleteIndex(db, pOldIdx);
438 }
drhf57b3392001-10-08 13:22:32 +0000439 }
440 }else{
441 sqliteDeleteTable(db, pTab);
442 }
drh75897232000-05-29 14:26:00 +0000443 }
drhbeae3192001-09-22 18:12:08 +0000444 sqliteHashClear(&temp1);
drh50e5dad2001-09-15 00:57:28 +0000445 db->flags &= ~SQLITE_Initialized;
446}
447
448/*
drhaf9ff332002-01-16 21:00:27 +0000449** Return the ROWID of the most recent insert
450*/
451int sqlite_last_insert_rowid(sqlite *db){
452 return db->lastRowid;
453}
454
455/*
drhc8d30ac2002-04-12 10:08:59 +0000456** Return the number of changes in the most recent call to sqlite_exec().
457*/
458int sqlite_changes(sqlite *db){
459 return db->nChange;
460}
461
462/*
drh50e5dad2001-09-15 00:57:28 +0000463** Close an existing SQLite database
464*/
465void sqlite_close(sqlite *db){
drh8e0a2f92002-02-23 23:45:45 +0000466 HashElem *i;
drhc22bd472002-05-10 13:14:07 +0000467 if( sqliteSafetyCheck(db) || sqliteSafetyOn(db) ){ return; }
drh247be432002-05-10 05:44:55 +0000468 db->magic = SQLITE_MAGIC_CLOSED;
drh50e5dad2001-09-15 00:57:28 +0000469 sqliteBtreeClose(db->pBe);
drhf57b3392001-10-08 13:22:32 +0000470 clearHashTable(db, 0);
471 if( db->pBeTemp ){
472 sqliteBtreeClose(db->pBeTemp);
473 }
drh0bce8352002-02-28 00:41:10 +0000474 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
475 FuncDef *pFunc, *pNext;
476 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
drh8e0a2f92002-02-23 23:45:45 +0000477 pNext = pFunc->pNext;
478 sqliteFree(pFunc);
479 }
480 }
drh0bce8352002-02-28 00:41:10 +0000481 sqliteHashClear(&db->aFunc);
drh75897232000-05-29 14:26:00 +0000482 sqliteFree(db);
483}
484
485/*
486** Return TRUE if the given SQL string ends in a semicolon.
drhce9079c2002-05-15 14:17:44 +0000487**
488** Special handling is require for CREATE TRIGGER statements.
489** Whenever the CREATE TRIGGER keywords are seen, the statement
490** must end with ";END;".
drh75897232000-05-29 14:26:00 +0000491*/
492int sqlite_complete(const char *zSql){
drhce9079c2002-05-15 14:17:44 +0000493 int isComplete = 1;
494 int requireEnd = 0;
495 int seenText = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000496 int seenCreate = 0;
drh8c82b352000-12-10 18:23:50 +0000497 while( *zSql ){
498 switch( *zSql ){
499 case ';': {
500 isComplete = 1;
drhce9079c2002-05-15 14:17:44 +0000501 seenText = 1;
502 seenCreate = 0;
drh75897232000-05-29 14:26:00 +0000503 break;
drh8c82b352000-12-10 18:23:50 +0000504 }
505 case ' ':
506 case '\t':
507 case '\n':
508 case '\f': {
drh75897232000-05-29 14:26:00 +0000509 break;
drh8c82b352000-12-10 18:23:50 +0000510 }
drh969fa7c2002-02-18 18:30:32 +0000511 case '[': {
512 isComplete = 0;
drhce9079c2002-05-15 14:17:44 +0000513 seenText = 1;
514 seenCreate = 0;
drh969fa7c2002-02-18 18:30:32 +0000515 zSql++;
516 while( *zSql && *zSql!=']' ){ zSql++; }
517 if( *zSql==0 ) return 0;
518 break;
519 }
drhce9079c2002-05-15 14:17:44 +0000520 case '"':
drh8c82b352000-12-10 18:23:50 +0000521 case '\'': {
drhce9079c2002-05-15 14:17:44 +0000522 int c = *zSql;
drh8c82b352000-12-10 18:23:50 +0000523 isComplete = 0;
drhce9079c2002-05-15 14:17:44 +0000524 seenText = 1;
525 seenCreate = 0;
drh8c82b352000-12-10 18:23:50 +0000526 zSql++;
drhce9079c2002-05-15 14:17:44 +0000527 while( *zSql && *zSql!=c ){ zSql++; }
drh8c82b352000-12-10 18:23:50 +0000528 if( *zSql==0 ) return 0;
529 break;
530 }
531 case '-': {
532 if( zSql[1]!='-' ){
533 isComplete = 0;
drhce9079c2002-05-15 14:17:44 +0000534 seenCreate = 0;
drh8c82b352000-12-10 18:23:50 +0000535 break;
536 }
537 while( *zSql && *zSql!='\n' ){ zSql++; }
drhce9079c2002-05-15 14:17:44 +0000538 if( *zSql==0 ) return seenText && isComplete && requireEnd==0;
539 break;
540 }
541 case 'c':
542 case 'C': {
543 seenText = 1;
544 if( !isComplete ) break;
545 isComplete = 0;
546 if( sqliteStrNICmp(zSql, "create", 6)!=0 ) break;
547 if( !isspace(zSql[6]) ) break;
548 zSql += 5;
549 seenCreate = 1;
550 while( isspace(zSql[1]) ) zSql++;
551 if( sqliteStrNICmp(&zSql[1],"trigger", 7)!=0 ) break;
552 zSql += 7;
553 requireEnd++;
554 break;
555 }
556 case 't':
557 case 'T': {
558 seenText = 1;
559 if( !seenCreate ) break;
560 seenCreate = 0;
561 isComplete = 0;
562 if( sqliteStrNICmp(zSql, "trigger", 7)!=0 ) break;
563 if( !isspace(zSql[7]) ) break;
564 zSql += 6;
565 requireEnd++;
566 break;
567 }
568 case 'e':
569 case 'E': {
570 seenCreate = 0;
571 seenText = 1;
572 if( !isComplete ) break;
573 isComplete = 0;
574 if( requireEnd==0 ) break;
575 if( sqliteStrNICmp(zSql, "end", 3)!=0 ) break;
576 zSql += 2;
577 while( isspace(zSql[1]) ) zSql++;
578 if( zSql[1]==';' ){
579 zSql++;
580 isComplete = 1;
581 requireEnd--;
582 }
drh8c82b352000-12-10 18:23:50 +0000583 break;
drh9adf9ac2002-05-15 11:44:13 +0000584 }
drh8c82b352000-12-10 18:23:50 +0000585 default: {
drhce9079c2002-05-15 14:17:44 +0000586 seenCreate = 0;
587 seenText = 1;
drh8c82b352000-12-10 18:23:50 +0000588 isComplete = 0;
589 break;
590 }
drh75897232000-05-29 14:26:00 +0000591 }
drh8c82b352000-12-10 18:23:50 +0000592 zSql++;
drh75897232000-05-29 14:26:00 +0000593 }
drhce9079c2002-05-15 14:17:44 +0000594 return seenText && isComplete && requireEnd==0;
drh75897232000-05-29 14:26:00 +0000595}
596
597/*
drhbed86902000-06-02 13:27:59 +0000598** Execute SQL code. Return one of the SQLITE_ success/failure
599** codes. Also write an error message into memory obtained from
600** malloc() and make *pzErrMsg point to that message.
601**
602** If the SQL is a query, then for each row in the query result
603** the xCallback() function is called. pArg becomes the first
604** argument to xCallback(). If xCallback=NULL then no callback
605** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000606*/
607int sqlite_exec(
608 sqlite *db, /* The database on which the SQL executes */
drh9f71c2e2001-11-03 23:57:09 +0000609 const char *zSql, /* The SQL to be executed */
drh75897232000-05-29 14:26:00 +0000610 sqlite_callback xCallback, /* Invoke this callback routine */
611 void *pArg, /* First argument to xCallback() */
612 char **pzErrMsg /* Write error messages here */
613){
614 Parse sParse;
drh75897232000-05-29 14:26:00 +0000615
616 if( pzErrMsg ) *pzErrMsg = 0;
drhc22bd472002-05-10 13:14:07 +0000617 if( sqliteSafetyOn(db) ) goto exec_misuse;
drh58b95762000-06-02 01:17:37 +0000618 if( (db->flags & SQLITE_Initialized)==0 ){
619 int rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000620 if( rc!=SQLITE_OK ){
621 sqliteStrRealloc(pzErrMsg);
drh247be432002-05-10 05:44:55 +0000622 sqliteSafetyOff(db);
drhdaffd0e2001-04-11 14:28:42 +0000623 return rc;
624 }
drh58b95762000-06-02 01:17:37 +0000625 }
drhc8d30ac2002-04-12 10:08:59 +0000626 if( db->recursionDepth==0 ){ db->nChange = 0; }
627 db->recursionDepth++;
drh75897232000-05-29 14:26:00 +0000628 memset(&sParse, 0, sizeof(sParse));
629 sParse.db = db;
drh5e00f6c2001-09-13 13:46:56 +0000630 sParse.pBe = db->pBe;
drh75897232000-05-29 14:26:00 +0000631 sParse.xCallback = xCallback;
632 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000633 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000634 if( sqlite_malloc_failed ){
635 sqliteSetString(pzErrMsg, "out of memory", 0);
636 sParse.rc = SQLITE_NOMEM;
drh6d4abfb2001-10-22 02:58:08 +0000637 sqliteBtreeRollback(db->pBe);
638 if( db->pBeTemp ) sqliteBtreeRollback(db->pBeTemp);
639 db->flags &= ~SQLITE_InTrans;
640 clearHashTable(db, 0);
drhdaffd0e2001-04-11 14:28:42 +0000641 }
642 sqliteStrRealloc(pzErrMsg);
drh50e5dad2001-09-15 00:57:28 +0000643 if( sParse.rc==SQLITE_SCHEMA ){
drhf57b3392001-10-08 13:22:32 +0000644 clearHashTable(db, 1);
drh50e5dad2001-09-15 00:57:28 +0000645 }
drhc8d30ac2002-04-12 10:08:59 +0000646 db->recursionDepth--;
drhc22bd472002-05-10 13:14:07 +0000647 if( sqliteSafetyOff(db) ) goto exec_misuse;
drh4c504392000-10-16 22:06:40 +0000648 return sParse.rc;
drhc22bd472002-05-10 13:14:07 +0000649
650exec_misuse:
651 if( pzErrMsg ){
652 *pzErrMsg = 0;
653 sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), 0);
654 sqliteStrRealloc(pzErrMsg);
655 }
656 return SQLITE_MISUSE;
drh75897232000-05-29 14:26:00 +0000657}
drh2dfbbca2000-07-28 14:32:48 +0000658
659/*
drhc22bd472002-05-10 13:14:07 +0000660** Return a static string that describes the kind of error specified in the
661** argument.
drh247be432002-05-10 05:44:55 +0000662*/
drhc22bd472002-05-10 13:14:07 +0000663const char *sqlite_error_string(int rc){
664 const char *z;
665 switch( rc ){
666 case SQLITE_OK: z = "not an error"; break;
667 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
668 case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break;
669 case SQLITE_PERM: z = "access permission denied"; break;
670 case SQLITE_ABORT: z = "callback requested query abort"; break;
671 case SQLITE_BUSY: z = "database is locked"; break;
672 case SQLITE_LOCKED: z = "database table is locked"; break;
673 case SQLITE_NOMEM: z = "out of memory"; break;
674 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
675 case SQLITE_INTERRUPT: z = "interrupted"; break;
676 case SQLITE_IOERR: z = "disk I/O error"; break;
677 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
678 case SQLITE_NOTFOUND: z = "table or record not found"; break;
679 case SQLITE_FULL: z = "database is full"; break;
680 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
681 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
682 case SQLITE_EMPTY: z = "table contains no data"; break;
683 case SQLITE_SCHEMA: z = "database schema has changed"; break;
684 case SQLITE_TOOBIG: z = "too much data for one table row"; break;
685 case SQLITE_CONSTRAINT: z = "constraint failed"; break;
686 case SQLITE_MISMATCH: z = "datatype mismatch"; break;
687 case SQLITE_MISUSE: z = "library routine called out of sequence";break;
688 default: z = "unknown error"; break;
drh247be432002-05-10 05:44:55 +0000689 }
drhc22bd472002-05-10 13:14:07 +0000690 return z;
drh247be432002-05-10 05:44:55 +0000691}
692
693/*
drh2dfbbca2000-07-28 14:32:48 +0000694** This routine implements a busy callback that sleeps and tries
695** again until a timeout value is reached. The timeout value is
696** an integer number of milliseconds passed in as the first
697** argument.
698*/
drhdaffd0e2001-04-11 14:28:42 +0000699static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000700 void *Timeout, /* Maximum amount of time to wait */
701 const char *NotUsed, /* The name of the table that is busy */
702 int count /* Number of times table has been busy */
703){
drh8cfbf082001-09-19 13:22:39 +0000704#if SQLITE_MIN_SLEEP_MS==1
705 int delay = 10;
drh2dfbbca2000-07-28 14:32:48 +0000706 int prior_delay = 0;
707 int timeout = (int)Timeout;
708 int i;
709
710 for(i=1; i<count; i++){
711 prior_delay += delay;
712 delay = delay*2;
drh8cfbf082001-09-19 13:22:39 +0000713 if( delay>=1000 ){
714 delay = 1000;
715 prior_delay += 1000*(count - i - 1);
drh2dfbbca2000-07-28 14:32:48 +0000716 break;
717 }
718 }
drh3109e022001-10-09 13:46:01 +0000719 if( prior_delay + delay > timeout ){
720 delay = timeout - prior_delay;
drh2dfbbca2000-07-28 14:32:48 +0000721 if( delay<=0 ) return 0;
722 }
drh8cfbf082001-09-19 13:22:39 +0000723 sqliteOsSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000724 return 1;
725#else
726 int timeout = (int)Timeout;
727 if( (count+1)*1000 > timeout ){
728 return 0;
729 }
drh8cfbf082001-09-19 13:22:39 +0000730 sqliteOsSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000731 return 1;
732#endif
733}
734
735/*
736** This routine sets the busy callback for an Sqlite database to the
737** given callback function with the given argument.
738*/
739void sqlite_busy_handler(
740 sqlite *db,
741 int (*xBusy)(void*,const char*,int),
742 void *pArg
743){
744 db->xBusyCallback = xBusy;
745 db->pBusyArg = pArg;
746}
747
748/*
749** This routine installs a default busy handler that waits for the
750** specified number of milliseconds before returning 0.
751*/
752void sqlite_busy_timeout(sqlite *db, int ms){
753 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000754 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000755 }else{
756 sqlite_busy_handler(db, 0, 0);
757 }
758}
drh4c504392000-10-16 22:06:40 +0000759
760/*
761** Cause any pending operation to stop at its earliest opportunity.
762*/
763void sqlite_interrupt(sqlite *db){
764 db->flags |= SQLITE_Interrupt;
765}
drhfa86c412002-02-02 15:01:15 +0000766
767/*
768** Windows systems should call this routine to free memory that
769** is returned in the in the errmsg parameter of sqlite_open() when
770** SQLite is a DLL. For some reason, it does not work to call free()
771** directly.
772**
773** Note that we need to call free() not sqliteFree() here, since every
774** string that is exported from SQLite should have already passed through
775** sqliteStrRealloc().
776*/
777void sqlite_freemem(void *p){ free(p); }
778
779/*
780** Windows systems need functions to call to return the sqlite_version
781** and sqlite_encoding strings.
782*/
783const char *sqlite_libversion(void){ return sqlite_version; }
784const char *sqlite_libencoding(void){ return sqlite_encoding; }
drh8e0a2f92002-02-23 23:45:45 +0000785
786/*
787** Create new user-defined functions. The sqlite_create_function()
788** routine creates a regular function and sqlite_create_aggregate()
789** creates an aggregate function.
790**
791** Passing a NULL xFunc argument or NULL xStep and xFinalize arguments
792** disables the function. Calling sqlite_create_function() with the
793** same name and number of arguments as a prior call to
794** sqlite_create_aggregate() disables the prior call to
795** sqlite_create_aggregate(), and vice versa.
796**
797** If nArg is -1 it means that this function will accept any number
798** of arguments, including 0.
799*/
800int sqlite_create_function(
801 sqlite *db, /* Add the function to this database connection */
802 const char *zName, /* Name of the function to add */
803 int nArg, /* Number of arguments */
drh1350b032002-02-27 19:00:20 +0000804 void (*xFunc)(sqlite_func*,int,const char**), /* The implementation */
805 void *pUserData /* User data */
drh8e0a2f92002-02-23 23:45:45 +0000806){
drh0bce8352002-02-28 00:41:10 +0000807 FuncDef *p;
drhc22bd472002-05-10 13:14:07 +0000808 if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1;
drh0bce8352002-02-28 00:41:10 +0000809 p = sqliteFindFunction(db, zName, strlen(zName), nArg, 1);
drh4e0f9952002-02-27 01:53:13 +0000810 if( p==0 ) return 1;
drh8e0a2f92002-02-23 23:45:45 +0000811 p->xFunc = xFunc;
812 p->xStep = 0;
813 p->xFinalize = 0;
drh1350b032002-02-27 19:00:20 +0000814 p->pUserData = pUserData;
drh8e0a2f92002-02-23 23:45:45 +0000815 return 0;
816}
817int sqlite_create_aggregate(
818 sqlite *db, /* Add the function to this database connection */
819 const char *zName, /* Name of the function to add */
820 int nArg, /* Number of arguments */
drh1350b032002-02-27 19:00:20 +0000821 void (*xStep)(sqlite_func*,int,const char**), /* The step function */
822 void (*xFinalize)(sqlite_func*), /* The finalizer */
823 void *pUserData /* User data */
drh8e0a2f92002-02-23 23:45:45 +0000824){
drh0bce8352002-02-28 00:41:10 +0000825 FuncDef *p;
drhc22bd472002-05-10 13:14:07 +0000826 if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1;
drh0bce8352002-02-28 00:41:10 +0000827 p = sqliteFindFunction(db, zName, strlen(zName), nArg, 1);
drh4e0f9952002-02-27 01:53:13 +0000828 if( p==0 ) return 1;
drh8e0a2f92002-02-23 23:45:45 +0000829 p->xFunc = 0;
830 p->xStep = xStep;
831 p->xFinalize = xFinalize;
drh1350b032002-02-27 19:00:20 +0000832 p->pUserData = pUserData;
drh8e0a2f92002-02-23 23:45:45 +0000833 return 0;
834}
drhc9b84a12002-06-20 11:36:48 +0000835
836/*
837** Change the datatype for all functions with a given name.
838*/
839int sqlite_function_type(sqlite *db, const char *zName, int dataType){
840 FuncDef *p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, strlen(zName));
841 while( p ){
842 p->dataType = dataType;
843 p = p->pNext;
844 }
845}