blob: dca52977c432e20a7eee9d8d0c5f5f7b53434e92 [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**
danielk19776b456a22005-03-21 04:04:02 +000017** $Id: main.c,v 1.283 2005/03/21 04:04:03 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
50/*
drh75897232000-05-29 14:26:00 +000051** This is the callback routine for the code that initializes the
danielk19774adee202004-05-08 08:23:19 +000052** database. See sqlite3Init() below for additional information.
drh234c39d2004-07-24 03:30:47 +000053** This routine is also called from the OP_ParseSchema opcode of the VDBE.
drh382c0242001-10-06 16:33:02 +000054**
55** Each callback contains the following information:
drh28037572000-08-02 13:47:41 +000056**
drh234c39d2004-07-24 03:30:47 +000057** argv[0] = name of thing being created
58** argv[1] = root page number for table or index. NULL for trigger or view.
59** argv[2] = SQL text for the CREATE statement.
60** argv[3] = "1" for temporary files, "0" for main database, "2" or more
drh1c2d8412003-03-31 00:30:47 +000061** for auxiliary database files.
drhd78eeee2001-09-13 16:18:53 +000062**
drh75897232000-05-29 14:26:00 +000063*/
danielk19774adee202004-05-08 08:23:19 +000064int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){
drhc2311722002-07-19 17:46:38 +000065 InitData *pData = (InitData*)pInit;
drh9bb575f2004-09-06 17:24:11 +000066 sqlite3 *db = pData->db;
drh234c39d2004-07-24 03:30:47 +000067 int iDb;
drh75897232000-05-29 14:26:00 +000068
drh234c39d2004-07-24 03:30:47 +000069 assert( argc==4 );
drh98e3e602003-07-27 17:26:22 +000070 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
drh234c39d2004-07-24 03:30:47 +000071 if( argv[1]==0 || argv[3]==0 ){
drh1d85d932004-02-14 23:05:52 +000072 corruptSchema(pData, 0);
drh8bf8dc92003-05-17 17:35:10 +000073 return 1;
74 }
drh234c39d2004-07-24 03:30:47 +000075 iDb = atoi(argv[3]);
76 assert( iDb>=0 && iDb<db->nDb );
77 if( argv[2] && argv[2][0] ){
78 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
79 ** But because db->init.busy is set to 1, no VDBE code is generated
80 ** or executed. All the parser does is build the internal data
81 ** structures that describe the table, index, or view.
82 */
83 char *zErr;
84 int rc;
85 assert( db->init.busy );
86 db->init.iDb = iDb;
87 db->init.newTnum = atoi(argv[1]);
88 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
89 db->init.iDb = 0;
90 if( SQLITE_OK!=rc ){
91 corruptSchema(pData, zErr);
92 sqlite3_free(zErr);
93 return rc;
drhd78eeee2001-09-13 16:18:53 +000094 }
drh234c39d2004-07-24 03:30:47 +000095 }else{
96 /* If the SQL column is blank it means this is an index that
97 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
98 ** constraint for a CREATE TABLE. The index should have already
99 ** been created when we processed the CREATE TABLE. All we have
100 ** to do here is record the root page number for that index.
101 */
102 Index *pIndex;
103 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
104 if( pIndex==0 || pIndex->tnum!=0 ){
105 /* This can occur if there exists an index on a TEMP table which
106 ** has the same name as another index on a permanent index. Since
107 ** the permanent table is hidden by the TEMP table, we can also
108 ** safely ignore the index on the permanent table.
109 */
110 /* Do Nothing */;
111 }else{
112 pIndex->tnum = atoi(argv[1]);
drhd78eeee2001-09-13 16:18:53 +0000113 }
drh28037572000-08-02 13:47:41 +0000114 }
drh234c39d2004-07-24 03:30:47 +0000115 return 0;
drh75897232000-05-29 14:26:00 +0000116}
117
118/*
drh58b95762000-06-02 01:17:37 +0000119** Attempt to read the database schema and initialize internal
drh1c2d8412003-03-31 00:30:47 +0000120** data structures for a single database file. The index of the
121** database file is given by iDb. iDb==0 is used for the main
122** database. iDb==1 should never be used. iDb>=2 is used for
123** auxiliary databases. Return one of the SQLITE_ error codes to
drh58b95762000-06-02 01:17:37 +0000124** indicate success or failure.
drh75897232000-05-29 14:26:00 +0000125*/
drh9bb575f2004-09-06 17:24:11 +0000126static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
drh58b95762000-06-02 01:17:37 +0000127 int rc;
drhe0bc4042002-06-25 01:09:11 +0000128 BtCursor *curMain;
129 int size;
130 Table *pTab;
drh234c39d2004-07-24 03:30:47 +0000131 char const *azArg[5];
drh1c2d8412003-03-31 00:30:47 +0000132 char zDbNum[30];
drha3b321d2004-05-11 09:31:31 +0000133 int meta[10];
drhc2311722002-07-19 17:46:38 +0000134 InitData initData;
danielk1977d008cfe2004-06-19 02:22:10 +0000135 char const *zMasterSchema;
136 char const *zMasterName;
drh58b95762000-06-02 01:17:37 +0000137
138 /*
139 ** The master database table has a structure like this
140 */
drh57196282004-10-06 15:41:16 +0000141 static const char master_schema[] =
drhe0bc4042002-06-25 01:09:11 +0000142 "CREATE TABLE sqlite_master(\n"
143 " type text,\n"
144 " name text,\n"
145 " tbl_name text,\n"
146 " rootpage integer,\n"
147 " sql text\n"
148 ")"
149 ;
drh57196282004-10-06 15:41:16 +0000150 static const char temp_master_schema[] =
drhe0bc4042002-06-25 01:09:11 +0000151 "CREATE TEMP TABLE sqlite_temp_master(\n"
drh75897232000-05-29 14:26:00 +0000152 " type text,\n"
153 " name text,\n"
154 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000155 " rootpage integer,\n"
drh75897232000-05-29 14:26:00 +0000156 " sql text\n"
157 ")"
158 ;
159
danielk1977d008cfe2004-06-19 02:22:10 +0000160 assert( iDb>=0 && iDb<db->nDb );
drh603240c2002-03-05 01:11:12 +0000161
danielk1977d008cfe2004-06-19 02:22:10 +0000162 /* zMasterSchema and zInitScript are set to point at the master schema
163 ** and initialisation script appropriate for the database being
164 ** initialised. zMasterName is the name of the master table.
drh58b95762000-06-02 01:17:37 +0000165 */
danielk1977d008cfe2004-06-19 02:22:10 +0000166 if( iDb==1 ){
167 zMasterSchema = temp_master_schema;
168 zMasterName = TEMP_MASTER_NAME;
169 }else{
170 zMasterSchema = master_schema;
171 zMasterName = MASTER_NAME;
172 }
173
174 /* Construct the schema tables. */
danielk19774adee202004-05-08 08:23:19 +0000175 sqlite3SafetyOff(db);
drh234c39d2004-07-24 03:30:47 +0000176 azArg[0] = zMasterName;
177 azArg[1] = "1";
178 azArg[2] = zMasterSchema;
drh1c2d8412003-03-31 00:30:47 +0000179 sprintf(zDbNum, "%d", iDb);
drh234c39d2004-07-24 03:30:47 +0000180 azArg[3] = zDbNum;
181 azArg[4] = 0;
drhc2311722002-07-19 17:46:38 +0000182 initData.db = db;
183 initData.pzErrMsg = pzErrMsg;
drh234c39d2004-07-24 03:30:47 +0000184 rc = sqlite3InitCallback(&initData, 4, (char **)azArg, 0);
danielk19772b444852004-06-29 07:45:33 +0000185 if( rc!=SQLITE_OK ){
186 sqlite3SafetyOn(db);
187 return rc;
188 }
danielk1977d008cfe2004-06-19 02:22:10 +0000189 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
drhe0bc4042002-06-25 01:09:11 +0000190 if( pTab ){
191 pTab->readOnly = 1;
drhd8bc7082000-06-07 23:51:50 +0000192 }
danielk19774adee202004-05-08 08:23:19 +0000193 sqlite3SafetyOn(db);
drhe0bc4042002-06-25 01:09:11 +0000194
195 /* Create a cursor to hold the database open
196 */
drhdc3ff9c2004-08-18 02:10:15 +0000197 if( db->aDb[iDb].pBt==0 ){
198 if( iDb==1 ) DbSetProperty(db, 1, DB_SchemaLoaded);
199 return SQLITE_OK;
200 }
danielk19778e150812004-05-10 01:17:37 +0000201 rc = sqlite3BtreeCursor(db->aDb[iDb].pBt, MASTER_ROOT, 0, 0, 0, &curMain);
drhf328bc82004-05-10 23:29:49 +0000202 if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
danielk1977f20b21c2004-05-31 23:56:42 +0000203 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
drh92ed08a2002-07-30 18:43:40 +0000204 return rc;
205 }
drhe0bc4042002-06-25 01:09:11 +0000206
drha3b321d2004-05-11 09:31:31 +0000207 /* Get the database meta information.
208 **
209 ** Meta values are as follows:
210 ** meta[0] Schema cookie. Changes with each schema change.
211 ** meta[1] File format of schema layer.
212 ** meta[2] Size of the page cache.
drhae157872004-08-14 19:20:09 +0000213 ** meta[3] Use freelist if 0. Autovacuum if greater than zero.
danielk1977dc8453f2004-06-12 00:42:34 +0000214 ** meta[4] Db text encoding. 1:UTF-8 3:UTF-16 LE 4:UTF-16 BE
danielk1977dae24952004-11-11 05:10:43 +0000215 ** meta[5] The user cookie. Used by the application.
danielk1977962398d2004-06-14 09:35:16 +0000216 ** meta[6]
drha3b321d2004-05-11 09:31:31 +0000217 ** meta[7]
218 ** meta[8]
219 ** meta[9]
danielk1977172bc392004-05-22 08:09:11 +0000220 **
danielk1977dc8453f2004-06-12 00:42:34 +0000221 ** Note: The hash defined SQLITE_UTF* symbols in sqliteInt.h correspond to
danielk1977172bc392004-05-22 08:09:11 +0000222 ** the possible values of meta[4].
drhe0bc4042002-06-25 01:09:11 +0000223 */
drhf328bc82004-05-10 23:29:49 +0000224 if( rc==SQLITE_OK ){
225 int i;
drha3b321d2004-05-11 09:31:31 +0000226 for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
danielk1977e0d4b062004-06-28 01:11:46 +0000227 rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, i+1, (u32 *)&meta[i]);
danielk19774adee202004-05-08 08:23:19 +0000228 }
drhf328bc82004-05-10 23:29:49 +0000229 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000230 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
drhf328bc82004-05-10 23:29:49 +0000231 sqlite3BtreeCloseCursor(curMain);
232 return rc;
233 }
234 }else{
235 memset(meta, 0, sizeof(meta));
drhe0bc4042002-06-25 01:09:11 +0000236 }
drha3b321d2004-05-11 09:31:31 +0000237 db->aDb[iDb].schema_cookie = meta[0];
danielk19773df6b252004-05-29 10:23:19 +0000238
239 /* If opening a non-empty database, check the text encoding. For the
240 ** main database, set sqlite3.enc to the encoding of the main database.
241 ** For an attached db, it is an error if the encoding is not the same
242 ** as sqlite3.enc.
243 */
244 if( meta[4] ){ /* text encoding */
245 if( iDb==0 ){
246 /* If opening the main database, set db->enc. */
danielk1977172bc392004-05-22 08:09:11 +0000247 db->enc = (u8)meta[4];
danielk1977466be562004-06-10 02:16:01 +0000248 db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0);
danielk19773df6b252004-05-29 10:23:19 +0000249 }else{
250 /* If opening an attached database, the encoding much match db->enc */
251 if( meta[4]!=db->enc ){
252 sqlite3BtreeCloseCursor(curMain);
253 sqlite3SetString(pzErrMsg, "attached databases must use the same"
254 " text encoding as main database", (char*)0);
255 return SQLITE_ERROR;
256 }
danielk1977172bc392004-05-22 08:09:11 +0000257 }
danielk19773df6b252004-05-29 10:23:19 +0000258 }
259
danielk197791cf71b2004-06-26 06:37:06 +0000260 size = meta[2];
261 if( size==0 ){ size = MAX_PAGES; }
262 db->aDb[iDb].cache_size = size;
drhe0bc4042002-06-25 01:09:11 +0000263
danielk197791cf71b2004-06-26 06:37:06 +0000264 if( iDb==0 ){
danielk19773df6b252004-05-29 10:23:19 +0000265 db->file_format = meta[1];
drh1c2d8412003-03-31 00:30:47 +0000266 if( db->file_format==0 ){
267 /* This happens if the database was initially empty */
drhf328bc82004-05-10 23:29:49 +0000268 db->file_format = 1;
drh1c2d8412003-03-31 00:30:47 +0000269 }
danielk197736963fd2005-02-19 08:18:05 +0000270
danielk1977aee18ef2005-03-09 12:26:50 +0000271 if( db->file_format==2 || db->file_format==3 ){
danielk197736963fd2005-02-19 08:18:05 +0000272 /* File format 2 is treated exactly as file format 1. New
273 ** databases are created with file format 1.
274 */
275 db->file_format = 1;
276 }
drh28037572000-08-02 13:47:41 +0000277 }
danielk19773df6b252004-05-29 10:23:19 +0000278
279 /*
danielk197736963fd2005-02-19 08:18:05 +0000280 ** file_format==1 Version 3.0.0.
281 ** file_format==2 Version 3.1.3.
danielk1977aee18ef2005-03-09 12:26:50 +0000282 ** file_format==3 Version 3.1.4.
danielk197736963fd2005-02-19 08:18:05 +0000283 **
284 ** Version 3.0 can only use files with file_format==1. Version 3.1.3
285 ** can read and write files with file_format==1 or file_format==2.
danielk1977aee18ef2005-03-09 12:26:50 +0000286 ** Version 3.1.4 can read and write file formats 1, 2 and 3.
danielk19773df6b252004-05-29 10:23:19 +0000287 */
danielk1977aee18ef2005-03-09 12:26:50 +0000288 if( meta[1]>3 ){
danielk19773df6b252004-05-29 10:23:19 +0000289 sqlite3BtreeCloseCursor(curMain);
290 sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
291 return SQLITE_ERROR;
292 }
293
danielk197791cf71b2004-06-26 06:37:06 +0000294 sqlite3BtreeSetCacheSize(db->aDb[iDb].pBt, db->aDb[iDb].cache_size);
drhaacc5432002-01-06 17:07:40 +0000295
drhe0bc4042002-06-25 01:09:11 +0000296 /* Read the schema information out of the schema tables
drhaacc5432002-01-06 17:07:40 +0000297 */
drh1d85d932004-02-14 23:05:52 +0000298 assert( db->init.busy );
drhf328bc82004-05-10 23:29:49 +0000299 if( rc==SQLITE_EMPTY ){
300 /* For an empty database, there is nothing to read */
301 rc = SQLITE_OK;
drh1c2d8412003-03-31 00:30:47 +0000302 }else{
drh234c39d2004-07-24 03:30:47 +0000303 char *zSql;
304 zSql = sqlite3MPrintf(
danielk1977c60e9b82005-01-31 12:42:29 +0000305 "SELECT name, rootpage, sql, '%s' FROM '%q'.%s",
drh234c39d2004-07-24 03:30:47 +0000306 zDbNum, db->aDb[iDb].zName, zMasterName);
danielk19773df6b252004-05-29 10:23:19 +0000307 sqlite3SafetyOff(db);
danielk1977d008cfe2004-06-19 02:22:10 +0000308 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
drhf328bc82004-05-10 23:29:49 +0000309 sqlite3SafetyOn(db);
drh234c39d2004-07-24 03:30:47 +0000310 sqliteFree(zSql);
drhf328bc82004-05-10 23:29:49 +0000311 sqlite3BtreeCloseCursor(curMain);
drh1c2d8412003-03-31 00:30:47 +0000312 }
danielk197724b03fd2004-05-10 10:34:34 +0000313 if( sqlite3_malloc_failed ){
danielk19774adee202004-05-08 08:23:19 +0000314 sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
drh1d85d932004-02-14 23:05:52 +0000315 rc = SQLITE_NOMEM;
danielk19774adee202004-05-08 08:23:19 +0000316 sqlite3ResetInternalSchema(db, 0);
drhe0bc4042002-06-25 01:09:11 +0000317 }
drh1d85d932004-02-14 23:05:52 +0000318 if( rc==SQLITE_OK ){
drh8bf8dc92003-05-17 17:35:10 +0000319 DbSetProperty(db, iDb, DB_SchemaLoaded);
drh1c2d8412003-03-31 00:30:47 +0000320 }else{
danielk19774adee202004-05-08 08:23:19 +0000321 sqlite3ResetInternalSchema(db, iDb);
drh1c2d8412003-03-31 00:30:47 +0000322 }
drh1d85d932004-02-14 23:05:52 +0000323 return rc;
drh1c2d8412003-03-31 00:30:47 +0000324}
325
326/*
327** Initialize all database files - the main database file, the file
328** used to store temporary tables, and any additional database files
329** created using ATTACH statements. Return a success code. If an
330** error occurs, write an error message into *pzErrMsg.
331**
332** After the database is initialized, the SQLITE_Initialized
danielk19778e227872004-06-07 07:52:17 +0000333** bit is set in the flags field of the sqlite structure.
drh1c2d8412003-03-31 00:30:47 +0000334*/
drh9bb575f2004-09-06 17:24:11 +0000335int sqlite3Init(sqlite3 *db, char **pzErrMsg){
drh1c2d8412003-03-31 00:30:47 +0000336 int i, rc;
337
drh1d85d932004-02-14 23:05:52 +0000338 if( db->init.busy ) return SQLITE_OK;
drh1c2d8412003-03-31 00:30:47 +0000339 assert( (db->flags & SQLITE_Initialized)==0 );
340 rc = SQLITE_OK;
drh1d85d932004-02-14 23:05:52 +0000341 db->init.busy = 1;
drh1c2d8412003-03-31 00:30:47 +0000342 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk1977d008cfe2004-06-19 02:22:10 +0000343 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
danielk19774adee202004-05-08 08:23:19 +0000344 rc = sqlite3InitOne(db, i, pzErrMsg);
drh8ef83ff2004-02-12 15:31:21 +0000345 if( rc ){
danielk19774adee202004-05-08 08:23:19 +0000346 sqlite3ResetInternalSchema(db, i);
drh8ef83ff2004-02-12 15:31:21 +0000347 }
drh1c2d8412003-03-31 00:30:47 +0000348 }
danielk1977d008cfe2004-06-19 02:22:10 +0000349
350 /* Once all the other databases have been initialised, load the schema
351 ** for the TEMP database. This is loaded last, as the TEMP database
352 ** schema may contain references to objects in other databases.
353 */
354 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
355 rc = sqlite3InitOne(db, 1, pzErrMsg);
356 if( rc ){
357 sqlite3ResetInternalSchema(db, 1);
358 }
359 }
360
drh1d85d932004-02-14 23:05:52 +0000361 db->init.busy = 0;
drh1c2d8412003-03-31 00:30:47 +0000362 if( rc==SQLITE_OK ){
drh58b95762000-06-02 01:17:37 +0000363 db->flags |= SQLITE_Initialized;
danielk19774adee202004-05-08 08:23:19 +0000364 sqlite3CommitInternalChanges(db);
drh2d71ca92004-02-10 02:27:04 +0000365 }
366
drh2d71ca92004-02-10 02:27:04 +0000367 if( rc!=SQLITE_OK ){
drhe0bc4042002-06-25 01:09:11 +0000368 db->flags &= ~SQLITE_Initialized;
drh58b95762000-06-02 01:17:37 +0000369 }
drh1c2d8412003-03-31 00:30:47 +0000370 return rc;
drh58b95762000-06-02 01:17:37 +0000371}
372
373/*
danielk19778e227872004-06-07 07:52:17 +0000374** This routine is a no-op if the database schema is already initialised.
375** Otherwise, the schema is loaded. An error code is returned.
376*/
danielk19778a414492004-06-29 08:59:35 +0000377int sqlite3ReadSchema(Parse *pParse){
danielk19778e227872004-06-07 07:52:17 +0000378 int rc = SQLITE_OK;
danielk19778a414492004-06-29 08:59:35 +0000379 sqlite3 *db = pParse->db;
danielk19778e227872004-06-07 07:52:17 +0000380 if( !db->init.busy ){
381 if( (db->flags & SQLITE_Initialized)==0 ){
danielk19778a414492004-06-29 08:59:35 +0000382 rc = sqlite3Init(db, &pParse->zErrMsg);
danielk19778e227872004-06-07 07:52:17 +0000383 }
384 }
danielk1977c0391392004-06-09 12:30:04 +0000385 assert( rc!=SQLITE_OK || (db->flags & SQLITE_Initialized)||db->init.busy );
danielk19778a414492004-06-29 08:59:35 +0000386 if( rc!=SQLITE_OK ){
387 pParse->rc = rc;
388 pParse->nErr++;
389 }
danielk19778e227872004-06-07 07:52:17 +0000390 return rc;
391}
392
393/*
drhb217a572000-08-22 13:40:18 +0000394** The version of the library
395*/
drh38f82712004-06-18 17:10:16 +0000396const char rcsid3[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $";
danielk197724b03fd2004-05-10 10:34:34 +0000397const char sqlite3_version[] = SQLITE_VERSION;
drh4aec8b62004-08-28 16:19:00 +0000398const char *sqlite3_libversion(void){ return sqlite3_version; }
danielk197799ba19e2005-02-05 07:33:34 +0000399int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
drhb217a572000-08-22 13:40:18 +0000400
401/*
drhd3d39e92004-05-20 22:16:29 +0000402** This is the default collating function named "BINARY" which is always
403** available.
404*/
danielk19772c336542005-01-13 02:14:23 +0000405static int binCollFunc(
drhd3d39e92004-05-20 22:16:29 +0000406 void *NotUsed,
407 int nKey1, const void *pKey1,
408 int nKey2, const void *pKey2
409){
410 int rc, n;
411 n = nKey1<nKey2 ? nKey1 : nKey2;
412 rc = memcmp(pKey1, pKey2, n);
413 if( rc==0 ){
414 rc = nKey1 - nKey2;
415 }
416 return rc;
417}
418
419/*
danielk1977dc1bdc42004-06-11 10:51:27 +0000420** Another built-in collating sequence: NOCASE.
421**
422** This collating sequence is intended to be used for "case independant
423** comparison". SQLite's knowledge of upper and lower case equivalents
424** extends only to the 26 characters used in the English language.
425**
426** At the moment there is only a UTF-8 implementation.
danielk19770202b292004-06-09 09:55:16 +0000427*/
428static int nocaseCollatingFunc(
429 void *NotUsed,
430 int nKey1, const void *pKey1,
431 int nKey2, const void *pKey2
432){
433 int r = sqlite3StrNICmp(
drh208f80a2004-08-29 20:08:58 +0000434 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
danielk19770202b292004-06-09 09:55:16 +0000435 if( 0==r ){
436 r = nKey1-nKey2;
437 }
438 return r;
439}
440
441/*
drhaf9ff332002-01-16 21:00:27 +0000442** Return the ROWID of the most recent insert
443*/
drh9bb575f2004-09-06 17:24:11 +0000444sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
drhaf9ff332002-01-16 21:00:27 +0000445 return db->lastRowid;
446}
447
448/*
danielk197724b03fd2004-05-10 10:34:34 +0000449** Return the number of changes in the most recent call to sqlite3_exec().
drhc8d30ac2002-04-12 10:08:59 +0000450*/
drh9bb575f2004-09-06 17:24:11 +0000451int sqlite3_changes(sqlite3 *db){
drhc8d30ac2002-04-12 10:08:59 +0000452 return db->nChange;
453}
454
rdcf146a772004-02-25 22:51:06 +0000455/*
danielk1977b28af712004-06-21 06:50:26 +0000456** Return the number of changes since the database handle was opened.
rdcf146a772004-02-25 22:51:06 +0000457*/
danielk1977b28af712004-06-21 06:50:26 +0000458int sqlite3_total_changes(sqlite3 *db){
459 return db->nTotalChange;
rdcb0c374f2004-02-20 22:53:38 +0000460}
461
drhc8d30ac2002-04-12 10:08:59 +0000462/*
drh50e5dad2001-09-15 00:57:28 +0000463** Close an existing SQLite database
464*/
drh9bb575f2004-09-06 17:24:11 +0000465int sqlite3_close(sqlite3 *db){
drh8e0a2f92002-02-23 23:45:45 +0000466 HashElem *i;
drh001bbcb2003-03-19 03:14:00 +0000467 int j;
danielk19775c4c7782004-06-16 10:39:23 +0000468
469 if( !db ){
danielk197796d81f92004-06-19 03:33:57 +0000470 return SQLITE_OK;
danielk19775c4c7782004-06-16 10:39:23 +0000471 }
drhc60d0442004-09-30 13:43:13 +0000472 if( sqlite3SafetyCheck(db) ){
danielk1977e35ee192004-06-26 09:50:11 +0000473 return SQLITE_MISUSE;
474 }
475
danielk197796d81f92004-06-19 03:33:57 +0000476 /* If there are any outstanding VMs, return SQLITE_BUSY. */
477 if( db->pVdbe ){
478 sqlite3Error(db, SQLITE_BUSY,
479 "Unable to close due to unfinalised statements");
480 return SQLITE_BUSY;
481 }
482 assert( !sqlite3SafetyCheck(db) );
danielk1977e0048402004-06-15 16:51:01 +0000483
484 /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database
485 ** cannot be opened for some reason. So this routine needs to run in
486 ** that case. But maybe there should be an extra magic value for the
487 ** "failed to open" state.
488 */
danielk197796d81f92004-06-19 03:33:57 +0000489 if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){
drh94e92032003-02-16 22:21:32 +0000490 /* printf("DID NOT CLOSE\n"); fflush(stdout); */
danielk197796d81f92004-06-19 03:33:57 +0000491 return SQLITE_ERROR;
drh94e92032003-02-16 22:21:32 +0000492 }
danielk1977e0048402004-06-15 16:51:01 +0000493
drh001bbcb2003-03-19 03:14:00 +0000494 for(j=0; j<db->nDb; j++){
drh4d189ca2004-02-12 18:46:38 +0000495 struct Db *pDb = &db->aDb[j];
496 if( pDb->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000497 sqlite3BtreeClose(pDb->pBt);
drh4d189ca2004-02-12 18:46:38 +0000498 pDb->pBt = 0;
drh113088e2003-03-20 01:16:58 +0000499 }
drhf57b3392001-10-08 13:22:32 +0000500 }
danielk19774adee202004-05-08 08:23:19 +0000501 sqlite3ResetInternalSchema(db, 0);
drh1c2d8412003-03-31 00:30:47 +0000502 assert( db->nDb<=2 );
503 assert( db->aDb==db->aDbStatic );
drh0bce8352002-02-28 00:41:10 +0000504 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
505 FuncDef *pFunc, *pNext;
506 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
drh8e0a2f92002-02-23 23:45:45 +0000507 pNext = pFunc->pNext;
508 sqliteFree(pFunc);
509 }
510 }
danielk1977466be562004-06-10 02:16:01 +0000511
danielk1977d8123362004-06-12 09:25:12 +0000512 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
danielk1977466be562004-06-10 02:16:01 +0000513 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
danielk1977d8123362004-06-12 09:25:12 +0000514 sqliteFree(pColl);
danielk1977466be562004-06-10 02:16:01 +0000515 }
danielk1977d8123362004-06-12 09:25:12 +0000516 sqlite3HashClear(&db->aCollSeq);
danielk1977466be562004-06-10 02:16:01 +0000517
danielk19774adee202004-05-08 08:23:19 +0000518 sqlite3HashClear(&db->aFunc);
danielk19776622cce2004-05-20 11:00:52 +0000519 sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
danielk1977bfd6cce2004-06-18 04:24:54 +0000520 if( db->pValue ){
521 sqlite3ValueFree(db->pValue);
522 }
523 if( db->pErr ){
524 sqlite3ValueFree(db->pErr);
525 }
danielk197796d81f92004-06-19 03:33:57 +0000526
danielk19776b456a22005-03-21 04:04:02 +0000527#ifndef SQLITE_OMIT_GLOBALRECOVER
528 {
529 sqlite3 *pPrev = pDbList;
530 sqlite3OsEnterMutex();
531 while( pPrev && pPrev->pNext!=db ){
532 pPrev = pPrev->pNext;
533 }
534 if( pPrev ){
535 pPrev->pNext = db->pNext;
536 }else{
537 assert( pDbList==db );
538 pDbList = db->pNext;
539 }
540 sqlite3OsLeaveMutex();
541 }
542#endif
543
danielk197796d81f92004-06-19 03:33:57 +0000544 db->magic = SQLITE_MAGIC_ERROR;
drh75897232000-05-29 14:26:00 +0000545 sqliteFree(db);
danielk197796d81f92004-06-19 03:33:57 +0000546 return SQLITE_OK;
drh75897232000-05-29 14:26:00 +0000547}
548
549/*
drh001bbcb2003-03-19 03:14:00 +0000550** Rollback all database files.
551*/
drh9bb575f2004-09-06 17:24:11 +0000552void sqlite3RollbackAll(sqlite3 *db){
drh001bbcb2003-03-19 03:14:00 +0000553 int i;
554 for(i=0; i<db->nDb; i++){
555 if( db->aDb[i].pBt ){
danielk19774adee202004-05-08 08:23:19 +0000556 sqlite3BtreeRollback(db->aDb[i].pBt);
drh001bbcb2003-03-19 03:14:00 +0000557 db->aDb[i].inTrans = 0;
558 }
559 }
danielk19774adee202004-05-08 08:23:19 +0000560 sqlite3ResetInternalSchema(db, 0);
drh001bbcb2003-03-19 03:14:00 +0000561}
562
563/*
drhc22bd472002-05-10 13:14:07 +0000564** Return a static string that describes the kind of error specified in the
565** argument.
drh247be432002-05-10 05:44:55 +0000566*/
danielk1977f20b21c2004-05-31 23:56:42 +0000567const char *sqlite3ErrStr(int rc){
drhc22bd472002-05-10 13:14:07 +0000568 const char *z;
569 switch( rc ){
drhc60d0442004-09-30 13:43:13 +0000570 case SQLITE_ROW:
571 case SQLITE_DONE:
drhc22bd472002-05-10 13:14:07 +0000572 case SQLITE_OK: z = "not an error"; break;
573 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
574 case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break;
575 case SQLITE_PERM: z = "access permission denied"; break;
576 case SQLITE_ABORT: z = "callback requested query abort"; break;
577 case SQLITE_BUSY: z = "database is locked"; break;
578 case SQLITE_LOCKED: z = "database table is locked"; break;
579 case SQLITE_NOMEM: z = "out of memory"; break;
580 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
581 case SQLITE_INTERRUPT: z = "interrupted"; break;
582 case SQLITE_IOERR: z = "disk I/O error"; break;
583 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
584 case SQLITE_NOTFOUND: z = "table or record not found"; break;
585 case SQLITE_FULL: z = "database is full"; break;
586 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
587 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
588 case SQLITE_EMPTY: z = "table contains no data"; break;
589 case SQLITE_SCHEMA: z = "database schema has changed"; break;
590 case SQLITE_TOOBIG: z = "too much data for one table row"; break;
591 case SQLITE_CONSTRAINT: z = "constraint failed"; break;
592 case SQLITE_MISMATCH: z = "datatype mismatch"; break;
593 case SQLITE_MISUSE: z = "library routine called out of sequence";break;
drh8766c342002-11-09 00:33:15 +0000594 case SQLITE_NOLFS: z = "kernel lacks large file support"; break;
drhed6c8672003-01-12 18:02:16 +0000595 case SQLITE_AUTH: z = "authorization denied"; break;
jplyon892f6712003-06-12 08:59:00 +0000596 case SQLITE_FORMAT: z = "auxiliary database format error"; break;
drhb08153d2004-11-20 20:18:55 +0000597 case SQLITE_RANGE: z = "bind or column index out of range"; break;
drhc602f9a2004-02-12 19:01:04 +0000598 case SQLITE_NOTADB: z = "file is encrypted or is not a database";break;
drhc22bd472002-05-10 13:14:07 +0000599 default: z = "unknown error"; break;
drh247be432002-05-10 05:44:55 +0000600 }
drhc22bd472002-05-10 13:14:07 +0000601 return z;
drh247be432002-05-10 05:44:55 +0000602}
603
604/*
drh2dfbbca2000-07-28 14:32:48 +0000605** This routine implements a busy callback that sleeps and tries
606** again until a timeout value is reached. The timeout value is
607** an integer number of milliseconds passed in as the first
608** argument.
609*/
drhdaffd0e2001-04-11 14:28:42 +0000610static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000611 void *Timeout, /* Maximum amount of time to wait */
drh2dfbbca2000-07-28 14:32:48 +0000612 int count /* Number of times table has been busy */
613){
drh8cfbf082001-09-19 13:22:39 +0000614#if SQLITE_MIN_SLEEP_MS==1
drhd1bec472004-01-15 13:29:31 +0000615 static const char delays[] =
616 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 50, 100};
617 static const short int totals[] =
618 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228, 287};
619# define NDELAY (sizeof(delays)/sizeof(delays[0]))
drhc44af712004-09-02 15:53:56 +0000620 ptr timeout = (ptr)Timeout;
621 ptr delay, prior;
drh2dfbbca2000-07-28 14:32:48 +0000622
drhd1bec472004-01-15 13:29:31 +0000623 if( count <= NDELAY ){
624 delay = delays[count-1];
625 prior = totals[count-1];
626 }else{
627 delay = delays[NDELAY-1];
628 prior = totals[NDELAY-1] + delay*(count-NDELAY-1);
drh2dfbbca2000-07-28 14:32:48 +0000629 }
drhd1bec472004-01-15 13:29:31 +0000630 if( prior + delay > timeout ){
631 delay = timeout - prior;
drh2dfbbca2000-07-28 14:32:48 +0000632 if( delay<=0 ) return 0;
633 }
danielk19774adee202004-05-08 08:23:19 +0000634 sqlite3OsSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000635 return 1;
636#else
637 int timeout = (int)Timeout;
638 if( (count+1)*1000 > timeout ){
639 return 0;
640 }
danielk19774adee202004-05-08 08:23:19 +0000641 sqlite3OsSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000642 return 1;
643#endif
644}
645
646/*
647** This routine sets the busy callback for an Sqlite database to the
648** given callback function with the given argument.
649*/
danielk1977f9d64d22004-06-19 08:18:07 +0000650int sqlite3_busy_handler(
651 sqlite3 *db,
danielk19772a764eb2004-06-12 01:43:26 +0000652 int (*xBusy)(void*,int),
drh2dfbbca2000-07-28 14:32:48 +0000653 void *pArg
654){
drhc60d0442004-09-30 13:43:13 +0000655 if( sqlite3SafetyCheck(db) ){
656 return SQLITE_MISUSE;
657 }
danielk197724162fe2004-06-04 06:22:00 +0000658 db->busyHandler.xFunc = xBusy;
659 db->busyHandler.pArg = pArg;
danielk1977f9d64d22004-06-19 08:18:07 +0000660 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000661}
662
danielk1977348bb5d2003-10-18 09:37:26 +0000663#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
664/*
665** This routine sets the progress callback for an Sqlite database to the
666** given callback function with the given argument. The progress callback will
667** be invoked every nOps opcodes.
668*/
danielk197724b03fd2004-05-10 10:34:34 +0000669void sqlite3_progress_handler(
drh9bb575f2004-09-06 17:24:11 +0000670 sqlite3 *db,
danielk1977348bb5d2003-10-18 09:37:26 +0000671 int nOps,
672 int (*xProgress)(void*),
673 void *pArg
674){
drhc60d0442004-09-30 13:43:13 +0000675 if( !sqlite3SafetyCheck(db) ){
676 if( nOps>0 ){
677 db->xProgress = xProgress;
678 db->nProgressOps = nOps;
679 db->pProgressArg = pArg;
680 }else{
681 db->xProgress = 0;
682 db->nProgressOps = 0;
683 db->pProgressArg = 0;
684 }
danielk1977348bb5d2003-10-18 09:37:26 +0000685 }
686}
687#endif
688
689
drh2dfbbca2000-07-28 14:32:48 +0000690/*
691** This routine installs a default busy handler that waits for the
692** specified number of milliseconds before returning 0.
693*/
danielk1977f9d64d22004-06-19 08:18:07 +0000694int sqlite3_busy_timeout(sqlite3 *db, int ms){
drh2dfbbca2000-07-28 14:32:48 +0000695 if( ms>0 ){
drhc44af712004-09-02 15:53:56 +0000696 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)(ptr)ms);
drh2dfbbca2000-07-28 14:32:48 +0000697 }else{
danielk197724b03fd2004-05-10 10:34:34 +0000698 sqlite3_busy_handler(db, 0, 0);
drh2dfbbca2000-07-28 14:32:48 +0000699 }
danielk1977f9d64d22004-06-19 08:18:07 +0000700 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000701}
drh4c504392000-10-16 22:06:40 +0000702
703/*
704** Cause any pending operation to stop at its earliest opportunity.
705*/
drh9bb575f2004-09-06 17:24:11 +0000706void sqlite3_interrupt(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000707 if( !sqlite3SafetyCheck(db) ){
708 db->flags |= SQLITE_Interrupt;
709 }
drh4c504392000-10-16 22:06:40 +0000710}
drhfa86c412002-02-02 15:01:15 +0000711
712/*
713** Windows systems should call this routine to free memory that
danielk197724b03fd2004-05-10 10:34:34 +0000714** is returned in the in the errmsg parameter of sqlite3_open() when
drhfa86c412002-02-02 15:01:15 +0000715** SQLite is a DLL. For some reason, it does not work to call free()
716** directly.
717**
drhae29ffb2004-09-25 14:39:18 +0000718** Note that we need to call free() not sqliteFree() here.
drhfa86c412002-02-02 15:01:15 +0000719*/
drh3f4fedb2004-05-31 19:34:33 +0000720void sqlite3_free(char *p){ free(p); }
drhfa86c412002-02-02 15:01:15 +0000721
722/*
drhdf014892004-06-02 00:41:09 +0000723** Create new user functions.
drhfa86c412002-02-02 15:01:15 +0000724*/
danielk197724b03fd2004-05-10 10:34:34 +0000725int sqlite3_create_function(
danielk197765904932004-05-26 06:18:37 +0000726 sqlite3 *db,
727 const char *zFunctionName,
728 int nArg,
danielk1977d8123362004-06-12 09:25:12 +0000729 int enc,
danielk197765904932004-05-26 06:18:37 +0000730 void *pUserData,
731 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
732 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
733 void (*xFinal)(sqlite3_context*)
drh8e0a2f92002-02-23 23:45:45 +0000734){
drh0bce8352002-02-28 00:41:10 +0000735 FuncDef *p;
drh4b59ab52002-08-24 18:24:51 +0000736 int nName;
danielk197765904932004-05-26 06:18:37 +0000737
drhc60d0442004-09-30 13:43:13 +0000738 if( sqlite3SafetyCheck(db) ){
739 return SQLITE_MISUSE;
740 }
741 if( zFunctionName==0 ||
danielk1977398eae72004-05-26 06:58:43 +0000742 (xFunc && (xFinal || xStep)) ||
743 (!xFunc && (xFinal && !xStep)) ||
744 (!xFunc && (!xFinal && xStep)) ||
745 (nArg<-1 || nArg>127) ||
746 (255<(nName = strlen(zFunctionName))) ){
danielk197765904932004-05-26 06:18:37 +0000747 return SQLITE_ERROR;
748 }
danielk1977d8123362004-06-12 09:25:12 +0000749
danielk197793758c82005-01-21 08:13:14 +0000750#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +0000751 /* If SQLITE_UTF16 is specified as the encoding type, transform this
752 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
753 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
754 **
755 ** If SQLITE_ANY is specified, add three versions of the function
756 ** to the hash table.
757 */
758 if( enc==SQLITE_UTF16 ){
759 enc = SQLITE_UTF16NATIVE;
760 }else if( enc==SQLITE_ANY ){
761 int rc;
762 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF8,
danielk1977f9d64d22004-06-19 08:18:07 +0000763 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000764 if( rc!=SQLITE_OK ) return rc;
765 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF16LE,
danielk1977f9d64d22004-06-19 08:18:07 +0000766 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000767 if( rc!=SQLITE_OK ) return rc;
768 enc = SQLITE_UTF16BE;
769 }
danielk197793758c82005-01-21 08:13:14 +0000770#else
771 enc = SQLITE_UTF8;
772#endif
danielk19779636c4e2005-01-25 04:27:54 +0000773
774 /* Check if an existing function is being overridden or deleted. If so,
775 ** and there are active VMs, then return SQLITE_BUSY. If a function
776 ** is being overridden/deleted but there are no active VMs, allow the
777 ** operation to continue but invalidate all precompiled statements.
778 */
779 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0);
780 if( p && p->iPrefEnc==enc && p->nArg==nArg ){
781 if( db->activeVdbeCnt ){
782 sqlite3Error(db, SQLITE_BUSY,
783 "Unable to delete/modify user-function due to active statements");
784 return SQLITE_BUSY;
785 }else{
786 sqlite3ExpirePreparedStatements(db);
787 }
788 }
danielk197765904932004-05-26 06:18:37 +0000789
danielk1977d8123362004-06-12 09:25:12 +0000790 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);
danielk197713073932004-06-30 11:54:06 +0000791 if( p==0 ) return SQLITE_NOMEM;
drh8e0a2f92002-02-23 23:45:45 +0000792 p->xFunc = xFunc;
drh8e0a2f92002-02-23 23:45:45 +0000793 p->xStep = xStep;
danielk197765904932004-05-26 06:18:37 +0000794 p->xFinalize = xFinal;
drh1350b032002-02-27 19:00:20 +0000795 p->pUserData = pUserData;
danielk197765904932004-05-26 06:18:37 +0000796 return SQLITE_OK;
797}
danielk197793758c82005-01-21 08:13:14 +0000798#ifndef SQLITE_OMIT_UTF16
danielk197765904932004-05-26 06:18:37 +0000799int sqlite3_create_function16(
800 sqlite3 *db,
801 const void *zFunctionName,
802 int nArg,
803 int eTextRep,
danielk197765904932004-05-26 06:18:37 +0000804 void *pUserData,
805 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
806 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
807 void (*xFinal)(sqlite3_context*)
808){
809 int rc;
danielk1977bfd6cce2004-06-18 04:24:54 +0000810 char const *zFunc8;
drhc60d0442004-09-30 13:43:13 +0000811 sqlite3_value *pTmp;
danielk1977bfd6cce2004-06-18 04:24:54 +0000812
drhc60d0442004-09-30 13:43:13 +0000813 if( sqlite3SafetyCheck(db) ){
814 return SQLITE_MISUSE;
815 }
816 pTmp = sqlite3GetTransientValue(db);
danielk1977bfd6cce2004-06-18 04:24:54 +0000817 sqlite3ValueSetStr(pTmp, -1, zFunctionName, SQLITE_UTF16NATIVE,SQLITE_STATIC);
818 zFunc8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
819
820 if( !zFunc8 ){
danielk197765904932004-05-26 06:18:37 +0000821 return SQLITE_NOMEM;
822 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000823 rc = sqlite3_create_function(db, zFunc8, nArg, eTextRep,
danielk1977f9d64d22004-06-19 08:18:07 +0000824 pUserData, xFunc, xStep, xFinal);
danielk197765904932004-05-26 06:18:37 +0000825 return rc;
drh8e0a2f92002-02-23 23:45:45 +0000826}
danielk197793758c82005-01-21 08:13:14 +0000827#endif
drhc9b84a12002-06-20 11:36:48 +0000828
829/*
drh18de4822003-01-16 16:28:53 +0000830** Register a trace function. The pArg from the previously registered trace
831** is returned.
832**
833** A NULL trace function means that no tracing is executes. A non-NULL
834** trace is a pointer to a function that is invoked at the start of each
danielk197724b03fd2004-05-10 10:34:34 +0000835** sqlite3_exec().
drh18de4822003-01-16 16:28:53 +0000836*/
drh9bb575f2004-09-06 17:24:11 +0000837void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
drh18de4822003-01-16 16:28:53 +0000838 void *pOld = db->pTraceArg;
839 db->xTrace = xTrace;
840 db->pTraceArg = pArg;
841 return pOld;
drh0d1a6432003-04-03 15:46:04 +0000842}
paulb0208cc2003-04-13 18:26:49 +0000843
drhaa940ea2004-01-15 02:44:03 +0000844/*** EXPERIMENTAL ***
845**
846** Register a function to be invoked when a transaction comments.
847** If either function returns non-zero, then the commit becomes a
848** rollback.
849*/
danielk197724b03fd2004-05-10 10:34:34 +0000850void *sqlite3_commit_hook(
drh9bb575f2004-09-06 17:24:11 +0000851 sqlite3 *db, /* Attach the hook to this database */
drhaa940ea2004-01-15 02:44:03 +0000852 int (*xCallback)(void*), /* Function to invoke on each commit */
853 void *pArg /* Argument to the function */
854){
855 void *pOld = db->pCommitArg;
856 db->xCommitCallback = xCallback;
857 db->pCommitArg = pArg;
858 return pOld;
859}
860
861
paulb0208cc2003-04-13 18:26:49 +0000862/*
drh13bff812003-04-15 01:19:47 +0000863** This routine is called to create a connection to a database BTree
864** driver. If zFilename is the name of a file, then that file is
865** opened and used. If zFilename is the magic name ":memory:" then
866** the database is stored in memory (and is thus forgotten as soon as
867** the connection is closed.) If zFilename is NULL then the database
868** is for temporary use only and is deleted as soon as the connection
869** is closed.
drh90f5ecb2004-07-22 01:19:35 +0000870**
871** A temporary database can be either a disk file (that is automatically
872** deleted when the file is closed) or a set of red-black trees held in memory,
873** depending on the values of the TEMP_STORE compile-time macro and the
874** db->temp_store variable, according to the following chart:
875**
876** TEMP_STORE db->temp_store Location of temporary database
877** ---------- -------------- ------------------------------
878** 0 any file
879** 1 1 file
880** 1 2 memory
881** 1 0 file
882** 2 1 file
883** 2 2 memory
884** 2 0 memory
885** 3 any memory
paulb0208cc2003-04-13 18:26:49 +0000886*/
danielk19774adee202004-05-08 08:23:19 +0000887int sqlite3BtreeFactory(
drh9bb575f2004-09-06 17:24:11 +0000888 const sqlite3 *db, /* Main database when opening aux otherwise 0 */
paulb0208cc2003-04-13 18:26:49 +0000889 const char *zFilename, /* Name of the file containing the BTree database */
890 int omitJournal, /* if TRUE then do not journal this file */
891 int nCache, /* How many pages in the page cache */
danielk19774adee202004-05-08 08:23:19 +0000892 Btree **ppBtree /* Pointer to new Btree object written here */
893){
danielk19774adee202004-05-08 08:23:19 +0000894 int btree_flags = 0;
drh90f5ecb2004-07-22 01:19:35 +0000895 int rc;
danielk19774adee202004-05-08 08:23:19 +0000896
drheec983e2004-05-08 10:11:36 +0000897 assert( ppBtree != 0);
danielk19774adee202004-05-08 08:23:19 +0000898 if( omitJournal ){
899 btree_flags |= BTREE_OMIT_JOURNAL;
paulb0208cc2003-04-13 18:26:49 +0000900 }
drh7bec5052005-02-06 02:45:41 +0000901 if( db->flags & SQLITE_NoReadlock ){
902 btree_flags |= BTREE_NO_READLOCK;
903 }
drh90f5ecb2004-07-22 01:19:35 +0000904 if( zFilename==0 ){
drh90f5ecb2004-07-22 01:19:35 +0000905#if TEMP_STORE==0
drh22ac46d2004-08-14 18:34:54 +0000906 /* Do nothing */
drh90f5ecb2004-07-22 01:19:35 +0000907#endif
danielk197703aded42004-11-22 05:26:27 +0000908#ifndef SQLITE_OMIT_MEMORYDB
drh90f5ecb2004-07-22 01:19:35 +0000909#if TEMP_STORE==1
drh22ac46d2004-08-14 18:34:54 +0000910 if( db->temp_store==2 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000911#endif
912#if TEMP_STORE==2
drh22ac46d2004-08-14 18:34:54 +0000913 if( db->temp_store!=1 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000914#endif
915#if TEMP_STORE==3
drh22ac46d2004-08-14 18:34:54 +0000916 zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000917#endif
danielk197703aded42004-11-22 05:26:27 +0000918#endif /* SQLITE_OMIT_MEMORYDB */
drh90f5ecb2004-07-22 01:19:35 +0000919 }
danielk19774adee202004-05-08 08:23:19 +0000920
drh90f5ecb2004-07-22 01:19:35 +0000921 rc = sqlite3BtreeOpen(zFilename, ppBtree, btree_flags);
922 if( rc==SQLITE_OK ){
923 sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler);
924 sqlite3BtreeSetCacheSize(*ppBtree, nCache);
925 }
926 return rc;
paulb0208cc2003-04-13 18:26:49 +0000927}
danielk19774adee202004-05-08 08:23:19 +0000928
danielk19774ad17132004-05-21 01:47:26 +0000929/*
930** Return UTF-8 encoded English language explanation of the most recent
931** error.
932*/
danielk19776622cce2004-05-20 11:00:52 +0000933const char *sqlite3_errmsg(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000934 const char *z;
935 if( sqlite3_malloc_failed ){
danielk1977f20b21c2004-05-31 23:56:42 +0000936 return sqlite3ErrStr(SQLITE_NOMEM);
danielk19774ad17132004-05-21 01:47:26 +0000937 }
drhc60d0442004-09-30 13:43:13 +0000938 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
danielk1977e35ee192004-06-26 09:50:11 +0000939 return sqlite3ErrStr(SQLITE_MISUSE);
940 }
drhc60d0442004-09-30 13:43:13 +0000941 z = sqlite3_value_text(db->pErr);
942 if( z==0 ){
943 z = sqlite3ErrStr(db->errCode);
danielk19776622cce2004-05-20 11:00:52 +0000944 }
drhc60d0442004-09-30 13:43:13 +0000945 return z;
danielk19776622cce2004-05-20 11:00:52 +0000946}
947
drh6c626082004-11-14 21:56:29 +0000948#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +0000949/*
950** Return UTF-16 encoded English language explanation of the most recent
951** error.
952*/
danielk19776622cce2004-05-20 11:00:52 +0000953const void *sqlite3_errmsg16(sqlite3 *db){
danielk1977bfd6cce2004-06-18 04:24:54 +0000954 /* Because all the characters in the string are in the unicode
955 ** range 0x00-0xFF, if we pad the big-endian string with a
956 ** zero byte, we can obtain the little-endian string with
957 ** &big_endian[1].
958 */
drh57196282004-10-06 15:41:16 +0000959 static const char outOfMemBe[] = {
danielk1977bfd6cce2004-06-18 04:24:54 +0000960 0, 'o', 0, 'u', 0, 't', 0, ' ',
961 0, 'o', 0, 'f', 0, ' ',
962 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0
963 };
drh57196282004-10-06 15:41:16 +0000964 static const char misuseBe [] = {
danielk1977e35ee192004-06-26 09:50:11 +0000965 0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ',
966 0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ',
967 0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ',
968 0, 'o', 0, 'u', 0, 't', 0, ' ',
969 0, 'o', 0, 'f', 0, ' ',
970 0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0
971 };
danielk19774ad17132004-05-21 01:47:26 +0000972
drhc60d0442004-09-30 13:43:13 +0000973 const void *z;
974 if( sqlite3_malloc_failed ){
975 return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
976 }
977 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
978 return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
979 }
980 z = sqlite3_value_text16(db->pErr);
981 if( z==0 ){
982 sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
983 SQLITE_UTF8, SQLITE_STATIC);
984 z = sqlite3_value_text16(db->pErr);
985 }
986 return z;
danielk19776622cce2004-05-20 11:00:52 +0000987}
drh6c626082004-11-14 21:56:29 +0000988#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +0000989
drhc60d0442004-09-30 13:43:13 +0000990/*
991** Return the most recent error code generated by an SQLite routine.
992*/
danielk19776622cce2004-05-20 11:00:52 +0000993int sqlite3_errcode(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000994 if( sqlite3_malloc_failed ){
995 return SQLITE_NOMEM;
996 }
997 if( sqlite3SafetyCheck(db) ){
998 return SQLITE_MISUSE;
999 }
danielk19776622cce2004-05-20 11:00:52 +00001000 return db->errCode;
1001}
1002
1003/*
drhc275b4e2004-07-19 17:25:24 +00001004** Check schema cookies in all databases. If any cookie is out
drha6ecd332004-06-10 00:29:09 +00001005** of date, return 0. If all schema cookies are current, return 1.
1006*/
drh9bb575f2004-09-06 17:24:11 +00001007static int schemaIsValid(sqlite3 *db){
drha6ecd332004-06-10 00:29:09 +00001008 int iDb;
1009 int rc;
1010 BtCursor *curTemp;
1011 int cookie;
1012 int allOk = 1;
1013
1014 for(iDb=0; allOk && iDb<db->nDb; iDb++){
1015 Btree *pBt;
drha6ecd332004-06-10 00:29:09 +00001016 pBt = db->aDb[iDb].pBt;
1017 if( pBt==0 ) continue;
1018 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
1019 if( rc==SQLITE_OK ){
danielk1977e0d4b062004-06-28 01:11:46 +00001020 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
drha6ecd332004-06-10 00:29:09 +00001021 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].schema_cookie ){
1022 allOk = 0;
1023 }
1024 sqlite3BtreeCloseCursor(curTemp);
1025 }
1026 }
1027 return allOk;
1028}
1029
1030/*
danielk19776622cce2004-05-20 11:00:52 +00001031** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
1032*/
1033int sqlite3_prepare(
1034 sqlite3 *db, /* Database handle. */
1035 const char *zSql, /* UTF-8 encoded SQL statement. */
1036 int nBytes, /* Length of zSql in bytes. */
1037 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1038 const char** pzTail /* OUT: End of parsed string */
1039){
1040 Parse sParse;
1041 char *zErrMsg = 0;
1042 int rc = SQLITE_OK;
1043
danielk197796fb0dd2004-06-30 09:49:22 +00001044 if( sqlite3_malloc_failed ){
1045 return SQLITE_NOMEM;
1046 }
1047
danielk19775c4c7782004-06-16 10:39:23 +00001048 assert( ppStmt );
1049 *ppStmt = 0;
danielk19776622cce2004-05-20 11:00:52 +00001050 if( sqlite3SafetyOn(db) ){
danielk1977e35ee192004-06-26 09:50:11 +00001051 return SQLITE_MISUSE;
danielk19776622cce2004-05-20 11:00:52 +00001052 }
1053
danielk19776622cce2004-05-20 11:00:52 +00001054 memset(&sParse, 0, sizeof(sParse));
1055 sParse.db = db;
1056 sqlite3RunParser(&sParse, zSql, &zErrMsg);
1057
danielk19776622cce2004-05-20 11:00:52 +00001058 if( sqlite3_malloc_failed ){
1059 rc = SQLITE_NOMEM;
1060 sqlite3RollbackAll(db);
1061 sqlite3ResetInternalSchema(db, 0);
1062 db->flags &= ~SQLITE_InTrans;
1063 goto prepare_out;
1064 }
1065 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
drh3f7d4e42004-07-24 14:35:58 +00001066 if( sParse.rc!=SQLITE_OK && sParse.checkSchema && !schemaIsValid(db) ){
drha6ecd332004-06-10 00:29:09 +00001067 sParse.rc = SQLITE_SCHEMA;
1068 }
danielk19776622cce2004-05-20 11:00:52 +00001069 if( sParse.rc==SQLITE_SCHEMA ){
1070 sqlite3ResetInternalSchema(db, 0);
1071 }
danielk19776622cce2004-05-20 11:00:52 +00001072 if( pzTail ) *pzTail = sParse.zTail;
danielk19776622cce2004-05-20 11:00:52 +00001073 rc = sParse.rc;
1074
danielk197793758c82005-01-21 08:13:14 +00001075#ifndef SQLITE_OMIT_EXPLAIN
danielk197722322fd2004-05-25 23:35:17 +00001076 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
1077 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
danielk19773cf86062004-05-26 10:11:05 +00001078 sqlite3VdbeSetColName(sParse.pVdbe, 0, "addr", P3_STATIC);
1079 sqlite3VdbeSetColName(sParse.pVdbe, 1, "opcode", P3_STATIC);
1080 sqlite3VdbeSetColName(sParse.pVdbe, 2, "p1", P3_STATIC);
1081 sqlite3VdbeSetColName(sParse.pVdbe, 3, "p2", P3_STATIC);
1082 sqlite3VdbeSetColName(sParse.pVdbe, 4, "p3", P3_STATIC);
danielk197722322fd2004-05-25 23:35:17 +00001083 }
danielk197793758c82005-01-21 08:13:14 +00001084#endif
danielk197722322fd2004-05-25 23:35:17 +00001085
danielk19776622cce2004-05-20 11:00:52 +00001086prepare_out:
danielk197722322fd2004-05-25 23:35:17 +00001087 if( sqlite3SafetyOff(db) ){
1088 rc = SQLITE_MISUSE;
1089 }
danielk19775c4c7782004-06-16 10:39:23 +00001090 if( rc==SQLITE_OK ){
1091 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
1092 }else if( sParse.pVdbe ){
danielk1977cfe9a692004-06-16 12:00:29 +00001093 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
danielk19775c4c7782004-06-16 10:39:23 +00001094 }
1095
danielk19776622cce2004-05-20 11:00:52 +00001096 if( zErrMsg ){
1097 sqlite3Error(db, rc, "%s", zErrMsg);
danielk1977b20e56b2004-06-15 13:36:30 +00001098 sqliteFree(zErrMsg);
danielk19776622cce2004-05-20 11:00:52 +00001099 }else{
1100 sqlite3Error(db, rc, 0);
1101 }
1102 return rc;
1103}
1104
drh6c626082004-11-14 21:56:29 +00001105#ifndef SQLITE_OMIT_UTF16
danielk19776622cce2004-05-20 11:00:52 +00001106/*
1107** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
1108*/
1109int sqlite3_prepare16(
1110 sqlite3 *db, /* Database handle. */
1111 const void *zSql, /* UTF-8 encoded SQL statement. */
1112 int nBytes, /* Length of zSql in bytes. */
1113 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1114 const void **pzTail /* OUT: End of parsed string */
1115){
1116 /* This function currently works by first transforming the UTF-16
1117 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
1118 ** tricky bit is figuring out the pointer to return in *pzTail.
1119 */
danielk1977bfd6cce2004-06-18 04:24:54 +00001120 char const *zSql8 = 0;
danielk19776622cce2004-05-20 11:00:52 +00001121 char const *zTail8 = 0;
1122 int rc;
danielk1977bfd6cce2004-06-18 04:24:54 +00001123 sqlite3_value *pTmp;
danielk19776622cce2004-05-20 11:00:52 +00001124
drhc60d0442004-09-30 13:43:13 +00001125 if( sqlite3SafetyCheck(db) ){
1126 return SQLITE_MISUSE;
1127 }
danielk1977bfd6cce2004-06-18 04:24:54 +00001128 pTmp = sqlite3GetTransientValue(db);
1129 sqlite3ValueSetStr(pTmp, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1130 zSql8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
danielk19776622cce2004-05-20 11:00:52 +00001131 if( !zSql8 ){
1132 sqlite3Error(db, SQLITE_NOMEM, 0);
1133 return SQLITE_NOMEM;
1134 }
1135 rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);
1136
1137 if( zTail8 && pzTail ){
1138 /* If sqlite3_prepare returns a tail pointer, we calculate the
1139 ** equivalent pointer into the UTF-16 string by counting the unicode
1140 ** characters between zSql8 and zTail8, and then returning a pointer
1141 ** the same number of characters into the UTF-16 string.
1142 */
1143 int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8);
1144 *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed);
1145 }
1146
1147 return rc;
1148}
drh6c626082004-11-14 21:56:29 +00001149#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00001150
danielk19774ad17132004-05-21 01:47:26 +00001151/*
1152** This routine does the work of opening a database on behalf of
1153** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
1154** is UTF-8 encoded. The fourth argument, "def_enc" is one of the TEXT_*
1155** macros from sqliteInt.h. If we end up creating a new database file
1156** (not opening an existing one), the text encoding of the database
1157** will be set to this value.
1158*/
1159static int openDatabase(
1160 const char *zFilename, /* Database filename UTF-8 encoded */
danielk19778e227872004-06-07 07:52:17 +00001161 sqlite3 **ppDb /* OUT: Returned database handle */
danielk19774ad17132004-05-21 01:47:26 +00001162){
1163 sqlite3 *db;
1164 int rc, i;
danielk19774ad17132004-05-21 01:47:26 +00001165
1166 /* Allocate the sqlite data structure */
drh9bb575f2004-09-06 17:24:11 +00001167 db = sqliteMalloc( sizeof(sqlite3) );
danielk19774ad17132004-05-21 01:47:26 +00001168 if( db==0 ) goto opendb_out;
danielk19774ad17132004-05-21 01:47:26 +00001169 db->priorNewRowid = 0;
1170 db->magic = SQLITE_MAGIC_BUSY;
1171 db->nDb = 2;
1172 db->aDb = db->aDbStatic;
danielk1977dc8453f2004-06-12 00:42:34 +00001173 db->enc = SQLITE_UTF8;
danielk19771d850a72004-05-31 08:26:49 +00001174 db->autoCommit = 1;
drh47a6db22005-01-18 16:02:40 +00001175 db->flags |= SQLITE_ShortColNames;
drhf9b596e2004-05-26 16:54:42 +00001176 sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
danielk19774ad17132004-05-21 01:47:26 +00001177 sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
1178 for(i=0; i<db->nDb; i++){
1179 sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
1180 sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
1181 sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);
1182 sqlite3HashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1);
1183 }
danielk19774ad17132004-05-21 01:47:26 +00001184
danielk19770202b292004-06-09 09:55:16 +00001185 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
1186 ** and UTF-16, so add a version for each to avoid any unnecessary
1187 ** conversions. The only error that can occur here is a malloc() failure.
1188 */
danielk19772c336542005-01-13 02:14:23 +00001189 if( sqlite3_create_collation(db, "BINARY", SQLITE_UTF8, 0,binCollFunc) ||
1190 sqlite3_create_collation(db, "BINARY", SQLITE_UTF16, 0,binCollFunc) ||
1191 !(db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0)) ){
danielk19770202b292004-06-09 09:55:16 +00001192 rc = db->errCode;
1193 assert( rc!=SQLITE_OK );
1194 db->magic = SQLITE_MAGIC_CLOSED;
1195 goto opendb_out;
1196 }
1197
1198 /* Also add a UTF-8 case-insensitive collation sequence. */
danielk1977466be562004-06-10 02:16:01 +00001199 sqlite3_create_collation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc);
danielk19770202b292004-06-09 09:55:16 +00001200
danielk19774ad17132004-05-21 01:47:26 +00001201 /* Open the backend database driver */
danielk19774ad17132004-05-21 01:47:26 +00001202 rc = sqlite3BtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt);
1203 if( rc!=SQLITE_OK ){
danielk19774ad17132004-05-21 01:47:26 +00001204 sqlite3Error(db, rc, 0);
1205 db->magic = SQLITE_MAGIC_CLOSED;
1206 goto opendb_out;
1207 }
1208 db->aDb[0].zName = "main";
1209 db->aDb[1].zName = "temp";
1210
danielk197791cf71b2004-06-26 06:37:06 +00001211 /* The default safety_level for the main database is 'full' for the temp
1212 ** database it is 'NONE'. This matches the pager layer defaults. */
1213 db->aDb[0].safety_level = 3;
1214 db->aDb[1].safety_level = 1;
1215
danielk19778e227872004-06-07 07:52:17 +00001216 /* Register all built-in functions, but do not attempt to read the
1217 ** database schema yet. This is delayed until the first time the database
1218 ** is accessed.
1219 */
danielk19774ad17132004-05-21 01:47:26 +00001220 sqlite3RegisterBuiltinFunctions(db);
danielk19774397de52005-01-12 12:44:03 +00001221 sqlite3Error(db, SQLITE_OK, 0);
1222 db->magic = SQLITE_MAGIC_OPEN;
danielk19774ad17132004-05-21 01:47:26 +00001223
1224opendb_out:
danielk197713073932004-06-30 11:54:06 +00001225 if( sqlite3_errcode(db)==SQLITE_OK && sqlite3_malloc_failed ){
1226 sqlite3Error(db, SQLITE_NOMEM, 0);
1227 }
danielk19774ad17132004-05-21 01:47:26 +00001228 *ppDb = db;
danielk19776b456a22005-03-21 04:04:02 +00001229#ifndef SQLITE_OMIT_GLOBALRECOVER
1230 if( db ){
1231 sqlite3OsEnterMutex();
1232 db->pNext = pDbList;
1233 pDbList = db;
1234 sqlite3OsLeaveMutex();
1235 }
1236#endif
danielk19774ad17132004-05-21 01:47:26 +00001237 return sqlite3_errcode(db);
1238}
1239
1240/*
1241** Open a new database handle.
1242*/
danielk197780290862004-05-22 09:21:21 +00001243int sqlite3_open(
danielk19774ad17132004-05-21 01:47:26 +00001244 const char *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00001245 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00001246){
danielk19778e227872004-06-07 07:52:17 +00001247 return openDatabase(zFilename, ppDb);
danielk197783ab5a82004-05-21 11:39:05 +00001248}
1249
drh6c626082004-11-14 21:56:29 +00001250#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +00001251/*
1252** Open a new database handle.
1253*/
1254int sqlite3_open16(
1255 const void *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00001256 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00001257){
danielk1977bfd6cce2004-06-18 04:24:54 +00001258 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
1259 int rc = SQLITE_NOMEM;
1260 sqlite3_value *pVal;
danielk19774ad17132004-05-21 01:47:26 +00001261
1262 assert( ppDb );
danielk1977bfd6cce2004-06-18 04:24:54 +00001263 *ppDb = 0;
1264 pVal = sqlite3ValueNew();
1265 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1266 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
1267 if( zFilename8 ){
1268 rc = openDatabase(zFilename8, ppDb);
1269 if( rc==SQLITE_OK && *ppDb ){
1270 sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
1271 }
danielk19774ad17132004-05-21 01:47:26 +00001272 }
danielk1977bfd6cce2004-06-18 04:24:54 +00001273 if( pVal ){
1274 sqlite3ValueFree(pVal);
danielk19774ad17132004-05-21 01:47:26 +00001275 }
danielk19778e227872004-06-07 07:52:17 +00001276
danielk19774ad17132004-05-21 01:47:26 +00001277 return rc;
1278}
drh6c626082004-11-14 21:56:29 +00001279#endif /* SQLITE_OMIT_UTF16 */
danielk19774ad17132004-05-21 01:47:26 +00001280
danielk1977106bb232004-05-21 10:08:53 +00001281/*
1282** The following routine destroys a virtual machine that is created by
1283** the sqlite3_compile() routine. The integer returned is an SQLITE_
1284** success/failure code that describes the result of executing the virtual
1285** machine.
1286**
1287** This routine sets the error code and string returned by
1288** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
1289*/
danielk1977fc57d7b2004-05-26 02:04:57 +00001290int sqlite3_finalize(sqlite3_stmt *pStmt){
drh1bcdb0c2004-08-28 14:49:46 +00001291 int rc;
1292 if( pStmt==0 ){
1293 rc = SQLITE_OK;
1294 }else{
1295 rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
1296 }
1297 return rc;
danielk1977106bb232004-05-21 10:08:53 +00001298}
1299
1300/*
1301** Terminate the current execution of an SQL statement and reset it
1302** back to its starting state so that it can be reused. A success code from
1303** the prior execution is returned.
1304**
1305** This routine sets the error code and string returned by
1306** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
1307*/
danielk1977fc57d7b2004-05-26 02:04:57 +00001308int sqlite3_reset(sqlite3_stmt *pStmt){
drh1bcdb0c2004-08-28 14:49:46 +00001309 int rc;
1310 if( pStmt==0 ){
1311 rc = SQLITE_OK;
1312 }else{
1313 rc = sqlite3VdbeReset((Vdbe*)pStmt);
danielk1977b3bce662005-01-29 08:32:43 +00001314 sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0, 0, 0, 0);
drh1bcdb0c2004-08-28 14:49:46 +00001315 }
danielk1977106bb232004-05-21 10:08:53 +00001316 return rc;
1317}
danielk19770202b292004-06-09 09:55:16 +00001318
danielk1977d8123362004-06-12 09:25:12 +00001319/*
1320** Register a new collation sequence with the database handle db.
1321*/
danielk19770202b292004-06-09 09:55:16 +00001322int sqlite3_create_collation(
1323 sqlite3* db,
1324 const char *zName,
danielk1977466be562004-06-10 02:16:01 +00001325 int enc,
danielk19770202b292004-06-09 09:55:16 +00001326 void* pCtx,
1327 int(*xCompare)(void*,int,const void*,int,const void*)
1328){
1329 CollSeq *pColl;
1330 int rc = SQLITE_OK;
danielk1977d8123362004-06-12 09:25:12 +00001331
drhc60d0442004-09-30 13:43:13 +00001332 if( sqlite3SafetyCheck(db) ){
1333 return SQLITE_MISUSE;
1334 }
1335
danielk1977d8123362004-06-12 09:25:12 +00001336 /* If SQLITE_UTF16 is specified as the encoding type, transform this
1337 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
1338 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
1339 */
1340 if( enc==SQLITE_UTF16 ){
1341 enc = SQLITE_UTF16NATIVE;
1342 }
1343
danielk1977466be562004-06-10 02:16:01 +00001344 if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16LE && enc!=SQLITE_UTF16BE ){
1345 sqlite3Error(db, SQLITE_ERROR,
1346 "Param 3 to sqlite3_create_collation() must be one of "
danielk1977d8123362004-06-12 09:25:12 +00001347 "SQLITE_UTF8, SQLITE_UTF16, SQLITE_UTF16LE or SQLITE_UTF16BE"
danielk1977466be562004-06-10 02:16:01 +00001348 );
1349 return SQLITE_ERROR;
1350 }
danielk1977a21c6b62005-01-24 10:25:59 +00001351
danielk19779636c4e2005-01-25 04:27:54 +00001352 /* Check if this call is removing or replacing an existing collation
1353 ** sequence. If so, and there are active VMs, return busy. If there
1354 ** are no active VMs, invalidate any pre-compiled statements.
danielk1977a21c6b62005-01-24 10:25:59 +00001355 */
danielk19779636c4e2005-01-25 04:27:54 +00001356 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 0);
1357 if( pColl && pColl->xCmp ){
1358 if( db->activeVdbeCnt ){
1359 sqlite3Error(db, SQLITE_BUSY,
1360 "Unable to delete/modify collation sequence due to active statements");
1361 return SQLITE_BUSY;
1362 }
danielk1977a21c6b62005-01-24 10:25:59 +00001363 sqlite3ExpirePreparedStatements(db);
1364 }
1365
danielk1977466be562004-06-10 02:16:01 +00001366 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 1);
danielk19770202b292004-06-09 09:55:16 +00001367 if( 0==pColl ){
1368 rc = SQLITE_NOMEM;
danielk19770202b292004-06-09 09:55:16 +00001369 }else{
1370 pColl->xCmp = xCompare;
1371 pColl->pUser = pCtx;
danielk1977312d6b32004-06-29 13:18:23 +00001372 pColl->enc = enc;
danielk19770202b292004-06-09 09:55:16 +00001373 }
1374 sqlite3Error(db, rc, 0);
danielk1977466be562004-06-10 02:16:01 +00001375 return rc;
danielk19770202b292004-06-09 09:55:16 +00001376}
1377
drh6c626082004-11-14 21:56:29 +00001378#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00001379/*
1380** Register a new collation sequence with the database handle db.
1381*/
danielk19770202b292004-06-09 09:55:16 +00001382int sqlite3_create_collation16(
1383 sqlite3* db,
1384 const char *zName,
danielk1977466be562004-06-10 02:16:01 +00001385 int enc,
danielk19770202b292004-06-09 09:55:16 +00001386 void* pCtx,
1387 int(*xCompare)(void*,int,const void*,int,const void*)
1388){
danielk1977bfd6cce2004-06-18 04:24:54 +00001389 char const *zName8;
drhc60d0442004-09-30 13:43:13 +00001390 sqlite3_value *pTmp;
1391 if( sqlite3SafetyCheck(db) ){
1392 return SQLITE_MISUSE;
1393 }
1394 pTmp = sqlite3GetTransientValue(db);
danielk1977bfd6cce2004-06-18 04:24:54 +00001395 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1396 zName8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
1397 return sqlite3_create_collation(db, zName8, enc, pCtx, xCompare);
danielk19770202b292004-06-09 09:55:16 +00001398}
drh6c626082004-11-14 21:56:29 +00001399#endif /* SQLITE_OMIT_UTF16 */
danielk19777cedc8d2004-06-10 10:50:08 +00001400
danielk1977d8123362004-06-12 09:25:12 +00001401/*
1402** Register a collation sequence factory callback with the database handle
1403** db. Replace any previously installed collation sequence factory.
1404*/
danielk19777cedc8d2004-06-10 10:50:08 +00001405int sqlite3_collation_needed(
1406 sqlite3 *db,
1407 void *pCollNeededArg,
1408 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
1409){
drhc60d0442004-09-30 13:43:13 +00001410 if( sqlite3SafetyCheck(db) ){
1411 return SQLITE_MISUSE;
1412 }
danielk19777cedc8d2004-06-10 10:50:08 +00001413 db->xCollNeeded = xCollNeeded;
1414 db->xCollNeeded16 = 0;
1415 db->pCollNeededArg = pCollNeededArg;
1416 return SQLITE_OK;
1417}
danielk1977d8123362004-06-12 09:25:12 +00001418
drh6c626082004-11-14 21:56:29 +00001419#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00001420/*
1421** Register a collation sequence factory callback with the database handle
1422** db. Replace any previously installed collation sequence factory.
1423*/
danielk19777cedc8d2004-06-10 10:50:08 +00001424int sqlite3_collation_needed16(
1425 sqlite3 *db,
1426 void *pCollNeededArg,
1427 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
1428){
drhc60d0442004-09-30 13:43:13 +00001429 if( sqlite3SafetyCheck(db) ){
1430 return SQLITE_MISUSE;
1431 }
danielk19777cedc8d2004-06-10 10:50:08 +00001432 db->xCollNeeded = 0;
1433 db->xCollNeeded16 = xCollNeeded16;
1434 db->pCollNeededArg = pCollNeededArg;
1435 return SQLITE_OK;
1436}
drh6c626082004-11-14 21:56:29 +00001437#endif /* SQLITE_OMIT_UTF16 */
danielk19776b456a22005-03-21 04:04:02 +00001438
1439#ifndef SQLITE_OMIT_GLOBALRECOVER
1440/*
1441** This function is called to recover from a malloc failure that occured
1442** within SQLite.
1443**
1444** This function is *not* threadsafe. Calling this from within a threaded
1445** application when threads other than the caller have used SQLite is
1446** dangerous and will almost certainly result in malfunctions.
1447*/
1448int sqlite3_global_recover(){
1449 int rc = SQLITE_OK;
1450
1451 if( sqlite3_malloc_failed ){
1452 sqlite3 *db;
1453 int i;
1454 sqlite3_malloc_failed = 0;
1455 for(db=pDbList; db; db=db->pNext ){
1456 sqlite3ExpirePreparedStatements(db);
1457 for(i=0; i<db->nDb; i++){
1458 Btree *pBt = db->aDb[i].pBt;
1459 if( pBt && (rc=sqlite3BtreeReset(pBt)) ){
1460 goto recover_out;
1461 }
1462 }
1463 db->autoCommit = 1;
1464 }
1465 }
1466
1467recover_out:
1468 if( rc!=SQLITE_OK ){
1469 sqlite3_malloc_failed = 1;
1470 }
1471 return rc;
1472}
1473#endif