blob: d36777df9c0a44b6e9d6bb4ec34eb5c8466ed210 [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**
drh7bec5052005-02-06 02:45:41 +000017** $Id: main.c,v 1.280 2005/02/06 02:45:42 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;
drh9bb575f2004-09-06 17:24:11 +000056 sqlite3 *db = pData->db;
drh234c39d2004-07-24 03:30:47 +000057 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*/
drh9bb575f2004-09-06 17:24:11 +0000116static int sqlite3InitOne(sqlite3 *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 */
drh57196282004-10-06 15:41:16 +0000131 static const 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 ;
drh57196282004-10-06 15:41:16 +0000140 static const char temp_master_schema[] =
drhe0bc4042002-06-25 01:09:11 +0000141 "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 */
drhdc3ff9c2004-08-18 02:10:15 +0000187 if( db->aDb[iDb].pBt==0 ){
188 if( iDb==1 ) DbSetProperty(db, 1, DB_SchemaLoaded);
189 return SQLITE_OK;
190 }
danielk19778e150812004-05-10 01:17:37 +0000191 rc = sqlite3BtreeCursor(db->aDb[iDb].pBt, MASTER_ROOT, 0, 0, 0, &curMain);
drhf328bc82004-05-10 23:29:49 +0000192 if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
danielk1977f20b21c2004-05-31 23:56:42 +0000193 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
drh92ed08a2002-07-30 18:43:40 +0000194 return rc;
195 }
drhe0bc4042002-06-25 01:09:11 +0000196
drha3b321d2004-05-11 09:31:31 +0000197 /* Get the database meta information.
198 **
199 ** Meta values are as follows:
200 ** meta[0] Schema cookie. Changes with each schema change.
201 ** meta[1] File format of schema layer.
202 ** meta[2] Size of the page cache.
drhae157872004-08-14 19:20:09 +0000203 ** meta[3] Use freelist if 0. Autovacuum if greater than zero.
danielk1977dc8453f2004-06-12 00:42:34 +0000204 ** meta[4] Db text encoding. 1:UTF-8 3:UTF-16 LE 4:UTF-16 BE
danielk1977dae24952004-11-11 05:10:43 +0000205 ** meta[5] The user cookie. Used by the application.
danielk1977962398d2004-06-14 09:35:16 +0000206 ** meta[6]
drha3b321d2004-05-11 09:31:31 +0000207 ** meta[7]
208 ** meta[8]
209 ** meta[9]
danielk1977172bc392004-05-22 08:09:11 +0000210 **
danielk1977dc8453f2004-06-12 00:42:34 +0000211 ** Note: The hash defined SQLITE_UTF* symbols in sqliteInt.h correspond to
danielk1977172bc392004-05-22 08:09:11 +0000212 ** the possible values of meta[4].
drhe0bc4042002-06-25 01:09:11 +0000213 */
drhf328bc82004-05-10 23:29:49 +0000214 if( rc==SQLITE_OK ){
215 int i;
drha3b321d2004-05-11 09:31:31 +0000216 for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
danielk1977e0d4b062004-06-28 01:11:46 +0000217 rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, i+1, (u32 *)&meta[i]);
danielk19774adee202004-05-08 08:23:19 +0000218 }
drhf328bc82004-05-10 23:29:49 +0000219 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000220 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
drhf328bc82004-05-10 23:29:49 +0000221 sqlite3BtreeCloseCursor(curMain);
222 return rc;
223 }
224 }else{
225 memset(meta, 0, sizeof(meta));
drhe0bc4042002-06-25 01:09:11 +0000226 }
drha3b321d2004-05-11 09:31:31 +0000227 db->aDb[iDb].schema_cookie = meta[0];
danielk19773df6b252004-05-29 10:23:19 +0000228
229 /* If opening a non-empty database, check the text encoding. For the
230 ** main database, set sqlite3.enc to the encoding of the main database.
231 ** For an attached db, it is an error if the encoding is not the same
232 ** as sqlite3.enc.
233 */
234 if( meta[4] ){ /* text encoding */
235 if( iDb==0 ){
236 /* If opening the main database, set db->enc. */
danielk1977172bc392004-05-22 08:09:11 +0000237 db->enc = (u8)meta[4];
danielk1977466be562004-06-10 02:16:01 +0000238 db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0);
danielk19773df6b252004-05-29 10:23:19 +0000239 }else{
240 /* If opening an attached database, the encoding much match db->enc */
241 if( meta[4]!=db->enc ){
242 sqlite3BtreeCloseCursor(curMain);
243 sqlite3SetString(pzErrMsg, "attached databases must use the same"
244 " text encoding as main database", (char*)0);
245 return SQLITE_ERROR;
246 }
danielk1977172bc392004-05-22 08:09:11 +0000247 }
danielk19773df6b252004-05-29 10:23:19 +0000248 }
249
danielk197791cf71b2004-06-26 06:37:06 +0000250 size = meta[2];
251 if( size==0 ){ size = MAX_PAGES; }
252 db->aDb[iDb].cache_size = size;
drhe0bc4042002-06-25 01:09:11 +0000253
danielk197791cf71b2004-06-26 06:37:06 +0000254 if( iDb==0 ){
danielk19773df6b252004-05-29 10:23:19 +0000255 db->file_format = meta[1];
drh1c2d8412003-03-31 00:30:47 +0000256 if( db->file_format==0 ){
257 /* This happens if the database was initially empty */
drhf328bc82004-05-10 23:29:49 +0000258 db->file_format = 1;
drh1c2d8412003-03-31 00:30:47 +0000259 }
drh28037572000-08-02 13:47:41 +0000260 }
danielk19773df6b252004-05-29 10:23:19 +0000261
262 /*
263 ** file_format==1 Version 3.0.0.
264 */
265 if( meta[1]>1 ){
266 sqlite3BtreeCloseCursor(curMain);
267 sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
268 return SQLITE_ERROR;
269 }
270
danielk197791cf71b2004-06-26 06:37:06 +0000271 sqlite3BtreeSetCacheSize(db->aDb[iDb].pBt, db->aDb[iDb].cache_size);
drhaacc5432002-01-06 17:07:40 +0000272
drhe0bc4042002-06-25 01:09:11 +0000273 /* Read the schema information out of the schema tables
drhaacc5432002-01-06 17:07:40 +0000274 */
drh1d85d932004-02-14 23:05:52 +0000275 assert( db->init.busy );
drhf328bc82004-05-10 23:29:49 +0000276 if( rc==SQLITE_EMPTY ){
277 /* For an empty database, there is nothing to read */
278 rc = SQLITE_OK;
drh1c2d8412003-03-31 00:30:47 +0000279 }else{
drh234c39d2004-07-24 03:30:47 +0000280 char *zSql;
281 zSql = sqlite3MPrintf(
danielk1977c60e9b82005-01-31 12:42:29 +0000282 "SELECT name, rootpage, sql, '%s' FROM '%q'.%s",
drh234c39d2004-07-24 03:30:47 +0000283 zDbNum, db->aDb[iDb].zName, zMasterName);
danielk19773df6b252004-05-29 10:23:19 +0000284 sqlite3SafetyOff(db);
danielk1977d008cfe2004-06-19 02:22:10 +0000285 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
drhf328bc82004-05-10 23:29:49 +0000286 sqlite3SafetyOn(db);
drh234c39d2004-07-24 03:30:47 +0000287 sqliteFree(zSql);
drhf328bc82004-05-10 23:29:49 +0000288 sqlite3BtreeCloseCursor(curMain);
drh1c2d8412003-03-31 00:30:47 +0000289 }
danielk197724b03fd2004-05-10 10:34:34 +0000290 if( sqlite3_malloc_failed ){
danielk19774adee202004-05-08 08:23:19 +0000291 sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
drh1d85d932004-02-14 23:05:52 +0000292 rc = SQLITE_NOMEM;
danielk19774adee202004-05-08 08:23:19 +0000293 sqlite3ResetInternalSchema(db, 0);
drhe0bc4042002-06-25 01:09:11 +0000294 }
drh1d85d932004-02-14 23:05:52 +0000295 if( rc==SQLITE_OK ){
drh8bf8dc92003-05-17 17:35:10 +0000296 DbSetProperty(db, iDb, DB_SchemaLoaded);
drh1c2d8412003-03-31 00:30:47 +0000297 }else{
danielk19774adee202004-05-08 08:23:19 +0000298 sqlite3ResetInternalSchema(db, iDb);
drh1c2d8412003-03-31 00:30:47 +0000299 }
drh1d85d932004-02-14 23:05:52 +0000300 return rc;
drh1c2d8412003-03-31 00:30:47 +0000301}
302
303/*
304** Initialize all database files - the main database file, the file
305** used to store temporary tables, and any additional database files
306** created using ATTACH statements. Return a success code. If an
307** error occurs, write an error message into *pzErrMsg.
308**
309** After the database is initialized, the SQLITE_Initialized
danielk19778e227872004-06-07 07:52:17 +0000310** bit is set in the flags field of the sqlite structure.
drh1c2d8412003-03-31 00:30:47 +0000311*/
drh9bb575f2004-09-06 17:24:11 +0000312int sqlite3Init(sqlite3 *db, char **pzErrMsg){
drh1c2d8412003-03-31 00:30:47 +0000313 int i, rc;
314
drh1d85d932004-02-14 23:05:52 +0000315 if( db->init.busy ) return SQLITE_OK;
drh1c2d8412003-03-31 00:30:47 +0000316 assert( (db->flags & SQLITE_Initialized)==0 );
317 rc = SQLITE_OK;
drh1d85d932004-02-14 23:05:52 +0000318 db->init.busy = 1;
drh1c2d8412003-03-31 00:30:47 +0000319 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk1977d008cfe2004-06-19 02:22:10 +0000320 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
danielk19774adee202004-05-08 08:23:19 +0000321 rc = sqlite3InitOne(db, i, pzErrMsg);
drh8ef83ff2004-02-12 15:31:21 +0000322 if( rc ){
danielk19774adee202004-05-08 08:23:19 +0000323 sqlite3ResetInternalSchema(db, i);
drh8ef83ff2004-02-12 15:31:21 +0000324 }
drh1c2d8412003-03-31 00:30:47 +0000325 }
danielk1977d008cfe2004-06-19 02:22:10 +0000326
327 /* Once all the other databases have been initialised, load the schema
328 ** for the TEMP database. This is loaded last, as the TEMP database
329 ** schema may contain references to objects in other databases.
330 */
331 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
332 rc = sqlite3InitOne(db, 1, pzErrMsg);
333 if( rc ){
334 sqlite3ResetInternalSchema(db, 1);
335 }
336 }
337
drh1d85d932004-02-14 23:05:52 +0000338 db->init.busy = 0;
drh1c2d8412003-03-31 00:30:47 +0000339 if( rc==SQLITE_OK ){
drh58b95762000-06-02 01:17:37 +0000340 db->flags |= SQLITE_Initialized;
danielk19774adee202004-05-08 08:23:19 +0000341 sqlite3CommitInternalChanges(db);
drh2d71ca92004-02-10 02:27:04 +0000342 }
343
drh2d71ca92004-02-10 02:27:04 +0000344 if( rc!=SQLITE_OK ){
drhe0bc4042002-06-25 01:09:11 +0000345 db->flags &= ~SQLITE_Initialized;
drh58b95762000-06-02 01:17:37 +0000346 }
drh1c2d8412003-03-31 00:30:47 +0000347 return rc;
drh58b95762000-06-02 01:17:37 +0000348}
349
350/*
danielk19778e227872004-06-07 07:52:17 +0000351** This routine is a no-op if the database schema is already initialised.
352** Otherwise, the schema is loaded. An error code is returned.
353*/
danielk19778a414492004-06-29 08:59:35 +0000354int sqlite3ReadSchema(Parse *pParse){
danielk19778e227872004-06-07 07:52:17 +0000355 int rc = SQLITE_OK;
danielk19778a414492004-06-29 08:59:35 +0000356 sqlite3 *db = pParse->db;
danielk19778e227872004-06-07 07:52:17 +0000357 if( !db->init.busy ){
358 if( (db->flags & SQLITE_Initialized)==0 ){
danielk19778a414492004-06-29 08:59:35 +0000359 rc = sqlite3Init(db, &pParse->zErrMsg);
danielk19778e227872004-06-07 07:52:17 +0000360 }
361 }
danielk1977c0391392004-06-09 12:30:04 +0000362 assert( rc!=SQLITE_OK || (db->flags & SQLITE_Initialized)||db->init.busy );
danielk19778a414492004-06-29 08:59:35 +0000363 if( rc!=SQLITE_OK ){
364 pParse->rc = rc;
365 pParse->nErr++;
366 }
danielk19778e227872004-06-07 07:52:17 +0000367 return rc;
368}
369
370/*
drhb217a572000-08-22 13:40:18 +0000371** The version of the library
372*/
drh38f82712004-06-18 17:10:16 +0000373const char rcsid3[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $";
danielk197724b03fd2004-05-10 10:34:34 +0000374const char sqlite3_version[] = SQLITE_VERSION;
drh4aec8b62004-08-28 16:19:00 +0000375const char *sqlite3_libversion(void){ return sqlite3_version; }
danielk197799ba19e2005-02-05 07:33:34 +0000376int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
drhb217a572000-08-22 13:40:18 +0000377
378/*
drhd3d39e92004-05-20 22:16:29 +0000379** This is the default collating function named "BINARY" which is always
380** available.
381*/
danielk19772c336542005-01-13 02:14:23 +0000382static int binCollFunc(
drhd3d39e92004-05-20 22:16:29 +0000383 void *NotUsed,
384 int nKey1, const void *pKey1,
385 int nKey2, const void *pKey2
386){
387 int rc, n;
388 n = nKey1<nKey2 ? nKey1 : nKey2;
389 rc = memcmp(pKey1, pKey2, n);
390 if( rc==0 ){
391 rc = nKey1 - nKey2;
392 }
393 return rc;
394}
395
396/*
danielk1977dc1bdc42004-06-11 10:51:27 +0000397** Another built-in collating sequence: NOCASE.
398**
399** This collating sequence is intended to be used for "case independant
400** comparison". SQLite's knowledge of upper and lower case equivalents
401** extends only to the 26 characters used in the English language.
402**
403** At the moment there is only a UTF-8 implementation.
danielk19770202b292004-06-09 09:55:16 +0000404*/
405static int nocaseCollatingFunc(
406 void *NotUsed,
407 int nKey1, const void *pKey1,
408 int nKey2, const void *pKey2
409){
410 int r = sqlite3StrNICmp(
drh208f80a2004-08-29 20:08:58 +0000411 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
danielk19770202b292004-06-09 09:55:16 +0000412 if( 0==r ){
413 r = nKey1-nKey2;
414 }
415 return r;
416}
417
418/*
drhaf9ff332002-01-16 21:00:27 +0000419** Return the ROWID of the most recent insert
420*/
drh9bb575f2004-09-06 17:24:11 +0000421sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
drhaf9ff332002-01-16 21:00:27 +0000422 return db->lastRowid;
423}
424
425/*
danielk197724b03fd2004-05-10 10:34:34 +0000426** Return the number of changes in the most recent call to sqlite3_exec().
drhc8d30ac2002-04-12 10:08:59 +0000427*/
drh9bb575f2004-09-06 17:24:11 +0000428int sqlite3_changes(sqlite3 *db){
drhc8d30ac2002-04-12 10:08:59 +0000429 return db->nChange;
430}
431
rdcf146a772004-02-25 22:51:06 +0000432/*
danielk1977b28af712004-06-21 06:50:26 +0000433** Return the number of changes since the database handle was opened.
rdcf146a772004-02-25 22:51:06 +0000434*/
danielk1977b28af712004-06-21 06:50:26 +0000435int sqlite3_total_changes(sqlite3 *db){
436 return db->nTotalChange;
rdcb0c374f2004-02-20 22:53:38 +0000437}
438
drhc8d30ac2002-04-12 10:08:59 +0000439/*
drh50e5dad2001-09-15 00:57:28 +0000440** Close an existing SQLite database
441*/
drh9bb575f2004-09-06 17:24:11 +0000442int sqlite3_close(sqlite3 *db){
drh8e0a2f92002-02-23 23:45:45 +0000443 HashElem *i;
drh001bbcb2003-03-19 03:14:00 +0000444 int j;
danielk19775c4c7782004-06-16 10:39:23 +0000445
446 if( !db ){
danielk197796d81f92004-06-19 03:33:57 +0000447 return SQLITE_OK;
danielk19775c4c7782004-06-16 10:39:23 +0000448 }
drhc60d0442004-09-30 13:43:13 +0000449 if( sqlite3SafetyCheck(db) ){
danielk1977e35ee192004-06-26 09:50:11 +0000450 return SQLITE_MISUSE;
451 }
452
danielk197796d81f92004-06-19 03:33:57 +0000453 /* If there are any outstanding VMs, return SQLITE_BUSY. */
454 if( db->pVdbe ){
455 sqlite3Error(db, SQLITE_BUSY,
456 "Unable to close due to unfinalised statements");
457 return SQLITE_BUSY;
458 }
459 assert( !sqlite3SafetyCheck(db) );
danielk1977e0048402004-06-15 16:51:01 +0000460
461 /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database
462 ** cannot be opened for some reason. So this routine needs to run in
463 ** that case. But maybe there should be an extra magic value for the
464 ** "failed to open" state.
465 */
danielk197796d81f92004-06-19 03:33:57 +0000466 if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){
drh94e92032003-02-16 22:21:32 +0000467 /* printf("DID NOT CLOSE\n"); fflush(stdout); */
danielk197796d81f92004-06-19 03:33:57 +0000468 return SQLITE_ERROR;
drh94e92032003-02-16 22:21:32 +0000469 }
danielk1977e0048402004-06-15 16:51:01 +0000470
drh001bbcb2003-03-19 03:14:00 +0000471 for(j=0; j<db->nDb; j++){
drh4d189ca2004-02-12 18:46:38 +0000472 struct Db *pDb = &db->aDb[j];
473 if( pDb->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000474 sqlite3BtreeClose(pDb->pBt);
drh4d189ca2004-02-12 18:46:38 +0000475 pDb->pBt = 0;
drh113088e2003-03-20 01:16:58 +0000476 }
drhf57b3392001-10-08 13:22:32 +0000477 }
danielk19774adee202004-05-08 08:23:19 +0000478 sqlite3ResetInternalSchema(db, 0);
drh1c2d8412003-03-31 00:30:47 +0000479 assert( db->nDb<=2 );
480 assert( db->aDb==db->aDbStatic );
drh0bce8352002-02-28 00:41:10 +0000481 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
482 FuncDef *pFunc, *pNext;
483 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
drh8e0a2f92002-02-23 23:45:45 +0000484 pNext = pFunc->pNext;
485 sqliteFree(pFunc);
486 }
487 }
danielk1977466be562004-06-10 02:16:01 +0000488
danielk1977d8123362004-06-12 09:25:12 +0000489 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
danielk1977466be562004-06-10 02:16:01 +0000490 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
danielk1977d8123362004-06-12 09:25:12 +0000491 sqliteFree(pColl);
danielk1977466be562004-06-10 02:16:01 +0000492 }
danielk1977d8123362004-06-12 09:25:12 +0000493 sqlite3HashClear(&db->aCollSeq);
danielk1977466be562004-06-10 02:16:01 +0000494
danielk19774adee202004-05-08 08:23:19 +0000495 sqlite3HashClear(&db->aFunc);
danielk19776622cce2004-05-20 11:00:52 +0000496 sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
danielk1977bfd6cce2004-06-18 04:24:54 +0000497 if( db->pValue ){
498 sqlite3ValueFree(db->pValue);
499 }
500 if( db->pErr ){
501 sqlite3ValueFree(db->pErr);
502 }
danielk197796d81f92004-06-19 03:33:57 +0000503
504 db->magic = SQLITE_MAGIC_ERROR;
drh75897232000-05-29 14:26:00 +0000505 sqliteFree(db);
danielk197796d81f92004-06-19 03:33:57 +0000506 return SQLITE_OK;
drh75897232000-05-29 14:26:00 +0000507}
508
509/*
drh001bbcb2003-03-19 03:14:00 +0000510** Rollback all database files.
511*/
drh9bb575f2004-09-06 17:24:11 +0000512void sqlite3RollbackAll(sqlite3 *db){
drh001bbcb2003-03-19 03:14:00 +0000513 int i;
514 for(i=0; i<db->nDb; i++){
515 if( db->aDb[i].pBt ){
danielk19774adee202004-05-08 08:23:19 +0000516 sqlite3BtreeRollback(db->aDb[i].pBt);
drh001bbcb2003-03-19 03:14:00 +0000517 db->aDb[i].inTrans = 0;
518 }
519 }
danielk19774adee202004-05-08 08:23:19 +0000520 sqlite3ResetInternalSchema(db, 0);
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 ){
drhc60d0442004-09-30 13:43:13 +0000530 case SQLITE_ROW:
531 case SQLITE_DONE:
drhc22bd472002-05-10 13:14:07 +0000532 case SQLITE_OK: z = "not an error"; break;
533 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
534 case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break;
535 case SQLITE_PERM: z = "access permission denied"; break;
536 case SQLITE_ABORT: z = "callback requested query abort"; break;
537 case SQLITE_BUSY: z = "database is locked"; break;
538 case SQLITE_LOCKED: z = "database table is locked"; break;
539 case SQLITE_NOMEM: z = "out of memory"; break;
540 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
541 case SQLITE_INTERRUPT: z = "interrupted"; break;
542 case SQLITE_IOERR: z = "disk I/O error"; break;
543 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
544 case SQLITE_NOTFOUND: z = "table or record not found"; break;
545 case SQLITE_FULL: z = "database is full"; break;
546 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
547 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
548 case SQLITE_EMPTY: z = "table contains no data"; break;
549 case SQLITE_SCHEMA: z = "database schema has changed"; break;
550 case SQLITE_TOOBIG: z = "too much data for one table row"; break;
551 case SQLITE_CONSTRAINT: z = "constraint failed"; break;
552 case SQLITE_MISMATCH: z = "datatype mismatch"; break;
553 case SQLITE_MISUSE: z = "library routine called out of sequence";break;
drh8766c342002-11-09 00:33:15 +0000554 case SQLITE_NOLFS: z = "kernel lacks large file support"; break;
drhed6c8672003-01-12 18:02:16 +0000555 case SQLITE_AUTH: z = "authorization denied"; break;
jplyon892f6712003-06-12 08:59:00 +0000556 case SQLITE_FORMAT: z = "auxiliary database format error"; break;
drhb08153d2004-11-20 20:18:55 +0000557 case SQLITE_RANGE: z = "bind or column index out of range"; break;
drhc602f9a2004-02-12 19:01:04 +0000558 case SQLITE_NOTADB: z = "file is encrypted or is not a database";break;
drhc22bd472002-05-10 13:14:07 +0000559 default: z = "unknown error"; break;
drh247be432002-05-10 05:44:55 +0000560 }
drhc22bd472002-05-10 13:14:07 +0000561 return z;
drh247be432002-05-10 05:44:55 +0000562}
563
564/*
drh2dfbbca2000-07-28 14:32:48 +0000565** This routine implements a busy callback that sleeps and tries
566** again until a timeout value is reached. The timeout value is
567** an integer number of milliseconds passed in as the first
568** argument.
569*/
drhdaffd0e2001-04-11 14:28:42 +0000570static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000571 void *Timeout, /* Maximum amount of time to wait */
drh2dfbbca2000-07-28 14:32:48 +0000572 int count /* Number of times table has been busy */
573){
drh8cfbf082001-09-19 13:22:39 +0000574#if SQLITE_MIN_SLEEP_MS==1
drhd1bec472004-01-15 13:29:31 +0000575 static const char delays[] =
576 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 50, 100};
577 static const short int totals[] =
578 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228, 287};
579# define NDELAY (sizeof(delays)/sizeof(delays[0]))
drhc44af712004-09-02 15:53:56 +0000580 ptr timeout = (ptr)Timeout;
581 ptr delay, prior;
drh2dfbbca2000-07-28 14:32:48 +0000582
drhd1bec472004-01-15 13:29:31 +0000583 if( count <= NDELAY ){
584 delay = delays[count-1];
585 prior = totals[count-1];
586 }else{
587 delay = delays[NDELAY-1];
588 prior = totals[NDELAY-1] + delay*(count-NDELAY-1);
drh2dfbbca2000-07-28 14:32:48 +0000589 }
drhd1bec472004-01-15 13:29:31 +0000590 if( prior + delay > timeout ){
591 delay = timeout - prior;
drh2dfbbca2000-07-28 14:32:48 +0000592 if( delay<=0 ) return 0;
593 }
danielk19774adee202004-05-08 08:23:19 +0000594 sqlite3OsSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000595 return 1;
596#else
597 int timeout = (int)Timeout;
598 if( (count+1)*1000 > timeout ){
599 return 0;
600 }
danielk19774adee202004-05-08 08:23:19 +0000601 sqlite3OsSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000602 return 1;
603#endif
604}
605
606/*
607** This routine sets the busy callback for an Sqlite database to the
608** given callback function with the given argument.
609*/
danielk1977f9d64d22004-06-19 08:18:07 +0000610int sqlite3_busy_handler(
611 sqlite3 *db,
danielk19772a764eb2004-06-12 01:43:26 +0000612 int (*xBusy)(void*,int),
drh2dfbbca2000-07-28 14:32:48 +0000613 void *pArg
614){
drhc60d0442004-09-30 13:43:13 +0000615 if( sqlite3SafetyCheck(db) ){
616 return SQLITE_MISUSE;
617 }
danielk197724162fe2004-06-04 06:22:00 +0000618 db->busyHandler.xFunc = xBusy;
619 db->busyHandler.pArg = pArg;
danielk1977f9d64d22004-06-19 08:18:07 +0000620 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000621}
622
danielk1977348bb5d2003-10-18 09:37:26 +0000623#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
624/*
625** This routine sets the progress callback for an Sqlite database to the
626** given callback function with the given argument. The progress callback will
627** be invoked every nOps opcodes.
628*/
danielk197724b03fd2004-05-10 10:34:34 +0000629void sqlite3_progress_handler(
drh9bb575f2004-09-06 17:24:11 +0000630 sqlite3 *db,
danielk1977348bb5d2003-10-18 09:37:26 +0000631 int nOps,
632 int (*xProgress)(void*),
633 void *pArg
634){
drhc60d0442004-09-30 13:43:13 +0000635 if( !sqlite3SafetyCheck(db) ){
636 if( nOps>0 ){
637 db->xProgress = xProgress;
638 db->nProgressOps = nOps;
639 db->pProgressArg = pArg;
640 }else{
641 db->xProgress = 0;
642 db->nProgressOps = 0;
643 db->pProgressArg = 0;
644 }
danielk1977348bb5d2003-10-18 09:37:26 +0000645 }
646}
647#endif
648
649
drh2dfbbca2000-07-28 14:32:48 +0000650/*
651** This routine installs a default busy handler that waits for the
652** specified number of milliseconds before returning 0.
653*/
danielk1977f9d64d22004-06-19 08:18:07 +0000654int sqlite3_busy_timeout(sqlite3 *db, int ms){
drh2dfbbca2000-07-28 14:32:48 +0000655 if( ms>0 ){
drhc44af712004-09-02 15:53:56 +0000656 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)(ptr)ms);
drh2dfbbca2000-07-28 14:32:48 +0000657 }else{
danielk197724b03fd2004-05-10 10:34:34 +0000658 sqlite3_busy_handler(db, 0, 0);
drh2dfbbca2000-07-28 14:32:48 +0000659 }
danielk1977f9d64d22004-06-19 08:18:07 +0000660 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000661}
drh4c504392000-10-16 22:06:40 +0000662
663/*
664** Cause any pending operation to stop at its earliest opportunity.
665*/
drh9bb575f2004-09-06 17:24:11 +0000666void sqlite3_interrupt(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000667 if( !sqlite3SafetyCheck(db) ){
668 db->flags |= SQLITE_Interrupt;
669 }
drh4c504392000-10-16 22:06:40 +0000670}
drhfa86c412002-02-02 15:01:15 +0000671
672/*
673** Windows systems should call this routine to free memory that
danielk197724b03fd2004-05-10 10:34:34 +0000674** is returned in the in the errmsg parameter of sqlite3_open() when
drhfa86c412002-02-02 15:01:15 +0000675** SQLite is a DLL. For some reason, it does not work to call free()
676** directly.
677**
drhae29ffb2004-09-25 14:39:18 +0000678** Note that we need to call free() not sqliteFree() here.
drhfa86c412002-02-02 15:01:15 +0000679*/
drh3f4fedb2004-05-31 19:34:33 +0000680void sqlite3_free(char *p){ free(p); }
drhfa86c412002-02-02 15:01:15 +0000681
682/*
drhdf014892004-06-02 00:41:09 +0000683** Create new user functions.
drhfa86c412002-02-02 15:01:15 +0000684*/
danielk197724b03fd2004-05-10 10:34:34 +0000685int sqlite3_create_function(
danielk197765904932004-05-26 06:18:37 +0000686 sqlite3 *db,
687 const char *zFunctionName,
688 int nArg,
danielk1977d8123362004-06-12 09:25:12 +0000689 int enc,
danielk197765904932004-05-26 06:18:37 +0000690 void *pUserData,
691 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
692 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
693 void (*xFinal)(sqlite3_context*)
drh8e0a2f92002-02-23 23:45:45 +0000694){
drh0bce8352002-02-28 00:41:10 +0000695 FuncDef *p;
drh4b59ab52002-08-24 18:24:51 +0000696 int nName;
danielk197765904932004-05-26 06:18:37 +0000697
drhc60d0442004-09-30 13:43:13 +0000698 if( sqlite3SafetyCheck(db) ){
699 return SQLITE_MISUSE;
700 }
701 if( zFunctionName==0 ||
danielk1977398eae72004-05-26 06:58:43 +0000702 (xFunc && (xFinal || xStep)) ||
703 (!xFunc && (xFinal && !xStep)) ||
704 (!xFunc && (!xFinal && xStep)) ||
705 (nArg<-1 || nArg>127) ||
706 (255<(nName = strlen(zFunctionName))) ){
danielk197765904932004-05-26 06:18:37 +0000707 return SQLITE_ERROR;
708 }
danielk1977d8123362004-06-12 09:25:12 +0000709
danielk197793758c82005-01-21 08:13:14 +0000710#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +0000711 /* If SQLITE_UTF16 is specified as the encoding type, transform this
712 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
713 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
714 **
715 ** If SQLITE_ANY is specified, add three versions of the function
716 ** to the hash table.
717 */
718 if( enc==SQLITE_UTF16 ){
719 enc = SQLITE_UTF16NATIVE;
720 }else if( enc==SQLITE_ANY ){
721 int rc;
722 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF8,
danielk1977f9d64d22004-06-19 08:18:07 +0000723 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000724 if( rc!=SQLITE_OK ) return rc;
725 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF16LE,
danielk1977f9d64d22004-06-19 08:18:07 +0000726 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000727 if( rc!=SQLITE_OK ) return rc;
728 enc = SQLITE_UTF16BE;
729 }
danielk197793758c82005-01-21 08:13:14 +0000730#else
731 enc = SQLITE_UTF8;
732#endif
danielk19779636c4e2005-01-25 04:27:54 +0000733
734 /* Check if an existing function is being overridden or deleted. If so,
735 ** and there are active VMs, then return SQLITE_BUSY. If a function
736 ** is being overridden/deleted but there are no active VMs, allow the
737 ** operation to continue but invalidate all precompiled statements.
738 */
739 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0);
740 if( p && p->iPrefEnc==enc && p->nArg==nArg ){
741 if( db->activeVdbeCnt ){
742 sqlite3Error(db, SQLITE_BUSY,
743 "Unable to delete/modify user-function due to active statements");
744 return SQLITE_BUSY;
745 }else{
746 sqlite3ExpirePreparedStatements(db);
747 }
748 }
danielk197765904932004-05-26 06:18:37 +0000749
danielk1977d8123362004-06-12 09:25:12 +0000750 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);
danielk197713073932004-06-30 11:54:06 +0000751 if( p==0 ) return SQLITE_NOMEM;
drh8e0a2f92002-02-23 23:45:45 +0000752 p->xFunc = xFunc;
drh8e0a2f92002-02-23 23:45:45 +0000753 p->xStep = xStep;
danielk197765904932004-05-26 06:18:37 +0000754 p->xFinalize = xFinal;
drh1350b032002-02-27 19:00:20 +0000755 p->pUserData = pUserData;
danielk197765904932004-05-26 06:18:37 +0000756 return SQLITE_OK;
757}
danielk197793758c82005-01-21 08:13:14 +0000758#ifndef SQLITE_OMIT_UTF16
danielk197765904932004-05-26 06:18:37 +0000759int sqlite3_create_function16(
760 sqlite3 *db,
761 const void *zFunctionName,
762 int nArg,
763 int eTextRep,
danielk197765904932004-05-26 06:18:37 +0000764 void *pUserData,
765 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
766 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
767 void (*xFinal)(sqlite3_context*)
768){
769 int rc;
danielk1977bfd6cce2004-06-18 04:24:54 +0000770 char const *zFunc8;
drhc60d0442004-09-30 13:43:13 +0000771 sqlite3_value *pTmp;
danielk1977bfd6cce2004-06-18 04:24:54 +0000772
drhc60d0442004-09-30 13:43:13 +0000773 if( sqlite3SafetyCheck(db) ){
774 return SQLITE_MISUSE;
775 }
776 pTmp = sqlite3GetTransientValue(db);
danielk1977bfd6cce2004-06-18 04:24:54 +0000777 sqlite3ValueSetStr(pTmp, -1, zFunctionName, SQLITE_UTF16NATIVE,SQLITE_STATIC);
778 zFunc8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
779
780 if( !zFunc8 ){
danielk197765904932004-05-26 06:18:37 +0000781 return SQLITE_NOMEM;
782 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000783 rc = sqlite3_create_function(db, zFunc8, nArg, eTextRep,
danielk1977f9d64d22004-06-19 08:18:07 +0000784 pUserData, xFunc, xStep, xFinal);
danielk197765904932004-05-26 06:18:37 +0000785 return rc;
drh8e0a2f92002-02-23 23:45:45 +0000786}
danielk197793758c82005-01-21 08:13:14 +0000787#endif
drhc9b84a12002-06-20 11:36:48 +0000788
789/*
drh18de4822003-01-16 16:28:53 +0000790** Register a trace function. The pArg from the previously registered trace
791** is returned.
792**
793** A NULL trace function means that no tracing is executes. A non-NULL
794** trace is a pointer to a function that is invoked at the start of each
danielk197724b03fd2004-05-10 10:34:34 +0000795** sqlite3_exec().
drh18de4822003-01-16 16:28:53 +0000796*/
drh9bb575f2004-09-06 17:24:11 +0000797void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
drh18de4822003-01-16 16:28:53 +0000798 void *pOld = db->pTraceArg;
799 db->xTrace = xTrace;
800 db->pTraceArg = pArg;
801 return pOld;
drh0d1a6432003-04-03 15:46:04 +0000802}
paulb0208cc2003-04-13 18:26:49 +0000803
drhaa940ea2004-01-15 02:44:03 +0000804/*** EXPERIMENTAL ***
805**
806** Register a function to be invoked when a transaction comments.
807** If either function returns non-zero, then the commit becomes a
808** rollback.
809*/
danielk197724b03fd2004-05-10 10:34:34 +0000810void *sqlite3_commit_hook(
drh9bb575f2004-09-06 17:24:11 +0000811 sqlite3 *db, /* Attach the hook to this database */
drhaa940ea2004-01-15 02:44:03 +0000812 int (*xCallback)(void*), /* Function to invoke on each commit */
813 void *pArg /* Argument to the function */
814){
815 void *pOld = db->pCommitArg;
816 db->xCommitCallback = xCallback;
817 db->pCommitArg = pArg;
818 return pOld;
819}
820
821
paulb0208cc2003-04-13 18:26:49 +0000822/*
drh13bff812003-04-15 01:19:47 +0000823** This routine is called to create a connection to a database BTree
824** driver. If zFilename is the name of a file, then that file is
825** opened and used. If zFilename is the magic name ":memory:" then
826** the database is stored in memory (and is thus forgotten as soon as
827** the connection is closed.) If zFilename is NULL then the database
828** is for temporary use only and is deleted as soon as the connection
829** is closed.
drh90f5ecb2004-07-22 01:19:35 +0000830**
831** A temporary database can be either a disk file (that is automatically
832** deleted when the file is closed) or a set of red-black trees held in memory,
833** depending on the values of the TEMP_STORE compile-time macro and the
834** db->temp_store variable, according to the following chart:
835**
836** TEMP_STORE db->temp_store Location of temporary database
837** ---------- -------------- ------------------------------
838** 0 any file
839** 1 1 file
840** 1 2 memory
841** 1 0 file
842** 2 1 file
843** 2 2 memory
844** 2 0 memory
845** 3 any memory
paulb0208cc2003-04-13 18:26:49 +0000846*/
danielk19774adee202004-05-08 08:23:19 +0000847int sqlite3BtreeFactory(
drh9bb575f2004-09-06 17:24:11 +0000848 const sqlite3 *db, /* Main database when opening aux otherwise 0 */
paulb0208cc2003-04-13 18:26:49 +0000849 const char *zFilename, /* Name of the file containing the BTree database */
850 int omitJournal, /* if TRUE then do not journal this file */
851 int nCache, /* How many pages in the page cache */
danielk19774adee202004-05-08 08:23:19 +0000852 Btree **ppBtree /* Pointer to new Btree object written here */
853){
danielk19774adee202004-05-08 08:23:19 +0000854 int btree_flags = 0;
drh90f5ecb2004-07-22 01:19:35 +0000855 int rc;
danielk19774adee202004-05-08 08:23:19 +0000856
drheec983e2004-05-08 10:11:36 +0000857 assert( ppBtree != 0);
danielk19774adee202004-05-08 08:23:19 +0000858 if( omitJournal ){
859 btree_flags |= BTREE_OMIT_JOURNAL;
paulb0208cc2003-04-13 18:26:49 +0000860 }
drh7bec5052005-02-06 02:45:41 +0000861 if( db->flags & SQLITE_NoReadlock ){
862 btree_flags |= BTREE_NO_READLOCK;
863 }
drh90f5ecb2004-07-22 01:19:35 +0000864 if( zFilename==0 ){
drh90f5ecb2004-07-22 01:19:35 +0000865#if TEMP_STORE==0
drh22ac46d2004-08-14 18:34:54 +0000866 /* Do nothing */
drh90f5ecb2004-07-22 01:19:35 +0000867#endif
danielk197703aded42004-11-22 05:26:27 +0000868#ifndef SQLITE_OMIT_MEMORYDB
drh90f5ecb2004-07-22 01:19:35 +0000869#if TEMP_STORE==1
drh22ac46d2004-08-14 18:34:54 +0000870 if( db->temp_store==2 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000871#endif
872#if TEMP_STORE==2
drh22ac46d2004-08-14 18:34:54 +0000873 if( db->temp_store!=1 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000874#endif
875#if TEMP_STORE==3
drh22ac46d2004-08-14 18:34:54 +0000876 zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000877#endif
danielk197703aded42004-11-22 05:26:27 +0000878#endif /* SQLITE_OMIT_MEMORYDB */
drh90f5ecb2004-07-22 01:19:35 +0000879 }
danielk19774adee202004-05-08 08:23:19 +0000880
drh90f5ecb2004-07-22 01:19:35 +0000881 rc = sqlite3BtreeOpen(zFilename, ppBtree, btree_flags);
882 if( rc==SQLITE_OK ){
883 sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler);
884 sqlite3BtreeSetCacheSize(*ppBtree, nCache);
885 }
886 return rc;
paulb0208cc2003-04-13 18:26:49 +0000887}
danielk19774adee202004-05-08 08:23:19 +0000888
danielk19774ad17132004-05-21 01:47:26 +0000889/*
890** Return UTF-8 encoded English language explanation of the most recent
891** error.
892*/
danielk19776622cce2004-05-20 11:00:52 +0000893const char *sqlite3_errmsg(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000894 const char *z;
895 if( sqlite3_malloc_failed ){
danielk1977f20b21c2004-05-31 23:56:42 +0000896 return sqlite3ErrStr(SQLITE_NOMEM);
danielk19774ad17132004-05-21 01:47:26 +0000897 }
drhc60d0442004-09-30 13:43:13 +0000898 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
danielk1977e35ee192004-06-26 09:50:11 +0000899 return sqlite3ErrStr(SQLITE_MISUSE);
900 }
drhc60d0442004-09-30 13:43:13 +0000901 z = sqlite3_value_text(db->pErr);
902 if( z==0 ){
903 z = sqlite3ErrStr(db->errCode);
danielk19776622cce2004-05-20 11:00:52 +0000904 }
drhc60d0442004-09-30 13:43:13 +0000905 return z;
danielk19776622cce2004-05-20 11:00:52 +0000906}
907
drh6c626082004-11-14 21:56:29 +0000908#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +0000909/*
910** Return UTF-16 encoded English language explanation of the most recent
911** error.
912*/
danielk19776622cce2004-05-20 11:00:52 +0000913const void *sqlite3_errmsg16(sqlite3 *db){
danielk1977bfd6cce2004-06-18 04:24:54 +0000914 /* Because all the characters in the string are in the unicode
915 ** range 0x00-0xFF, if we pad the big-endian string with a
916 ** zero byte, we can obtain the little-endian string with
917 ** &big_endian[1].
918 */
drh57196282004-10-06 15:41:16 +0000919 static const char outOfMemBe[] = {
danielk1977bfd6cce2004-06-18 04:24:54 +0000920 0, 'o', 0, 'u', 0, 't', 0, ' ',
921 0, 'o', 0, 'f', 0, ' ',
922 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0
923 };
drh57196282004-10-06 15:41:16 +0000924 static const char misuseBe [] = {
danielk1977e35ee192004-06-26 09:50:11 +0000925 0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ',
926 0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ',
927 0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ',
928 0, 'o', 0, 'u', 0, 't', 0, ' ',
929 0, 'o', 0, 'f', 0, ' ',
930 0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0
931 };
danielk19774ad17132004-05-21 01:47:26 +0000932
drhc60d0442004-09-30 13:43:13 +0000933 const void *z;
934 if( sqlite3_malloc_failed ){
935 return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
936 }
937 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
938 return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
939 }
940 z = sqlite3_value_text16(db->pErr);
941 if( z==0 ){
942 sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
943 SQLITE_UTF8, SQLITE_STATIC);
944 z = sqlite3_value_text16(db->pErr);
945 }
946 return z;
danielk19776622cce2004-05-20 11:00:52 +0000947}
drh6c626082004-11-14 21:56:29 +0000948#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +0000949
drhc60d0442004-09-30 13:43:13 +0000950/*
951** Return the most recent error code generated by an SQLite routine.
952*/
danielk19776622cce2004-05-20 11:00:52 +0000953int sqlite3_errcode(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000954 if( sqlite3_malloc_failed ){
955 return SQLITE_NOMEM;
956 }
957 if( sqlite3SafetyCheck(db) ){
958 return SQLITE_MISUSE;
959 }
danielk19776622cce2004-05-20 11:00:52 +0000960 return db->errCode;
961}
962
963/*
drhc275b4e2004-07-19 17:25:24 +0000964** Check schema cookies in all databases. If any cookie is out
drha6ecd332004-06-10 00:29:09 +0000965** of date, return 0. If all schema cookies are current, return 1.
966*/
drh9bb575f2004-09-06 17:24:11 +0000967static int schemaIsValid(sqlite3 *db){
drha6ecd332004-06-10 00:29:09 +0000968 int iDb;
969 int rc;
970 BtCursor *curTemp;
971 int cookie;
972 int allOk = 1;
973
974 for(iDb=0; allOk && iDb<db->nDb; iDb++){
975 Btree *pBt;
drha6ecd332004-06-10 00:29:09 +0000976 pBt = db->aDb[iDb].pBt;
977 if( pBt==0 ) continue;
978 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
979 if( rc==SQLITE_OK ){
danielk1977e0d4b062004-06-28 01:11:46 +0000980 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
drha6ecd332004-06-10 00:29:09 +0000981 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].schema_cookie ){
982 allOk = 0;
983 }
984 sqlite3BtreeCloseCursor(curTemp);
985 }
986 }
987 return allOk;
988}
989
990/*
danielk19776622cce2004-05-20 11:00:52 +0000991** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
992*/
993int sqlite3_prepare(
994 sqlite3 *db, /* Database handle. */
995 const char *zSql, /* UTF-8 encoded SQL statement. */
996 int nBytes, /* Length of zSql in bytes. */
997 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
998 const char** pzTail /* OUT: End of parsed string */
999){
1000 Parse sParse;
1001 char *zErrMsg = 0;
1002 int rc = SQLITE_OK;
1003
danielk197796fb0dd2004-06-30 09:49:22 +00001004 if( sqlite3_malloc_failed ){
1005 return SQLITE_NOMEM;
1006 }
1007
danielk19775c4c7782004-06-16 10:39:23 +00001008 assert( ppStmt );
1009 *ppStmt = 0;
danielk19776622cce2004-05-20 11:00:52 +00001010 if( sqlite3SafetyOn(db) ){
danielk1977e35ee192004-06-26 09:50:11 +00001011 return SQLITE_MISUSE;
danielk19776622cce2004-05-20 11:00:52 +00001012 }
1013
danielk19776622cce2004-05-20 11:00:52 +00001014 memset(&sParse, 0, sizeof(sParse));
1015 sParse.db = db;
1016 sqlite3RunParser(&sParse, zSql, &zErrMsg);
1017
danielk19776622cce2004-05-20 11:00:52 +00001018 if( sqlite3_malloc_failed ){
1019 rc = SQLITE_NOMEM;
1020 sqlite3RollbackAll(db);
1021 sqlite3ResetInternalSchema(db, 0);
1022 db->flags &= ~SQLITE_InTrans;
1023 goto prepare_out;
1024 }
1025 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
drh3f7d4e42004-07-24 14:35:58 +00001026 if( sParse.rc!=SQLITE_OK && sParse.checkSchema && !schemaIsValid(db) ){
drha6ecd332004-06-10 00:29:09 +00001027 sParse.rc = SQLITE_SCHEMA;
1028 }
danielk19776622cce2004-05-20 11:00:52 +00001029 if( sParse.rc==SQLITE_SCHEMA ){
1030 sqlite3ResetInternalSchema(db, 0);
1031 }
danielk19776622cce2004-05-20 11:00:52 +00001032 if( pzTail ) *pzTail = sParse.zTail;
danielk19776622cce2004-05-20 11:00:52 +00001033 rc = sParse.rc;
1034
danielk197793758c82005-01-21 08:13:14 +00001035#ifndef SQLITE_OMIT_EXPLAIN
danielk197722322fd2004-05-25 23:35:17 +00001036 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
1037 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
danielk19773cf86062004-05-26 10:11:05 +00001038 sqlite3VdbeSetColName(sParse.pVdbe, 0, "addr", P3_STATIC);
1039 sqlite3VdbeSetColName(sParse.pVdbe, 1, "opcode", P3_STATIC);
1040 sqlite3VdbeSetColName(sParse.pVdbe, 2, "p1", P3_STATIC);
1041 sqlite3VdbeSetColName(sParse.pVdbe, 3, "p2", P3_STATIC);
1042 sqlite3VdbeSetColName(sParse.pVdbe, 4, "p3", P3_STATIC);
danielk197722322fd2004-05-25 23:35:17 +00001043 }
danielk197793758c82005-01-21 08:13:14 +00001044#endif
danielk197722322fd2004-05-25 23:35:17 +00001045
danielk19776622cce2004-05-20 11:00:52 +00001046prepare_out:
danielk197722322fd2004-05-25 23:35:17 +00001047 if( sqlite3SafetyOff(db) ){
1048 rc = SQLITE_MISUSE;
1049 }
danielk19775c4c7782004-06-16 10:39:23 +00001050 if( rc==SQLITE_OK ){
1051 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
1052 }else if( sParse.pVdbe ){
danielk1977cfe9a692004-06-16 12:00:29 +00001053 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
danielk19775c4c7782004-06-16 10:39:23 +00001054 }
1055
danielk19776622cce2004-05-20 11:00:52 +00001056 if( zErrMsg ){
1057 sqlite3Error(db, rc, "%s", zErrMsg);
danielk1977b20e56b2004-06-15 13:36:30 +00001058 sqliteFree(zErrMsg);
danielk19776622cce2004-05-20 11:00:52 +00001059 }else{
1060 sqlite3Error(db, rc, 0);
1061 }
1062 return rc;
1063}
1064
drh6c626082004-11-14 21:56:29 +00001065#ifndef SQLITE_OMIT_UTF16
danielk19776622cce2004-05-20 11:00:52 +00001066/*
1067** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
1068*/
1069int sqlite3_prepare16(
1070 sqlite3 *db, /* Database handle. */
1071 const void *zSql, /* UTF-8 encoded SQL statement. */
1072 int nBytes, /* Length of zSql in bytes. */
1073 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1074 const void **pzTail /* OUT: End of parsed string */
1075){
1076 /* This function currently works by first transforming the UTF-16
1077 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
1078 ** tricky bit is figuring out the pointer to return in *pzTail.
1079 */
danielk1977bfd6cce2004-06-18 04:24:54 +00001080 char const *zSql8 = 0;
danielk19776622cce2004-05-20 11:00:52 +00001081 char const *zTail8 = 0;
1082 int rc;
danielk1977bfd6cce2004-06-18 04:24:54 +00001083 sqlite3_value *pTmp;
danielk19776622cce2004-05-20 11:00:52 +00001084
drhc60d0442004-09-30 13:43:13 +00001085 if( sqlite3SafetyCheck(db) ){
1086 return SQLITE_MISUSE;
1087 }
danielk1977bfd6cce2004-06-18 04:24:54 +00001088 pTmp = sqlite3GetTransientValue(db);
1089 sqlite3ValueSetStr(pTmp, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1090 zSql8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
danielk19776622cce2004-05-20 11:00:52 +00001091 if( !zSql8 ){
1092 sqlite3Error(db, SQLITE_NOMEM, 0);
1093 return SQLITE_NOMEM;
1094 }
1095 rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);
1096
1097 if( zTail8 && pzTail ){
1098 /* If sqlite3_prepare returns a tail pointer, we calculate the
1099 ** equivalent pointer into the UTF-16 string by counting the unicode
1100 ** characters between zSql8 and zTail8, and then returning a pointer
1101 ** the same number of characters into the UTF-16 string.
1102 */
1103 int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8);
1104 *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed);
1105 }
1106
1107 return rc;
1108}
drh6c626082004-11-14 21:56:29 +00001109#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00001110
danielk19774ad17132004-05-21 01:47:26 +00001111/*
1112** This routine does the work of opening a database on behalf of
1113** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
1114** is UTF-8 encoded. The fourth argument, "def_enc" is one of the TEXT_*
1115** macros from sqliteInt.h. If we end up creating a new database file
1116** (not opening an existing one), the text encoding of the database
1117** will be set to this value.
1118*/
1119static int openDatabase(
1120 const char *zFilename, /* Database filename UTF-8 encoded */
danielk19778e227872004-06-07 07:52:17 +00001121 sqlite3 **ppDb /* OUT: Returned database handle */
danielk19774ad17132004-05-21 01:47:26 +00001122){
1123 sqlite3 *db;
1124 int rc, i;
danielk19774ad17132004-05-21 01:47:26 +00001125
1126 /* Allocate the sqlite data structure */
drh9bb575f2004-09-06 17:24:11 +00001127 db = sqliteMalloc( sizeof(sqlite3) );
danielk19774ad17132004-05-21 01:47:26 +00001128 if( db==0 ) goto opendb_out;
danielk19774ad17132004-05-21 01:47:26 +00001129 db->priorNewRowid = 0;
1130 db->magic = SQLITE_MAGIC_BUSY;
1131 db->nDb = 2;
1132 db->aDb = db->aDbStatic;
danielk1977dc8453f2004-06-12 00:42:34 +00001133 db->enc = SQLITE_UTF8;
danielk19771d850a72004-05-31 08:26:49 +00001134 db->autoCommit = 1;
drh47a6db22005-01-18 16:02:40 +00001135 db->flags |= SQLITE_ShortColNames;
drhf9b596e2004-05-26 16:54:42 +00001136 sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
danielk19774ad17132004-05-21 01:47:26 +00001137 sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
1138 for(i=0; i<db->nDb; i++){
1139 sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
1140 sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
1141 sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);
1142 sqlite3HashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1);
1143 }
danielk19774ad17132004-05-21 01:47:26 +00001144
danielk19770202b292004-06-09 09:55:16 +00001145 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
1146 ** and UTF-16, so add a version for each to avoid any unnecessary
1147 ** conversions. The only error that can occur here is a malloc() failure.
1148 */
danielk19772c336542005-01-13 02:14:23 +00001149 if( sqlite3_create_collation(db, "BINARY", SQLITE_UTF8, 0,binCollFunc) ||
1150 sqlite3_create_collation(db, "BINARY", SQLITE_UTF16, 0,binCollFunc) ||
1151 !(db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0)) ){
danielk19770202b292004-06-09 09:55:16 +00001152 rc = db->errCode;
1153 assert( rc!=SQLITE_OK );
1154 db->magic = SQLITE_MAGIC_CLOSED;
1155 goto opendb_out;
1156 }
1157
1158 /* Also add a UTF-8 case-insensitive collation sequence. */
danielk1977466be562004-06-10 02:16:01 +00001159 sqlite3_create_collation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc);
danielk19770202b292004-06-09 09:55:16 +00001160
danielk19774ad17132004-05-21 01:47:26 +00001161 /* Open the backend database driver */
danielk19774ad17132004-05-21 01:47:26 +00001162 rc = sqlite3BtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt);
1163 if( rc!=SQLITE_OK ){
danielk19774ad17132004-05-21 01:47:26 +00001164 sqlite3Error(db, rc, 0);
1165 db->magic = SQLITE_MAGIC_CLOSED;
1166 goto opendb_out;
1167 }
1168 db->aDb[0].zName = "main";
1169 db->aDb[1].zName = "temp";
1170
danielk197791cf71b2004-06-26 06:37:06 +00001171 /* The default safety_level for the main database is 'full' for the temp
1172 ** database it is 'NONE'. This matches the pager layer defaults. */
1173 db->aDb[0].safety_level = 3;
1174 db->aDb[1].safety_level = 1;
1175
danielk19778e227872004-06-07 07:52:17 +00001176 /* Register all built-in functions, but do not attempt to read the
1177 ** database schema yet. This is delayed until the first time the database
1178 ** is accessed.
1179 */
danielk19774ad17132004-05-21 01:47:26 +00001180 sqlite3RegisterBuiltinFunctions(db);
danielk19774397de52005-01-12 12:44:03 +00001181 sqlite3Error(db, SQLITE_OK, 0);
1182 db->magic = SQLITE_MAGIC_OPEN;
danielk19774ad17132004-05-21 01:47:26 +00001183
1184opendb_out:
danielk197713073932004-06-30 11:54:06 +00001185 if( sqlite3_errcode(db)==SQLITE_OK && sqlite3_malloc_failed ){
1186 sqlite3Error(db, SQLITE_NOMEM, 0);
1187 }
danielk19774ad17132004-05-21 01:47:26 +00001188 *ppDb = db;
1189 return sqlite3_errcode(db);
1190}
1191
1192/*
1193** Open a new database handle.
1194*/
danielk197780290862004-05-22 09:21:21 +00001195int sqlite3_open(
danielk19774ad17132004-05-21 01:47:26 +00001196 const char *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00001197 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00001198){
danielk19778e227872004-06-07 07:52:17 +00001199 return openDatabase(zFilename, ppDb);
danielk197783ab5a82004-05-21 11:39:05 +00001200}
1201
drh6c626082004-11-14 21:56:29 +00001202#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +00001203/*
1204** Open a new database handle.
1205*/
1206int sqlite3_open16(
1207 const void *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00001208 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00001209){
danielk1977bfd6cce2004-06-18 04:24:54 +00001210 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
1211 int rc = SQLITE_NOMEM;
1212 sqlite3_value *pVal;
danielk19774ad17132004-05-21 01:47:26 +00001213
1214 assert( ppDb );
danielk1977bfd6cce2004-06-18 04:24:54 +00001215 *ppDb = 0;
1216 pVal = sqlite3ValueNew();
1217 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1218 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
1219 if( zFilename8 ){
1220 rc = openDatabase(zFilename8, ppDb);
1221 if( rc==SQLITE_OK && *ppDb ){
1222 sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
1223 }
danielk19774ad17132004-05-21 01:47:26 +00001224 }
danielk1977bfd6cce2004-06-18 04:24:54 +00001225 if( pVal ){
1226 sqlite3ValueFree(pVal);
danielk19774ad17132004-05-21 01:47:26 +00001227 }
danielk19778e227872004-06-07 07:52:17 +00001228
danielk19774ad17132004-05-21 01:47:26 +00001229 return rc;
1230}
drh6c626082004-11-14 21:56:29 +00001231#endif /* SQLITE_OMIT_UTF16 */
danielk19774ad17132004-05-21 01:47:26 +00001232
danielk1977106bb232004-05-21 10:08:53 +00001233/*
1234** The following routine destroys a virtual machine that is created by
1235** the sqlite3_compile() routine. The integer returned is an SQLITE_
1236** success/failure code that describes the result of executing the virtual
1237** machine.
1238**
1239** This routine sets the error code and string returned by
1240** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
1241*/
danielk1977fc57d7b2004-05-26 02:04:57 +00001242int sqlite3_finalize(sqlite3_stmt *pStmt){
drh1bcdb0c2004-08-28 14:49:46 +00001243 int rc;
1244 if( pStmt==0 ){
1245 rc = SQLITE_OK;
1246 }else{
1247 rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
1248 }
1249 return rc;
danielk1977106bb232004-05-21 10:08:53 +00001250}
1251
1252/*
1253** Terminate the current execution of an SQL statement and reset it
1254** back to its starting state so that it can be reused. A success code from
1255** the prior execution is returned.
1256**
1257** This routine sets the error code and string returned by
1258** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
1259*/
danielk1977fc57d7b2004-05-26 02:04:57 +00001260int sqlite3_reset(sqlite3_stmt *pStmt){
drh1bcdb0c2004-08-28 14:49:46 +00001261 int rc;
1262 if( pStmt==0 ){
1263 rc = SQLITE_OK;
1264 }else{
1265 rc = sqlite3VdbeReset((Vdbe*)pStmt);
danielk1977b3bce662005-01-29 08:32:43 +00001266 sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0, 0, 0, 0);
drh1bcdb0c2004-08-28 14:49:46 +00001267 }
danielk1977106bb232004-05-21 10:08:53 +00001268 return rc;
1269}
danielk19770202b292004-06-09 09:55:16 +00001270
danielk1977d8123362004-06-12 09:25:12 +00001271/*
1272** Register a new collation sequence with the database handle db.
1273*/
danielk19770202b292004-06-09 09:55:16 +00001274int sqlite3_create_collation(
1275 sqlite3* db,
1276 const char *zName,
danielk1977466be562004-06-10 02:16:01 +00001277 int enc,
danielk19770202b292004-06-09 09:55:16 +00001278 void* pCtx,
1279 int(*xCompare)(void*,int,const void*,int,const void*)
1280){
1281 CollSeq *pColl;
1282 int rc = SQLITE_OK;
danielk1977d8123362004-06-12 09:25:12 +00001283
drhc60d0442004-09-30 13:43:13 +00001284 if( sqlite3SafetyCheck(db) ){
1285 return SQLITE_MISUSE;
1286 }
1287
danielk1977d8123362004-06-12 09:25:12 +00001288 /* If SQLITE_UTF16 is specified as the encoding type, transform this
1289 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
1290 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
1291 */
1292 if( enc==SQLITE_UTF16 ){
1293 enc = SQLITE_UTF16NATIVE;
1294 }
1295
danielk1977466be562004-06-10 02:16:01 +00001296 if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16LE && enc!=SQLITE_UTF16BE ){
1297 sqlite3Error(db, SQLITE_ERROR,
1298 "Param 3 to sqlite3_create_collation() must be one of "
danielk1977d8123362004-06-12 09:25:12 +00001299 "SQLITE_UTF8, SQLITE_UTF16, SQLITE_UTF16LE or SQLITE_UTF16BE"
danielk1977466be562004-06-10 02:16:01 +00001300 );
1301 return SQLITE_ERROR;
1302 }
danielk1977a21c6b62005-01-24 10:25:59 +00001303
danielk19779636c4e2005-01-25 04:27:54 +00001304 /* Check if this call is removing or replacing an existing collation
1305 ** sequence. If so, and there are active VMs, return busy. If there
1306 ** are no active VMs, invalidate any pre-compiled statements.
danielk1977a21c6b62005-01-24 10:25:59 +00001307 */
danielk19779636c4e2005-01-25 04:27:54 +00001308 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 0);
1309 if( pColl && pColl->xCmp ){
1310 if( db->activeVdbeCnt ){
1311 sqlite3Error(db, SQLITE_BUSY,
1312 "Unable to delete/modify collation sequence due to active statements");
1313 return SQLITE_BUSY;
1314 }
danielk1977a21c6b62005-01-24 10:25:59 +00001315 sqlite3ExpirePreparedStatements(db);
1316 }
1317
danielk1977466be562004-06-10 02:16:01 +00001318 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 1);
danielk19770202b292004-06-09 09:55:16 +00001319 if( 0==pColl ){
1320 rc = SQLITE_NOMEM;
danielk19770202b292004-06-09 09:55:16 +00001321 }else{
1322 pColl->xCmp = xCompare;
1323 pColl->pUser = pCtx;
danielk1977312d6b32004-06-29 13:18:23 +00001324 pColl->enc = enc;
danielk19770202b292004-06-09 09:55:16 +00001325 }
1326 sqlite3Error(db, rc, 0);
danielk1977466be562004-06-10 02:16:01 +00001327 return rc;
danielk19770202b292004-06-09 09:55:16 +00001328}
1329
drh6c626082004-11-14 21:56:29 +00001330#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00001331/*
1332** Register a new collation sequence with the database handle db.
1333*/
danielk19770202b292004-06-09 09:55:16 +00001334int sqlite3_create_collation16(
1335 sqlite3* db,
1336 const char *zName,
danielk1977466be562004-06-10 02:16:01 +00001337 int enc,
danielk19770202b292004-06-09 09:55:16 +00001338 void* pCtx,
1339 int(*xCompare)(void*,int,const void*,int,const void*)
1340){
danielk1977bfd6cce2004-06-18 04:24:54 +00001341 char const *zName8;
drhc60d0442004-09-30 13:43:13 +00001342 sqlite3_value *pTmp;
1343 if( sqlite3SafetyCheck(db) ){
1344 return SQLITE_MISUSE;
1345 }
1346 pTmp = sqlite3GetTransientValue(db);
danielk1977bfd6cce2004-06-18 04:24:54 +00001347 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1348 zName8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
1349 return sqlite3_create_collation(db, zName8, enc, pCtx, xCompare);
danielk19770202b292004-06-09 09:55:16 +00001350}
drh6c626082004-11-14 21:56:29 +00001351#endif /* SQLITE_OMIT_UTF16 */
danielk19777cedc8d2004-06-10 10:50:08 +00001352
danielk1977d8123362004-06-12 09:25:12 +00001353/*
1354** Register a collation sequence factory callback with the database handle
1355** db. Replace any previously installed collation sequence factory.
1356*/
danielk19777cedc8d2004-06-10 10:50:08 +00001357int sqlite3_collation_needed(
1358 sqlite3 *db,
1359 void *pCollNeededArg,
1360 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
1361){
drhc60d0442004-09-30 13:43:13 +00001362 if( sqlite3SafetyCheck(db) ){
1363 return SQLITE_MISUSE;
1364 }
danielk19777cedc8d2004-06-10 10:50:08 +00001365 db->xCollNeeded = xCollNeeded;
1366 db->xCollNeeded16 = 0;
1367 db->pCollNeededArg = pCollNeededArg;
1368 return SQLITE_OK;
1369}
danielk1977d8123362004-06-12 09:25:12 +00001370
drh6c626082004-11-14 21:56:29 +00001371#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00001372/*
1373** Register a collation sequence factory callback with the database handle
1374** db. Replace any previously installed collation sequence factory.
1375*/
danielk19777cedc8d2004-06-10 10:50:08 +00001376int sqlite3_collation_needed16(
1377 sqlite3 *db,
1378 void *pCollNeededArg,
1379 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
1380){
drhc60d0442004-09-30 13:43:13 +00001381 if( sqlite3SafetyCheck(db) ){
1382 return SQLITE_MISUSE;
1383 }
danielk19777cedc8d2004-06-10 10:50:08 +00001384 db->xCollNeeded = 0;
1385 db->xCollNeeded16 = xCollNeeded16;
1386 db->pCollNeededArg = pCollNeededArg;
1387 return SQLITE_OK;
1388}
drh6c626082004-11-14 21:56:29 +00001389#endif /* SQLITE_OMIT_UTF16 */