blob: 6f4a2c07bfbb5cfe0e358a71179b8efcfea747c9 [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**
danielk1977de0fe3e2006-01-06 06:33:12 +000017** $Id: main.c,v 1.316 2006/01/06 06:33:13 danielk1977 Exp $
drh75897232000-05-29 14:26:00 +000018*/
19#include "sqliteInt.h"
drh8cfbf082001-09-19 13:22:39 +000020#include "os.h"
drhce9079c2002-05-15 14:17:44 +000021#include <ctype.h>
drh75897232000-05-29 14:26:00 +000022
23/*
drh9c054832004-05-31 18:51:57 +000024** The following constant value is used by the SQLITE_BIGENDIAN and
25** SQLITE_LITTLEENDIAN macros.
drhbbd42a62004-05-22 17:41:58 +000026*/
27const int sqlite3one = 1;
28
drhc2311722002-07-19 17:46:38 +000029/*
drhb217a572000-08-22 13:40:18 +000030** The version of the library
31*/
drh38f82712004-06-18 17:10:16 +000032const char rcsid3[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $";
danielk197724b03fd2004-05-10 10:34:34 +000033const char sqlite3_version[] = SQLITE_VERSION;
drh4aec8b62004-08-28 16:19:00 +000034const char *sqlite3_libversion(void){ return sqlite3_version; }
danielk197799ba19e2005-02-05 07:33:34 +000035int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
drhb217a572000-08-22 13:40:18 +000036
37/*
drhd3d39e92004-05-20 22:16:29 +000038** This is the default collating function named "BINARY" which is always
39** available.
40*/
danielk19772c336542005-01-13 02:14:23 +000041static int binCollFunc(
drhd3d39e92004-05-20 22:16:29 +000042 void *NotUsed,
43 int nKey1, const void *pKey1,
44 int nKey2, const void *pKey2
45){
46 int rc, n;
47 n = nKey1<nKey2 ? nKey1 : nKey2;
48 rc = memcmp(pKey1, pKey2, n);
49 if( rc==0 ){
50 rc = nKey1 - nKey2;
51 }
52 return rc;
53}
54
55/*
danielk1977dc1bdc42004-06-11 10:51:27 +000056** Another built-in collating sequence: NOCASE.
57**
58** This collating sequence is intended to be used for "case independant
59** comparison". SQLite's knowledge of upper and lower case equivalents
60** extends only to the 26 characters used in the English language.
61**
62** At the moment there is only a UTF-8 implementation.
danielk19770202b292004-06-09 09:55:16 +000063*/
64static int nocaseCollatingFunc(
65 void *NotUsed,
66 int nKey1, const void *pKey1,
67 int nKey2, const void *pKey2
68){
69 int r = sqlite3StrNICmp(
drh208f80a2004-08-29 20:08:58 +000070 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
danielk19770202b292004-06-09 09:55:16 +000071 if( 0==r ){
72 r = nKey1-nKey2;
73 }
74 return r;
75}
76
77/*
drhaf9ff332002-01-16 21:00:27 +000078** Return the ROWID of the most recent insert
79*/
drh9bb575f2004-09-06 17:24:11 +000080sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
drhaf9ff332002-01-16 21:00:27 +000081 return db->lastRowid;
82}
83
84/*
danielk197724b03fd2004-05-10 10:34:34 +000085** Return the number of changes in the most recent call to sqlite3_exec().
drhc8d30ac2002-04-12 10:08:59 +000086*/
drh9bb575f2004-09-06 17:24:11 +000087int sqlite3_changes(sqlite3 *db){
drhc8d30ac2002-04-12 10:08:59 +000088 return db->nChange;
89}
90
rdcf146a772004-02-25 22:51:06 +000091/*
danielk1977b28af712004-06-21 06:50:26 +000092** Return the number of changes since the database handle was opened.
rdcf146a772004-02-25 22:51:06 +000093*/
danielk1977b28af712004-06-21 06:50:26 +000094int sqlite3_total_changes(sqlite3 *db){
95 return db->nTotalChange;
rdcb0c374f2004-02-20 22:53:38 +000096}
97
drhc8d30ac2002-04-12 10:08:59 +000098/*
danielk1977de0fe3e2006-01-06 06:33:12 +000099** Free all resources held by the schema structure. The void* argument points
100** at a DbSchema struct. This function does not call sqliteFree() on the
101** pointer itself, it just cleans up subsiduary resources (i.e. the contents
102** of the schema hash tables).
danielk1977da184232006-01-05 11:34:32 +0000103*/
104void sqlite3SchemaFree(void *p){
danielk1977de0fe3e2006-01-06 06:33:12 +0000105 Hash temp1;
106 Hash temp2;
107 HashElem *pElem;
108 DbSchema *pSchema = (DbSchema *)p;
109
110 temp1 = pSchema->tblHash;
111 temp2 = pSchema->trigHash;
112 sqlite3HashInit(&pSchema->trigHash, SQLITE_HASH_STRING, 0);
113 sqlite3HashClear(&pSchema->aFKey);
114 sqlite3HashClear(&pSchema->idxHash);
115 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
116 sqlite3DeleteTrigger((Trigger*)sqliteHashData(pElem));
117 }
118 sqlite3HashClear(&temp2);
119 sqlite3HashInit(&pSchema->tblHash, SQLITE_HASH_STRING, 0);
120 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
121 Table *pTab = sqliteHashData(pElem);
122 sqlite3DeleteTable(0, pTab);
123 }
124 sqlite3HashClear(&temp1);
125 pSchema->pSeqTab = 0;
126 pSchema->flags &= ~DB_SchemaLoaded;
danielk1977da184232006-01-05 11:34:32 +0000127}
128
129DbSchema *sqlite3SchemaGet(Btree *pBt){
130 DbSchema * p;
131 if( pBt ){
132 p = (DbSchema *)sqlite3BtreeSchema(pBt,sizeof(DbSchema),sqlite3SchemaFree);
133 }else{
134 p = (DbSchema *)sqliteMalloc(sizeof(DbSchema));
135 }
danielk1977de0fe3e2006-01-06 06:33:12 +0000136 if( p && 0==p->file_format ){
danielk1977da184232006-01-05 11:34:32 +0000137 sqlite3HashInit(&p->tblHash, SQLITE_HASH_STRING, 0);
138 sqlite3HashInit(&p->idxHash, SQLITE_HASH_STRING, 0);
139 sqlite3HashInit(&p->trigHash, SQLITE_HASH_STRING, 0);
140 sqlite3HashInit(&p->aFKey, SQLITE_HASH_STRING, 1);
141 }
142 return p;
143}
144
145int sqlite3SchemaToIndex(sqlite3 *db, DbSchema *pSchema){
146 int i = -1000000;
147
148 /* If pSchema is NULL, then return -1000000. This happens when code in
149 ** expr.c is trying to resolve a reference to a transient table (i.e. one
150 ** created by a sub-select). In this case the return value of this
151 ** function should never be used.
152 **
153 ** We return -1000000 instead of the more usual -1 simply because using
154 ** -1000000 as incorrectly using -1000000 index into db->aDb[] is much
155 ** more likely to cause a segfault than -1 (of course there are assert()
156 ** statements too, but it never hurts to play the odds).
157 */
158 if( pSchema ){
159 for(i=0; i<db->nDb; i++){
160 if( db->aDb[i].pSchema==pSchema ){
161 break;
162 }
163 }
164 assert( i>=0 &&i>=0 && i<db->nDb );
165 }
166 return i;
167}
168
169/*
drh50e5dad2001-09-15 00:57:28 +0000170** Close an existing SQLite database
171*/
drh9bb575f2004-09-06 17:24:11 +0000172int sqlite3_close(sqlite3 *db){
drh8e0a2f92002-02-23 23:45:45 +0000173 HashElem *i;
drh001bbcb2003-03-19 03:14:00 +0000174 int j;
danielk19775c4c7782004-06-16 10:39:23 +0000175
176 if( !db ){
danielk197796d81f92004-06-19 03:33:57 +0000177 return SQLITE_OK;
danielk19775c4c7782004-06-16 10:39:23 +0000178 }
drhc60d0442004-09-30 13:43:13 +0000179 if( sqlite3SafetyCheck(db) ){
danielk1977e35ee192004-06-26 09:50:11 +0000180 return SQLITE_MISUSE;
181 }
182
danielk19771f723bd2005-05-26 12:37:29 +0000183#ifdef SQLITE_SSE
184 sqlite3_finalize(db->pFetch);
185#endif
186
danielk197796d81f92004-06-19 03:33:57 +0000187 /* If there are any outstanding VMs, return SQLITE_BUSY. */
188 if( db->pVdbe ){
189 sqlite3Error(db, SQLITE_BUSY,
190 "Unable to close due to unfinalised statements");
191 return SQLITE_BUSY;
192 }
193 assert( !sqlite3SafetyCheck(db) );
danielk1977e0048402004-06-15 16:51:01 +0000194
195 /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database
196 ** cannot be opened for some reason. So this routine needs to run in
197 ** that case. But maybe there should be an extra magic value for the
198 ** "failed to open" state.
199 */
danielk197796d81f92004-06-19 03:33:57 +0000200 if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){
drh94e92032003-02-16 22:21:32 +0000201 /* printf("DID NOT CLOSE\n"); fflush(stdout); */
danielk197796d81f92004-06-19 03:33:57 +0000202 return SQLITE_ERROR;
drh94e92032003-02-16 22:21:32 +0000203 }
danielk1977e0048402004-06-15 16:51:01 +0000204
danielk19777ddad962005-12-12 06:53:03 +0000205 /* sqlite3_close() may not invoke sqliteMalloc(). */
206 sqlite3MallocDisallow();
207
drh001bbcb2003-03-19 03:14:00 +0000208 for(j=0; j<db->nDb; j++){
drh4d189ca2004-02-12 18:46:38 +0000209 struct Db *pDb = &db->aDb[j];
210 if( pDb->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000211 sqlite3BtreeClose(pDb->pBt);
drh4d189ca2004-02-12 18:46:38 +0000212 pDb->pBt = 0;
drh113088e2003-03-20 01:16:58 +0000213 }
drhf57b3392001-10-08 13:22:32 +0000214 }
danielk19774adee202004-05-08 08:23:19 +0000215 sqlite3ResetInternalSchema(db, 0);
drh1c2d8412003-03-31 00:30:47 +0000216 assert( db->nDb<=2 );
217 assert( db->aDb==db->aDbStatic );
drh0bce8352002-02-28 00:41:10 +0000218 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
219 FuncDef *pFunc, *pNext;
220 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
drh8e0a2f92002-02-23 23:45:45 +0000221 pNext = pFunc->pNext;
222 sqliteFree(pFunc);
223 }
224 }
danielk1977466be562004-06-10 02:16:01 +0000225
danielk1977d8123362004-06-12 09:25:12 +0000226 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
danielk1977466be562004-06-10 02:16:01 +0000227 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
danielk1977d8123362004-06-12 09:25:12 +0000228 sqliteFree(pColl);
danielk1977466be562004-06-10 02:16:01 +0000229 }
danielk1977d8123362004-06-12 09:25:12 +0000230 sqlite3HashClear(&db->aCollSeq);
danielk1977466be562004-06-10 02:16:01 +0000231
danielk19774adee202004-05-08 08:23:19 +0000232 sqlite3HashClear(&db->aFunc);
danielk19776622cce2004-05-20 11:00:52 +0000233 sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
danielk1977bfd6cce2004-06-18 04:24:54 +0000234 if( db->pErr ){
235 sqlite3ValueFree(db->pErr);
236 }
danielk197796d81f92004-06-19 03:33:57 +0000237
238 db->magic = SQLITE_MAGIC_ERROR;
danielk1977da184232006-01-05 11:34:32 +0000239 sqliteFree(db->aDb[1].pSchema);
drh75897232000-05-29 14:26:00 +0000240 sqliteFree(db);
danielk19777ddad962005-12-12 06:53:03 +0000241 sqlite3MallocAllow();
danielk197796d81f92004-06-19 03:33:57 +0000242 return SQLITE_OK;
drh75897232000-05-29 14:26:00 +0000243}
244
245/*
drh001bbcb2003-03-19 03:14:00 +0000246** Rollback all database files.
247*/
drh9bb575f2004-09-06 17:24:11 +0000248void sqlite3RollbackAll(sqlite3 *db){
drh001bbcb2003-03-19 03:14:00 +0000249 int i;
danielk1977f3f06bb2005-12-16 15:24:28 +0000250 int inTrans = 0;
drh001bbcb2003-03-19 03:14:00 +0000251 for(i=0; i<db->nDb; i++){
252 if( db->aDb[i].pBt ){
danielk1977f3f06bb2005-12-16 15:24:28 +0000253 if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){
254 inTrans = 1;
255 }
danielk19774adee202004-05-08 08:23:19 +0000256 sqlite3BtreeRollback(db->aDb[i].pBt);
drh001bbcb2003-03-19 03:14:00 +0000257 db->aDb[i].inTrans = 0;
258 }
259 }
danielk197771fd80b2005-12-16 06:54:01 +0000260 if( db->flags&SQLITE_InternChanges ){
261 sqlite3ResetInternalSchema(db, 0);
262 }
263
264 /* If one has been configured, invoke the rollback-hook callback */
danielk1977f3f06bb2005-12-16 15:24:28 +0000265 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
danielk197771fd80b2005-12-16 06:54:01 +0000266 db->xRollbackCallback(db->pRollbackArg);
267 }
drh001bbcb2003-03-19 03:14:00 +0000268}
269
270/*
drhc22bd472002-05-10 13:14:07 +0000271** Return a static string that describes the kind of error specified in the
272** argument.
drh247be432002-05-10 05:44:55 +0000273*/
danielk1977f20b21c2004-05-31 23:56:42 +0000274const char *sqlite3ErrStr(int rc){
drhc22bd472002-05-10 13:14:07 +0000275 const char *z;
276 switch( rc ){
drhc60d0442004-09-30 13:43:13 +0000277 case SQLITE_ROW:
278 case SQLITE_DONE:
drhc22bd472002-05-10 13:14:07 +0000279 case SQLITE_OK: z = "not an error"; break;
280 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
drhc22bd472002-05-10 13:14:07 +0000281 case SQLITE_PERM: z = "access permission denied"; break;
282 case SQLITE_ABORT: z = "callback requested query abort"; break;
283 case SQLITE_BUSY: z = "database is locked"; break;
284 case SQLITE_LOCKED: z = "database table is locked"; break;
285 case SQLITE_NOMEM: z = "out of memory"; break;
286 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
287 case SQLITE_INTERRUPT: z = "interrupted"; break;
288 case SQLITE_IOERR: z = "disk I/O error"; break;
289 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
drh2db0bbc2005-08-11 02:10:18 +0000290 case SQLITE_FULL: z = "database or disk is full"; break;
drhc22bd472002-05-10 13:14:07 +0000291 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
292 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
293 case SQLITE_EMPTY: z = "table contains no data"; break;
294 case SQLITE_SCHEMA: z = "database schema has changed"; break;
drhc22bd472002-05-10 13:14:07 +0000295 case SQLITE_CONSTRAINT: z = "constraint failed"; break;
296 case SQLITE_MISMATCH: z = "datatype mismatch"; break;
297 case SQLITE_MISUSE: z = "library routine called out of sequence";break;
drh8766c342002-11-09 00:33:15 +0000298 case SQLITE_NOLFS: z = "kernel lacks large file support"; break;
drhed6c8672003-01-12 18:02:16 +0000299 case SQLITE_AUTH: z = "authorization denied"; break;
jplyon892f6712003-06-12 08:59:00 +0000300 case SQLITE_FORMAT: z = "auxiliary database format error"; break;
drhb08153d2004-11-20 20:18:55 +0000301 case SQLITE_RANGE: z = "bind or column index out of range"; break;
drhc602f9a2004-02-12 19:01:04 +0000302 case SQLITE_NOTADB: z = "file is encrypted or is not a database";break;
drhc22bd472002-05-10 13:14:07 +0000303 default: z = "unknown error"; break;
drh247be432002-05-10 05:44:55 +0000304 }
drhc22bd472002-05-10 13:14:07 +0000305 return z;
drh247be432002-05-10 05:44:55 +0000306}
307
308/*
drh2dfbbca2000-07-28 14:32:48 +0000309** This routine implements a busy callback that sleeps and tries
310** again until a timeout value is reached. The timeout value is
311** an integer number of milliseconds passed in as the first
312** argument.
313*/
drhdaffd0e2001-04-11 14:28:42 +0000314static int sqliteDefaultBusyCallback(
drh97903fe2005-05-24 20:19:57 +0000315 void *ptr, /* Database connection */
drh2dfbbca2000-07-28 14:32:48 +0000316 int count /* Number of times table has been busy */
317){
drh8cfbf082001-09-19 13:22:39 +0000318#if SQLITE_MIN_SLEEP_MS==1
drhee570fa2005-04-28 12:06:05 +0000319 static const u8 delays[] =
320 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
321 static const u8 totals[] =
322 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
drhd1bec472004-01-15 13:29:31 +0000323# define NDELAY (sizeof(delays)/sizeof(delays[0]))
drh97903fe2005-05-24 20:19:57 +0000324 int timeout = ((sqlite3 *)ptr)->busyTimeout;
325 int delay, prior;
drh2dfbbca2000-07-28 14:32:48 +0000326
drhee570fa2005-04-28 12:06:05 +0000327 assert( count>=0 );
328 if( count < NDELAY ){
329 delay = delays[count];
330 prior = totals[count];
drhd1bec472004-01-15 13:29:31 +0000331 }else{
332 delay = delays[NDELAY-1];
drh68cb6192005-05-06 22:05:56 +0000333 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
drh2dfbbca2000-07-28 14:32:48 +0000334 }
drhd1bec472004-01-15 13:29:31 +0000335 if( prior + delay > timeout ){
336 delay = timeout - prior;
drh2dfbbca2000-07-28 14:32:48 +0000337 if( delay<=0 ) return 0;
338 }
drh054889e2005-11-30 03:20:31 +0000339 sqlite3Os.xSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000340 return 1;
341#else
drh3f737082005-06-14 02:24:31 +0000342 int timeout = ((sqlite3 *)ptr)->busyTimeout;
drh2dfbbca2000-07-28 14:32:48 +0000343 if( (count+1)*1000 > timeout ){
344 return 0;
345 }
drh054889e2005-11-30 03:20:31 +0000346 sqlite3Os.xSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000347 return 1;
348#endif
349}
350
351/*
drha4afb652005-07-09 02:16:02 +0000352** Invoke the given busy handler.
353**
354** This routine is called when an operation failed with a lock.
355** If this routine returns non-zero, the lock is retried. If it
356** returns 0, the operation aborts with an SQLITE_BUSY error.
357*/
358int sqlite3InvokeBusyHandler(BusyHandler *p){
359 int rc;
360 if( p==0 || p->xFunc==0 || p->nBusy<0 ) return 0;
361 rc = p->xFunc(p->pArg, p->nBusy);
362 if( rc==0 ){
363 p->nBusy = -1;
364 }else{
365 p->nBusy++;
366 }
367 return rc;
368}
369
370/*
drh2dfbbca2000-07-28 14:32:48 +0000371** This routine sets the busy callback for an Sqlite database to the
372** given callback function with the given argument.
373*/
danielk1977f9d64d22004-06-19 08:18:07 +0000374int sqlite3_busy_handler(
375 sqlite3 *db,
danielk19772a764eb2004-06-12 01:43:26 +0000376 int (*xBusy)(void*,int),
drh2dfbbca2000-07-28 14:32:48 +0000377 void *pArg
378){
drhc60d0442004-09-30 13:43:13 +0000379 if( sqlite3SafetyCheck(db) ){
380 return SQLITE_MISUSE;
381 }
danielk197724162fe2004-06-04 06:22:00 +0000382 db->busyHandler.xFunc = xBusy;
383 db->busyHandler.pArg = pArg;
drha4afb652005-07-09 02:16:02 +0000384 db->busyHandler.nBusy = 0;
danielk1977f9d64d22004-06-19 08:18:07 +0000385 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000386}
387
danielk1977348bb5d2003-10-18 09:37:26 +0000388#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
389/*
390** This routine sets the progress callback for an Sqlite database to the
391** given callback function with the given argument. The progress callback will
392** be invoked every nOps opcodes.
393*/
danielk197724b03fd2004-05-10 10:34:34 +0000394void sqlite3_progress_handler(
drh9bb575f2004-09-06 17:24:11 +0000395 sqlite3 *db,
danielk1977348bb5d2003-10-18 09:37:26 +0000396 int nOps,
397 int (*xProgress)(void*),
398 void *pArg
399){
drhc60d0442004-09-30 13:43:13 +0000400 if( !sqlite3SafetyCheck(db) ){
401 if( nOps>0 ){
402 db->xProgress = xProgress;
403 db->nProgressOps = nOps;
404 db->pProgressArg = pArg;
405 }else{
406 db->xProgress = 0;
407 db->nProgressOps = 0;
408 db->pProgressArg = 0;
409 }
danielk1977348bb5d2003-10-18 09:37:26 +0000410 }
411}
412#endif
413
414
drh2dfbbca2000-07-28 14:32:48 +0000415/*
416** This routine installs a default busy handler that waits for the
417** specified number of milliseconds before returning 0.
418*/
danielk1977f9d64d22004-06-19 08:18:07 +0000419int sqlite3_busy_timeout(sqlite3 *db, int ms){
drh2dfbbca2000-07-28 14:32:48 +0000420 if( ms>0 ){
drh97903fe2005-05-24 20:19:57 +0000421 db->busyTimeout = ms;
422 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
drh2dfbbca2000-07-28 14:32:48 +0000423 }else{
danielk197724b03fd2004-05-10 10:34:34 +0000424 sqlite3_busy_handler(db, 0, 0);
drh2dfbbca2000-07-28 14:32:48 +0000425 }
danielk1977f9d64d22004-06-19 08:18:07 +0000426 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000427}
drh4c504392000-10-16 22:06:40 +0000428
429/*
430** Cause any pending operation to stop at its earliest opportunity.
431*/
drh9bb575f2004-09-06 17:24:11 +0000432void sqlite3_interrupt(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000433 if( !sqlite3SafetyCheck(db) ){
434 db->flags |= SQLITE_Interrupt;
435 }
drh4c504392000-10-16 22:06:40 +0000436}
drhfa86c412002-02-02 15:01:15 +0000437
438/*
439** Windows systems should call this routine to free memory that
danielk197724b03fd2004-05-10 10:34:34 +0000440** is returned in the in the errmsg parameter of sqlite3_open() when
drhfa86c412002-02-02 15:01:15 +0000441** SQLite is a DLL. For some reason, it does not work to call free()
442** directly.
443**
drhae29ffb2004-09-25 14:39:18 +0000444** Note that we need to call free() not sqliteFree() here.
drhfa86c412002-02-02 15:01:15 +0000445*/
drh3f4fedb2004-05-31 19:34:33 +0000446void sqlite3_free(char *p){ free(p); }
drhfa86c412002-02-02 15:01:15 +0000447
448/*
drhdf014892004-06-02 00:41:09 +0000449** Create new user functions.
drhfa86c412002-02-02 15:01:15 +0000450*/
danielk197724b03fd2004-05-10 10:34:34 +0000451int sqlite3_create_function(
danielk197765904932004-05-26 06:18:37 +0000452 sqlite3 *db,
453 const char *zFunctionName,
454 int nArg,
danielk1977d8123362004-06-12 09:25:12 +0000455 int enc,
danielk197765904932004-05-26 06:18:37 +0000456 void *pUserData,
457 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
458 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
459 void (*xFinal)(sqlite3_context*)
drh8e0a2f92002-02-23 23:45:45 +0000460){
drh0bce8352002-02-28 00:41:10 +0000461 FuncDef *p;
drh4b59ab52002-08-24 18:24:51 +0000462 int nName;
danielk197765904932004-05-26 06:18:37 +0000463
drhc60d0442004-09-30 13:43:13 +0000464 if( sqlite3SafetyCheck(db) ){
465 return SQLITE_MISUSE;
466 }
467 if( zFunctionName==0 ||
danielk1977398eae72004-05-26 06:58:43 +0000468 (xFunc && (xFinal || xStep)) ||
469 (!xFunc && (xFinal && !xStep)) ||
470 (!xFunc && (!xFinal && xStep)) ||
471 (nArg<-1 || nArg>127) ||
472 (255<(nName = strlen(zFunctionName))) ){
danielk197765904932004-05-26 06:18:37 +0000473 return SQLITE_ERROR;
474 }
danielk1977d8123362004-06-12 09:25:12 +0000475
danielk197793758c82005-01-21 08:13:14 +0000476#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +0000477 /* If SQLITE_UTF16 is specified as the encoding type, transform this
478 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
479 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
480 **
481 ** If SQLITE_ANY is specified, add three versions of the function
482 ** to the hash table.
483 */
484 if( enc==SQLITE_UTF16 ){
485 enc = SQLITE_UTF16NATIVE;
486 }else if( enc==SQLITE_ANY ){
487 int rc;
488 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF8,
danielk1977f9d64d22004-06-19 08:18:07 +0000489 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000490 if( rc!=SQLITE_OK ) return rc;
491 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF16LE,
danielk1977f9d64d22004-06-19 08:18:07 +0000492 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000493 if( rc!=SQLITE_OK ) return rc;
494 enc = SQLITE_UTF16BE;
495 }
danielk197793758c82005-01-21 08:13:14 +0000496#else
497 enc = SQLITE_UTF8;
498#endif
danielk19779636c4e2005-01-25 04:27:54 +0000499
500 /* Check if an existing function is being overridden or deleted. If so,
501 ** and there are active VMs, then return SQLITE_BUSY. If a function
502 ** is being overridden/deleted but there are no active VMs, allow the
503 ** operation to continue but invalidate all precompiled statements.
504 */
505 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0);
506 if( p && p->iPrefEnc==enc && p->nArg==nArg ){
507 if( db->activeVdbeCnt ){
508 sqlite3Error(db, SQLITE_BUSY,
509 "Unable to delete/modify user-function due to active statements");
510 return SQLITE_BUSY;
511 }else{
512 sqlite3ExpirePreparedStatements(db);
513 }
514 }
danielk197765904932004-05-26 06:18:37 +0000515
danielk1977d8123362004-06-12 09:25:12 +0000516 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);
danielk197713073932004-06-30 11:54:06 +0000517 if( p==0 ) return SQLITE_NOMEM;
drh55ef4d92005-08-14 01:20:37 +0000518 p->flags = 0;
drh8e0a2f92002-02-23 23:45:45 +0000519 p->xFunc = xFunc;
drh8e0a2f92002-02-23 23:45:45 +0000520 p->xStep = xStep;
danielk197765904932004-05-26 06:18:37 +0000521 p->xFinalize = xFinal;
drh1350b032002-02-27 19:00:20 +0000522 p->pUserData = pUserData;
danielk197765904932004-05-26 06:18:37 +0000523 return SQLITE_OK;
524}
danielk197793758c82005-01-21 08:13:14 +0000525#ifndef SQLITE_OMIT_UTF16
danielk197765904932004-05-26 06:18:37 +0000526int sqlite3_create_function16(
527 sqlite3 *db,
528 const void *zFunctionName,
529 int nArg,
530 int eTextRep,
danielk197765904932004-05-26 06:18:37 +0000531 void *pUserData,
532 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
533 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
534 void (*xFinal)(sqlite3_context*)
535){
536 int rc;
drhaf9a7c22005-12-15 03:04:10 +0000537 char *zFunc8;
danielk1977bfd6cce2004-06-18 04:24:54 +0000538
drhc60d0442004-09-30 13:43:13 +0000539 if( sqlite3SafetyCheck(db) ){
540 return SQLITE_MISUSE;
541 }
drhaf9a7c22005-12-15 03:04:10 +0000542 zFunc8 = sqlite3utf16to8(zFunctionName, -1);
danielk1977bfd6cce2004-06-18 04:24:54 +0000543 if( !zFunc8 ){
danielk197765904932004-05-26 06:18:37 +0000544 return SQLITE_NOMEM;
545 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000546 rc = sqlite3_create_function(db, zFunc8, nArg, eTextRep,
danielk1977f9d64d22004-06-19 08:18:07 +0000547 pUserData, xFunc, xStep, xFinal);
drhaf9a7c22005-12-15 03:04:10 +0000548 sqliteFree(zFunc8);
danielk197765904932004-05-26 06:18:37 +0000549 return rc;
drh8e0a2f92002-02-23 23:45:45 +0000550}
danielk197793758c82005-01-21 08:13:14 +0000551#endif
drhc9b84a12002-06-20 11:36:48 +0000552
drh19e2d372005-08-29 23:00:03 +0000553#ifndef SQLITE_OMIT_TRACE
drhc9b84a12002-06-20 11:36:48 +0000554/*
drh18de4822003-01-16 16:28:53 +0000555** Register a trace function. The pArg from the previously registered trace
556** is returned.
557**
558** A NULL trace function means that no tracing is executes. A non-NULL
559** trace is a pointer to a function that is invoked at the start of each
drh19e2d372005-08-29 23:00:03 +0000560** SQL statement.
drh18de4822003-01-16 16:28:53 +0000561*/
drh9bb575f2004-09-06 17:24:11 +0000562void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
drh18de4822003-01-16 16:28:53 +0000563 void *pOld = db->pTraceArg;
564 db->xTrace = xTrace;
565 db->pTraceArg = pArg;
566 return pOld;
drh0d1a6432003-04-03 15:46:04 +0000567}
drh19e2d372005-08-29 23:00:03 +0000568/*
569** Register a profile function. The pArg from the previously registered
570** profile function is returned.
571**
572** A NULL profile function means that no profiling is executes. A non-NULL
573** profile is a pointer to a function that is invoked at the conclusion of
574** each SQL statement that is run.
575*/
576void *sqlite3_profile(
577 sqlite3 *db,
578 void (*xProfile)(void*,const char*,sqlite_uint64),
579 void *pArg
580){
581 void *pOld = db->pProfileArg;
582 db->xProfile = xProfile;
583 db->pProfileArg = pArg;
584 return pOld;
585}
586#endif /* SQLITE_OMIT_TRACE */
paulb0208cc2003-04-13 18:26:49 +0000587
drhaa940ea2004-01-15 02:44:03 +0000588/*** EXPERIMENTAL ***
589**
590** Register a function to be invoked when a transaction comments.
danielk197771fd80b2005-12-16 06:54:01 +0000591** If the invoked function returns non-zero, then the commit becomes a
drhaa940ea2004-01-15 02:44:03 +0000592** rollback.
593*/
danielk197724b03fd2004-05-10 10:34:34 +0000594void *sqlite3_commit_hook(
drh9bb575f2004-09-06 17:24:11 +0000595 sqlite3 *db, /* Attach the hook to this database */
drhaa940ea2004-01-15 02:44:03 +0000596 int (*xCallback)(void*), /* Function to invoke on each commit */
597 void *pArg /* Argument to the function */
598){
599 void *pOld = db->pCommitArg;
600 db->xCommitCallback = xCallback;
601 db->pCommitArg = pArg;
602 return pOld;
603}
604
danielk197794eb6a12005-12-15 15:22:08 +0000605/*
606** Register a callback to be invoked each time a row is updated,
607** inserted or deleted using this database connection.
608*/
danielk197771fd80b2005-12-16 06:54:01 +0000609void *sqlite3_update_hook(
danielk197794eb6a12005-12-15 15:22:08 +0000610 sqlite3 *db, /* Attach the hook to this database */
611 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
612 void *pArg /* Argument to the function */
613){
danielk197771fd80b2005-12-16 06:54:01 +0000614 void *pRet = db->pUpdateArg;
danielk197794eb6a12005-12-15 15:22:08 +0000615 db->xUpdateCallback = xCallback;
616 db->pUpdateArg = pArg;
danielk197771fd80b2005-12-16 06:54:01 +0000617 return pRet;
danielk197794eb6a12005-12-15 15:22:08 +0000618}
619
danielk197771fd80b2005-12-16 06:54:01 +0000620/*
621** Register a callback to be invoked each time a transaction is rolled
622** back by this database connection.
623*/
624void *sqlite3_rollback_hook(
625 sqlite3 *db, /* Attach the hook to this database */
626 void (*xCallback)(void*), /* Callback function */
627 void *pArg /* Argument to the function */
628){
629 void *pRet = db->pRollbackArg;
630 db->xRollbackCallback = xCallback;
631 db->pRollbackArg = pArg;
632 return pRet;
633}
drhaa940ea2004-01-15 02:44:03 +0000634
paulb0208cc2003-04-13 18:26:49 +0000635/*
drh13bff812003-04-15 01:19:47 +0000636** This routine is called to create a connection to a database BTree
637** driver. If zFilename is the name of a file, then that file is
638** opened and used. If zFilename is the magic name ":memory:" then
639** the database is stored in memory (and is thus forgotten as soon as
640** the connection is closed.) If zFilename is NULL then the database
drh84bfda42005-07-15 13:05:21 +0000641** is a "virtual" database for transient use only and is deleted as
642** soon as the connection is closed.
drh90f5ecb2004-07-22 01:19:35 +0000643**
drh84bfda42005-07-15 13:05:21 +0000644** A virtual database can be either a disk file (that is automatically
645** deleted when the file is closed) or it an be held entirely in memory,
drh90f5ecb2004-07-22 01:19:35 +0000646** depending on the values of the TEMP_STORE compile-time macro and the
647** db->temp_store variable, according to the following chart:
648**
649** TEMP_STORE db->temp_store Location of temporary database
650** ---------- -------------- ------------------------------
651** 0 any file
652** 1 1 file
653** 1 2 memory
654** 1 0 file
655** 2 1 file
656** 2 2 memory
657** 2 0 memory
658** 3 any memory
paulb0208cc2003-04-13 18:26:49 +0000659*/
danielk19774adee202004-05-08 08:23:19 +0000660int sqlite3BtreeFactory(
drh9bb575f2004-09-06 17:24:11 +0000661 const sqlite3 *db, /* Main database when opening aux otherwise 0 */
paulb0208cc2003-04-13 18:26:49 +0000662 const char *zFilename, /* Name of the file containing the BTree database */
663 int omitJournal, /* if TRUE then do not journal this file */
664 int nCache, /* How many pages in the page cache */
danielk19774adee202004-05-08 08:23:19 +0000665 Btree **ppBtree /* Pointer to new Btree object written here */
666){
danielk19774adee202004-05-08 08:23:19 +0000667 int btree_flags = 0;
drh90f5ecb2004-07-22 01:19:35 +0000668 int rc;
danielk19774adee202004-05-08 08:23:19 +0000669
drheec983e2004-05-08 10:11:36 +0000670 assert( ppBtree != 0);
danielk19774adee202004-05-08 08:23:19 +0000671 if( omitJournal ){
672 btree_flags |= BTREE_OMIT_JOURNAL;
paulb0208cc2003-04-13 18:26:49 +0000673 }
drh7bec5052005-02-06 02:45:41 +0000674 if( db->flags & SQLITE_NoReadlock ){
675 btree_flags |= BTREE_NO_READLOCK;
676 }
drh90f5ecb2004-07-22 01:19:35 +0000677 if( zFilename==0 ){
drh90f5ecb2004-07-22 01:19:35 +0000678#if TEMP_STORE==0
drh22ac46d2004-08-14 18:34:54 +0000679 /* Do nothing */
drh90f5ecb2004-07-22 01:19:35 +0000680#endif
danielk197703aded42004-11-22 05:26:27 +0000681#ifndef SQLITE_OMIT_MEMORYDB
drh90f5ecb2004-07-22 01:19:35 +0000682#if TEMP_STORE==1
drh22ac46d2004-08-14 18:34:54 +0000683 if( db->temp_store==2 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000684#endif
685#if TEMP_STORE==2
drh22ac46d2004-08-14 18:34:54 +0000686 if( db->temp_store!=1 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000687#endif
688#if TEMP_STORE==3
drh22ac46d2004-08-14 18:34:54 +0000689 zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000690#endif
danielk197703aded42004-11-22 05:26:27 +0000691#endif /* SQLITE_OMIT_MEMORYDB */
drh90f5ecb2004-07-22 01:19:35 +0000692 }
danielk19774adee202004-05-08 08:23:19 +0000693
danielk1977da184232006-01-05 11:34:32 +0000694 rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btree_flags);
drh90f5ecb2004-07-22 01:19:35 +0000695 if( rc==SQLITE_OK ){
696 sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler);
697 sqlite3BtreeSetCacheSize(*ppBtree, nCache);
698 }
699 return rc;
paulb0208cc2003-04-13 18:26:49 +0000700}
danielk19774adee202004-05-08 08:23:19 +0000701
danielk19774ad17132004-05-21 01:47:26 +0000702/*
703** Return UTF-8 encoded English language explanation of the most recent
704** error.
705*/
danielk19776622cce2004-05-20 11:00:52 +0000706const char *sqlite3_errmsg(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000707 const char *z;
danielk1977261919c2005-12-06 12:52:59 +0000708 if( sqlite3Tsd()->mallocFailed ){
danielk1977f20b21c2004-05-31 23:56:42 +0000709 return sqlite3ErrStr(SQLITE_NOMEM);
danielk19774ad17132004-05-21 01:47:26 +0000710 }
drhc60d0442004-09-30 13:43:13 +0000711 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
danielk1977e35ee192004-06-26 09:50:11 +0000712 return sqlite3ErrStr(SQLITE_MISUSE);
713 }
drh2646da72005-12-09 20:02:05 +0000714 z = (char*)sqlite3_value_text(db->pErr);
drhc60d0442004-09-30 13:43:13 +0000715 if( z==0 ){
716 z = sqlite3ErrStr(db->errCode);
danielk19776622cce2004-05-20 11:00:52 +0000717 }
drhc60d0442004-09-30 13:43:13 +0000718 return z;
danielk19776622cce2004-05-20 11:00:52 +0000719}
720
drh6c626082004-11-14 21:56:29 +0000721#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +0000722/*
723** Return UTF-16 encoded English language explanation of the most recent
724** error.
725*/
danielk19776622cce2004-05-20 11:00:52 +0000726const void *sqlite3_errmsg16(sqlite3 *db){
danielk1977bfd6cce2004-06-18 04:24:54 +0000727 /* Because all the characters in the string are in the unicode
728 ** range 0x00-0xFF, if we pad the big-endian string with a
729 ** zero byte, we can obtain the little-endian string with
730 ** &big_endian[1].
731 */
drh57196282004-10-06 15:41:16 +0000732 static const char outOfMemBe[] = {
danielk1977bfd6cce2004-06-18 04:24:54 +0000733 0, 'o', 0, 'u', 0, 't', 0, ' ',
734 0, 'o', 0, 'f', 0, ' ',
735 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0
736 };
drh57196282004-10-06 15:41:16 +0000737 static const char misuseBe [] = {
danielk1977e35ee192004-06-26 09:50:11 +0000738 0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ',
739 0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ',
740 0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ',
741 0, 'o', 0, 'u', 0, 't', 0, ' ',
742 0, 'o', 0, 'f', 0, ' ',
743 0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0
744 };
danielk19774ad17132004-05-21 01:47:26 +0000745
drhc60d0442004-09-30 13:43:13 +0000746 const void *z;
danielk1977261919c2005-12-06 12:52:59 +0000747 if( sqlite3Tsd()->mallocFailed ){
drhc60d0442004-09-30 13:43:13 +0000748 return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
749 }
750 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
751 return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
752 }
753 z = sqlite3_value_text16(db->pErr);
754 if( z==0 ){
755 sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
756 SQLITE_UTF8, SQLITE_STATIC);
757 z = sqlite3_value_text16(db->pErr);
758 }
759 return z;
danielk19776622cce2004-05-20 11:00:52 +0000760}
drh6c626082004-11-14 21:56:29 +0000761#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +0000762
drhc60d0442004-09-30 13:43:13 +0000763/*
danielk19777ddad962005-12-12 06:53:03 +0000764** Return the most recent error code generated by an SQLite routine. If NULL is
765** passed to this function, we assume a malloc() failed during sqlite3_open().
drhc60d0442004-09-30 13:43:13 +0000766*/
danielk19776622cce2004-05-20 11:00:52 +0000767int sqlite3_errcode(sqlite3 *db){
danielk19777ddad962005-12-12 06:53:03 +0000768 if( !db || sqlite3Tsd()->mallocFailed ){
drhc60d0442004-09-30 13:43:13 +0000769 return SQLITE_NOMEM;
770 }
771 if( sqlite3SafetyCheck(db) ){
772 return SQLITE_MISUSE;
773 }
danielk19776622cce2004-05-20 11:00:52 +0000774 return db->errCode;
775}
776
777/*
danielk19774ad17132004-05-21 01:47:26 +0000778** This routine does the work of opening a database on behalf of
779** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
drhee570fa2005-04-28 12:06:05 +0000780** is UTF-8 encoded.
danielk19774ad17132004-05-21 01:47:26 +0000781*/
782static int openDatabase(
783 const char *zFilename, /* Database filename UTF-8 encoded */
danielk19778e227872004-06-07 07:52:17 +0000784 sqlite3 **ppDb /* OUT: Returned database handle */
danielk19774ad17132004-05-21 01:47:26 +0000785){
786 sqlite3 *db;
danielk1977da184232006-01-05 11:34:32 +0000787 int rc;
drhd64fe2f2005-08-28 17:00:23 +0000788 CollSeq *pColl;
danielk19774ad17132004-05-21 01:47:26 +0000789
danielk19777ddad962005-12-12 06:53:03 +0000790 assert( !sqlite3Tsd()->mallocFailed );
791
danielk19774ad17132004-05-21 01:47:26 +0000792 /* Allocate the sqlite data structure */
drh9bb575f2004-09-06 17:24:11 +0000793 db = sqliteMalloc( sizeof(sqlite3) );
danielk19774ad17132004-05-21 01:47:26 +0000794 if( db==0 ) goto opendb_out;
danielk19774ad17132004-05-21 01:47:26 +0000795 db->priorNewRowid = 0;
796 db->magic = SQLITE_MAGIC_BUSY;
797 db->nDb = 2;
798 db->aDb = db->aDbStatic;
danielk1977dc8453f2004-06-12 00:42:34 +0000799 db->enc = SQLITE_UTF8;
danielk19771d850a72004-05-31 08:26:49 +0000800 db->autoCommit = 1;
drh47a6db22005-01-18 16:02:40 +0000801 db->flags |= SQLITE_ShortColNames;
drhf9b596e2004-05-26 16:54:42 +0000802 sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
danielk19774ad17132004-05-21 01:47:26 +0000803 sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
danielk1977da184232006-01-05 11:34:32 +0000804
805#if 0
danielk19774ad17132004-05-21 01:47:26 +0000806 for(i=0; i<db->nDb; i++){
807 sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
808 sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
809 sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);
810 sqlite3HashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1);
811 }
danielk1977da184232006-01-05 11:34:32 +0000812#endif
danielk19774ad17132004-05-21 01:47:26 +0000813
danielk19770202b292004-06-09 09:55:16 +0000814 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
815 ** and UTF-16, so add a version for each to avoid any unnecessary
816 ** conversions. The only error that can occur here is a malloc() failure.
817 */
danielk19772c336542005-01-13 02:14:23 +0000818 if( sqlite3_create_collation(db, "BINARY", SQLITE_UTF8, 0,binCollFunc) ||
819 sqlite3_create_collation(db, "BINARY", SQLITE_UTF16, 0,binCollFunc) ||
danielk19777ddad962005-12-12 06:53:03 +0000820 (db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0))==0
821 ){
822 /* sqlite3_create_collation() is an external API. So the mallocFailed flag
823 ** will have been cleared before returning. So set it explicitly here.
824 */
825 sqlite3Tsd()->mallocFailed = 1;
danielk19770202b292004-06-09 09:55:16 +0000826 db->magic = SQLITE_MAGIC_CLOSED;
827 goto opendb_out;
828 }
829
830 /* Also add a UTF-8 case-insensitive collation sequence. */
danielk1977466be562004-06-10 02:16:01 +0000831 sqlite3_create_collation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc);
danielk19770202b292004-06-09 09:55:16 +0000832
drhd64fe2f2005-08-28 17:00:23 +0000833 /* Set flags on the built-in collating sequences */
834 db->pDfltColl->type = SQLITE_COLL_BINARY;
835 pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "NOCASE", 6, 0);
836 if( pColl ){
837 pColl->type = SQLITE_COLL_NOCASE;
838 }
839
danielk19774ad17132004-05-21 01:47:26 +0000840 /* Open the backend database driver */
danielk19774ad17132004-05-21 01:47:26 +0000841 rc = sqlite3BtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt);
842 if( rc!=SQLITE_OK ){
danielk19774ad17132004-05-21 01:47:26 +0000843 sqlite3Error(db, rc, 0);
844 db->magic = SQLITE_MAGIC_CLOSED;
845 goto opendb_out;
846 }
danielk1977da184232006-01-05 11:34:32 +0000847 db->aDb[0].pSchema = sqlite3SchemaGet(db->aDb[0].pBt);
848 db->aDb[1].pSchema = sqlite3SchemaGet(0);
danielk19774ad17132004-05-21 01:47:26 +0000849
danielk197753c0f742005-03-29 03:10:59 +0000850 /* The default safety_level for the main database is 'full'; for the temp
851 ** database it is 'NONE'. This matches the pager layer defaults.
852 */
853 db->aDb[0].zName = "main";
danielk197791cf71b2004-06-26 06:37:06 +0000854 db->aDb[0].safety_level = 3;
danielk197753c0f742005-03-29 03:10:59 +0000855#ifndef SQLITE_OMIT_TEMPDB
856 db->aDb[1].zName = "temp";
danielk197791cf71b2004-06-26 06:37:06 +0000857 db->aDb[1].safety_level = 1;
danielk197753c0f742005-03-29 03:10:59 +0000858#endif
859
danielk19778e227872004-06-07 07:52:17 +0000860 /* Register all built-in functions, but do not attempt to read the
861 ** database schema yet. This is delayed until the first time the database
862 ** is accessed.
863 */
danielk19774ad17132004-05-21 01:47:26 +0000864 sqlite3RegisterBuiltinFunctions(db);
danielk19774397de52005-01-12 12:44:03 +0000865 sqlite3Error(db, SQLITE_OK, 0);
866 db->magic = SQLITE_MAGIC_OPEN;
danielk19774ad17132004-05-21 01:47:26 +0000867
868opendb_out:
danielk19777ddad962005-12-12 06:53:03 +0000869 if( SQLITE_NOMEM==(rc = sqlite3_errcode(db)) ){
870 sqlite3_close(db);
871 db = 0;
danielk197713073932004-06-30 11:54:06 +0000872 }
danielk19774ad17132004-05-21 01:47:26 +0000873 *ppDb = db;
danielk19777ddad962005-12-12 06:53:03 +0000874 sqlite3MallocClearFailed();
875 return rc;
danielk19774ad17132004-05-21 01:47:26 +0000876}
877
878/*
879** Open a new database handle.
880*/
danielk197780290862004-05-22 09:21:21 +0000881int sqlite3_open(
danielk19774ad17132004-05-21 01:47:26 +0000882 const char *zFilename,
danielk19774f057f92004-06-08 00:02:33 +0000883 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +0000884){
danielk19778e227872004-06-07 07:52:17 +0000885 return openDatabase(zFilename, ppDb);
danielk197783ab5a82004-05-21 11:39:05 +0000886}
887
drh6c626082004-11-14 21:56:29 +0000888#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +0000889/*
890** Open a new database handle.
891*/
892int sqlite3_open16(
893 const void *zFilename,
danielk19774f057f92004-06-08 00:02:33 +0000894 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +0000895){
danielk1977bfd6cce2004-06-18 04:24:54 +0000896 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
897 int rc = SQLITE_NOMEM;
898 sqlite3_value *pVal;
danielk19774ad17132004-05-21 01:47:26 +0000899
danielk19777ddad962005-12-12 06:53:03 +0000900 assert( zFilename );
danielk19774ad17132004-05-21 01:47:26 +0000901 assert( ppDb );
danielk1977bfd6cce2004-06-18 04:24:54 +0000902 *ppDb = 0;
903 pVal = sqlite3ValueNew();
904 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
905 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
906 if( zFilename8 ){
907 rc = openDatabase(zFilename8, ppDb);
908 if( rc==SQLITE_OK && *ppDb ){
drh7dd90a42005-12-06 13:19:07 +0000909 rc = sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
danielk1977bfd6cce2004-06-18 04:24:54 +0000910 }
danielk19777ddad962005-12-12 06:53:03 +0000911 }else{
912 assert( sqlite3Tsd()->mallocFailed );
913 sqlite3MallocClearFailed();
danielk19774ad17132004-05-21 01:47:26 +0000914 }
danielk19777ddad962005-12-12 06:53:03 +0000915 sqlite3ValueFree(pVal);
danielk19778e227872004-06-07 07:52:17 +0000916
danielk19774ad17132004-05-21 01:47:26 +0000917 return rc;
918}
drh6c626082004-11-14 21:56:29 +0000919#endif /* SQLITE_OMIT_UTF16 */
danielk19774ad17132004-05-21 01:47:26 +0000920
danielk1977106bb232004-05-21 10:08:53 +0000921/*
922** The following routine destroys a virtual machine that is created by
923** the sqlite3_compile() routine. The integer returned is an SQLITE_
924** success/failure code that describes the result of executing the virtual
925** machine.
926**
927** This routine sets the error code and string returned by
928** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
929*/
danielk1977fc57d7b2004-05-26 02:04:57 +0000930int sqlite3_finalize(sqlite3_stmt *pStmt){
drh1bcdb0c2004-08-28 14:49:46 +0000931 int rc;
932 if( pStmt==0 ){
933 rc = SQLITE_OK;
934 }else{
935 rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
936 }
937 return rc;
danielk1977106bb232004-05-21 10:08:53 +0000938}
939
940/*
941** Terminate the current execution of an SQL statement and reset it
942** back to its starting state so that it can be reused. A success code from
943** the prior execution is returned.
944**
945** This routine sets the error code and string returned by
946** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
947*/
danielk1977fc57d7b2004-05-26 02:04:57 +0000948int sqlite3_reset(sqlite3_stmt *pStmt){
drh1bcdb0c2004-08-28 14:49:46 +0000949 int rc;
950 if( pStmt==0 ){
951 rc = SQLITE_OK;
952 }else{
953 rc = sqlite3VdbeReset((Vdbe*)pStmt);
drh13449892005-09-07 21:22:45 +0000954 sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0, 0, 0);
drh1bcdb0c2004-08-28 14:49:46 +0000955 }
danielk1977106bb232004-05-21 10:08:53 +0000956 return rc;
957}
danielk19770202b292004-06-09 09:55:16 +0000958
danielk1977d8123362004-06-12 09:25:12 +0000959/*
960** Register a new collation sequence with the database handle db.
961*/
danielk19770202b292004-06-09 09:55:16 +0000962int sqlite3_create_collation(
963 sqlite3* db,
964 const char *zName,
danielk1977466be562004-06-10 02:16:01 +0000965 int enc,
danielk19770202b292004-06-09 09:55:16 +0000966 void* pCtx,
967 int(*xCompare)(void*,int,const void*,int,const void*)
968){
969 CollSeq *pColl;
970 int rc = SQLITE_OK;
danielk1977d8123362004-06-12 09:25:12 +0000971
drhc60d0442004-09-30 13:43:13 +0000972 if( sqlite3SafetyCheck(db) ){
973 return SQLITE_MISUSE;
974 }
975
danielk1977d8123362004-06-12 09:25:12 +0000976 /* If SQLITE_UTF16 is specified as the encoding type, transform this
977 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
978 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
979 */
980 if( enc==SQLITE_UTF16 ){
981 enc = SQLITE_UTF16NATIVE;
982 }
983
danielk1977466be562004-06-10 02:16:01 +0000984 if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16LE && enc!=SQLITE_UTF16BE ){
985 sqlite3Error(db, SQLITE_ERROR,
986 "Param 3 to sqlite3_create_collation() must be one of "
danielk1977d8123362004-06-12 09:25:12 +0000987 "SQLITE_UTF8, SQLITE_UTF16, SQLITE_UTF16LE or SQLITE_UTF16BE"
danielk1977466be562004-06-10 02:16:01 +0000988 );
989 return SQLITE_ERROR;
990 }
danielk1977a21c6b62005-01-24 10:25:59 +0000991
danielk19779636c4e2005-01-25 04:27:54 +0000992 /* Check if this call is removing or replacing an existing collation
993 ** sequence. If so, and there are active VMs, return busy. If there
994 ** are no active VMs, invalidate any pre-compiled statements.
danielk1977a21c6b62005-01-24 10:25:59 +0000995 */
danielk19779636c4e2005-01-25 04:27:54 +0000996 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 0);
997 if( pColl && pColl->xCmp ){
998 if( db->activeVdbeCnt ){
999 sqlite3Error(db, SQLITE_BUSY,
1000 "Unable to delete/modify collation sequence due to active statements");
1001 return SQLITE_BUSY;
1002 }
danielk1977a21c6b62005-01-24 10:25:59 +00001003 sqlite3ExpirePreparedStatements(db);
1004 }
1005
danielk1977466be562004-06-10 02:16:01 +00001006 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 1);
danielk19770202b292004-06-09 09:55:16 +00001007 if( 0==pColl ){
drhd64fe2f2005-08-28 17:00:23 +00001008 rc = SQLITE_NOMEM;
danielk19770202b292004-06-09 09:55:16 +00001009 }else{
1010 pColl->xCmp = xCompare;
1011 pColl->pUser = pCtx;
danielk1977312d6b32004-06-29 13:18:23 +00001012 pColl->enc = enc;
danielk19770202b292004-06-09 09:55:16 +00001013 }
1014 sqlite3Error(db, rc, 0);
danielk1977466be562004-06-10 02:16:01 +00001015 return rc;
danielk19770202b292004-06-09 09:55:16 +00001016}
1017
drh6c626082004-11-14 21:56:29 +00001018#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00001019/*
1020** Register a new collation sequence with the database handle db.
1021*/
danielk19770202b292004-06-09 09:55:16 +00001022int sqlite3_create_collation16(
1023 sqlite3* db,
1024 const char *zName,
danielk1977466be562004-06-10 02:16:01 +00001025 int enc,
danielk19770202b292004-06-09 09:55:16 +00001026 void* pCtx,
1027 int(*xCompare)(void*,int,const void*,int,const void*)
1028){
drhaf9a7c22005-12-15 03:04:10 +00001029 char *zName8;
1030 int rc;
drhc60d0442004-09-30 13:43:13 +00001031 if( sqlite3SafetyCheck(db) ){
1032 return SQLITE_MISUSE;
1033 }
drhaf9a7c22005-12-15 03:04:10 +00001034 zName8 = sqlite3utf16to8(zName, -1);
1035 rc = sqlite3_create_collation(db, zName8, enc, pCtx, xCompare);
1036 sqliteFree(zName8);
1037 return rc;
danielk19770202b292004-06-09 09:55:16 +00001038}
drh6c626082004-11-14 21:56:29 +00001039#endif /* SQLITE_OMIT_UTF16 */
danielk19777cedc8d2004-06-10 10:50:08 +00001040
danielk1977d8123362004-06-12 09:25:12 +00001041/*
1042** Register a collation sequence factory callback with the database handle
1043** db. Replace any previously installed collation sequence factory.
1044*/
danielk19777cedc8d2004-06-10 10:50:08 +00001045int sqlite3_collation_needed(
1046 sqlite3 *db,
1047 void *pCollNeededArg,
1048 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
1049){
drhc60d0442004-09-30 13:43:13 +00001050 if( sqlite3SafetyCheck(db) ){
1051 return SQLITE_MISUSE;
1052 }
danielk19777cedc8d2004-06-10 10:50:08 +00001053 db->xCollNeeded = xCollNeeded;
1054 db->xCollNeeded16 = 0;
1055 db->pCollNeededArg = pCollNeededArg;
1056 return SQLITE_OK;
1057}
danielk1977d8123362004-06-12 09:25:12 +00001058
drh6c626082004-11-14 21:56:29 +00001059#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00001060/*
1061** Register a collation sequence factory callback with the database handle
1062** db. Replace any previously installed collation sequence factory.
1063*/
danielk19777cedc8d2004-06-10 10:50:08 +00001064int sqlite3_collation_needed16(
1065 sqlite3 *db,
1066 void *pCollNeededArg,
1067 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
1068){
drhc60d0442004-09-30 13:43:13 +00001069 if( sqlite3SafetyCheck(db) ){
1070 return SQLITE_MISUSE;
1071 }
danielk19777cedc8d2004-06-10 10:50:08 +00001072 db->xCollNeeded = 0;
1073 db->xCollNeeded16 = xCollNeeded16;
1074 db->pCollNeededArg = pCollNeededArg;
1075 return SQLITE_OK;
1076}
drh6c626082004-11-14 21:56:29 +00001077#endif /* SQLITE_OMIT_UTF16 */
danielk19776b456a22005-03-21 04:04:02 +00001078
1079#ifndef SQLITE_OMIT_GLOBALRECOVER
1080/*
danielk19777ddad962005-12-12 06:53:03 +00001081** This function is now an anachronism. It used to be used to recover from a
1082** malloc() failure, but SQLite now does this automatically.
danielk19776b456a22005-03-21 04:04:02 +00001083*/
1084int sqlite3_global_recover(){
danielk1977261919c2005-12-06 12:52:59 +00001085 return SQLITE_OK;
danielk19776b456a22005-03-21 04:04:02 +00001086}
1087#endif
drh3e1d8e62005-05-26 16:23:34 +00001088
1089/*
1090** Test to see whether or not the database connection is in autocommit
1091** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
1092** by default. Autocommit is disabled by a BEGIN statement and reenabled
1093** by the next COMMIT or ROLLBACK.
1094**
1095******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
1096*/
1097int sqlite3_get_autocommit(sqlite3 *db){
1098 return db->autoCommit;
1099}
drh49285702005-09-17 15:20:26 +00001100
1101#ifdef SQLITE_DEBUG
1102/*
1103** The following routine is subtituted for constant SQLITE_CORRUPT in
1104** debugging builds. This provides a way to set a breakpoint for when
1105** corruption is first detected.
1106*/
1107int sqlite3Corrupt(void){
1108 return SQLITE_CORRUPT;
1109}
1110#endif