blob: 653c315e99163a598461a2c2aa37795a307c8e64 [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**
drhbb3a6db2002-06-21 11:55:48 +000017** $Id: main.c,v 1.82 2002/06/21 11:55: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);
drhbb3a6db2002-06-21 11:55:48 +0000470 sqliteRollbackInternalChanges(db);
drhf57b3392001-10-08 13:22:32 +0000471 clearHashTable(db, 0);
472 if( db->pBeTemp ){
473 sqliteBtreeClose(db->pBeTemp);
474 }
drh0bce8352002-02-28 00:41:10 +0000475 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
476 FuncDef *pFunc, *pNext;
477 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
drh8e0a2f92002-02-23 23:45:45 +0000478 pNext = pFunc->pNext;
479 sqliteFree(pFunc);
480 }
481 }
drh0bce8352002-02-28 00:41:10 +0000482 sqliteHashClear(&db->aFunc);
drh75897232000-05-29 14:26:00 +0000483 sqliteFree(db);
484}
485
486/*
487** Return TRUE if the given SQL string ends in a semicolon.
drhce9079c2002-05-15 14:17:44 +0000488**
489** Special handling is require for CREATE TRIGGER statements.
490** Whenever the CREATE TRIGGER keywords are seen, the statement
491** must end with ";END;".
drh75897232000-05-29 14:26:00 +0000492*/
493int sqlite_complete(const char *zSql){
drhce9079c2002-05-15 14:17:44 +0000494 int isComplete = 1;
495 int requireEnd = 0;
496 int seenText = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000497 int seenCreate = 0;
drh8c82b352000-12-10 18:23:50 +0000498 while( *zSql ){
499 switch( *zSql ){
500 case ';': {
501 isComplete = 1;
drhce9079c2002-05-15 14:17:44 +0000502 seenText = 1;
503 seenCreate = 0;
drh75897232000-05-29 14:26:00 +0000504 break;
drh8c82b352000-12-10 18:23:50 +0000505 }
506 case ' ':
507 case '\t':
508 case '\n':
509 case '\f': {
drh75897232000-05-29 14:26:00 +0000510 break;
drh8c82b352000-12-10 18:23:50 +0000511 }
drh969fa7c2002-02-18 18:30:32 +0000512 case '[': {
513 isComplete = 0;
drhce9079c2002-05-15 14:17:44 +0000514 seenText = 1;
515 seenCreate = 0;
drh969fa7c2002-02-18 18:30:32 +0000516 zSql++;
517 while( *zSql && *zSql!=']' ){ zSql++; }
518 if( *zSql==0 ) return 0;
519 break;
520 }
drhce9079c2002-05-15 14:17:44 +0000521 case '"':
drh8c82b352000-12-10 18:23:50 +0000522 case '\'': {
drhce9079c2002-05-15 14:17:44 +0000523 int c = *zSql;
drh8c82b352000-12-10 18:23:50 +0000524 isComplete = 0;
drhce9079c2002-05-15 14:17:44 +0000525 seenText = 1;
526 seenCreate = 0;
drh8c82b352000-12-10 18:23:50 +0000527 zSql++;
drhce9079c2002-05-15 14:17:44 +0000528 while( *zSql && *zSql!=c ){ zSql++; }
drh8c82b352000-12-10 18:23:50 +0000529 if( *zSql==0 ) return 0;
530 break;
531 }
532 case '-': {
533 if( zSql[1]!='-' ){
534 isComplete = 0;
drhce9079c2002-05-15 14:17:44 +0000535 seenCreate = 0;
drh8c82b352000-12-10 18:23:50 +0000536 break;
537 }
538 while( *zSql && *zSql!='\n' ){ zSql++; }
drhce9079c2002-05-15 14:17:44 +0000539 if( *zSql==0 ) return seenText && isComplete && requireEnd==0;
540 break;
541 }
542 case 'c':
543 case 'C': {
544 seenText = 1;
545 if( !isComplete ) break;
546 isComplete = 0;
547 if( sqliteStrNICmp(zSql, "create", 6)!=0 ) break;
548 if( !isspace(zSql[6]) ) break;
549 zSql += 5;
550 seenCreate = 1;
551 while( isspace(zSql[1]) ) zSql++;
552 if( sqliteStrNICmp(&zSql[1],"trigger", 7)!=0 ) break;
553 zSql += 7;
554 requireEnd++;
555 break;
556 }
557 case 't':
558 case 'T': {
559 seenText = 1;
560 if( !seenCreate ) break;
561 seenCreate = 0;
562 isComplete = 0;
563 if( sqliteStrNICmp(zSql, "trigger", 7)!=0 ) break;
564 if( !isspace(zSql[7]) ) break;
565 zSql += 6;
566 requireEnd++;
567 break;
568 }
569 case 'e':
570 case 'E': {
571 seenCreate = 0;
572 seenText = 1;
573 if( !isComplete ) break;
574 isComplete = 0;
575 if( requireEnd==0 ) break;
576 if( sqliteStrNICmp(zSql, "end", 3)!=0 ) break;
577 zSql += 2;
578 while( isspace(zSql[1]) ) zSql++;
579 if( zSql[1]==';' ){
580 zSql++;
581 isComplete = 1;
582 requireEnd--;
583 }
drh8c82b352000-12-10 18:23:50 +0000584 break;
drh9adf9ac2002-05-15 11:44:13 +0000585 }
drh8c82b352000-12-10 18:23:50 +0000586 default: {
drhce9079c2002-05-15 14:17:44 +0000587 seenCreate = 0;
588 seenText = 1;
drh8c82b352000-12-10 18:23:50 +0000589 isComplete = 0;
590 break;
591 }
drh75897232000-05-29 14:26:00 +0000592 }
drh8c82b352000-12-10 18:23:50 +0000593 zSql++;
drh75897232000-05-29 14:26:00 +0000594 }
drhce9079c2002-05-15 14:17:44 +0000595 return seenText && isComplete && requireEnd==0;
drh75897232000-05-29 14:26:00 +0000596}
597
598/*
drhbed86902000-06-02 13:27:59 +0000599** Execute SQL code. Return one of the SQLITE_ success/failure
600** codes. Also write an error message into memory obtained from
601** malloc() and make *pzErrMsg point to that message.
602**
603** If the SQL is a query, then for each row in the query result
604** the xCallback() function is called. pArg becomes the first
605** argument to xCallback(). If xCallback=NULL then no callback
606** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000607*/
608int sqlite_exec(
609 sqlite *db, /* The database on which the SQL executes */
drh9f71c2e2001-11-03 23:57:09 +0000610 const char *zSql, /* The SQL to be executed */
drh75897232000-05-29 14:26:00 +0000611 sqlite_callback xCallback, /* Invoke this callback routine */
612 void *pArg, /* First argument to xCallback() */
613 char **pzErrMsg /* Write error messages here */
614){
615 Parse sParse;
drh75897232000-05-29 14:26:00 +0000616
617 if( pzErrMsg ) *pzErrMsg = 0;
drhc22bd472002-05-10 13:14:07 +0000618 if( sqliteSafetyOn(db) ) goto exec_misuse;
drh58b95762000-06-02 01:17:37 +0000619 if( (db->flags & SQLITE_Initialized)==0 ){
620 int rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000621 if( rc!=SQLITE_OK ){
622 sqliteStrRealloc(pzErrMsg);
drh247be432002-05-10 05:44:55 +0000623 sqliteSafetyOff(db);
drhdaffd0e2001-04-11 14:28:42 +0000624 return rc;
625 }
drh58b95762000-06-02 01:17:37 +0000626 }
drhc8d30ac2002-04-12 10:08:59 +0000627 if( db->recursionDepth==0 ){ db->nChange = 0; }
628 db->recursionDepth++;
drh75897232000-05-29 14:26:00 +0000629 memset(&sParse, 0, sizeof(sParse));
630 sParse.db = db;
drh5e00f6c2001-09-13 13:46:56 +0000631 sParse.pBe = db->pBe;
drh75897232000-05-29 14:26:00 +0000632 sParse.xCallback = xCallback;
633 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000634 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000635 if( sqlite_malloc_failed ){
636 sqliteSetString(pzErrMsg, "out of memory", 0);
637 sParse.rc = SQLITE_NOMEM;
drh6d4abfb2001-10-22 02:58:08 +0000638 sqliteBtreeRollback(db->pBe);
639 if( db->pBeTemp ) sqliteBtreeRollback(db->pBeTemp);
640 db->flags &= ~SQLITE_InTrans;
641 clearHashTable(db, 0);
drhdaffd0e2001-04-11 14:28:42 +0000642 }
643 sqliteStrRealloc(pzErrMsg);
drh50e5dad2001-09-15 00:57:28 +0000644 if( sParse.rc==SQLITE_SCHEMA ){
drhf57b3392001-10-08 13:22:32 +0000645 clearHashTable(db, 1);
drh50e5dad2001-09-15 00:57:28 +0000646 }
drhc8d30ac2002-04-12 10:08:59 +0000647 db->recursionDepth--;
drhc22bd472002-05-10 13:14:07 +0000648 if( sqliteSafetyOff(db) ) goto exec_misuse;
drh4c504392000-10-16 22:06:40 +0000649 return sParse.rc;
drhc22bd472002-05-10 13:14:07 +0000650
651exec_misuse:
652 if( pzErrMsg ){
653 *pzErrMsg = 0;
654 sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), 0);
655 sqliteStrRealloc(pzErrMsg);
656 }
657 return SQLITE_MISUSE;
drh75897232000-05-29 14:26:00 +0000658}
drh2dfbbca2000-07-28 14:32:48 +0000659
660/*
drhc22bd472002-05-10 13:14:07 +0000661** Return a static string that describes the kind of error specified in the
662** argument.
drh247be432002-05-10 05:44:55 +0000663*/
drhc22bd472002-05-10 13:14:07 +0000664const char *sqlite_error_string(int rc){
665 const char *z;
666 switch( rc ){
667 case SQLITE_OK: z = "not an error"; break;
668 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
669 case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break;
670 case SQLITE_PERM: z = "access permission denied"; break;
671 case SQLITE_ABORT: z = "callback requested query abort"; break;
672 case SQLITE_BUSY: z = "database is locked"; break;
673 case SQLITE_LOCKED: z = "database table is locked"; break;
674 case SQLITE_NOMEM: z = "out of memory"; break;
675 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
676 case SQLITE_INTERRUPT: z = "interrupted"; break;
677 case SQLITE_IOERR: z = "disk I/O error"; break;
678 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
679 case SQLITE_NOTFOUND: z = "table or record not found"; break;
680 case SQLITE_FULL: z = "database is full"; break;
681 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
682 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
683 case SQLITE_EMPTY: z = "table contains no data"; break;
684 case SQLITE_SCHEMA: z = "database schema has changed"; break;
685 case SQLITE_TOOBIG: z = "too much data for one table row"; break;
686 case SQLITE_CONSTRAINT: z = "constraint failed"; break;
687 case SQLITE_MISMATCH: z = "datatype mismatch"; break;
688 case SQLITE_MISUSE: z = "library routine called out of sequence";break;
689 default: z = "unknown error"; break;
drh247be432002-05-10 05:44:55 +0000690 }
drhc22bd472002-05-10 13:14:07 +0000691 return z;
drh247be432002-05-10 05:44:55 +0000692}
693
694/*
drh2dfbbca2000-07-28 14:32:48 +0000695** This routine implements a busy callback that sleeps and tries
696** again until a timeout value is reached. The timeout value is
697** an integer number of milliseconds passed in as the first
698** argument.
699*/
drhdaffd0e2001-04-11 14:28:42 +0000700static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000701 void *Timeout, /* Maximum amount of time to wait */
702 const char *NotUsed, /* The name of the table that is busy */
703 int count /* Number of times table has been busy */
704){
drh8cfbf082001-09-19 13:22:39 +0000705#if SQLITE_MIN_SLEEP_MS==1
706 int delay = 10;
drh2dfbbca2000-07-28 14:32:48 +0000707 int prior_delay = 0;
708 int timeout = (int)Timeout;
709 int i;
710
711 for(i=1; i<count; i++){
712 prior_delay += delay;
713 delay = delay*2;
drh8cfbf082001-09-19 13:22:39 +0000714 if( delay>=1000 ){
715 delay = 1000;
716 prior_delay += 1000*(count - i - 1);
drh2dfbbca2000-07-28 14:32:48 +0000717 break;
718 }
719 }
drh3109e022001-10-09 13:46:01 +0000720 if( prior_delay + delay > timeout ){
721 delay = timeout - prior_delay;
drh2dfbbca2000-07-28 14:32:48 +0000722 if( delay<=0 ) return 0;
723 }
drh8cfbf082001-09-19 13:22:39 +0000724 sqliteOsSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000725 return 1;
726#else
727 int timeout = (int)Timeout;
728 if( (count+1)*1000 > timeout ){
729 return 0;
730 }
drh8cfbf082001-09-19 13:22:39 +0000731 sqliteOsSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000732 return 1;
733#endif
734}
735
736/*
737** This routine sets the busy callback for an Sqlite database to the
738** given callback function with the given argument.
739*/
740void sqlite_busy_handler(
741 sqlite *db,
742 int (*xBusy)(void*,const char*,int),
743 void *pArg
744){
745 db->xBusyCallback = xBusy;
746 db->pBusyArg = pArg;
747}
748
749/*
750** This routine installs a default busy handler that waits for the
751** specified number of milliseconds before returning 0.
752*/
753void sqlite_busy_timeout(sqlite *db, int ms){
754 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000755 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000756 }else{
757 sqlite_busy_handler(db, 0, 0);
758 }
759}
drh4c504392000-10-16 22:06:40 +0000760
761/*
762** Cause any pending operation to stop at its earliest opportunity.
763*/
764void sqlite_interrupt(sqlite *db){
765 db->flags |= SQLITE_Interrupt;
766}
drhfa86c412002-02-02 15:01:15 +0000767
768/*
769** Windows systems should call this routine to free memory that
770** is returned in the in the errmsg parameter of sqlite_open() when
771** SQLite is a DLL. For some reason, it does not work to call free()
772** directly.
773**
774** Note that we need to call free() not sqliteFree() here, since every
775** string that is exported from SQLite should have already passed through
776** sqliteStrRealloc().
777*/
778void sqlite_freemem(void *p){ free(p); }
779
780/*
781** Windows systems need functions to call to return the sqlite_version
782** and sqlite_encoding strings.
783*/
784const char *sqlite_libversion(void){ return sqlite_version; }
785const char *sqlite_libencoding(void){ return sqlite_encoding; }
drh8e0a2f92002-02-23 23:45:45 +0000786
787/*
788** Create new user-defined functions. The sqlite_create_function()
789** routine creates a regular function and sqlite_create_aggregate()
790** creates an aggregate function.
791**
792** Passing a NULL xFunc argument or NULL xStep and xFinalize arguments
793** disables the function. Calling sqlite_create_function() with the
794** same name and number of arguments as a prior call to
795** sqlite_create_aggregate() disables the prior call to
796** sqlite_create_aggregate(), and vice versa.
797**
798** If nArg is -1 it means that this function will accept any number
799** of arguments, including 0.
800*/
801int sqlite_create_function(
802 sqlite *db, /* Add the function to this database connection */
803 const char *zName, /* Name of the function to add */
804 int nArg, /* Number of arguments */
drh1350b032002-02-27 19:00:20 +0000805 void (*xFunc)(sqlite_func*,int,const char**), /* The implementation */
806 void *pUserData /* User data */
drh8e0a2f92002-02-23 23:45:45 +0000807){
drh0bce8352002-02-28 00:41:10 +0000808 FuncDef *p;
drhc22bd472002-05-10 13:14:07 +0000809 if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1;
drh0bce8352002-02-28 00:41:10 +0000810 p = sqliteFindFunction(db, zName, strlen(zName), nArg, 1);
drh4e0f9952002-02-27 01:53:13 +0000811 if( p==0 ) return 1;
drh8e0a2f92002-02-23 23:45:45 +0000812 p->xFunc = xFunc;
813 p->xStep = 0;
814 p->xFinalize = 0;
drh1350b032002-02-27 19:00:20 +0000815 p->pUserData = pUserData;
drh8e0a2f92002-02-23 23:45:45 +0000816 return 0;
817}
818int sqlite_create_aggregate(
819 sqlite *db, /* Add the function to this database connection */
820 const char *zName, /* Name of the function to add */
821 int nArg, /* Number of arguments */
drh1350b032002-02-27 19:00:20 +0000822 void (*xStep)(sqlite_func*,int,const char**), /* The step function */
823 void (*xFinalize)(sqlite_func*), /* The finalizer */
824 void *pUserData /* User data */
drh8e0a2f92002-02-23 23:45:45 +0000825){
drh0bce8352002-02-28 00:41:10 +0000826 FuncDef *p;
drhc22bd472002-05-10 13:14:07 +0000827 if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1;
drh0bce8352002-02-28 00:41:10 +0000828 p = sqliteFindFunction(db, zName, strlen(zName), nArg, 1);
drh4e0f9952002-02-27 01:53:13 +0000829 if( p==0 ) return 1;
drh8e0a2f92002-02-23 23:45:45 +0000830 p->xFunc = 0;
831 p->xStep = xStep;
832 p->xFinalize = xFinalize;
drh1350b032002-02-27 19:00:20 +0000833 p->pUserData = pUserData;
drh8e0a2f92002-02-23 23:45:45 +0000834 return 0;
835}
drhc9b84a12002-06-20 11:36:48 +0000836
837/*
838** Change the datatype for all functions with a given name.
839*/
840int sqlite_function_type(sqlite *db, const char *zName, int dataType){
841 FuncDef *p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, strlen(zName));
842 while( p ){
843 p->dataType = dataType;
844 p = p->pNext;
845 }
846}