blob: 3813101957134f0f4f752cc3ff58bd78242b8aff [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**
danielk1977fd9a0a42005-05-24 12:01:00 +000017** $Id: main.c,v 1.289 2005/05/24 12:01:02 danielk1977 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(
drh2dfbbca2000-07-28 14:32:48 +0000634 void *Timeout, /* Maximum amount of time to wait */
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]))
drhc44af712004-09-02 15:53:56 +0000643 ptr timeout = (ptr)Timeout;
644 ptr 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 ){
drhc44af712004-09-02 15:53:56 +0000720 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)(ptr)ms);
drh2dfbbca2000-07-28 14:32:48 +0000721 }else{
danielk197724b03fd2004-05-10 10:34:34 +0000722 sqlite3_busy_handler(db, 0, 0);
drh2dfbbca2000-07-28 14:32:48 +0000723 }
danielk1977f9d64d22004-06-19 08:18:07 +0000724 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000725}
drh4c504392000-10-16 22:06:40 +0000726
727/*
728** Cause any pending operation to stop at its earliest opportunity.
729*/
drh9bb575f2004-09-06 17:24:11 +0000730void sqlite3_interrupt(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000731 if( !sqlite3SafetyCheck(db) ){
732 db->flags |= SQLITE_Interrupt;
733 }
drh4c504392000-10-16 22:06:40 +0000734}
drhfa86c412002-02-02 15:01:15 +0000735
736/*
737** Windows systems should call this routine to free memory that
danielk197724b03fd2004-05-10 10:34:34 +0000738** is returned in the in the errmsg parameter of sqlite3_open() when
drhfa86c412002-02-02 15:01:15 +0000739** SQLite is a DLL. For some reason, it does not work to call free()
740** directly.
741**
drhae29ffb2004-09-25 14:39:18 +0000742** Note that we need to call free() not sqliteFree() here.
drhfa86c412002-02-02 15:01:15 +0000743*/
drh3f4fedb2004-05-31 19:34:33 +0000744void sqlite3_free(char *p){ free(p); }
drhfa86c412002-02-02 15:01:15 +0000745
746/*
drhdf014892004-06-02 00:41:09 +0000747** Create new user functions.
drhfa86c412002-02-02 15:01:15 +0000748*/
danielk197724b03fd2004-05-10 10:34:34 +0000749int sqlite3_create_function(
danielk197765904932004-05-26 06:18:37 +0000750 sqlite3 *db,
751 const char *zFunctionName,
752 int nArg,
danielk1977d8123362004-06-12 09:25:12 +0000753 int enc,
danielk197765904932004-05-26 06:18:37 +0000754 void *pUserData,
755 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
756 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
757 void (*xFinal)(sqlite3_context*)
drh8e0a2f92002-02-23 23:45:45 +0000758){
drh0bce8352002-02-28 00:41:10 +0000759 FuncDef *p;
drh4b59ab52002-08-24 18:24:51 +0000760 int nName;
danielk197765904932004-05-26 06:18:37 +0000761
drhc60d0442004-09-30 13:43:13 +0000762 if( sqlite3SafetyCheck(db) ){
763 return SQLITE_MISUSE;
764 }
765 if( zFunctionName==0 ||
danielk1977398eae72004-05-26 06:58:43 +0000766 (xFunc && (xFinal || xStep)) ||
767 (!xFunc && (xFinal && !xStep)) ||
768 (!xFunc && (!xFinal && xStep)) ||
769 (nArg<-1 || nArg>127) ||
770 (255<(nName = strlen(zFunctionName))) ){
danielk197765904932004-05-26 06:18:37 +0000771 return SQLITE_ERROR;
772 }
danielk1977d8123362004-06-12 09:25:12 +0000773
danielk197793758c82005-01-21 08:13:14 +0000774#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +0000775 /* If SQLITE_UTF16 is specified as the encoding type, transform this
776 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
777 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
778 **
779 ** If SQLITE_ANY is specified, add three versions of the function
780 ** to the hash table.
781 */
782 if( enc==SQLITE_UTF16 ){
783 enc = SQLITE_UTF16NATIVE;
784 }else if( enc==SQLITE_ANY ){
785 int rc;
786 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF8,
danielk1977f9d64d22004-06-19 08:18:07 +0000787 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000788 if( rc!=SQLITE_OK ) return rc;
789 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF16LE,
danielk1977f9d64d22004-06-19 08:18:07 +0000790 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000791 if( rc!=SQLITE_OK ) return rc;
792 enc = SQLITE_UTF16BE;
793 }
danielk197793758c82005-01-21 08:13:14 +0000794#else
795 enc = SQLITE_UTF8;
796#endif
danielk19779636c4e2005-01-25 04:27:54 +0000797
798 /* Check if an existing function is being overridden or deleted. If so,
799 ** and there are active VMs, then return SQLITE_BUSY. If a function
800 ** is being overridden/deleted but there are no active VMs, allow the
801 ** operation to continue but invalidate all precompiled statements.
802 */
803 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0);
804 if( p && p->iPrefEnc==enc && p->nArg==nArg ){
805 if( db->activeVdbeCnt ){
806 sqlite3Error(db, SQLITE_BUSY,
807 "Unable to delete/modify user-function due to active statements");
808 return SQLITE_BUSY;
809 }else{
810 sqlite3ExpirePreparedStatements(db);
811 }
812 }
danielk197765904932004-05-26 06:18:37 +0000813
danielk1977d8123362004-06-12 09:25:12 +0000814 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);
danielk197713073932004-06-30 11:54:06 +0000815 if( p==0 ) return SQLITE_NOMEM;
drh8e0a2f92002-02-23 23:45:45 +0000816 p->xFunc = xFunc;
drh8e0a2f92002-02-23 23:45:45 +0000817 p->xStep = xStep;
danielk197765904932004-05-26 06:18:37 +0000818 p->xFinalize = xFinal;
drh1350b032002-02-27 19:00:20 +0000819 p->pUserData = pUserData;
danielk197765904932004-05-26 06:18:37 +0000820 return SQLITE_OK;
821}
danielk197793758c82005-01-21 08:13:14 +0000822#ifndef SQLITE_OMIT_UTF16
danielk197765904932004-05-26 06:18:37 +0000823int sqlite3_create_function16(
824 sqlite3 *db,
825 const void *zFunctionName,
826 int nArg,
827 int eTextRep,
danielk197765904932004-05-26 06:18:37 +0000828 void *pUserData,
829 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
830 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
831 void (*xFinal)(sqlite3_context*)
832){
833 int rc;
danielk1977bfd6cce2004-06-18 04:24:54 +0000834 char const *zFunc8;
drhc60d0442004-09-30 13:43:13 +0000835 sqlite3_value *pTmp;
danielk1977bfd6cce2004-06-18 04:24:54 +0000836
drhc60d0442004-09-30 13:43:13 +0000837 if( sqlite3SafetyCheck(db) ){
838 return SQLITE_MISUSE;
839 }
840 pTmp = sqlite3GetTransientValue(db);
danielk1977bfd6cce2004-06-18 04:24:54 +0000841 sqlite3ValueSetStr(pTmp, -1, zFunctionName, SQLITE_UTF16NATIVE,SQLITE_STATIC);
842 zFunc8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
843
844 if( !zFunc8 ){
danielk197765904932004-05-26 06:18:37 +0000845 return SQLITE_NOMEM;
846 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000847 rc = sqlite3_create_function(db, zFunc8, nArg, eTextRep,
danielk1977f9d64d22004-06-19 08:18:07 +0000848 pUserData, xFunc, xStep, xFinal);
danielk197765904932004-05-26 06:18:37 +0000849 return rc;
drh8e0a2f92002-02-23 23:45:45 +0000850}
danielk197793758c82005-01-21 08:13:14 +0000851#endif
drhc9b84a12002-06-20 11:36:48 +0000852
853/*
drh18de4822003-01-16 16:28:53 +0000854** Register a trace function. The pArg from the previously registered trace
855** is returned.
856**
857** A NULL trace function means that no tracing is executes. A non-NULL
858** trace is a pointer to a function that is invoked at the start of each
danielk197724b03fd2004-05-10 10:34:34 +0000859** sqlite3_exec().
drh18de4822003-01-16 16:28:53 +0000860*/
drh9bb575f2004-09-06 17:24:11 +0000861void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
drh18de4822003-01-16 16:28:53 +0000862 void *pOld = db->pTraceArg;
863 db->xTrace = xTrace;
864 db->pTraceArg = pArg;
865 return pOld;
drh0d1a6432003-04-03 15:46:04 +0000866}
paulb0208cc2003-04-13 18:26:49 +0000867
drhaa940ea2004-01-15 02:44:03 +0000868/*** EXPERIMENTAL ***
869**
870** Register a function to be invoked when a transaction comments.
871** If either function returns non-zero, then the commit becomes a
872** rollback.
873*/
danielk197724b03fd2004-05-10 10:34:34 +0000874void *sqlite3_commit_hook(
drh9bb575f2004-09-06 17:24:11 +0000875 sqlite3 *db, /* Attach the hook to this database */
drhaa940ea2004-01-15 02:44:03 +0000876 int (*xCallback)(void*), /* Function to invoke on each commit */
877 void *pArg /* Argument to the function */
878){
879 void *pOld = db->pCommitArg;
880 db->xCommitCallback = xCallback;
881 db->pCommitArg = pArg;
882 return pOld;
883}
884
885
paulb0208cc2003-04-13 18:26:49 +0000886/*
drh13bff812003-04-15 01:19:47 +0000887** This routine is called to create a connection to a database BTree
888** driver. If zFilename is the name of a file, then that file is
889** opened and used. If zFilename is the magic name ":memory:" then
890** the database is stored in memory (and is thus forgotten as soon as
891** the connection is closed.) If zFilename is NULL then the database
892** is for temporary use only and is deleted as soon as the connection
893** is closed.
drh90f5ecb2004-07-22 01:19:35 +0000894**
895** A temporary database can be either a disk file (that is automatically
896** deleted when the file is closed) or a set of red-black trees held in memory,
897** depending on the values of the TEMP_STORE compile-time macro and the
898** db->temp_store variable, according to the following chart:
899**
900** TEMP_STORE db->temp_store Location of temporary database
901** ---------- -------------- ------------------------------
902** 0 any file
903** 1 1 file
904** 1 2 memory
905** 1 0 file
906** 2 1 file
907** 2 2 memory
908** 2 0 memory
909** 3 any memory
paulb0208cc2003-04-13 18:26:49 +0000910*/
danielk19774adee202004-05-08 08:23:19 +0000911int sqlite3BtreeFactory(
drh9bb575f2004-09-06 17:24:11 +0000912 const sqlite3 *db, /* Main database when opening aux otherwise 0 */
paulb0208cc2003-04-13 18:26:49 +0000913 const char *zFilename, /* Name of the file containing the BTree database */
914 int omitJournal, /* if TRUE then do not journal this file */
915 int nCache, /* How many pages in the page cache */
danielk19774adee202004-05-08 08:23:19 +0000916 Btree **ppBtree /* Pointer to new Btree object written here */
917){
danielk19774adee202004-05-08 08:23:19 +0000918 int btree_flags = 0;
drh90f5ecb2004-07-22 01:19:35 +0000919 int rc;
danielk19774adee202004-05-08 08:23:19 +0000920
drheec983e2004-05-08 10:11:36 +0000921 assert( ppBtree != 0);
danielk19774adee202004-05-08 08:23:19 +0000922 if( omitJournal ){
923 btree_flags |= BTREE_OMIT_JOURNAL;
paulb0208cc2003-04-13 18:26:49 +0000924 }
drh7bec5052005-02-06 02:45:41 +0000925 if( db->flags & SQLITE_NoReadlock ){
926 btree_flags |= BTREE_NO_READLOCK;
927 }
drh90f5ecb2004-07-22 01:19:35 +0000928 if( zFilename==0 ){
drh90f5ecb2004-07-22 01:19:35 +0000929#if TEMP_STORE==0
drh22ac46d2004-08-14 18:34:54 +0000930 /* Do nothing */
drh90f5ecb2004-07-22 01:19:35 +0000931#endif
danielk197703aded42004-11-22 05:26:27 +0000932#ifndef SQLITE_OMIT_MEMORYDB
drh90f5ecb2004-07-22 01:19:35 +0000933#if TEMP_STORE==1
drh22ac46d2004-08-14 18:34:54 +0000934 if( db->temp_store==2 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000935#endif
936#if TEMP_STORE==2
drh22ac46d2004-08-14 18:34:54 +0000937 if( db->temp_store!=1 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000938#endif
939#if TEMP_STORE==3
drh22ac46d2004-08-14 18:34:54 +0000940 zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000941#endif
danielk197703aded42004-11-22 05:26:27 +0000942#endif /* SQLITE_OMIT_MEMORYDB */
drh90f5ecb2004-07-22 01:19:35 +0000943 }
danielk19774adee202004-05-08 08:23:19 +0000944
drh90f5ecb2004-07-22 01:19:35 +0000945 rc = sqlite3BtreeOpen(zFilename, ppBtree, btree_flags);
946 if( rc==SQLITE_OK ){
947 sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler);
948 sqlite3BtreeSetCacheSize(*ppBtree, nCache);
949 }
950 return rc;
paulb0208cc2003-04-13 18:26:49 +0000951}
danielk19774adee202004-05-08 08:23:19 +0000952
danielk19774ad17132004-05-21 01:47:26 +0000953/*
954** Return UTF-8 encoded English language explanation of the most recent
955** error.
956*/
danielk19776622cce2004-05-20 11:00:52 +0000957const char *sqlite3_errmsg(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000958 const char *z;
959 if( sqlite3_malloc_failed ){
danielk1977f20b21c2004-05-31 23:56:42 +0000960 return sqlite3ErrStr(SQLITE_NOMEM);
danielk19774ad17132004-05-21 01:47:26 +0000961 }
drhc60d0442004-09-30 13:43:13 +0000962 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
danielk1977e35ee192004-06-26 09:50:11 +0000963 return sqlite3ErrStr(SQLITE_MISUSE);
964 }
drhc60d0442004-09-30 13:43:13 +0000965 z = sqlite3_value_text(db->pErr);
966 if( z==0 ){
967 z = sqlite3ErrStr(db->errCode);
danielk19776622cce2004-05-20 11:00:52 +0000968 }
drhc60d0442004-09-30 13:43:13 +0000969 return z;
danielk19776622cce2004-05-20 11:00:52 +0000970}
971
drh6c626082004-11-14 21:56:29 +0000972#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +0000973/*
974** Return UTF-16 encoded English language explanation of the most recent
975** error.
976*/
danielk19776622cce2004-05-20 11:00:52 +0000977const void *sqlite3_errmsg16(sqlite3 *db){
danielk1977bfd6cce2004-06-18 04:24:54 +0000978 /* Because all the characters in the string are in the unicode
979 ** range 0x00-0xFF, if we pad the big-endian string with a
980 ** zero byte, we can obtain the little-endian string with
981 ** &big_endian[1].
982 */
drh57196282004-10-06 15:41:16 +0000983 static const char outOfMemBe[] = {
danielk1977bfd6cce2004-06-18 04:24:54 +0000984 0, 'o', 0, 'u', 0, 't', 0, ' ',
985 0, 'o', 0, 'f', 0, ' ',
986 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0
987 };
drh57196282004-10-06 15:41:16 +0000988 static const char misuseBe [] = {
danielk1977e35ee192004-06-26 09:50:11 +0000989 0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ',
990 0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ',
991 0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ',
992 0, 'o', 0, 'u', 0, 't', 0, ' ',
993 0, 'o', 0, 'f', 0, ' ',
994 0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0
995 };
danielk19774ad17132004-05-21 01:47:26 +0000996
drhc60d0442004-09-30 13:43:13 +0000997 const void *z;
998 if( sqlite3_malloc_failed ){
999 return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
1000 }
1001 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
1002 return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
1003 }
1004 z = sqlite3_value_text16(db->pErr);
1005 if( z==0 ){
1006 sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
1007 SQLITE_UTF8, SQLITE_STATIC);
1008 z = sqlite3_value_text16(db->pErr);
1009 }
1010 return z;
danielk19776622cce2004-05-20 11:00:52 +00001011}
drh6c626082004-11-14 21:56:29 +00001012#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00001013
drhc60d0442004-09-30 13:43:13 +00001014/*
1015** Return the most recent error code generated by an SQLite routine.
1016*/
danielk19776622cce2004-05-20 11:00:52 +00001017int sqlite3_errcode(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +00001018 if( sqlite3_malloc_failed ){
1019 return SQLITE_NOMEM;
1020 }
1021 if( sqlite3SafetyCheck(db) ){
1022 return SQLITE_MISUSE;
1023 }
danielk19776622cce2004-05-20 11:00:52 +00001024 return db->errCode;
1025}
1026
1027/*
drhc275b4e2004-07-19 17:25:24 +00001028** Check schema cookies in all databases. If any cookie is out
drha6ecd332004-06-10 00:29:09 +00001029** of date, return 0. If all schema cookies are current, return 1.
1030*/
drh9bb575f2004-09-06 17:24:11 +00001031static int schemaIsValid(sqlite3 *db){
drha6ecd332004-06-10 00:29:09 +00001032 int iDb;
1033 int rc;
1034 BtCursor *curTemp;
1035 int cookie;
1036 int allOk = 1;
1037
1038 for(iDb=0; allOk && iDb<db->nDb; iDb++){
1039 Btree *pBt;
drha6ecd332004-06-10 00:29:09 +00001040 pBt = db->aDb[iDb].pBt;
1041 if( pBt==0 ) continue;
1042 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
1043 if( rc==SQLITE_OK ){
danielk1977e0d4b062004-06-28 01:11:46 +00001044 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
drha6ecd332004-06-10 00:29:09 +00001045 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].schema_cookie ){
1046 allOk = 0;
1047 }
1048 sqlite3BtreeCloseCursor(curTemp);
1049 }
1050 }
1051 return allOk;
1052}
1053
1054/*
danielk19776622cce2004-05-20 11:00:52 +00001055** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
1056*/
1057int sqlite3_prepare(
1058 sqlite3 *db, /* Database handle. */
1059 const char *zSql, /* UTF-8 encoded SQL statement. */
1060 int nBytes, /* Length of zSql in bytes. */
1061 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1062 const char** pzTail /* OUT: End of parsed string */
1063){
1064 Parse sParse;
1065 char *zErrMsg = 0;
1066 int rc = SQLITE_OK;
1067
danielk197796fb0dd2004-06-30 09:49:22 +00001068 if( sqlite3_malloc_failed ){
1069 return SQLITE_NOMEM;
1070 }
1071
danielk19775c4c7782004-06-16 10:39:23 +00001072 assert( ppStmt );
1073 *ppStmt = 0;
danielk19776622cce2004-05-20 11:00:52 +00001074 if( sqlite3SafetyOn(db) ){
danielk1977e35ee192004-06-26 09:50:11 +00001075 return SQLITE_MISUSE;
danielk19776622cce2004-05-20 11:00:52 +00001076 }
1077
danielk19776622cce2004-05-20 11:00:52 +00001078 memset(&sParse, 0, sizeof(sParse));
1079 sParse.db = db;
1080 sqlite3RunParser(&sParse, zSql, &zErrMsg);
1081
danielk19776622cce2004-05-20 11:00:52 +00001082 if( sqlite3_malloc_failed ){
1083 rc = SQLITE_NOMEM;
1084 sqlite3RollbackAll(db);
1085 sqlite3ResetInternalSchema(db, 0);
1086 db->flags &= ~SQLITE_InTrans;
1087 goto prepare_out;
1088 }
1089 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
drh3f7d4e42004-07-24 14:35:58 +00001090 if( sParse.rc!=SQLITE_OK && sParse.checkSchema && !schemaIsValid(db) ){
drha6ecd332004-06-10 00:29:09 +00001091 sParse.rc = SQLITE_SCHEMA;
1092 }
danielk19776622cce2004-05-20 11:00:52 +00001093 if( sParse.rc==SQLITE_SCHEMA ){
1094 sqlite3ResetInternalSchema(db, 0);
1095 }
danielk19776622cce2004-05-20 11:00:52 +00001096 if( pzTail ) *pzTail = sParse.zTail;
danielk19776622cce2004-05-20 11:00:52 +00001097 rc = sParse.rc;
1098
danielk197793758c82005-01-21 08:13:14 +00001099#ifndef SQLITE_OMIT_EXPLAIN
danielk197722322fd2004-05-25 23:35:17 +00001100 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
1101 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
danielk19773cf86062004-05-26 10:11:05 +00001102 sqlite3VdbeSetColName(sParse.pVdbe, 0, "addr", P3_STATIC);
1103 sqlite3VdbeSetColName(sParse.pVdbe, 1, "opcode", P3_STATIC);
1104 sqlite3VdbeSetColName(sParse.pVdbe, 2, "p1", P3_STATIC);
1105 sqlite3VdbeSetColName(sParse.pVdbe, 3, "p2", P3_STATIC);
1106 sqlite3VdbeSetColName(sParse.pVdbe, 4, "p3", P3_STATIC);
danielk197722322fd2004-05-25 23:35:17 +00001107 }
danielk197793758c82005-01-21 08:13:14 +00001108#endif
danielk197722322fd2004-05-25 23:35:17 +00001109
danielk19776622cce2004-05-20 11:00:52 +00001110prepare_out:
danielk197722322fd2004-05-25 23:35:17 +00001111 if( sqlite3SafetyOff(db) ){
1112 rc = SQLITE_MISUSE;
1113 }
danielk19775c4c7782004-06-16 10:39:23 +00001114 if( rc==SQLITE_OK ){
1115 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
1116 }else if( sParse.pVdbe ){
danielk1977cfe9a692004-06-16 12:00:29 +00001117 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
danielk19775c4c7782004-06-16 10:39:23 +00001118 }
1119
danielk19776622cce2004-05-20 11:00:52 +00001120 if( zErrMsg ){
1121 sqlite3Error(db, rc, "%s", zErrMsg);
danielk1977b20e56b2004-06-15 13:36:30 +00001122 sqliteFree(zErrMsg);
danielk19776622cce2004-05-20 11:00:52 +00001123 }else{
1124 sqlite3Error(db, rc, 0);
1125 }
1126 return rc;
1127}
1128
drh6c626082004-11-14 21:56:29 +00001129#ifndef SQLITE_OMIT_UTF16
danielk19776622cce2004-05-20 11:00:52 +00001130/*
1131** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
1132*/
1133int sqlite3_prepare16(
1134 sqlite3 *db, /* Database handle. */
1135 const void *zSql, /* UTF-8 encoded SQL statement. */
1136 int nBytes, /* Length of zSql in bytes. */
1137 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1138 const void **pzTail /* OUT: End of parsed string */
1139){
1140 /* This function currently works by first transforming the UTF-16
1141 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
1142 ** tricky bit is figuring out the pointer to return in *pzTail.
1143 */
danielk1977bfd6cce2004-06-18 04:24:54 +00001144 char const *zSql8 = 0;
danielk19776622cce2004-05-20 11:00:52 +00001145 char const *zTail8 = 0;
1146 int rc;
danielk1977bfd6cce2004-06-18 04:24:54 +00001147 sqlite3_value *pTmp;
danielk19776622cce2004-05-20 11:00:52 +00001148
drhc60d0442004-09-30 13:43:13 +00001149 if( sqlite3SafetyCheck(db) ){
1150 return SQLITE_MISUSE;
1151 }
danielk1977bfd6cce2004-06-18 04:24:54 +00001152 pTmp = sqlite3GetTransientValue(db);
1153 sqlite3ValueSetStr(pTmp, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1154 zSql8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
danielk19776622cce2004-05-20 11:00:52 +00001155 if( !zSql8 ){
1156 sqlite3Error(db, SQLITE_NOMEM, 0);
1157 return SQLITE_NOMEM;
1158 }
1159 rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);
1160
1161 if( zTail8 && pzTail ){
1162 /* If sqlite3_prepare returns a tail pointer, we calculate the
1163 ** equivalent pointer into the UTF-16 string by counting the unicode
1164 ** characters between zSql8 and zTail8, and then returning a pointer
1165 ** the same number of characters into the UTF-16 string.
1166 */
1167 int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8);
1168 *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed);
1169 }
1170
1171 return rc;
1172}
drh6c626082004-11-14 21:56:29 +00001173#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00001174
danielk19774ad17132004-05-21 01:47:26 +00001175/*
1176** This routine does the work of opening a database on behalf of
1177** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
drhee570fa2005-04-28 12:06:05 +00001178** is UTF-8 encoded.
danielk19774ad17132004-05-21 01:47:26 +00001179*/
1180static int openDatabase(
1181 const char *zFilename, /* Database filename UTF-8 encoded */
danielk19778e227872004-06-07 07:52:17 +00001182 sqlite3 **ppDb /* OUT: Returned database handle */
danielk19774ad17132004-05-21 01:47:26 +00001183){
1184 sqlite3 *db;
1185 int rc, i;
danielk19774ad17132004-05-21 01:47:26 +00001186
1187 /* Allocate the sqlite data structure */
drh9bb575f2004-09-06 17:24:11 +00001188 db = sqliteMalloc( sizeof(sqlite3) );
danielk19774ad17132004-05-21 01:47:26 +00001189 if( db==0 ) goto opendb_out;
danielk19774ad17132004-05-21 01:47:26 +00001190 db->priorNewRowid = 0;
1191 db->magic = SQLITE_MAGIC_BUSY;
1192 db->nDb = 2;
1193 db->aDb = db->aDbStatic;
danielk1977dc8453f2004-06-12 00:42:34 +00001194 db->enc = SQLITE_UTF8;
danielk19771d850a72004-05-31 08:26:49 +00001195 db->autoCommit = 1;
drh47a6db22005-01-18 16:02:40 +00001196 db->flags |= SQLITE_ShortColNames;
drhf9b596e2004-05-26 16:54:42 +00001197 sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
danielk19774ad17132004-05-21 01:47:26 +00001198 sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
1199 for(i=0; i<db->nDb; i++){
1200 sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
1201 sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
1202 sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);
1203 sqlite3HashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1);
1204 }
danielk19774ad17132004-05-21 01:47:26 +00001205
danielk19770202b292004-06-09 09:55:16 +00001206 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
1207 ** and UTF-16, so add a version for each to avoid any unnecessary
1208 ** conversions. The only error that can occur here is a malloc() failure.
1209 */
danielk19772c336542005-01-13 02:14:23 +00001210 if( sqlite3_create_collation(db, "BINARY", SQLITE_UTF8, 0,binCollFunc) ||
1211 sqlite3_create_collation(db, "BINARY", SQLITE_UTF16, 0,binCollFunc) ||
1212 !(db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0)) ){
danielk19770202b292004-06-09 09:55:16 +00001213 rc = db->errCode;
1214 assert( rc!=SQLITE_OK );
1215 db->magic = SQLITE_MAGIC_CLOSED;
1216 goto opendb_out;
1217 }
1218
1219 /* Also add a UTF-8 case-insensitive collation sequence. */
danielk1977466be562004-06-10 02:16:01 +00001220 sqlite3_create_collation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc);
danielk19770202b292004-06-09 09:55:16 +00001221
danielk19774ad17132004-05-21 01:47:26 +00001222 /* Open the backend database driver */
danielk19774ad17132004-05-21 01:47:26 +00001223 rc = sqlite3BtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt);
1224 if( rc!=SQLITE_OK ){
danielk19774ad17132004-05-21 01:47:26 +00001225 sqlite3Error(db, rc, 0);
1226 db->magic = SQLITE_MAGIC_CLOSED;
1227 goto opendb_out;
1228 }
danielk19774ad17132004-05-21 01:47:26 +00001229
danielk197753c0f742005-03-29 03:10:59 +00001230 /* The default safety_level for the main database is 'full'; for the temp
1231 ** database it is 'NONE'. This matches the pager layer defaults.
1232 */
1233 db->aDb[0].zName = "main";
danielk197791cf71b2004-06-26 06:37:06 +00001234 db->aDb[0].safety_level = 3;
danielk197753c0f742005-03-29 03:10:59 +00001235#ifndef SQLITE_OMIT_TEMPDB
1236 db->aDb[1].zName = "temp";
danielk197791cf71b2004-06-26 06:37:06 +00001237 db->aDb[1].safety_level = 1;
danielk197753c0f742005-03-29 03:10:59 +00001238#endif
1239
danielk197791cf71b2004-06-26 06:37:06 +00001240
danielk19778e227872004-06-07 07:52:17 +00001241 /* Register all built-in functions, but do not attempt to read the
1242 ** database schema yet. This is delayed until the first time the database
1243 ** is accessed.
1244 */
danielk19774ad17132004-05-21 01:47:26 +00001245 sqlite3RegisterBuiltinFunctions(db);
danielk19774397de52005-01-12 12:44:03 +00001246 sqlite3Error(db, SQLITE_OK, 0);
1247 db->magic = SQLITE_MAGIC_OPEN;
danielk19774ad17132004-05-21 01:47:26 +00001248
1249opendb_out:
danielk197713073932004-06-30 11:54:06 +00001250 if( sqlite3_errcode(db)==SQLITE_OK && sqlite3_malloc_failed ){
1251 sqlite3Error(db, SQLITE_NOMEM, 0);
1252 }
danielk19774ad17132004-05-21 01:47:26 +00001253 *ppDb = db;
danielk19776b456a22005-03-21 04:04:02 +00001254#ifndef SQLITE_OMIT_GLOBALRECOVER
1255 if( db ){
1256 sqlite3OsEnterMutex();
1257 db->pNext = pDbList;
1258 pDbList = db;
1259 sqlite3OsLeaveMutex();
1260 }
1261#endif
danielk19774ad17132004-05-21 01:47:26 +00001262 return sqlite3_errcode(db);
1263}
1264
1265/*
1266** Open a new database handle.
1267*/
danielk197780290862004-05-22 09:21:21 +00001268int sqlite3_open(
danielk19774ad17132004-05-21 01:47:26 +00001269 const char *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00001270 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00001271){
danielk19778e227872004-06-07 07:52:17 +00001272 return openDatabase(zFilename, ppDb);
danielk197783ab5a82004-05-21 11:39:05 +00001273}
1274
drh6c626082004-11-14 21:56:29 +00001275#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +00001276/*
1277** Open a new database handle.
1278*/
1279int sqlite3_open16(
1280 const void *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00001281 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00001282){
danielk1977bfd6cce2004-06-18 04:24:54 +00001283 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
1284 int rc = SQLITE_NOMEM;
1285 sqlite3_value *pVal;
danielk19774ad17132004-05-21 01:47:26 +00001286
1287 assert( ppDb );
danielk1977bfd6cce2004-06-18 04:24:54 +00001288 *ppDb = 0;
1289 pVal = sqlite3ValueNew();
1290 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1291 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
1292 if( zFilename8 ){
1293 rc = openDatabase(zFilename8, ppDb);
1294 if( rc==SQLITE_OK && *ppDb ){
1295 sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
1296 }
danielk19774ad17132004-05-21 01:47:26 +00001297 }
danielk1977bfd6cce2004-06-18 04:24:54 +00001298 if( pVal ){
1299 sqlite3ValueFree(pVal);
danielk19774ad17132004-05-21 01:47:26 +00001300 }
danielk19778e227872004-06-07 07:52:17 +00001301
danielk19774ad17132004-05-21 01:47:26 +00001302 return rc;
1303}
drh6c626082004-11-14 21:56:29 +00001304#endif /* SQLITE_OMIT_UTF16 */
danielk19774ad17132004-05-21 01:47:26 +00001305
danielk1977106bb232004-05-21 10:08:53 +00001306/*
1307** The following routine destroys a virtual machine that is created by
1308** the sqlite3_compile() routine. The integer returned is an SQLITE_
1309** success/failure code that describes the result of executing the virtual
1310** machine.
1311**
1312** This routine sets the error code and string returned by
1313** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
1314*/
danielk1977fc57d7b2004-05-26 02:04:57 +00001315int sqlite3_finalize(sqlite3_stmt *pStmt){
drh1bcdb0c2004-08-28 14:49:46 +00001316 int rc;
1317 if( pStmt==0 ){
1318 rc = SQLITE_OK;
1319 }else{
1320 rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
1321 }
1322 return rc;
danielk1977106bb232004-05-21 10:08:53 +00001323}
1324
1325/*
1326** Terminate the current execution of an SQL statement and reset it
1327** back to its starting state so that it can be reused. A success code from
1328** the prior execution is returned.
1329**
1330** This routine sets the error code and string returned by
1331** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
1332*/
danielk1977fc57d7b2004-05-26 02:04:57 +00001333int sqlite3_reset(sqlite3_stmt *pStmt){
drh1bcdb0c2004-08-28 14:49:46 +00001334 int rc;
1335 if( pStmt==0 ){
1336 rc = SQLITE_OK;
1337 }else{
1338 rc = sqlite3VdbeReset((Vdbe*)pStmt);
danielk1977b3bce662005-01-29 08:32:43 +00001339 sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0, 0, 0, 0);
drh1bcdb0c2004-08-28 14:49:46 +00001340 }
danielk1977106bb232004-05-21 10:08:53 +00001341 return rc;
1342}
danielk19770202b292004-06-09 09:55:16 +00001343
danielk1977d8123362004-06-12 09:25:12 +00001344/*
1345** Register a new collation sequence with the database handle db.
1346*/
danielk19770202b292004-06-09 09:55:16 +00001347int sqlite3_create_collation(
1348 sqlite3* db,
1349 const char *zName,
danielk1977466be562004-06-10 02:16:01 +00001350 int enc,
danielk19770202b292004-06-09 09:55:16 +00001351 void* pCtx,
1352 int(*xCompare)(void*,int,const void*,int,const void*)
1353){
1354 CollSeq *pColl;
1355 int rc = SQLITE_OK;
danielk1977d8123362004-06-12 09:25:12 +00001356
drhc60d0442004-09-30 13:43:13 +00001357 if( sqlite3SafetyCheck(db) ){
1358 return SQLITE_MISUSE;
1359 }
1360
danielk1977d8123362004-06-12 09:25:12 +00001361 /* If SQLITE_UTF16 is specified as the encoding type, transform this
1362 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
1363 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
1364 */
1365 if( enc==SQLITE_UTF16 ){
1366 enc = SQLITE_UTF16NATIVE;
1367 }
1368
danielk1977466be562004-06-10 02:16:01 +00001369 if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16LE && enc!=SQLITE_UTF16BE ){
1370 sqlite3Error(db, SQLITE_ERROR,
1371 "Param 3 to sqlite3_create_collation() must be one of "
danielk1977d8123362004-06-12 09:25:12 +00001372 "SQLITE_UTF8, SQLITE_UTF16, SQLITE_UTF16LE or SQLITE_UTF16BE"
danielk1977466be562004-06-10 02:16:01 +00001373 );
1374 return SQLITE_ERROR;
1375 }
danielk1977a21c6b62005-01-24 10:25:59 +00001376
danielk19779636c4e2005-01-25 04:27:54 +00001377 /* Check if this call is removing or replacing an existing collation
1378 ** sequence. If so, and there are active VMs, return busy. If there
1379 ** are no active VMs, invalidate any pre-compiled statements.
danielk1977a21c6b62005-01-24 10:25:59 +00001380 */
danielk19779636c4e2005-01-25 04:27:54 +00001381 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 0);
1382 if( pColl && pColl->xCmp ){
1383 if( db->activeVdbeCnt ){
1384 sqlite3Error(db, SQLITE_BUSY,
1385 "Unable to delete/modify collation sequence due to active statements");
1386 return SQLITE_BUSY;
1387 }
danielk1977a21c6b62005-01-24 10:25:59 +00001388 sqlite3ExpirePreparedStatements(db);
1389 }
1390
danielk1977466be562004-06-10 02:16:01 +00001391 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 1);
danielk19770202b292004-06-09 09:55:16 +00001392 if( 0==pColl ){
1393 rc = SQLITE_NOMEM;
danielk19770202b292004-06-09 09:55:16 +00001394 }else{
1395 pColl->xCmp = xCompare;
1396 pColl->pUser = pCtx;
danielk1977312d6b32004-06-29 13:18:23 +00001397 pColl->enc = enc;
danielk19770202b292004-06-09 09:55:16 +00001398 }
1399 sqlite3Error(db, rc, 0);
danielk1977466be562004-06-10 02:16:01 +00001400 return rc;
danielk19770202b292004-06-09 09:55:16 +00001401}
1402
drh6c626082004-11-14 21:56:29 +00001403#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00001404/*
1405** Register a new collation sequence with the database handle db.
1406*/
danielk19770202b292004-06-09 09:55:16 +00001407int sqlite3_create_collation16(
1408 sqlite3* db,
1409 const char *zName,
danielk1977466be562004-06-10 02:16:01 +00001410 int enc,
danielk19770202b292004-06-09 09:55:16 +00001411 void* pCtx,
1412 int(*xCompare)(void*,int,const void*,int,const void*)
1413){
danielk1977bfd6cce2004-06-18 04:24:54 +00001414 char const *zName8;
drhc60d0442004-09-30 13:43:13 +00001415 sqlite3_value *pTmp;
1416 if( sqlite3SafetyCheck(db) ){
1417 return SQLITE_MISUSE;
1418 }
1419 pTmp = sqlite3GetTransientValue(db);
danielk1977bfd6cce2004-06-18 04:24:54 +00001420 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1421 zName8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
1422 return sqlite3_create_collation(db, zName8, enc, pCtx, xCompare);
danielk19770202b292004-06-09 09:55:16 +00001423}
drh6c626082004-11-14 21:56:29 +00001424#endif /* SQLITE_OMIT_UTF16 */
danielk19777cedc8d2004-06-10 10:50:08 +00001425
danielk1977d8123362004-06-12 09:25:12 +00001426/*
1427** Register a collation sequence factory callback with the database handle
1428** db. Replace any previously installed collation sequence factory.
1429*/
danielk19777cedc8d2004-06-10 10:50:08 +00001430int sqlite3_collation_needed(
1431 sqlite3 *db,
1432 void *pCollNeededArg,
1433 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
1434){
drhc60d0442004-09-30 13:43:13 +00001435 if( sqlite3SafetyCheck(db) ){
1436 return SQLITE_MISUSE;
1437 }
danielk19777cedc8d2004-06-10 10:50:08 +00001438 db->xCollNeeded = xCollNeeded;
1439 db->xCollNeeded16 = 0;
1440 db->pCollNeededArg = pCollNeededArg;
1441 return SQLITE_OK;
1442}
danielk1977d8123362004-06-12 09:25:12 +00001443
drh6c626082004-11-14 21:56:29 +00001444#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00001445/*
1446** Register a collation sequence factory callback with the database handle
1447** db. Replace any previously installed collation sequence factory.
1448*/
danielk19777cedc8d2004-06-10 10:50:08 +00001449int sqlite3_collation_needed16(
1450 sqlite3 *db,
1451 void *pCollNeededArg,
1452 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
1453){
drhc60d0442004-09-30 13:43:13 +00001454 if( sqlite3SafetyCheck(db) ){
1455 return SQLITE_MISUSE;
1456 }
danielk19777cedc8d2004-06-10 10:50:08 +00001457 db->xCollNeeded = 0;
1458 db->xCollNeeded16 = xCollNeeded16;
1459 db->pCollNeededArg = pCollNeededArg;
1460 return SQLITE_OK;
1461}
drh6c626082004-11-14 21:56:29 +00001462#endif /* SQLITE_OMIT_UTF16 */
danielk19776b456a22005-03-21 04:04:02 +00001463
1464#ifndef SQLITE_OMIT_GLOBALRECOVER
1465/*
1466** This function is called to recover from a malloc failure that occured
1467** within SQLite.
1468**
1469** This function is *not* threadsafe. Calling this from within a threaded
1470** application when threads other than the caller have used SQLite is
1471** dangerous and will almost certainly result in malfunctions.
1472*/
1473int sqlite3_global_recover(){
1474 int rc = SQLITE_OK;
1475
1476 if( sqlite3_malloc_failed ){
1477 sqlite3 *db;
1478 int i;
1479 sqlite3_malloc_failed = 0;
1480 for(db=pDbList; db; db=db->pNext ){
1481 sqlite3ExpirePreparedStatements(db);
1482 for(i=0; i<db->nDb; i++){
1483 Btree *pBt = db->aDb[i].pBt;
1484 if( pBt && (rc=sqlite3BtreeReset(pBt)) ){
1485 goto recover_out;
1486 }
1487 }
1488 db->autoCommit = 1;
1489 }
1490 }
1491
1492recover_out:
1493 if( rc!=SQLITE_OK ){
1494 sqlite3_malloc_failed = 1;
1495 }
1496 return rc;
1497}
1498#endif