blob: 4fa80f623827d45f8fde2597b4a2c973525d1fe0 [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**
drhc2eef3b2002-08-31 18:53:06 +000017** $Id: main.c,v 1.100 2002/08/31 18:53:06 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/*
drhc2311722002-07-19 17:46:38 +000024** A pointer to this structure is used to communicate information
25** from sqliteInit into the sqliteInitCallback.
26*/
27typedef struct {
28 sqlite *db; /* The database being initialized */
29 char **pzErrMsg; /* Error message stored here */
30} InitData;
31
32
33/*
drh75897232000-05-29 14:26:00 +000034** This is the callback routine for the code that initializes the
drh382c0242001-10-06 16:33:02 +000035** database. See sqliteInit() below for additional information.
36**
37** Each callback contains the following information:
drh28037572000-08-02 13:47:41 +000038**
drh4a324312001-12-21 14:30:42 +000039** argv[0] = "file-format" or "schema-cookie" or "table" or "index"
drhe3c41372001-09-17 20:25:58 +000040** argv[1] = table or index name or meta statement type.
41** argv[2] = root page number for table or index. NULL for meta.
drhadbca9c2001-09-27 15:11:53 +000042** argv[3] = SQL create statement for the table or index
drhe0bc4042002-06-25 01:09:11 +000043** argv[4] = "1" for temporary files, "0" for main database
drhd78eeee2001-09-13 16:18:53 +000044**
drh75897232000-05-29 14:26:00 +000045*/
drhc2311722002-07-19 17:46:38 +000046static
47int sqliteInitCallback(void *pInit, int argc, char **argv, char **azColName){
48 InitData *pData = (InitData*)pInit;
drh75897232000-05-29 14:26:00 +000049 Parse sParse;
drhd78eeee2001-09-13 16:18:53 +000050 int nErr = 0;
drh75897232000-05-29 14:26:00 +000051
drh382c0242001-10-06 16:33:02 +000052 /* TODO: Do some validity checks on all fields. In particular,
53 ** make sure fields do not contain NULLs. Otherwise we might core
54 ** when attempting to initialize from a corrupt database file. */
drhe3c41372001-09-17 20:25:58 +000055
drhe0bc4042002-06-25 01:09:11 +000056 assert( argc==5 );
drhd78eeee2001-09-13 16:18:53 +000057 switch( argv[0][0] ){
drh17f71932002-02-21 12:01:27 +000058 case 'v':
drhd78eeee2001-09-13 16:18:53 +000059 case 'i':
drh17f71932002-02-21 12:01:27 +000060 case 't': { /* CREATE TABLE, CREATE INDEX, or CREATE VIEW statements */
drhadbca9c2001-09-27 15:11:53 +000061 if( argv[3] && argv[3][0] ){
drh17f71932002-02-21 12:01:27 +000062 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
drh382c0242001-10-06 16:33:02 +000063 ** But because sParse.initFlag is set to 1, no VDBE code is generated
64 ** or executed. All the parser does is build the internal data
drh17f71932002-02-21 12:01:27 +000065 ** structures that describe the table, index, or view.
drh382c0242001-10-06 16:33:02 +000066 */
drhadbca9c2001-09-27 15:11:53 +000067 memset(&sParse, 0, sizeof(sParse));
drhc2311722002-07-19 17:46:38 +000068 sParse.db = pData->db;
drhadbca9c2001-09-27 15:11:53 +000069 sParse.initFlag = 1;
drhe0bc4042002-06-25 01:09:11 +000070 sParse.isTemp = argv[4][0] - '0';
drhadbca9c2001-09-27 15:11:53 +000071 sParse.newTnum = atoi(argv[2]);
drhc2311722002-07-19 17:46:38 +000072 sqliteRunParser(&sParse, argv[3], pData->pzErrMsg);
drhadbca9c2001-09-27 15:11:53 +000073 }else{
drh382c0242001-10-06 16:33:02 +000074 /* If the SQL column is blank it means this is an index that
75 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
drhaacc5432002-01-06 17:07:40 +000076 ** constraint for a CREATE TABLE. The index should have already
drh382c0242001-10-06 16:33:02 +000077 ** been created when we processed the CREATE TABLE. All we have
drhaacc5432002-01-06 17:07:40 +000078 ** to do here is record the root page number for that index.
drh382c0242001-10-06 16:33:02 +000079 */
drhc2311722002-07-19 17:46:38 +000080 Index *pIndex = sqliteFindIndex(pData->db, argv[1]);
drhadbca9c2001-09-27 15:11:53 +000081 if( pIndex==0 || pIndex->tnum!=0 ){
drhda9e0342002-01-10 14:31:48 +000082 /* This can occur if there exists an index on a TEMP table which
83 ** has the same name as another index on a permanent index. Since
84 ** the permanent table is hidden by the TEMP table, we can also
85 ** safely ignore the index on the permanent table.
86 */
87 /* Do Nothing */;
drhadbca9c2001-09-27 15:11:53 +000088 }else{
89 pIndex->tnum = atoi(argv[2]);
90 }
91 }
drhd78eeee2001-09-13 16:18:53 +000092 break;
93 }
94 default: {
95 /* This can not happen! */
96 nErr = 1;
97 assert( nErr==0 );
98 }
drh28037572000-08-02 13:47:41 +000099 }
drh75897232000-05-29 14:26:00 +0000100 return nErr;
101}
102
103/*
drh491791a2002-07-18 00:34:09 +0000104** This is a callback procedure used to reconstruct a table. The
105** name of the table to be reconstructed is passed in as argv[0].
106**
107** This routine is used to automatically upgrade a database from
108** format version 1 or 2 to version 3. The correct operation of
109** this routine relys on the fact that no indices are used when
110** copying a table out to a temporary file.
111*/
drhc2311722002-07-19 17:46:38 +0000112static
113int upgrade_3_callback(void *pInit, int argc, char **argv, char **NotUsed){
114 InitData *pData = (InitData*)pInit;
drh491791a2002-07-18 00:34:09 +0000115 int rc;
drh8e5ba842002-07-18 01:27:17 +0000116 Table *pTab;
117 Trigger *pTrig;
drh26b3e1b2002-07-19 18:52:40 +0000118 char *zErr = 0;
drh491791a2002-07-18 00:34:09 +0000119
drhc2311722002-07-19 17:46:38 +0000120 pTab = sqliteFindTable(pData->db, argv[0]);
drhf573c992002-07-31 00:32:50 +0000121 assert( pTab!=0 );
122 assert( sqliteStrICmp(pTab->zName, argv[0])==0 );
drh8e5ba842002-07-18 01:27:17 +0000123 if( pTab ){
124 pTrig = pTab->pTrigger;
125 pTab->pTrigger = 0; /* Disable all triggers before rebuilding the table */
126 }
drhc2311722002-07-19 17:46:38 +0000127 rc = sqlite_exec_printf(pData->db,
drh491791a2002-07-18 00:34:09 +0000128 "CREATE TEMP TABLE sqlite_x AS SELECT * FROM '%q'; "
129 "DELETE FROM '%q'; "
130 "INSERT INTO '%q' SELECT * FROM sqlite_x; "
131 "DROP TABLE sqlite_x;",
drhc2311722002-07-19 17:46:38 +0000132 0, 0, &zErr, argv[0], argv[0], argv[0]);
133 if( zErr ){
134 sqliteSetString(pData->pzErrMsg, zErr, 0);
135 sqlite_freemem(zErr);
136 }
drhf573c992002-07-31 00:32:50 +0000137
138 /* If an error occurred in the SQL above, then the transaction will
139 ** rollback which will delete the internal symbol tables. This will
140 ** cause the structure that pTab points to be deleted. In case that
141 ** happened, we need to refetch pTab.
142 */
143 pTab = sqliteFindTable(pData->db, argv[0]);
144 if( pTab ){
145 assert( sqliteStrICmp(pTab->zName, argv[0])==0 );
146 pTab->pTrigger = pTrig; /* Re-enable triggers */
147 }
drh491791a2002-07-18 00:34:09 +0000148 return rc!=SQLITE_OK;
149}
150
151
152
153/*
drh58b95762000-06-02 01:17:37 +0000154** Attempt to read the database schema and initialize internal
155** data structures. Return one of the SQLITE_ error codes to
156** indicate success or failure.
drhbed86902000-06-02 13:27:59 +0000157**
158** After the database is initialized, the SQLITE_Initialized
159** bit is set in the flags field of the sqlite structure. An
160** attempt is made to initialize the database as soon as it
161** is opened. If that fails (perhaps because another process
162** has the sqlite_master table locked) than another attempt
163** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +0000164*/
drhe0bc4042002-06-25 01:09:11 +0000165int sqliteInit(sqlite *db, char **pzErrMsg){
drh58b95762000-06-02 01:17:37 +0000166 int rc;
drhe0bc4042002-06-25 01:09:11 +0000167 BtCursor *curMain;
168 int size;
169 Table *pTab;
170 char *azArg[6];
171 int meta[SQLITE_N_BTREE_META];
172 Parse sParse;
drhc2311722002-07-19 17:46:38 +0000173 InitData initData;
drh58b95762000-06-02 01:17:37 +0000174
175 /*
176 ** The master database table has a structure like this
177 */
drh75897232000-05-29 14:26:00 +0000178 static char master_schema[] =
drhe0bc4042002-06-25 01:09:11 +0000179 "CREATE TABLE sqlite_master(\n"
180 " type text,\n"
181 " name text,\n"
182 " tbl_name text,\n"
183 " rootpage integer,\n"
184 " sql text\n"
185 ")"
186 ;
187 static char temp_master_schema[] =
188 "CREATE TEMP TABLE sqlite_temp_master(\n"
drh75897232000-05-29 14:26:00 +0000189 " type text,\n"
190 " name text,\n"
191 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000192 " rootpage integer,\n"
drh75897232000-05-29 14:26:00 +0000193 " sql text\n"
194 ")"
195 ;
196
drhe0bc4042002-06-25 01:09:11 +0000197 /* The following SQL will read the schema from the master tables.
198 ** The first version works with SQLite file formats 2 or greater.
199 ** The second version is for format 1 files.
drh75897232000-05-29 14:26:00 +0000200 **
drhe0bc4042002-06-25 01:09:11 +0000201 ** Beginning with file format 2, the rowid for new table entries
202 ** (including entries in sqlite_master) is an increasing integer.
203 ** So for file format 2 and later, we can play back sqlite_master
204 ** and all the CREATE statements will appear in the right order.
205 ** But with file format 1, table entries were random and so we
206 ** have to make sure the CREATE TABLEs occur before their corresponding
207 ** CREATE INDEXs. (We don't have to deal with CREATE VIEW or
208 ** CREATE TRIGGER in file format 1 because those constructs did
209 ** not exist then.)
drh75897232000-05-29 14:26:00 +0000210 */
drhe0bc4042002-06-25 01:09:11 +0000211 static char init_script[] =
212 "SELECT type, name, rootpage, sql, 1 FROM sqlite_temp_master "
213 "UNION ALL "
214 "SELECT type, name, rootpage, sql, 0 FROM sqlite_master";
215 static char older_init_script[] =
216 "SELECT type, name, rootpage, sql, 1 FROM sqlite_temp_master "
217 "UNION ALL "
218 "SELECT type, name, rootpage, sql, 0 FROM sqlite_master "
219 "WHERE type='table' "
220 "UNION ALL "
221 "SELECT type, name, rootpage, sql, 0 FROM sqlite_master "
222 "WHERE type='index'";
drh17f71932002-02-21 12:01:27 +0000223
drh603240c2002-03-05 01:11:12 +0000224
drhe0bc4042002-06-25 01:09:11 +0000225 /* Construct the schema tables: sqlite_master and sqlite_temp_master
drh58b95762000-06-02 01:17:37 +0000226 */
drhe0bc4042002-06-25 01:09:11 +0000227 azArg[0] = "table";
228 azArg[1] = MASTER_NAME;
229 azArg[2] = "2";
230 azArg[3] = master_schema;
231 azArg[4] = "0";
232 azArg[5] = 0;
drhc2311722002-07-19 17:46:38 +0000233 initData.db = db;
234 initData.pzErrMsg = pzErrMsg;
235 sqliteInitCallback(&initData, 5, azArg, 0);
drhe0bc4042002-06-25 01:09:11 +0000236 pTab = sqliteFindTable(db, MASTER_NAME);
237 if( pTab ){
238 pTab->readOnly = 1;
drhd8bc7082000-06-07 23:51:50 +0000239 }
drhe0bc4042002-06-25 01:09:11 +0000240 azArg[1] = TEMP_MASTER_NAME;
241 azArg[3] = temp_master_schema;
242 azArg[4] = "1";
drhc2311722002-07-19 17:46:38 +0000243 sqliteInitCallback(&initData, 5, azArg, 0);
drhe0bc4042002-06-25 01:09:11 +0000244 pTab = sqliteFindTable(db, TEMP_MASTER_NAME);
245 if( pTab ){
246 pTab->readOnly = 1;
247 }
248
249 /* Create a cursor to hold the database open
250 */
251 if( db->pBe==0 ) return SQLITE_OK;
252 rc = sqliteBtreeCursor(db->pBe, 2, 0, &curMain);
drh92ed08a2002-07-30 18:43:40 +0000253 if( rc ){
drh1e0ccab2002-08-29 23:59:47 +0000254 sqliteSetString(pzErrMsg, sqlite_error_string(rc), 0);
drh92ed08a2002-07-30 18:43:40 +0000255 sqliteResetInternalSchema(db);
256 return rc;
257 }
drhe0bc4042002-06-25 01:09:11 +0000258
259 /* Get the database meta information
260 */
261 rc = sqliteBtreeGetMeta(db->pBe, meta);
262 if( rc ){
drh1e0ccab2002-08-29 23:59:47 +0000263 sqliteSetString(pzErrMsg, sqlite_error_string(rc), 0);
drh92ed08a2002-07-30 18:43:40 +0000264 sqliteResetInternalSchema(db);
drhe0bc4042002-06-25 01:09:11 +0000265 sqliteBtreeCloseCursor(curMain);
266 return rc;
267 }
268 db->schema_cookie = meta[1];
269 db->next_cookie = db->schema_cookie;
270 db->file_format = meta[2];
271 size = meta[3];
272 if( size==0 ){ size = MAX_PAGES; }
273 db->cache_size = size;
274 sqliteBtreeSetCacheSize(db->pBe, size);
275
276 /*
277 ** file_format==1 Version 2.1.0.
278 ** file_format==2 Version 2.2.0. Add support for INTEGER PRIMARY KEY.
drh491791a2002-07-18 00:34:09 +0000279 ** file_format==3 Version 2.6.0. Fix empty-string index bug.
280 ** file_format==4 Version 2.7.0. Add support for separate numeric and
drhe0bc4042002-06-25 01:09:11 +0000281 ** text datatypes.
282 */
283 if( db->file_format==0 ){
drh491791a2002-07-18 00:34:09 +0000284 /* This happens if the database was initially empty */
drha9e99ae2002-08-13 23:02:57 +0000285 db->file_format = 4;
286 }else if( db->file_format>4 ){
drhe0bc4042002-06-25 01:09:11 +0000287 sqliteBtreeCloseCursor(curMain);
drhd78eeee2001-09-13 16:18:53 +0000288 sqliteSetString(pzErrMsg, "unsupported file format", 0);
drh8e5ba842002-07-18 01:27:17 +0000289 return SQLITE_ERROR;
drh28037572000-08-02 13:47:41 +0000290 }
drhaacc5432002-01-06 17:07:40 +0000291
drhe0bc4042002-06-25 01:09:11 +0000292 /* Read the schema information out of the schema tables
drhaacc5432002-01-06 17:07:40 +0000293 */
drhe0bc4042002-06-25 01:09:11 +0000294 memset(&sParse, 0, sizeof(sParse));
295 sParse.db = db;
296 sParse.pBe = db->pBe;
297 sParse.xCallback = sqliteInitCallback;
drhc2311722002-07-19 17:46:38 +0000298 sParse.pArg = (void*)&initData;
drhe0bc4042002-06-25 01:09:11 +0000299 sParse.initFlag = 1;
300 sqliteRunParser(&sParse,
301 db->file_format>=2 ? init_script : older_init_script,
302 pzErrMsg);
303 if( sqlite_malloc_failed ){
304 sqliteSetString(pzErrMsg, "out of memory", 0);
305 sParse.rc = SQLITE_NOMEM;
306 sqliteBtreeRollback(db->pBe);
307 sqliteResetInternalSchema(db);
308 }
309 if( sParse.rc==SQLITE_OK ){
drh58b95762000-06-02 01:17:37 +0000310 db->flags |= SQLITE_Initialized;
drh5e00f6c2001-09-13 13:46:56 +0000311 sqliteCommitInternalChanges(db);
drhe0bc4042002-06-25 01:09:11 +0000312 }else{
313 db->flags &= ~SQLITE_Initialized;
314 sqliteResetInternalSchema(db);
drh58b95762000-06-02 01:17:37 +0000315 }
drhe0bc4042002-06-25 01:09:11 +0000316 sqliteBtreeCloseCursor(curMain);
317 return sParse.rc;
drh58b95762000-06-02 01:17:37 +0000318}
319
320/*
drhb217a572000-08-22 13:40:18 +0000321** The version of the library
322*/
drh096c4972002-07-19 19:03:41 +0000323const char rcsid[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $";
drh3d0b5592000-08-22 13:40:51 +0000324const char sqlite_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000325
326/*
drh297ecf12001-04-05 15:57:13 +0000327** Does the library expect data to be encoded as UTF-8 or iso8859? The
328** following global constant always lets us know.
329*/
330#ifdef SQLITE_UTF8
drhfbc3eab2001-04-06 16:13:42 +0000331const char sqlite_encoding[] = "UTF-8";
drh297ecf12001-04-05 15:57:13 +0000332#else
drhfbc3eab2001-04-06 16:13:42 +0000333const char sqlite_encoding[] = "iso8859";
drh297ecf12001-04-05 15:57:13 +0000334#endif
335
336/*
drh58b95762000-06-02 01:17:37 +0000337** Open a new SQLite database. Construct an "sqlite" structure to define
338** the state of this database and return a pointer to that structure.
339**
340** An attempt is made to initialize the in-memory data structures that
341** hold the database schema. But if this fails (because the schema file
342** is locked) then that step is deferred until the first call to
343** sqlite_exec().
344*/
345sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
346 sqlite *db;
347 int rc;
348
349 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000350 db = sqliteMalloc( sizeof(sqlite) );
351 if( pzErrMsg ) *pzErrMsg = 0;
drhdaffd0e2001-04-11 14:28:42 +0000352 if( db==0 ) goto no_mem_on_open;
drhbeae3192001-09-22 18:12:08 +0000353 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
354 sqliteHashInit(&db->idxHash, SQLITE_HASH_STRING, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000355 sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0);
drh0bce8352002-02-28 00:41:10 +0000356 sqliteHashInit(&db->aFunc, SQLITE_HASH_STRING, 1);
drhc2eef3b2002-08-31 18:53:06 +0000357 sqliteHashInit(&db->aFKey, SQLITE_HASH_STRING, 1);
drh28f4b682002-06-09 10:14:18 +0000358 sqliteRegisterBuiltinFunctions(db);
drh1c928532002-01-31 15:54:21 +0000359 db->onError = OE_Default;
drh5cf8e8c2002-02-19 22:42:05 +0000360 db->priorNewRowid = 0;
drh247be432002-05-10 05:44:55 +0000361 db->magic = SQLITE_MAGIC_BUSY;
drh75897232000-05-29 14:26:00 +0000362
363 /* Open the backend database driver */
drha1b351a2001-09-14 16:42:12 +0000364 rc = sqliteBtreeOpen(zFilename, mode, MAX_PAGES, &db->pBe);
drh5e00f6c2001-09-13 13:46:56 +0000365 if( rc!=SQLITE_OK ){
366 switch( rc ){
367 default: {
drhaacc5432002-01-06 17:07:40 +0000368 sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0);
drh5e00f6c2001-09-13 13:46:56 +0000369 }
370 }
drh75897232000-05-29 14:26:00 +0000371 sqliteFree(db);
drh5edc3122001-09-13 21:53:09 +0000372 sqliteStrRealloc(pzErrMsg);
drhbe0072d2001-09-13 14:46:09 +0000373 return 0;
drh75897232000-05-29 14:26:00 +0000374 }
375
drh58b95762000-06-02 01:17:37 +0000376 /* Attempt to read the schema */
377 rc = sqliteInit(db, pzErrMsg);
drhc67980b2002-06-14 20:54:14 +0000378 db->magic = SQLITE_MAGIC_OPEN;
drhdaffd0e2001-04-11 14:28:42 +0000379 if( sqlite_malloc_failed ){
drh6d4abfb2001-10-22 02:58:08 +0000380 sqlite_close(db);
drhdaffd0e2001-04-11 14:28:42 +0000381 goto no_mem_on_open;
382 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
drh58b95762000-06-02 01:17:37 +0000383 sqlite_close(db);
drh5edc3122001-09-13 21:53:09 +0000384 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000385 return 0;
drhaacc5432002-01-06 17:07:40 +0000386 }else if( pzErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +0000387 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000388 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000389 }
drh491791a2002-07-18 00:34:09 +0000390
391 /* If the database is in formats 1 or 2, then upgrade it to
392 ** version 3. This will reconstruct all indices. If the
393 ** upgrade fails for any reason (ex: out of disk space, database
394 ** is read only, interrupt receive, etc.) then refuse to open.
395 */
drh92ed08a2002-07-30 18:43:40 +0000396 if( rc==SQLITE_OK && db->file_format<3 ){
drhc2311722002-07-19 17:46:38 +0000397 char *zErr = 0;
398 InitData initData;
drh491791a2002-07-18 00:34:09 +0000399 int meta[SQLITE_N_BTREE_META];
400
drhc2311722002-07-19 17:46:38 +0000401 initData.db = db;
402 initData.pzErrMsg = &zErr;
drh491791a2002-07-18 00:34:09 +0000403 db->file_format = 3;
404 rc = sqlite_exec(db,
405 "BEGIN; SELECT name FROM sqlite_master WHERE type='table';",
406 upgrade_3_callback,
drhc2311722002-07-19 17:46:38 +0000407 &initData,
drh491791a2002-07-18 00:34:09 +0000408 &zErr);
409 if( rc==SQLITE_OK ){
410 sqliteBtreeGetMeta(db->pBe, meta);
drha9e99ae2002-08-13 23:02:57 +0000411 meta[2] = 4;
drh491791a2002-07-18 00:34:09 +0000412 sqliteBtreeUpdateMeta(db->pBe, meta);
413 sqlite_exec(db, "COMMIT", 0, 0, 0);
414 }
415 if( rc!=SQLITE_OK ){
416 sqliteSetString(pzErrMsg,
417 "unable to upgrade database to the version 2.6 format",
418 zErr ? ": " : 0, zErr, 0);
drhc2311722002-07-19 17:46:38 +0000419 sqlite_freemem(zErr);
drh491791a2002-07-18 00:34:09 +0000420 sqliteStrRealloc(pzErrMsg);
421 sqlite_close(db);
422 return 0;
423 }
drhc2311722002-07-19 17:46:38 +0000424 sqlite_freemem(zErr);
drh491791a2002-07-18 00:34:09 +0000425 }
426
427 /* Return a pointer to the newly opened database structure */
drh75897232000-05-29 14:26:00 +0000428 return db;
drhdaffd0e2001-04-11 14:28:42 +0000429
430no_mem_on_open:
431 sqliteSetString(pzErrMsg, "out of memory", 0);
432 sqliteStrRealloc(pzErrMsg);
433 return 0;
drh75897232000-05-29 14:26:00 +0000434}
435
436/*
drhaf9ff332002-01-16 21:00:27 +0000437** Return the ROWID of the most recent insert
438*/
439int sqlite_last_insert_rowid(sqlite *db){
440 return db->lastRowid;
441}
442
443/*
drhc8d30ac2002-04-12 10:08:59 +0000444** Return the number of changes in the most recent call to sqlite_exec().
445*/
446int sqlite_changes(sqlite *db){
447 return db->nChange;
448}
449
450/*
drh50e5dad2001-09-15 00:57:28 +0000451** Close an existing SQLite database
452*/
453void sqlite_close(sqlite *db){
drh8e0a2f92002-02-23 23:45:45 +0000454 HashElem *i;
drhc22bd472002-05-10 13:14:07 +0000455 if( sqliteSafetyCheck(db) || sqliteSafetyOn(db) ){ return; }
drh247be432002-05-10 05:44:55 +0000456 db->magic = SQLITE_MAGIC_CLOSED;
drh50e5dad2001-09-15 00:57:28 +0000457 sqliteBtreeClose(db->pBe);
drhe0bc4042002-06-25 01:09:11 +0000458 sqliteResetInternalSchema(db);
drhf57b3392001-10-08 13:22:32 +0000459 if( db->pBeTemp ){
460 sqliteBtreeClose(db->pBeTemp);
461 }
drh0bce8352002-02-28 00:41:10 +0000462 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
463 FuncDef *pFunc, *pNext;
464 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
drh8e0a2f92002-02-23 23:45:45 +0000465 pNext = pFunc->pNext;
466 sqliteFree(pFunc);
467 }
468 }
drh0bce8352002-02-28 00:41:10 +0000469 sqliteHashClear(&db->aFunc);
drhc2eef3b2002-08-31 18:53:06 +0000470 sqliteHashClear(&db->aFKey);
drh75897232000-05-29 14:26:00 +0000471 sqliteFree(db);
472}
473
474/*
475** Return TRUE if the given SQL string ends in a semicolon.
drhce9079c2002-05-15 14:17:44 +0000476**
477** Special handling is require for CREATE TRIGGER statements.
478** Whenever the CREATE TRIGGER keywords are seen, the statement
479** must end with ";END;".
drh75897232000-05-29 14:26:00 +0000480*/
481int sqlite_complete(const char *zSql){
drhce9079c2002-05-15 14:17:44 +0000482 int isComplete = 1;
483 int requireEnd = 0;
484 int seenText = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000485 int seenCreate = 0;
drh8c82b352000-12-10 18:23:50 +0000486 while( *zSql ){
487 switch( *zSql ){
488 case ';': {
489 isComplete = 1;
drhce9079c2002-05-15 14:17:44 +0000490 seenText = 1;
491 seenCreate = 0;
drh75897232000-05-29 14:26:00 +0000492 break;
drh8c82b352000-12-10 18:23:50 +0000493 }
494 case ' ':
495 case '\t':
496 case '\n':
497 case '\f': {
drh75897232000-05-29 14:26:00 +0000498 break;
drh8c82b352000-12-10 18:23:50 +0000499 }
drh969fa7c2002-02-18 18:30:32 +0000500 case '[': {
501 isComplete = 0;
drhce9079c2002-05-15 14:17:44 +0000502 seenText = 1;
503 seenCreate = 0;
drh969fa7c2002-02-18 18:30:32 +0000504 zSql++;
505 while( *zSql && *zSql!=']' ){ zSql++; }
506 if( *zSql==0 ) return 0;
507 break;
508 }
drhce9079c2002-05-15 14:17:44 +0000509 case '"':
drh8c82b352000-12-10 18:23:50 +0000510 case '\'': {
drhce9079c2002-05-15 14:17:44 +0000511 int c = *zSql;
drh8c82b352000-12-10 18:23:50 +0000512 isComplete = 0;
drhce9079c2002-05-15 14:17:44 +0000513 seenText = 1;
514 seenCreate = 0;
drh8c82b352000-12-10 18:23:50 +0000515 zSql++;
drhce9079c2002-05-15 14:17:44 +0000516 while( *zSql && *zSql!=c ){ zSql++; }
drh8c82b352000-12-10 18:23:50 +0000517 if( *zSql==0 ) return 0;
518 break;
519 }
520 case '-': {
521 if( zSql[1]!='-' ){
522 isComplete = 0;
drhce9079c2002-05-15 14:17:44 +0000523 seenCreate = 0;
drh8c82b352000-12-10 18:23:50 +0000524 break;
525 }
526 while( *zSql && *zSql!='\n' ){ zSql++; }
drhce9079c2002-05-15 14:17:44 +0000527 if( *zSql==0 ) return seenText && isComplete && requireEnd==0;
528 break;
529 }
530 case 'c':
531 case 'C': {
532 seenText = 1;
533 if( !isComplete ) break;
534 isComplete = 0;
535 if( sqliteStrNICmp(zSql, "create", 6)!=0 ) break;
536 if( !isspace(zSql[6]) ) break;
537 zSql += 5;
538 seenCreate = 1;
539 while( isspace(zSql[1]) ) zSql++;
540 if( sqliteStrNICmp(&zSql[1],"trigger", 7)!=0 ) break;
541 zSql += 7;
542 requireEnd++;
543 break;
544 }
545 case 't':
546 case 'T': {
547 seenText = 1;
548 if( !seenCreate ) break;
549 seenCreate = 0;
550 isComplete = 0;
551 if( sqliteStrNICmp(zSql, "trigger", 7)!=0 ) break;
552 if( !isspace(zSql[7]) ) break;
553 zSql += 6;
554 requireEnd++;
555 break;
556 }
557 case 'e':
558 case 'E': {
559 seenCreate = 0;
560 seenText = 1;
561 if( !isComplete ) break;
562 isComplete = 0;
563 if( requireEnd==0 ) break;
564 if( sqliteStrNICmp(zSql, "end", 3)!=0 ) break;
565 zSql += 2;
566 while( isspace(zSql[1]) ) zSql++;
567 if( zSql[1]==';' ){
568 zSql++;
569 isComplete = 1;
570 requireEnd--;
571 }
drh8c82b352000-12-10 18:23:50 +0000572 break;
drh9adf9ac2002-05-15 11:44:13 +0000573 }
drh8c82b352000-12-10 18:23:50 +0000574 default: {
drhce9079c2002-05-15 14:17:44 +0000575 seenCreate = 0;
576 seenText = 1;
drh8c82b352000-12-10 18:23:50 +0000577 isComplete = 0;
578 break;
579 }
drh75897232000-05-29 14:26:00 +0000580 }
drh8c82b352000-12-10 18:23:50 +0000581 zSql++;
drh75897232000-05-29 14:26:00 +0000582 }
drhce9079c2002-05-15 14:17:44 +0000583 return seenText && isComplete && requireEnd==0;
drh75897232000-05-29 14:26:00 +0000584}
585
586/*
drhbed86902000-06-02 13:27:59 +0000587** Execute SQL code. Return one of the SQLITE_ success/failure
588** codes. Also write an error message into memory obtained from
589** malloc() and make *pzErrMsg point to that message.
590**
591** If the SQL is a query, then for each row in the query result
592** the xCallback() function is called. pArg becomes the first
593** argument to xCallback(). If xCallback=NULL then no callback
594** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000595*/
596int sqlite_exec(
597 sqlite *db, /* The database on which the SQL executes */
drh9f71c2e2001-11-03 23:57:09 +0000598 const char *zSql, /* The SQL to be executed */
drh75897232000-05-29 14:26:00 +0000599 sqlite_callback xCallback, /* Invoke this callback routine */
600 void *pArg, /* First argument to xCallback() */
601 char **pzErrMsg /* Write error messages here */
602){
603 Parse sParse;
drh75897232000-05-29 14:26:00 +0000604
605 if( pzErrMsg ) *pzErrMsg = 0;
drhc22bd472002-05-10 13:14:07 +0000606 if( sqliteSafetyOn(db) ) goto exec_misuse;
drh58b95762000-06-02 01:17:37 +0000607 if( (db->flags & SQLITE_Initialized)==0 ){
drh1e0ccab2002-08-29 23:59:47 +0000608 int rc, cnt = 1;
609 while( (rc = sqliteInit(db, pzErrMsg))==SQLITE_BUSY
610 && db->xBusyCallback && db->xBusyCallback(db->pBusyArg, "", cnt++)!=0 ){}
drhdaffd0e2001-04-11 14:28:42 +0000611 if( rc!=SQLITE_OK ){
612 sqliteStrRealloc(pzErrMsg);
drh247be432002-05-10 05:44:55 +0000613 sqliteSafetyOff(db);
drhdaffd0e2001-04-11 14:28:42 +0000614 return rc;
615 }
drh58b95762000-06-02 01:17:37 +0000616 }
drh491791a2002-07-18 00:34:09 +0000617 if( db->file_format<3 ){
618 sqliteSafetyOff(db);
619 sqliteSetString(pzErrMsg, "obsolete database file format", 0);
620 return SQLITE_ERROR;
621 }
drhc8d30ac2002-04-12 10:08:59 +0000622 if( db->recursionDepth==0 ){ db->nChange = 0; }
623 db->recursionDepth++;
drh75897232000-05-29 14:26:00 +0000624 memset(&sParse, 0, sizeof(sParse));
625 sParse.db = db;
drh5e00f6c2001-09-13 13:46:56 +0000626 sParse.pBe = db->pBe;
drh75897232000-05-29 14:26:00 +0000627 sParse.xCallback = xCallback;
628 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000629 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000630 if( sqlite_malloc_failed ){
631 sqliteSetString(pzErrMsg, "out of memory", 0);
632 sParse.rc = SQLITE_NOMEM;
drh6d4abfb2001-10-22 02:58:08 +0000633 sqliteBtreeRollback(db->pBe);
634 if( db->pBeTemp ) sqliteBtreeRollback(db->pBeTemp);
635 db->flags &= ~SQLITE_InTrans;
drhe0bc4042002-06-25 01:09:11 +0000636 sqliteResetInternalSchema(db);
drhdaffd0e2001-04-11 14:28:42 +0000637 }
638 sqliteStrRealloc(pzErrMsg);
drh50e5dad2001-09-15 00:57:28 +0000639 if( sParse.rc==SQLITE_SCHEMA ){
drhe0bc4042002-06-25 01:09:11 +0000640 sqliteResetInternalSchema(db);
drh50e5dad2001-09-15 00:57:28 +0000641 }
drhc8d30ac2002-04-12 10:08:59 +0000642 db->recursionDepth--;
drhc22bd472002-05-10 13:14:07 +0000643 if( sqliteSafetyOff(db) ) goto exec_misuse;
drh4c504392000-10-16 22:06:40 +0000644 return sParse.rc;
drhc22bd472002-05-10 13:14:07 +0000645
646exec_misuse:
647 if( pzErrMsg ){
648 *pzErrMsg = 0;
649 sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), 0);
650 sqliteStrRealloc(pzErrMsg);
651 }
652 return SQLITE_MISUSE;
drh75897232000-05-29 14:26:00 +0000653}
drh2dfbbca2000-07-28 14:32:48 +0000654
655/*
drhc22bd472002-05-10 13:14:07 +0000656** Return a static string that describes the kind of error specified in the
657** argument.
drh247be432002-05-10 05:44:55 +0000658*/
drhc22bd472002-05-10 13:14:07 +0000659const char *sqlite_error_string(int rc){
660 const char *z;
661 switch( rc ){
662 case SQLITE_OK: z = "not an error"; break;
663 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
664 case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break;
665 case SQLITE_PERM: z = "access permission denied"; break;
666 case SQLITE_ABORT: z = "callback requested query abort"; break;
667 case SQLITE_BUSY: z = "database is locked"; break;
668 case SQLITE_LOCKED: z = "database table is locked"; break;
669 case SQLITE_NOMEM: z = "out of memory"; break;
670 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
671 case SQLITE_INTERRUPT: z = "interrupted"; break;
672 case SQLITE_IOERR: z = "disk I/O error"; break;
673 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
674 case SQLITE_NOTFOUND: z = "table or record not found"; break;
675 case SQLITE_FULL: z = "database is full"; break;
676 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
677 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
678 case SQLITE_EMPTY: z = "table contains no data"; break;
679 case SQLITE_SCHEMA: z = "database schema has changed"; break;
680 case SQLITE_TOOBIG: z = "too much data for one table row"; break;
681 case SQLITE_CONSTRAINT: z = "constraint failed"; break;
682 case SQLITE_MISMATCH: z = "datatype mismatch"; break;
683 case SQLITE_MISUSE: z = "library routine called out of sequence";break;
684 default: z = "unknown error"; break;
drh247be432002-05-10 05:44:55 +0000685 }
drhc22bd472002-05-10 13:14:07 +0000686 return z;
drh247be432002-05-10 05:44:55 +0000687}
688
689/*
drh2dfbbca2000-07-28 14:32:48 +0000690** This routine implements a busy callback that sleeps and tries
691** again until a timeout value is reached. The timeout value is
692** an integer number of milliseconds passed in as the first
693** argument.
694*/
drhdaffd0e2001-04-11 14:28:42 +0000695static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000696 void *Timeout, /* Maximum amount of time to wait */
697 const char *NotUsed, /* The name of the table that is busy */
698 int count /* Number of times table has been busy */
699){
drh8cfbf082001-09-19 13:22:39 +0000700#if SQLITE_MIN_SLEEP_MS==1
701 int delay = 10;
drh2dfbbca2000-07-28 14:32:48 +0000702 int prior_delay = 0;
703 int timeout = (int)Timeout;
704 int i;
705
706 for(i=1; i<count; i++){
707 prior_delay += delay;
708 delay = delay*2;
drh8cfbf082001-09-19 13:22:39 +0000709 if( delay>=1000 ){
710 delay = 1000;
711 prior_delay += 1000*(count - i - 1);
drh2dfbbca2000-07-28 14:32:48 +0000712 break;
713 }
714 }
drh3109e022001-10-09 13:46:01 +0000715 if( prior_delay + delay > timeout ){
716 delay = timeout - prior_delay;
drh2dfbbca2000-07-28 14:32:48 +0000717 if( delay<=0 ) return 0;
718 }
drh8cfbf082001-09-19 13:22:39 +0000719 sqliteOsSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000720 return 1;
721#else
722 int timeout = (int)Timeout;
723 if( (count+1)*1000 > timeout ){
724 return 0;
725 }
drh8cfbf082001-09-19 13:22:39 +0000726 sqliteOsSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000727 return 1;
728#endif
729}
730
731/*
732** This routine sets the busy callback for an Sqlite database to the
733** given callback function with the given argument.
734*/
735void sqlite_busy_handler(
736 sqlite *db,
737 int (*xBusy)(void*,const char*,int),
738 void *pArg
739){
740 db->xBusyCallback = xBusy;
741 db->pBusyArg = pArg;
742}
743
744/*
745** This routine installs a default busy handler that waits for the
746** specified number of milliseconds before returning 0.
747*/
748void sqlite_busy_timeout(sqlite *db, int ms){
749 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000750 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000751 }else{
752 sqlite_busy_handler(db, 0, 0);
753 }
754}
drh4c504392000-10-16 22:06:40 +0000755
756/*
757** Cause any pending operation to stop at its earliest opportunity.
758*/
759void sqlite_interrupt(sqlite *db){
760 db->flags |= SQLITE_Interrupt;
761}
drhfa86c412002-02-02 15:01:15 +0000762
763/*
764** Windows systems should call this routine to free memory that
765** is returned in the in the errmsg parameter of sqlite_open() when
766** SQLite is a DLL. For some reason, it does not work to call free()
767** directly.
768**
769** Note that we need to call free() not sqliteFree() here, since every
770** string that is exported from SQLite should have already passed through
771** sqliteStrRealloc().
772*/
773void sqlite_freemem(void *p){ free(p); }
774
775/*
776** Windows systems need functions to call to return the sqlite_version
777** and sqlite_encoding strings.
778*/
779const char *sqlite_libversion(void){ return sqlite_version; }
780const char *sqlite_libencoding(void){ return sqlite_encoding; }
drh8e0a2f92002-02-23 23:45:45 +0000781
782/*
783** Create new user-defined functions. The sqlite_create_function()
784** routine creates a regular function and sqlite_create_aggregate()
785** creates an aggregate function.
786**
787** Passing a NULL xFunc argument or NULL xStep and xFinalize arguments
788** disables the function. Calling sqlite_create_function() with the
789** same name and number of arguments as a prior call to
790** sqlite_create_aggregate() disables the prior call to
791** sqlite_create_aggregate(), and vice versa.
792**
793** If nArg is -1 it means that this function will accept any number
794** of arguments, including 0.
795*/
796int sqlite_create_function(
797 sqlite *db, /* Add the function to this database connection */
798 const char *zName, /* Name of the function to add */
799 int nArg, /* Number of arguments */
drh1350b032002-02-27 19:00:20 +0000800 void (*xFunc)(sqlite_func*,int,const char**), /* The implementation */
801 void *pUserData /* User data */
drh8e0a2f92002-02-23 23:45:45 +0000802){
drh0bce8352002-02-28 00:41:10 +0000803 FuncDef *p;
drh4b59ab52002-08-24 18:24:51 +0000804 int nName;
drhc22bd472002-05-10 13:14:07 +0000805 if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1;
drh4b59ab52002-08-24 18:24:51 +0000806 nName = strlen(zName);
807 if( nName>255 ) return 1;
808 p = sqliteFindFunction(db, zName, nName, nArg, 1);
drh4e0f9952002-02-27 01:53:13 +0000809 if( p==0 ) return 1;
drh8e0a2f92002-02-23 23:45:45 +0000810 p->xFunc = xFunc;
811 p->xStep = 0;
812 p->xFinalize = 0;
drh1350b032002-02-27 19:00:20 +0000813 p->pUserData = pUserData;
drh8e0a2f92002-02-23 23:45:45 +0000814 return 0;
815}
816int sqlite_create_aggregate(
817 sqlite *db, /* Add the function to this database connection */
818 const char *zName, /* Name of the function to add */
819 int nArg, /* Number of arguments */
drh1350b032002-02-27 19:00:20 +0000820 void (*xStep)(sqlite_func*,int,const char**), /* The step function */
821 void (*xFinalize)(sqlite_func*), /* The finalizer */
822 void *pUserData /* User data */
drh8e0a2f92002-02-23 23:45:45 +0000823){
drh0bce8352002-02-28 00:41:10 +0000824 FuncDef *p;
drh4b59ab52002-08-24 18:24:51 +0000825 int nName;
drhc22bd472002-05-10 13:14:07 +0000826 if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1;
drh4b59ab52002-08-24 18:24:51 +0000827 nName = strlen(zName);
828 if( nName>255 ) return 1;
829 p = sqliteFindFunction(db, zName, nName, nArg, 1);
drh4e0f9952002-02-27 01:53:13 +0000830 if( p==0 ) return 1;
drh8e0a2f92002-02-23 23:45:45 +0000831 p->xFunc = 0;
832 p->xStep = xStep;
833 p->xFinalize = xFinalize;
drh1350b032002-02-27 19:00:20 +0000834 p->pUserData = pUserData;
drh8e0a2f92002-02-23 23:45:45 +0000835 return 0;
836}
drhc9b84a12002-06-20 11:36:48 +0000837
838/*
drh411995d2002-06-25 19:31:18 +0000839** Change the datatype for all functions with a given name. See the
840** header comment for the prototype of this function in sqlite.h for
841** additional information.
drhc9b84a12002-06-20 11:36:48 +0000842*/
843int sqlite_function_type(sqlite *db, const char *zName, int dataType){
844 FuncDef *p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, strlen(zName));
845 while( p ){
846 p->dataType = dataType;
847 p = p->pNext;
848 }
drhf46f9052002-06-22 02:33:38 +0000849 return SQLITE_OK;
drhc9b84a12002-06-20 11:36:48 +0000850}
drh411995d2002-06-25 19:31:18 +0000851
852/*
853** Attempt to open the file named in the argument as the auxiliary database
854** file. The auxiliary database file is used to store TEMP tables. But
855** by using this API, it is possible to trick SQLite into opening two
856** separate databases and acting on them as if they were one.
857**
858** This routine closes the existing auxiliary database file, which will
drh76800322002-08-13 20:45:39 +0000859** cause any previously created TEMP tables to be dropped.
drh411995d2002-06-25 19:31:18 +0000860**
861** The zName parameter can be a NULL pointer or an empty string to cause
862** a temporary file to be opened and automatically deleted when closed.
863*/
864int sqlite_open_aux_file(sqlite *db, const char *zName, char **pzErrMsg){
865 int rc;
866 if( zName && zName[0]==0 ) zName = 0;
867 if( sqliteSafetyOn(db) ) goto openaux_misuse;
868 sqliteResetInternalSchema(db);
869 if( db->pBeTemp!=0 ){
870 sqliteBtreeClose(db->pBeTemp);
871 }
872 rc = sqliteBtreeOpen(zName, 0, MAX_PAGES, &db->pBeTemp);
873 if( rc ){
874 if( zName==0 ) zName = "a temporary file";
875 sqliteSetString(pzErrMsg, "unable to open ", zName,
876 ": ", sqlite_error_string(rc), 0);
877 sqliteStrRealloc(pzErrMsg);
878 sqliteSafetyOff(db);
879 return rc;
880 }
881 rc = sqliteInit(db, pzErrMsg);
882 if( sqliteSafetyOff(db) ) goto openaux_misuse;
883 sqliteStrRealloc(pzErrMsg);
884 return rc;
885
886openaux_misuse:
887 sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), 0);
888 sqliteStrRealloc(pzErrMsg);
889 return SQLITE_MISUSE;
890}