blob: 5012dafc764d1fbece2af3aa265a344635c084be [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**
drh8e5ba842002-07-18 01:27:17 +000017** $Id: main.c,v 1.88 2002/07/18 01:27:18 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
drhe0bc4042002-06-25 01:09:11 +000033** argv[4] = "1" for temporary files, "0" for main database
drhd78eeee2001-09-13 16:18:53 +000034**
drh75897232000-05-29 14:26:00 +000035*/
drhe0140fc2002-06-16 18:21:44 +000036int sqliteInitCallback(void *pDb, int argc, char **argv, char **azColName){
drh75897232000-05-29 14:26:00 +000037 sqlite *db = (sqlite*)pDb;
38 Parse sParse;
drhd78eeee2001-09-13 16:18:53 +000039 int nErr = 0;
drh75897232000-05-29 14:26:00 +000040
drh382c0242001-10-06 16:33:02 +000041 /* TODO: Do some validity checks on all fields. In particular,
42 ** make sure fields do not contain NULLs. Otherwise we might core
43 ** when attempting to initialize from a corrupt database file. */
drhe3c41372001-09-17 20:25:58 +000044
drhe0bc4042002-06-25 01:09:11 +000045 assert( argc==5 );
drhd78eeee2001-09-13 16:18:53 +000046 switch( argv[0][0] ){
drh17f71932002-02-21 12:01:27 +000047 case 'v':
drhd78eeee2001-09-13 16:18:53 +000048 case 'i':
drh17f71932002-02-21 12:01:27 +000049 case 't': { /* CREATE TABLE, CREATE INDEX, or CREATE VIEW statements */
drhadbca9c2001-09-27 15:11:53 +000050 if( argv[3] && argv[3][0] ){
drh17f71932002-02-21 12:01:27 +000051 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
drh382c0242001-10-06 16:33:02 +000052 ** But because sParse.initFlag is set to 1, no VDBE code is generated
53 ** or executed. All the parser does is build the internal data
drh17f71932002-02-21 12:01:27 +000054 ** structures that describe the table, index, or view.
drh382c0242001-10-06 16:33:02 +000055 */
drhadbca9c2001-09-27 15:11:53 +000056 memset(&sParse, 0, sizeof(sParse));
57 sParse.db = db;
58 sParse.initFlag = 1;
drhe0bc4042002-06-25 01:09:11 +000059 sParse.isTemp = argv[4][0] - '0';
drhadbca9c2001-09-27 15:11:53 +000060 sParse.newTnum = atoi(argv[2]);
drhf5bf0a72001-11-23 00:24:12 +000061 sqliteRunParser(&sParse, argv[3], 0);
drhadbca9c2001-09-27 15:11:53 +000062 }else{
drh382c0242001-10-06 16:33:02 +000063 /* If the SQL column is blank it means this is an index that
64 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
drhaacc5432002-01-06 17:07:40 +000065 ** constraint for a CREATE TABLE. The index should have already
drh382c0242001-10-06 16:33:02 +000066 ** been created when we processed the CREATE TABLE. All we have
drhaacc5432002-01-06 17:07:40 +000067 ** to do here is record the root page number for that index.
drh382c0242001-10-06 16:33:02 +000068 */
drhadbca9c2001-09-27 15:11:53 +000069 Index *pIndex = sqliteFindIndex(db, argv[1]);
70 if( pIndex==0 || pIndex->tnum!=0 ){
drhda9e0342002-01-10 14:31:48 +000071 /* This can occur if there exists an index on a TEMP table which
72 ** has the same name as another index on a permanent index. Since
73 ** the permanent table is hidden by the TEMP table, we can also
74 ** safely ignore the index on the permanent table.
75 */
76 /* Do Nothing */;
drhadbca9c2001-09-27 15:11:53 +000077 }else{
78 pIndex->tnum = atoi(argv[2]);
79 }
80 }
drhd78eeee2001-09-13 16:18:53 +000081 break;
82 }
83 default: {
84 /* This can not happen! */
85 nErr = 1;
86 assert( nErr==0 );
87 }
drh28037572000-08-02 13:47:41 +000088 }
drh75897232000-05-29 14:26:00 +000089 return nErr;
90}
91
92/*
drh491791a2002-07-18 00:34:09 +000093** This is a callback procedure used to reconstruct a table. The
94** name of the table to be reconstructed is passed in as argv[0].
95**
96** This routine is used to automatically upgrade a database from
97** format version 1 or 2 to version 3. The correct operation of
98** this routine relys on the fact that no indices are used when
99** copying a table out to a temporary file.
100*/
101static int
102upgrade_3_callback(void *pDb, int argc, char **argv, char **NotUsed){
103 sqlite *db = (sqlite*)pDb;
104 int rc;
drh8e5ba842002-07-18 01:27:17 +0000105 Table *pTab;
106 Trigger *pTrig;
drh491791a2002-07-18 00:34:09 +0000107
drh8e5ba842002-07-18 01:27:17 +0000108 pTab = sqliteFindTable(db, argv[0]);
109 if( pTab ){
110 pTrig = pTab->pTrigger;
111 pTab->pTrigger = 0; /* Disable all triggers before rebuilding the table */
112 }
drh491791a2002-07-18 00:34:09 +0000113 rc = sqlite_exec_printf(db,
114 "CREATE TEMP TABLE sqlite_x AS SELECT * FROM '%q'; "
115 "DELETE FROM '%q'; "
116 "INSERT INTO '%q' SELECT * FROM sqlite_x; "
117 "DROP TABLE sqlite_x;",
118 0, 0, 0, argv[0], argv[0], argv[0]);
drh8e5ba842002-07-18 01:27:17 +0000119 if( pTab ) pTab->pTrigger = pTrig; /* Re-enable triggers */
drh491791a2002-07-18 00:34:09 +0000120 return rc!=SQLITE_OK;
121}
122
123
124
125/*
drh58b95762000-06-02 01:17:37 +0000126** Attempt to read the database schema and initialize internal
127** data structures. Return one of the SQLITE_ error codes to
128** indicate success or failure.
drhbed86902000-06-02 13:27:59 +0000129**
130** After the database is initialized, the SQLITE_Initialized
131** bit is set in the flags field of the sqlite structure. An
132** attempt is made to initialize the database as soon as it
133** is opened. If that fails (perhaps because another process
134** has the sqlite_master table locked) than another attempt
135** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +0000136*/
drhe0bc4042002-06-25 01:09:11 +0000137int sqliteInit(sqlite *db, char **pzErrMsg){
drh58b95762000-06-02 01:17:37 +0000138 int rc;
drhe0bc4042002-06-25 01:09:11 +0000139 BtCursor *curMain;
140 int size;
141 Table *pTab;
142 char *azArg[6];
143 int meta[SQLITE_N_BTREE_META];
144 Parse sParse;
drh58b95762000-06-02 01:17:37 +0000145
146 /*
147 ** The master database table has a structure like this
148 */
drh75897232000-05-29 14:26:00 +0000149 static char master_schema[] =
drhe0bc4042002-06-25 01:09:11 +0000150 "CREATE TABLE sqlite_master(\n"
151 " type text,\n"
152 " name text,\n"
153 " tbl_name text,\n"
154 " rootpage integer,\n"
155 " sql text\n"
156 ")"
157 ;
158 static char temp_master_schema[] =
159 "CREATE TEMP TABLE sqlite_temp_master(\n"
drh75897232000-05-29 14:26:00 +0000160 " type text,\n"
161 " name text,\n"
162 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000163 " rootpage integer,\n"
drh75897232000-05-29 14:26:00 +0000164 " sql text\n"
165 ")"
166 ;
167
drhe0bc4042002-06-25 01:09:11 +0000168 /* The following SQL will read the schema from the master tables.
169 ** The first version works with SQLite file formats 2 or greater.
170 ** The second version is for format 1 files.
drh75897232000-05-29 14:26:00 +0000171 **
drhe0bc4042002-06-25 01:09:11 +0000172 ** Beginning with file format 2, the rowid for new table entries
173 ** (including entries in sqlite_master) is an increasing integer.
174 ** So for file format 2 and later, we can play back sqlite_master
175 ** and all the CREATE statements will appear in the right order.
176 ** But with file format 1, table entries were random and so we
177 ** have to make sure the CREATE TABLEs occur before their corresponding
178 ** CREATE INDEXs. (We don't have to deal with CREATE VIEW or
179 ** CREATE TRIGGER in file format 1 because those constructs did
180 ** not exist then.)
drh75897232000-05-29 14:26:00 +0000181 */
drhe0bc4042002-06-25 01:09:11 +0000182 static char init_script[] =
183 "SELECT type, name, rootpage, sql, 1 FROM sqlite_temp_master "
184 "UNION ALL "
185 "SELECT type, name, rootpage, sql, 0 FROM sqlite_master";
186 static char older_init_script[] =
187 "SELECT type, name, rootpage, sql, 1 FROM sqlite_temp_master "
188 "UNION ALL "
189 "SELECT type, name, rootpage, sql, 0 FROM sqlite_master "
190 "WHERE type='table' "
191 "UNION ALL "
192 "SELECT type, name, rootpage, sql, 0 FROM sqlite_master "
193 "WHERE type='index'";
drh17f71932002-02-21 12:01:27 +0000194
drh603240c2002-03-05 01:11:12 +0000195
drhe0bc4042002-06-25 01:09:11 +0000196 /* Construct the schema tables: sqlite_master and sqlite_temp_master
drh58b95762000-06-02 01:17:37 +0000197 */
drhe0bc4042002-06-25 01:09:11 +0000198 azArg[0] = "table";
199 azArg[1] = MASTER_NAME;
200 azArg[2] = "2";
201 azArg[3] = master_schema;
202 azArg[4] = "0";
203 azArg[5] = 0;
204 sqliteInitCallback(db, 5, azArg, 0);
205 pTab = sqliteFindTable(db, MASTER_NAME);
206 if( pTab ){
207 pTab->readOnly = 1;
drhd8bc7082000-06-07 23:51:50 +0000208 }
drhe0bc4042002-06-25 01:09:11 +0000209 azArg[1] = TEMP_MASTER_NAME;
210 azArg[3] = temp_master_schema;
211 azArg[4] = "1";
212 sqliteInitCallback(db, 5, azArg, 0);
213 pTab = sqliteFindTable(db, TEMP_MASTER_NAME);
214 if( pTab ){
215 pTab->readOnly = 1;
216 }
217
218 /* Create a cursor to hold the database open
219 */
220 if( db->pBe==0 ) return SQLITE_OK;
221 rc = sqliteBtreeCursor(db->pBe, 2, 0, &curMain);
222 if( rc ) return rc;
223
224 /* Get the database meta information
225 */
226 rc = sqliteBtreeGetMeta(db->pBe, meta);
227 if( rc ){
228 sqliteBtreeCloseCursor(curMain);
229 return rc;
230 }
231 db->schema_cookie = meta[1];
232 db->next_cookie = db->schema_cookie;
233 db->file_format = meta[2];
234 size = meta[3];
235 if( size==0 ){ size = MAX_PAGES; }
236 db->cache_size = size;
237 sqliteBtreeSetCacheSize(db->pBe, size);
238
239 /*
240 ** file_format==1 Version 2.1.0.
241 ** file_format==2 Version 2.2.0. Add support for INTEGER PRIMARY KEY.
drh491791a2002-07-18 00:34:09 +0000242 ** file_format==3 Version 2.6.0. Fix empty-string index bug.
243 ** file_format==4 Version 2.7.0. Add support for separate numeric and
drhe0bc4042002-06-25 01:09:11 +0000244 ** text datatypes.
245 */
246 if( db->file_format==0 ){
drh491791a2002-07-18 00:34:09 +0000247 /* This happens if the database was initially empty */
248 db->file_format = 3;
249 }else if( db->file_format>3 ){
drhe0bc4042002-06-25 01:09:11 +0000250 sqliteBtreeCloseCursor(curMain);
drhd78eeee2001-09-13 16:18:53 +0000251 sqliteSetString(pzErrMsg, "unsupported file format", 0);
drh8e5ba842002-07-18 01:27:17 +0000252 return SQLITE_ERROR;
drh28037572000-08-02 13:47:41 +0000253 }
drhaacc5432002-01-06 17:07:40 +0000254
drhe0bc4042002-06-25 01:09:11 +0000255 /* Read the schema information out of the schema tables
drhaacc5432002-01-06 17:07:40 +0000256 */
drhe0bc4042002-06-25 01:09:11 +0000257 memset(&sParse, 0, sizeof(sParse));
258 sParse.db = db;
259 sParse.pBe = db->pBe;
260 sParse.xCallback = sqliteInitCallback;
261 sParse.pArg = (void*)db;
262 sParse.initFlag = 1;
263 sqliteRunParser(&sParse,
264 db->file_format>=2 ? init_script : older_init_script,
265 pzErrMsg);
266 if( sqlite_malloc_failed ){
267 sqliteSetString(pzErrMsg, "out of memory", 0);
268 sParse.rc = SQLITE_NOMEM;
269 sqliteBtreeRollback(db->pBe);
270 sqliteResetInternalSchema(db);
271 }
272 if( sParse.rc==SQLITE_OK ){
drh58b95762000-06-02 01:17:37 +0000273 db->flags |= SQLITE_Initialized;
drh5e00f6c2001-09-13 13:46:56 +0000274 sqliteCommitInternalChanges(db);
drhe0bc4042002-06-25 01:09:11 +0000275 }else{
276 db->flags &= ~SQLITE_Initialized;
277 sqliteResetInternalSchema(db);
drh58b95762000-06-02 01:17:37 +0000278 }
drhe0bc4042002-06-25 01:09:11 +0000279 sqliteBtreeCloseCursor(curMain);
280 return sParse.rc;
drh58b95762000-06-02 01:17:37 +0000281}
282
283/*
drhb217a572000-08-22 13:40:18 +0000284** The version of the library
285*/
drh3d0b5592000-08-22 13:40:51 +0000286const char sqlite_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000287
288/*
drh297ecf12001-04-05 15:57:13 +0000289** Does the library expect data to be encoded as UTF-8 or iso8859? The
290** following global constant always lets us know.
291*/
292#ifdef SQLITE_UTF8
drhfbc3eab2001-04-06 16:13:42 +0000293const char sqlite_encoding[] = "UTF-8";
drh297ecf12001-04-05 15:57:13 +0000294#else
drhfbc3eab2001-04-06 16:13:42 +0000295const char sqlite_encoding[] = "iso8859";
drh297ecf12001-04-05 15:57:13 +0000296#endif
297
298/*
drh58b95762000-06-02 01:17:37 +0000299** Open a new SQLite database. Construct an "sqlite" structure to define
300** the state of this database and return a pointer to that structure.
301**
302** An attempt is made to initialize the in-memory data structures that
303** hold the database schema. But if this fails (because the schema file
304** is locked) then that step is deferred until the first call to
305** sqlite_exec().
306*/
307sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
308 sqlite *db;
309 int rc;
310
311 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000312 db = sqliteMalloc( sizeof(sqlite) );
313 if( pzErrMsg ) *pzErrMsg = 0;
drhdaffd0e2001-04-11 14:28:42 +0000314 if( db==0 ) goto no_mem_on_open;
drhbeae3192001-09-22 18:12:08 +0000315 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
316 sqliteHashInit(&db->idxHash, SQLITE_HASH_STRING, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000317 sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0);
drh0bce8352002-02-28 00:41:10 +0000318 sqliteHashInit(&db->aFunc, SQLITE_HASH_STRING, 1);
drh28f4b682002-06-09 10:14:18 +0000319 sqliteRegisterBuiltinFunctions(db);
drh1c928532002-01-31 15:54:21 +0000320 db->onError = OE_Default;
drh5cf8e8c2002-02-19 22:42:05 +0000321 db->priorNewRowid = 0;
drh247be432002-05-10 05:44:55 +0000322 db->magic = SQLITE_MAGIC_BUSY;
drh75897232000-05-29 14:26:00 +0000323
324 /* Open the backend database driver */
drha1b351a2001-09-14 16:42:12 +0000325 rc = sqliteBtreeOpen(zFilename, mode, MAX_PAGES, &db->pBe);
drh5e00f6c2001-09-13 13:46:56 +0000326 if( rc!=SQLITE_OK ){
327 switch( rc ){
328 default: {
drhaacc5432002-01-06 17:07:40 +0000329 sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0);
drh5e00f6c2001-09-13 13:46:56 +0000330 }
331 }
drh75897232000-05-29 14:26:00 +0000332 sqliteFree(db);
drh5edc3122001-09-13 21:53:09 +0000333 sqliteStrRealloc(pzErrMsg);
drhbe0072d2001-09-13 14:46:09 +0000334 return 0;
drh75897232000-05-29 14:26:00 +0000335 }
336
drh58b95762000-06-02 01:17:37 +0000337 /* Attempt to read the schema */
338 rc = sqliteInit(db, pzErrMsg);
drhc67980b2002-06-14 20:54:14 +0000339 db->magic = SQLITE_MAGIC_OPEN;
drhdaffd0e2001-04-11 14:28:42 +0000340 if( sqlite_malloc_failed ){
drh6d4abfb2001-10-22 02:58:08 +0000341 sqlite_close(db);
drhdaffd0e2001-04-11 14:28:42 +0000342 goto no_mem_on_open;
343 }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
drh58b95762000-06-02 01:17:37 +0000344 sqlite_close(db);
drh5edc3122001-09-13 21:53:09 +0000345 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000346 return 0;
drhaacc5432002-01-06 17:07:40 +0000347 }else if( pzErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +0000348 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000349 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000350 }
drh491791a2002-07-18 00:34:09 +0000351
352 /* If the database is in formats 1 or 2, then upgrade it to
353 ** version 3. This will reconstruct all indices. If the
354 ** upgrade fails for any reason (ex: out of disk space, database
355 ** is read only, interrupt receive, etc.) then refuse to open.
356 */
357 if( db->file_format<3 ){
358 char *zErr;
359 int meta[SQLITE_N_BTREE_META];
360
361 db->file_format = 3;
362 rc = sqlite_exec(db,
363 "BEGIN; SELECT name FROM sqlite_master WHERE type='table';",
364 upgrade_3_callback,
365 db,
366 &zErr);
367 if( rc==SQLITE_OK ){
368 sqliteBtreeGetMeta(db->pBe, meta);
369 meta[2] = 3;
370 sqliteBtreeUpdateMeta(db->pBe, meta);
371 sqlite_exec(db, "COMMIT", 0, 0, 0);
372 }
373 if( rc!=SQLITE_OK ){
374 sqliteSetString(pzErrMsg,
375 "unable to upgrade database to the version 2.6 format",
376 zErr ? ": " : 0, zErr, 0);
377 sqliteFree(zErr);
378 sqliteStrRealloc(pzErrMsg);
379 sqlite_close(db);
380 return 0;
381 }
382 }
383
384 /* Return a pointer to the newly opened database structure */
drh75897232000-05-29 14:26:00 +0000385 return db;
drhdaffd0e2001-04-11 14:28:42 +0000386
387no_mem_on_open:
388 sqliteSetString(pzErrMsg, "out of memory", 0);
389 sqliteStrRealloc(pzErrMsg);
390 return 0;
drh75897232000-05-29 14:26:00 +0000391}
392
393/*
drhaf9ff332002-01-16 21:00:27 +0000394** Return the ROWID of the most recent insert
395*/
396int sqlite_last_insert_rowid(sqlite *db){
397 return db->lastRowid;
398}
399
400/*
drhc8d30ac2002-04-12 10:08:59 +0000401** Return the number of changes in the most recent call to sqlite_exec().
402*/
403int sqlite_changes(sqlite *db){
404 return db->nChange;
405}
406
407/*
drh50e5dad2001-09-15 00:57:28 +0000408** Close an existing SQLite database
409*/
410void sqlite_close(sqlite *db){
drh8e0a2f92002-02-23 23:45:45 +0000411 HashElem *i;
drhc22bd472002-05-10 13:14:07 +0000412 if( sqliteSafetyCheck(db) || sqliteSafetyOn(db) ){ return; }
drh247be432002-05-10 05:44:55 +0000413 db->magic = SQLITE_MAGIC_CLOSED;
drh50e5dad2001-09-15 00:57:28 +0000414 sqliteBtreeClose(db->pBe);
drhe0bc4042002-06-25 01:09:11 +0000415 sqliteResetInternalSchema(db);
drhf57b3392001-10-08 13:22:32 +0000416 if( db->pBeTemp ){
417 sqliteBtreeClose(db->pBeTemp);
418 }
drh0bce8352002-02-28 00:41:10 +0000419 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
420 FuncDef *pFunc, *pNext;
421 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
drh8e0a2f92002-02-23 23:45:45 +0000422 pNext = pFunc->pNext;
423 sqliteFree(pFunc);
424 }
425 }
drh0bce8352002-02-28 00:41:10 +0000426 sqliteHashClear(&db->aFunc);
drh75897232000-05-29 14:26:00 +0000427 sqliteFree(db);
428}
429
430/*
431** Return TRUE if the given SQL string ends in a semicolon.
drhce9079c2002-05-15 14:17:44 +0000432**
433** Special handling is require for CREATE TRIGGER statements.
434** Whenever the CREATE TRIGGER keywords are seen, the statement
435** must end with ";END;".
drh75897232000-05-29 14:26:00 +0000436*/
437int sqlite_complete(const char *zSql){
drhce9079c2002-05-15 14:17:44 +0000438 int isComplete = 1;
439 int requireEnd = 0;
440 int seenText = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000441 int seenCreate = 0;
drh8c82b352000-12-10 18:23:50 +0000442 while( *zSql ){
443 switch( *zSql ){
444 case ';': {
445 isComplete = 1;
drhce9079c2002-05-15 14:17:44 +0000446 seenText = 1;
447 seenCreate = 0;
drh75897232000-05-29 14:26:00 +0000448 break;
drh8c82b352000-12-10 18:23:50 +0000449 }
450 case ' ':
451 case '\t':
452 case '\n':
453 case '\f': {
drh75897232000-05-29 14:26:00 +0000454 break;
drh8c82b352000-12-10 18:23:50 +0000455 }
drh969fa7c2002-02-18 18:30:32 +0000456 case '[': {
457 isComplete = 0;
drhce9079c2002-05-15 14:17:44 +0000458 seenText = 1;
459 seenCreate = 0;
drh969fa7c2002-02-18 18:30:32 +0000460 zSql++;
461 while( *zSql && *zSql!=']' ){ zSql++; }
462 if( *zSql==0 ) return 0;
463 break;
464 }
drhce9079c2002-05-15 14:17:44 +0000465 case '"':
drh8c82b352000-12-10 18:23:50 +0000466 case '\'': {
drhce9079c2002-05-15 14:17:44 +0000467 int c = *zSql;
drh8c82b352000-12-10 18:23:50 +0000468 isComplete = 0;
drhce9079c2002-05-15 14:17:44 +0000469 seenText = 1;
470 seenCreate = 0;
drh8c82b352000-12-10 18:23:50 +0000471 zSql++;
drhce9079c2002-05-15 14:17:44 +0000472 while( *zSql && *zSql!=c ){ zSql++; }
drh8c82b352000-12-10 18:23:50 +0000473 if( *zSql==0 ) return 0;
474 break;
475 }
476 case '-': {
477 if( zSql[1]!='-' ){
478 isComplete = 0;
drhce9079c2002-05-15 14:17:44 +0000479 seenCreate = 0;
drh8c82b352000-12-10 18:23:50 +0000480 break;
481 }
482 while( *zSql && *zSql!='\n' ){ zSql++; }
drhce9079c2002-05-15 14:17:44 +0000483 if( *zSql==0 ) return seenText && isComplete && requireEnd==0;
484 break;
485 }
486 case 'c':
487 case 'C': {
488 seenText = 1;
489 if( !isComplete ) break;
490 isComplete = 0;
491 if( sqliteStrNICmp(zSql, "create", 6)!=0 ) break;
492 if( !isspace(zSql[6]) ) break;
493 zSql += 5;
494 seenCreate = 1;
495 while( isspace(zSql[1]) ) zSql++;
496 if( sqliteStrNICmp(&zSql[1],"trigger", 7)!=0 ) break;
497 zSql += 7;
498 requireEnd++;
499 break;
500 }
501 case 't':
502 case 'T': {
503 seenText = 1;
504 if( !seenCreate ) break;
505 seenCreate = 0;
506 isComplete = 0;
507 if( sqliteStrNICmp(zSql, "trigger", 7)!=0 ) break;
508 if( !isspace(zSql[7]) ) break;
509 zSql += 6;
510 requireEnd++;
511 break;
512 }
513 case 'e':
514 case 'E': {
515 seenCreate = 0;
516 seenText = 1;
517 if( !isComplete ) break;
518 isComplete = 0;
519 if( requireEnd==0 ) break;
520 if( sqliteStrNICmp(zSql, "end", 3)!=0 ) break;
521 zSql += 2;
522 while( isspace(zSql[1]) ) zSql++;
523 if( zSql[1]==';' ){
524 zSql++;
525 isComplete = 1;
526 requireEnd--;
527 }
drh8c82b352000-12-10 18:23:50 +0000528 break;
drh9adf9ac2002-05-15 11:44:13 +0000529 }
drh8c82b352000-12-10 18:23:50 +0000530 default: {
drhce9079c2002-05-15 14:17:44 +0000531 seenCreate = 0;
532 seenText = 1;
drh8c82b352000-12-10 18:23:50 +0000533 isComplete = 0;
534 break;
535 }
drh75897232000-05-29 14:26:00 +0000536 }
drh8c82b352000-12-10 18:23:50 +0000537 zSql++;
drh75897232000-05-29 14:26:00 +0000538 }
drhce9079c2002-05-15 14:17:44 +0000539 return seenText && isComplete && requireEnd==0;
drh75897232000-05-29 14:26:00 +0000540}
541
542/*
drhbed86902000-06-02 13:27:59 +0000543** Execute SQL code. Return one of the SQLITE_ success/failure
544** codes. Also write an error message into memory obtained from
545** malloc() and make *pzErrMsg point to that message.
546**
547** If the SQL is a query, then for each row in the query result
548** the xCallback() function is called. pArg becomes the first
549** argument to xCallback(). If xCallback=NULL then no callback
550** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000551*/
552int sqlite_exec(
553 sqlite *db, /* The database on which the SQL executes */
drh9f71c2e2001-11-03 23:57:09 +0000554 const char *zSql, /* The SQL to be executed */
drh75897232000-05-29 14:26:00 +0000555 sqlite_callback xCallback, /* Invoke this callback routine */
556 void *pArg, /* First argument to xCallback() */
557 char **pzErrMsg /* Write error messages here */
558){
559 Parse sParse;
drh75897232000-05-29 14:26:00 +0000560
561 if( pzErrMsg ) *pzErrMsg = 0;
drhc22bd472002-05-10 13:14:07 +0000562 if( sqliteSafetyOn(db) ) goto exec_misuse;
drh70562cd2002-07-13 17:23:21 +0000563 if( (db->flags & SQLITE_InTrans)!=0 && db->pid!=sqliteOsProcessId() ){
564 goto exec_misuse;
565 }
drh58b95762000-06-02 01:17:37 +0000566 if( (db->flags & SQLITE_Initialized)==0 ){
567 int rc = sqliteInit(db, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000568 if( rc!=SQLITE_OK ){
569 sqliteStrRealloc(pzErrMsg);
drh247be432002-05-10 05:44:55 +0000570 sqliteSafetyOff(db);
drhdaffd0e2001-04-11 14:28:42 +0000571 return rc;
572 }
drh58b95762000-06-02 01:17:37 +0000573 }
drh491791a2002-07-18 00:34:09 +0000574 if( db->file_format<3 ){
575 sqliteSafetyOff(db);
576 sqliteSetString(pzErrMsg, "obsolete database file format", 0);
577 return SQLITE_ERROR;
578 }
drhc8d30ac2002-04-12 10:08:59 +0000579 if( db->recursionDepth==0 ){ db->nChange = 0; }
580 db->recursionDepth++;
drh75897232000-05-29 14:26:00 +0000581 memset(&sParse, 0, sizeof(sParse));
582 sParse.db = db;
drh5e00f6c2001-09-13 13:46:56 +0000583 sParse.pBe = db->pBe;
drh75897232000-05-29 14:26:00 +0000584 sParse.xCallback = xCallback;
585 sParse.pArg = pArg;
drh4c504392000-10-16 22:06:40 +0000586 sqliteRunParser(&sParse, zSql, pzErrMsg);
drhdaffd0e2001-04-11 14:28:42 +0000587 if( sqlite_malloc_failed ){
588 sqliteSetString(pzErrMsg, "out of memory", 0);
589 sParse.rc = SQLITE_NOMEM;
drh6d4abfb2001-10-22 02:58:08 +0000590 sqliteBtreeRollback(db->pBe);
591 if( db->pBeTemp ) sqliteBtreeRollback(db->pBeTemp);
592 db->flags &= ~SQLITE_InTrans;
drhe0bc4042002-06-25 01:09:11 +0000593 sqliteResetInternalSchema(db);
drhdaffd0e2001-04-11 14:28:42 +0000594 }
595 sqliteStrRealloc(pzErrMsg);
drh50e5dad2001-09-15 00:57:28 +0000596 if( sParse.rc==SQLITE_SCHEMA ){
drhe0bc4042002-06-25 01:09:11 +0000597 sqliteResetInternalSchema(db);
drh50e5dad2001-09-15 00:57:28 +0000598 }
drhc8d30ac2002-04-12 10:08:59 +0000599 db->recursionDepth--;
drhc22bd472002-05-10 13:14:07 +0000600 if( sqliteSafetyOff(db) ) goto exec_misuse;
drh4c504392000-10-16 22:06:40 +0000601 return sParse.rc;
drhc22bd472002-05-10 13:14:07 +0000602
603exec_misuse:
604 if( pzErrMsg ){
605 *pzErrMsg = 0;
606 sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), 0);
607 sqliteStrRealloc(pzErrMsg);
608 }
609 return SQLITE_MISUSE;
drh75897232000-05-29 14:26:00 +0000610}
drh2dfbbca2000-07-28 14:32:48 +0000611
612/*
drhc22bd472002-05-10 13:14:07 +0000613** Return a static string that describes the kind of error specified in the
614** argument.
drh247be432002-05-10 05:44:55 +0000615*/
drhc22bd472002-05-10 13:14:07 +0000616const char *sqlite_error_string(int rc){
617 const char *z;
618 switch( rc ){
619 case SQLITE_OK: z = "not an error"; break;
620 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
621 case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break;
622 case SQLITE_PERM: z = "access permission denied"; break;
623 case SQLITE_ABORT: z = "callback requested query abort"; break;
624 case SQLITE_BUSY: z = "database is locked"; break;
625 case SQLITE_LOCKED: z = "database table is locked"; break;
626 case SQLITE_NOMEM: z = "out of memory"; break;
627 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
628 case SQLITE_INTERRUPT: z = "interrupted"; break;
629 case SQLITE_IOERR: z = "disk I/O error"; break;
630 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
631 case SQLITE_NOTFOUND: z = "table or record not found"; break;
632 case SQLITE_FULL: z = "database is full"; break;
633 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
634 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
635 case SQLITE_EMPTY: z = "table contains no data"; break;
636 case SQLITE_SCHEMA: z = "database schema has changed"; break;
637 case SQLITE_TOOBIG: z = "too much data for one table row"; break;
638 case SQLITE_CONSTRAINT: z = "constraint failed"; break;
639 case SQLITE_MISMATCH: z = "datatype mismatch"; break;
640 case SQLITE_MISUSE: z = "library routine called out of sequence";break;
641 default: z = "unknown error"; break;
drh247be432002-05-10 05:44:55 +0000642 }
drhc22bd472002-05-10 13:14:07 +0000643 return z;
drh247be432002-05-10 05:44:55 +0000644}
645
646/*
drh2dfbbca2000-07-28 14:32:48 +0000647** This routine implements a busy callback that sleeps and tries
648** again until a timeout value is reached. The timeout value is
649** an integer number of milliseconds passed in as the first
650** argument.
651*/
drhdaffd0e2001-04-11 14:28:42 +0000652static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000653 void *Timeout, /* Maximum amount of time to wait */
654 const char *NotUsed, /* The name of the table that is busy */
655 int count /* Number of times table has been busy */
656){
drh8cfbf082001-09-19 13:22:39 +0000657#if SQLITE_MIN_SLEEP_MS==1
658 int delay = 10;
drh2dfbbca2000-07-28 14:32:48 +0000659 int prior_delay = 0;
660 int timeout = (int)Timeout;
661 int i;
662
663 for(i=1; i<count; i++){
664 prior_delay += delay;
665 delay = delay*2;
drh8cfbf082001-09-19 13:22:39 +0000666 if( delay>=1000 ){
667 delay = 1000;
668 prior_delay += 1000*(count - i - 1);
drh2dfbbca2000-07-28 14:32:48 +0000669 break;
670 }
671 }
drh3109e022001-10-09 13:46:01 +0000672 if( prior_delay + delay > timeout ){
673 delay = timeout - prior_delay;
drh2dfbbca2000-07-28 14:32:48 +0000674 if( delay<=0 ) return 0;
675 }
drh8cfbf082001-09-19 13:22:39 +0000676 sqliteOsSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000677 return 1;
678#else
679 int timeout = (int)Timeout;
680 if( (count+1)*1000 > timeout ){
681 return 0;
682 }
drh8cfbf082001-09-19 13:22:39 +0000683 sqliteOsSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000684 return 1;
685#endif
686}
687
688/*
689** This routine sets the busy callback for an Sqlite database to the
690** given callback function with the given argument.
691*/
692void sqlite_busy_handler(
693 sqlite *db,
694 int (*xBusy)(void*,const char*,int),
695 void *pArg
696){
697 db->xBusyCallback = xBusy;
698 db->pBusyArg = pArg;
699}
700
701/*
702** This routine installs a default busy handler that waits for the
703** specified number of milliseconds before returning 0.
704*/
705void sqlite_busy_timeout(sqlite *db, int ms){
706 if( ms>0 ){
drhdaffd0e2001-04-11 14:28:42 +0000707 sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000708 }else{
709 sqlite_busy_handler(db, 0, 0);
710 }
711}
drh4c504392000-10-16 22:06:40 +0000712
713/*
714** Cause any pending operation to stop at its earliest opportunity.
715*/
716void sqlite_interrupt(sqlite *db){
717 db->flags |= SQLITE_Interrupt;
718}
drhfa86c412002-02-02 15:01:15 +0000719
720/*
721** Windows systems should call this routine to free memory that
722** is returned in the in the errmsg parameter of sqlite_open() when
723** SQLite is a DLL. For some reason, it does not work to call free()
724** directly.
725**
726** Note that we need to call free() not sqliteFree() here, since every
727** string that is exported from SQLite should have already passed through
728** sqliteStrRealloc().
729*/
730void sqlite_freemem(void *p){ free(p); }
731
732/*
733** Windows systems need functions to call to return the sqlite_version
734** and sqlite_encoding strings.
735*/
736const char *sqlite_libversion(void){ return sqlite_version; }
737const char *sqlite_libencoding(void){ return sqlite_encoding; }
drh8e0a2f92002-02-23 23:45:45 +0000738
739/*
740** Create new user-defined functions. The sqlite_create_function()
741** routine creates a regular function and sqlite_create_aggregate()
742** creates an aggregate function.
743**
744** Passing a NULL xFunc argument or NULL xStep and xFinalize arguments
745** disables the function. Calling sqlite_create_function() with the
746** same name and number of arguments as a prior call to
747** sqlite_create_aggregate() disables the prior call to
748** sqlite_create_aggregate(), and vice versa.
749**
750** If nArg is -1 it means that this function will accept any number
751** of arguments, including 0.
752*/
753int sqlite_create_function(
754 sqlite *db, /* Add the function to this database connection */
755 const char *zName, /* Name of the function to add */
756 int nArg, /* Number of arguments */
drh1350b032002-02-27 19:00:20 +0000757 void (*xFunc)(sqlite_func*,int,const char**), /* The implementation */
758 void *pUserData /* User data */
drh8e0a2f92002-02-23 23:45:45 +0000759){
drh0bce8352002-02-28 00:41:10 +0000760 FuncDef *p;
drhc22bd472002-05-10 13:14:07 +0000761 if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1;
drh0bce8352002-02-28 00:41:10 +0000762 p = sqliteFindFunction(db, zName, strlen(zName), nArg, 1);
drh4e0f9952002-02-27 01:53:13 +0000763 if( p==0 ) return 1;
drh8e0a2f92002-02-23 23:45:45 +0000764 p->xFunc = xFunc;
765 p->xStep = 0;
766 p->xFinalize = 0;
drh1350b032002-02-27 19:00:20 +0000767 p->pUserData = pUserData;
drh8e0a2f92002-02-23 23:45:45 +0000768 return 0;
769}
770int sqlite_create_aggregate(
771 sqlite *db, /* Add the function to this database connection */
772 const char *zName, /* Name of the function to add */
773 int nArg, /* Number of arguments */
drh1350b032002-02-27 19:00:20 +0000774 void (*xStep)(sqlite_func*,int,const char**), /* The step function */
775 void (*xFinalize)(sqlite_func*), /* The finalizer */
776 void *pUserData /* User data */
drh8e0a2f92002-02-23 23:45:45 +0000777){
drh0bce8352002-02-28 00:41:10 +0000778 FuncDef *p;
drhc22bd472002-05-10 13:14:07 +0000779 if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1;
drh0bce8352002-02-28 00:41:10 +0000780 p = sqliteFindFunction(db, zName, strlen(zName), nArg, 1);
drh4e0f9952002-02-27 01:53:13 +0000781 if( p==0 ) return 1;
drh8e0a2f92002-02-23 23:45:45 +0000782 p->xFunc = 0;
783 p->xStep = xStep;
784 p->xFinalize = xFinalize;
drh1350b032002-02-27 19:00:20 +0000785 p->pUserData = pUserData;
drh8e0a2f92002-02-23 23:45:45 +0000786 return 0;
787}
drhc9b84a12002-06-20 11:36:48 +0000788
789/*
drh411995d2002-06-25 19:31:18 +0000790** Change the datatype for all functions with a given name. See the
791** header comment for the prototype of this function in sqlite.h for
792** additional information.
drhc9b84a12002-06-20 11:36:48 +0000793*/
794int sqlite_function_type(sqlite *db, const char *zName, int dataType){
795 FuncDef *p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, strlen(zName));
796 while( p ){
797 p->dataType = dataType;
798 p = p->pNext;
799 }
drhf46f9052002-06-22 02:33:38 +0000800 return SQLITE_OK;
drhc9b84a12002-06-20 11:36:48 +0000801}
drh411995d2002-06-25 19:31:18 +0000802
803/*
804** Attempt to open the file named in the argument as the auxiliary database
805** file. The auxiliary database file is used to store TEMP tables. But
806** by using this API, it is possible to trick SQLite into opening two
807** separate databases and acting on them as if they were one.
808**
809** This routine closes the existing auxiliary database file, which will
810** cause any previously created TEMP tables to be created.
811**
812** The zName parameter can be a NULL pointer or an empty string to cause
813** a temporary file to be opened and automatically deleted when closed.
814*/
815int sqlite_open_aux_file(sqlite *db, const char *zName, char **pzErrMsg){
816 int rc;
817 if( zName && zName[0]==0 ) zName = 0;
818 if( sqliteSafetyOn(db) ) goto openaux_misuse;
819 sqliteResetInternalSchema(db);
820 if( db->pBeTemp!=0 ){
821 sqliteBtreeClose(db->pBeTemp);
822 }
823 rc = sqliteBtreeOpen(zName, 0, MAX_PAGES, &db->pBeTemp);
824 if( rc ){
825 if( zName==0 ) zName = "a temporary file";
826 sqliteSetString(pzErrMsg, "unable to open ", zName,
827 ": ", sqlite_error_string(rc), 0);
828 sqliteStrRealloc(pzErrMsg);
829 sqliteSafetyOff(db);
830 return rc;
831 }
832 rc = sqliteInit(db, pzErrMsg);
833 if( sqliteSafetyOff(db) ) goto openaux_misuse;
834 sqliteStrRealloc(pzErrMsg);
835 return rc;
836
837openaux_misuse:
838 sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), 0);
839 sqliteStrRealloc(pzErrMsg);
840 return SQLITE_MISUSE;
841}