blob: 445a0889f04cbd15992ad9f8f91a113dd7adf145 [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**
drh97903fe2005-05-24 20:19:57 +000017** $Id: main.c,v 1.290 2005/05/24 20:19:59 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
danielk1977fd9a0a42005-05-24 12:01:00 +000050#ifndef SQLITE_OMIT_UTF16
51/*
52** Return the transient sqlite3_value object used for encoding conversions
53** during SQL compilation.
54*/
55sqlite3_value *sqlite3GetTransientValue(sqlite3 *db){
56 if( !db->pValue ){
57 db->pValue = sqlite3ValueNew();
58 }
59 return db->pValue;
60}
61#endif
62
drhc2311722002-07-19 17:46:38 +000063/*
drh75897232000-05-29 14:26:00 +000064** This is the callback routine for the code that initializes the
danielk19774adee202004-05-08 08:23:19 +000065** database. See sqlite3Init() below for additional information.
drh234c39d2004-07-24 03:30:47 +000066** This routine is also called from the OP_ParseSchema opcode of the VDBE.
drh382c0242001-10-06 16:33:02 +000067**
68** Each callback contains the following information:
drh28037572000-08-02 13:47:41 +000069**
drh234c39d2004-07-24 03:30:47 +000070** argv[0] = name of thing being created
71** argv[1] = root page number for table or index. NULL for trigger or view.
72** argv[2] = SQL text for the CREATE statement.
73** argv[3] = "1" for temporary files, "0" for main database, "2" or more
drh1c2d8412003-03-31 00:30:47 +000074** for auxiliary database files.
drhd78eeee2001-09-13 16:18:53 +000075**
drh75897232000-05-29 14:26:00 +000076*/
danielk19774adee202004-05-08 08:23:19 +000077int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){
drhc2311722002-07-19 17:46:38 +000078 InitData *pData = (InitData*)pInit;
drh9bb575f2004-09-06 17:24:11 +000079 sqlite3 *db = pData->db;
drh234c39d2004-07-24 03:30:47 +000080 int iDb;
drh75897232000-05-29 14:26:00 +000081
drh234c39d2004-07-24 03:30:47 +000082 assert( argc==4 );
drh98e3e602003-07-27 17:26:22 +000083 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
drh234c39d2004-07-24 03:30:47 +000084 if( argv[1]==0 || argv[3]==0 ){
drh1d85d932004-02-14 23:05:52 +000085 corruptSchema(pData, 0);
drh8bf8dc92003-05-17 17:35:10 +000086 return 1;
87 }
drh234c39d2004-07-24 03:30:47 +000088 iDb = atoi(argv[3]);
89 assert( iDb>=0 && iDb<db->nDb );
90 if( argv[2] && argv[2][0] ){
91 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
92 ** But because db->init.busy is set to 1, no VDBE code is generated
93 ** or executed. All the parser does is build the internal data
94 ** structures that describe the table, index, or view.
95 */
96 char *zErr;
97 int rc;
98 assert( db->init.busy );
99 db->init.iDb = iDb;
100 db->init.newTnum = atoi(argv[1]);
101 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
102 db->init.iDb = 0;
103 if( SQLITE_OK!=rc ){
104 corruptSchema(pData, zErr);
105 sqlite3_free(zErr);
106 return rc;
drhd78eeee2001-09-13 16:18:53 +0000107 }
drh234c39d2004-07-24 03:30:47 +0000108 }else{
109 /* If the SQL column is blank it means this is an index that
110 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
111 ** constraint for a CREATE TABLE. The index should have already
112 ** been created when we processed the CREATE TABLE. All we have
113 ** to do here is record the root page number for that index.
114 */
115 Index *pIndex;
116 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
117 if( pIndex==0 || pIndex->tnum!=0 ){
118 /* This can occur if there exists an index on a TEMP table which
119 ** has the same name as another index on a permanent index. Since
120 ** the permanent table is hidden by the TEMP table, we can also
121 ** safely ignore the index on the permanent table.
122 */
123 /* Do Nothing */;
124 }else{
125 pIndex->tnum = atoi(argv[1]);
drhd78eeee2001-09-13 16:18:53 +0000126 }
drh28037572000-08-02 13:47:41 +0000127 }
drh234c39d2004-07-24 03:30:47 +0000128 return 0;
drh75897232000-05-29 14:26:00 +0000129}
130
131/*
drh58b95762000-06-02 01:17:37 +0000132** Attempt to read the database schema and initialize internal
drh1c2d8412003-03-31 00:30:47 +0000133** data structures for a single database file. The index of the
134** database file is given by iDb. iDb==0 is used for the main
135** database. iDb==1 should never be used. iDb>=2 is used for
136** auxiliary databases. Return one of the SQLITE_ error codes to
drh58b95762000-06-02 01:17:37 +0000137** indicate success or failure.
drh75897232000-05-29 14:26:00 +0000138*/
drh9bb575f2004-09-06 17:24:11 +0000139static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
drh58b95762000-06-02 01:17:37 +0000140 int rc;
drhe0bc4042002-06-25 01:09:11 +0000141 BtCursor *curMain;
142 int size;
143 Table *pTab;
drh234c39d2004-07-24 03:30:47 +0000144 char const *azArg[5];
drh1c2d8412003-03-31 00:30:47 +0000145 char zDbNum[30];
drha3b321d2004-05-11 09:31:31 +0000146 int meta[10];
drhc2311722002-07-19 17:46:38 +0000147 InitData initData;
danielk1977d008cfe2004-06-19 02:22:10 +0000148 char const *zMasterSchema;
danielk197753c0f742005-03-29 03:10:59 +0000149 char const *zMasterName = SCHEMA_TABLE(iDb);
drh58b95762000-06-02 01:17:37 +0000150
151 /*
152 ** The master database table has a structure like this
153 */
drh57196282004-10-06 15:41:16 +0000154 static const char master_schema[] =
drhe0bc4042002-06-25 01:09:11 +0000155 "CREATE TABLE sqlite_master(\n"
156 " type text,\n"
157 " name text,\n"
158 " tbl_name text,\n"
159 " rootpage integer,\n"
160 " sql text\n"
161 ")"
162 ;
danielk197753c0f742005-03-29 03:10:59 +0000163#ifndef SQLITE_OMIT_TEMPDB
drh57196282004-10-06 15:41:16 +0000164 static const char temp_master_schema[] =
drhe0bc4042002-06-25 01:09:11 +0000165 "CREATE TEMP TABLE sqlite_temp_master(\n"
drh75897232000-05-29 14:26:00 +0000166 " type text,\n"
167 " name text,\n"
168 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000169 " rootpage integer,\n"
drh75897232000-05-29 14:26:00 +0000170 " sql text\n"
171 ")"
172 ;
danielk197753c0f742005-03-29 03:10:59 +0000173#else
174 #define temp_master_schema 0
175#endif
drh75897232000-05-29 14:26:00 +0000176
danielk1977d008cfe2004-06-19 02:22:10 +0000177 assert( iDb>=0 && iDb<db->nDb );
drh603240c2002-03-05 01:11:12 +0000178
danielk1977d008cfe2004-06-19 02:22:10 +0000179 /* zMasterSchema and zInitScript are set to point at the master schema
180 ** and initialisation script appropriate for the database being
181 ** initialised. zMasterName is the name of the master table.
drh58b95762000-06-02 01:17:37 +0000182 */
danielk197753c0f742005-03-29 03:10:59 +0000183 if( !OMIT_TEMPDB && iDb==1 ){
danielk1977d008cfe2004-06-19 02:22:10 +0000184 zMasterSchema = temp_master_schema;
danielk1977d008cfe2004-06-19 02:22:10 +0000185 }else{
186 zMasterSchema = master_schema;
danielk1977d008cfe2004-06-19 02:22:10 +0000187 }
danielk197753c0f742005-03-29 03:10:59 +0000188 zMasterName = SCHEMA_TABLE(iDb);
danielk1977d008cfe2004-06-19 02:22:10 +0000189
190 /* Construct the schema tables. */
danielk19774adee202004-05-08 08:23:19 +0000191 sqlite3SafetyOff(db);
drh234c39d2004-07-24 03:30:47 +0000192 azArg[0] = zMasterName;
193 azArg[1] = "1";
194 azArg[2] = zMasterSchema;
drh1c2d8412003-03-31 00:30:47 +0000195 sprintf(zDbNum, "%d", iDb);
drh234c39d2004-07-24 03:30:47 +0000196 azArg[3] = zDbNum;
197 azArg[4] = 0;
drhc2311722002-07-19 17:46:38 +0000198 initData.db = db;
199 initData.pzErrMsg = pzErrMsg;
drh234c39d2004-07-24 03:30:47 +0000200 rc = sqlite3InitCallback(&initData, 4, (char **)azArg, 0);
danielk19772b444852004-06-29 07:45:33 +0000201 if( rc!=SQLITE_OK ){
202 sqlite3SafetyOn(db);
203 return rc;
204 }
danielk1977d008cfe2004-06-19 02:22:10 +0000205 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
drhe0bc4042002-06-25 01:09:11 +0000206 if( pTab ){
207 pTab->readOnly = 1;
drhd8bc7082000-06-07 23:51:50 +0000208 }
danielk19774adee202004-05-08 08:23:19 +0000209 sqlite3SafetyOn(db);
drhe0bc4042002-06-25 01:09:11 +0000210
211 /* Create a cursor to hold the database open
212 */
drhdc3ff9c2004-08-18 02:10:15 +0000213 if( db->aDb[iDb].pBt==0 ){
danielk197753c0f742005-03-29 03:10:59 +0000214 if( !OMIT_TEMPDB && iDb==1 ) DbSetProperty(db, 1, DB_SchemaLoaded);
drhdc3ff9c2004-08-18 02:10:15 +0000215 return SQLITE_OK;
216 }
danielk19778e150812004-05-10 01:17:37 +0000217 rc = sqlite3BtreeCursor(db->aDb[iDb].pBt, MASTER_ROOT, 0, 0, 0, &curMain);
drhf328bc82004-05-10 23:29:49 +0000218 if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
danielk1977f20b21c2004-05-31 23:56:42 +0000219 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
drh92ed08a2002-07-30 18:43:40 +0000220 return rc;
221 }
drhe0bc4042002-06-25 01:09:11 +0000222
drha3b321d2004-05-11 09:31:31 +0000223 /* Get the database meta information.
224 **
225 ** Meta values are as follows:
226 ** meta[0] Schema cookie. Changes with each schema change.
227 ** meta[1] File format of schema layer.
228 ** meta[2] Size of the page cache.
drhae157872004-08-14 19:20:09 +0000229 ** meta[3] Use freelist if 0. Autovacuum if greater than zero.
danielk1977dc8453f2004-06-12 00:42:34 +0000230 ** meta[4] Db text encoding. 1:UTF-8 3:UTF-16 LE 4:UTF-16 BE
danielk1977dae24952004-11-11 05:10:43 +0000231 ** meta[5] The user cookie. Used by the application.
danielk1977962398d2004-06-14 09:35:16 +0000232 ** meta[6]
drha3b321d2004-05-11 09:31:31 +0000233 ** meta[7]
234 ** meta[8]
235 ** meta[9]
danielk1977172bc392004-05-22 08:09:11 +0000236 **
danielk1977dc8453f2004-06-12 00:42:34 +0000237 ** Note: The hash defined SQLITE_UTF* symbols in sqliteInt.h correspond to
danielk1977172bc392004-05-22 08:09:11 +0000238 ** the possible values of meta[4].
drhe0bc4042002-06-25 01:09:11 +0000239 */
drhf328bc82004-05-10 23:29:49 +0000240 if( rc==SQLITE_OK ){
241 int i;
drha3b321d2004-05-11 09:31:31 +0000242 for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
danielk1977e0d4b062004-06-28 01:11:46 +0000243 rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, i+1, (u32 *)&meta[i]);
danielk19774adee202004-05-08 08:23:19 +0000244 }
drhf328bc82004-05-10 23:29:49 +0000245 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000246 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
drhf328bc82004-05-10 23:29:49 +0000247 sqlite3BtreeCloseCursor(curMain);
248 return rc;
249 }
250 }else{
251 memset(meta, 0, sizeof(meta));
drhe0bc4042002-06-25 01:09:11 +0000252 }
drha3b321d2004-05-11 09:31:31 +0000253 db->aDb[iDb].schema_cookie = meta[0];
danielk19773df6b252004-05-29 10:23:19 +0000254
255 /* If opening a non-empty database, check the text encoding. For the
256 ** main database, set sqlite3.enc to the encoding of the main database.
257 ** For an attached db, it is an error if the encoding is not the same
258 ** as sqlite3.enc.
259 */
260 if( meta[4] ){ /* text encoding */
261 if( iDb==0 ){
262 /* If opening the main database, set db->enc. */
danielk1977172bc392004-05-22 08:09:11 +0000263 db->enc = (u8)meta[4];
danielk1977466be562004-06-10 02:16:01 +0000264 db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0);
danielk19773df6b252004-05-29 10:23:19 +0000265 }else{
266 /* If opening an attached database, the encoding much match db->enc */
267 if( meta[4]!=db->enc ){
268 sqlite3BtreeCloseCursor(curMain);
269 sqlite3SetString(pzErrMsg, "attached databases must use the same"
270 " text encoding as main database", (char*)0);
271 return SQLITE_ERROR;
272 }
danielk1977172bc392004-05-22 08:09:11 +0000273 }
danielk19773df6b252004-05-29 10:23:19 +0000274 }
275
danielk197791cf71b2004-06-26 06:37:06 +0000276 size = meta[2];
277 if( size==0 ){ size = MAX_PAGES; }
278 db->aDb[iDb].cache_size = size;
drhe0bc4042002-06-25 01:09:11 +0000279
danielk197791cf71b2004-06-26 06:37:06 +0000280 if( iDb==0 ){
danielk19773df6b252004-05-29 10:23:19 +0000281 db->file_format = meta[1];
drh1c2d8412003-03-31 00:30:47 +0000282 if( db->file_format==0 ){
283 /* This happens if the database was initially empty */
drhf328bc82004-05-10 23:29:49 +0000284 db->file_format = 1;
drh1c2d8412003-03-31 00:30:47 +0000285 }
danielk197736963fd2005-02-19 08:18:05 +0000286
danielk1977aee18ef2005-03-09 12:26:50 +0000287 if( db->file_format==2 || db->file_format==3 ){
danielk197736963fd2005-02-19 08:18:05 +0000288 /* File format 2 is treated exactly as file format 1. New
289 ** databases are created with file format 1.
290 */
291 db->file_format = 1;
292 }
drh28037572000-08-02 13:47:41 +0000293 }
danielk19773df6b252004-05-29 10:23:19 +0000294
295 /*
danielk197736963fd2005-02-19 08:18:05 +0000296 ** file_format==1 Version 3.0.0.
297 ** file_format==2 Version 3.1.3.
danielk1977aee18ef2005-03-09 12:26:50 +0000298 ** file_format==3 Version 3.1.4.
danielk197736963fd2005-02-19 08:18:05 +0000299 **
300 ** Version 3.0 can only use files with file_format==1. Version 3.1.3
301 ** can read and write files with file_format==1 or file_format==2.
danielk1977aee18ef2005-03-09 12:26:50 +0000302 ** Version 3.1.4 can read and write file formats 1, 2 and 3.
danielk19773df6b252004-05-29 10:23:19 +0000303 */
danielk1977aee18ef2005-03-09 12:26:50 +0000304 if( meta[1]>3 ){
danielk19773df6b252004-05-29 10:23:19 +0000305 sqlite3BtreeCloseCursor(curMain);
306 sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
307 return SQLITE_ERROR;
308 }
309
danielk197791cf71b2004-06-26 06:37:06 +0000310 sqlite3BtreeSetCacheSize(db->aDb[iDb].pBt, db->aDb[iDb].cache_size);
drhaacc5432002-01-06 17:07:40 +0000311
drhe0bc4042002-06-25 01:09:11 +0000312 /* Read the schema information out of the schema tables
drhaacc5432002-01-06 17:07:40 +0000313 */
drh1d85d932004-02-14 23:05:52 +0000314 assert( db->init.busy );
drhf328bc82004-05-10 23:29:49 +0000315 if( rc==SQLITE_EMPTY ){
316 /* For an empty database, there is nothing to read */
317 rc = SQLITE_OK;
drh1c2d8412003-03-31 00:30:47 +0000318 }else{
drh234c39d2004-07-24 03:30:47 +0000319 char *zSql;
320 zSql = sqlite3MPrintf(
danielk1977c60e9b82005-01-31 12:42:29 +0000321 "SELECT name, rootpage, sql, '%s' FROM '%q'.%s",
drh234c39d2004-07-24 03:30:47 +0000322 zDbNum, db->aDb[iDb].zName, zMasterName);
danielk19773df6b252004-05-29 10:23:19 +0000323 sqlite3SafetyOff(db);
danielk1977d008cfe2004-06-19 02:22:10 +0000324 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
drhf328bc82004-05-10 23:29:49 +0000325 sqlite3SafetyOn(db);
drh234c39d2004-07-24 03:30:47 +0000326 sqliteFree(zSql);
drhf328bc82004-05-10 23:29:49 +0000327 sqlite3BtreeCloseCursor(curMain);
drh1c2d8412003-03-31 00:30:47 +0000328 }
danielk197724b03fd2004-05-10 10:34:34 +0000329 if( sqlite3_malloc_failed ){
danielk19774adee202004-05-08 08:23:19 +0000330 sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
drh1d85d932004-02-14 23:05:52 +0000331 rc = SQLITE_NOMEM;
danielk19774adee202004-05-08 08:23:19 +0000332 sqlite3ResetInternalSchema(db, 0);
drhe0bc4042002-06-25 01:09:11 +0000333 }
drh1d85d932004-02-14 23:05:52 +0000334 if( rc==SQLITE_OK ){
drh8bf8dc92003-05-17 17:35:10 +0000335 DbSetProperty(db, iDb, DB_SchemaLoaded);
drh1c2d8412003-03-31 00:30:47 +0000336 }else{
danielk19774adee202004-05-08 08:23:19 +0000337 sqlite3ResetInternalSchema(db, iDb);
drh1c2d8412003-03-31 00:30:47 +0000338 }
drh1d85d932004-02-14 23:05:52 +0000339 return rc;
drh1c2d8412003-03-31 00:30:47 +0000340}
341
342/*
343** Initialize all database files - the main database file, the file
344** used to store temporary tables, and any additional database files
345** created using ATTACH statements. Return a success code. If an
346** error occurs, write an error message into *pzErrMsg.
347**
348** After the database is initialized, the SQLITE_Initialized
danielk19778e227872004-06-07 07:52:17 +0000349** bit is set in the flags field of the sqlite structure.
drh1c2d8412003-03-31 00:30:47 +0000350*/
drh9bb575f2004-09-06 17:24:11 +0000351int sqlite3Init(sqlite3 *db, char **pzErrMsg){
drh1c2d8412003-03-31 00:30:47 +0000352 int i, rc;
353
drh1d85d932004-02-14 23:05:52 +0000354 if( db->init.busy ) return SQLITE_OK;
drh1c2d8412003-03-31 00:30:47 +0000355 assert( (db->flags & SQLITE_Initialized)==0 );
356 rc = SQLITE_OK;
drh1d85d932004-02-14 23:05:52 +0000357 db->init.busy = 1;
drh1c2d8412003-03-31 00:30:47 +0000358 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk1977d008cfe2004-06-19 02:22:10 +0000359 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
danielk19774adee202004-05-08 08:23:19 +0000360 rc = sqlite3InitOne(db, i, pzErrMsg);
drh8ef83ff2004-02-12 15:31:21 +0000361 if( rc ){
danielk19774adee202004-05-08 08:23:19 +0000362 sqlite3ResetInternalSchema(db, i);
drh8ef83ff2004-02-12 15:31:21 +0000363 }
drh1c2d8412003-03-31 00:30:47 +0000364 }
danielk1977d008cfe2004-06-19 02:22:10 +0000365
366 /* Once all the other databases have been initialised, load the schema
367 ** for the TEMP database. This is loaded last, as the TEMP database
368 ** schema may contain references to objects in other databases.
369 */
danielk197753c0f742005-03-29 03:10:59 +0000370#ifndef SQLITE_OMIT_TEMPDB
danielk1977d008cfe2004-06-19 02:22:10 +0000371 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
372 rc = sqlite3InitOne(db, 1, pzErrMsg);
373 if( rc ){
374 sqlite3ResetInternalSchema(db, 1);
375 }
376 }
danielk197753c0f742005-03-29 03:10:59 +0000377#endif
danielk1977d008cfe2004-06-19 02:22:10 +0000378
drh1d85d932004-02-14 23:05:52 +0000379 db->init.busy = 0;
drh1c2d8412003-03-31 00:30:47 +0000380 if( rc==SQLITE_OK ){
drh58b95762000-06-02 01:17:37 +0000381 db->flags |= SQLITE_Initialized;
danielk19774adee202004-05-08 08:23:19 +0000382 sqlite3CommitInternalChanges(db);
drh2d71ca92004-02-10 02:27:04 +0000383 }
384
drh2d71ca92004-02-10 02:27:04 +0000385 if( rc!=SQLITE_OK ){
drhe0bc4042002-06-25 01:09:11 +0000386 db->flags &= ~SQLITE_Initialized;
drh58b95762000-06-02 01:17:37 +0000387 }
drh1c2d8412003-03-31 00:30:47 +0000388 return rc;
drh58b95762000-06-02 01:17:37 +0000389}
390
391/*
danielk19778e227872004-06-07 07:52:17 +0000392** This routine is a no-op if the database schema is already initialised.
393** Otherwise, the schema is loaded. An error code is returned.
394*/
danielk19778a414492004-06-29 08:59:35 +0000395int sqlite3ReadSchema(Parse *pParse){
danielk19778e227872004-06-07 07:52:17 +0000396 int rc = SQLITE_OK;
danielk19778a414492004-06-29 08:59:35 +0000397 sqlite3 *db = pParse->db;
danielk19778e227872004-06-07 07:52:17 +0000398 if( !db->init.busy ){
399 if( (db->flags & SQLITE_Initialized)==0 ){
danielk19778a414492004-06-29 08:59:35 +0000400 rc = sqlite3Init(db, &pParse->zErrMsg);
danielk19778e227872004-06-07 07:52:17 +0000401 }
402 }
danielk1977c0391392004-06-09 12:30:04 +0000403 assert( rc!=SQLITE_OK || (db->flags & SQLITE_Initialized)||db->init.busy );
danielk19778a414492004-06-29 08:59:35 +0000404 if( rc!=SQLITE_OK ){
405 pParse->rc = rc;
406 pParse->nErr++;
407 }
danielk19778e227872004-06-07 07:52:17 +0000408 return rc;
409}
410
411/*
drhb217a572000-08-22 13:40:18 +0000412** The version of the library
413*/
drh38f82712004-06-18 17:10:16 +0000414const char rcsid3[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $";
danielk197724b03fd2004-05-10 10:34:34 +0000415const char sqlite3_version[] = SQLITE_VERSION;
drh4aec8b62004-08-28 16:19:00 +0000416const char *sqlite3_libversion(void){ return sqlite3_version; }
danielk197799ba19e2005-02-05 07:33:34 +0000417int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
drhb217a572000-08-22 13:40:18 +0000418
419/*
drhd3d39e92004-05-20 22:16:29 +0000420** This is the default collating function named "BINARY" which is always
421** available.
422*/
danielk19772c336542005-01-13 02:14:23 +0000423static int binCollFunc(
drhd3d39e92004-05-20 22:16:29 +0000424 void *NotUsed,
425 int nKey1, const void *pKey1,
426 int nKey2, const void *pKey2
427){
428 int rc, n;
429 n = nKey1<nKey2 ? nKey1 : nKey2;
430 rc = memcmp(pKey1, pKey2, n);
431 if( rc==0 ){
432 rc = nKey1 - nKey2;
433 }
434 return rc;
435}
436
437/*
danielk1977dc1bdc42004-06-11 10:51:27 +0000438** Another built-in collating sequence: NOCASE.
439**
440** This collating sequence is intended to be used for "case independant
441** comparison". SQLite's knowledge of upper and lower case equivalents
442** extends only to the 26 characters used in the English language.
443**
444** At the moment there is only a UTF-8 implementation.
danielk19770202b292004-06-09 09:55:16 +0000445*/
446static int nocaseCollatingFunc(
447 void *NotUsed,
448 int nKey1, const void *pKey1,
449 int nKey2, const void *pKey2
450){
451 int r = sqlite3StrNICmp(
drh208f80a2004-08-29 20:08:58 +0000452 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
danielk19770202b292004-06-09 09:55:16 +0000453 if( 0==r ){
454 r = nKey1-nKey2;
455 }
456 return r;
457}
458
459/*
drhaf9ff332002-01-16 21:00:27 +0000460** Return the ROWID of the most recent insert
461*/
drh9bb575f2004-09-06 17:24:11 +0000462sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
drhaf9ff332002-01-16 21:00:27 +0000463 return db->lastRowid;
464}
465
466/*
danielk197724b03fd2004-05-10 10:34:34 +0000467** Return the number of changes in the most recent call to sqlite3_exec().
drhc8d30ac2002-04-12 10:08:59 +0000468*/
drh9bb575f2004-09-06 17:24:11 +0000469int sqlite3_changes(sqlite3 *db){
drhc8d30ac2002-04-12 10:08:59 +0000470 return db->nChange;
471}
472
rdcf146a772004-02-25 22:51:06 +0000473/*
danielk1977b28af712004-06-21 06:50:26 +0000474** Return the number of changes since the database handle was opened.
rdcf146a772004-02-25 22:51:06 +0000475*/
danielk1977b28af712004-06-21 06:50:26 +0000476int sqlite3_total_changes(sqlite3 *db){
477 return db->nTotalChange;
rdcb0c374f2004-02-20 22:53:38 +0000478}
479
drhc8d30ac2002-04-12 10:08:59 +0000480/*
drh50e5dad2001-09-15 00:57:28 +0000481** Close an existing SQLite database
482*/
drh9bb575f2004-09-06 17:24:11 +0000483int sqlite3_close(sqlite3 *db){
drh8e0a2f92002-02-23 23:45:45 +0000484 HashElem *i;
drh001bbcb2003-03-19 03:14:00 +0000485 int j;
danielk19775c4c7782004-06-16 10:39:23 +0000486
487 if( !db ){
danielk197796d81f92004-06-19 03:33:57 +0000488 return SQLITE_OK;
danielk19775c4c7782004-06-16 10:39:23 +0000489 }
drhc60d0442004-09-30 13:43:13 +0000490 if( sqlite3SafetyCheck(db) ){
danielk1977e35ee192004-06-26 09:50:11 +0000491 return SQLITE_MISUSE;
492 }
493
danielk197796d81f92004-06-19 03:33:57 +0000494 /* If there are any outstanding VMs, return SQLITE_BUSY. */
495 if( db->pVdbe ){
496 sqlite3Error(db, SQLITE_BUSY,
497 "Unable to close due to unfinalised statements");
498 return SQLITE_BUSY;
499 }
500 assert( !sqlite3SafetyCheck(db) );
danielk1977e0048402004-06-15 16:51:01 +0000501
502 /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database
503 ** cannot be opened for some reason. So this routine needs to run in
504 ** that case. But maybe there should be an extra magic value for the
505 ** "failed to open" state.
506 */
danielk197796d81f92004-06-19 03:33:57 +0000507 if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){
drh94e92032003-02-16 22:21:32 +0000508 /* printf("DID NOT CLOSE\n"); fflush(stdout); */
danielk197796d81f92004-06-19 03:33:57 +0000509 return SQLITE_ERROR;
drh94e92032003-02-16 22:21:32 +0000510 }
danielk1977e0048402004-06-15 16:51:01 +0000511
drh001bbcb2003-03-19 03:14:00 +0000512 for(j=0; j<db->nDb; j++){
drh4d189ca2004-02-12 18:46:38 +0000513 struct Db *pDb = &db->aDb[j];
514 if( pDb->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000515 sqlite3BtreeClose(pDb->pBt);
drh4d189ca2004-02-12 18:46:38 +0000516 pDb->pBt = 0;
drh113088e2003-03-20 01:16:58 +0000517 }
drhf57b3392001-10-08 13:22:32 +0000518 }
danielk19774adee202004-05-08 08:23:19 +0000519 sqlite3ResetInternalSchema(db, 0);
drh1c2d8412003-03-31 00:30:47 +0000520 assert( db->nDb<=2 );
521 assert( db->aDb==db->aDbStatic );
drh0bce8352002-02-28 00:41:10 +0000522 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
523 FuncDef *pFunc, *pNext;
524 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
drh8e0a2f92002-02-23 23:45:45 +0000525 pNext = pFunc->pNext;
526 sqliteFree(pFunc);
527 }
528 }
danielk1977466be562004-06-10 02:16:01 +0000529
danielk1977d8123362004-06-12 09:25:12 +0000530 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
danielk1977466be562004-06-10 02:16:01 +0000531 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
danielk1977d8123362004-06-12 09:25:12 +0000532 sqliteFree(pColl);
danielk1977466be562004-06-10 02:16:01 +0000533 }
danielk1977d8123362004-06-12 09:25:12 +0000534 sqlite3HashClear(&db->aCollSeq);
danielk1977466be562004-06-10 02:16:01 +0000535
danielk19774adee202004-05-08 08:23:19 +0000536 sqlite3HashClear(&db->aFunc);
danielk19776622cce2004-05-20 11:00:52 +0000537 sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
danielk1977bfd6cce2004-06-18 04:24:54 +0000538 if( db->pValue ){
539 sqlite3ValueFree(db->pValue);
540 }
541 if( db->pErr ){
542 sqlite3ValueFree(db->pErr);
543 }
danielk197796d81f92004-06-19 03:33:57 +0000544
danielk19776b456a22005-03-21 04:04:02 +0000545#ifndef SQLITE_OMIT_GLOBALRECOVER
546 {
danielk19773eb8db92005-03-29 23:34:58 +0000547 sqlite3 *pPrev;
danielk19776b456a22005-03-21 04:04:02 +0000548 sqlite3OsEnterMutex();
danielk19773eb8db92005-03-29 23:34:58 +0000549 pPrev = pDbList;
danielk19776b456a22005-03-21 04:04:02 +0000550 while( pPrev && pPrev->pNext!=db ){
551 pPrev = pPrev->pNext;
552 }
553 if( pPrev ){
554 pPrev->pNext = db->pNext;
555 }else{
556 assert( pDbList==db );
557 pDbList = db->pNext;
558 }
559 sqlite3OsLeaveMutex();
560 }
561#endif
562
danielk19777e900ab2005-05-23 04:51:01 +0000563#ifdef SQLITE_SSE
564 sqlite3_finalize(db->pFetch);
565#endif
566
danielk197796d81f92004-06-19 03:33:57 +0000567 db->magic = SQLITE_MAGIC_ERROR;
drh75897232000-05-29 14:26:00 +0000568 sqliteFree(db);
danielk197796d81f92004-06-19 03:33:57 +0000569 return SQLITE_OK;
drh75897232000-05-29 14:26:00 +0000570}
571
572/*
drh001bbcb2003-03-19 03:14:00 +0000573** Rollback all database files.
574*/
drh9bb575f2004-09-06 17:24:11 +0000575void sqlite3RollbackAll(sqlite3 *db){
drh001bbcb2003-03-19 03:14:00 +0000576 int i;
577 for(i=0; i<db->nDb; i++){
578 if( db->aDb[i].pBt ){
danielk19774adee202004-05-08 08:23:19 +0000579 sqlite3BtreeRollback(db->aDb[i].pBt);
drh001bbcb2003-03-19 03:14:00 +0000580 db->aDb[i].inTrans = 0;
581 }
582 }
danielk19774adee202004-05-08 08:23:19 +0000583 sqlite3ResetInternalSchema(db, 0);
drh001bbcb2003-03-19 03:14:00 +0000584}
585
586/*
drhc22bd472002-05-10 13:14:07 +0000587** Return a static string that describes the kind of error specified in the
588** argument.
drh247be432002-05-10 05:44:55 +0000589*/
danielk1977f20b21c2004-05-31 23:56:42 +0000590const char *sqlite3ErrStr(int rc){
drhc22bd472002-05-10 13:14:07 +0000591 const char *z;
592 switch( rc ){
drhc60d0442004-09-30 13:43:13 +0000593 case SQLITE_ROW:
594 case SQLITE_DONE:
drhc22bd472002-05-10 13:14:07 +0000595 case SQLITE_OK: z = "not an error"; break;
596 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
597 case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break;
598 case SQLITE_PERM: z = "access permission denied"; break;
599 case SQLITE_ABORT: z = "callback requested query abort"; break;
600 case SQLITE_BUSY: z = "database is locked"; break;
601 case SQLITE_LOCKED: z = "database table is locked"; break;
602 case SQLITE_NOMEM: z = "out of memory"; break;
603 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
604 case SQLITE_INTERRUPT: z = "interrupted"; break;
605 case SQLITE_IOERR: z = "disk I/O error"; break;
606 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
607 case SQLITE_NOTFOUND: z = "table or record not found"; break;
608 case SQLITE_FULL: z = "database is full"; break;
609 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
610 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
611 case SQLITE_EMPTY: z = "table contains no data"; break;
612 case SQLITE_SCHEMA: z = "database schema has changed"; break;
613 case SQLITE_TOOBIG: z = "too much data for one table row"; break;
614 case SQLITE_CONSTRAINT: z = "constraint failed"; break;
615 case SQLITE_MISMATCH: z = "datatype mismatch"; break;
616 case SQLITE_MISUSE: z = "library routine called out of sequence";break;
drh8766c342002-11-09 00:33:15 +0000617 case SQLITE_NOLFS: z = "kernel lacks large file support"; break;
drhed6c8672003-01-12 18:02:16 +0000618 case SQLITE_AUTH: z = "authorization denied"; break;
jplyon892f6712003-06-12 08:59:00 +0000619 case SQLITE_FORMAT: z = "auxiliary database format error"; break;
drhb08153d2004-11-20 20:18:55 +0000620 case SQLITE_RANGE: z = "bind or column index out of range"; break;
drhc602f9a2004-02-12 19:01:04 +0000621 case SQLITE_NOTADB: z = "file is encrypted or is not a database";break;
drhc22bd472002-05-10 13:14:07 +0000622 default: z = "unknown error"; break;
drh247be432002-05-10 05:44:55 +0000623 }
drhc22bd472002-05-10 13:14:07 +0000624 return z;
drh247be432002-05-10 05:44:55 +0000625}
626
627/*
drh2dfbbca2000-07-28 14:32:48 +0000628** This routine implements a busy callback that sleeps and tries
629** again until a timeout value is reached. The timeout value is
630** an integer number of milliseconds passed in as the first
631** argument.
632*/
drhdaffd0e2001-04-11 14:28:42 +0000633static int sqliteDefaultBusyCallback(
drh97903fe2005-05-24 20:19:57 +0000634 void *ptr, /* Database connection */
drh2dfbbca2000-07-28 14:32:48 +0000635 int count /* Number of times table has been busy */
636){
drh8cfbf082001-09-19 13:22:39 +0000637#if SQLITE_MIN_SLEEP_MS==1
drhee570fa2005-04-28 12:06:05 +0000638 static const u8 delays[] =
639 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
640 static const u8 totals[] =
641 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
drhd1bec472004-01-15 13:29:31 +0000642# define NDELAY (sizeof(delays)/sizeof(delays[0]))
drh97903fe2005-05-24 20:19:57 +0000643 int timeout = ((sqlite3 *)ptr)->busyTimeout;
644 int delay, prior;
drh2dfbbca2000-07-28 14:32:48 +0000645
drhee570fa2005-04-28 12:06:05 +0000646 assert( count>=0 );
647 if( count < NDELAY ){
648 delay = delays[count];
649 prior = totals[count];
drhd1bec472004-01-15 13:29:31 +0000650 }else{
651 delay = delays[NDELAY-1];
drh68cb6192005-05-06 22:05:56 +0000652 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
drh2dfbbca2000-07-28 14:32:48 +0000653 }
drhd1bec472004-01-15 13:29:31 +0000654 if( prior + delay > timeout ){
655 delay = timeout - prior;
drh2dfbbca2000-07-28 14:32:48 +0000656 if( delay<=0 ) return 0;
657 }
danielk19774adee202004-05-08 08:23:19 +0000658 sqlite3OsSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000659 return 1;
660#else
661 int timeout = (int)Timeout;
662 if( (count+1)*1000 > timeout ){
663 return 0;
664 }
danielk19774adee202004-05-08 08:23:19 +0000665 sqlite3OsSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000666 return 1;
667#endif
668}
669
670/*
671** This routine sets the busy callback for an Sqlite database to the
672** given callback function with the given argument.
673*/
danielk1977f9d64d22004-06-19 08:18:07 +0000674int sqlite3_busy_handler(
675 sqlite3 *db,
danielk19772a764eb2004-06-12 01:43:26 +0000676 int (*xBusy)(void*,int),
drh2dfbbca2000-07-28 14:32:48 +0000677 void *pArg
678){
drhc60d0442004-09-30 13:43:13 +0000679 if( sqlite3SafetyCheck(db) ){
680 return SQLITE_MISUSE;
681 }
danielk197724162fe2004-06-04 06:22:00 +0000682 db->busyHandler.xFunc = xBusy;
683 db->busyHandler.pArg = pArg;
danielk1977f9d64d22004-06-19 08:18:07 +0000684 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000685}
686
danielk1977348bb5d2003-10-18 09:37:26 +0000687#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
688/*
689** This routine sets the progress callback for an Sqlite database to the
690** given callback function with the given argument. The progress callback will
691** be invoked every nOps opcodes.
692*/
danielk197724b03fd2004-05-10 10:34:34 +0000693void sqlite3_progress_handler(
drh9bb575f2004-09-06 17:24:11 +0000694 sqlite3 *db,
danielk1977348bb5d2003-10-18 09:37:26 +0000695 int nOps,
696 int (*xProgress)(void*),
697 void *pArg
698){
drhc60d0442004-09-30 13:43:13 +0000699 if( !sqlite3SafetyCheck(db) ){
700 if( nOps>0 ){
701 db->xProgress = xProgress;
702 db->nProgressOps = nOps;
703 db->pProgressArg = pArg;
704 }else{
705 db->xProgress = 0;
706 db->nProgressOps = 0;
707 db->pProgressArg = 0;
708 }
danielk1977348bb5d2003-10-18 09:37:26 +0000709 }
710}
711#endif
712
713
drh2dfbbca2000-07-28 14:32:48 +0000714/*
715** This routine installs a default busy handler that waits for the
716** specified number of milliseconds before returning 0.
717*/
danielk1977f9d64d22004-06-19 08:18:07 +0000718int sqlite3_busy_timeout(sqlite3 *db, int ms){
drh2dfbbca2000-07-28 14:32:48 +0000719 if( ms>0 ){
drh97903fe2005-05-24 20:19:57 +0000720 db->busyTimeout = ms;
721 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
drh2dfbbca2000-07-28 14:32:48 +0000722 }else{
danielk197724b03fd2004-05-10 10:34:34 +0000723 sqlite3_busy_handler(db, 0, 0);
drh2dfbbca2000-07-28 14:32:48 +0000724 }
danielk1977f9d64d22004-06-19 08:18:07 +0000725 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000726}
drh4c504392000-10-16 22:06:40 +0000727
728/*
729** Cause any pending operation to stop at its earliest opportunity.
730*/
drh9bb575f2004-09-06 17:24:11 +0000731void sqlite3_interrupt(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000732 if( !sqlite3SafetyCheck(db) ){
733 db->flags |= SQLITE_Interrupt;
734 }
drh4c504392000-10-16 22:06:40 +0000735}
drhfa86c412002-02-02 15:01:15 +0000736
737/*
738** Windows systems should call this routine to free memory that
danielk197724b03fd2004-05-10 10:34:34 +0000739** is returned in the in the errmsg parameter of sqlite3_open() when
drhfa86c412002-02-02 15:01:15 +0000740** SQLite is a DLL. For some reason, it does not work to call free()
741** directly.
742**
drhae29ffb2004-09-25 14:39:18 +0000743** Note that we need to call free() not sqliteFree() here.
drhfa86c412002-02-02 15:01:15 +0000744*/
drh3f4fedb2004-05-31 19:34:33 +0000745void sqlite3_free(char *p){ free(p); }
drhfa86c412002-02-02 15:01:15 +0000746
747/*
drhdf014892004-06-02 00:41:09 +0000748** Create new user functions.
drhfa86c412002-02-02 15:01:15 +0000749*/
danielk197724b03fd2004-05-10 10:34:34 +0000750int sqlite3_create_function(
danielk197765904932004-05-26 06:18:37 +0000751 sqlite3 *db,
752 const char *zFunctionName,
753 int nArg,
danielk1977d8123362004-06-12 09:25:12 +0000754 int enc,
danielk197765904932004-05-26 06:18:37 +0000755 void *pUserData,
756 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
757 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
758 void (*xFinal)(sqlite3_context*)
drh8e0a2f92002-02-23 23:45:45 +0000759){
drh0bce8352002-02-28 00:41:10 +0000760 FuncDef *p;
drh4b59ab52002-08-24 18:24:51 +0000761 int nName;
danielk197765904932004-05-26 06:18:37 +0000762
drhc60d0442004-09-30 13:43:13 +0000763 if( sqlite3SafetyCheck(db) ){
764 return SQLITE_MISUSE;
765 }
766 if( zFunctionName==0 ||
danielk1977398eae72004-05-26 06:58:43 +0000767 (xFunc && (xFinal || xStep)) ||
768 (!xFunc && (xFinal && !xStep)) ||
769 (!xFunc && (!xFinal && xStep)) ||
770 (nArg<-1 || nArg>127) ||
771 (255<(nName = strlen(zFunctionName))) ){
danielk197765904932004-05-26 06:18:37 +0000772 return SQLITE_ERROR;
773 }
danielk1977d8123362004-06-12 09:25:12 +0000774
danielk197793758c82005-01-21 08:13:14 +0000775#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +0000776 /* If SQLITE_UTF16 is specified as the encoding type, transform this
777 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
778 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
779 **
780 ** If SQLITE_ANY is specified, add three versions of the function
781 ** to the hash table.
782 */
783 if( enc==SQLITE_UTF16 ){
784 enc = SQLITE_UTF16NATIVE;
785 }else if( enc==SQLITE_ANY ){
786 int rc;
787 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF8,
danielk1977f9d64d22004-06-19 08:18:07 +0000788 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000789 if( rc!=SQLITE_OK ) return rc;
790 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF16LE,
danielk1977f9d64d22004-06-19 08:18:07 +0000791 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000792 if( rc!=SQLITE_OK ) return rc;
793 enc = SQLITE_UTF16BE;
794 }
danielk197793758c82005-01-21 08:13:14 +0000795#else
796 enc = SQLITE_UTF8;
797#endif
danielk19779636c4e2005-01-25 04:27:54 +0000798
799 /* Check if an existing function is being overridden or deleted. If so,
800 ** and there are active VMs, then return SQLITE_BUSY. If a function
801 ** is being overridden/deleted but there are no active VMs, allow the
802 ** operation to continue but invalidate all precompiled statements.
803 */
804 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0);
805 if( p && p->iPrefEnc==enc && p->nArg==nArg ){
806 if( db->activeVdbeCnt ){
807 sqlite3Error(db, SQLITE_BUSY,
808 "Unable to delete/modify user-function due to active statements");
809 return SQLITE_BUSY;
810 }else{
811 sqlite3ExpirePreparedStatements(db);
812 }
813 }
danielk197765904932004-05-26 06:18:37 +0000814
danielk1977d8123362004-06-12 09:25:12 +0000815 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);
danielk197713073932004-06-30 11:54:06 +0000816 if( p==0 ) return SQLITE_NOMEM;
drh8e0a2f92002-02-23 23:45:45 +0000817 p->xFunc = xFunc;
drh8e0a2f92002-02-23 23:45:45 +0000818 p->xStep = xStep;
danielk197765904932004-05-26 06:18:37 +0000819 p->xFinalize = xFinal;
drh1350b032002-02-27 19:00:20 +0000820 p->pUserData = pUserData;
danielk197765904932004-05-26 06:18:37 +0000821 return SQLITE_OK;
822}
danielk197793758c82005-01-21 08:13:14 +0000823#ifndef SQLITE_OMIT_UTF16
danielk197765904932004-05-26 06:18:37 +0000824int sqlite3_create_function16(
825 sqlite3 *db,
826 const void *zFunctionName,
827 int nArg,
828 int eTextRep,
danielk197765904932004-05-26 06:18:37 +0000829 void *pUserData,
830 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
831 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
832 void (*xFinal)(sqlite3_context*)
833){
834 int rc;
danielk1977bfd6cce2004-06-18 04:24:54 +0000835 char const *zFunc8;
drhc60d0442004-09-30 13:43:13 +0000836 sqlite3_value *pTmp;
danielk1977bfd6cce2004-06-18 04:24:54 +0000837
drhc60d0442004-09-30 13:43:13 +0000838 if( sqlite3SafetyCheck(db) ){
839 return SQLITE_MISUSE;
840 }
841 pTmp = sqlite3GetTransientValue(db);
danielk1977bfd6cce2004-06-18 04:24:54 +0000842 sqlite3ValueSetStr(pTmp, -1, zFunctionName, SQLITE_UTF16NATIVE,SQLITE_STATIC);
843 zFunc8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
844
845 if( !zFunc8 ){
danielk197765904932004-05-26 06:18:37 +0000846 return SQLITE_NOMEM;
847 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000848 rc = sqlite3_create_function(db, zFunc8, nArg, eTextRep,
danielk1977f9d64d22004-06-19 08:18:07 +0000849 pUserData, xFunc, xStep, xFinal);
danielk197765904932004-05-26 06:18:37 +0000850 return rc;
drh8e0a2f92002-02-23 23:45:45 +0000851}
danielk197793758c82005-01-21 08:13:14 +0000852#endif
drhc9b84a12002-06-20 11:36:48 +0000853
854/*
drh18de4822003-01-16 16:28:53 +0000855** Register a trace function. The pArg from the previously registered trace
856** is returned.
857**
858** A NULL trace function means that no tracing is executes. A non-NULL
859** trace is a pointer to a function that is invoked at the start of each
danielk197724b03fd2004-05-10 10:34:34 +0000860** sqlite3_exec().
drh18de4822003-01-16 16:28:53 +0000861*/
drh9bb575f2004-09-06 17:24:11 +0000862void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
drh18de4822003-01-16 16:28:53 +0000863 void *pOld = db->pTraceArg;
864 db->xTrace = xTrace;
865 db->pTraceArg = pArg;
866 return pOld;
drh0d1a6432003-04-03 15:46:04 +0000867}
paulb0208cc2003-04-13 18:26:49 +0000868
drhaa940ea2004-01-15 02:44:03 +0000869/*** EXPERIMENTAL ***
870**
871** Register a function to be invoked when a transaction comments.
872** If either function returns non-zero, then the commit becomes a
873** rollback.
874*/
danielk197724b03fd2004-05-10 10:34:34 +0000875void *sqlite3_commit_hook(
drh9bb575f2004-09-06 17:24:11 +0000876 sqlite3 *db, /* Attach the hook to this database */
drhaa940ea2004-01-15 02:44:03 +0000877 int (*xCallback)(void*), /* Function to invoke on each commit */
878 void *pArg /* Argument to the function */
879){
880 void *pOld = db->pCommitArg;
881 db->xCommitCallback = xCallback;
882 db->pCommitArg = pArg;
883 return pOld;
884}
885
886
paulb0208cc2003-04-13 18:26:49 +0000887/*
drh13bff812003-04-15 01:19:47 +0000888** This routine is called to create a connection to a database BTree
889** driver. If zFilename is the name of a file, then that file is
890** opened and used. If zFilename is the magic name ":memory:" then
891** the database is stored in memory (and is thus forgotten as soon as
892** the connection is closed.) If zFilename is NULL then the database
893** is for temporary use only and is deleted as soon as the connection
894** is closed.
drh90f5ecb2004-07-22 01:19:35 +0000895**
896** A temporary database can be either a disk file (that is automatically
897** deleted when the file is closed) or a set of red-black trees held in memory,
898** depending on the values of the TEMP_STORE compile-time macro and the
899** db->temp_store variable, according to the following chart:
900**
901** TEMP_STORE db->temp_store Location of temporary database
902** ---------- -------------- ------------------------------
903** 0 any file
904** 1 1 file
905** 1 2 memory
906** 1 0 file
907** 2 1 file
908** 2 2 memory
909** 2 0 memory
910** 3 any memory
paulb0208cc2003-04-13 18:26:49 +0000911*/
danielk19774adee202004-05-08 08:23:19 +0000912int sqlite3BtreeFactory(
drh9bb575f2004-09-06 17:24:11 +0000913 const sqlite3 *db, /* Main database when opening aux otherwise 0 */
paulb0208cc2003-04-13 18:26:49 +0000914 const char *zFilename, /* Name of the file containing the BTree database */
915 int omitJournal, /* if TRUE then do not journal this file */
916 int nCache, /* How many pages in the page cache */
danielk19774adee202004-05-08 08:23:19 +0000917 Btree **ppBtree /* Pointer to new Btree object written here */
918){
danielk19774adee202004-05-08 08:23:19 +0000919 int btree_flags = 0;
drh90f5ecb2004-07-22 01:19:35 +0000920 int rc;
danielk19774adee202004-05-08 08:23:19 +0000921
drheec983e2004-05-08 10:11:36 +0000922 assert( ppBtree != 0);
danielk19774adee202004-05-08 08:23:19 +0000923 if( omitJournal ){
924 btree_flags |= BTREE_OMIT_JOURNAL;
paulb0208cc2003-04-13 18:26:49 +0000925 }
drh7bec5052005-02-06 02:45:41 +0000926 if( db->flags & SQLITE_NoReadlock ){
927 btree_flags |= BTREE_NO_READLOCK;
928 }
drh90f5ecb2004-07-22 01:19:35 +0000929 if( zFilename==0 ){
drh90f5ecb2004-07-22 01:19:35 +0000930#if TEMP_STORE==0
drh22ac46d2004-08-14 18:34:54 +0000931 /* Do nothing */
drh90f5ecb2004-07-22 01:19:35 +0000932#endif
danielk197703aded42004-11-22 05:26:27 +0000933#ifndef SQLITE_OMIT_MEMORYDB
drh90f5ecb2004-07-22 01:19:35 +0000934#if TEMP_STORE==1
drh22ac46d2004-08-14 18:34:54 +0000935 if( db->temp_store==2 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000936#endif
937#if TEMP_STORE==2
drh22ac46d2004-08-14 18:34:54 +0000938 if( db->temp_store!=1 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000939#endif
940#if TEMP_STORE==3
drh22ac46d2004-08-14 18:34:54 +0000941 zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000942#endif
danielk197703aded42004-11-22 05:26:27 +0000943#endif /* SQLITE_OMIT_MEMORYDB */
drh90f5ecb2004-07-22 01:19:35 +0000944 }
danielk19774adee202004-05-08 08:23:19 +0000945
drh90f5ecb2004-07-22 01:19:35 +0000946 rc = sqlite3BtreeOpen(zFilename, ppBtree, btree_flags);
947 if( rc==SQLITE_OK ){
948 sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler);
949 sqlite3BtreeSetCacheSize(*ppBtree, nCache);
950 }
951 return rc;
paulb0208cc2003-04-13 18:26:49 +0000952}
danielk19774adee202004-05-08 08:23:19 +0000953
danielk19774ad17132004-05-21 01:47:26 +0000954/*
955** Return UTF-8 encoded English language explanation of the most recent
956** error.
957*/
danielk19776622cce2004-05-20 11:00:52 +0000958const char *sqlite3_errmsg(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000959 const char *z;
960 if( sqlite3_malloc_failed ){
danielk1977f20b21c2004-05-31 23:56:42 +0000961 return sqlite3ErrStr(SQLITE_NOMEM);
danielk19774ad17132004-05-21 01:47:26 +0000962 }
drhc60d0442004-09-30 13:43:13 +0000963 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
danielk1977e35ee192004-06-26 09:50:11 +0000964 return sqlite3ErrStr(SQLITE_MISUSE);
965 }
drhc60d0442004-09-30 13:43:13 +0000966 z = sqlite3_value_text(db->pErr);
967 if( z==0 ){
968 z = sqlite3ErrStr(db->errCode);
danielk19776622cce2004-05-20 11:00:52 +0000969 }
drhc60d0442004-09-30 13:43:13 +0000970 return z;
danielk19776622cce2004-05-20 11:00:52 +0000971}
972
drh6c626082004-11-14 21:56:29 +0000973#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +0000974/*
975** Return UTF-16 encoded English language explanation of the most recent
976** error.
977*/
danielk19776622cce2004-05-20 11:00:52 +0000978const void *sqlite3_errmsg16(sqlite3 *db){
danielk1977bfd6cce2004-06-18 04:24:54 +0000979 /* Because all the characters in the string are in the unicode
980 ** range 0x00-0xFF, if we pad the big-endian string with a
981 ** zero byte, we can obtain the little-endian string with
982 ** &big_endian[1].
983 */
drh57196282004-10-06 15:41:16 +0000984 static const char outOfMemBe[] = {
danielk1977bfd6cce2004-06-18 04:24:54 +0000985 0, 'o', 0, 'u', 0, 't', 0, ' ',
986 0, 'o', 0, 'f', 0, ' ',
987 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0
988 };
drh57196282004-10-06 15:41:16 +0000989 static const char misuseBe [] = {
danielk1977e35ee192004-06-26 09:50:11 +0000990 0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ',
991 0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ',
992 0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ',
993 0, 'o', 0, 'u', 0, 't', 0, ' ',
994 0, 'o', 0, 'f', 0, ' ',
995 0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0
996 };
danielk19774ad17132004-05-21 01:47:26 +0000997
drhc60d0442004-09-30 13:43:13 +0000998 const void *z;
999 if( sqlite3_malloc_failed ){
1000 return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
1001 }
1002 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
1003 return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
1004 }
1005 z = sqlite3_value_text16(db->pErr);
1006 if( z==0 ){
1007 sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
1008 SQLITE_UTF8, SQLITE_STATIC);
1009 z = sqlite3_value_text16(db->pErr);
1010 }
1011 return z;
danielk19776622cce2004-05-20 11:00:52 +00001012}
drh6c626082004-11-14 21:56:29 +00001013#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00001014
drhc60d0442004-09-30 13:43:13 +00001015/*
1016** Return the most recent error code generated by an SQLite routine.
1017*/
danielk19776622cce2004-05-20 11:00:52 +00001018int sqlite3_errcode(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +00001019 if( sqlite3_malloc_failed ){
1020 return SQLITE_NOMEM;
1021 }
1022 if( sqlite3SafetyCheck(db) ){
1023 return SQLITE_MISUSE;
1024 }
danielk19776622cce2004-05-20 11:00:52 +00001025 return db->errCode;
1026}
1027
1028/*
drhc275b4e2004-07-19 17:25:24 +00001029** Check schema cookies in all databases. If any cookie is out
drha6ecd332004-06-10 00:29:09 +00001030** of date, return 0. If all schema cookies are current, return 1.
1031*/
drh9bb575f2004-09-06 17:24:11 +00001032static int schemaIsValid(sqlite3 *db){
drha6ecd332004-06-10 00:29:09 +00001033 int iDb;
1034 int rc;
1035 BtCursor *curTemp;
1036 int cookie;
1037 int allOk = 1;
1038
1039 for(iDb=0; allOk && iDb<db->nDb; iDb++){
1040 Btree *pBt;
drha6ecd332004-06-10 00:29:09 +00001041 pBt = db->aDb[iDb].pBt;
1042 if( pBt==0 ) continue;
1043 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
1044 if( rc==SQLITE_OK ){
danielk1977e0d4b062004-06-28 01:11:46 +00001045 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
drha6ecd332004-06-10 00:29:09 +00001046 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].schema_cookie ){
1047 allOk = 0;
1048 }
1049 sqlite3BtreeCloseCursor(curTemp);
1050 }
1051 }
1052 return allOk;
1053}
1054
1055/*
danielk19776622cce2004-05-20 11:00:52 +00001056** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
1057*/
1058int sqlite3_prepare(
1059 sqlite3 *db, /* Database handle. */
1060 const char *zSql, /* UTF-8 encoded SQL statement. */
1061 int nBytes, /* Length of zSql in bytes. */
1062 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1063 const char** pzTail /* OUT: End of parsed string */
1064){
1065 Parse sParse;
1066 char *zErrMsg = 0;
1067 int rc = SQLITE_OK;
1068
danielk197796fb0dd2004-06-30 09:49:22 +00001069 if( sqlite3_malloc_failed ){
1070 return SQLITE_NOMEM;
1071 }
1072
danielk19775c4c7782004-06-16 10:39:23 +00001073 assert( ppStmt );
1074 *ppStmt = 0;
danielk19776622cce2004-05-20 11:00:52 +00001075 if( sqlite3SafetyOn(db) ){
danielk1977e35ee192004-06-26 09:50:11 +00001076 return SQLITE_MISUSE;
danielk19776622cce2004-05-20 11:00:52 +00001077 }
1078
danielk19776622cce2004-05-20 11:00:52 +00001079 memset(&sParse, 0, sizeof(sParse));
1080 sParse.db = db;
1081 sqlite3RunParser(&sParse, zSql, &zErrMsg);
1082
danielk19776622cce2004-05-20 11:00:52 +00001083 if( sqlite3_malloc_failed ){
1084 rc = SQLITE_NOMEM;
1085 sqlite3RollbackAll(db);
1086 sqlite3ResetInternalSchema(db, 0);
1087 db->flags &= ~SQLITE_InTrans;
1088 goto prepare_out;
1089 }
1090 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
drh3f7d4e42004-07-24 14:35:58 +00001091 if( sParse.rc!=SQLITE_OK && sParse.checkSchema && !schemaIsValid(db) ){
drha6ecd332004-06-10 00:29:09 +00001092 sParse.rc = SQLITE_SCHEMA;
1093 }
danielk19776622cce2004-05-20 11:00:52 +00001094 if( sParse.rc==SQLITE_SCHEMA ){
1095 sqlite3ResetInternalSchema(db, 0);
1096 }
danielk19776622cce2004-05-20 11:00:52 +00001097 if( pzTail ) *pzTail = sParse.zTail;
danielk19776622cce2004-05-20 11:00:52 +00001098 rc = sParse.rc;
1099
danielk197793758c82005-01-21 08:13:14 +00001100#ifndef SQLITE_OMIT_EXPLAIN
danielk197722322fd2004-05-25 23:35:17 +00001101 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
1102 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
danielk19773cf86062004-05-26 10:11:05 +00001103 sqlite3VdbeSetColName(sParse.pVdbe, 0, "addr", P3_STATIC);
1104 sqlite3VdbeSetColName(sParse.pVdbe, 1, "opcode", P3_STATIC);
1105 sqlite3VdbeSetColName(sParse.pVdbe, 2, "p1", P3_STATIC);
1106 sqlite3VdbeSetColName(sParse.pVdbe, 3, "p2", P3_STATIC);
1107 sqlite3VdbeSetColName(sParse.pVdbe, 4, "p3", P3_STATIC);
danielk197722322fd2004-05-25 23:35:17 +00001108 }
danielk197793758c82005-01-21 08:13:14 +00001109#endif
danielk197722322fd2004-05-25 23:35:17 +00001110
danielk19776622cce2004-05-20 11:00:52 +00001111prepare_out:
danielk197722322fd2004-05-25 23:35:17 +00001112 if( sqlite3SafetyOff(db) ){
1113 rc = SQLITE_MISUSE;
1114 }
danielk19775c4c7782004-06-16 10:39:23 +00001115 if( rc==SQLITE_OK ){
1116 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
1117 }else if( sParse.pVdbe ){
danielk1977cfe9a692004-06-16 12:00:29 +00001118 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
danielk19775c4c7782004-06-16 10:39:23 +00001119 }
1120
danielk19776622cce2004-05-20 11:00:52 +00001121 if( zErrMsg ){
1122 sqlite3Error(db, rc, "%s", zErrMsg);
danielk1977b20e56b2004-06-15 13:36:30 +00001123 sqliteFree(zErrMsg);
danielk19776622cce2004-05-20 11:00:52 +00001124 }else{
1125 sqlite3Error(db, rc, 0);
1126 }
1127 return rc;
1128}
1129
drh6c626082004-11-14 21:56:29 +00001130#ifndef SQLITE_OMIT_UTF16
danielk19776622cce2004-05-20 11:00:52 +00001131/*
1132** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
1133*/
1134int sqlite3_prepare16(
1135 sqlite3 *db, /* Database handle. */
1136 const void *zSql, /* UTF-8 encoded SQL statement. */
1137 int nBytes, /* Length of zSql in bytes. */
1138 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1139 const void **pzTail /* OUT: End of parsed string */
1140){
1141 /* This function currently works by first transforming the UTF-16
1142 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
1143 ** tricky bit is figuring out the pointer to return in *pzTail.
1144 */
danielk1977bfd6cce2004-06-18 04:24:54 +00001145 char const *zSql8 = 0;
danielk19776622cce2004-05-20 11:00:52 +00001146 char const *zTail8 = 0;
1147 int rc;
danielk1977bfd6cce2004-06-18 04:24:54 +00001148 sqlite3_value *pTmp;
danielk19776622cce2004-05-20 11:00:52 +00001149
drhc60d0442004-09-30 13:43:13 +00001150 if( sqlite3SafetyCheck(db) ){
1151 return SQLITE_MISUSE;
1152 }
danielk1977bfd6cce2004-06-18 04:24:54 +00001153 pTmp = sqlite3GetTransientValue(db);
1154 sqlite3ValueSetStr(pTmp, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1155 zSql8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
danielk19776622cce2004-05-20 11:00:52 +00001156 if( !zSql8 ){
1157 sqlite3Error(db, SQLITE_NOMEM, 0);
1158 return SQLITE_NOMEM;
1159 }
1160 rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);
1161
1162 if( zTail8 && pzTail ){
1163 /* If sqlite3_prepare returns a tail pointer, we calculate the
1164 ** equivalent pointer into the UTF-16 string by counting the unicode
1165 ** characters between zSql8 and zTail8, and then returning a pointer
1166 ** the same number of characters into the UTF-16 string.
1167 */
1168 int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8);
1169 *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed);
1170 }
1171
1172 return rc;
1173}
drh6c626082004-11-14 21:56:29 +00001174#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00001175
danielk19774ad17132004-05-21 01:47:26 +00001176/*
1177** This routine does the work of opening a database on behalf of
1178** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
drhee570fa2005-04-28 12:06:05 +00001179** is UTF-8 encoded.
danielk19774ad17132004-05-21 01:47:26 +00001180*/
1181static int openDatabase(
1182 const char *zFilename, /* Database filename UTF-8 encoded */
danielk19778e227872004-06-07 07:52:17 +00001183 sqlite3 **ppDb /* OUT: Returned database handle */
danielk19774ad17132004-05-21 01:47:26 +00001184){
1185 sqlite3 *db;
1186 int rc, i;
danielk19774ad17132004-05-21 01:47:26 +00001187
1188 /* Allocate the sqlite data structure */
drh9bb575f2004-09-06 17:24:11 +00001189 db = sqliteMalloc( sizeof(sqlite3) );
danielk19774ad17132004-05-21 01:47:26 +00001190 if( db==0 ) goto opendb_out;
danielk19774ad17132004-05-21 01:47:26 +00001191 db->priorNewRowid = 0;
1192 db->magic = SQLITE_MAGIC_BUSY;
1193 db->nDb = 2;
1194 db->aDb = db->aDbStatic;
danielk1977dc8453f2004-06-12 00:42:34 +00001195 db->enc = SQLITE_UTF8;
danielk19771d850a72004-05-31 08:26:49 +00001196 db->autoCommit = 1;
drh47a6db22005-01-18 16:02:40 +00001197 db->flags |= SQLITE_ShortColNames;
drhf9b596e2004-05-26 16:54:42 +00001198 sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
danielk19774ad17132004-05-21 01:47:26 +00001199 sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
1200 for(i=0; i<db->nDb; i++){
1201 sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
1202 sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
1203 sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);
1204 sqlite3HashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1);
1205 }
danielk19774ad17132004-05-21 01:47:26 +00001206
danielk19770202b292004-06-09 09:55:16 +00001207 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
1208 ** and UTF-16, so add a version for each to avoid any unnecessary
1209 ** conversions. The only error that can occur here is a malloc() failure.
1210 */
danielk19772c336542005-01-13 02:14:23 +00001211 if( sqlite3_create_collation(db, "BINARY", SQLITE_UTF8, 0,binCollFunc) ||
1212 sqlite3_create_collation(db, "BINARY", SQLITE_UTF16, 0,binCollFunc) ||
1213 !(db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0)) ){
danielk19770202b292004-06-09 09:55:16 +00001214 rc = db->errCode;
1215 assert( rc!=SQLITE_OK );
1216 db->magic = SQLITE_MAGIC_CLOSED;
1217 goto opendb_out;
1218 }
1219
1220 /* Also add a UTF-8 case-insensitive collation sequence. */
danielk1977466be562004-06-10 02:16:01 +00001221 sqlite3_create_collation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc);
danielk19770202b292004-06-09 09:55:16 +00001222
danielk19774ad17132004-05-21 01:47:26 +00001223 /* Open the backend database driver */
danielk19774ad17132004-05-21 01:47:26 +00001224 rc = sqlite3BtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt);
1225 if( rc!=SQLITE_OK ){
danielk19774ad17132004-05-21 01:47:26 +00001226 sqlite3Error(db, rc, 0);
1227 db->magic = SQLITE_MAGIC_CLOSED;
1228 goto opendb_out;
1229 }
danielk19774ad17132004-05-21 01:47:26 +00001230
danielk197753c0f742005-03-29 03:10:59 +00001231 /* The default safety_level for the main database is 'full'; for the temp
1232 ** database it is 'NONE'. This matches the pager layer defaults.
1233 */
1234 db->aDb[0].zName = "main";
danielk197791cf71b2004-06-26 06:37:06 +00001235 db->aDb[0].safety_level = 3;
danielk197753c0f742005-03-29 03:10:59 +00001236#ifndef SQLITE_OMIT_TEMPDB
1237 db->aDb[1].zName = "temp";
danielk197791cf71b2004-06-26 06:37:06 +00001238 db->aDb[1].safety_level = 1;
danielk197753c0f742005-03-29 03:10:59 +00001239#endif
1240
danielk197791cf71b2004-06-26 06:37:06 +00001241
danielk19778e227872004-06-07 07:52:17 +00001242 /* Register all built-in functions, but do not attempt to read the
1243 ** database schema yet. This is delayed until the first time the database
1244 ** is accessed.
1245 */
danielk19774ad17132004-05-21 01:47:26 +00001246 sqlite3RegisterBuiltinFunctions(db);
danielk19774397de52005-01-12 12:44:03 +00001247 sqlite3Error(db, SQLITE_OK, 0);
1248 db->magic = SQLITE_MAGIC_OPEN;
danielk19774ad17132004-05-21 01:47:26 +00001249
1250opendb_out:
danielk197713073932004-06-30 11:54:06 +00001251 if( sqlite3_errcode(db)==SQLITE_OK && sqlite3_malloc_failed ){
1252 sqlite3Error(db, SQLITE_NOMEM, 0);
1253 }
danielk19774ad17132004-05-21 01:47:26 +00001254 *ppDb = db;
danielk19776b456a22005-03-21 04:04:02 +00001255#ifndef SQLITE_OMIT_GLOBALRECOVER
1256 if( db ){
1257 sqlite3OsEnterMutex();
1258 db->pNext = pDbList;
1259 pDbList = db;
1260 sqlite3OsLeaveMutex();
1261 }
1262#endif
danielk19774ad17132004-05-21 01:47:26 +00001263 return sqlite3_errcode(db);
1264}
1265
1266/*
1267** Open a new database handle.
1268*/
danielk197780290862004-05-22 09:21:21 +00001269int sqlite3_open(
danielk19774ad17132004-05-21 01:47:26 +00001270 const char *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00001271 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00001272){
danielk19778e227872004-06-07 07:52:17 +00001273 return openDatabase(zFilename, ppDb);
danielk197783ab5a82004-05-21 11:39:05 +00001274}
1275
drh6c626082004-11-14 21:56:29 +00001276#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +00001277/*
1278** Open a new database handle.
1279*/
1280int sqlite3_open16(
1281 const void *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00001282 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00001283){
danielk1977bfd6cce2004-06-18 04:24:54 +00001284 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
1285 int rc = SQLITE_NOMEM;
1286 sqlite3_value *pVal;
danielk19774ad17132004-05-21 01:47:26 +00001287
1288 assert( ppDb );
danielk1977bfd6cce2004-06-18 04:24:54 +00001289 *ppDb = 0;
1290 pVal = sqlite3ValueNew();
1291 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1292 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
1293 if( zFilename8 ){
1294 rc = openDatabase(zFilename8, ppDb);
1295 if( rc==SQLITE_OK && *ppDb ){
1296 sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
1297 }
danielk19774ad17132004-05-21 01:47:26 +00001298 }
danielk1977bfd6cce2004-06-18 04:24:54 +00001299 if( pVal ){
1300 sqlite3ValueFree(pVal);
danielk19774ad17132004-05-21 01:47:26 +00001301 }
danielk19778e227872004-06-07 07:52:17 +00001302
danielk19774ad17132004-05-21 01:47:26 +00001303 return rc;
1304}
drh6c626082004-11-14 21:56:29 +00001305#endif /* SQLITE_OMIT_UTF16 */
danielk19774ad17132004-05-21 01:47:26 +00001306
danielk1977106bb232004-05-21 10:08:53 +00001307/*
1308** The following routine destroys a virtual machine that is created by
1309** the sqlite3_compile() routine. The integer returned is an SQLITE_
1310** success/failure code that describes the result of executing the virtual
1311** machine.
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_finalize(sqlite3_stmt *pStmt){
drh1bcdb0c2004-08-28 14:49:46 +00001317 int rc;
1318 if( pStmt==0 ){
1319 rc = SQLITE_OK;
1320 }else{
1321 rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
1322 }
1323 return rc;
danielk1977106bb232004-05-21 10:08:53 +00001324}
1325
1326/*
1327** Terminate the current execution of an SQL statement and reset it
1328** back to its starting state so that it can be reused. A success code from
1329** the prior execution is returned.
1330**
1331** This routine sets the error code and string returned by
1332** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
1333*/
danielk1977fc57d7b2004-05-26 02:04:57 +00001334int sqlite3_reset(sqlite3_stmt *pStmt){
drh1bcdb0c2004-08-28 14:49:46 +00001335 int rc;
1336 if( pStmt==0 ){
1337 rc = SQLITE_OK;
1338 }else{
1339 rc = sqlite3VdbeReset((Vdbe*)pStmt);
danielk1977b3bce662005-01-29 08:32:43 +00001340 sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0, 0, 0, 0);
drh1bcdb0c2004-08-28 14:49:46 +00001341 }
danielk1977106bb232004-05-21 10:08:53 +00001342 return rc;
1343}
danielk19770202b292004-06-09 09:55:16 +00001344
danielk1977d8123362004-06-12 09:25:12 +00001345/*
1346** Register a new collation sequence with the database handle db.
1347*/
danielk19770202b292004-06-09 09:55:16 +00001348int sqlite3_create_collation(
1349 sqlite3* db,
1350 const char *zName,
danielk1977466be562004-06-10 02:16:01 +00001351 int enc,
danielk19770202b292004-06-09 09:55:16 +00001352 void* pCtx,
1353 int(*xCompare)(void*,int,const void*,int,const void*)
1354){
1355 CollSeq *pColl;
1356 int rc = SQLITE_OK;
danielk1977d8123362004-06-12 09:25:12 +00001357
drhc60d0442004-09-30 13:43:13 +00001358 if( sqlite3SafetyCheck(db) ){
1359 return SQLITE_MISUSE;
1360 }
1361
danielk1977d8123362004-06-12 09:25:12 +00001362 /* If SQLITE_UTF16 is specified as the encoding type, transform this
1363 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
1364 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
1365 */
1366 if( enc==SQLITE_UTF16 ){
1367 enc = SQLITE_UTF16NATIVE;
1368 }
1369
danielk1977466be562004-06-10 02:16:01 +00001370 if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16LE && enc!=SQLITE_UTF16BE ){
1371 sqlite3Error(db, SQLITE_ERROR,
1372 "Param 3 to sqlite3_create_collation() must be one of "
danielk1977d8123362004-06-12 09:25:12 +00001373 "SQLITE_UTF8, SQLITE_UTF16, SQLITE_UTF16LE or SQLITE_UTF16BE"
danielk1977466be562004-06-10 02:16:01 +00001374 );
1375 return SQLITE_ERROR;
1376 }
danielk1977a21c6b62005-01-24 10:25:59 +00001377
danielk19779636c4e2005-01-25 04:27:54 +00001378 /* Check if this call is removing or replacing an existing collation
1379 ** sequence. If so, and there are active VMs, return busy. If there
1380 ** are no active VMs, invalidate any pre-compiled statements.
danielk1977a21c6b62005-01-24 10:25:59 +00001381 */
danielk19779636c4e2005-01-25 04:27:54 +00001382 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 0);
1383 if( pColl && pColl->xCmp ){
1384 if( db->activeVdbeCnt ){
1385 sqlite3Error(db, SQLITE_BUSY,
1386 "Unable to delete/modify collation sequence due to active statements");
1387 return SQLITE_BUSY;
1388 }
danielk1977a21c6b62005-01-24 10:25:59 +00001389 sqlite3ExpirePreparedStatements(db);
1390 }
1391
danielk1977466be562004-06-10 02:16:01 +00001392 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 1);
danielk19770202b292004-06-09 09:55:16 +00001393 if( 0==pColl ){
1394 rc = SQLITE_NOMEM;
danielk19770202b292004-06-09 09:55:16 +00001395 }else{
1396 pColl->xCmp = xCompare;
1397 pColl->pUser = pCtx;
danielk1977312d6b32004-06-29 13:18:23 +00001398 pColl->enc = enc;
danielk19770202b292004-06-09 09:55:16 +00001399 }
1400 sqlite3Error(db, rc, 0);
danielk1977466be562004-06-10 02:16:01 +00001401 return rc;
danielk19770202b292004-06-09 09:55:16 +00001402}
1403
drh6c626082004-11-14 21:56:29 +00001404#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00001405/*
1406** Register a new collation sequence with the database handle db.
1407*/
danielk19770202b292004-06-09 09:55:16 +00001408int sqlite3_create_collation16(
1409 sqlite3* db,
1410 const char *zName,
danielk1977466be562004-06-10 02:16:01 +00001411 int enc,
danielk19770202b292004-06-09 09:55:16 +00001412 void* pCtx,
1413 int(*xCompare)(void*,int,const void*,int,const void*)
1414){
danielk1977bfd6cce2004-06-18 04:24:54 +00001415 char const *zName8;
drhc60d0442004-09-30 13:43:13 +00001416 sqlite3_value *pTmp;
1417 if( sqlite3SafetyCheck(db) ){
1418 return SQLITE_MISUSE;
1419 }
1420 pTmp = sqlite3GetTransientValue(db);
danielk1977bfd6cce2004-06-18 04:24:54 +00001421 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1422 zName8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
1423 return sqlite3_create_collation(db, zName8, enc, pCtx, xCompare);
danielk19770202b292004-06-09 09:55:16 +00001424}
drh6c626082004-11-14 21:56:29 +00001425#endif /* SQLITE_OMIT_UTF16 */
danielk19777cedc8d2004-06-10 10:50:08 +00001426
danielk1977d8123362004-06-12 09:25:12 +00001427/*
1428** Register a collation sequence factory callback with the database handle
1429** db. Replace any previously installed collation sequence factory.
1430*/
danielk19777cedc8d2004-06-10 10:50:08 +00001431int sqlite3_collation_needed(
1432 sqlite3 *db,
1433 void *pCollNeededArg,
1434 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
1435){
drhc60d0442004-09-30 13:43:13 +00001436 if( sqlite3SafetyCheck(db) ){
1437 return SQLITE_MISUSE;
1438 }
danielk19777cedc8d2004-06-10 10:50:08 +00001439 db->xCollNeeded = xCollNeeded;
1440 db->xCollNeeded16 = 0;
1441 db->pCollNeededArg = pCollNeededArg;
1442 return SQLITE_OK;
1443}
danielk1977d8123362004-06-12 09:25:12 +00001444
drh6c626082004-11-14 21:56:29 +00001445#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00001446/*
1447** Register a collation sequence factory callback with the database handle
1448** db. Replace any previously installed collation sequence factory.
1449*/
danielk19777cedc8d2004-06-10 10:50:08 +00001450int sqlite3_collation_needed16(
1451 sqlite3 *db,
1452 void *pCollNeededArg,
1453 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
1454){
drhc60d0442004-09-30 13:43:13 +00001455 if( sqlite3SafetyCheck(db) ){
1456 return SQLITE_MISUSE;
1457 }
danielk19777cedc8d2004-06-10 10:50:08 +00001458 db->xCollNeeded = 0;
1459 db->xCollNeeded16 = xCollNeeded16;
1460 db->pCollNeededArg = pCollNeededArg;
1461 return SQLITE_OK;
1462}
drh6c626082004-11-14 21:56:29 +00001463#endif /* SQLITE_OMIT_UTF16 */
danielk19776b456a22005-03-21 04:04:02 +00001464
1465#ifndef SQLITE_OMIT_GLOBALRECOVER
1466/*
1467** This function is called to recover from a malloc failure that occured
1468** within SQLite.
1469**
1470** This function is *not* threadsafe. Calling this from within a threaded
1471** application when threads other than the caller have used SQLite is
1472** dangerous and will almost certainly result in malfunctions.
1473*/
1474int sqlite3_global_recover(){
1475 int rc = SQLITE_OK;
1476
1477 if( sqlite3_malloc_failed ){
1478 sqlite3 *db;
1479 int i;
1480 sqlite3_malloc_failed = 0;
1481 for(db=pDbList; db; db=db->pNext ){
1482 sqlite3ExpirePreparedStatements(db);
1483 for(i=0; i<db->nDb; i++){
1484 Btree *pBt = db->aDb[i].pBt;
1485 if( pBt && (rc=sqlite3BtreeReset(pBt)) ){
1486 goto recover_out;
1487 }
1488 }
1489 db->autoCommit = 1;
1490 }
1491 }
1492
1493recover_out:
1494 if( rc!=SQLITE_OK ){
1495 sqlite3_malloc_failed = 1;
1496 }
1497 return rc;
1498}
1499#endif