2000-05-29 18:26:00 +04:00
|
|
|
/*
|
2001-09-16 04:13:26 +04:00
|
|
|
** 2001 September 15
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
2001-09-16 04:13:26 +04:00
|
|
|
** The author disclaims copyright to this source code. In place of
|
|
|
|
** a legal notice, here is a blessing:
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
2001-09-16 04:13:26 +04:00
|
|
|
** May you do good and not evil.
|
|
|
|
** May you find forgiveness for yourself and forgive others.
|
|
|
|
** May you share freely, never taking more than you give.
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
|
|
|
*************************************************************************
|
|
|
|
** Main file for the SQLite library. The routines in this file
|
|
|
|
** implement the programmer interface to the library. Routines in
|
|
|
|
** other files are for internal use by SQLite and should not be
|
|
|
|
** accessed by users of the library.
|
|
|
|
**
|
2002-04-12 14:08:59 +04:00
|
|
|
** $Id: main.c,v 1.69 2002/04/12 10:08:59 drh Exp $
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
|
|
|
#include "sqliteInt.h"
|
2001-09-19 17:22:39 +04:00
|
|
|
#include "os.h"
|
2000-05-29 18:26:00 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** This is the callback routine for the code that initializes the
|
2001-10-06 20:33:02 +04:00
|
|
|
** database. See sqliteInit() below for additional information.
|
|
|
|
**
|
|
|
|
** Each callback contains the following information:
|
2001-09-13 20:18:53 +04:00
|
|
|
**
|
2001-12-21 17:30:42 +03:00
|
|
|
** argv[0] = "file-format" or "schema-cookie" or "table" or "index"
|
2001-09-18 00:25:58 +04:00
|
|
|
** argv[1] = table or index name or meta statement type.
|
|
|
|
** argv[2] = root page number for table or index. NULL for meta.
|
2001-09-27 19:11:53 +04:00
|
|
|
** argv[3] = SQL create statement for the table or index
|
2000-08-02 17:47:41 +04:00
|
|
|
**
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
|
|
|
static int sqliteOpenCb(void *pDb, int argc, char **argv, char **azColName){
|
|
|
|
sqlite *db = (sqlite*)pDb;
|
|
|
|
Parse sParse;
|
2001-09-13 20:18:53 +04:00
|
|
|
int nErr = 0;
|
2000-05-29 18:26:00 +04:00
|
|
|
|
2001-10-06 20:33:02 +04:00
|
|
|
/* TODO: Do some validity checks on all fields. In particular,
|
|
|
|
** make sure fields do not contain NULLs. Otherwise we might core
|
|
|
|
** when attempting to initialize from a corrupt database file. */
|
2001-09-18 00:25:58 +04:00
|
|
|
|
2001-09-27 19:11:53 +04:00
|
|
|
assert( argc==4 );
|
2001-09-13 20:18:53 +04:00
|
|
|
switch( argv[0][0] ){
|
2002-03-05 04:11:12 +03:00
|
|
|
case 'c': { /* Recommended pager cache size */
|
|
|
|
int size = atoi(argv[3]);
|
2002-03-07 01:01:34 +03:00
|
|
|
if( size==0 ){ size = MAX_PAGES; }
|
|
|
|
db->cache_size = size;
|
|
|
|
sqliteBtreeSetCacheSize(db->pBe, size);
|
2002-03-05 04:11:12 +03:00
|
|
|
break;
|
|
|
|
}
|
2001-12-21 17:30:42 +03:00
|
|
|
case 'f': { /* File format */
|
|
|
|
db->file_format = atoi(argv[3]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 's': { /* Schema cookie */
|
|
|
|
db->schema_cookie = atoi(argv[3]);
|
|
|
|
db->next_cookie = db->schema_cookie;
|
2001-09-13 20:18:53 +04:00
|
|
|
break;
|
|
|
|
}
|
2002-02-21 15:01:27 +03:00
|
|
|
case 'v':
|
2001-09-13 20:18:53 +04:00
|
|
|
case 'i':
|
2002-02-21 15:01:27 +03:00
|
|
|
case 't': { /* CREATE TABLE, CREATE INDEX, or CREATE VIEW statements */
|
2001-09-27 19:11:53 +04:00
|
|
|
if( argv[3] && argv[3][0] ){
|
2002-02-21 15:01:27 +03:00
|
|
|
/* Call the parser to process a CREATE TABLE, INDEX or VIEW.
|
2001-10-06 20:33:02 +04:00
|
|
|
** But because sParse.initFlag is set to 1, no VDBE code is generated
|
|
|
|
** or executed. All the parser does is build the internal data
|
2002-02-21 15:01:27 +03:00
|
|
|
** structures that describe the table, index, or view.
|
2001-10-06 20:33:02 +04:00
|
|
|
*/
|
2001-09-27 19:11:53 +04:00
|
|
|
memset(&sParse, 0, sizeof(sParse));
|
|
|
|
sParse.db = db;
|
|
|
|
sParse.initFlag = 1;
|
|
|
|
sParse.newTnum = atoi(argv[2]);
|
2001-11-23 03:24:12 +03:00
|
|
|
sqliteRunParser(&sParse, argv[3], 0);
|
2001-09-27 19:11:53 +04:00
|
|
|
}else{
|
2001-10-06 20:33:02 +04:00
|
|
|
/* If the SQL column is blank it means this is an index that
|
|
|
|
** was created to be the PRIMARY KEY or to fulfill a UNIQUE
|
2002-01-06 20:07:40 +03:00
|
|
|
** constraint for a CREATE TABLE. The index should have already
|
2001-10-06 20:33:02 +04:00
|
|
|
** been created when we processed the CREATE TABLE. All we have
|
2002-01-06 20:07:40 +03:00
|
|
|
** to do here is record the root page number for that index.
|
2001-10-06 20:33:02 +04:00
|
|
|
*/
|
2001-09-27 19:11:53 +04:00
|
|
|
Index *pIndex = sqliteFindIndex(db, argv[1]);
|
|
|
|
if( pIndex==0 || pIndex->tnum!=0 ){
|
2002-01-10 17:31:48 +03:00
|
|
|
/* This can occur if there exists an index on a TEMP table which
|
|
|
|
** has the same name as another index on a permanent index. Since
|
|
|
|
** the permanent table is hidden by the TEMP table, we can also
|
|
|
|
** safely ignore the index on the permanent table.
|
|
|
|
*/
|
|
|
|
/* Do Nothing */;
|
2001-09-27 19:11:53 +04:00
|
|
|
}else{
|
|
|
|
pIndex->tnum = atoi(argv[2]);
|
|
|
|
}
|
|
|
|
}
|
2001-09-13 20:18:53 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
/* This can not happen! */
|
|
|
|
nErr = 1;
|
|
|
|
assert( nErr==0 );
|
2000-08-02 17:47:41 +04:00
|
|
|
}
|
|
|
|
}
|
2000-05-29 18:26:00 +04:00
|
|
|
return nErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-06-02 05:17:37 +04:00
|
|
|
** Attempt to read the database schema and initialize internal
|
|
|
|
** data structures. Return one of the SQLITE_ error codes to
|
|
|
|
** indicate success or failure.
|
2000-06-02 17:27:59 +04:00
|
|
|
**
|
|
|
|
** After the database is initialized, the SQLITE_Initialized
|
|
|
|
** bit is set in the flags field of the sqlite structure. An
|
|
|
|
** attempt is made to initialize the database as soon as it
|
|
|
|
** is opened. If that fails (perhaps because another process
|
|
|
|
** has the sqlite_master table locked) than another attempt
|
|
|
|
** is made the first time the database is accessed.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2000-06-02 05:17:37 +04:00
|
|
|
static int sqliteInit(sqlite *db, char **pzErrMsg){
|
2000-05-29 18:26:00 +04:00
|
|
|
Vdbe *vdbe;
|
2000-06-02 05:17:37 +04:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The master database table has a structure like this
|
|
|
|
*/
|
2000-05-29 18:26:00 +04:00
|
|
|
static char master_schema[] =
|
|
|
|
"CREATE TABLE " MASTER_NAME " (\n"
|
|
|
|
" type text,\n"
|
|
|
|
" name text,\n"
|
|
|
|
" tbl_name text,\n"
|
2001-09-27 19:11:53 +04:00
|
|
|
" rootpage integer,\n"
|
2000-05-29 18:26:00 +04:00
|
|
|
" sql text\n"
|
|
|
|
")"
|
|
|
|
;
|
|
|
|
|
2002-01-06 20:07:40 +03:00
|
|
|
/* The following VDBE program is used to initialize the internal
|
2000-05-29 18:26:00 +04:00
|
|
|
** structure holding the tables and indexes of the database.
|
|
|
|
** The database contains a special table named "sqlite_master"
|
|
|
|
** defined as follows:
|
|
|
|
**
|
|
|
|
** CREATE TABLE sqlite_master (
|
2000-08-02 17:47:41 +04:00
|
|
|
** type text, -- Either "table" or "index" or "meta"
|
2000-05-29 18:26:00 +04:00
|
|
|
** name text, -- Name of table or index
|
|
|
|
** tbl_name text, -- Associated table
|
2001-09-27 19:11:53 +04:00
|
|
|
** rootpage integer, -- The integer page number of root page
|
2000-05-29 18:26:00 +04:00
|
|
|
** sql text -- The CREATE statement for this object
|
|
|
|
** );
|
|
|
|
**
|
|
|
|
** The sqlite_master table contains a single entry for each table
|
2000-06-21 17:59:10 +04:00
|
|
|
** and each index. The "type" column tells whether the entry is
|
|
|
|
** a table or index. The "name" column is the name of the object.
|
2000-05-29 18:26:00 +04:00
|
|
|
** The "tbl_name" is the name of the associated table. For tables,
|
2000-06-21 17:59:10 +04:00
|
|
|
** the tbl_name column is always the same as name. For indices, the
|
|
|
|
** tbl_name column contains the name of the table that the index
|
2001-10-06 20:33:02 +04:00
|
|
|
** indexes. The "rootpage" column holds the number of the root page
|
|
|
|
** for the b-tree for the table or index. Finally, the "sql" column
|
|
|
|
** contains the complete text of the CREATE TABLE or CREATE INDEX
|
|
|
|
** statement that originally created the table or index. If an index
|
|
|
|
** was created to fulfill a PRIMARY KEY or UNIQUE constraint on a table,
|
|
|
|
** then the "sql" column is NULL.
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
2002-02-21 15:01:27 +03:00
|
|
|
** In format 1, entries in the sqlite_master table are in a random
|
|
|
|
** order. Two passes must be made through the table to initialize
|
|
|
|
** internal data structures. The first pass reads table definitions
|
|
|
|
** and the second pass read index definitions. Having two passes
|
|
|
|
** insures that indices appear after their tables.
|
|
|
|
**
|
|
|
|
** In format 2, entries appear in chronological order. Only a single
|
|
|
|
** pass needs to be made through the table since everything will be
|
|
|
|
** in the write order. VIEWs may only occur in format 2.
|
2000-08-02 17:47:41 +04:00
|
|
|
**
|
2000-05-29 18:26:00 +04:00
|
|
|
** The following program invokes its callback on the SQL for each
|
|
|
|
** table then goes back and invokes the callback on the
|
|
|
|
** SQL for each index. The callback will invoke the
|
|
|
|
** parser to build the internal representation of the
|
|
|
|
** database scheme.
|
|
|
|
*/
|
|
|
|
static VdbeOp initProg[] = {
|
2002-02-21 15:01:27 +03:00
|
|
|
/* Send the file format to the callback routine
|
|
|
|
*/
|
2001-12-21 17:30:42 +03:00
|
|
|
{ OP_Open, 0, 2, 0},
|
|
|
|
{ OP_String, 0, 0, "file-format"},
|
|
|
|
{ OP_String, 0, 0, 0},
|
|
|
|
{ OP_String, 0, 0, 0},
|
|
|
|
{ OP_ReadCookie, 0, 1, 0},
|
|
|
|
{ OP_Callback, 4, 0, 0},
|
2002-02-21 15:01:27 +03:00
|
|
|
|
2002-03-05 04:11:12 +03:00
|
|
|
/* Send the recommended pager cache size to the callback routine
|
|
|
|
*/
|
|
|
|
{ OP_String, 0, 0, "cache-size"},
|
|
|
|
{ OP_String, 0, 0, 0},
|
|
|
|
{ OP_String, 0, 0, 0},
|
|
|
|
{ OP_ReadCookie, 0, 2, 0},
|
|
|
|
{ OP_Callback, 4, 0, 0},
|
|
|
|
|
2002-02-21 15:01:27 +03:00
|
|
|
/* Send the initial schema cookie to the callback
|
|
|
|
*/
|
2001-12-21 17:30:42 +03:00
|
|
|
{ OP_String, 0, 0, "schema_cookie"},
|
|
|
|
{ OP_String, 0, 0, 0},
|
|
|
|
{ OP_String, 0, 0, 0},
|
|
|
|
{ OP_ReadCookie, 0, 0, 0},
|
|
|
|
{ OP_Callback, 4, 0, 0},
|
2002-02-21 15:01:27 +03:00
|
|
|
|
|
|
|
/* Check the file format. If the format number is 2 or more,
|
|
|
|
** then do a single pass through the SQLITE_MASTER table. For
|
|
|
|
** a format number of less than 2, jump forward to a different
|
|
|
|
** algorithm that makes two passes through the SQLITE_MASTER table,
|
|
|
|
** once for tables and a second time for indices.
|
|
|
|
*/
|
|
|
|
{ OP_ReadCookie, 0, 1, 0},
|
|
|
|
{ OP_Integer, 2, 0, 0},
|
2002-03-05 04:11:12 +03:00
|
|
|
{ OP_Lt, 0, 28, 0},
|
2002-02-21 15:01:27 +03:00
|
|
|
|
|
|
|
/* This is the code for doing a single scan through the SQLITE_MASTER
|
|
|
|
** table. This code runs for format 2 and greater.
|
|
|
|
*/
|
2002-03-05 04:11:12 +03:00
|
|
|
{ OP_Rewind, 0, 26, 0},
|
|
|
|
{ OP_Column, 0, 0, 0}, /* 20 */
|
2002-02-21 15:01:27 +03:00
|
|
|
{ OP_Column, 0, 1, 0},
|
|
|
|
{ OP_Column, 0, 3, 0},
|
|
|
|
{ OP_Column, 0, 4, 0},
|
|
|
|
{ OP_Callback, 4, 0, 0},
|
2002-03-05 04:11:12 +03:00
|
|
|
{ OP_Next, 0, 20, 0},
|
|
|
|
{ OP_Close, 0, 0, 0}, /* 26 */
|
2002-02-21 15:01:27 +03:00
|
|
|
{ OP_Halt, 0, 0, 0},
|
|
|
|
|
|
|
|
/* This is the code for doing two passes through SQLITE_MASTER. This
|
|
|
|
** code runs for file format 1.
|
|
|
|
*/
|
2002-03-05 04:11:12 +03:00
|
|
|
{ OP_Rewind, 0, 48, 0}, /* 28 */
|
|
|
|
{ OP_Column, 0, 0, 0}, /* 29 */
|
2001-12-21 17:30:42 +03:00
|
|
|
{ OP_String, 0, 0, "table"},
|
2002-03-05 04:11:12 +03:00
|
|
|
{ OP_Ne, 0, 37, 0},
|
2001-12-21 17:30:42 +03:00
|
|
|
{ OP_Column, 0, 0, 0},
|
|
|
|
{ OP_Column, 0, 1, 0},
|
|
|
|
{ OP_Column, 0, 3, 0},
|
|
|
|
{ OP_Column, 0, 4, 0},
|
|
|
|
{ OP_Callback, 4, 0, 0},
|
2002-03-05 04:11:12 +03:00
|
|
|
{ OP_Next, 0, 29, 0}, /* 37 */
|
|
|
|
{ OP_Rewind, 0, 48, 0}, /* 38 */
|
|
|
|
{ OP_Column, 0, 0, 0}, /* 39 */
|
2001-12-21 17:30:42 +03:00
|
|
|
{ OP_String, 0, 0, "index"},
|
2002-03-05 04:11:12 +03:00
|
|
|
{ OP_Ne, 0, 47, 0},
|
2001-12-21 17:30:42 +03:00
|
|
|
{ OP_Column, 0, 0, 0},
|
|
|
|
{ OP_Column, 0, 1, 0},
|
|
|
|
{ OP_Column, 0, 3, 0},
|
|
|
|
{ OP_Column, 0, 4, 0},
|
|
|
|
{ OP_Callback, 4, 0, 0},
|
2002-03-05 04:11:12 +03:00
|
|
|
{ OP_Next, 0, 39, 0}, /* 47 */
|
|
|
|
{ OP_Close, 0, 0, 0}, /* 48 */
|
2001-12-21 17:30:42 +03:00
|
|
|
{ OP_Halt, 0, 0, 0},
|
2000-05-29 18:26:00 +04:00
|
|
|
};
|
|
|
|
|
2000-06-02 05:17:37 +04:00
|
|
|
/* Create a virtual machine to run the initialization program. Run
|
2001-10-06 20:33:02 +04:00
|
|
|
** the program. Then delete the virtual machine.
|
2000-06-02 05:17:37 +04:00
|
|
|
*/
|
2000-10-17 02:06:40 +04:00
|
|
|
vdbe = sqliteVdbeCreate(db);
|
2000-06-08 03:51:50 +04:00
|
|
|
if( vdbe==0 ){
|
2001-10-22 06:58:08 +04:00
|
|
|
sqliteSetString(pzErrMsg, "out of memory", 0);
|
2001-04-11 18:28:42 +04:00
|
|
|
return SQLITE_NOMEM;
|
2000-06-08 03:51:50 +04:00
|
|
|
}
|
2000-06-02 05:17:37 +04:00
|
|
|
sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
|
2000-07-28 18:32:48 +04:00
|
|
|
rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg,
|
|
|
|
db->pBusyArg, db->xBusyCallback);
|
2000-06-02 05:17:37 +04:00
|
|
|
sqliteVdbeDelete(vdbe);
|
2001-12-21 17:30:42 +03:00
|
|
|
if( rc==SQLITE_OK && db->nTable==0 ){
|
2002-02-21 15:01:27 +03:00
|
|
|
db->file_format = 2;
|
2001-12-21 17:30:42 +03:00
|
|
|
}
|
2002-02-21 15:01:27 +03:00
|
|
|
if( rc==SQLITE_OK && db->file_format>2 ){
|
2001-09-13 20:18:53 +04:00
|
|
|
sqliteSetString(pzErrMsg, "unsupported file format", 0);
|
2000-08-02 17:47:41 +04:00
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
}
|
2002-01-06 20:07:40 +03:00
|
|
|
|
|
|
|
/* The schema for the SQLITE_MASTER table is not stored in the
|
|
|
|
** database itself. We have to invoke the callback one extra
|
|
|
|
** time to get it to process the SQLITE_MASTER table defintion.
|
|
|
|
*/
|
2000-06-02 05:17:37 +04:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
Table *pTab;
|
2001-09-18 00:25:58 +04:00
|
|
|
char *azArg[6];
|
2001-09-13 20:18:53 +04:00
|
|
|
azArg[0] = "table";
|
|
|
|
azArg[1] = MASTER_NAME;
|
|
|
|
azArg[2] = "2";
|
2001-09-27 19:11:53 +04:00
|
|
|
azArg[3] = master_schema;
|
|
|
|
azArg[4] = 0;
|
|
|
|
sqliteOpenCb(db, 4, azArg, 0);
|
2000-06-02 05:17:37 +04:00
|
|
|
pTab = sqliteFindTable(db, MASTER_NAME);
|
|
|
|
if( pTab ){
|
|
|
|
pTab->readOnly = 1;
|
|
|
|
}
|
|
|
|
db->flags |= SQLITE_Initialized;
|
2001-09-13 17:46:56 +04:00
|
|
|
sqliteCommitInternalChanges(db);
|
2000-06-02 05:17:37 +04:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2000-08-22 17:40:18 +04:00
|
|
|
/*
|
|
|
|
** The version of the library
|
|
|
|
*/
|
2000-08-22 17:40:51 +04:00
|
|
|
const char sqlite_version[] = SQLITE_VERSION;
|
2000-08-22 17:40:18 +04:00
|
|
|
|
2001-04-05 19:57:13 +04:00
|
|
|
/*
|
|
|
|
** Does the library expect data to be encoded as UTF-8 or iso8859? The
|
|
|
|
** following global constant always lets us know.
|
|
|
|
*/
|
|
|
|
#ifdef SQLITE_UTF8
|
2001-04-06 20:13:42 +04:00
|
|
|
const char sqlite_encoding[] = "UTF-8";
|
2001-04-05 19:57:13 +04:00
|
|
|
#else
|
2001-04-06 20:13:42 +04:00
|
|
|
const char sqlite_encoding[] = "iso8859";
|
2001-04-05 19:57:13 +04:00
|
|
|
#endif
|
|
|
|
|
2000-06-02 05:17:37 +04:00
|
|
|
/*
|
|
|
|
** Open a new SQLite database. Construct an "sqlite" structure to define
|
|
|
|
** the state of this database and return a pointer to that structure.
|
|
|
|
**
|
|
|
|
** An attempt is made to initialize the in-memory data structures that
|
|
|
|
** hold the database schema. But if this fails (because the schema file
|
|
|
|
** is locked) then that step is deferred until the first call to
|
|
|
|
** sqlite_exec().
|
|
|
|
*/
|
|
|
|
sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
|
|
|
|
sqlite *db;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Allocate the sqlite data structure */
|
2000-05-29 18:26:00 +04:00
|
|
|
db = sqliteMalloc( sizeof(sqlite) );
|
|
|
|
if( pzErrMsg ) *pzErrMsg = 0;
|
2001-04-11 18:28:42 +04:00
|
|
|
if( db==0 ) goto no_mem_on_open;
|
2001-09-22 22:12:08 +04:00
|
|
|
sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
|
|
|
|
sqliteHashInit(&db->idxHash, SQLITE_HASH_STRING, 0);
|
2002-01-09 06:19:59 +03:00
|
|
|
sqliteHashInit(&db->tblDrop, SQLITE_HASH_POINTER, 0);
|
|
|
|
sqliteHashInit(&db->idxDrop, SQLITE_HASH_POINTER, 0);
|
2002-02-28 03:41:10 +03:00
|
|
|
sqliteHashInit(&db->aFunc, SQLITE_HASH_STRING, 1);
|
2002-02-24 04:55:15 +03:00
|
|
|
sqliteRegisterBuildinFunctions(db);
|
2002-01-31 18:54:21 +03:00
|
|
|
db->onError = OE_Default;
|
2002-02-20 01:42:05 +03:00
|
|
|
db->priorNewRowid = 0;
|
2000-05-29 18:26:00 +04:00
|
|
|
|
|
|
|
/* Open the backend database driver */
|
2001-09-14 20:42:12 +04:00
|
|
|
rc = sqliteBtreeOpen(zFilename, mode, MAX_PAGES, &db->pBe);
|
2001-09-13 17:46:56 +04:00
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
switch( rc ){
|
|
|
|
default: {
|
2002-01-06 20:07:40 +03:00
|
|
|
sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0);
|
2001-09-13 17:46:56 +04:00
|
|
|
}
|
|
|
|
}
|
2000-05-29 18:26:00 +04:00
|
|
|
sqliteFree(db);
|
2001-09-14 01:53:09 +04:00
|
|
|
sqliteStrRealloc(pzErrMsg);
|
2001-09-13 18:46:09 +04:00
|
|
|
return 0;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
|
|
|
|
2000-06-02 05:17:37 +04:00
|
|
|
/* Attempt to read the schema */
|
|
|
|
rc = sqliteInit(db, pzErrMsg);
|
2001-04-11 18:28:42 +04:00
|
|
|
if( sqlite_malloc_failed ){
|
2001-10-22 06:58:08 +04:00
|
|
|
sqlite_close(db);
|
2001-04-11 18:28:42 +04:00
|
|
|
goto no_mem_on_open;
|
|
|
|
}else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
|
2000-06-02 05:17:37 +04:00
|
|
|
sqlite_close(db);
|
2001-09-14 01:53:09 +04:00
|
|
|
sqliteStrRealloc(pzErrMsg);
|
2000-06-02 05:17:37 +04:00
|
|
|
return 0;
|
2002-01-06 20:07:40 +03:00
|
|
|
}else if( pzErrMsg ){
|
2001-04-11 18:28:42 +04:00
|
|
|
sqliteFree(*pzErrMsg);
|
2000-06-02 17:27:59 +04:00
|
|
|
*pzErrMsg = 0;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
|
|
|
return db;
|
2001-04-11 18:28:42 +04:00
|
|
|
|
|
|
|
no_mem_on_open:
|
|
|
|
sqliteSetString(pzErrMsg, "out of memory", 0);
|
|
|
|
sqliteStrRealloc(pzErrMsg);
|
|
|
|
return 0;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-10-08 17:22:32 +04:00
|
|
|
** Erase all schema information from the schema hash table. Except
|
|
|
|
** tables that are created using CREATE TEMPORARY TABLE are preserved
|
2002-01-06 20:07:40 +03:00
|
|
|
** if the preserveTemps flag is true.
|
2001-09-15 04:57:28 +04:00
|
|
|
**
|
|
|
|
** The database schema is normally read in once when the database
|
|
|
|
** is first opened and stored in a hash table in the sqlite structure.
|
|
|
|
** This routine erases the stored schema. This erasure occurs because
|
|
|
|
** either the database is being closed or because some other process
|
|
|
|
** changed the schema and this process needs to reread it.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2001-10-08 17:22:32 +04:00
|
|
|
static void clearHashTable(sqlite *db, int preserveTemps){
|
2001-09-22 22:12:08 +04:00
|
|
|
HashElem *pElem;
|
|
|
|
Hash temp1;
|
2002-01-09 06:19:59 +03:00
|
|
|
assert( sqliteHashFirst(&db->tblDrop)==0 ); /* There can not be uncommitted */
|
|
|
|
assert( sqliteHashFirst(&db->idxDrop)==0 ); /* DROP TABLEs or DROP INDEXs */
|
2001-09-22 22:12:08 +04:00
|
|
|
temp1 = db->tblHash;
|
|
|
|
sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
|
|
|
|
sqliteHashClear(&db->idxHash);
|
|
|
|
for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
|
2001-10-08 17:22:32 +04:00
|
|
|
Table *pTab = sqliteHashData(pElem);
|
|
|
|
if( preserveTemps && pTab->isTemp ){
|
|
|
|
Index *pIdx;
|
2001-10-22 06:58:08 +04:00
|
|
|
int nName = strlen(pTab->zName);
|
|
|
|
Table *pOld = sqliteHashInsert(&db->tblHash, pTab->zName, nName+1, pTab);
|
|
|
|
if( pOld!=0 ){
|
|
|
|
assert( pOld==pTab ); /* Malloc failed on the HashInsert */
|
|
|
|
sqliteDeleteTable(db, pOld);
|
|
|
|
continue;
|
|
|
|
}
|
2001-10-08 17:22:32 +04:00
|
|
|
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
|
|
|
|
int n = strlen(pIdx->zName)+1;
|
2001-10-22 06:58:08 +04:00
|
|
|
Index *pOldIdx;
|
|
|
|
pOldIdx = sqliteHashInsert(&db->idxHash, pIdx->zName, n, pIdx);
|
|
|
|
if( pOld ){
|
|
|
|
assert( pOldIdx==pIdx );
|
|
|
|
sqliteUnlinkAndDeleteIndex(db, pOldIdx);
|
|
|
|
}
|
2001-10-08 17:22:32 +04:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
sqliteDeleteTable(db, pTab);
|
|
|
|
}
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
2001-09-22 22:12:08 +04:00
|
|
|
sqliteHashClear(&temp1);
|
2001-09-15 04:57:28 +04:00
|
|
|
db->flags &= ~SQLITE_Initialized;
|
|
|
|
}
|
|
|
|
|
2002-01-17 00:00:27 +03:00
|
|
|
/*
|
|
|
|
** Return the ROWID of the most recent insert
|
|
|
|
*/
|
|
|
|
int sqlite_last_insert_rowid(sqlite *db){
|
|
|
|
return db->lastRowid;
|
|
|
|
}
|
|
|
|
|
2002-04-12 14:08:59 +04:00
|
|
|
/*
|
|
|
|
** Return the number of changes in the most recent call to sqlite_exec().
|
|
|
|
*/
|
|
|
|
int sqlite_changes(sqlite *db){
|
|
|
|
return db->nChange;
|
|
|
|
}
|
|
|
|
|
2001-09-15 04:57:28 +04:00
|
|
|
/*
|
|
|
|
** Close an existing SQLite database
|
|
|
|
*/
|
|
|
|
void sqlite_close(sqlite *db){
|
2002-02-24 02:45:45 +03:00
|
|
|
HashElem *i;
|
2001-09-15 04:57:28 +04:00
|
|
|
sqliteBtreeClose(db->pBe);
|
2001-10-08 17:22:32 +04:00
|
|
|
clearHashTable(db, 0);
|
|
|
|
if( db->pBeTemp ){
|
|
|
|
sqliteBtreeClose(db->pBeTemp);
|
|
|
|
}
|
2002-02-28 03:41:10 +03:00
|
|
|
for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
|
|
|
|
FuncDef *pFunc, *pNext;
|
|
|
|
for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
|
2002-02-24 02:45:45 +03:00
|
|
|
pNext = pFunc->pNext;
|
|
|
|
sqliteFree(pFunc);
|
|
|
|
}
|
|
|
|
}
|
2002-02-28 03:41:10 +03:00
|
|
|
sqliteHashClear(&db->aFunc);
|
2000-05-29 18:26:00 +04:00
|
|
|
sqliteFree(db);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return TRUE if the given SQL string ends in a semicolon.
|
|
|
|
*/
|
|
|
|
int sqlite_complete(const char *zSql){
|
2000-12-10 21:23:50 +03:00
|
|
|
int isComplete = 0;
|
|
|
|
while( *zSql ){
|
|
|
|
switch( *zSql ){
|
|
|
|
case ';': {
|
|
|
|
isComplete = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\f': {
|
|
|
|
break;
|
|
|
|
}
|
2002-02-18 21:30:32 +03:00
|
|
|
case '[': {
|
|
|
|
isComplete = 0;
|
|
|
|
zSql++;
|
|
|
|
while( *zSql && *zSql!=']' ){ zSql++; }
|
|
|
|
if( *zSql==0 ) return 0;
|
|
|
|
break;
|
|
|
|
}
|
2000-12-10 21:23:50 +03:00
|
|
|
case '\'': {
|
|
|
|
isComplete = 0;
|
|
|
|
zSql++;
|
|
|
|
while( *zSql && *zSql!='\'' ){ zSql++; }
|
|
|
|
if( *zSql==0 ) return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '"': {
|
|
|
|
isComplete = 0;
|
|
|
|
zSql++;
|
|
|
|
while( *zSql && *zSql!='"' ){ zSql++; }
|
|
|
|
if( *zSql==0 ) return 0;
|
2000-05-29 18:26:00 +04:00
|
|
|
break;
|
2000-12-10 21:23:50 +03:00
|
|
|
}
|
|
|
|
case '-': {
|
|
|
|
if( zSql[1]!='-' ){
|
|
|
|
isComplete = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while( *zSql && *zSql!='\n' ){ zSql++; }
|
|
|
|
if( *zSql==0 ) return isComplete;
|
2000-05-29 18:26:00 +04:00
|
|
|
break;
|
2000-12-10 21:23:50 +03:00
|
|
|
}
|
|
|
|
default: {
|
|
|
|
isComplete = 0;
|
2000-05-29 18:26:00 +04:00
|
|
|
break;
|
2000-12-10 21:23:50 +03:00
|
|
|
}
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
2000-12-10 21:23:50 +03:00
|
|
|
zSql++;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
2000-12-10 21:23:50 +03:00
|
|
|
return isComplete;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-06-02 17:27:59 +04:00
|
|
|
** Execute SQL code. Return one of the SQLITE_ success/failure
|
|
|
|
** codes. Also write an error message into memory obtained from
|
|
|
|
** malloc() and make *pzErrMsg point to that message.
|
|
|
|
**
|
|
|
|
** If the SQL is a query, then for each row in the query result
|
|
|
|
** the xCallback() function is called. pArg becomes the first
|
|
|
|
** argument to xCallback(). If xCallback=NULL then no callback
|
|
|
|
** is invoked, even for queries.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
|
|
|
int sqlite_exec(
|
|
|
|
sqlite *db, /* The database on which the SQL executes */
|
2001-11-04 02:57:09 +03:00
|
|
|
const char *zSql, /* The SQL to be executed */
|
2000-05-29 18:26:00 +04:00
|
|
|
sqlite_callback xCallback, /* Invoke this callback routine */
|
|
|
|
void *pArg, /* First argument to xCallback() */
|
|
|
|
char **pzErrMsg /* Write error messages here */
|
|
|
|
){
|
|
|
|
Parse sParse;
|
|
|
|
|
|
|
|
if( pzErrMsg ) *pzErrMsg = 0;
|
2000-06-02 05:17:37 +04:00
|
|
|
if( (db->flags & SQLITE_Initialized)==0 ){
|
|
|
|
int rc = sqliteInit(db, pzErrMsg);
|
2001-04-11 18:28:42 +04:00
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
sqliteStrRealloc(pzErrMsg);
|
|
|
|
return rc;
|
|
|
|
}
|
2000-06-02 05:17:37 +04:00
|
|
|
}
|
2002-04-12 14:08:59 +04:00
|
|
|
if( db->recursionDepth==0 ){ db->nChange = 0; }
|
|
|
|
db->recursionDepth++;
|
2000-05-29 18:26:00 +04:00
|
|
|
memset(&sParse, 0, sizeof(sParse));
|
|
|
|
sParse.db = db;
|
2001-09-13 17:46:56 +04:00
|
|
|
sParse.pBe = db->pBe;
|
2000-05-29 18:26:00 +04:00
|
|
|
sParse.xCallback = xCallback;
|
|
|
|
sParse.pArg = pArg;
|
2000-10-17 02:06:40 +04:00
|
|
|
sqliteRunParser(&sParse, zSql, pzErrMsg);
|
2001-04-11 18:28:42 +04:00
|
|
|
if( sqlite_malloc_failed ){
|
|
|
|
sqliteSetString(pzErrMsg, "out of memory", 0);
|
|
|
|
sParse.rc = SQLITE_NOMEM;
|
2001-10-22 06:58:08 +04:00
|
|
|
sqliteBtreeRollback(db->pBe);
|
|
|
|
if( db->pBeTemp ) sqliteBtreeRollback(db->pBeTemp);
|
|
|
|
db->flags &= ~SQLITE_InTrans;
|
|
|
|
clearHashTable(db, 0);
|
2001-04-11 18:28:42 +04:00
|
|
|
}
|
|
|
|
sqliteStrRealloc(pzErrMsg);
|
2001-09-15 04:57:28 +04:00
|
|
|
if( sParse.rc==SQLITE_SCHEMA ){
|
2001-10-08 17:22:32 +04:00
|
|
|
clearHashTable(db, 1);
|
2001-09-15 04:57:28 +04:00
|
|
|
}
|
2002-04-12 14:08:59 +04:00
|
|
|
db->recursionDepth--;
|
2000-10-17 02:06:40 +04:00
|
|
|
return sParse.rc;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
2000-07-28 18:32:48 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** This routine implements a busy callback that sleeps and tries
|
|
|
|
** again until a timeout value is reached. The timeout value is
|
|
|
|
** an integer number of milliseconds passed in as the first
|
|
|
|
** argument.
|
|
|
|
*/
|
2001-04-11 18:28:42 +04:00
|
|
|
static int sqliteDefaultBusyCallback(
|
2000-07-28 18:32:48 +04:00
|
|
|
void *Timeout, /* Maximum amount of time to wait */
|
|
|
|
const char *NotUsed, /* The name of the table that is busy */
|
|
|
|
int count /* Number of times table has been busy */
|
|
|
|
){
|
2001-09-19 17:22:39 +04:00
|
|
|
#if SQLITE_MIN_SLEEP_MS==1
|
|
|
|
int delay = 10;
|
2000-07-28 18:32:48 +04:00
|
|
|
int prior_delay = 0;
|
|
|
|
int timeout = (int)Timeout;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i=1; i<count; i++){
|
|
|
|
prior_delay += delay;
|
|
|
|
delay = delay*2;
|
2001-09-19 17:22:39 +04:00
|
|
|
if( delay>=1000 ){
|
|
|
|
delay = 1000;
|
|
|
|
prior_delay += 1000*(count - i - 1);
|
2000-07-28 18:32:48 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-10-09 17:46:01 +04:00
|
|
|
if( prior_delay + delay > timeout ){
|
|
|
|
delay = timeout - prior_delay;
|
2000-07-28 18:32:48 +04:00
|
|
|
if( delay<=0 ) return 0;
|
|
|
|
}
|
2001-09-19 17:22:39 +04:00
|
|
|
sqliteOsSleep(delay);
|
2000-07-28 18:32:48 +04:00
|
|
|
return 1;
|
|
|
|
#else
|
|
|
|
int timeout = (int)Timeout;
|
|
|
|
if( (count+1)*1000 > timeout ){
|
|
|
|
return 0;
|
|
|
|
}
|
2001-09-19 17:22:39 +04:00
|
|
|
sqliteOsSleep(1000);
|
2000-07-28 18:32:48 +04:00
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This routine sets the busy callback for an Sqlite database to the
|
|
|
|
** given callback function with the given argument.
|
|
|
|
*/
|
|
|
|
void sqlite_busy_handler(
|
|
|
|
sqlite *db,
|
|
|
|
int (*xBusy)(void*,const char*,int),
|
|
|
|
void *pArg
|
|
|
|
){
|
|
|
|
db->xBusyCallback = xBusy;
|
|
|
|
db->pBusyArg = pArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This routine installs a default busy handler that waits for the
|
|
|
|
** specified number of milliseconds before returning 0.
|
|
|
|
*/
|
|
|
|
void sqlite_busy_timeout(sqlite *db, int ms){
|
|
|
|
if( ms>0 ){
|
2001-04-11 18:28:42 +04:00
|
|
|
sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
|
2000-07-28 18:32:48 +04:00
|
|
|
}else{
|
|
|
|
sqlite_busy_handler(db, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
2000-10-17 02:06:40 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Cause any pending operation to stop at its earliest opportunity.
|
|
|
|
*/
|
|
|
|
void sqlite_interrupt(sqlite *db){
|
|
|
|
db->flags |= SQLITE_Interrupt;
|
|
|
|
}
|
2002-02-02 18:01:15 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Windows systems should call this routine to free memory that
|
|
|
|
** is returned in the in the errmsg parameter of sqlite_open() when
|
|
|
|
** SQLite is a DLL. For some reason, it does not work to call free()
|
|
|
|
** directly.
|
|
|
|
**
|
|
|
|
** Note that we need to call free() not sqliteFree() here, since every
|
|
|
|
** string that is exported from SQLite should have already passed through
|
|
|
|
** sqliteStrRealloc().
|
|
|
|
*/
|
|
|
|
void sqlite_freemem(void *p){ free(p); }
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Windows systems need functions to call to return the sqlite_version
|
|
|
|
** and sqlite_encoding strings.
|
|
|
|
*/
|
|
|
|
const char *sqlite_libversion(void){ return sqlite_version; }
|
|
|
|
const char *sqlite_libencoding(void){ return sqlite_encoding; }
|
2002-02-24 02:45:45 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Create new user-defined functions. The sqlite_create_function()
|
|
|
|
** routine creates a regular function and sqlite_create_aggregate()
|
|
|
|
** creates an aggregate function.
|
|
|
|
**
|
|
|
|
** Passing a NULL xFunc argument or NULL xStep and xFinalize arguments
|
|
|
|
** disables the function. Calling sqlite_create_function() with the
|
|
|
|
** same name and number of arguments as a prior call to
|
|
|
|
** sqlite_create_aggregate() disables the prior call to
|
|
|
|
** sqlite_create_aggregate(), and vice versa.
|
|
|
|
**
|
|
|
|
** If nArg is -1 it means that this function will accept any number
|
|
|
|
** of arguments, including 0.
|
|
|
|
*/
|
|
|
|
int sqlite_create_function(
|
|
|
|
sqlite *db, /* Add the function to this database connection */
|
|
|
|
const char *zName, /* Name of the function to add */
|
|
|
|
int nArg, /* Number of arguments */
|
2002-02-27 22:00:20 +03:00
|
|
|
void (*xFunc)(sqlite_func*,int,const char**), /* The implementation */
|
|
|
|
void *pUserData /* User data */
|
2002-02-24 02:45:45 +03:00
|
|
|
){
|
2002-02-28 03:41:10 +03:00
|
|
|
FuncDef *p;
|
2002-02-24 02:45:45 +03:00
|
|
|
if( db==0 || zName==0 ) return 1;
|
2002-02-28 03:41:10 +03:00
|
|
|
p = sqliteFindFunction(db, zName, strlen(zName), nArg, 1);
|
2002-02-27 04:53:13 +03:00
|
|
|
if( p==0 ) return 1;
|
2002-02-24 02:45:45 +03:00
|
|
|
p->xFunc = xFunc;
|
|
|
|
p->xStep = 0;
|
|
|
|
p->xFinalize = 0;
|
2002-02-27 22:00:20 +03:00
|
|
|
p->pUserData = pUserData;
|
2002-02-24 02:45:45 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int sqlite_create_aggregate(
|
|
|
|
sqlite *db, /* Add the function to this database connection */
|
|
|
|
const char *zName, /* Name of the function to add */
|
|
|
|
int nArg, /* Number of arguments */
|
2002-02-27 22:00:20 +03:00
|
|
|
void (*xStep)(sqlite_func*,int,const char**), /* The step function */
|
|
|
|
void (*xFinalize)(sqlite_func*), /* The finalizer */
|
|
|
|
void *pUserData /* User data */
|
2002-02-24 02:45:45 +03:00
|
|
|
){
|
2002-02-28 03:41:10 +03:00
|
|
|
FuncDef *p;
|
2002-02-24 02:45:45 +03:00
|
|
|
if( db==0 || zName==0 ) return 1;
|
2002-02-28 03:41:10 +03:00
|
|
|
p = sqliteFindFunction(db, zName, strlen(zName), nArg, 1);
|
2002-02-27 04:53:13 +03:00
|
|
|
if( p==0 ) return 1;
|
2002-02-24 02:45:45 +03:00
|
|
|
p->xFunc = 0;
|
|
|
|
p->xStep = xStep;
|
|
|
|
p->xFinalize = xFinalize;
|
2002-02-27 22:00:20 +03:00
|
|
|
p->pUserData = pUserData;
|
2002-02-24 02:45:45 +03:00
|
|
|
return 0;
|
|
|
|
}
|