blob: ff5bc08752e7bcb092fc1989852746da15147bc3 [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**
danielk19777ddad962005-12-12 06:53:03 +000017** $Id: main.c,v 1.308 2005/12/12 06:53:04 danielk1977 Exp $
drh75897232000-05-29 14:26:00 +000018*/
19#include "sqliteInt.h"
drh8cfbf082001-09-19 13:22:39 +000020#include "os.h"
drhce9079c2002-05-15 14:17:44 +000021#include <ctype.h>
drh75897232000-05-29 14:26:00 +000022
23/*
drh9c054832004-05-31 18:51:57 +000024** The following constant value is used by the SQLITE_BIGENDIAN and
25** SQLITE_LITTLEENDIAN macros.
drhbbd42a62004-05-22 17:41:58 +000026*/
27const int sqlite3one = 1;
28
danielk19776b456a22005-03-21 04:04:02 +000029#ifndef SQLITE_OMIT_GLOBALRECOVER
30/*
31** Linked list of all open database handles. This is used by the
32** sqlite3_global_recover() function. Entries are added to the list
33** by openDatabase() and removed by sqlite3_close().
34*/
35static sqlite3 *pDbList = 0;
36#endif
37
danielk1977fd9a0a42005-05-24 12:01:00 +000038#ifndef SQLITE_OMIT_UTF16
39/*
40** Return the transient sqlite3_value object used for encoding conversions
41** during SQL compilation.
42*/
43sqlite3_value *sqlite3GetTransientValue(sqlite3 *db){
44 if( !db->pValue ){
45 db->pValue = sqlite3ValueNew();
46 }
47 return db->pValue;
48}
49#endif
50
drhc2311722002-07-19 17:46:38 +000051/*
drhb217a572000-08-22 13:40:18 +000052** The version of the library
53*/
drh38f82712004-06-18 17:10:16 +000054const char rcsid3[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $";
danielk197724b03fd2004-05-10 10:34:34 +000055const char sqlite3_version[] = SQLITE_VERSION;
drh4aec8b62004-08-28 16:19:00 +000056const char *sqlite3_libversion(void){ return sqlite3_version; }
danielk197799ba19e2005-02-05 07:33:34 +000057int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
drhb217a572000-08-22 13:40:18 +000058
59/*
drhd3d39e92004-05-20 22:16:29 +000060** This is the default collating function named "BINARY" which is always
61** available.
62*/
danielk19772c336542005-01-13 02:14:23 +000063static int binCollFunc(
drhd3d39e92004-05-20 22:16:29 +000064 void *NotUsed,
65 int nKey1, const void *pKey1,
66 int nKey2, const void *pKey2
67){
68 int rc, n;
69 n = nKey1<nKey2 ? nKey1 : nKey2;
70 rc = memcmp(pKey1, pKey2, n);
71 if( rc==0 ){
72 rc = nKey1 - nKey2;
73 }
74 return rc;
75}
76
77/*
danielk1977dc1bdc42004-06-11 10:51:27 +000078** Another built-in collating sequence: NOCASE.
79**
80** This collating sequence is intended to be used for "case independant
81** comparison". SQLite's knowledge of upper and lower case equivalents
82** extends only to the 26 characters used in the English language.
83**
84** At the moment there is only a UTF-8 implementation.
danielk19770202b292004-06-09 09:55:16 +000085*/
86static int nocaseCollatingFunc(
87 void *NotUsed,
88 int nKey1, const void *pKey1,
89 int nKey2, const void *pKey2
90){
91 int r = sqlite3StrNICmp(
drh208f80a2004-08-29 20:08:58 +000092 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
danielk19770202b292004-06-09 09:55:16 +000093 if( 0==r ){
94 r = nKey1-nKey2;
95 }
96 return r;
97}
98
99/*
drhaf9ff332002-01-16 21:00:27 +0000100** Return the ROWID of the most recent insert
101*/
drh9bb575f2004-09-06 17:24:11 +0000102sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
drhaf9ff332002-01-16 21:00:27 +0000103 return db->lastRowid;
104}
105
106/*
danielk197724b03fd2004-05-10 10:34:34 +0000107** Return the number of changes in the most recent call to sqlite3_exec().
drhc8d30ac2002-04-12 10:08:59 +0000108*/
drh9bb575f2004-09-06 17:24:11 +0000109int sqlite3_changes(sqlite3 *db){
drhc8d30ac2002-04-12 10:08:59 +0000110 return db->nChange;
111}
112
rdcf146a772004-02-25 22:51:06 +0000113/*
danielk1977b28af712004-06-21 06:50:26 +0000114** Return the number of changes since the database handle was opened.
rdcf146a772004-02-25 22:51:06 +0000115*/
danielk1977b28af712004-06-21 06:50:26 +0000116int sqlite3_total_changes(sqlite3 *db){
117 return db->nTotalChange;
rdcb0c374f2004-02-20 22:53:38 +0000118}
119
drhc8d30ac2002-04-12 10:08:59 +0000120/*
drh50e5dad2001-09-15 00:57:28 +0000121** Close an existing SQLite database
122*/
drh9bb575f2004-09-06 17:24:11 +0000123int sqlite3_close(sqlite3 *db){
drh8e0a2f92002-02-23 23:45:45 +0000124 HashElem *i;
drh001bbcb2003-03-19 03:14:00 +0000125 int j;
danielk19775c4c7782004-06-16 10:39:23 +0000126
127 if( !db ){
danielk197796d81f92004-06-19 03:33:57 +0000128 return SQLITE_OK;
danielk19775c4c7782004-06-16 10:39:23 +0000129 }
drhc60d0442004-09-30 13:43:13 +0000130 if( sqlite3SafetyCheck(db) ){
danielk1977e35ee192004-06-26 09:50:11 +0000131 return SQLITE_MISUSE;
132 }
133
danielk19771f723bd2005-05-26 12:37:29 +0000134#ifdef SQLITE_SSE
135 sqlite3_finalize(db->pFetch);
136#endif
137
danielk197796d81f92004-06-19 03:33:57 +0000138 /* If there are any outstanding VMs, return SQLITE_BUSY. */
139 if( db->pVdbe ){
140 sqlite3Error(db, SQLITE_BUSY,
141 "Unable to close due to unfinalised statements");
142 return SQLITE_BUSY;
143 }
144 assert( !sqlite3SafetyCheck(db) );
danielk1977e0048402004-06-15 16:51:01 +0000145
146 /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database
147 ** cannot be opened for some reason. So this routine needs to run in
148 ** that case. But maybe there should be an extra magic value for the
149 ** "failed to open" state.
150 */
danielk197796d81f92004-06-19 03:33:57 +0000151 if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){
drh94e92032003-02-16 22:21:32 +0000152 /* printf("DID NOT CLOSE\n"); fflush(stdout); */
danielk197796d81f92004-06-19 03:33:57 +0000153 return SQLITE_ERROR;
drh94e92032003-02-16 22:21:32 +0000154 }
danielk1977e0048402004-06-15 16:51:01 +0000155
danielk19777ddad962005-12-12 06:53:03 +0000156 /* sqlite3_close() may not invoke sqliteMalloc(). */
157 sqlite3MallocDisallow();
158
drh001bbcb2003-03-19 03:14:00 +0000159 for(j=0; j<db->nDb; j++){
drh4d189ca2004-02-12 18:46:38 +0000160 struct Db *pDb = &db->aDb[j];
161 if( pDb->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000162 sqlite3BtreeClose(pDb->pBt);
drh4d189ca2004-02-12 18:46:38 +0000163 pDb->pBt = 0;
drh113088e2003-03-20 01:16:58 +0000164 }
drhf57b3392001-10-08 13:22:32 +0000165 }
danielk19774adee202004-05-08 08:23:19 +0000166 sqlite3ResetInternalSchema(db, 0);
drh1c2d8412003-03-31 00:30:47 +0000167 assert( db->nDb<=2 );
168 assert( db->aDb==db->aDbStatic );
drh0bce8352002-02-28 00:41:10 +0000169 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
170 FuncDef *pFunc, *pNext;
171 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
drh8e0a2f92002-02-23 23:45:45 +0000172 pNext = pFunc->pNext;
173 sqliteFree(pFunc);
174 }
175 }
danielk1977466be562004-06-10 02:16:01 +0000176
danielk1977d8123362004-06-12 09:25:12 +0000177 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
danielk1977466be562004-06-10 02:16:01 +0000178 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
danielk1977d8123362004-06-12 09:25:12 +0000179 sqliteFree(pColl);
danielk1977466be562004-06-10 02:16:01 +0000180 }
danielk1977d8123362004-06-12 09:25:12 +0000181 sqlite3HashClear(&db->aCollSeq);
danielk1977466be562004-06-10 02:16:01 +0000182
danielk19774adee202004-05-08 08:23:19 +0000183 sqlite3HashClear(&db->aFunc);
danielk19776622cce2004-05-20 11:00:52 +0000184 sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
danielk1977bfd6cce2004-06-18 04:24:54 +0000185 if( db->pValue ){
186 sqlite3ValueFree(db->pValue);
187 }
188 if( db->pErr ){
189 sqlite3ValueFree(db->pErr);
190 }
danielk197796d81f92004-06-19 03:33:57 +0000191
danielk19777ddad962005-12-12 06:53:03 +0000192#if 0
danielk19776b456a22005-03-21 04:04:02 +0000193#ifndef SQLITE_OMIT_GLOBALRECOVER
194 {
danielk19773eb8db92005-03-29 23:34:58 +0000195 sqlite3 *pPrev;
drh054889e2005-11-30 03:20:31 +0000196 sqlite3Os.xEnterMutex();
danielk19773eb8db92005-03-29 23:34:58 +0000197 pPrev = pDbList;
danielk19776b456a22005-03-21 04:04:02 +0000198 while( pPrev && pPrev->pNext!=db ){
199 pPrev = pPrev->pNext;
200 }
201 if( pPrev ){
202 pPrev->pNext = db->pNext;
203 }else{
204 assert( pDbList==db );
205 pDbList = db->pNext;
206 }
drh054889e2005-11-30 03:20:31 +0000207 sqlite3Os.xLeaveMutex();
danielk19776b456a22005-03-21 04:04:02 +0000208 }
209#endif
danielk19777ddad962005-12-12 06:53:03 +0000210#endif
danielk19776b456a22005-03-21 04:04:02 +0000211
danielk197796d81f92004-06-19 03:33:57 +0000212 db->magic = SQLITE_MAGIC_ERROR;
drh75897232000-05-29 14:26:00 +0000213 sqliteFree(db);
danielk19777ddad962005-12-12 06:53:03 +0000214 sqlite3MallocAllow();
danielk197796d81f92004-06-19 03:33:57 +0000215 return SQLITE_OK;
drh75897232000-05-29 14:26:00 +0000216}
217
218/*
drh001bbcb2003-03-19 03:14:00 +0000219** Rollback all database files.
220*/
drh9bb575f2004-09-06 17:24:11 +0000221void sqlite3RollbackAll(sqlite3 *db){
drh001bbcb2003-03-19 03:14:00 +0000222 int i;
223 for(i=0; i<db->nDb; i++){
224 if( db->aDb[i].pBt ){
danielk19774adee202004-05-08 08:23:19 +0000225 sqlite3BtreeRollback(db->aDb[i].pBt);
drh001bbcb2003-03-19 03:14:00 +0000226 db->aDb[i].inTrans = 0;
227 }
228 }
danielk19774adee202004-05-08 08:23:19 +0000229 sqlite3ResetInternalSchema(db, 0);
drh001bbcb2003-03-19 03:14:00 +0000230}
231
232/*
drhc22bd472002-05-10 13:14:07 +0000233** Return a static string that describes the kind of error specified in the
234** argument.
drh247be432002-05-10 05:44:55 +0000235*/
danielk1977f20b21c2004-05-31 23:56:42 +0000236const char *sqlite3ErrStr(int rc){
drhc22bd472002-05-10 13:14:07 +0000237 const char *z;
238 switch( rc ){
drhc60d0442004-09-30 13:43:13 +0000239 case SQLITE_ROW:
240 case SQLITE_DONE:
drhc22bd472002-05-10 13:14:07 +0000241 case SQLITE_OK: z = "not an error"; break;
242 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
drhc22bd472002-05-10 13:14:07 +0000243 case SQLITE_PERM: z = "access permission denied"; break;
244 case SQLITE_ABORT: z = "callback requested query abort"; break;
245 case SQLITE_BUSY: z = "database is locked"; break;
246 case SQLITE_LOCKED: z = "database table is locked"; break;
247 case SQLITE_NOMEM: z = "out of memory"; break;
248 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
249 case SQLITE_INTERRUPT: z = "interrupted"; break;
250 case SQLITE_IOERR: z = "disk I/O error"; break;
251 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
drh2db0bbc2005-08-11 02:10:18 +0000252 case SQLITE_FULL: z = "database or disk is full"; break;
drhc22bd472002-05-10 13:14:07 +0000253 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
254 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
255 case SQLITE_EMPTY: z = "table contains no data"; break;
256 case SQLITE_SCHEMA: z = "database schema has changed"; break;
drhc22bd472002-05-10 13:14:07 +0000257 case SQLITE_CONSTRAINT: z = "constraint failed"; break;
258 case SQLITE_MISMATCH: z = "datatype mismatch"; break;
259 case SQLITE_MISUSE: z = "library routine called out of sequence";break;
drh8766c342002-11-09 00:33:15 +0000260 case SQLITE_NOLFS: z = "kernel lacks large file support"; break;
drhed6c8672003-01-12 18:02:16 +0000261 case SQLITE_AUTH: z = "authorization denied"; break;
jplyon892f6712003-06-12 08:59:00 +0000262 case SQLITE_FORMAT: z = "auxiliary database format error"; break;
drhb08153d2004-11-20 20:18:55 +0000263 case SQLITE_RANGE: z = "bind or column index out of range"; break;
drhc602f9a2004-02-12 19:01:04 +0000264 case SQLITE_NOTADB: z = "file is encrypted or is not a database";break;
drhc22bd472002-05-10 13:14:07 +0000265 default: z = "unknown error"; break;
drh247be432002-05-10 05:44:55 +0000266 }
drhc22bd472002-05-10 13:14:07 +0000267 return z;
drh247be432002-05-10 05:44:55 +0000268}
269
270/*
drh2dfbbca2000-07-28 14:32:48 +0000271** This routine implements a busy callback that sleeps and tries
272** again until a timeout value is reached. The timeout value is
273** an integer number of milliseconds passed in as the first
274** argument.
275*/
drhdaffd0e2001-04-11 14:28:42 +0000276static int sqliteDefaultBusyCallback(
drh97903fe2005-05-24 20:19:57 +0000277 void *ptr, /* Database connection */
drh2dfbbca2000-07-28 14:32:48 +0000278 int count /* Number of times table has been busy */
279){
drh8cfbf082001-09-19 13:22:39 +0000280#if SQLITE_MIN_SLEEP_MS==1
drhee570fa2005-04-28 12:06:05 +0000281 static const u8 delays[] =
282 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
283 static const u8 totals[] =
284 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
drhd1bec472004-01-15 13:29:31 +0000285# define NDELAY (sizeof(delays)/sizeof(delays[0]))
drh97903fe2005-05-24 20:19:57 +0000286 int timeout = ((sqlite3 *)ptr)->busyTimeout;
287 int delay, prior;
drh2dfbbca2000-07-28 14:32:48 +0000288
drhee570fa2005-04-28 12:06:05 +0000289 assert( count>=0 );
290 if( count < NDELAY ){
291 delay = delays[count];
292 prior = totals[count];
drhd1bec472004-01-15 13:29:31 +0000293 }else{
294 delay = delays[NDELAY-1];
drh68cb6192005-05-06 22:05:56 +0000295 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
drh2dfbbca2000-07-28 14:32:48 +0000296 }
drhd1bec472004-01-15 13:29:31 +0000297 if( prior + delay > timeout ){
298 delay = timeout - prior;
drh2dfbbca2000-07-28 14:32:48 +0000299 if( delay<=0 ) return 0;
300 }
drh054889e2005-11-30 03:20:31 +0000301 sqlite3Os.xSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000302 return 1;
303#else
drh3f737082005-06-14 02:24:31 +0000304 int timeout = ((sqlite3 *)ptr)->busyTimeout;
drh2dfbbca2000-07-28 14:32:48 +0000305 if( (count+1)*1000 > timeout ){
306 return 0;
307 }
drh054889e2005-11-30 03:20:31 +0000308 sqlite3Os.xSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000309 return 1;
310#endif
311}
312
313/*
drha4afb652005-07-09 02:16:02 +0000314** Invoke the given busy handler.
315**
316** This routine is called when an operation failed with a lock.
317** If this routine returns non-zero, the lock is retried. If it
318** returns 0, the operation aborts with an SQLITE_BUSY error.
319*/
320int sqlite3InvokeBusyHandler(BusyHandler *p){
321 int rc;
322 if( p==0 || p->xFunc==0 || p->nBusy<0 ) return 0;
323 rc = p->xFunc(p->pArg, p->nBusy);
324 if( rc==0 ){
325 p->nBusy = -1;
326 }else{
327 p->nBusy++;
328 }
329 return rc;
330}
331
332/*
drh2dfbbca2000-07-28 14:32:48 +0000333** This routine sets the busy callback for an Sqlite database to the
334** given callback function with the given argument.
335*/
danielk1977f9d64d22004-06-19 08:18:07 +0000336int sqlite3_busy_handler(
337 sqlite3 *db,
danielk19772a764eb2004-06-12 01:43:26 +0000338 int (*xBusy)(void*,int),
drh2dfbbca2000-07-28 14:32:48 +0000339 void *pArg
340){
drhc60d0442004-09-30 13:43:13 +0000341 if( sqlite3SafetyCheck(db) ){
342 return SQLITE_MISUSE;
343 }
danielk197724162fe2004-06-04 06:22:00 +0000344 db->busyHandler.xFunc = xBusy;
345 db->busyHandler.pArg = pArg;
drha4afb652005-07-09 02:16:02 +0000346 db->busyHandler.nBusy = 0;
danielk1977f9d64d22004-06-19 08:18:07 +0000347 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000348}
349
danielk1977348bb5d2003-10-18 09:37:26 +0000350#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
351/*
352** This routine sets the progress callback for an Sqlite database to the
353** given callback function with the given argument. The progress callback will
354** be invoked every nOps opcodes.
355*/
danielk197724b03fd2004-05-10 10:34:34 +0000356void sqlite3_progress_handler(
drh9bb575f2004-09-06 17:24:11 +0000357 sqlite3 *db,
danielk1977348bb5d2003-10-18 09:37:26 +0000358 int nOps,
359 int (*xProgress)(void*),
360 void *pArg
361){
drhc60d0442004-09-30 13:43:13 +0000362 if( !sqlite3SafetyCheck(db) ){
363 if( nOps>0 ){
364 db->xProgress = xProgress;
365 db->nProgressOps = nOps;
366 db->pProgressArg = pArg;
367 }else{
368 db->xProgress = 0;
369 db->nProgressOps = 0;
370 db->pProgressArg = 0;
371 }
danielk1977348bb5d2003-10-18 09:37:26 +0000372 }
373}
374#endif
375
376
drh2dfbbca2000-07-28 14:32:48 +0000377/*
378** This routine installs a default busy handler that waits for the
379** specified number of milliseconds before returning 0.
380*/
danielk1977f9d64d22004-06-19 08:18:07 +0000381int sqlite3_busy_timeout(sqlite3 *db, int ms){
drh2dfbbca2000-07-28 14:32:48 +0000382 if( ms>0 ){
drh97903fe2005-05-24 20:19:57 +0000383 db->busyTimeout = ms;
384 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
drh2dfbbca2000-07-28 14:32:48 +0000385 }else{
danielk197724b03fd2004-05-10 10:34:34 +0000386 sqlite3_busy_handler(db, 0, 0);
drh2dfbbca2000-07-28 14:32:48 +0000387 }
danielk1977f9d64d22004-06-19 08:18:07 +0000388 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000389}
drh4c504392000-10-16 22:06:40 +0000390
391/*
392** Cause any pending operation to stop at its earliest opportunity.
393*/
drh9bb575f2004-09-06 17:24:11 +0000394void sqlite3_interrupt(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000395 if( !sqlite3SafetyCheck(db) ){
396 db->flags |= SQLITE_Interrupt;
397 }
drh4c504392000-10-16 22:06:40 +0000398}
drhfa86c412002-02-02 15:01:15 +0000399
400/*
401** Windows systems should call this routine to free memory that
danielk197724b03fd2004-05-10 10:34:34 +0000402** is returned in the in the errmsg parameter of sqlite3_open() when
drhfa86c412002-02-02 15:01:15 +0000403** SQLite is a DLL. For some reason, it does not work to call free()
404** directly.
405**
drhae29ffb2004-09-25 14:39:18 +0000406** Note that we need to call free() not sqliteFree() here.
drhfa86c412002-02-02 15:01:15 +0000407*/
drh3f4fedb2004-05-31 19:34:33 +0000408void sqlite3_free(char *p){ free(p); }
drhfa86c412002-02-02 15:01:15 +0000409
410/*
drhdf014892004-06-02 00:41:09 +0000411** Create new user functions.
drhfa86c412002-02-02 15:01:15 +0000412*/
danielk197724b03fd2004-05-10 10:34:34 +0000413int sqlite3_create_function(
danielk197765904932004-05-26 06:18:37 +0000414 sqlite3 *db,
415 const char *zFunctionName,
416 int nArg,
danielk1977d8123362004-06-12 09:25:12 +0000417 int enc,
danielk197765904932004-05-26 06:18:37 +0000418 void *pUserData,
419 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
420 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
421 void (*xFinal)(sqlite3_context*)
drh8e0a2f92002-02-23 23:45:45 +0000422){
drh0bce8352002-02-28 00:41:10 +0000423 FuncDef *p;
drh4b59ab52002-08-24 18:24:51 +0000424 int nName;
danielk197765904932004-05-26 06:18:37 +0000425
drhc60d0442004-09-30 13:43:13 +0000426 if( sqlite3SafetyCheck(db) ){
427 return SQLITE_MISUSE;
428 }
429 if( zFunctionName==0 ||
danielk1977398eae72004-05-26 06:58:43 +0000430 (xFunc && (xFinal || xStep)) ||
431 (!xFunc && (xFinal && !xStep)) ||
432 (!xFunc && (!xFinal && xStep)) ||
433 (nArg<-1 || nArg>127) ||
434 (255<(nName = strlen(zFunctionName))) ){
danielk197765904932004-05-26 06:18:37 +0000435 return SQLITE_ERROR;
436 }
danielk1977d8123362004-06-12 09:25:12 +0000437
danielk197793758c82005-01-21 08:13:14 +0000438#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +0000439 /* If SQLITE_UTF16 is specified as the encoding type, transform this
440 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
441 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
442 **
443 ** If SQLITE_ANY is specified, add three versions of the function
444 ** to the hash table.
445 */
446 if( enc==SQLITE_UTF16 ){
447 enc = SQLITE_UTF16NATIVE;
448 }else if( enc==SQLITE_ANY ){
449 int rc;
450 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF8,
danielk1977f9d64d22004-06-19 08:18:07 +0000451 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000452 if( rc!=SQLITE_OK ) return rc;
453 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF16LE,
danielk1977f9d64d22004-06-19 08:18:07 +0000454 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000455 if( rc!=SQLITE_OK ) return rc;
456 enc = SQLITE_UTF16BE;
457 }
danielk197793758c82005-01-21 08:13:14 +0000458#else
459 enc = SQLITE_UTF8;
460#endif
danielk19779636c4e2005-01-25 04:27:54 +0000461
462 /* Check if an existing function is being overridden or deleted. If so,
463 ** and there are active VMs, then return SQLITE_BUSY. If a function
464 ** is being overridden/deleted but there are no active VMs, allow the
465 ** operation to continue but invalidate all precompiled statements.
466 */
467 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0);
468 if( p && p->iPrefEnc==enc && p->nArg==nArg ){
469 if( db->activeVdbeCnt ){
470 sqlite3Error(db, SQLITE_BUSY,
471 "Unable to delete/modify user-function due to active statements");
472 return SQLITE_BUSY;
473 }else{
474 sqlite3ExpirePreparedStatements(db);
475 }
476 }
danielk197765904932004-05-26 06:18:37 +0000477
danielk1977d8123362004-06-12 09:25:12 +0000478 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);
danielk197713073932004-06-30 11:54:06 +0000479 if( p==0 ) return SQLITE_NOMEM;
drh55ef4d92005-08-14 01:20:37 +0000480 p->flags = 0;
drh8e0a2f92002-02-23 23:45:45 +0000481 p->xFunc = xFunc;
drh8e0a2f92002-02-23 23:45:45 +0000482 p->xStep = xStep;
danielk197765904932004-05-26 06:18:37 +0000483 p->xFinalize = xFinal;
drh1350b032002-02-27 19:00:20 +0000484 p->pUserData = pUserData;
danielk197765904932004-05-26 06:18:37 +0000485 return SQLITE_OK;
486}
danielk197793758c82005-01-21 08:13:14 +0000487#ifndef SQLITE_OMIT_UTF16
danielk197765904932004-05-26 06:18:37 +0000488int sqlite3_create_function16(
489 sqlite3 *db,
490 const void *zFunctionName,
491 int nArg,
492 int eTextRep,
danielk197765904932004-05-26 06:18:37 +0000493 void *pUserData,
494 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
495 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
496 void (*xFinal)(sqlite3_context*)
497){
498 int rc;
danielk1977bfd6cce2004-06-18 04:24:54 +0000499 char const *zFunc8;
drhc60d0442004-09-30 13:43:13 +0000500 sqlite3_value *pTmp;
danielk1977bfd6cce2004-06-18 04:24:54 +0000501
drhc60d0442004-09-30 13:43:13 +0000502 if( sqlite3SafetyCheck(db) ){
503 return SQLITE_MISUSE;
504 }
505 pTmp = sqlite3GetTransientValue(db);
danielk1977bfd6cce2004-06-18 04:24:54 +0000506 sqlite3ValueSetStr(pTmp, -1, zFunctionName, SQLITE_UTF16NATIVE,SQLITE_STATIC);
507 zFunc8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
508
509 if( !zFunc8 ){
danielk197765904932004-05-26 06:18:37 +0000510 return SQLITE_NOMEM;
511 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000512 rc = sqlite3_create_function(db, zFunc8, nArg, eTextRep,
danielk1977f9d64d22004-06-19 08:18:07 +0000513 pUserData, xFunc, xStep, xFinal);
danielk197765904932004-05-26 06:18:37 +0000514 return rc;
drh8e0a2f92002-02-23 23:45:45 +0000515}
danielk197793758c82005-01-21 08:13:14 +0000516#endif
drhc9b84a12002-06-20 11:36:48 +0000517
drh19e2d372005-08-29 23:00:03 +0000518#ifndef SQLITE_OMIT_TRACE
drhc9b84a12002-06-20 11:36:48 +0000519/*
drh18de4822003-01-16 16:28:53 +0000520** Register a trace function. The pArg from the previously registered trace
521** is returned.
522**
523** A NULL trace function means that no tracing is executes. A non-NULL
524** trace is a pointer to a function that is invoked at the start of each
drh19e2d372005-08-29 23:00:03 +0000525** SQL statement.
drh18de4822003-01-16 16:28:53 +0000526*/
drh9bb575f2004-09-06 17:24:11 +0000527void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
drh18de4822003-01-16 16:28:53 +0000528 void *pOld = db->pTraceArg;
529 db->xTrace = xTrace;
530 db->pTraceArg = pArg;
531 return pOld;
drh0d1a6432003-04-03 15:46:04 +0000532}
drh19e2d372005-08-29 23:00:03 +0000533/*
534** Register a profile function. The pArg from the previously registered
535** profile function is returned.
536**
537** A NULL profile function means that no profiling is executes. A non-NULL
538** profile is a pointer to a function that is invoked at the conclusion of
539** each SQL statement that is run.
540*/
541void *sqlite3_profile(
542 sqlite3 *db,
543 void (*xProfile)(void*,const char*,sqlite_uint64),
544 void *pArg
545){
546 void *pOld = db->pProfileArg;
547 db->xProfile = xProfile;
548 db->pProfileArg = pArg;
549 return pOld;
550}
551#endif /* SQLITE_OMIT_TRACE */
paulb0208cc2003-04-13 18:26:49 +0000552
drhaa940ea2004-01-15 02:44:03 +0000553/*** EXPERIMENTAL ***
554**
555** Register a function to be invoked when a transaction comments.
556** If either function returns non-zero, then the commit becomes a
557** rollback.
558*/
danielk197724b03fd2004-05-10 10:34:34 +0000559void *sqlite3_commit_hook(
drh9bb575f2004-09-06 17:24:11 +0000560 sqlite3 *db, /* Attach the hook to this database */
drhaa940ea2004-01-15 02:44:03 +0000561 int (*xCallback)(void*), /* Function to invoke on each commit */
562 void *pArg /* Argument to the function */
563){
564 void *pOld = db->pCommitArg;
565 db->xCommitCallback = xCallback;
566 db->pCommitArg = pArg;
567 return pOld;
568}
569
570
paulb0208cc2003-04-13 18:26:49 +0000571/*
drh13bff812003-04-15 01:19:47 +0000572** This routine is called to create a connection to a database BTree
573** driver. If zFilename is the name of a file, then that file is
574** opened and used. If zFilename is the magic name ":memory:" then
575** the database is stored in memory (and is thus forgotten as soon as
576** the connection is closed.) If zFilename is NULL then the database
drh84bfda42005-07-15 13:05:21 +0000577** is a "virtual" database for transient use only and is deleted as
578** soon as the connection is closed.
drh90f5ecb2004-07-22 01:19:35 +0000579**
drh84bfda42005-07-15 13:05:21 +0000580** A virtual database can be either a disk file (that is automatically
581** deleted when the file is closed) or it an be held entirely in memory,
drh90f5ecb2004-07-22 01:19:35 +0000582** depending on the values of the TEMP_STORE compile-time macro and the
583** db->temp_store variable, according to the following chart:
584**
585** TEMP_STORE db->temp_store Location of temporary database
586** ---------- -------------- ------------------------------
587** 0 any file
588** 1 1 file
589** 1 2 memory
590** 1 0 file
591** 2 1 file
592** 2 2 memory
593** 2 0 memory
594** 3 any memory
paulb0208cc2003-04-13 18:26:49 +0000595*/
danielk19774adee202004-05-08 08:23:19 +0000596int sqlite3BtreeFactory(
drh9bb575f2004-09-06 17:24:11 +0000597 const sqlite3 *db, /* Main database when opening aux otherwise 0 */
paulb0208cc2003-04-13 18:26:49 +0000598 const char *zFilename, /* Name of the file containing the BTree database */
599 int omitJournal, /* if TRUE then do not journal this file */
600 int nCache, /* How many pages in the page cache */
danielk19774adee202004-05-08 08:23:19 +0000601 Btree **ppBtree /* Pointer to new Btree object written here */
602){
danielk19774adee202004-05-08 08:23:19 +0000603 int btree_flags = 0;
drh90f5ecb2004-07-22 01:19:35 +0000604 int rc;
danielk19774adee202004-05-08 08:23:19 +0000605
drheec983e2004-05-08 10:11:36 +0000606 assert( ppBtree != 0);
danielk19774adee202004-05-08 08:23:19 +0000607 if( omitJournal ){
608 btree_flags |= BTREE_OMIT_JOURNAL;
paulb0208cc2003-04-13 18:26:49 +0000609 }
drh7bec5052005-02-06 02:45:41 +0000610 if( db->flags & SQLITE_NoReadlock ){
611 btree_flags |= BTREE_NO_READLOCK;
612 }
drh90f5ecb2004-07-22 01:19:35 +0000613 if( zFilename==0 ){
drh90f5ecb2004-07-22 01:19:35 +0000614#if TEMP_STORE==0
drh22ac46d2004-08-14 18:34:54 +0000615 /* Do nothing */
drh90f5ecb2004-07-22 01:19:35 +0000616#endif
danielk197703aded42004-11-22 05:26:27 +0000617#ifndef SQLITE_OMIT_MEMORYDB
drh90f5ecb2004-07-22 01:19:35 +0000618#if TEMP_STORE==1
drh22ac46d2004-08-14 18:34:54 +0000619 if( db->temp_store==2 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000620#endif
621#if TEMP_STORE==2
drh22ac46d2004-08-14 18:34:54 +0000622 if( db->temp_store!=1 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000623#endif
624#if TEMP_STORE==3
drh22ac46d2004-08-14 18:34:54 +0000625 zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000626#endif
danielk197703aded42004-11-22 05:26:27 +0000627#endif /* SQLITE_OMIT_MEMORYDB */
drh90f5ecb2004-07-22 01:19:35 +0000628 }
danielk19774adee202004-05-08 08:23:19 +0000629
drh90f5ecb2004-07-22 01:19:35 +0000630 rc = sqlite3BtreeOpen(zFilename, ppBtree, btree_flags);
631 if( rc==SQLITE_OK ){
632 sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler);
633 sqlite3BtreeSetCacheSize(*ppBtree, nCache);
634 }
635 return rc;
paulb0208cc2003-04-13 18:26:49 +0000636}
danielk19774adee202004-05-08 08:23:19 +0000637
danielk19774ad17132004-05-21 01:47:26 +0000638/*
639** Return UTF-8 encoded English language explanation of the most recent
640** error.
641*/
danielk19776622cce2004-05-20 11:00:52 +0000642const char *sqlite3_errmsg(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000643 const char *z;
danielk1977261919c2005-12-06 12:52:59 +0000644 if( sqlite3Tsd()->mallocFailed ){
danielk1977f20b21c2004-05-31 23:56:42 +0000645 return sqlite3ErrStr(SQLITE_NOMEM);
danielk19774ad17132004-05-21 01:47:26 +0000646 }
drhc60d0442004-09-30 13:43:13 +0000647 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
danielk1977e35ee192004-06-26 09:50:11 +0000648 return sqlite3ErrStr(SQLITE_MISUSE);
649 }
drh2646da72005-12-09 20:02:05 +0000650 z = (char*)sqlite3_value_text(db->pErr);
drhc60d0442004-09-30 13:43:13 +0000651 if( z==0 ){
652 z = sqlite3ErrStr(db->errCode);
danielk19776622cce2004-05-20 11:00:52 +0000653 }
drhc60d0442004-09-30 13:43:13 +0000654 return z;
danielk19776622cce2004-05-20 11:00:52 +0000655}
656
drh6c626082004-11-14 21:56:29 +0000657#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +0000658/*
659** Return UTF-16 encoded English language explanation of the most recent
660** error.
661*/
danielk19776622cce2004-05-20 11:00:52 +0000662const void *sqlite3_errmsg16(sqlite3 *db){
danielk1977bfd6cce2004-06-18 04:24:54 +0000663 /* Because all the characters in the string are in the unicode
664 ** range 0x00-0xFF, if we pad the big-endian string with a
665 ** zero byte, we can obtain the little-endian string with
666 ** &big_endian[1].
667 */
drh57196282004-10-06 15:41:16 +0000668 static const char outOfMemBe[] = {
danielk1977bfd6cce2004-06-18 04:24:54 +0000669 0, 'o', 0, 'u', 0, 't', 0, ' ',
670 0, 'o', 0, 'f', 0, ' ',
671 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0
672 };
drh57196282004-10-06 15:41:16 +0000673 static const char misuseBe [] = {
danielk1977e35ee192004-06-26 09:50:11 +0000674 0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ',
675 0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ',
676 0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ',
677 0, 'o', 0, 'u', 0, 't', 0, ' ',
678 0, 'o', 0, 'f', 0, ' ',
679 0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0
680 };
danielk19774ad17132004-05-21 01:47:26 +0000681
drhc60d0442004-09-30 13:43:13 +0000682 const void *z;
danielk1977261919c2005-12-06 12:52:59 +0000683 if( sqlite3Tsd()->mallocFailed ){
drhc60d0442004-09-30 13:43:13 +0000684 return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
685 }
686 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
687 return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
688 }
689 z = sqlite3_value_text16(db->pErr);
690 if( z==0 ){
691 sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
692 SQLITE_UTF8, SQLITE_STATIC);
693 z = sqlite3_value_text16(db->pErr);
694 }
695 return z;
danielk19776622cce2004-05-20 11:00:52 +0000696}
drh6c626082004-11-14 21:56:29 +0000697#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +0000698
drhc60d0442004-09-30 13:43:13 +0000699/*
danielk19777ddad962005-12-12 06:53:03 +0000700** Return the most recent error code generated by an SQLite routine. If NULL is
701** passed to this function, we assume a malloc() failed during sqlite3_open().
drhc60d0442004-09-30 13:43:13 +0000702*/
danielk19776622cce2004-05-20 11:00:52 +0000703int sqlite3_errcode(sqlite3 *db){
danielk19777ddad962005-12-12 06:53:03 +0000704 if( !db || sqlite3Tsd()->mallocFailed ){
drhc60d0442004-09-30 13:43:13 +0000705 return SQLITE_NOMEM;
706 }
707 if( sqlite3SafetyCheck(db) ){
708 return SQLITE_MISUSE;
709 }
danielk19776622cce2004-05-20 11:00:52 +0000710 return db->errCode;
711}
712
713/*
danielk19774ad17132004-05-21 01:47:26 +0000714** This routine does the work of opening a database on behalf of
715** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
drhee570fa2005-04-28 12:06:05 +0000716** is UTF-8 encoded.
danielk19774ad17132004-05-21 01:47:26 +0000717*/
718static int openDatabase(
719 const char *zFilename, /* Database filename UTF-8 encoded */
danielk19778e227872004-06-07 07:52:17 +0000720 sqlite3 **ppDb /* OUT: Returned database handle */
danielk19774ad17132004-05-21 01:47:26 +0000721){
722 sqlite3 *db;
723 int rc, i;
drhd64fe2f2005-08-28 17:00:23 +0000724 CollSeq *pColl;
danielk19774ad17132004-05-21 01:47:26 +0000725
danielk19777ddad962005-12-12 06:53:03 +0000726 assert( !sqlite3Tsd()->mallocFailed );
727
danielk19774ad17132004-05-21 01:47:26 +0000728 /* Allocate the sqlite data structure */
drh9bb575f2004-09-06 17:24:11 +0000729 db = sqliteMalloc( sizeof(sqlite3) );
danielk19774ad17132004-05-21 01:47:26 +0000730 if( db==0 ) goto opendb_out;
danielk19774ad17132004-05-21 01:47:26 +0000731 db->priorNewRowid = 0;
732 db->magic = SQLITE_MAGIC_BUSY;
733 db->nDb = 2;
734 db->aDb = db->aDbStatic;
danielk1977dc8453f2004-06-12 00:42:34 +0000735 db->enc = SQLITE_UTF8;
danielk19771d850a72004-05-31 08:26:49 +0000736 db->autoCommit = 1;
drh47a6db22005-01-18 16:02:40 +0000737 db->flags |= SQLITE_ShortColNames;
drhf9b596e2004-05-26 16:54:42 +0000738 sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
danielk19774ad17132004-05-21 01:47:26 +0000739 sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
740 for(i=0; i<db->nDb; i++){
741 sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
742 sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
743 sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);
744 sqlite3HashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1);
745 }
danielk19774ad17132004-05-21 01:47:26 +0000746
danielk19770202b292004-06-09 09:55:16 +0000747 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
748 ** and UTF-16, so add a version for each to avoid any unnecessary
749 ** conversions. The only error that can occur here is a malloc() failure.
750 */
danielk19772c336542005-01-13 02:14:23 +0000751 if( sqlite3_create_collation(db, "BINARY", SQLITE_UTF8, 0,binCollFunc) ||
752 sqlite3_create_collation(db, "BINARY", SQLITE_UTF16, 0,binCollFunc) ||
danielk19777ddad962005-12-12 06:53:03 +0000753 (db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0))==0
754 ){
755 /* sqlite3_create_collation() is an external API. So the mallocFailed flag
756 ** will have been cleared before returning. So set it explicitly here.
757 */
758 sqlite3Tsd()->mallocFailed = 1;
danielk19770202b292004-06-09 09:55:16 +0000759 db->magic = SQLITE_MAGIC_CLOSED;
760 goto opendb_out;
761 }
762
763 /* Also add a UTF-8 case-insensitive collation sequence. */
danielk1977466be562004-06-10 02:16:01 +0000764 sqlite3_create_collation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc);
danielk19770202b292004-06-09 09:55:16 +0000765
drhd64fe2f2005-08-28 17:00:23 +0000766 /* Set flags on the built-in collating sequences */
767 db->pDfltColl->type = SQLITE_COLL_BINARY;
768 pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "NOCASE", 6, 0);
769 if( pColl ){
770 pColl->type = SQLITE_COLL_NOCASE;
771 }
772
danielk19774ad17132004-05-21 01:47:26 +0000773 /* Open the backend database driver */
danielk19774ad17132004-05-21 01:47:26 +0000774 rc = sqlite3BtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt);
775 if( rc!=SQLITE_OK ){
danielk19774ad17132004-05-21 01:47:26 +0000776 sqlite3Error(db, rc, 0);
777 db->magic = SQLITE_MAGIC_CLOSED;
778 goto opendb_out;
779 }
danielk19774ad17132004-05-21 01:47:26 +0000780
danielk197753c0f742005-03-29 03:10:59 +0000781 /* The default safety_level for the main database is 'full'; for the temp
782 ** database it is 'NONE'. This matches the pager layer defaults.
783 */
784 db->aDb[0].zName = "main";
danielk197791cf71b2004-06-26 06:37:06 +0000785 db->aDb[0].safety_level = 3;
danielk197753c0f742005-03-29 03:10:59 +0000786#ifndef SQLITE_OMIT_TEMPDB
787 db->aDb[1].zName = "temp";
danielk197791cf71b2004-06-26 06:37:06 +0000788 db->aDb[1].safety_level = 1;
danielk197753c0f742005-03-29 03:10:59 +0000789#endif
790
danielk197791cf71b2004-06-26 06:37:06 +0000791
danielk19778e227872004-06-07 07:52:17 +0000792 /* Register all built-in functions, but do not attempt to read the
793 ** database schema yet. This is delayed until the first time the database
794 ** is accessed.
795 */
danielk19774ad17132004-05-21 01:47:26 +0000796 sqlite3RegisterBuiltinFunctions(db);
danielk19774397de52005-01-12 12:44:03 +0000797 sqlite3Error(db, SQLITE_OK, 0);
798 db->magic = SQLITE_MAGIC_OPEN;
danielk19774ad17132004-05-21 01:47:26 +0000799
800opendb_out:
danielk19777ddad962005-12-12 06:53:03 +0000801 if( SQLITE_NOMEM==(rc = sqlite3_errcode(db)) ){
802 sqlite3_close(db);
803 db = 0;
danielk197713073932004-06-30 11:54:06 +0000804 }
danielk19774ad17132004-05-21 01:47:26 +0000805 *ppDb = db;
danielk19777ddad962005-12-12 06:53:03 +0000806 sqlite3MallocClearFailed();
807 return rc;
danielk19774ad17132004-05-21 01:47:26 +0000808}
809
810/*
811** Open a new database handle.
812*/
danielk197780290862004-05-22 09:21:21 +0000813int sqlite3_open(
danielk19774ad17132004-05-21 01:47:26 +0000814 const char *zFilename,
danielk19774f057f92004-06-08 00:02:33 +0000815 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +0000816){
danielk19778e227872004-06-07 07:52:17 +0000817 return openDatabase(zFilename, ppDb);
danielk197783ab5a82004-05-21 11:39:05 +0000818}
819
drh6c626082004-11-14 21:56:29 +0000820#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +0000821/*
822** Open a new database handle.
823*/
824int sqlite3_open16(
825 const void *zFilename,
danielk19774f057f92004-06-08 00:02:33 +0000826 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +0000827){
danielk1977bfd6cce2004-06-18 04:24:54 +0000828 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
829 int rc = SQLITE_NOMEM;
830 sqlite3_value *pVal;
danielk19774ad17132004-05-21 01:47:26 +0000831
danielk19777ddad962005-12-12 06:53:03 +0000832 assert( zFilename );
danielk19774ad17132004-05-21 01:47:26 +0000833 assert( ppDb );
danielk1977bfd6cce2004-06-18 04:24:54 +0000834 *ppDb = 0;
835 pVal = sqlite3ValueNew();
836 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
837 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
838 if( zFilename8 ){
839 rc = openDatabase(zFilename8, ppDb);
840 if( rc==SQLITE_OK && *ppDb ){
drh7dd90a42005-12-06 13:19:07 +0000841 rc = sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
danielk1977bfd6cce2004-06-18 04:24:54 +0000842 }
danielk19777ddad962005-12-12 06:53:03 +0000843 }else{
844 assert( sqlite3Tsd()->mallocFailed );
845 sqlite3MallocClearFailed();
danielk19774ad17132004-05-21 01:47:26 +0000846 }
danielk19777ddad962005-12-12 06:53:03 +0000847 sqlite3ValueFree(pVal);
danielk19778e227872004-06-07 07:52:17 +0000848
danielk19774ad17132004-05-21 01:47:26 +0000849 return rc;
850}
drh6c626082004-11-14 21:56:29 +0000851#endif /* SQLITE_OMIT_UTF16 */
danielk19774ad17132004-05-21 01:47:26 +0000852
danielk1977106bb232004-05-21 10:08:53 +0000853/*
854** The following routine destroys a virtual machine that is created by
855** the sqlite3_compile() routine. The integer returned is an SQLITE_
856** success/failure code that describes the result of executing the virtual
857** machine.
858**
859** This routine sets the error code and string returned by
860** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
861*/
danielk1977fc57d7b2004-05-26 02:04:57 +0000862int sqlite3_finalize(sqlite3_stmt *pStmt){
drh1bcdb0c2004-08-28 14:49:46 +0000863 int rc;
864 if( pStmt==0 ){
865 rc = SQLITE_OK;
866 }else{
867 rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
868 }
869 return rc;
danielk1977106bb232004-05-21 10:08:53 +0000870}
871
872/*
873** Terminate the current execution of an SQL statement and reset it
874** back to its starting state so that it can be reused. A success code from
875** the prior execution is returned.
876**
877** This routine sets the error code and string returned by
878** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
879*/
danielk1977fc57d7b2004-05-26 02:04:57 +0000880int sqlite3_reset(sqlite3_stmt *pStmt){
drh1bcdb0c2004-08-28 14:49:46 +0000881 int rc;
882 if( pStmt==0 ){
883 rc = SQLITE_OK;
884 }else{
885 rc = sqlite3VdbeReset((Vdbe*)pStmt);
drh13449892005-09-07 21:22:45 +0000886 sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0, 0, 0);
drh1bcdb0c2004-08-28 14:49:46 +0000887 }
danielk1977106bb232004-05-21 10:08:53 +0000888 return rc;
889}
danielk19770202b292004-06-09 09:55:16 +0000890
danielk1977d8123362004-06-12 09:25:12 +0000891/*
892** Register a new collation sequence with the database handle db.
893*/
danielk19770202b292004-06-09 09:55:16 +0000894int sqlite3_create_collation(
895 sqlite3* db,
896 const char *zName,
danielk1977466be562004-06-10 02:16:01 +0000897 int enc,
danielk19770202b292004-06-09 09:55:16 +0000898 void* pCtx,
899 int(*xCompare)(void*,int,const void*,int,const void*)
900){
901 CollSeq *pColl;
902 int rc = SQLITE_OK;
danielk1977d8123362004-06-12 09:25:12 +0000903
drhc60d0442004-09-30 13:43:13 +0000904 if( sqlite3SafetyCheck(db) ){
905 return SQLITE_MISUSE;
906 }
907
danielk1977d8123362004-06-12 09:25:12 +0000908 /* If SQLITE_UTF16 is specified as the encoding type, transform this
909 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
910 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
911 */
912 if( enc==SQLITE_UTF16 ){
913 enc = SQLITE_UTF16NATIVE;
914 }
915
danielk1977466be562004-06-10 02:16:01 +0000916 if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16LE && enc!=SQLITE_UTF16BE ){
917 sqlite3Error(db, SQLITE_ERROR,
918 "Param 3 to sqlite3_create_collation() must be one of "
danielk1977d8123362004-06-12 09:25:12 +0000919 "SQLITE_UTF8, SQLITE_UTF16, SQLITE_UTF16LE or SQLITE_UTF16BE"
danielk1977466be562004-06-10 02:16:01 +0000920 );
921 return SQLITE_ERROR;
922 }
danielk1977a21c6b62005-01-24 10:25:59 +0000923
danielk19779636c4e2005-01-25 04:27:54 +0000924 /* Check if this call is removing or replacing an existing collation
925 ** sequence. If so, and there are active VMs, return busy. If there
926 ** are no active VMs, invalidate any pre-compiled statements.
danielk1977a21c6b62005-01-24 10:25:59 +0000927 */
danielk19779636c4e2005-01-25 04:27:54 +0000928 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 0);
929 if( pColl && pColl->xCmp ){
930 if( db->activeVdbeCnt ){
931 sqlite3Error(db, SQLITE_BUSY,
932 "Unable to delete/modify collation sequence due to active statements");
933 return SQLITE_BUSY;
934 }
danielk1977a21c6b62005-01-24 10:25:59 +0000935 sqlite3ExpirePreparedStatements(db);
936 }
937
danielk1977466be562004-06-10 02:16:01 +0000938 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 1);
danielk19770202b292004-06-09 09:55:16 +0000939 if( 0==pColl ){
drhd64fe2f2005-08-28 17:00:23 +0000940 rc = SQLITE_NOMEM;
danielk19770202b292004-06-09 09:55:16 +0000941 }else{
942 pColl->xCmp = xCompare;
943 pColl->pUser = pCtx;
danielk1977312d6b32004-06-29 13:18:23 +0000944 pColl->enc = enc;
danielk19770202b292004-06-09 09:55:16 +0000945 }
946 sqlite3Error(db, rc, 0);
danielk1977466be562004-06-10 02:16:01 +0000947 return rc;
danielk19770202b292004-06-09 09:55:16 +0000948}
949
drh6c626082004-11-14 21:56:29 +0000950#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +0000951/*
952** Register a new collation sequence with the database handle db.
953*/
danielk19770202b292004-06-09 09:55:16 +0000954int sqlite3_create_collation16(
955 sqlite3* db,
956 const char *zName,
danielk1977466be562004-06-10 02:16:01 +0000957 int enc,
danielk19770202b292004-06-09 09:55:16 +0000958 void* pCtx,
959 int(*xCompare)(void*,int,const void*,int,const void*)
960){
danielk1977bfd6cce2004-06-18 04:24:54 +0000961 char const *zName8;
drhc60d0442004-09-30 13:43:13 +0000962 sqlite3_value *pTmp;
963 if( sqlite3SafetyCheck(db) ){
964 return SQLITE_MISUSE;
965 }
966 pTmp = sqlite3GetTransientValue(db);
danielk1977bfd6cce2004-06-18 04:24:54 +0000967 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF16NATIVE, SQLITE_STATIC);
968 zName8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
969 return sqlite3_create_collation(db, zName8, enc, pCtx, xCompare);
danielk19770202b292004-06-09 09:55:16 +0000970}
drh6c626082004-11-14 21:56:29 +0000971#endif /* SQLITE_OMIT_UTF16 */
danielk19777cedc8d2004-06-10 10:50:08 +0000972
danielk1977d8123362004-06-12 09:25:12 +0000973/*
974** Register a collation sequence factory callback with the database handle
975** db. Replace any previously installed collation sequence factory.
976*/
danielk19777cedc8d2004-06-10 10:50:08 +0000977int sqlite3_collation_needed(
978 sqlite3 *db,
979 void *pCollNeededArg,
980 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
981){
drhc60d0442004-09-30 13:43:13 +0000982 if( sqlite3SafetyCheck(db) ){
983 return SQLITE_MISUSE;
984 }
danielk19777cedc8d2004-06-10 10:50:08 +0000985 db->xCollNeeded = xCollNeeded;
986 db->xCollNeeded16 = 0;
987 db->pCollNeededArg = pCollNeededArg;
988 return SQLITE_OK;
989}
danielk1977d8123362004-06-12 09:25:12 +0000990
drh6c626082004-11-14 21:56:29 +0000991#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +0000992/*
993** Register a collation sequence factory callback with the database handle
994** db. Replace any previously installed collation sequence factory.
995*/
danielk19777cedc8d2004-06-10 10:50:08 +0000996int sqlite3_collation_needed16(
997 sqlite3 *db,
998 void *pCollNeededArg,
999 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
1000){
drhc60d0442004-09-30 13:43:13 +00001001 if( sqlite3SafetyCheck(db) ){
1002 return SQLITE_MISUSE;
1003 }
danielk19777cedc8d2004-06-10 10:50:08 +00001004 db->xCollNeeded = 0;
1005 db->xCollNeeded16 = xCollNeeded16;
1006 db->pCollNeededArg = pCollNeededArg;
1007 return SQLITE_OK;
1008}
drh6c626082004-11-14 21:56:29 +00001009#endif /* SQLITE_OMIT_UTF16 */
danielk19776b456a22005-03-21 04:04:02 +00001010
1011#ifndef SQLITE_OMIT_GLOBALRECOVER
1012/*
danielk19777ddad962005-12-12 06:53:03 +00001013** This function is now an anachronism. It used to be used to recover from a
1014** malloc() failure, but SQLite now does this automatically.
danielk19776b456a22005-03-21 04:04:02 +00001015*/
1016int sqlite3_global_recover(){
danielk1977261919c2005-12-06 12:52:59 +00001017 return SQLITE_OK;
danielk19776b456a22005-03-21 04:04:02 +00001018}
1019#endif
drh3e1d8e62005-05-26 16:23:34 +00001020
1021/*
1022** Test to see whether or not the database connection is in autocommit
1023** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
1024** by default. Autocommit is disabled by a BEGIN statement and reenabled
1025** by the next COMMIT or ROLLBACK.
1026**
1027******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
1028*/
1029int sqlite3_get_autocommit(sqlite3 *db){
1030 return db->autoCommit;
1031}
drh49285702005-09-17 15:20:26 +00001032
1033#ifdef SQLITE_DEBUG
1034/*
1035** The following routine is subtituted for constant SQLITE_CORRUPT in
1036** debugging builds. This provides a way to set a breakpoint for when
1037** corruption is first detected.
1038*/
1039int sqlite3Corrupt(void){
1040 return SQLITE_CORRUPT;
1041}
1042#endif