blob: 774575526653f70f065405e0c724c49142094f2b [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**
drhee570fa2005-04-28 12:06:05 +000017** $Id: main.c,v 1.286 2005/04/28 12:06: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/*
drh9c054832004-05-31 18:51:57 +000024** The following constant value is used by the SQLITE_BIGENDIAN and
25** SQLITE_LITTLEENDIAN macros.
drhbbd42a62004-05-22 17:41:58 +000026*/
27const int sqlite3one = 1;
28
danielk19776b456a22005-03-21 04:04:02 +000029#ifndef SQLITE_OMIT_GLOBALRECOVER
30/*
31** Linked list of all open database handles. This is used by the
32** sqlite3_global_recover() function. Entries are added to the list
33** by openDatabase() and removed by sqlite3_close().
34*/
35static sqlite3 *pDbList = 0;
36#endif
37
38
drhbbd42a62004-05-22 17:41:58 +000039/*
drh8bf8dc92003-05-17 17:35:10 +000040** Fill the InitData structure with an error message that indicates
41** that the database is corrupt.
42*/
drh1d85d932004-02-14 23:05:52 +000043static void corruptSchema(InitData *pData, const char *zExtra){
drhef0715c2004-06-29 10:53:54 +000044 if( !sqlite3_malloc_failed ){
45 sqlite3SetString(pData->pzErrMsg, "malformed database schema",
46 zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0);
47 }
drh8bf8dc92003-05-17 17:35:10 +000048}
drhc2311722002-07-19 17:46:38 +000049
50/*
drh75897232000-05-29 14:26:00 +000051** This is the callback routine for the code that initializes the
danielk19774adee202004-05-08 08:23:19 +000052** database. See sqlite3Init() below for additional information.
drh234c39d2004-07-24 03:30:47 +000053** This routine is also called from the OP_ParseSchema opcode of the VDBE.
drh382c0242001-10-06 16:33:02 +000054**
55** Each callback contains the following information:
drh28037572000-08-02 13:47:41 +000056**
drh234c39d2004-07-24 03:30:47 +000057** argv[0] = name of thing being created
58** argv[1] = root page number for table or index. NULL for trigger or view.
59** argv[2] = SQL text for the CREATE statement.
60** argv[3] = "1" for temporary files, "0" for main database, "2" or more
drh1c2d8412003-03-31 00:30:47 +000061** for auxiliary database files.
drhd78eeee2001-09-13 16:18:53 +000062**
drh75897232000-05-29 14:26:00 +000063*/
danielk19774adee202004-05-08 08:23:19 +000064int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){
drhc2311722002-07-19 17:46:38 +000065 InitData *pData = (InitData*)pInit;
drh9bb575f2004-09-06 17:24:11 +000066 sqlite3 *db = pData->db;
drh234c39d2004-07-24 03:30:47 +000067 int iDb;
drh75897232000-05-29 14:26:00 +000068
drh234c39d2004-07-24 03:30:47 +000069 assert( argc==4 );
drh98e3e602003-07-27 17:26:22 +000070 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
drh234c39d2004-07-24 03:30:47 +000071 if( argv[1]==0 || argv[3]==0 ){
drh1d85d932004-02-14 23:05:52 +000072 corruptSchema(pData, 0);
drh8bf8dc92003-05-17 17:35:10 +000073 return 1;
74 }
drh234c39d2004-07-24 03:30:47 +000075 iDb = atoi(argv[3]);
76 assert( iDb>=0 && iDb<db->nDb );
77 if( argv[2] && argv[2][0] ){
78 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
79 ** But because db->init.busy is set to 1, no VDBE code is generated
80 ** or executed. All the parser does is build the internal data
81 ** structures that describe the table, index, or view.
82 */
83 char *zErr;
84 int rc;
85 assert( db->init.busy );
86 db->init.iDb = iDb;
87 db->init.newTnum = atoi(argv[1]);
88 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
89 db->init.iDb = 0;
90 if( SQLITE_OK!=rc ){
91 corruptSchema(pData, zErr);
92 sqlite3_free(zErr);
93 return rc;
drhd78eeee2001-09-13 16:18:53 +000094 }
drh234c39d2004-07-24 03:30:47 +000095 }else{
96 /* If the SQL column is blank it means this is an index that
97 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
98 ** constraint for a CREATE TABLE. The index should have already
99 ** been created when we processed the CREATE TABLE. All we have
100 ** to do here is record the root page number for that index.
101 */
102 Index *pIndex;
103 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
104 if( pIndex==0 || pIndex->tnum!=0 ){
105 /* This can occur if there exists an index on a TEMP table which
106 ** has the same name as another index on a permanent index. Since
107 ** the permanent table is hidden by the TEMP table, we can also
108 ** safely ignore the index on the permanent table.
109 */
110 /* Do Nothing */;
111 }else{
112 pIndex->tnum = atoi(argv[1]);
drhd78eeee2001-09-13 16:18:53 +0000113 }
drh28037572000-08-02 13:47:41 +0000114 }
drh234c39d2004-07-24 03:30:47 +0000115 return 0;
drh75897232000-05-29 14:26:00 +0000116}
117
118/*
drh58b95762000-06-02 01:17:37 +0000119** Attempt to read the database schema and initialize internal
drh1c2d8412003-03-31 00:30:47 +0000120** data structures for a single database file. The index of the
121** database file is given by iDb. iDb==0 is used for the main
122** database. iDb==1 should never be used. iDb>=2 is used for
123** auxiliary databases. Return one of the SQLITE_ error codes to
drh58b95762000-06-02 01:17:37 +0000124** indicate success or failure.
drh75897232000-05-29 14:26:00 +0000125*/
drh9bb575f2004-09-06 17:24:11 +0000126static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
drh58b95762000-06-02 01:17:37 +0000127 int rc;
drhe0bc4042002-06-25 01:09:11 +0000128 BtCursor *curMain;
129 int size;
130 Table *pTab;
drh234c39d2004-07-24 03:30:47 +0000131 char const *azArg[5];
drh1c2d8412003-03-31 00:30:47 +0000132 char zDbNum[30];
drha3b321d2004-05-11 09:31:31 +0000133 int meta[10];
drhc2311722002-07-19 17:46:38 +0000134 InitData initData;
danielk1977d008cfe2004-06-19 02:22:10 +0000135 char const *zMasterSchema;
danielk197753c0f742005-03-29 03:10:59 +0000136 char const *zMasterName = SCHEMA_TABLE(iDb);
drh58b95762000-06-02 01:17:37 +0000137
138 /*
139 ** The master database table has a structure like this
140 */
drh57196282004-10-06 15:41:16 +0000141 static const char master_schema[] =
drhe0bc4042002-06-25 01:09:11 +0000142 "CREATE TABLE sqlite_master(\n"
143 " type text,\n"
144 " name text,\n"
145 " tbl_name text,\n"
146 " rootpage integer,\n"
147 " sql text\n"
148 ")"
149 ;
danielk197753c0f742005-03-29 03:10:59 +0000150#ifndef SQLITE_OMIT_TEMPDB
drh57196282004-10-06 15:41:16 +0000151 static const char temp_master_schema[] =
drhe0bc4042002-06-25 01:09:11 +0000152 "CREATE TEMP TABLE sqlite_temp_master(\n"
drh75897232000-05-29 14:26:00 +0000153 " type text,\n"
154 " name text,\n"
155 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000156 " rootpage integer,\n"
drh75897232000-05-29 14:26:00 +0000157 " sql text\n"
158 ")"
159 ;
danielk197753c0f742005-03-29 03:10:59 +0000160#else
161 #define temp_master_schema 0
162#endif
drh75897232000-05-29 14:26:00 +0000163
danielk1977d008cfe2004-06-19 02:22:10 +0000164 assert( iDb>=0 && iDb<db->nDb );
drh603240c2002-03-05 01:11:12 +0000165
danielk1977d008cfe2004-06-19 02:22:10 +0000166 /* zMasterSchema and zInitScript are set to point at the master schema
167 ** and initialisation script appropriate for the database being
168 ** initialised. zMasterName is the name of the master table.
drh58b95762000-06-02 01:17:37 +0000169 */
danielk197753c0f742005-03-29 03:10:59 +0000170 if( !OMIT_TEMPDB && iDb==1 ){
danielk1977d008cfe2004-06-19 02:22:10 +0000171 zMasterSchema = temp_master_schema;
danielk1977d008cfe2004-06-19 02:22:10 +0000172 }else{
173 zMasterSchema = master_schema;
danielk1977d008cfe2004-06-19 02:22:10 +0000174 }
danielk197753c0f742005-03-29 03:10:59 +0000175 zMasterName = SCHEMA_TABLE(iDb);
danielk1977d008cfe2004-06-19 02:22:10 +0000176
177 /* Construct the schema tables. */
danielk19774adee202004-05-08 08:23:19 +0000178 sqlite3SafetyOff(db);
drh234c39d2004-07-24 03:30:47 +0000179 azArg[0] = zMasterName;
180 azArg[1] = "1";
181 azArg[2] = zMasterSchema;
drh1c2d8412003-03-31 00:30:47 +0000182 sprintf(zDbNum, "%d", iDb);
drh234c39d2004-07-24 03:30:47 +0000183 azArg[3] = zDbNum;
184 azArg[4] = 0;
drhc2311722002-07-19 17:46:38 +0000185 initData.db = db;
186 initData.pzErrMsg = pzErrMsg;
drh234c39d2004-07-24 03:30:47 +0000187 rc = sqlite3InitCallback(&initData, 4, (char **)azArg, 0);
danielk19772b444852004-06-29 07:45:33 +0000188 if( rc!=SQLITE_OK ){
189 sqlite3SafetyOn(db);
190 return rc;
191 }
danielk1977d008cfe2004-06-19 02:22:10 +0000192 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
drhe0bc4042002-06-25 01:09:11 +0000193 if( pTab ){
194 pTab->readOnly = 1;
drhd8bc7082000-06-07 23:51:50 +0000195 }
danielk19774adee202004-05-08 08:23:19 +0000196 sqlite3SafetyOn(db);
drhe0bc4042002-06-25 01:09:11 +0000197
198 /* Create a cursor to hold the database open
199 */
drhdc3ff9c2004-08-18 02:10:15 +0000200 if( db->aDb[iDb].pBt==0 ){
danielk197753c0f742005-03-29 03:10:59 +0000201 if( !OMIT_TEMPDB && iDb==1 ) DbSetProperty(db, 1, DB_SchemaLoaded);
drhdc3ff9c2004-08-18 02:10:15 +0000202 return SQLITE_OK;
203 }
danielk19778e150812004-05-10 01:17:37 +0000204 rc = sqlite3BtreeCursor(db->aDb[iDb].pBt, MASTER_ROOT, 0, 0, 0, &curMain);
drhf328bc82004-05-10 23:29:49 +0000205 if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
danielk1977f20b21c2004-05-31 23:56:42 +0000206 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
drh92ed08a2002-07-30 18:43:40 +0000207 return rc;
208 }
drhe0bc4042002-06-25 01:09:11 +0000209
drha3b321d2004-05-11 09:31:31 +0000210 /* Get the database meta information.
211 **
212 ** Meta values are as follows:
213 ** meta[0] Schema cookie. Changes with each schema change.
214 ** meta[1] File format of schema layer.
215 ** meta[2] Size of the page cache.
drhae157872004-08-14 19:20:09 +0000216 ** meta[3] Use freelist if 0. Autovacuum if greater than zero.
danielk1977dc8453f2004-06-12 00:42:34 +0000217 ** meta[4] Db text encoding. 1:UTF-8 3:UTF-16 LE 4:UTF-16 BE
danielk1977dae24952004-11-11 05:10:43 +0000218 ** meta[5] The user cookie. Used by the application.
danielk1977962398d2004-06-14 09:35:16 +0000219 ** meta[6]
drha3b321d2004-05-11 09:31:31 +0000220 ** meta[7]
221 ** meta[8]
222 ** meta[9]
danielk1977172bc392004-05-22 08:09:11 +0000223 **
danielk1977dc8453f2004-06-12 00:42:34 +0000224 ** Note: The hash defined SQLITE_UTF* symbols in sqliteInt.h correspond to
danielk1977172bc392004-05-22 08:09:11 +0000225 ** the possible values of meta[4].
drhe0bc4042002-06-25 01:09:11 +0000226 */
drhf328bc82004-05-10 23:29:49 +0000227 if( rc==SQLITE_OK ){
228 int i;
drha3b321d2004-05-11 09:31:31 +0000229 for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
danielk1977e0d4b062004-06-28 01:11:46 +0000230 rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, i+1, (u32 *)&meta[i]);
danielk19774adee202004-05-08 08:23:19 +0000231 }
drhf328bc82004-05-10 23:29:49 +0000232 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000233 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
drhf328bc82004-05-10 23:29:49 +0000234 sqlite3BtreeCloseCursor(curMain);
235 return rc;
236 }
237 }else{
238 memset(meta, 0, sizeof(meta));
drhe0bc4042002-06-25 01:09:11 +0000239 }
drha3b321d2004-05-11 09:31:31 +0000240 db->aDb[iDb].schema_cookie = meta[0];
danielk19773df6b252004-05-29 10:23:19 +0000241
242 /* If opening a non-empty database, check the text encoding. For the
243 ** main database, set sqlite3.enc to the encoding of the main database.
244 ** For an attached db, it is an error if the encoding is not the same
245 ** as sqlite3.enc.
246 */
247 if( meta[4] ){ /* text encoding */
248 if( iDb==0 ){
249 /* If opening the main database, set db->enc. */
danielk1977172bc392004-05-22 08:09:11 +0000250 db->enc = (u8)meta[4];
danielk1977466be562004-06-10 02:16:01 +0000251 db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0);
danielk19773df6b252004-05-29 10:23:19 +0000252 }else{
253 /* If opening an attached database, the encoding much match db->enc */
254 if( meta[4]!=db->enc ){
255 sqlite3BtreeCloseCursor(curMain);
256 sqlite3SetString(pzErrMsg, "attached databases must use the same"
257 " text encoding as main database", (char*)0);
258 return SQLITE_ERROR;
259 }
danielk1977172bc392004-05-22 08:09:11 +0000260 }
danielk19773df6b252004-05-29 10:23:19 +0000261 }
262
danielk197791cf71b2004-06-26 06:37:06 +0000263 size = meta[2];
264 if( size==0 ){ size = MAX_PAGES; }
265 db->aDb[iDb].cache_size = size;
drhe0bc4042002-06-25 01:09:11 +0000266
danielk197791cf71b2004-06-26 06:37:06 +0000267 if( iDb==0 ){
danielk19773df6b252004-05-29 10:23:19 +0000268 db->file_format = meta[1];
drh1c2d8412003-03-31 00:30:47 +0000269 if( db->file_format==0 ){
270 /* This happens if the database was initially empty */
drhf328bc82004-05-10 23:29:49 +0000271 db->file_format = 1;
drh1c2d8412003-03-31 00:30:47 +0000272 }
danielk197736963fd2005-02-19 08:18:05 +0000273
danielk1977aee18ef2005-03-09 12:26:50 +0000274 if( db->file_format==2 || db->file_format==3 ){
danielk197736963fd2005-02-19 08:18:05 +0000275 /* File format 2 is treated exactly as file format 1. New
276 ** databases are created with file format 1.
277 */
278 db->file_format = 1;
279 }
drh28037572000-08-02 13:47:41 +0000280 }
danielk19773df6b252004-05-29 10:23:19 +0000281
282 /*
danielk197736963fd2005-02-19 08:18:05 +0000283 ** file_format==1 Version 3.0.0.
284 ** file_format==2 Version 3.1.3.
danielk1977aee18ef2005-03-09 12:26:50 +0000285 ** file_format==3 Version 3.1.4.
danielk197736963fd2005-02-19 08:18:05 +0000286 **
287 ** Version 3.0 can only use files with file_format==1. Version 3.1.3
288 ** can read and write files with file_format==1 or file_format==2.
danielk1977aee18ef2005-03-09 12:26:50 +0000289 ** Version 3.1.4 can read and write file formats 1, 2 and 3.
danielk19773df6b252004-05-29 10:23:19 +0000290 */
danielk1977aee18ef2005-03-09 12:26:50 +0000291 if( meta[1]>3 ){
danielk19773df6b252004-05-29 10:23:19 +0000292 sqlite3BtreeCloseCursor(curMain);
293 sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
294 return SQLITE_ERROR;
295 }
296
danielk197791cf71b2004-06-26 06:37:06 +0000297 sqlite3BtreeSetCacheSize(db->aDb[iDb].pBt, db->aDb[iDb].cache_size);
drhaacc5432002-01-06 17:07:40 +0000298
drhe0bc4042002-06-25 01:09:11 +0000299 /* Read the schema information out of the schema tables
drhaacc5432002-01-06 17:07:40 +0000300 */
drh1d85d932004-02-14 23:05:52 +0000301 assert( db->init.busy );
drhf328bc82004-05-10 23:29:49 +0000302 if( rc==SQLITE_EMPTY ){
303 /* For an empty database, there is nothing to read */
304 rc = SQLITE_OK;
drh1c2d8412003-03-31 00:30:47 +0000305 }else{
drh234c39d2004-07-24 03:30:47 +0000306 char *zSql;
307 zSql = sqlite3MPrintf(
danielk1977c60e9b82005-01-31 12:42:29 +0000308 "SELECT name, rootpage, sql, '%s' FROM '%q'.%s",
drh234c39d2004-07-24 03:30:47 +0000309 zDbNum, db->aDb[iDb].zName, zMasterName);
danielk19773df6b252004-05-29 10:23:19 +0000310 sqlite3SafetyOff(db);
danielk1977d008cfe2004-06-19 02:22:10 +0000311 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
drhf328bc82004-05-10 23:29:49 +0000312 sqlite3SafetyOn(db);
drh234c39d2004-07-24 03:30:47 +0000313 sqliteFree(zSql);
drhf328bc82004-05-10 23:29:49 +0000314 sqlite3BtreeCloseCursor(curMain);
drh1c2d8412003-03-31 00:30:47 +0000315 }
danielk197724b03fd2004-05-10 10:34:34 +0000316 if( sqlite3_malloc_failed ){
danielk19774adee202004-05-08 08:23:19 +0000317 sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
drh1d85d932004-02-14 23:05:52 +0000318 rc = SQLITE_NOMEM;
danielk19774adee202004-05-08 08:23:19 +0000319 sqlite3ResetInternalSchema(db, 0);
drhe0bc4042002-06-25 01:09:11 +0000320 }
drh1d85d932004-02-14 23:05:52 +0000321 if( rc==SQLITE_OK ){
drh8bf8dc92003-05-17 17:35:10 +0000322 DbSetProperty(db, iDb, DB_SchemaLoaded);
drh1c2d8412003-03-31 00:30:47 +0000323 }else{
danielk19774adee202004-05-08 08:23:19 +0000324 sqlite3ResetInternalSchema(db, iDb);
drh1c2d8412003-03-31 00:30:47 +0000325 }
drh1d85d932004-02-14 23:05:52 +0000326 return rc;
drh1c2d8412003-03-31 00:30:47 +0000327}
328
329/*
330** Initialize all database files - the main database file, the file
331** used to store temporary tables, and any additional database files
332** created using ATTACH statements. Return a success code. If an
333** error occurs, write an error message into *pzErrMsg.
334**
335** After the database is initialized, the SQLITE_Initialized
danielk19778e227872004-06-07 07:52:17 +0000336** bit is set in the flags field of the sqlite structure.
drh1c2d8412003-03-31 00:30:47 +0000337*/
drh9bb575f2004-09-06 17:24:11 +0000338int sqlite3Init(sqlite3 *db, char **pzErrMsg){
drh1c2d8412003-03-31 00:30:47 +0000339 int i, rc;
340
drh1d85d932004-02-14 23:05:52 +0000341 if( db->init.busy ) return SQLITE_OK;
drh1c2d8412003-03-31 00:30:47 +0000342 assert( (db->flags & SQLITE_Initialized)==0 );
343 rc = SQLITE_OK;
drh1d85d932004-02-14 23:05:52 +0000344 db->init.busy = 1;
drh1c2d8412003-03-31 00:30:47 +0000345 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk1977d008cfe2004-06-19 02:22:10 +0000346 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
danielk19774adee202004-05-08 08:23:19 +0000347 rc = sqlite3InitOne(db, i, pzErrMsg);
drh8ef83ff2004-02-12 15:31:21 +0000348 if( rc ){
danielk19774adee202004-05-08 08:23:19 +0000349 sqlite3ResetInternalSchema(db, i);
drh8ef83ff2004-02-12 15:31:21 +0000350 }
drh1c2d8412003-03-31 00:30:47 +0000351 }
danielk1977d008cfe2004-06-19 02:22:10 +0000352
353 /* Once all the other databases have been initialised, load the schema
354 ** for the TEMP database. This is loaded last, as the TEMP database
355 ** schema may contain references to objects in other databases.
356 */
danielk197753c0f742005-03-29 03:10:59 +0000357#ifndef SQLITE_OMIT_TEMPDB
danielk1977d008cfe2004-06-19 02:22:10 +0000358 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
359 rc = sqlite3InitOne(db, 1, pzErrMsg);
360 if( rc ){
361 sqlite3ResetInternalSchema(db, 1);
362 }
363 }
danielk197753c0f742005-03-29 03:10:59 +0000364#endif
danielk1977d008cfe2004-06-19 02:22:10 +0000365
drh1d85d932004-02-14 23:05:52 +0000366 db->init.busy = 0;
drh1c2d8412003-03-31 00:30:47 +0000367 if( rc==SQLITE_OK ){
drh58b95762000-06-02 01:17:37 +0000368 db->flags |= SQLITE_Initialized;
danielk19774adee202004-05-08 08:23:19 +0000369 sqlite3CommitInternalChanges(db);
drh2d71ca92004-02-10 02:27:04 +0000370 }
371
drh2d71ca92004-02-10 02:27:04 +0000372 if( rc!=SQLITE_OK ){
drhe0bc4042002-06-25 01:09:11 +0000373 db->flags &= ~SQLITE_Initialized;
drh58b95762000-06-02 01:17:37 +0000374 }
drh1c2d8412003-03-31 00:30:47 +0000375 return rc;
drh58b95762000-06-02 01:17:37 +0000376}
377
378/*
danielk19778e227872004-06-07 07:52:17 +0000379** This routine is a no-op if the database schema is already initialised.
380** Otherwise, the schema is loaded. An error code is returned.
381*/
danielk19778a414492004-06-29 08:59:35 +0000382int sqlite3ReadSchema(Parse *pParse){
danielk19778e227872004-06-07 07:52:17 +0000383 int rc = SQLITE_OK;
danielk19778a414492004-06-29 08:59:35 +0000384 sqlite3 *db = pParse->db;
danielk19778e227872004-06-07 07:52:17 +0000385 if( !db->init.busy ){
386 if( (db->flags & SQLITE_Initialized)==0 ){
danielk19778a414492004-06-29 08:59:35 +0000387 rc = sqlite3Init(db, &pParse->zErrMsg);
danielk19778e227872004-06-07 07:52:17 +0000388 }
389 }
danielk1977c0391392004-06-09 12:30:04 +0000390 assert( rc!=SQLITE_OK || (db->flags & SQLITE_Initialized)||db->init.busy );
danielk19778a414492004-06-29 08:59:35 +0000391 if( rc!=SQLITE_OK ){
392 pParse->rc = rc;
393 pParse->nErr++;
394 }
danielk19778e227872004-06-07 07:52:17 +0000395 return rc;
396}
397
398/*
drhb217a572000-08-22 13:40:18 +0000399** The version of the library
400*/
drh38f82712004-06-18 17:10:16 +0000401const char rcsid3[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $";
danielk197724b03fd2004-05-10 10:34:34 +0000402const char sqlite3_version[] = SQLITE_VERSION;
drh4aec8b62004-08-28 16:19:00 +0000403const char *sqlite3_libversion(void){ return sqlite3_version; }
danielk197799ba19e2005-02-05 07:33:34 +0000404int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
drhb217a572000-08-22 13:40:18 +0000405
406/*
drhd3d39e92004-05-20 22:16:29 +0000407** This is the default collating function named "BINARY" which is always
408** available.
409*/
danielk19772c336542005-01-13 02:14:23 +0000410static int binCollFunc(
drhd3d39e92004-05-20 22:16:29 +0000411 void *NotUsed,
412 int nKey1, const void *pKey1,
413 int nKey2, const void *pKey2
414){
415 int rc, n;
416 n = nKey1<nKey2 ? nKey1 : nKey2;
417 rc = memcmp(pKey1, pKey2, n);
418 if( rc==0 ){
419 rc = nKey1 - nKey2;
420 }
421 return rc;
422}
423
424/*
danielk1977dc1bdc42004-06-11 10:51:27 +0000425** Another built-in collating sequence: NOCASE.
426**
427** This collating sequence is intended to be used for "case independant
428** comparison". SQLite's knowledge of upper and lower case equivalents
429** extends only to the 26 characters used in the English language.
430**
431** At the moment there is only a UTF-8 implementation.
danielk19770202b292004-06-09 09:55:16 +0000432*/
433static int nocaseCollatingFunc(
434 void *NotUsed,
435 int nKey1, const void *pKey1,
436 int nKey2, const void *pKey2
437){
438 int r = sqlite3StrNICmp(
drh208f80a2004-08-29 20:08:58 +0000439 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
danielk19770202b292004-06-09 09:55:16 +0000440 if( 0==r ){
441 r = nKey1-nKey2;
442 }
443 return r;
444}
445
446/*
drhaf9ff332002-01-16 21:00:27 +0000447** Return the ROWID of the most recent insert
448*/
drh9bb575f2004-09-06 17:24:11 +0000449sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
drhaf9ff332002-01-16 21:00:27 +0000450 return db->lastRowid;
451}
452
453/*
danielk197724b03fd2004-05-10 10:34:34 +0000454** Return the number of changes in the most recent call to sqlite3_exec().
drhc8d30ac2002-04-12 10:08:59 +0000455*/
drh9bb575f2004-09-06 17:24:11 +0000456int sqlite3_changes(sqlite3 *db){
drhc8d30ac2002-04-12 10:08:59 +0000457 return db->nChange;
458}
459
rdcf146a772004-02-25 22:51:06 +0000460/*
danielk1977b28af712004-06-21 06:50:26 +0000461** Return the number of changes since the database handle was opened.
rdcf146a772004-02-25 22:51:06 +0000462*/
danielk1977b28af712004-06-21 06:50:26 +0000463int sqlite3_total_changes(sqlite3 *db){
464 return db->nTotalChange;
rdcb0c374f2004-02-20 22:53:38 +0000465}
466
drhc8d30ac2002-04-12 10:08:59 +0000467/*
drh50e5dad2001-09-15 00:57:28 +0000468** Close an existing SQLite database
469*/
drh9bb575f2004-09-06 17:24:11 +0000470int sqlite3_close(sqlite3 *db){
drh8e0a2f92002-02-23 23:45:45 +0000471 HashElem *i;
drh001bbcb2003-03-19 03:14:00 +0000472 int j;
danielk19775c4c7782004-06-16 10:39:23 +0000473
474 if( !db ){
danielk197796d81f92004-06-19 03:33:57 +0000475 return SQLITE_OK;
danielk19775c4c7782004-06-16 10:39:23 +0000476 }
drhc60d0442004-09-30 13:43:13 +0000477 if( sqlite3SafetyCheck(db) ){
danielk1977e35ee192004-06-26 09:50:11 +0000478 return SQLITE_MISUSE;
479 }
480
danielk197796d81f92004-06-19 03:33:57 +0000481 /* If there are any outstanding VMs, return SQLITE_BUSY. */
482 if( db->pVdbe ){
483 sqlite3Error(db, SQLITE_BUSY,
484 "Unable to close due to unfinalised statements");
485 return SQLITE_BUSY;
486 }
487 assert( !sqlite3SafetyCheck(db) );
danielk1977e0048402004-06-15 16:51:01 +0000488
489 /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database
490 ** cannot be opened for some reason. So this routine needs to run in
491 ** that case. But maybe there should be an extra magic value for the
492 ** "failed to open" state.
493 */
danielk197796d81f92004-06-19 03:33:57 +0000494 if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){
drh94e92032003-02-16 22:21:32 +0000495 /* printf("DID NOT CLOSE\n"); fflush(stdout); */
danielk197796d81f92004-06-19 03:33:57 +0000496 return SQLITE_ERROR;
drh94e92032003-02-16 22:21:32 +0000497 }
danielk1977e0048402004-06-15 16:51:01 +0000498
drh001bbcb2003-03-19 03:14:00 +0000499 for(j=0; j<db->nDb; j++){
drh4d189ca2004-02-12 18:46:38 +0000500 struct Db *pDb = &db->aDb[j];
501 if( pDb->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000502 sqlite3BtreeClose(pDb->pBt);
drh4d189ca2004-02-12 18:46:38 +0000503 pDb->pBt = 0;
drh113088e2003-03-20 01:16:58 +0000504 }
drhf57b3392001-10-08 13:22:32 +0000505 }
danielk19774adee202004-05-08 08:23:19 +0000506 sqlite3ResetInternalSchema(db, 0);
drh1c2d8412003-03-31 00:30:47 +0000507 assert( db->nDb<=2 );
508 assert( db->aDb==db->aDbStatic );
drh0bce8352002-02-28 00:41:10 +0000509 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
510 FuncDef *pFunc, *pNext;
511 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
drh8e0a2f92002-02-23 23:45:45 +0000512 pNext = pFunc->pNext;
513 sqliteFree(pFunc);
514 }
515 }
danielk1977466be562004-06-10 02:16:01 +0000516
danielk1977d8123362004-06-12 09:25:12 +0000517 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
danielk1977466be562004-06-10 02:16:01 +0000518 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
danielk1977d8123362004-06-12 09:25:12 +0000519 sqliteFree(pColl);
danielk1977466be562004-06-10 02:16:01 +0000520 }
danielk1977d8123362004-06-12 09:25:12 +0000521 sqlite3HashClear(&db->aCollSeq);
danielk1977466be562004-06-10 02:16:01 +0000522
danielk19774adee202004-05-08 08:23:19 +0000523 sqlite3HashClear(&db->aFunc);
danielk19776622cce2004-05-20 11:00:52 +0000524 sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
danielk1977bfd6cce2004-06-18 04:24:54 +0000525 if( db->pValue ){
526 sqlite3ValueFree(db->pValue);
527 }
528 if( db->pErr ){
529 sqlite3ValueFree(db->pErr);
530 }
danielk197796d81f92004-06-19 03:33:57 +0000531
danielk19776b456a22005-03-21 04:04:02 +0000532#ifndef SQLITE_OMIT_GLOBALRECOVER
533 {
danielk19773eb8db92005-03-29 23:34:58 +0000534 sqlite3 *pPrev;
danielk19776b456a22005-03-21 04:04:02 +0000535 sqlite3OsEnterMutex();
danielk19773eb8db92005-03-29 23:34:58 +0000536 pPrev = pDbList;
danielk19776b456a22005-03-21 04:04:02 +0000537 while( pPrev && pPrev->pNext!=db ){
538 pPrev = pPrev->pNext;
539 }
540 if( pPrev ){
541 pPrev->pNext = db->pNext;
542 }else{
543 assert( pDbList==db );
544 pDbList = db->pNext;
545 }
546 sqlite3OsLeaveMutex();
547 }
548#endif
549
danielk197796d81f92004-06-19 03:33:57 +0000550 db->magic = SQLITE_MAGIC_ERROR;
drh75897232000-05-29 14:26:00 +0000551 sqliteFree(db);
danielk197796d81f92004-06-19 03:33:57 +0000552 return SQLITE_OK;
drh75897232000-05-29 14:26:00 +0000553}
554
555/*
drh001bbcb2003-03-19 03:14:00 +0000556** Rollback all database files.
557*/
drh9bb575f2004-09-06 17:24:11 +0000558void sqlite3RollbackAll(sqlite3 *db){
drh001bbcb2003-03-19 03:14:00 +0000559 int i;
560 for(i=0; i<db->nDb; i++){
561 if( db->aDb[i].pBt ){
danielk19774adee202004-05-08 08:23:19 +0000562 sqlite3BtreeRollback(db->aDb[i].pBt);
drh001bbcb2003-03-19 03:14:00 +0000563 db->aDb[i].inTrans = 0;
564 }
565 }
danielk19774adee202004-05-08 08:23:19 +0000566 sqlite3ResetInternalSchema(db, 0);
drh001bbcb2003-03-19 03:14:00 +0000567}
568
569/*
drhc22bd472002-05-10 13:14:07 +0000570** Return a static string that describes the kind of error specified in the
571** argument.
drh247be432002-05-10 05:44:55 +0000572*/
danielk1977f20b21c2004-05-31 23:56:42 +0000573const char *sqlite3ErrStr(int rc){
drhc22bd472002-05-10 13:14:07 +0000574 const char *z;
575 switch( rc ){
drhc60d0442004-09-30 13:43:13 +0000576 case SQLITE_ROW:
577 case SQLITE_DONE:
drhc22bd472002-05-10 13:14:07 +0000578 case SQLITE_OK: z = "not an error"; break;
579 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
580 case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break;
581 case SQLITE_PERM: z = "access permission denied"; break;
582 case SQLITE_ABORT: z = "callback requested query abort"; break;
583 case SQLITE_BUSY: z = "database is locked"; break;
584 case SQLITE_LOCKED: z = "database table is locked"; break;
585 case SQLITE_NOMEM: z = "out of memory"; break;
586 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
587 case SQLITE_INTERRUPT: z = "interrupted"; break;
588 case SQLITE_IOERR: z = "disk I/O error"; break;
589 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
590 case SQLITE_NOTFOUND: z = "table or record not found"; break;
591 case SQLITE_FULL: z = "database is full"; break;
592 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
593 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
594 case SQLITE_EMPTY: z = "table contains no data"; break;
595 case SQLITE_SCHEMA: z = "database schema has changed"; break;
596 case SQLITE_TOOBIG: z = "too much data for one table row"; break;
597 case SQLITE_CONSTRAINT: z = "constraint failed"; break;
598 case SQLITE_MISMATCH: z = "datatype mismatch"; break;
599 case SQLITE_MISUSE: z = "library routine called out of sequence";break;
drh8766c342002-11-09 00:33:15 +0000600 case SQLITE_NOLFS: z = "kernel lacks large file support"; break;
drhed6c8672003-01-12 18:02:16 +0000601 case SQLITE_AUTH: z = "authorization denied"; break;
jplyon892f6712003-06-12 08:59:00 +0000602 case SQLITE_FORMAT: z = "auxiliary database format error"; break;
drhb08153d2004-11-20 20:18:55 +0000603 case SQLITE_RANGE: z = "bind or column index out of range"; break;
drhc602f9a2004-02-12 19:01:04 +0000604 case SQLITE_NOTADB: z = "file is encrypted or is not a database";break;
drhc22bd472002-05-10 13:14:07 +0000605 default: z = "unknown error"; break;
drh247be432002-05-10 05:44:55 +0000606 }
drhc22bd472002-05-10 13:14:07 +0000607 return z;
drh247be432002-05-10 05:44:55 +0000608}
609
610/*
drh2dfbbca2000-07-28 14:32:48 +0000611** This routine implements a busy callback that sleeps and tries
612** again until a timeout value is reached. The timeout value is
613** an integer number of milliseconds passed in as the first
614** argument.
615*/
drhdaffd0e2001-04-11 14:28:42 +0000616static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000617 void *Timeout, /* Maximum amount of time to wait */
drh2dfbbca2000-07-28 14:32:48 +0000618 int count /* Number of times table has been busy */
619){
drh8cfbf082001-09-19 13:22:39 +0000620#if SQLITE_MIN_SLEEP_MS==1
drhee570fa2005-04-28 12:06:05 +0000621 static const u8 delays[] =
622 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
623 static const u8 totals[] =
624 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
drhd1bec472004-01-15 13:29:31 +0000625# define NDELAY (sizeof(delays)/sizeof(delays[0]))
drhc44af712004-09-02 15:53:56 +0000626 ptr timeout = (ptr)Timeout;
627 ptr delay, prior;
drh2dfbbca2000-07-28 14:32:48 +0000628
drhee570fa2005-04-28 12:06:05 +0000629 assert( count>=0 );
630 if( count < NDELAY ){
631 delay = delays[count];
632 prior = totals[count];
drhd1bec472004-01-15 13:29:31 +0000633 }else{
634 delay = delays[NDELAY-1];
635 prior = totals[NDELAY-1] + delay*(count-NDELAY-1);
drh2dfbbca2000-07-28 14:32:48 +0000636 }
drhd1bec472004-01-15 13:29:31 +0000637 if( prior + delay > timeout ){
638 delay = timeout - prior;
drh2dfbbca2000-07-28 14:32:48 +0000639 if( delay<=0 ) return 0;
640 }
danielk19774adee202004-05-08 08:23:19 +0000641 sqlite3OsSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000642 return 1;
643#else
644 int timeout = (int)Timeout;
645 if( (count+1)*1000 > timeout ){
646 return 0;
647 }
danielk19774adee202004-05-08 08:23:19 +0000648 sqlite3OsSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000649 return 1;
650#endif
651}
652
653/*
654** This routine sets the busy callback for an Sqlite database to the
655** given callback function with the given argument.
656*/
danielk1977f9d64d22004-06-19 08:18:07 +0000657int sqlite3_busy_handler(
658 sqlite3 *db,
danielk19772a764eb2004-06-12 01:43:26 +0000659 int (*xBusy)(void*,int),
drh2dfbbca2000-07-28 14:32:48 +0000660 void *pArg
661){
drhc60d0442004-09-30 13:43:13 +0000662 if( sqlite3SafetyCheck(db) ){
663 return SQLITE_MISUSE;
664 }
danielk197724162fe2004-06-04 06:22:00 +0000665 db->busyHandler.xFunc = xBusy;
666 db->busyHandler.pArg = pArg;
danielk1977f9d64d22004-06-19 08:18:07 +0000667 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000668}
669
danielk1977348bb5d2003-10-18 09:37:26 +0000670#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
671/*
672** This routine sets the progress callback for an Sqlite database to the
673** given callback function with the given argument. The progress callback will
674** be invoked every nOps opcodes.
675*/
danielk197724b03fd2004-05-10 10:34:34 +0000676void sqlite3_progress_handler(
drh9bb575f2004-09-06 17:24:11 +0000677 sqlite3 *db,
danielk1977348bb5d2003-10-18 09:37:26 +0000678 int nOps,
679 int (*xProgress)(void*),
680 void *pArg
681){
drhc60d0442004-09-30 13:43:13 +0000682 if( !sqlite3SafetyCheck(db) ){
683 if( nOps>0 ){
684 db->xProgress = xProgress;
685 db->nProgressOps = nOps;
686 db->pProgressArg = pArg;
687 }else{
688 db->xProgress = 0;
689 db->nProgressOps = 0;
690 db->pProgressArg = 0;
691 }
danielk1977348bb5d2003-10-18 09:37:26 +0000692 }
693}
694#endif
695
696
drh2dfbbca2000-07-28 14:32:48 +0000697/*
698** This routine installs a default busy handler that waits for the
699** specified number of milliseconds before returning 0.
700*/
danielk1977f9d64d22004-06-19 08:18:07 +0000701int sqlite3_busy_timeout(sqlite3 *db, int ms){
drh2dfbbca2000-07-28 14:32:48 +0000702 if( ms>0 ){
drhc44af712004-09-02 15:53:56 +0000703 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)(ptr)ms);
drh2dfbbca2000-07-28 14:32:48 +0000704 }else{
danielk197724b03fd2004-05-10 10:34:34 +0000705 sqlite3_busy_handler(db, 0, 0);
drh2dfbbca2000-07-28 14:32:48 +0000706 }
danielk1977f9d64d22004-06-19 08:18:07 +0000707 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000708}
drh4c504392000-10-16 22:06:40 +0000709
710/*
711** Cause any pending operation to stop at its earliest opportunity.
712*/
drh9bb575f2004-09-06 17:24:11 +0000713void sqlite3_interrupt(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000714 if( !sqlite3SafetyCheck(db) ){
715 db->flags |= SQLITE_Interrupt;
716 }
drh4c504392000-10-16 22:06:40 +0000717}
drhfa86c412002-02-02 15:01:15 +0000718
719/*
720** Windows systems should call this routine to free memory that
danielk197724b03fd2004-05-10 10:34:34 +0000721** is returned in the in the errmsg parameter of sqlite3_open() when
drhfa86c412002-02-02 15:01:15 +0000722** SQLite is a DLL. For some reason, it does not work to call free()
723** directly.
724**
drhae29ffb2004-09-25 14:39:18 +0000725** Note that we need to call free() not sqliteFree() here.
drhfa86c412002-02-02 15:01:15 +0000726*/
drh3f4fedb2004-05-31 19:34:33 +0000727void sqlite3_free(char *p){ free(p); }
drhfa86c412002-02-02 15:01:15 +0000728
729/*
drhdf014892004-06-02 00:41:09 +0000730** Create new user functions.
drhfa86c412002-02-02 15:01:15 +0000731*/
danielk197724b03fd2004-05-10 10:34:34 +0000732int sqlite3_create_function(
danielk197765904932004-05-26 06:18:37 +0000733 sqlite3 *db,
734 const char *zFunctionName,
735 int nArg,
danielk1977d8123362004-06-12 09:25:12 +0000736 int enc,
danielk197765904932004-05-26 06:18:37 +0000737 void *pUserData,
738 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
739 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
740 void (*xFinal)(sqlite3_context*)
drh8e0a2f92002-02-23 23:45:45 +0000741){
drh0bce8352002-02-28 00:41:10 +0000742 FuncDef *p;
drh4b59ab52002-08-24 18:24:51 +0000743 int nName;
danielk197765904932004-05-26 06:18:37 +0000744
drhc60d0442004-09-30 13:43:13 +0000745 if( sqlite3SafetyCheck(db) ){
746 return SQLITE_MISUSE;
747 }
748 if( zFunctionName==0 ||
danielk1977398eae72004-05-26 06:58:43 +0000749 (xFunc && (xFinal || xStep)) ||
750 (!xFunc && (xFinal && !xStep)) ||
751 (!xFunc && (!xFinal && xStep)) ||
752 (nArg<-1 || nArg>127) ||
753 (255<(nName = strlen(zFunctionName))) ){
danielk197765904932004-05-26 06:18:37 +0000754 return SQLITE_ERROR;
755 }
danielk1977d8123362004-06-12 09:25:12 +0000756
danielk197793758c82005-01-21 08:13:14 +0000757#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +0000758 /* If SQLITE_UTF16 is specified as the encoding type, transform this
759 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
760 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
761 **
762 ** If SQLITE_ANY is specified, add three versions of the function
763 ** to the hash table.
764 */
765 if( enc==SQLITE_UTF16 ){
766 enc = SQLITE_UTF16NATIVE;
767 }else if( enc==SQLITE_ANY ){
768 int rc;
769 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF8,
danielk1977f9d64d22004-06-19 08:18:07 +0000770 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000771 if( rc!=SQLITE_OK ) return rc;
772 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF16LE,
danielk1977f9d64d22004-06-19 08:18:07 +0000773 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000774 if( rc!=SQLITE_OK ) return rc;
775 enc = SQLITE_UTF16BE;
776 }
danielk197793758c82005-01-21 08:13:14 +0000777#else
778 enc = SQLITE_UTF8;
779#endif
danielk19779636c4e2005-01-25 04:27:54 +0000780
781 /* Check if an existing function is being overridden or deleted. If so,
782 ** and there are active VMs, then return SQLITE_BUSY. If a function
783 ** is being overridden/deleted but there are no active VMs, allow the
784 ** operation to continue but invalidate all precompiled statements.
785 */
786 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0);
787 if( p && p->iPrefEnc==enc && p->nArg==nArg ){
788 if( db->activeVdbeCnt ){
789 sqlite3Error(db, SQLITE_BUSY,
790 "Unable to delete/modify user-function due to active statements");
791 return SQLITE_BUSY;
792 }else{
793 sqlite3ExpirePreparedStatements(db);
794 }
795 }
danielk197765904932004-05-26 06:18:37 +0000796
danielk1977d8123362004-06-12 09:25:12 +0000797 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);
danielk197713073932004-06-30 11:54:06 +0000798 if( p==0 ) return SQLITE_NOMEM;
drh8e0a2f92002-02-23 23:45:45 +0000799 p->xFunc = xFunc;
drh8e0a2f92002-02-23 23:45:45 +0000800 p->xStep = xStep;
danielk197765904932004-05-26 06:18:37 +0000801 p->xFinalize = xFinal;
drh1350b032002-02-27 19:00:20 +0000802 p->pUserData = pUserData;
danielk197765904932004-05-26 06:18:37 +0000803 return SQLITE_OK;
804}
danielk197793758c82005-01-21 08:13:14 +0000805#ifndef SQLITE_OMIT_UTF16
danielk197765904932004-05-26 06:18:37 +0000806int sqlite3_create_function16(
807 sqlite3 *db,
808 const void *zFunctionName,
809 int nArg,
810 int eTextRep,
danielk197765904932004-05-26 06:18:37 +0000811 void *pUserData,
812 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
813 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
814 void (*xFinal)(sqlite3_context*)
815){
816 int rc;
danielk1977bfd6cce2004-06-18 04:24:54 +0000817 char const *zFunc8;
drhc60d0442004-09-30 13:43:13 +0000818 sqlite3_value *pTmp;
danielk1977bfd6cce2004-06-18 04:24:54 +0000819
drhc60d0442004-09-30 13:43:13 +0000820 if( sqlite3SafetyCheck(db) ){
821 return SQLITE_MISUSE;
822 }
823 pTmp = sqlite3GetTransientValue(db);
danielk1977bfd6cce2004-06-18 04:24:54 +0000824 sqlite3ValueSetStr(pTmp, -1, zFunctionName, SQLITE_UTF16NATIVE,SQLITE_STATIC);
825 zFunc8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
826
827 if( !zFunc8 ){
danielk197765904932004-05-26 06:18:37 +0000828 return SQLITE_NOMEM;
829 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000830 rc = sqlite3_create_function(db, zFunc8, nArg, eTextRep,
danielk1977f9d64d22004-06-19 08:18:07 +0000831 pUserData, xFunc, xStep, xFinal);
danielk197765904932004-05-26 06:18:37 +0000832 return rc;
drh8e0a2f92002-02-23 23:45:45 +0000833}
danielk197793758c82005-01-21 08:13:14 +0000834#endif
drhc9b84a12002-06-20 11:36:48 +0000835
836/*
drh18de4822003-01-16 16:28:53 +0000837** Register a trace function. The pArg from the previously registered trace
838** is returned.
839**
840** A NULL trace function means that no tracing is executes. A non-NULL
841** trace is a pointer to a function that is invoked at the start of each
danielk197724b03fd2004-05-10 10:34:34 +0000842** sqlite3_exec().
drh18de4822003-01-16 16:28:53 +0000843*/
drh9bb575f2004-09-06 17:24:11 +0000844void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
drh18de4822003-01-16 16:28:53 +0000845 void *pOld = db->pTraceArg;
846 db->xTrace = xTrace;
847 db->pTraceArg = pArg;
848 return pOld;
drh0d1a6432003-04-03 15:46:04 +0000849}
paulb0208cc2003-04-13 18:26:49 +0000850
drhaa940ea2004-01-15 02:44:03 +0000851/*** EXPERIMENTAL ***
852**
853** Register a function to be invoked when a transaction comments.
854** If either function returns non-zero, then the commit becomes a
855** rollback.
856*/
danielk197724b03fd2004-05-10 10:34:34 +0000857void *sqlite3_commit_hook(
drh9bb575f2004-09-06 17:24:11 +0000858 sqlite3 *db, /* Attach the hook to this database */
drhaa940ea2004-01-15 02:44:03 +0000859 int (*xCallback)(void*), /* Function to invoke on each commit */
860 void *pArg /* Argument to the function */
861){
862 void *pOld = db->pCommitArg;
863 db->xCommitCallback = xCallback;
864 db->pCommitArg = pArg;
865 return pOld;
866}
867
868
paulb0208cc2003-04-13 18:26:49 +0000869/*
drh13bff812003-04-15 01:19:47 +0000870** This routine is called to create a connection to a database BTree
871** driver. If zFilename is the name of a file, then that file is
872** opened and used. If zFilename is the magic name ":memory:" then
873** the database is stored in memory (and is thus forgotten as soon as
874** the connection is closed.) If zFilename is NULL then the database
875** is for temporary use only and is deleted as soon as the connection
876** is closed.
drh90f5ecb2004-07-22 01:19:35 +0000877**
878** A temporary database can be either a disk file (that is automatically
879** deleted when the file is closed) or a set of red-black trees held in memory,
880** depending on the values of the TEMP_STORE compile-time macro and the
881** db->temp_store variable, according to the following chart:
882**
883** TEMP_STORE db->temp_store Location of temporary database
884** ---------- -------------- ------------------------------
885** 0 any file
886** 1 1 file
887** 1 2 memory
888** 1 0 file
889** 2 1 file
890** 2 2 memory
891** 2 0 memory
892** 3 any memory
paulb0208cc2003-04-13 18:26:49 +0000893*/
danielk19774adee202004-05-08 08:23:19 +0000894int sqlite3BtreeFactory(
drh9bb575f2004-09-06 17:24:11 +0000895 const sqlite3 *db, /* Main database when opening aux otherwise 0 */
paulb0208cc2003-04-13 18:26:49 +0000896 const char *zFilename, /* Name of the file containing the BTree database */
897 int omitJournal, /* if TRUE then do not journal this file */
898 int nCache, /* How many pages in the page cache */
danielk19774adee202004-05-08 08:23:19 +0000899 Btree **ppBtree /* Pointer to new Btree object written here */
900){
danielk19774adee202004-05-08 08:23:19 +0000901 int btree_flags = 0;
drh90f5ecb2004-07-22 01:19:35 +0000902 int rc;
danielk19774adee202004-05-08 08:23:19 +0000903
drheec983e2004-05-08 10:11:36 +0000904 assert( ppBtree != 0);
danielk19774adee202004-05-08 08:23:19 +0000905 if( omitJournal ){
906 btree_flags |= BTREE_OMIT_JOURNAL;
paulb0208cc2003-04-13 18:26:49 +0000907 }
drh7bec5052005-02-06 02:45:41 +0000908 if( db->flags & SQLITE_NoReadlock ){
909 btree_flags |= BTREE_NO_READLOCK;
910 }
drh90f5ecb2004-07-22 01:19:35 +0000911 if( zFilename==0 ){
drh90f5ecb2004-07-22 01:19:35 +0000912#if TEMP_STORE==0
drh22ac46d2004-08-14 18:34:54 +0000913 /* Do nothing */
drh90f5ecb2004-07-22 01:19:35 +0000914#endif
danielk197703aded42004-11-22 05:26:27 +0000915#ifndef SQLITE_OMIT_MEMORYDB
drh90f5ecb2004-07-22 01:19:35 +0000916#if TEMP_STORE==1
drh22ac46d2004-08-14 18:34:54 +0000917 if( db->temp_store==2 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000918#endif
919#if TEMP_STORE==2
drh22ac46d2004-08-14 18:34:54 +0000920 if( db->temp_store!=1 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000921#endif
922#if TEMP_STORE==3
drh22ac46d2004-08-14 18:34:54 +0000923 zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000924#endif
danielk197703aded42004-11-22 05:26:27 +0000925#endif /* SQLITE_OMIT_MEMORYDB */
drh90f5ecb2004-07-22 01:19:35 +0000926 }
danielk19774adee202004-05-08 08:23:19 +0000927
drh90f5ecb2004-07-22 01:19:35 +0000928 rc = sqlite3BtreeOpen(zFilename, ppBtree, btree_flags);
929 if( rc==SQLITE_OK ){
930 sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler);
931 sqlite3BtreeSetCacheSize(*ppBtree, nCache);
932 }
933 return rc;
paulb0208cc2003-04-13 18:26:49 +0000934}
danielk19774adee202004-05-08 08:23:19 +0000935
danielk19774ad17132004-05-21 01:47:26 +0000936/*
937** Return UTF-8 encoded English language explanation of the most recent
938** error.
939*/
danielk19776622cce2004-05-20 11:00:52 +0000940const char *sqlite3_errmsg(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000941 const char *z;
942 if( sqlite3_malloc_failed ){
danielk1977f20b21c2004-05-31 23:56:42 +0000943 return sqlite3ErrStr(SQLITE_NOMEM);
danielk19774ad17132004-05-21 01:47:26 +0000944 }
drhc60d0442004-09-30 13:43:13 +0000945 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
danielk1977e35ee192004-06-26 09:50:11 +0000946 return sqlite3ErrStr(SQLITE_MISUSE);
947 }
drhc60d0442004-09-30 13:43:13 +0000948 z = sqlite3_value_text(db->pErr);
949 if( z==0 ){
950 z = sqlite3ErrStr(db->errCode);
danielk19776622cce2004-05-20 11:00:52 +0000951 }
drhc60d0442004-09-30 13:43:13 +0000952 return z;
danielk19776622cce2004-05-20 11:00:52 +0000953}
954
drh6c626082004-11-14 21:56:29 +0000955#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +0000956/*
957** Return UTF-16 encoded English language explanation of the most recent
958** error.
959*/
danielk19776622cce2004-05-20 11:00:52 +0000960const void *sqlite3_errmsg16(sqlite3 *db){
danielk1977bfd6cce2004-06-18 04:24:54 +0000961 /* Because all the characters in the string are in the unicode
962 ** range 0x00-0xFF, if we pad the big-endian string with a
963 ** zero byte, we can obtain the little-endian string with
964 ** &big_endian[1].
965 */
drh57196282004-10-06 15:41:16 +0000966 static const char outOfMemBe[] = {
danielk1977bfd6cce2004-06-18 04:24:54 +0000967 0, 'o', 0, 'u', 0, 't', 0, ' ',
968 0, 'o', 0, 'f', 0, ' ',
969 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0
970 };
drh57196282004-10-06 15:41:16 +0000971 static const char misuseBe [] = {
danielk1977e35ee192004-06-26 09:50:11 +0000972 0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ',
973 0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ',
974 0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ',
975 0, 'o', 0, 'u', 0, 't', 0, ' ',
976 0, 'o', 0, 'f', 0, ' ',
977 0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0
978 };
danielk19774ad17132004-05-21 01:47:26 +0000979
drhc60d0442004-09-30 13:43:13 +0000980 const void *z;
981 if( sqlite3_malloc_failed ){
982 return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
983 }
984 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
985 return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
986 }
987 z = sqlite3_value_text16(db->pErr);
988 if( z==0 ){
989 sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
990 SQLITE_UTF8, SQLITE_STATIC);
991 z = sqlite3_value_text16(db->pErr);
992 }
993 return z;
danielk19776622cce2004-05-20 11:00:52 +0000994}
drh6c626082004-11-14 21:56:29 +0000995#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +0000996
drhc60d0442004-09-30 13:43:13 +0000997/*
998** Return the most recent error code generated by an SQLite routine.
999*/
danielk19776622cce2004-05-20 11:00:52 +00001000int sqlite3_errcode(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +00001001 if( sqlite3_malloc_failed ){
1002 return SQLITE_NOMEM;
1003 }
1004 if( sqlite3SafetyCheck(db) ){
1005 return SQLITE_MISUSE;
1006 }
danielk19776622cce2004-05-20 11:00:52 +00001007 return db->errCode;
1008}
1009
1010/*
drhc275b4e2004-07-19 17:25:24 +00001011** Check schema cookies in all databases. If any cookie is out
drha6ecd332004-06-10 00:29:09 +00001012** of date, return 0. If all schema cookies are current, return 1.
1013*/
drh9bb575f2004-09-06 17:24:11 +00001014static int schemaIsValid(sqlite3 *db){
drha6ecd332004-06-10 00:29:09 +00001015 int iDb;
1016 int rc;
1017 BtCursor *curTemp;
1018 int cookie;
1019 int allOk = 1;
1020
1021 for(iDb=0; allOk && iDb<db->nDb; iDb++){
1022 Btree *pBt;
drha6ecd332004-06-10 00:29:09 +00001023 pBt = db->aDb[iDb].pBt;
1024 if( pBt==0 ) continue;
1025 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
1026 if( rc==SQLITE_OK ){
danielk1977e0d4b062004-06-28 01:11:46 +00001027 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
drha6ecd332004-06-10 00:29:09 +00001028 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].schema_cookie ){
1029 allOk = 0;
1030 }
1031 sqlite3BtreeCloseCursor(curTemp);
1032 }
1033 }
1034 return allOk;
1035}
1036
1037/*
danielk19776622cce2004-05-20 11:00:52 +00001038** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
1039*/
1040int sqlite3_prepare(
1041 sqlite3 *db, /* Database handle. */
1042 const char *zSql, /* UTF-8 encoded SQL statement. */
1043 int nBytes, /* Length of zSql in bytes. */
1044 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1045 const char** pzTail /* OUT: End of parsed string */
1046){
1047 Parse sParse;
1048 char *zErrMsg = 0;
1049 int rc = SQLITE_OK;
1050
danielk197796fb0dd2004-06-30 09:49:22 +00001051 if( sqlite3_malloc_failed ){
1052 return SQLITE_NOMEM;
1053 }
1054
danielk19775c4c7782004-06-16 10:39:23 +00001055 assert( ppStmt );
1056 *ppStmt = 0;
danielk19776622cce2004-05-20 11:00:52 +00001057 if( sqlite3SafetyOn(db) ){
danielk1977e35ee192004-06-26 09:50:11 +00001058 return SQLITE_MISUSE;
danielk19776622cce2004-05-20 11:00:52 +00001059 }
1060
danielk19776622cce2004-05-20 11:00:52 +00001061 memset(&sParse, 0, sizeof(sParse));
1062 sParse.db = db;
1063 sqlite3RunParser(&sParse, zSql, &zErrMsg);
1064
danielk19776622cce2004-05-20 11:00:52 +00001065 if( sqlite3_malloc_failed ){
1066 rc = SQLITE_NOMEM;
1067 sqlite3RollbackAll(db);
1068 sqlite3ResetInternalSchema(db, 0);
1069 db->flags &= ~SQLITE_InTrans;
1070 goto prepare_out;
1071 }
1072 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
drh3f7d4e42004-07-24 14:35:58 +00001073 if( sParse.rc!=SQLITE_OK && sParse.checkSchema && !schemaIsValid(db) ){
drha6ecd332004-06-10 00:29:09 +00001074 sParse.rc = SQLITE_SCHEMA;
1075 }
danielk19776622cce2004-05-20 11:00:52 +00001076 if( sParse.rc==SQLITE_SCHEMA ){
1077 sqlite3ResetInternalSchema(db, 0);
1078 }
danielk19776622cce2004-05-20 11:00:52 +00001079 if( pzTail ) *pzTail = sParse.zTail;
danielk19776622cce2004-05-20 11:00:52 +00001080 rc = sParse.rc;
1081
danielk197793758c82005-01-21 08:13:14 +00001082#ifndef SQLITE_OMIT_EXPLAIN
danielk197722322fd2004-05-25 23:35:17 +00001083 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
1084 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
danielk19773cf86062004-05-26 10:11:05 +00001085 sqlite3VdbeSetColName(sParse.pVdbe, 0, "addr", P3_STATIC);
1086 sqlite3VdbeSetColName(sParse.pVdbe, 1, "opcode", P3_STATIC);
1087 sqlite3VdbeSetColName(sParse.pVdbe, 2, "p1", P3_STATIC);
1088 sqlite3VdbeSetColName(sParse.pVdbe, 3, "p2", P3_STATIC);
1089 sqlite3VdbeSetColName(sParse.pVdbe, 4, "p3", P3_STATIC);
danielk197722322fd2004-05-25 23:35:17 +00001090 }
danielk197793758c82005-01-21 08:13:14 +00001091#endif
danielk197722322fd2004-05-25 23:35:17 +00001092
danielk19776622cce2004-05-20 11:00:52 +00001093prepare_out:
danielk197722322fd2004-05-25 23:35:17 +00001094 if( sqlite3SafetyOff(db) ){
1095 rc = SQLITE_MISUSE;
1096 }
danielk19775c4c7782004-06-16 10:39:23 +00001097 if( rc==SQLITE_OK ){
1098 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
1099 }else if( sParse.pVdbe ){
danielk1977cfe9a692004-06-16 12:00:29 +00001100 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
danielk19775c4c7782004-06-16 10:39:23 +00001101 }
1102
danielk19776622cce2004-05-20 11:00:52 +00001103 if( zErrMsg ){
1104 sqlite3Error(db, rc, "%s", zErrMsg);
danielk1977b20e56b2004-06-15 13:36:30 +00001105 sqliteFree(zErrMsg);
danielk19776622cce2004-05-20 11:00:52 +00001106 }else{
1107 sqlite3Error(db, rc, 0);
1108 }
1109 return rc;
1110}
1111
drh6c626082004-11-14 21:56:29 +00001112#ifndef SQLITE_OMIT_UTF16
danielk19776622cce2004-05-20 11:00:52 +00001113/*
1114** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
1115*/
1116int sqlite3_prepare16(
1117 sqlite3 *db, /* Database handle. */
1118 const void *zSql, /* UTF-8 encoded SQL statement. */
1119 int nBytes, /* Length of zSql in bytes. */
1120 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1121 const void **pzTail /* OUT: End of parsed string */
1122){
1123 /* This function currently works by first transforming the UTF-16
1124 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
1125 ** tricky bit is figuring out the pointer to return in *pzTail.
1126 */
danielk1977bfd6cce2004-06-18 04:24:54 +00001127 char const *zSql8 = 0;
danielk19776622cce2004-05-20 11:00:52 +00001128 char const *zTail8 = 0;
1129 int rc;
danielk1977bfd6cce2004-06-18 04:24:54 +00001130 sqlite3_value *pTmp;
danielk19776622cce2004-05-20 11:00:52 +00001131
drhc60d0442004-09-30 13:43:13 +00001132 if( sqlite3SafetyCheck(db) ){
1133 return SQLITE_MISUSE;
1134 }
danielk1977bfd6cce2004-06-18 04:24:54 +00001135 pTmp = sqlite3GetTransientValue(db);
1136 sqlite3ValueSetStr(pTmp, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1137 zSql8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
danielk19776622cce2004-05-20 11:00:52 +00001138 if( !zSql8 ){
1139 sqlite3Error(db, SQLITE_NOMEM, 0);
1140 return SQLITE_NOMEM;
1141 }
1142 rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);
1143
1144 if( zTail8 && pzTail ){
1145 /* If sqlite3_prepare returns a tail pointer, we calculate the
1146 ** equivalent pointer into the UTF-16 string by counting the unicode
1147 ** characters between zSql8 and zTail8, and then returning a pointer
1148 ** the same number of characters into the UTF-16 string.
1149 */
1150 int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8);
1151 *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed);
1152 }
1153
1154 return rc;
1155}
drh6c626082004-11-14 21:56:29 +00001156#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00001157
danielk19774ad17132004-05-21 01:47:26 +00001158/*
1159** This routine does the work of opening a database on behalf of
1160** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
drhee570fa2005-04-28 12:06:05 +00001161** is UTF-8 encoded.
danielk19774ad17132004-05-21 01:47:26 +00001162*/
1163static int openDatabase(
1164 const char *zFilename, /* Database filename UTF-8 encoded */
danielk19778e227872004-06-07 07:52:17 +00001165 sqlite3 **ppDb /* OUT: Returned database handle */
danielk19774ad17132004-05-21 01:47:26 +00001166){
1167 sqlite3 *db;
1168 int rc, i;
danielk19774ad17132004-05-21 01:47:26 +00001169
1170 /* Allocate the sqlite data structure */
drh9bb575f2004-09-06 17:24:11 +00001171 db = sqliteMalloc( sizeof(sqlite3) );
danielk19774ad17132004-05-21 01:47:26 +00001172 if( db==0 ) goto opendb_out;
danielk19774ad17132004-05-21 01:47:26 +00001173 db->priorNewRowid = 0;
1174 db->magic = SQLITE_MAGIC_BUSY;
1175 db->nDb = 2;
1176 db->aDb = db->aDbStatic;
danielk1977dc8453f2004-06-12 00:42:34 +00001177 db->enc = SQLITE_UTF8;
danielk19771d850a72004-05-31 08:26:49 +00001178 db->autoCommit = 1;
drh47a6db22005-01-18 16:02:40 +00001179 db->flags |= SQLITE_ShortColNames;
drhf9b596e2004-05-26 16:54:42 +00001180 sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
danielk19774ad17132004-05-21 01:47:26 +00001181 sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
1182 for(i=0; i<db->nDb; i++){
1183 sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
1184 sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
1185 sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);
1186 sqlite3HashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1);
1187 }
danielk19774ad17132004-05-21 01:47:26 +00001188
danielk19770202b292004-06-09 09:55:16 +00001189 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
1190 ** and UTF-16, so add a version for each to avoid any unnecessary
1191 ** conversions. The only error that can occur here is a malloc() failure.
1192 */
danielk19772c336542005-01-13 02:14:23 +00001193 if( sqlite3_create_collation(db, "BINARY", SQLITE_UTF8, 0,binCollFunc) ||
1194 sqlite3_create_collation(db, "BINARY", SQLITE_UTF16, 0,binCollFunc) ||
1195 !(db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0)) ){
danielk19770202b292004-06-09 09:55:16 +00001196 rc = db->errCode;
1197 assert( rc!=SQLITE_OK );
1198 db->magic = SQLITE_MAGIC_CLOSED;
1199 goto opendb_out;
1200 }
1201
1202 /* Also add a UTF-8 case-insensitive collation sequence. */
danielk1977466be562004-06-10 02:16:01 +00001203 sqlite3_create_collation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc);
danielk19770202b292004-06-09 09:55:16 +00001204
danielk19774ad17132004-05-21 01:47:26 +00001205 /* Open the backend database driver */
danielk19774ad17132004-05-21 01:47:26 +00001206 rc = sqlite3BtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt);
1207 if( rc!=SQLITE_OK ){
danielk19774ad17132004-05-21 01:47:26 +00001208 sqlite3Error(db, rc, 0);
1209 db->magic = SQLITE_MAGIC_CLOSED;
1210 goto opendb_out;
1211 }
danielk19774ad17132004-05-21 01:47:26 +00001212
danielk197753c0f742005-03-29 03:10:59 +00001213 /* The default safety_level for the main database is 'full'; for the temp
1214 ** database it is 'NONE'. This matches the pager layer defaults.
1215 */
1216 db->aDb[0].zName = "main";
danielk197791cf71b2004-06-26 06:37:06 +00001217 db->aDb[0].safety_level = 3;
danielk197753c0f742005-03-29 03:10:59 +00001218#ifndef SQLITE_OMIT_TEMPDB
1219 db->aDb[1].zName = "temp";
danielk197791cf71b2004-06-26 06:37:06 +00001220 db->aDb[1].safety_level = 1;
danielk197753c0f742005-03-29 03:10:59 +00001221#endif
1222
danielk197791cf71b2004-06-26 06:37:06 +00001223
danielk19778e227872004-06-07 07:52:17 +00001224 /* Register all built-in functions, but do not attempt to read the
1225 ** database schema yet. This is delayed until the first time the database
1226 ** is accessed.
1227 */
danielk19774ad17132004-05-21 01:47:26 +00001228 sqlite3RegisterBuiltinFunctions(db);
danielk19774397de52005-01-12 12:44:03 +00001229 sqlite3Error(db, SQLITE_OK, 0);
1230 db->magic = SQLITE_MAGIC_OPEN;
danielk19774ad17132004-05-21 01:47:26 +00001231
1232opendb_out:
danielk197713073932004-06-30 11:54:06 +00001233 if( sqlite3_errcode(db)==SQLITE_OK && sqlite3_malloc_failed ){
1234 sqlite3Error(db, SQLITE_NOMEM, 0);
1235 }
danielk19774ad17132004-05-21 01:47:26 +00001236 *ppDb = db;
danielk19776b456a22005-03-21 04:04:02 +00001237#ifndef SQLITE_OMIT_GLOBALRECOVER
1238 if( db ){
1239 sqlite3OsEnterMutex();
1240 db->pNext = pDbList;
1241 pDbList = db;
1242 sqlite3OsLeaveMutex();
1243 }
1244#endif
danielk19774ad17132004-05-21 01:47:26 +00001245 return sqlite3_errcode(db);
1246}
1247
1248/*
1249** Open a new database handle.
1250*/
danielk197780290862004-05-22 09:21:21 +00001251int sqlite3_open(
danielk19774ad17132004-05-21 01:47:26 +00001252 const char *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00001253 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00001254){
danielk19778e227872004-06-07 07:52:17 +00001255 return openDatabase(zFilename, ppDb);
danielk197783ab5a82004-05-21 11:39:05 +00001256}
1257
drh6c626082004-11-14 21:56:29 +00001258#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +00001259/*
1260** Open a new database handle.
1261*/
1262int sqlite3_open16(
1263 const void *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00001264 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00001265){
danielk1977bfd6cce2004-06-18 04:24:54 +00001266 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
1267 int rc = SQLITE_NOMEM;
1268 sqlite3_value *pVal;
danielk19774ad17132004-05-21 01:47:26 +00001269
1270 assert( ppDb );
danielk1977bfd6cce2004-06-18 04:24:54 +00001271 *ppDb = 0;
1272 pVal = sqlite3ValueNew();
1273 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1274 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
1275 if( zFilename8 ){
1276 rc = openDatabase(zFilename8, ppDb);
1277 if( rc==SQLITE_OK && *ppDb ){
1278 sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
1279 }
danielk19774ad17132004-05-21 01:47:26 +00001280 }
danielk1977bfd6cce2004-06-18 04:24:54 +00001281 if( pVal ){
1282 sqlite3ValueFree(pVal);
danielk19774ad17132004-05-21 01:47:26 +00001283 }
danielk19778e227872004-06-07 07:52:17 +00001284
danielk19774ad17132004-05-21 01:47:26 +00001285 return rc;
1286}
drh6c626082004-11-14 21:56:29 +00001287#endif /* SQLITE_OMIT_UTF16 */
danielk19774ad17132004-05-21 01:47:26 +00001288
danielk1977106bb232004-05-21 10:08:53 +00001289/*
1290** The following routine destroys a virtual machine that is created by
1291** the sqlite3_compile() routine. The integer returned is an SQLITE_
1292** success/failure code that describes the result of executing the virtual
1293** machine.
1294**
1295** This routine sets the error code and string returned by
1296** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
1297*/
danielk1977fc57d7b2004-05-26 02:04:57 +00001298int sqlite3_finalize(sqlite3_stmt *pStmt){
drh1bcdb0c2004-08-28 14:49:46 +00001299 int rc;
1300 if( pStmt==0 ){
1301 rc = SQLITE_OK;
1302 }else{
1303 rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
1304 }
1305 return rc;
danielk1977106bb232004-05-21 10:08:53 +00001306}
1307
1308/*
1309** Terminate the current execution of an SQL statement and reset it
1310** back to its starting state so that it can be reused. A success code from
1311** the prior execution is returned.
1312**
1313** This routine sets the error code and string returned by
1314** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
1315*/
danielk1977fc57d7b2004-05-26 02:04:57 +00001316int sqlite3_reset(sqlite3_stmt *pStmt){
drh1bcdb0c2004-08-28 14:49:46 +00001317 int rc;
1318 if( pStmt==0 ){
1319 rc = SQLITE_OK;
1320 }else{
1321 rc = sqlite3VdbeReset((Vdbe*)pStmt);
danielk1977b3bce662005-01-29 08:32:43 +00001322 sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0, 0, 0, 0);
drh1bcdb0c2004-08-28 14:49:46 +00001323 }
danielk1977106bb232004-05-21 10:08:53 +00001324 return rc;
1325}
danielk19770202b292004-06-09 09:55:16 +00001326
danielk1977d8123362004-06-12 09:25:12 +00001327/*
1328** Register a new collation sequence with the database handle db.
1329*/
danielk19770202b292004-06-09 09:55:16 +00001330int sqlite3_create_collation(
1331 sqlite3* db,
1332 const char *zName,
danielk1977466be562004-06-10 02:16:01 +00001333 int enc,
danielk19770202b292004-06-09 09:55:16 +00001334 void* pCtx,
1335 int(*xCompare)(void*,int,const void*,int,const void*)
1336){
1337 CollSeq *pColl;
1338 int rc = SQLITE_OK;
danielk1977d8123362004-06-12 09:25:12 +00001339
drhc60d0442004-09-30 13:43:13 +00001340 if( sqlite3SafetyCheck(db) ){
1341 return SQLITE_MISUSE;
1342 }
1343
danielk1977d8123362004-06-12 09:25:12 +00001344 /* If SQLITE_UTF16 is specified as the encoding type, transform this
1345 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
1346 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
1347 */
1348 if( enc==SQLITE_UTF16 ){
1349 enc = SQLITE_UTF16NATIVE;
1350 }
1351
danielk1977466be562004-06-10 02:16:01 +00001352 if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16LE && enc!=SQLITE_UTF16BE ){
1353 sqlite3Error(db, SQLITE_ERROR,
1354 "Param 3 to sqlite3_create_collation() must be one of "
danielk1977d8123362004-06-12 09:25:12 +00001355 "SQLITE_UTF8, SQLITE_UTF16, SQLITE_UTF16LE or SQLITE_UTF16BE"
danielk1977466be562004-06-10 02:16:01 +00001356 );
1357 return SQLITE_ERROR;
1358 }
danielk1977a21c6b62005-01-24 10:25:59 +00001359
danielk19779636c4e2005-01-25 04:27:54 +00001360 /* Check if this call is removing or replacing an existing collation
1361 ** sequence. If so, and there are active VMs, return busy. If there
1362 ** are no active VMs, invalidate any pre-compiled statements.
danielk1977a21c6b62005-01-24 10:25:59 +00001363 */
danielk19779636c4e2005-01-25 04:27:54 +00001364 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 0);
1365 if( pColl && pColl->xCmp ){
1366 if( db->activeVdbeCnt ){
1367 sqlite3Error(db, SQLITE_BUSY,
1368 "Unable to delete/modify collation sequence due to active statements");
1369 return SQLITE_BUSY;
1370 }
danielk1977a21c6b62005-01-24 10:25:59 +00001371 sqlite3ExpirePreparedStatements(db);
1372 }
1373
danielk1977466be562004-06-10 02:16:01 +00001374 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 1);
danielk19770202b292004-06-09 09:55:16 +00001375 if( 0==pColl ){
1376 rc = SQLITE_NOMEM;
danielk19770202b292004-06-09 09:55:16 +00001377 }else{
1378 pColl->xCmp = xCompare;
1379 pColl->pUser = pCtx;
danielk1977312d6b32004-06-29 13:18:23 +00001380 pColl->enc = enc;
danielk19770202b292004-06-09 09:55:16 +00001381 }
1382 sqlite3Error(db, rc, 0);
danielk1977466be562004-06-10 02:16:01 +00001383 return rc;
danielk19770202b292004-06-09 09:55:16 +00001384}
1385
drh6c626082004-11-14 21:56:29 +00001386#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00001387/*
1388** Register a new collation sequence with the database handle db.
1389*/
danielk19770202b292004-06-09 09:55:16 +00001390int sqlite3_create_collation16(
1391 sqlite3* db,
1392 const char *zName,
danielk1977466be562004-06-10 02:16:01 +00001393 int enc,
danielk19770202b292004-06-09 09:55:16 +00001394 void* pCtx,
1395 int(*xCompare)(void*,int,const void*,int,const void*)
1396){
danielk1977bfd6cce2004-06-18 04:24:54 +00001397 char const *zName8;
drhc60d0442004-09-30 13:43:13 +00001398 sqlite3_value *pTmp;
1399 if( sqlite3SafetyCheck(db) ){
1400 return SQLITE_MISUSE;
1401 }
1402 pTmp = sqlite3GetTransientValue(db);
danielk1977bfd6cce2004-06-18 04:24:54 +00001403 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1404 zName8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
1405 return sqlite3_create_collation(db, zName8, enc, pCtx, xCompare);
danielk19770202b292004-06-09 09:55:16 +00001406}
drh6c626082004-11-14 21:56:29 +00001407#endif /* SQLITE_OMIT_UTF16 */
danielk19777cedc8d2004-06-10 10:50:08 +00001408
danielk1977d8123362004-06-12 09:25:12 +00001409/*
1410** Register a collation sequence factory callback with the database handle
1411** db. Replace any previously installed collation sequence factory.
1412*/
danielk19777cedc8d2004-06-10 10:50:08 +00001413int sqlite3_collation_needed(
1414 sqlite3 *db,
1415 void *pCollNeededArg,
1416 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
1417){
drhc60d0442004-09-30 13:43:13 +00001418 if( sqlite3SafetyCheck(db) ){
1419 return SQLITE_MISUSE;
1420 }
danielk19777cedc8d2004-06-10 10:50:08 +00001421 db->xCollNeeded = xCollNeeded;
1422 db->xCollNeeded16 = 0;
1423 db->pCollNeededArg = pCollNeededArg;
1424 return SQLITE_OK;
1425}
danielk1977d8123362004-06-12 09:25:12 +00001426
drh6c626082004-11-14 21:56:29 +00001427#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00001428/*
1429** Register a collation sequence factory callback with the database handle
1430** db. Replace any previously installed collation sequence factory.
1431*/
danielk19777cedc8d2004-06-10 10:50:08 +00001432int sqlite3_collation_needed16(
1433 sqlite3 *db,
1434 void *pCollNeededArg,
1435 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
1436){
drhc60d0442004-09-30 13:43:13 +00001437 if( sqlite3SafetyCheck(db) ){
1438 return SQLITE_MISUSE;
1439 }
danielk19777cedc8d2004-06-10 10:50:08 +00001440 db->xCollNeeded = 0;
1441 db->xCollNeeded16 = xCollNeeded16;
1442 db->pCollNeededArg = pCollNeededArg;
1443 return SQLITE_OK;
1444}
drh6c626082004-11-14 21:56:29 +00001445#endif /* SQLITE_OMIT_UTF16 */
danielk19776b456a22005-03-21 04:04:02 +00001446
1447#ifndef SQLITE_OMIT_GLOBALRECOVER
1448/*
1449** This function is called to recover from a malloc failure that occured
1450** within SQLite.
1451**
1452** This function is *not* threadsafe. Calling this from within a threaded
1453** application when threads other than the caller have used SQLite is
1454** dangerous and will almost certainly result in malfunctions.
1455*/
1456int sqlite3_global_recover(){
1457 int rc = SQLITE_OK;
1458
1459 if( sqlite3_malloc_failed ){
1460 sqlite3 *db;
1461 int i;
1462 sqlite3_malloc_failed = 0;
1463 for(db=pDbList; db; db=db->pNext ){
1464 sqlite3ExpirePreparedStatements(db);
1465 for(i=0; i<db->nDb; i++){
1466 Btree *pBt = db->aDb[i].pBt;
1467 if( pBt && (rc=sqlite3BtreeReset(pBt)) ){
1468 goto recover_out;
1469 }
1470 }
1471 db->autoCommit = 1;
1472 }
1473 }
1474
1475recover_out:
1476 if( rc!=SQLITE_OK ){
1477 sqlite3_malloc_failed = 1;
1478 }
1479 return rc;
1480}
1481#endif