blob: ce7f223add254e95784f773e8964585a583204dc [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**
drhae157872004-08-14 19:20:09 +000017** $Id: main.c,v 1.251 2004/08/14 19:20:10 drh Exp $
drh75897232000-05-29 14:26:00 +000018*/
19#include "sqliteInt.h"
drh8cfbf082001-09-19 13:22:39 +000020#include "os.h"
drhce9079c2002-05-15 14:17:44 +000021#include <ctype.h>
drh75897232000-05-29 14:26:00 +000022
23/*
drh9c054832004-05-31 18:51:57 +000024** The following constant value is used by the SQLITE_BIGENDIAN and
25** SQLITE_LITTLEENDIAN macros.
drhbbd42a62004-05-22 17:41:58 +000026*/
27const int sqlite3one = 1;
28
29/*
drh8bf8dc92003-05-17 17:35:10 +000030** Fill the InitData structure with an error message that indicates
31** that the database is corrupt.
32*/
drh1d85d932004-02-14 23:05:52 +000033static void corruptSchema(InitData *pData, const char *zExtra){
drhef0715c2004-06-29 10:53:54 +000034 if( !sqlite3_malloc_failed ){
35 sqlite3SetString(pData->pzErrMsg, "malformed database schema",
36 zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0);
37 }
drh8bf8dc92003-05-17 17:35:10 +000038}
drhc2311722002-07-19 17:46:38 +000039
40/*
drh75897232000-05-29 14:26:00 +000041** This is the callback routine for the code that initializes the
danielk19774adee202004-05-08 08:23:19 +000042** database. See sqlite3Init() below for additional information.
drh234c39d2004-07-24 03:30:47 +000043** This routine is also called from the OP_ParseSchema opcode of the VDBE.
drh382c0242001-10-06 16:33:02 +000044**
45** Each callback contains the following information:
drh28037572000-08-02 13:47:41 +000046**
drh234c39d2004-07-24 03:30:47 +000047** argv[0] = name of thing being created
48** argv[1] = root page number for table or index. NULL for trigger or view.
49** argv[2] = SQL text for the CREATE statement.
50** argv[3] = "1" for temporary files, "0" for main database, "2" or more
drh1c2d8412003-03-31 00:30:47 +000051** for auxiliary database files.
drhd78eeee2001-09-13 16:18:53 +000052**
drh75897232000-05-29 14:26:00 +000053*/
danielk19774adee202004-05-08 08:23:19 +000054int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){
drhc2311722002-07-19 17:46:38 +000055 InitData *pData = (InitData*)pInit;
drh234c39d2004-07-24 03:30:47 +000056 sqlite *db = pData->db;
57 int iDb;
drh75897232000-05-29 14:26:00 +000058
drh234c39d2004-07-24 03:30:47 +000059 assert( argc==4 );
drh98e3e602003-07-27 17:26:22 +000060 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
drh234c39d2004-07-24 03:30:47 +000061 if( argv[1]==0 || argv[3]==0 ){
drh1d85d932004-02-14 23:05:52 +000062 corruptSchema(pData, 0);
drh8bf8dc92003-05-17 17:35:10 +000063 return 1;
64 }
drh234c39d2004-07-24 03:30:47 +000065 iDb = atoi(argv[3]);
66 assert( iDb>=0 && iDb<db->nDb );
67 if( argv[2] && argv[2][0] ){
68 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
69 ** But because db->init.busy is set to 1, no VDBE code is generated
70 ** or executed. All the parser does is build the internal data
71 ** structures that describe the table, index, or view.
72 */
73 char *zErr;
74 int rc;
75 assert( db->init.busy );
76 db->init.iDb = iDb;
77 db->init.newTnum = atoi(argv[1]);
78 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
79 db->init.iDb = 0;
80 if( SQLITE_OK!=rc ){
81 corruptSchema(pData, zErr);
82 sqlite3_free(zErr);
83 return rc;
drhd78eeee2001-09-13 16:18:53 +000084 }
drh234c39d2004-07-24 03:30:47 +000085 }else{
86 /* If the SQL column is blank it means this is an index that
87 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
88 ** constraint for a CREATE TABLE. The index should have already
89 ** been created when we processed the CREATE TABLE. All we have
90 ** to do here is record the root page number for that index.
91 */
92 Index *pIndex;
93 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
94 if( pIndex==0 || pIndex->tnum!=0 ){
95 /* This can occur if there exists an index on a TEMP table which
96 ** has the same name as another index on a permanent index. Since
97 ** the permanent table is hidden by the TEMP table, we can also
98 ** safely ignore the index on the permanent table.
99 */
100 /* Do Nothing */;
101 }else{
102 pIndex->tnum = atoi(argv[1]);
drhd78eeee2001-09-13 16:18:53 +0000103 }
drh28037572000-08-02 13:47:41 +0000104 }
drh234c39d2004-07-24 03:30:47 +0000105 return 0;
drh75897232000-05-29 14:26:00 +0000106}
107
108/*
drh58b95762000-06-02 01:17:37 +0000109** Attempt to read the database schema and initialize internal
drh1c2d8412003-03-31 00:30:47 +0000110** data structures for a single database file. The index of the
111** database file is given by iDb. iDb==0 is used for the main
112** database. iDb==1 should never be used. iDb>=2 is used for
113** auxiliary databases. Return one of the SQLITE_ error codes to
drh58b95762000-06-02 01:17:37 +0000114** indicate success or failure.
drh75897232000-05-29 14:26:00 +0000115*/
danielk19774adee202004-05-08 08:23:19 +0000116static int sqlite3InitOne(sqlite *db, int iDb, char **pzErrMsg){
drh58b95762000-06-02 01:17:37 +0000117 int rc;
drhe0bc4042002-06-25 01:09:11 +0000118 BtCursor *curMain;
119 int size;
120 Table *pTab;
drh234c39d2004-07-24 03:30:47 +0000121 char const *azArg[5];
drh1c2d8412003-03-31 00:30:47 +0000122 char zDbNum[30];
drha3b321d2004-05-11 09:31:31 +0000123 int meta[10];
drhc2311722002-07-19 17:46:38 +0000124 InitData initData;
danielk1977d008cfe2004-06-19 02:22:10 +0000125 char const *zMasterSchema;
126 char const *zMasterName;
drh58b95762000-06-02 01:17:37 +0000127
128 /*
129 ** The master database table has a structure like this
130 */
drh75897232000-05-29 14:26:00 +0000131 static char master_schema[] =
drhe0bc4042002-06-25 01:09:11 +0000132 "CREATE TABLE sqlite_master(\n"
133 " type text,\n"
134 " name text,\n"
135 " tbl_name text,\n"
136 " rootpage integer,\n"
137 " sql text\n"
138 ")"
139 ;
140 static char temp_master_schema[] =
141 "CREATE TEMP TABLE sqlite_temp_master(\n"
drh75897232000-05-29 14:26:00 +0000142 " type text,\n"
143 " name text,\n"
144 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000145 " rootpage integer,\n"
drh75897232000-05-29 14:26:00 +0000146 " sql text\n"
147 ")"
148 ;
149
danielk1977d008cfe2004-06-19 02:22:10 +0000150 assert( iDb>=0 && iDb<db->nDb );
drh603240c2002-03-05 01:11:12 +0000151
danielk1977d008cfe2004-06-19 02:22:10 +0000152 /* zMasterSchema and zInitScript are set to point at the master schema
153 ** and initialisation script appropriate for the database being
154 ** initialised. zMasterName is the name of the master table.
drh58b95762000-06-02 01:17:37 +0000155 */
danielk1977d008cfe2004-06-19 02:22:10 +0000156 if( iDb==1 ){
157 zMasterSchema = temp_master_schema;
158 zMasterName = TEMP_MASTER_NAME;
159 }else{
160 zMasterSchema = master_schema;
161 zMasterName = MASTER_NAME;
162 }
163
164 /* Construct the schema tables. */
danielk19774adee202004-05-08 08:23:19 +0000165 sqlite3SafetyOff(db);
drh234c39d2004-07-24 03:30:47 +0000166 azArg[0] = zMasterName;
167 azArg[1] = "1";
168 azArg[2] = zMasterSchema;
drh1c2d8412003-03-31 00:30:47 +0000169 sprintf(zDbNum, "%d", iDb);
drh234c39d2004-07-24 03:30:47 +0000170 azArg[3] = zDbNum;
171 azArg[4] = 0;
drhc2311722002-07-19 17:46:38 +0000172 initData.db = db;
173 initData.pzErrMsg = pzErrMsg;
drh234c39d2004-07-24 03:30:47 +0000174 rc = sqlite3InitCallback(&initData, 4, (char **)azArg, 0);
danielk19772b444852004-06-29 07:45:33 +0000175 if( rc!=SQLITE_OK ){
176 sqlite3SafetyOn(db);
177 return rc;
178 }
danielk1977d008cfe2004-06-19 02:22:10 +0000179 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
drhe0bc4042002-06-25 01:09:11 +0000180 if( pTab ){
181 pTab->readOnly = 1;
drhd8bc7082000-06-07 23:51:50 +0000182 }
danielk19774adee202004-05-08 08:23:19 +0000183 sqlite3SafetyOn(db);
drhe0bc4042002-06-25 01:09:11 +0000184
185 /* Create a cursor to hold the database open
186 */
drh1c2d8412003-03-31 00:30:47 +0000187 if( db->aDb[iDb].pBt==0 ) return SQLITE_OK;
danielk19778e150812004-05-10 01:17:37 +0000188 rc = sqlite3BtreeCursor(db->aDb[iDb].pBt, MASTER_ROOT, 0, 0, 0, &curMain);
drhf328bc82004-05-10 23:29:49 +0000189 if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
danielk1977f20b21c2004-05-31 23:56:42 +0000190 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
drh92ed08a2002-07-30 18:43:40 +0000191 return rc;
192 }
drhe0bc4042002-06-25 01:09:11 +0000193
drha3b321d2004-05-11 09:31:31 +0000194 /* Get the database meta information.
195 **
196 ** Meta values are as follows:
197 ** meta[0] Schema cookie. Changes with each schema change.
198 ** meta[1] File format of schema layer.
199 ** meta[2] Size of the page cache.
drhae157872004-08-14 19:20:09 +0000200 ** meta[3] Use freelist if 0. Autovacuum if greater than zero.
danielk1977dc8453f2004-06-12 00:42:34 +0000201 ** meta[4] Db text encoding. 1:UTF-8 3:UTF-16 LE 4:UTF-16 BE
danielk197791cf71b2004-06-26 06:37:06 +0000202 ** meta[5]
danielk1977962398d2004-06-14 09:35:16 +0000203 ** meta[6]
drha3b321d2004-05-11 09:31:31 +0000204 ** meta[7]
205 ** meta[8]
206 ** meta[9]
danielk1977172bc392004-05-22 08:09:11 +0000207 **
danielk1977dc8453f2004-06-12 00:42:34 +0000208 ** Note: The hash defined SQLITE_UTF* symbols in sqliteInt.h correspond to
danielk1977172bc392004-05-22 08:09:11 +0000209 ** the possible values of meta[4].
drhe0bc4042002-06-25 01:09:11 +0000210 */
drhf328bc82004-05-10 23:29:49 +0000211 if( rc==SQLITE_OK ){
212 int i;
drha3b321d2004-05-11 09:31:31 +0000213 for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
danielk1977e0d4b062004-06-28 01:11:46 +0000214 rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, i+1, (u32 *)&meta[i]);
danielk19774adee202004-05-08 08:23:19 +0000215 }
drhf328bc82004-05-10 23:29:49 +0000216 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000217 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
drhf328bc82004-05-10 23:29:49 +0000218 sqlite3BtreeCloseCursor(curMain);
219 return rc;
220 }
221 }else{
222 memset(meta, 0, sizeof(meta));
drhe0bc4042002-06-25 01:09:11 +0000223 }
drha3b321d2004-05-11 09:31:31 +0000224 db->aDb[iDb].schema_cookie = meta[0];
danielk19773df6b252004-05-29 10:23:19 +0000225
226 /* If opening a non-empty database, check the text encoding. For the
227 ** main database, set sqlite3.enc to the encoding of the main database.
228 ** For an attached db, it is an error if the encoding is not the same
229 ** as sqlite3.enc.
230 */
231 if( meta[4] ){ /* text encoding */
232 if( iDb==0 ){
233 /* If opening the main database, set db->enc. */
danielk1977172bc392004-05-22 08:09:11 +0000234 db->enc = (u8)meta[4];
danielk1977466be562004-06-10 02:16:01 +0000235 db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0);
danielk19773df6b252004-05-29 10:23:19 +0000236 }else{
237 /* If opening an attached database, the encoding much match db->enc */
238 if( meta[4]!=db->enc ){
239 sqlite3BtreeCloseCursor(curMain);
240 sqlite3SetString(pzErrMsg, "attached databases must use the same"
241 " text encoding as main database", (char*)0);
242 return SQLITE_ERROR;
243 }
danielk1977172bc392004-05-22 08:09:11 +0000244 }
danielk19773df6b252004-05-29 10:23:19 +0000245 }
246
danielk197791cf71b2004-06-26 06:37:06 +0000247 size = meta[2];
248 if( size==0 ){ size = MAX_PAGES; }
249 db->aDb[iDb].cache_size = size;
drhe0bc4042002-06-25 01:09:11 +0000250
danielk197791cf71b2004-06-26 06:37:06 +0000251 if( iDb==0 ){
danielk19773df6b252004-05-29 10:23:19 +0000252 db->file_format = meta[1];
drh1c2d8412003-03-31 00:30:47 +0000253 if( db->file_format==0 ){
254 /* This happens if the database was initially empty */
drhf328bc82004-05-10 23:29:49 +0000255 db->file_format = 1;
drh1c2d8412003-03-31 00:30:47 +0000256 }
drh28037572000-08-02 13:47:41 +0000257 }
danielk19773df6b252004-05-29 10:23:19 +0000258
259 /*
260 ** file_format==1 Version 3.0.0.
261 */
262 if( meta[1]>1 ){
263 sqlite3BtreeCloseCursor(curMain);
264 sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
265 return SQLITE_ERROR;
266 }
267
danielk197791cf71b2004-06-26 06:37:06 +0000268 sqlite3BtreeSetCacheSize(db->aDb[iDb].pBt, db->aDb[iDb].cache_size);
drhaacc5432002-01-06 17:07:40 +0000269
drhe0bc4042002-06-25 01:09:11 +0000270 /* Read the schema information out of the schema tables
drhaacc5432002-01-06 17:07:40 +0000271 */
drh1d85d932004-02-14 23:05:52 +0000272 assert( db->init.busy );
drhf328bc82004-05-10 23:29:49 +0000273 if( rc==SQLITE_EMPTY ){
274 /* For an empty database, there is nothing to read */
275 rc = SQLITE_OK;
drh1c2d8412003-03-31 00:30:47 +0000276 }else{
drh234c39d2004-07-24 03:30:47 +0000277 char *zSql;
278 zSql = sqlite3MPrintf(
279 "SELECT name, rootpage, sql, %s FROM '%q'.%s",
280 zDbNum, db->aDb[iDb].zName, zMasterName);
danielk19773df6b252004-05-29 10:23:19 +0000281 sqlite3SafetyOff(db);
danielk1977d008cfe2004-06-19 02:22:10 +0000282 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
drhf328bc82004-05-10 23:29:49 +0000283 sqlite3SafetyOn(db);
drh234c39d2004-07-24 03:30:47 +0000284 sqliteFree(zSql);
drhf328bc82004-05-10 23:29:49 +0000285 sqlite3BtreeCloseCursor(curMain);
drh1c2d8412003-03-31 00:30:47 +0000286 }
danielk197724b03fd2004-05-10 10:34:34 +0000287 if( sqlite3_malloc_failed ){
danielk19774adee202004-05-08 08:23:19 +0000288 sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
drh1d85d932004-02-14 23:05:52 +0000289 rc = SQLITE_NOMEM;
danielk19774adee202004-05-08 08:23:19 +0000290 sqlite3ResetInternalSchema(db, 0);
drhe0bc4042002-06-25 01:09:11 +0000291 }
drh1d85d932004-02-14 23:05:52 +0000292 if( rc==SQLITE_OK ){
drh8bf8dc92003-05-17 17:35:10 +0000293 DbSetProperty(db, iDb, DB_SchemaLoaded);
drh1c2d8412003-03-31 00:30:47 +0000294 }else{
danielk19774adee202004-05-08 08:23:19 +0000295 sqlite3ResetInternalSchema(db, iDb);
drh1c2d8412003-03-31 00:30:47 +0000296 }
drh1d85d932004-02-14 23:05:52 +0000297 return rc;
drh1c2d8412003-03-31 00:30:47 +0000298}
299
300/*
301** Initialize all database files - the main database file, the file
302** used to store temporary tables, and any additional database files
303** created using ATTACH statements. Return a success code. If an
304** error occurs, write an error message into *pzErrMsg.
305**
306** After the database is initialized, the SQLITE_Initialized
danielk19778e227872004-06-07 07:52:17 +0000307** bit is set in the flags field of the sqlite structure.
drh1c2d8412003-03-31 00:30:47 +0000308*/
danielk19774adee202004-05-08 08:23:19 +0000309int sqlite3Init(sqlite *db, char **pzErrMsg){
drh1c2d8412003-03-31 00:30:47 +0000310 int i, rc;
311
drh1d85d932004-02-14 23:05:52 +0000312 if( db->init.busy ) return SQLITE_OK;
drh1c2d8412003-03-31 00:30:47 +0000313 assert( (db->flags & SQLITE_Initialized)==0 );
314 rc = SQLITE_OK;
drh1d85d932004-02-14 23:05:52 +0000315 db->init.busy = 1;
drh1c2d8412003-03-31 00:30:47 +0000316 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk1977d008cfe2004-06-19 02:22:10 +0000317 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
danielk19774adee202004-05-08 08:23:19 +0000318 rc = sqlite3InitOne(db, i, pzErrMsg);
drh8ef83ff2004-02-12 15:31:21 +0000319 if( rc ){
danielk19774adee202004-05-08 08:23:19 +0000320 sqlite3ResetInternalSchema(db, i);
drh8ef83ff2004-02-12 15:31:21 +0000321 }
drh1c2d8412003-03-31 00:30:47 +0000322 }
danielk1977d008cfe2004-06-19 02:22:10 +0000323
324 /* Once all the other databases have been initialised, load the schema
325 ** for the TEMP database. This is loaded last, as the TEMP database
326 ** schema may contain references to objects in other databases.
327 */
328 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
329 rc = sqlite3InitOne(db, 1, pzErrMsg);
330 if( rc ){
331 sqlite3ResetInternalSchema(db, 1);
332 }
333 }
334
drh1d85d932004-02-14 23:05:52 +0000335 db->init.busy = 0;
drh1c2d8412003-03-31 00:30:47 +0000336 if( rc==SQLITE_OK ){
drh58b95762000-06-02 01:17:37 +0000337 db->flags |= SQLITE_Initialized;
danielk19774adee202004-05-08 08:23:19 +0000338 sqlite3CommitInternalChanges(db);
drh2d71ca92004-02-10 02:27:04 +0000339 }
340
drh2d71ca92004-02-10 02:27:04 +0000341 if( rc!=SQLITE_OK ){
drhe0bc4042002-06-25 01:09:11 +0000342 db->flags &= ~SQLITE_Initialized;
drh58b95762000-06-02 01:17:37 +0000343 }
drh1c2d8412003-03-31 00:30:47 +0000344 return rc;
drh58b95762000-06-02 01:17:37 +0000345}
346
347/*
danielk19778e227872004-06-07 07:52:17 +0000348** This routine is a no-op if the database schema is already initialised.
349** Otherwise, the schema is loaded. An error code is returned.
350*/
danielk19778a414492004-06-29 08:59:35 +0000351int sqlite3ReadSchema(Parse *pParse){
danielk19778e227872004-06-07 07:52:17 +0000352 int rc = SQLITE_OK;
danielk19778a414492004-06-29 08:59:35 +0000353 sqlite3 *db = pParse->db;
danielk19778e227872004-06-07 07:52:17 +0000354 if( !db->init.busy ){
355 if( (db->flags & SQLITE_Initialized)==0 ){
danielk19778a414492004-06-29 08:59:35 +0000356 rc = sqlite3Init(db, &pParse->zErrMsg);
danielk19778e227872004-06-07 07:52:17 +0000357 }
358 }
danielk1977c0391392004-06-09 12:30:04 +0000359 assert( rc!=SQLITE_OK || (db->flags & SQLITE_Initialized)||db->init.busy );
danielk19778a414492004-06-29 08:59:35 +0000360 if( rc!=SQLITE_OK ){
361 pParse->rc = rc;
362 pParse->nErr++;
363 }
danielk19778e227872004-06-07 07:52:17 +0000364 return rc;
365}
366
367/*
drhb217a572000-08-22 13:40:18 +0000368** The version of the library
369*/
drh38f82712004-06-18 17:10:16 +0000370const char rcsid3[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $";
danielk197724b03fd2004-05-10 10:34:34 +0000371const char sqlite3_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000372
373/*
drhd3d39e92004-05-20 22:16:29 +0000374** This is the default collating function named "BINARY" which is always
375** available.
376*/
377static int binaryCollatingFunc(
378 void *NotUsed,
379 int nKey1, const void *pKey1,
380 int nKey2, const void *pKey2
381){
382 int rc, n;
383 n = nKey1<nKey2 ? nKey1 : nKey2;
384 rc = memcmp(pKey1, pKey2, n);
385 if( rc==0 ){
386 rc = nKey1 - nKey2;
387 }
388 return rc;
389}
390
391/*
danielk1977dc1bdc42004-06-11 10:51:27 +0000392** Another built-in collating sequence: NOCASE.
393**
394** This collating sequence is intended to be used for "case independant
395** comparison". SQLite's knowledge of upper and lower case equivalents
396** extends only to the 26 characters used in the English language.
397**
398** At the moment there is only a UTF-8 implementation.
danielk19770202b292004-06-09 09:55:16 +0000399*/
400static int nocaseCollatingFunc(
401 void *NotUsed,
402 int nKey1, const void *pKey1,
403 int nKey2, const void *pKey2
404){
405 int r = sqlite3StrNICmp(
406 (const char *)pKey1, (const char *)pKey2, (nKey1>nKey2)?nKey1:nKey2);
407 if( 0==r ){
408 r = nKey1-nKey2;
409 }
410 return r;
411}
412
413/*
drhaf9ff332002-01-16 21:00:27 +0000414** Return the ROWID of the most recent insert
415*/
drhefad9992004-06-22 12:13:55 +0000416sqlite_int64 sqlite3_last_insert_rowid(sqlite *db){
drhaf9ff332002-01-16 21:00:27 +0000417 return db->lastRowid;
418}
419
420/*
danielk197724b03fd2004-05-10 10:34:34 +0000421** Return the number of changes in the most recent call to sqlite3_exec().
drhc8d30ac2002-04-12 10:08:59 +0000422*/
danielk197724b03fd2004-05-10 10:34:34 +0000423int sqlite3_changes(sqlite *db){
drhc8d30ac2002-04-12 10:08:59 +0000424 return db->nChange;
425}
426
rdcf146a772004-02-25 22:51:06 +0000427/*
danielk1977b28af712004-06-21 06:50:26 +0000428** Return the number of changes since the database handle was opened.
rdcf146a772004-02-25 22:51:06 +0000429*/
danielk1977b28af712004-06-21 06:50:26 +0000430int sqlite3_total_changes(sqlite3 *db){
431 return db->nTotalChange;
rdcb0c374f2004-02-20 22:53:38 +0000432}
433
drhc8d30ac2002-04-12 10:08:59 +0000434/*
drh50e5dad2001-09-15 00:57:28 +0000435** Close an existing SQLite database
436*/
danielk197796d81f92004-06-19 03:33:57 +0000437int sqlite3_close(sqlite *db){
drh8e0a2f92002-02-23 23:45:45 +0000438 HashElem *i;
drh001bbcb2003-03-19 03:14:00 +0000439 int j;
danielk19775c4c7782004-06-16 10:39:23 +0000440
441 if( !db ){
danielk197796d81f92004-06-19 03:33:57 +0000442 return SQLITE_OK;
danielk19775c4c7782004-06-16 10:39:23 +0000443 }
danielk197796d81f92004-06-19 03:33:57 +0000444
danielk1977e35ee192004-06-26 09:50:11 +0000445 if( db->magic!=SQLITE_MAGIC_CLOSED &&
446 db->magic!=SQLITE_MAGIC_OPEN &&
447 db->magic!=SQLITE_MAGIC_BUSY
448 ){
449 return SQLITE_MISUSE;
450 }
451
danielk197796d81f92004-06-19 03:33:57 +0000452 /* If there are any outstanding VMs, return SQLITE_BUSY. */
453 if( db->pVdbe ){
454 sqlite3Error(db, SQLITE_BUSY,
455 "Unable to close due to unfinalised statements");
456 return SQLITE_BUSY;
457 }
458 assert( !sqlite3SafetyCheck(db) );
danielk1977e0048402004-06-15 16:51:01 +0000459
460 /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database
461 ** cannot be opened for some reason. So this routine needs to run in
462 ** that case. But maybe there should be an extra magic value for the
463 ** "failed to open" state.
464 */
danielk197796d81f92004-06-19 03:33:57 +0000465 if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){
drh94e92032003-02-16 22:21:32 +0000466 /* printf("DID NOT CLOSE\n"); fflush(stdout); */
danielk197796d81f92004-06-19 03:33:57 +0000467 return SQLITE_ERROR;
drh94e92032003-02-16 22:21:32 +0000468 }
danielk1977e0048402004-06-15 16:51:01 +0000469
drh001bbcb2003-03-19 03:14:00 +0000470 for(j=0; j<db->nDb; j++){
drh4d189ca2004-02-12 18:46:38 +0000471 struct Db *pDb = &db->aDb[j];
472 if( pDb->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000473 sqlite3BtreeClose(pDb->pBt);
drh4d189ca2004-02-12 18:46:38 +0000474 pDb->pBt = 0;
drh113088e2003-03-20 01:16:58 +0000475 }
drhf57b3392001-10-08 13:22:32 +0000476 }
danielk19774adee202004-05-08 08:23:19 +0000477 sqlite3ResetInternalSchema(db, 0);
drh1c2d8412003-03-31 00:30:47 +0000478 assert( db->nDb<=2 );
479 assert( db->aDb==db->aDbStatic );
drh0bce8352002-02-28 00:41:10 +0000480 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
481 FuncDef *pFunc, *pNext;
482 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
drh8e0a2f92002-02-23 23:45:45 +0000483 pNext = pFunc->pNext;
484 sqliteFree(pFunc);
485 }
486 }
danielk1977466be562004-06-10 02:16:01 +0000487
danielk1977d8123362004-06-12 09:25:12 +0000488 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
danielk1977466be562004-06-10 02:16:01 +0000489 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
danielk1977d8123362004-06-12 09:25:12 +0000490 sqliteFree(pColl);
danielk1977466be562004-06-10 02:16:01 +0000491 }
danielk1977d8123362004-06-12 09:25:12 +0000492 sqlite3HashClear(&db->aCollSeq);
danielk1977466be562004-06-10 02:16:01 +0000493
danielk19774adee202004-05-08 08:23:19 +0000494 sqlite3HashClear(&db->aFunc);
danielk19776622cce2004-05-20 11:00:52 +0000495 sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
danielk1977bfd6cce2004-06-18 04:24:54 +0000496 if( db->pValue ){
497 sqlite3ValueFree(db->pValue);
498 }
499 if( db->pErr ){
500 sqlite3ValueFree(db->pErr);
501 }
danielk197796d81f92004-06-19 03:33:57 +0000502
503 db->magic = SQLITE_MAGIC_ERROR;
drh75897232000-05-29 14:26:00 +0000504 sqliteFree(db);
danielk197796d81f92004-06-19 03:33:57 +0000505 return SQLITE_OK;
drh75897232000-05-29 14:26:00 +0000506}
507
508/*
drh001bbcb2003-03-19 03:14:00 +0000509** Rollback all database files.
510*/
danielk19774adee202004-05-08 08:23:19 +0000511void sqlite3RollbackAll(sqlite *db){
drh001bbcb2003-03-19 03:14:00 +0000512 int i;
513 for(i=0; i<db->nDb; i++){
514 if( db->aDb[i].pBt ){
danielk19774adee202004-05-08 08:23:19 +0000515 sqlite3BtreeRollback(db->aDb[i].pBt);
drh001bbcb2003-03-19 03:14:00 +0000516 db->aDb[i].inTrans = 0;
517 }
518 }
danielk19774adee202004-05-08 08:23:19 +0000519 sqlite3ResetInternalSchema(db, 0);
520 /* sqlite3RollbackInternalChanges(db); */
drh001bbcb2003-03-19 03:14:00 +0000521}
522
523/*
drhc22bd472002-05-10 13:14:07 +0000524** Return a static string that describes the kind of error specified in the
525** argument.
drh247be432002-05-10 05:44:55 +0000526*/
danielk1977f20b21c2004-05-31 23:56:42 +0000527const char *sqlite3ErrStr(int rc){
drhc22bd472002-05-10 13:14:07 +0000528 const char *z;
529 switch( rc ){
530 case SQLITE_OK: z = "not an error"; break;
531 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
532 case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break;
533 case SQLITE_PERM: z = "access permission denied"; break;
534 case SQLITE_ABORT: z = "callback requested query abort"; break;
535 case SQLITE_BUSY: z = "database is locked"; break;
536 case SQLITE_LOCKED: z = "database table is locked"; break;
537 case SQLITE_NOMEM: z = "out of memory"; break;
538 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
539 case SQLITE_INTERRUPT: z = "interrupted"; break;
540 case SQLITE_IOERR: z = "disk I/O error"; break;
541 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
542 case SQLITE_NOTFOUND: z = "table or record not found"; break;
543 case SQLITE_FULL: z = "database is full"; break;
544 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
545 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
546 case SQLITE_EMPTY: z = "table contains no data"; break;
547 case SQLITE_SCHEMA: z = "database schema has changed"; break;
548 case SQLITE_TOOBIG: z = "too much data for one table row"; break;
549 case SQLITE_CONSTRAINT: z = "constraint failed"; break;
550 case SQLITE_MISMATCH: z = "datatype mismatch"; break;
551 case SQLITE_MISUSE: z = "library routine called out of sequence";break;
drh8766c342002-11-09 00:33:15 +0000552 case SQLITE_NOLFS: z = "kernel lacks large file support"; break;
drhed6c8672003-01-12 18:02:16 +0000553 case SQLITE_AUTH: z = "authorization denied"; break;
jplyon892f6712003-06-12 08:59:00 +0000554 case SQLITE_FORMAT: z = "auxiliary database format error"; break;
drh7c972de2003-09-06 22:18:07 +0000555 case SQLITE_RANGE: z = "bind index out of range"; break;
drhc602f9a2004-02-12 19:01:04 +0000556 case SQLITE_NOTADB: z = "file is encrypted or is not a database";break;
drhc22bd472002-05-10 13:14:07 +0000557 default: z = "unknown error"; break;
drh247be432002-05-10 05:44:55 +0000558 }
drhc22bd472002-05-10 13:14:07 +0000559 return z;
drh247be432002-05-10 05:44:55 +0000560}
561
562/*
drh2dfbbca2000-07-28 14:32:48 +0000563** This routine implements a busy callback that sleeps and tries
564** again until a timeout value is reached. The timeout value is
565** an integer number of milliseconds passed in as the first
566** argument.
567*/
drhdaffd0e2001-04-11 14:28:42 +0000568static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000569 void *Timeout, /* Maximum amount of time to wait */
drh2dfbbca2000-07-28 14:32:48 +0000570 int count /* Number of times table has been busy */
571){
drh8cfbf082001-09-19 13:22:39 +0000572#if SQLITE_MIN_SLEEP_MS==1
drhd1bec472004-01-15 13:29:31 +0000573 static const char delays[] =
574 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 50, 100};
575 static const short int totals[] =
576 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228, 287};
577# define NDELAY (sizeof(delays)/sizeof(delays[0]))
drh2dfbbca2000-07-28 14:32:48 +0000578 int timeout = (int)Timeout;
drhd1bec472004-01-15 13:29:31 +0000579 int delay, prior;
drh2dfbbca2000-07-28 14:32:48 +0000580
drhd1bec472004-01-15 13:29:31 +0000581 if( count <= NDELAY ){
582 delay = delays[count-1];
583 prior = totals[count-1];
584 }else{
585 delay = delays[NDELAY-1];
586 prior = totals[NDELAY-1] + delay*(count-NDELAY-1);
drh2dfbbca2000-07-28 14:32:48 +0000587 }
drhd1bec472004-01-15 13:29:31 +0000588 if( prior + delay > timeout ){
589 delay = timeout - prior;
drh2dfbbca2000-07-28 14:32:48 +0000590 if( delay<=0 ) return 0;
591 }
danielk19774adee202004-05-08 08:23:19 +0000592 sqlite3OsSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000593 return 1;
594#else
595 int timeout = (int)Timeout;
596 if( (count+1)*1000 > timeout ){
597 return 0;
598 }
danielk19774adee202004-05-08 08:23:19 +0000599 sqlite3OsSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000600 return 1;
601#endif
602}
603
604/*
605** This routine sets the busy callback for an Sqlite database to the
606** given callback function with the given argument.
607*/
danielk1977f9d64d22004-06-19 08:18:07 +0000608int sqlite3_busy_handler(
609 sqlite3 *db,
danielk19772a764eb2004-06-12 01:43:26 +0000610 int (*xBusy)(void*,int),
drh2dfbbca2000-07-28 14:32:48 +0000611 void *pArg
612){
danielk197724162fe2004-06-04 06:22:00 +0000613 db->busyHandler.xFunc = xBusy;
614 db->busyHandler.pArg = pArg;
danielk1977f9d64d22004-06-19 08:18:07 +0000615 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000616}
617
danielk1977348bb5d2003-10-18 09:37:26 +0000618#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
619/*
620** This routine sets the progress callback for an Sqlite database to the
621** given callback function with the given argument. The progress callback will
622** be invoked every nOps opcodes.
623*/
danielk197724b03fd2004-05-10 10:34:34 +0000624void sqlite3_progress_handler(
danielk1977348bb5d2003-10-18 09:37:26 +0000625 sqlite *db,
626 int nOps,
627 int (*xProgress)(void*),
628 void *pArg
629){
630 if( nOps>0 ){
631 db->xProgress = xProgress;
632 db->nProgressOps = nOps;
633 db->pProgressArg = pArg;
634 }else{
635 db->xProgress = 0;
636 db->nProgressOps = 0;
637 db->pProgressArg = 0;
638 }
639}
640#endif
641
642
drh2dfbbca2000-07-28 14:32:48 +0000643/*
644** This routine installs a default busy handler that waits for the
645** specified number of milliseconds before returning 0.
646*/
danielk1977f9d64d22004-06-19 08:18:07 +0000647int sqlite3_busy_timeout(sqlite3 *db, int ms){
drh2dfbbca2000-07-28 14:32:48 +0000648 if( ms>0 ){
danielk197724b03fd2004-05-10 10:34:34 +0000649 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000650 }else{
danielk197724b03fd2004-05-10 10:34:34 +0000651 sqlite3_busy_handler(db, 0, 0);
drh2dfbbca2000-07-28 14:32:48 +0000652 }
danielk1977f9d64d22004-06-19 08:18:07 +0000653 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000654}
drh4c504392000-10-16 22:06:40 +0000655
656/*
657** Cause any pending operation to stop at its earliest opportunity.
658*/
danielk197724b03fd2004-05-10 10:34:34 +0000659void sqlite3_interrupt(sqlite *db){
drh4c504392000-10-16 22:06:40 +0000660 db->flags |= SQLITE_Interrupt;
661}
drhfa86c412002-02-02 15:01:15 +0000662
663/*
664** Windows systems should call this routine to free memory that
danielk197724b03fd2004-05-10 10:34:34 +0000665** is returned in the in the errmsg parameter of sqlite3_open() when
drhfa86c412002-02-02 15:01:15 +0000666** SQLite is a DLL. For some reason, it does not work to call free()
667** directly.
668**
669** Note that we need to call free() not sqliteFree() here, since every
670** string that is exported from SQLite should have already passed through
danielk19774adee202004-05-08 08:23:19 +0000671** sqlite3StrRealloc().
drhfa86c412002-02-02 15:01:15 +0000672*/
drh3f4fedb2004-05-31 19:34:33 +0000673void sqlite3_free(char *p){ free(p); }
drhfa86c412002-02-02 15:01:15 +0000674
675/*
drhdf014892004-06-02 00:41:09 +0000676** Create new user functions.
drhfa86c412002-02-02 15:01:15 +0000677*/
danielk197724b03fd2004-05-10 10:34:34 +0000678int sqlite3_create_function(
danielk197765904932004-05-26 06:18:37 +0000679 sqlite3 *db,
680 const char *zFunctionName,
681 int nArg,
danielk1977d8123362004-06-12 09:25:12 +0000682 int enc,
danielk197765904932004-05-26 06:18:37 +0000683 void *pUserData,
684 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
685 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
686 void (*xFinal)(sqlite3_context*)
drh8e0a2f92002-02-23 23:45:45 +0000687){
drh0bce8352002-02-28 00:41:10 +0000688 FuncDef *p;
drh4b59ab52002-08-24 18:24:51 +0000689 int nName;
danielk197765904932004-05-26 06:18:37 +0000690
danielk1977398eae72004-05-26 06:58:43 +0000691 if( (db==0 || zFunctionName==0 || sqlite3SafetyCheck(db)) ||
692 (xFunc && (xFinal || xStep)) ||
693 (!xFunc && (xFinal && !xStep)) ||
694 (!xFunc && (!xFinal && xStep)) ||
695 (nArg<-1 || nArg>127) ||
696 (255<(nName = strlen(zFunctionName))) ){
danielk197765904932004-05-26 06:18:37 +0000697 return SQLITE_ERROR;
698 }
danielk1977d8123362004-06-12 09:25:12 +0000699
700 /* If SQLITE_UTF16 is specified as the encoding type, transform this
701 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
702 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
703 **
704 ** If SQLITE_ANY is specified, add three versions of the function
705 ** to the hash table.
706 */
707 if( enc==SQLITE_UTF16 ){
708 enc = SQLITE_UTF16NATIVE;
709 }else if( enc==SQLITE_ANY ){
710 int rc;
711 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF8,
danielk1977f9d64d22004-06-19 08:18:07 +0000712 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000713 if( rc!=SQLITE_OK ) return rc;
714 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF16LE,
danielk1977f9d64d22004-06-19 08:18:07 +0000715 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000716 if( rc!=SQLITE_OK ) return rc;
717 enc = SQLITE_UTF16BE;
718 }
danielk197765904932004-05-26 06:18:37 +0000719
danielk1977d8123362004-06-12 09:25:12 +0000720 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);
danielk197713073932004-06-30 11:54:06 +0000721 if( p==0 ) return SQLITE_NOMEM;
drh8e0a2f92002-02-23 23:45:45 +0000722 p->xFunc = xFunc;
drh8e0a2f92002-02-23 23:45:45 +0000723 p->xStep = xStep;
danielk197765904932004-05-26 06:18:37 +0000724 p->xFinalize = xFinal;
drh1350b032002-02-27 19:00:20 +0000725 p->pUserData = pUserData;
danielk197765904932004-05-26 06:18:37 +0000726 return SQLITE_OK;
727}
danielk197765904932004-05-26 06:18:37 +0000728int sqlite3_create_function16(
729 sqlite3 *db,
730 const void *zFunctionName,
731 int nArg,
732 int eTextRep,
danielk197765904932004-05-26 06:18:37 +0000733 void *pUserData,
734 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
735 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
736 void (*xFinal)(sqlite3_context*)
737){
738 int rc;
danielk1977bfd6cce2004-06-18 04:24:54 +0000739 char const *zFunc8;
740
741 sqlite3_value *pTmp = sqlite3GetTransientValue(db);
742 sqlite3ValueSetStr(pTmp, -1, zFunctionName, SQLITE_UTF16NATIVE,SQLITE_STATIC);
743 zFunc8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
744
745 if( !zFunc8 ){
danielk197765904932004-05-26 06:18:37 +0000746 return SQLITE_NOMEM;
747 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000748 rc = sqlite3_create_function(db, zFunc8, nArg, eTextRep,
danielk1977f9d64d22004-06-19 08:18:07 +0000749 pUserData, xFunc, xStep, xFinal);
danielk197765904932004-05-26 06:18:37 +0000750 return rc;
drh8e0a2f92002-02-23 23:45:45 +0000751}
drhc9b84a12002-06-20 11:36:48 +0000752
753/*
drh18de4822003-01-16 16:28:53 +0000754** Register a trace function. The pArg from the previously registered trace
755** is returned.
756**
757** A NULL trace function means that no tracing is executes. A non-NULL
758** trace is a pointer to a function that is invoked at the start of each
danielk197724b03fd2004-05-10 10:34:34 +0000759** sqlite3_exec().
drh18de4822003-01-16 16:28:53 +0000760*/
danielk197724b03fd2004-05-10 10:34:34 +0000761void *sqlite3_trace(sqlite *db, void (*xTrace)(void*,const char*), void *pArg){
drh18de4822003-01-16 16:28:53 +0000762 void *pOld = db->pTraceArg;
763 db->xTrace = xTrace;
764 db->pTraceArg = pArg;
765 return pOld;
drh0d1a6432003-04-03 15:46:04 +0000766}
paulb0208cc2003-04-13 18:26:49 +0000767
drhaa940ea2004-01-15 02:44:03 +0000768/*** EXPERIMENTAL ***
769**
770** Register a function to be invoked when a transaction comments.
771** If either function returns non-zero, then the commit becomes a
772** rollback.
773*/
danielk197724b03fd2004-05-10 10:34:34 +0000774void *sqlite3_commit_hook(
drhaa940ea2004-01-15 02:44:03 +0000775 sqlite *db, /* Attach the hook to this database */
776 int (*xCallback)(void*), /* Function to invoke on each commit */
777 void *pArg /* Argument to the function */
778){
779 void *pOld = db->pCommitArg;
780 db->xCommitCallback = xCallback;
781 db->pCommitArg = pArg;
782 return pOld;
783}
784
785
paulb0208cc2003-04-13 18:26:49 +0000786/*
drh13bff812003-04-15 01:19:47 +0000787** This routine is called to create a connection to a database BTree
788** driver. If zFilename is the name of a file, then that file is
789** opened and used. If zFilename is the magic name ":memory:" then
790** the database is stored in memory (and is thus forgotten as soon as
791** the connection is closed.) If zFilename is NULL then the database
792** is for temporary use only and is deleted as soon as the connection
793** is closed.
drh90f5ecb2004-07-22 01:19:35 +0000794**
795** A temporary database can be either a disk file (that is automatically
796** deleted when the file is closed) or a set of red-black trees held in memory,
797** depending on the values of the TEMP_STORE compile-time macro and the
798** db->temp_store variable, according to the following chart:
799**
800** TEMP_STORE db->temp_store Location of temporary database
801** ---------- -------------- ------------------------------
802** 0 any file
803** 1 1 file
804** 1 2 memory
805** 1 0 file
806** 2 1 file
807** 2 2 memory
808** 2 0 memory
809** 3 any memory
paulb0208cc2003-04-13 18:26:49 +0000810*/
danielk19774adee202004-05-08 08:23:19 +0000811int sqlite3BtreeFactory(
paulb0208cc2003-04-13 18:26:49 +0000812 const sqlite *db, /* Main database when opening aux otherwise 0 */
813 const char *zFilename, /* Name of the file containing the BTree database */
814 int omitJournal, /* if TRUE then do not journal this file */
815 int nCache, /* How many pages in the page cache */
danielk19774adee202004-05-08 08:23:19 +0000816 Btree **ppBtree /* Pointer to new Btree object written here */
817){
danielk19774adee202004-05-08 08:23:19 +0000818 int btree_flags = 0;
drh90f5ecb2004-07-22 01:19:35 +0000819 int rc;
danielk19774adee202004-05-08 08:23:19 +0000820
drheec983e2004-05-08 10:11:36 +0000821 assert( ppBtree != 0);
danielk19774adee202004-05-08 08:23:19 +0000822 if( omitJournal ){
823 btree_flags |= BTREE_OMIT_JOURNAL;
paulb0208cc2003-04-13 18:26:49 +0000824 }
drh90f5ecb2004-07-22 01:19:35 +0000825 if( zFilename==0 ){
826#ifndef TEMP_STORE
drh22ac46d2004-08-14 18:34:54 +0000827# define TEMP_STORE 1
drh90f5ecb2004-07-22 01:19:35 +0000828#endif
829#if TEMP_STORE==0
drh22ac46d2004-08-14 18:34:54 +0000830 /* Do nothing */
drh90f5ecb2004-07-22 01:19:35 +0000831#endif
832#if TEMP_STORE==1
drh22ac46d2004-08-14 18:34:54 +0000833 if( db->temp_store==2 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000834#endif
835#if TEMP_STORE==2
drh22ac46d2004-08-14 18:34:54 +0000836 if( db->temp_store!=1 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000837#endif
838#if TEMP_STORE==3
drh22ac46d2004-08-14 18:34:54 +0000839 zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000840#endif
841 }
danielk19774adee202004-05-08 08:23:19 +0000842
drh90f5ecb2004-07-22 01:19:35 +0000843 rc = sqlite3BtreeOpen(zFilename, ppBtree, btree_flags);
844 if( rc==SQLITE_OK ){
845 sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler);
846 sqlite3BtreeSetCacheSize(*ppBtree, nCache);
847 }
848 return rc;
paulb0208cc2003-04-13 18:26:49 +0000849}
danielk19774adee202004-05-08 08:23:19 +0000850
danielk19774ad17132004-05-21 01:47:26 +0000851/*
852** Return UTF-8 encoded English language explanation of the most recent
853** error.
854*/
danielk19776622cce2004-05-20 11:00:52 +0000855const char *sqlite3_errmsg(sqlite3 *db){
danielk1977bfd6cce2004-06-18 04:24:54 +0000856 if( !db || !db->pErr ){
danielk19774ad17132004-05-21 01:47:26 +0000857 /* If db is NULL, then assume that a malloc() failed during an
858 ** sqlite3_open() call.
859 */
danielk1977f20b21c2004-05-31 23:56:42 +0000860 return sqlite3ErrStr(SQLITE_NOMEM);
danielk19774ad17132004-05-21 01:47:26 +0000861 }
danielk1977b2c7f5b2004-06-26 10:02:16 +0000862 if( db->magic!=SQLITE_MAGIC_OPEN &&
863 db->magic!=SQLITE_MAGIC_BUSY &&
864 db->magic!=SQLITE_MAGIC_CLOSED
865 ){
danielk1977e35ee192004-06-26 09:50:11 +0000866 return sqlite3ErrStr(SQLITE_MISUSE);
867 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000868 if( !sqlite3_value_text(db->pErr) ){
869 return sqlite3ErrStr(db->errCode);
danielk19776622cce2004-05-20 11:00:52 +0000870 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000871 return sqlite3_value_text(db->pErr);
danielk19776622cce2004-05-20 11:00:52 +0000872}
873
danielk19774ad17132004-05-21 01:47:26 +0000874/*
875** Return UTF-16 encoded English language explanation of the most recent
876** error.
877*/
danielk19776622cce2004-05-20 11:00:52 +0000878const void *sqlite3_errmsg16(sqlite3 *db){
danielk1977bfd6cce2004-06-18 04:24:54 +0000879 /* Because all the characters in the string are in the unicode
880 ** range 0x00-0xFF, if we pad the big-endian string with a
881 ** zero byte, we can obtain the little-endian string with
882 ** &big_endian[1].
883 */
884 static char outOfMemBe[] = {
885 0, 'o', 0, 'u', 0, 't', 0, ' ',
886 0, 'o', 0, 'f', 0, ' ',
887 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0
888 };
danielk1977e35ee192004-06-26 09:50:11 +0000889 static char misuseBe [] = {
890 0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ',
891 0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ',
892 0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ',
893 0, 'o', 0, 'u', 0, 't', 0, ' ',
894 0, 'o', 0, 'f', 0, ' ',
895 0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0
896 };
danielk19774ad17132004-05-21 01:47:26 +0000897
danielk1977bfd6cce2004-06-18 04:24:54 +0000898 if( db && db->pErr ){
danielk1977312d6b32004-06-29 13:18:23 +0000899 if( db->magic!=SQLITE_MAGIC_OPEN &&
900 db->magic!=SQLITE_MAGIC_BUSY &&
901 db->magic!=SQLITE_MAGIC_CLOSED
902 ){
danielk1977e35ee192004-06-26 09:50:11 +0000903 return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
904 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000905 if( !sqlite3_value_text16(db->pErr) ){
906 sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
907 SQLITE_UTF8, SQLITE_STATIC);
danielk19774ad17132004-05-21 01:47:26 +0000908 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000909 if( sqlite3_value_text16(db->pErr) ){
910 return sqlite3_value_text16(db->pErr);
danielk19776622cce2004-05-20 11:00:52 +0000911 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000912 }
913
914 /* If db is NULL, then assume that a malloc() failed during an
915 ** sqlite3_open() call. We have a static version of the string
916 ** "out of memory" encoded using UTF-16 just for this purpose.
917 */
918 return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
danielk19776622cce2004-05-20 11:00:52 +0000919}
920
921int sqlite3_errcode(sqlite3 *db){
danielk19775c4c7782004-06-16 10:39:23 +0000922 if( !db ) return SQLITE_NOMEM;
danielk19776622cce2004-05-20 11:00:52 +0000923 return db->errCode;
924}
925
926/*
drhc275b4e2004-07-19 17:25:24 +0000927** Check schema cookies in all databases. If any cookie is out
drha6ecd332004-06-10 00:29:09 +0000928** of date, return 0. If all schema cookies are current, return 1.
929*/
930static int schemaIsValid(sqlite *db){
931 int iDb;
932 int rc;
933 BtCursor *curTemp;
934 int cookie;
935 int allOk = 1;
936
937 for(iDb=0; allOk && iDb<db->nDb; iDb++){
938 Btree *pBt;
drha6ecd332004-06-10 00:29:09 +0000939 pBt = db->aDb[iDb].pBt;
940 if( pBt==0 ) continue;
941 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
942 if( rc==SQLITE_OK ){
danielk1977e0d4b062004-06-28 01:11:46 +0000943 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
drha6ecd332004-06-10 00:29:09 +0000944 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].schema_cookie ){
945 allOk = 0;
946 }
947 sqlite3BtreeCloseCursor(curTemp);
948 }
949 }
950 return allOk;
951}
952
953/*
danielk19776622cce2004-05-20 11:00:52 +0000954** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
955*/
956int sqlite3_prepare(
957 sqlite3 *db, /* Database handle. */
958 const char *zSql, /* UTF-8 encoded SQL statement. */
959 int nBytes, /* Length of zSql in bytes. */
960 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
961 const char** pzTail /* OUT: End of parsed string */
962){
963 Parse sParse;
964 char *zErrMsg = 0;
965 int rc = SQLITE_OK;
966
danielk197796fb0dd2004-06-30 09:49:22 +0000967 if( sqlite3_malloc_failed ){
968 return SQLITE_NOMEM;
969 }
970
danielk19775c4c7782004-06-16 10:39:23 +0000971 assert( ppStmt );
972 *ppStmt = 0;
danielk19776622cce2004-05-20 11:00:52 +0000973 if( sqlite3SafetyOn(db) ){
danielk1977e35ee192004-06-26 09:50:11 +0000974 return SQLITE_MISUSE;
danielk19776622cce2004-05-20 11:00:52 +0000975 }
976
danielk19776622cce2004-05-20 11:00:52 +0000977 memset(&sParse, 0, sizeof(sParse));
978 sParse.db = db;
979 sqlite3RunParser(&sParse, zSql, &zErrMsg);
980
981 if( db->xTrace && !db->init.busy ){
982 /* Trace only the statment that was compiled.
983 ** Make a copy of that part of the SQL string since zSQL is const
984 ** and we must pass a zero terminated string to the trace function
985 ** The copy is unnecessary if the tail pointer is pointing at the
986 ** beginnig or end of the SQL string.
987 */
988 if( sParse.zTail && sParse.zTail!=zSql && *sParse.zTail ){
989 char *tmpSql = sqliteStrNDup(zSql, sParse.zTail - zSql);
990 if( tmpSql ){
991 db->xTrace(db->pTraceArg, tmpSql);
drhb97759e2004-06-29 11:26:59 +0000992 sqliteFree(tmpSql);
danielk19776622cce2004-05-20 11:00:52 +0000993 }else{
994 /* If a memory error occurred during the copy,
995 ** trace entire SQL string and fall through to the
996 ** sqlite3_malloc_failed test to report the error.
997 */
998 db->xTrace(db->pTraceArg, zSql);
999 }
1000 }else{
1001 db->xTrace(db->pTraceArg, zSql);
1002 }
1003 }
1004
drh35d4c2f2004-06-10 01:30:59 +00001005 /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
1006 ** on in debugging mode.
1007 */
1008#ifdef SQLITE_DEBUG
1009 if( (db->flags & SQLITE_SqlTrace)!=0 && sParse.zTail && sParse.zTail!=zSql ){
1010 sqlite3DebugPrintf("SQL-trace: %.*s\n", sParse.zTail - zSql, zSql);
1011 }
1012#endif /* SQLITE_DEBUG */
1013
1014
danielk19776622cce2004-05-20 11:00:52 +00001015 if( sqlite3_malloc_failed ){
1016 rc = SQLITE_NOMEM;
1017 sqlite3RollbackAll(db);
1018 sqlite3ResetInternalSchema(db, 0);
1019 db->flags &= ~SQLITE_InTrans;
1020 goto prepare_out;
1021 }
1022 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
drh3f7d4e42004-07-24 14:35:58 +00001023 if( sParse.rc!=SQLITE_OK && sParse.checkSchema && !schemaIsValid(db) ){
drha6ecd332004-06-10 00:29:09 +00001024 sParse.rc = SQLITE_SCHEMA;
1025 }
danielk19776622cce2004-05-20 11:00:52 +00001026 if( sParse.rc==SQLITE_SCHEMA ){
1027 sqlite3ResetInternalSchema(db, 0);
1028 }
danielk19776622cce2004-05-20 11:00:52 +00001029 if( pzTail ) *pzTail = sParse.zTail;
danielk19776622cce2004-05-20 11:00:52 +00001030 rc = sParse.rc;
1031
danielk197722322fd2004-05-25 23:35:17 +00001032 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
1033 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
danielk19773cf86062004-05-26 10:11:05 +00001034 sqlite3VdbeSetColName(sParse.pVdbe, 0, "addr", P3_STATIC);
1035 sqlite3VdbeSetColName(sParse.pVdbe, 1, "opcode", P3_STATIC);
1036 sqlite3VdbeSetColName(sParse.pVdbe, 2, "p1", P3_STATIC);
1037 sqlite3VdbeSetColName(sParse.pVdbe, 3, "p2", P3_STATIC);
1038 sqlite3VdbeSetColName(sParse.pVdbe, 4, "p3", P3_STATIC);
danielk197722322fd2004-05-25 23:35:17 +00001039 }
1040
danielk19776622cce2004-05-20 11:00:52 +00001041prepare_out:
danielk197722322fd2004-05-25 23:35:17 +00001042 if( sqlite3SafetyOff(db) ){
1043 rc = SQLITE_MISUSE;
1044 }
danielk19775c4c7782004-06-16 10:39:23 +00001045 if( rc==SQLITE_OK ){
1046 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
1047 }else if( sParse.pVdbe ){
danielk1977cfe9a692004-06-16 12:00:29 +00001048 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
danielk19775c4c7782004-06-16 10:39:23 +00001049 }
1050
danielk19776622cce2004-05-20 11:00:52 +00001051 if( zErrMsg ){
1052 sqlite3Error(db, rc, "%s", zErrMsg);
danielk1977b20e56b2004-06-15 13:36:30 +00001053 sqliteFree(zErrMsg);
danielk19776622cce2004-05-20 11:00:52 +00001054 }else{
1055 sqlite3Error(db, rc, 0);
1056 }
1057 return rc;
1058}
1059
1060/*
1061** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
1062*/
1063int sqlite3_prepare16(
1064 sqlite3 *db, /* Database handle. */
1065 const void *zSql, /* UTF-8 encoded SQL statement. */
1066 int nBytes, /* Length of zSql in bytes. */
1067 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1068 const void **pzTail /* OUT: End of parsed string */
1069){
1070 /* This function currently works by first transforming the UTF-16
1071 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
1072 ** tricky bit is figuring out the pointer to return in *pzTail.
1073 */
danielk1977bfd6cce2004-06-18 04:24:54 +00001074 char const *zSql8 = 0;
danielk19776622cce2004-05-20 11:00:52 +00001075 char const *zTail8 = 0;
1076 int rc;
danielk1977bfd6cce2004-06-18 04:24:54 +00001077 sqlite3_value *pTmp;
danielk19776622cce2004-05-20 11:00:52 +00001078
danielk1977bfd6cce2004-06-18 04:24:54 +00001079 pTmp = sqlite3GetTransientValue(db);
1080 sqlite3ValueSetStr(pTmp, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1081 zSql8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
danielk19776622cce2004-05-20 11:00:52 +00001082 if( !zSql8 ){
1083 sqlite3Error(db, SQLITE_NOMEM, 0);
1084 return SQLITE_NOMEM;
1085 }
1086 rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);
1087
1088 if( zTail8 && pzTail ){
1089 /* If sqlite3_prepare returns a tail pointer, we calculate the
1090 ** equivalent pointer into the UTF-16 string by counting the unicode
1091 ** characters between zSql8 and zTail8, and then returning a pointer
1092 ** the same number of characters into the UTF-16 string.
1093 */
1094 int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8);
1095 *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed);
1096 }
1097
1098 return rc;
1099}
1100
danielk19774ad17132004-05-21 01:47:26 +00001101/*
1102** This routine does the work of opening a database on behalf of
1103** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
1104** is UTF-8 encoded. The fourth argument, "def_enc" is one of the TEXT_*
1105** macros from sqliteInt.h. If we end up creating a new database file
1106** (not opening an existing one), the text encoding of the database
1107** will be set to this value.
1108*/
1109static int openDatabase(
1110 const char *zFilename, /* Database filename UTF-8 encoded */
danielk19778e227872004-06-07 07:52:17 +00001111 sqlite3 **ppDb /* OUT: Returned database handle */
danielk19774ad17132004-05-21 01:47:26 +00001112){
1113 sqlite3 *db;
1114 int rc, i;
1115 char *zErrMsg = 0;
1116
1117 /* Allocate the sqlite data structure */
1118 db = sqliteMalloc( sizeof(sqlite) );
1119 if( db==0 ) goto opendb_out;
danielk19774ad17132004-05-21 01:47:26 +00001120 db->priorNewRowid = 0;
1121 db->magic = SQLITE_MAGIC_BUSY;
1122 db->nDb = 2;
1123 db->aDb = db->aDbStatic;
danielk1977dc8453f2004-06-12 00:42:34 +00001124 db->enc = SQLITE_UTF8;
danielk19771d850a72004-05-31 08:26:49 +00001125 db->autoCommit = 1;
danielk19774ad17132004-05-21 01:47:26 +00001126 /* db->flags |= SQLITE_ShortColNames; */
drhf9b596e2004-05-26 16:54:42 +00001127 sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
danielk19774ad17132004-05-21 01:47:26 +00001128 sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
1129 for(i=0; i<db->nDb; i++){
1130 sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
1131 sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
1132 sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);
1133 sqlite3HashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1);
1134 }
danielk19774ad17132004-05-21 01:47:26 +00001135
danielk19770202b292004-06-09 09:55:16 +00001136 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
1137 ** and UTF-16, so add a version for each to avoid any unnecessary
1138 ** conversions. The only error that can occur here is a malloc() failure.
1139 */
danielk1977466be562004-06-10 02:16:01 +00001140 sqlite3_create_collation(db, "BINARY", SQLITE_UTF8, 0,binaryCollatingFunc);
1141 sqlite3_create_collation(db, "BINARY", SQLITE_UTF16LE, 0,binaryCollatingFunc);
1142 sqlite3_create_collation(db, "BINARY", SQLITE_UTF16BE, 0,binaryCollatingFunc);
1143 db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0);
danielk19770202b292004-06-09 09:55:16 +00001144 if( !db->pDfltColl ){
1145 rc = db->errCode;
1146 assert( rc!=SQLITE_OK );
1147 db->magic = SQLITE_MAGIC_CLOSED;
1148 goto opendb_out;
1149 }
1150
1151 /* Also add a UTF-8 case-insensitive collation sequence. */
danielk1977466be562004-06-10 02:16:01 +00001152 sqlite3_create_collation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc);
danielk19770202b292004-06-09 09:55:16 +00001153
danielk19774ad17132004-05-21 01:47:26 +00001154 /* Open the backend database driver */
danielk19774ad17132004-05-21 01:47:26 +00001155 rc = sqlite3BtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt);
1156 if( rc!=SQLITE_OK ){
danielk19774ad17132004-05-21 01:47:26 +00001157 sqlite3Error(db, rc, 0);
1158 db->magic = SQLITE_MAGIC_CLOSED;
1159 goto opendb_out;
1160 }
1161 db->aDb[0].zName = "main";
1162 db->aDb[1].zName = "temp";
1163
danielk197791cf71b2004-06-26 06:37:06 +00001164 /* The default safety_level for the main database is 'full' for the temp
1165 ** database it is 'NONE'. This matches the pager layer defaults. */
1166 db->aDb[0].safety_level = 3;
1167 db->aDb[1].safety_level = 1;
1168
danielk19778e227872004-06-07 07:52:17 +00001169 /* Register all built-in functions, but do not attempt to read the
1170 ** database schema yet. This is delayed until the first time the database
1171 ** is accessed.
1172 */
danielk19774ad17132004-05-21 01:47:26 +00001173 sqlite3RegisterBuiltinFunctions(db);
danielk19778e227872004-06-07 07:52:17 +00001174 if( rc==SQLITE_OK ){
danielk1977bfd6cce2004-06-18 04:24:54 +00001175 sqlite3Error(db, SQLITE_OK, 0);
danielk19774ad17132004-05-21 01:47:26 +00001176 db->magic = SQLITE_MAGIC_OPEN;
danielk19778e227872004-06-07 07:52:17 +00001177 }else{
1178 sqlite3Error(db, rc, "%s", zErrMsg, 0);
1179 if( zErrMsg ) sqliteFree(zErrMsg);
1180 db->magic = SQLITE_MAGIC_CLOSED;
danielk19774ad17132004-05-21 01:47:26 +00001181 }
danielk19774ad17132004-05-21 01:47:26 +00001182
1183opendb_out:
danielk197713073932004-06-30 11:54:06 +00001184 if( sqlite3_errcode(db)==SQLITE_OK && sqlite3_malloc_failed ){
1185 sqlite3Error(db, SQLITE_NOMEM, 0);
1186 }
danielk19774ad17132004-05-21 01:47:26 +00001187 *ppDb = db;
1188 return sqlite3_errcode(db);
1189}
1190
1191/*
1192** Open a new database handle.
1193*/
danielk197780290862004-05-22 09:21:21 +00001194int sqlite3_open(
danielk19774ad17132004-05-21 01:47:26 +00001195 const char *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00001196 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00001197){
danielk19778e227872004-06-07 07:52:17 +00001198 return openDatabase(zFilename, ppDb);
danielk197783ab5a82004-05-21 11:39:05 +00001199}
1200
danielk19774ad17132004-05-21 01:47:26 +00001201/*
1202** Open a new database handle.
1203*/
1204int sqlite3_open16(
1205 const void *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00001206 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00001207){
danielk1977bfd6cce2004-06-18 04:24:54 +00001208 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
1209 int rc = SQLITE_NOMEM;
1210 sqlite3_value *pVal;
danielk19774ad17132004-05-21 01:47:26 +00001211
1212 assert( ppDb );
danielk1977bfd6cce2004-06-18 04:24:54 +00001213 *ppDb = 0;
1214 pVal = sqlite3ValueNew();
1215 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1216 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
1217 if( zFilename8 ){
1218 rc = openDatabase(zFilename8, ppDb);
1219 if( rc==SQLITE_OK && *ppDb ){
1220 sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
1221 }
danielk19774ad17132004-05-21 01:47:26 +00001222 }
danielk1977bfd6cce2004-06-18 04:24:54 +00001223 if( pVal ){
1224 sqlite3ValueFree(pVal);
danielk19774ad17132004-05-21 01:47:26 +00001225 }
danielk19778e227872004-06-07 07:52:17 +00001226
danielk19774ad17132004-05-21 01:47:26 +00001227 return rc;
1228}
1229
danielk1977106bb232004-05-21 10:08:53 +00001230/*
1231** The following routine destroys a virtual machine that is created by
1232** the sqlite3_compile() routine. The integer returned is an SQLITE_
1233** success/failure code that describes the result of executing the virtual
1234** machine.
1235**
1236** This routine sets the error code and string returned by
1237** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
1238*/
danielk1977fc57d7b2004-05-26 02:04:57 +00001239int sqlite3_finalize(sqlite3_stmt *pStmt){
drhfeac5f82004-08-01 00:10:45 +00001240 return pStmt ? sqlite3VdbeFinalize((Vdbe*)pStmt) : SQLITE_OK;
danielk1977106bb232004-05-21 10:08:53 +00001241}
1242
1243/*
1244** Terminate the current execution of an SQL statement and reset it
1245** back to its starting state so that it can be reused. A success code from
1246** the prior execution is returned.
1247**
1248** This routine sets the error code and string returned by
1249** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
1250*/
danielk1977fc57d7b2004-05-26 02:04:57 +00001251int sqlite3_reset(sqlite3_stmt *pStmt){
danielk19779e6db7d2004-06-21 08:18:51 +00001252 int rc = sqlite3VdbeReset((Vdbe*)pStmt);
danielk1977106bb232004-05-21 10:08:53 +00001253 sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0);
1254 return rc;
1255}
danielk19770202b292004-06-09 09:55:16 +00001256
danielk1977d8123362004-06-12 09:25:12 +00001257/*
1258** Register a new collation sequence with the database handle db.
1259*/
danielk19770202b292004-06-09 09:55:16 +00001260int sqlite3_create_collation(
1261 sqlite3* db,
1262 const char *zName,
danielk1977466be562004-06-10 02:16:01 +00001263 int enc,
danielk19770202b292004-06-09 09:55:16 +00001264 void* pCtx,
1265 int(*xCompare)(void*,int,const void*,int,const void*)
1266){
1267 CollSeq *pColl;
1268 int rc = SQLITE_OK;
danielk1977d8123362004-06-12 09:25:12 +00001269
1270 /* If SQLITE_UTF16 is specified as the encoding type, transform this
1271 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
1272 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
1273 */
1274 if( enc==SQLITE_UTF16 ){
1275 enc = SQLITE_UTF16NATIVE;
1276 }
1277
danielk1977466be562004-06-10 02:16:01 +00001278 if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16LE && enc!=SQLITE_UTF16BE ){
1279 sqlite3Error(db, SQLITE_ERROR,
1280 "Param 3 to sqlite3_create_collation() must be one of "
danielk1977d8123362004-06-12 09:25:12 +00001281 "SQLITE_UTF8, SQLITE_UTF16, SQLITE_UTF16LE or SQLITE_UTF16BE"
danielk1977466be562004-06-10 02:16:01 +00001282 );
1283 return SQLITE_ERROR;
1284 }
1285 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 1);
danielk19770202b292004-06-09 09:55:16 +00001286 if( 0==pColl ){
1287 rc = SQLITE_NOMEM;
danielk19770202b292004-06-09 09:55:16 +00001288 }else{
1289 pColl->xCmp = xCompare;
1290 pColl->pUser = pCtx;
danielk1977312d6b32004-06-29 13:18:23 +00001291 pColl->enc = enc;
danielk19770202b292004-06-09 09:55:16 +00001292 }
1293 sqlite3Error(db, rc, 0);
danielk1977466be562004-06-10 02:16:01 +00001294 return rc;
danielk19770202b292004-06-09 09:55:16 +00001295}
1296
danielk1977d8123362004-06-12 09:25:12 +00001297/*
1298** Register a new collation sequence with the database handle db.
1299*/
danielk19770202b292004-06-09 09:55:16 +00001300int sqlite3_create_collation16(
1301 sqlite3* db,
1302 const char *zName,
danielk1977466be562004-06-10 02:16:01 +00001303 int enc,
danielk19770202b292004-06-09 09:55:16 +00001304 void* pCtx,
1305 int(*xCompare)(void*,int,const void*,int,const void*)
1306){
danielk1977bfd6cce2004-06-18 04:24:54 +00001307 char const *zName8;
1308 sqlite3_value *pTmp = sqlite3GetTransientValue(db);
1309 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1310 zName8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
1311 return sqlite3_create_collation(db, zName8, enc, pCtx, xCompare);
danielk19770202b292004-06-09 09:55:16 +00001312}
danielk19777cedc8d2004-06-10 10:50:08 +00001313
danielk1977d8123362004-06-12 09:25:12 +00001314/*
1315** Register a collation sequence factory callback with the database handle
1316** db. Replace any previously installed collation sequence factory.
1317*/
danielk19777cedc8d2004-06-10 10:50:08 +00001318int sqlite3_collation_needed(
1319 sqlite3 *db,
1320 void *pCollNeededArg,
1321 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
1322){
1323 db->xCollNeeded = xCollNeeded;
1324 db->xCollNeeded16 = 0;
1325 db->pCollNeededArg = pCollNeededArg;
1326 return SQLITE_OK;
1327}
danielk1977d8123362004-06-12 09:25:12 +00001328
1329/*
1330** Register a collation sequence factory callback with the database handle
1331** db. Replace any previously installed collation sequence factory.
1332*/
danielk19777cedc8d2004-06-10 10:50:08 +00001333int sqlite3_collation_needed16(
1334 sqlite3 *db,
1335 void *pCollNeededArg,
1336 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
1337){
1338 db->xCollNeeded = 0;
1339 db->xCollNeeded16 = xCollNeeded16;
1340 db->pCollNeededArg = pCollNeededArg;
1341 return SQLITE_OK;
1342}