blob: 0becf229e87c19153b32040e88ab31bceb093da5 [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**
drh3e1d8e62005-05-26 16:23:34 +000017** $Id: main.c,v 1.293 2005/05/26 16:23:34 drh Exp $
drh75897232000-05-29 14:26:00 +000018*/
19#include "sqliteInt.h"
drh8cfbf082001-09-19 13:22:39 +000020#include "os.h"
drhce9079c2002-05-15 14:17:44 +000021#include <ctype.h>
drh75897232000-05-29 14:26:00 +000022
23/*
drh9c054832004-05-31 18:51:57 +000024** The following constant value is used by the SQLITE_BIGENDIAN and
25** SQLITE_LITTLEENDIAN macros.
drhbbd42a62004-05-22 17:41:58 +000026*/
27const int sqlite3one = 1;
28
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
drh001bbcb2003-03-19 03:14:00 +0000156 for(j=0; j<db->nDb; j++){
drh4d189ca2004-02-12 18:46:38 +0000157 struct Db *pDb = &db->aDb[j];
158 if( pDb->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000159 sqlite3BtreeClose(pDb->pBt);
drh4d189ca2004-02-12 18:46:38 +0000160 pDb->pBt = 0;
drh113088e2003-03-20 01:16:58 +0000161 }
drhf57b3392001-10-08 13:22:32 +0000162 }
danielk19774adee202004-05-08 08:23:19 +0000163 sqlite3ResetInternalSchema(db, 0);
drh1c2d8412003-03-31 00:30:47 +0000164 assert( db->nDb<=2 );
165 assert( db->aDb==db->aDbStatic );
drh0bce8352002-02-28 00:41:10 +0000166 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
167 FuncDef *pFunc, *pNext;
168 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
drh8e0a2f92002-02-23 23:45:45 +0000169 pNext = pFunc->pNext;
170 sqliteFree(pFunc);
171 }
172 }
danielk1977466be562004-06-10 02:16:01 +0000173
danielk1977d8123362004-06-12 09:25:12 +0000174 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
danielk1977466be562004-06-10 02:16:01 +0000175 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
danielk1977d8123362004-06-12 09:25:12 +0000176 sqliteFree(pColl);
danielk1977466be562004-06-10 02:16:01 +0000177 }
danielk1977d8123362004-06-12 09:25:12 +0000178 sqlite3HashClear(&db->aCollSeq);
danielk1977466be562004-06-10 02:16:01 +0000179
danielk19774adee202004-05-08 08:23:19 +0000180 sqlite3HashClear(&db->aFunc);
danielk19776622cce2004-05-20 11:00:52 +0000181 sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
danielk1977bfd6cce2004-06-18 04:24:54 +0000182 if( db->pValue ){
183 sqlite3ValueFree(db->pValue);
184 }
185 if( db->pErr ){
186 sqlite3ValueFree(db->pErr);
187 }
danielk197796d81f92004-06-19 03:33:57 +0000188
danielk19776b456a22005-03-21 04:04:02 +0000189#ifndef SQLITE_OMIT_GLOBALRECOVER
190 {
danielk19773eb8db92005-03-29 23:34:58 +0000191 sqlite3 *pPrev;
danielk19776b456a22005-03-21 04:04:02 +0000192 sqlite3OsEnterMutex();
danielk19773eb8db92005-03-29 23:34:58 +0000193 pPrev = pDbList;
danielk19776b456a22005-03-21 04:04:02 +0000194 while( pPrev && pPrev->pNext!=db ){
195 pPrev = pPrev->pNext;
196 }
197 if( pPrev ){
198 pPrev->pNext = db->pNext;
199 }else{
200 assert( pDbList==db );
201 pDbList = db->pNext;
202 }
203 sqlite3OsLeaveMutex();
204 }
205#endif
206
danielk197796d81f92004-06-19 03:33:57 +0000207 db->magic = SQLITE_MAGIC_ERROR;
drh75897232000-05-29 14:26:00 +0000208 sqliteFree(db);
danielk197796d81f92004-06-19 03:33:57 +0000209 return SQLITE_OK;
drh75897232000-05-29 14:26:00 +0000210}
211
212/*
drh001bbcb2003-03-19 03:14:00 +0000213** Rollback all database files.
214*/
drh9bb575f2004-09-06 17:24:11 +0000215void sqlite3RollbackAll(sqlite3 *db){
drh001bbcb2003-03-19 03:14:00 +0000216 int i;
217 for(i=0; i<db->nDb; i++){
218 if( db->aDb[i].pBt ){
danielk19774adee202004-05-08 08:23:19 +0000219 sqlite3BtreeRollback(db->aDb[i].pBt);
drh001bbcb2003-03-19 03:14:00 +0000220 db->aDb[i].inTrans = 0;
221 }
222 }
danielk19774adee202004-05-08 08:23:19 +0000223 sqlite3ResetInternalSchema(db, 0);
drh001bbcb2003-03-19 03:14:00 +0000224}
225
226/*
drhc22bd472002-05-10 13:14:07 +0000227** Return a static string that describes the kind of error specified in the
228** argument.
drh247be432002-05-10 05:44:55 +0000229*/
danielk1977f20b21c2004-05-31 23:56:42 +0000230const char *sqlite3ErrStr(int rc){
drhc22bd472002-05-10 13:14:07 +0000231 const char *z;
232 switch( rc ){
drhc60d0442004-09-30 13:43:13 +0000233 case SQLITE_ROW:
234 case SQLITE_DONE:
drhc22bd472002-05-10 13:14:07 +0000235 case SQLITE_OK: z = "not an error"; break;
236 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
237 case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break;
238 case SQLITE_PERM: z = "access permission denied"; break;
239 case SQLITE_ABORT: z = "callback requested query abort"; break;
240 case SQLITE_BUSY: z = "database is locked"; break;
241 case SQLITE_LOCKED: z = "database table is locked"; break;
242 case SQLITE_NOMEM: z = "out of memory"; break;
243 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
244 case SQLITE_INTERRUPT: z = "interrupted"; break;
245 case SQLITE_IOERR: z = "disk I/O error"; break;
246 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
247 case SQLITE_NOTFOUND: z = "table or record not found"; break;
248 case SQLITE_FULL: z = "database is full"; break;
249 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
250 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
251 case SQLITE_EMPTY: z = "table contains no data"; break;
252 case SQLITE_SCHEMA: z = "database schema has changed"; break;
253 case SQLITE_TOOBIG: z = "too much data for one table row"; break;
254 case SQLITE_CONSTRAINT: z = "constraint failed"; break;
255 case SQLITE_MISMATCH: z = "datatype mismatch"; break;
256 case SQLITE_MISUSE: z = "library routine called out of sequence";break;
drh8766c342002-11-09 00:33:15 +0000257 case SQLITE_NOLFS: z = "kernel lacks large file support"; break;
drhed6c8672003-01-12 18:02:16 +0000258 case SQLITE_AUTH: z = "authorization denied"; break;
jplyon892f6712003-06-12 08:59:00 +0000259 case SQLITE_FORMAT: z = "auxiliary database format error"; break;
drhb08153d2004-11-20 20:18:55 +0000260 case SQLITE_RANGE: z = "bind or column index out of range"; break;
drhc602f9a2004-02-12 19:01:04 +0000261 case SQLITE_NOTADB: z = "file is encrypted or is not a database";break;
drhc22bd472002-05-10 13:14:07 +0000262 default: z = "unknown error"; break;
drh247be432002-05-10 05:44:55 +0000263 }
drhc22bd472002-05-10 13:14:07 +0000264 return z;
drh247be432002-05-10 05:44:55 +0000265}
266
267/*
drh2dfbbca2000-07-28 14:32:48 +0000268** This routine implements a busy callback that sleeps and tries
269** again until a timeout value is reached. The timeout value is
270** an integer number of milliseconds passed in as the first
271** argument.
272*/
drhdaffd0e2001-04-11 14:28:42 +0000273static int sqliteDefaultBusyCallback(
drh97903fe2005-05-24 20:19:57 +0000274 void *ptr, /* Database connection */
drh2dfbbca2000-07-28 14:32:48 +0000275 int count /* Number of times table has been busy */
276){
drh8cfbf082001-09-19 13:22:39 +0000277#if SQLITE_MIN_SLEEP_MS==1
drhee570fa2005-04-28 12:06:05 +0000278 static const u8 delays[] =
279 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
280 static const u8 totals[] =
281 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
drhd1bec472004-01-15 13:29:31 +0000282# define NDELAY (sizeof(delays)/sizeof(delays[0]))
drh97903fe2005-05-24 20:19:57 +0000283 int timeout = ((sqlite3 *)ptr)->busyTimeout;
284 int delay, prior;
drh2dfbbca2000-07-28 14:32:48 +0000285
drhee570fa2005-04-28 12:06:05 +0000286 assert( count>=0 );
287 if( count < NDELAY ){
288 delay = delays[count];
289 prior = totals[count];
drhd1bec472004-01-15 13:29:31 +0000290 }else{
291 delay = delays[NDELAY-1];
drh68cb6192005-05-06 22:05:56 +0000292 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
drh2dfbbca2000-07-28 14:32:48 +0000293 }
drhd1bec472004-01-15 13:29:31 +0000294 if( prior + delay > timeout ){
295 delay = timeout - prior;
drh2dfbbca2000-07-28 14:32:48 +0000296 if( delay<=0 ) return 0;
297 }
danielk19774adee202004-05-08 08:23:19 +0000298 sqlite3OsSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000299 return 1;
300#else
301 int timeout = (int)Timeout;
302 if( (count+1)*1000 > timeout ){
303 return 0;
304 }
danielk19774adee202004-05-08 08:23:19 +0000305 sqlite3OsSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000306 return 1;
307#endif
308}
309
310/*
311** This routine sets the busy callback for an Sqlite database to the
312** given callback function with the given argument.
313*/
danielk1977f9d64d22004-06-19 08:18:07 +0000314int sqlite3_busy_handler(
315 sqlite3 *db,
danielk19772a764eb2004-06-12 01:43:26 +0000316 int (*xBusy)(void*,int),
drh2dfbbca2000-07-28 14:32:48 +0000317 void *pArg
318){
drhc60d0442004-09-30 13:43:13 +0000319 if( sqlite3SafetyCheck(db) ){
320 return SQLITE_MISUSE;
321 }
danielk197724162fe2004-06-04 06:22:00 +0000322 db->busyHandler.xFunc = xBusy;
323 db->busyHandler.pArg = pArg;
danielk1977f9d64d22004-06-19 08:18:07 +0000324 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000325}
326
danielk1977348bb5d2003-10-18 09:37:26 +0000327#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
328/*
329** This routine sets the progress callback for an Sqlite database to the
330** given callback function with the given argument. The progress callback will
331** be invoked every nOps opcodes.
332*/
danielk197724b03fd2004-05-10 10:34:34 +0000333void sqlite3_progress_handler(
drh9bb575f2004-09-06 17:24:11 +0000334 sqlite3 *db,
danielk1977348bb5d2003-10-18 09:37:26 +0000335 int nOps,
336 int (*xProgress)(void*),
337 void *pArg
338){
drhc60d0442004-09-30 13:43:13 +0000339 if( !sqlite3SafetyCheck(db) ){
340 if( nOps>0 ){
341 db->xProgress = xProgress;
342 db->nProgressOps = nOps;
343 db->pProgressArg = pArg;
344 }else{
345 db->xProgress = 0;
346 db->nProgressOps = 0;
347 db->pProgressArg = 0;
348 }
danielk1977348bb5d2003-10-18 09:37:26 +0000349 }
350}
351#endif
352
353
drh2dfbbca2000-07-28 14:32:48 +0000354/*
355** This routine installs a default busy handler that waits for the
356** specified number of milliseconds before returning 0.
357*/
danielk1977f9d64d22004-06-19 08:18:07 +0000358int sqlite3_busy_timeout(sqlite3 *db, int ms){
drh2dfbbca2000-07-28 14:32:48 +0000359 if( ms>0 ){
drh97903fe2005-05-24 20:19:57 +0000360 db->busyTimeout = ms;
361 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
drh2dfbbca2000-07-28 14:32:48 +0000362 }else{
danielk197724b03fd2004-05-10 10:34:34 +0000363 sqlite3_busy_handler(db, 0, 0);
drh2dfbbca2000-07-28 14:32:48 +0000364 }
danielk1977f9d64d22004-06-19 08:18:07 +0000365 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000366}
drh4c504392000-10-16 22:06:40 +0000367
368/*
369** Cause any pending operation to stop at its earliest opportunity.
370*/
drh9bb575f2004-09-06 17:24:11 +0000371void sqlite3_interrupt(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000372 if( !sqlite3SafetyCheck(db) ){
373 db->flags |= SQLITE_Interrupt;
374 }
drh4c504392000-10-16 22:06:40 +0000375}
drhfa86c412002-02-02 15:01:15 +0000376
377/*
378** Windows systems should call this routine to free memory that
danielk197724b03fd2004-05-10 10:34:34 +0000379** is returned in the in the errmsg parameter of sqlite3_open() when
drhfa86c412002-02-02 15:01:15 +0000380** SQLite is a DLL. For some reason, it does not work to call free()
381** directly.
382**
drhae29ffb2004-09-25 14:39:18 +0000383** Note that we need to call free() not sqliteFree() here.
drhfa86c412002-02-02 15:01:15 +0000384*/
drh3f4fedb2004-05-31 19:34:33 +0000385void sqlite3_free(char *p){ free(p); }
drhfa86c412002-02-02 15:01:15 +0000386
387/*
drhdf014892004-06-02 00:41:09 +0000388** Create new user functions.
drhfa86c412002-02-02 15:01:15 +0000389*/
danielk197724b03fd2004-05-10 10:34:34 +0000390int sqlite3_create_function(
danielk197765904932004-05-26 06:18:37 +0000391 sqlite3 *db,
392 const char *zFunctionName,
393 int nArg,
danielk1977d8123362004-06-12 09:25:12 +0000394 int enc,
danielk197765904932004-05-26 06:18:37 +0000395 void *pUserData,
396 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
397 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
398 void (*xFinal)(sqlite3_context*)
drh8e0a2f92002-02-23 23:45:45 +0000399){
drh0bce8352002-02-28 00:41:10 +0000400 FuncDef *p;
drh4b59ab52002-08-24 18:24:51 +0000401 int nName;
danielk197765904932004-05-26 06:18:37 +0000402
drhc60d0442004-09-30 13:43:13 +0000403 if( sqlite3SafetyCheck(db) ){
404 return SQLITE_MISUSE;
405 }
406 if( zFunctionName==0 ||
danielk1977398eae72004-05-26 06:58:43 +0000407 (xFunc && (xFinal || xStep)) ||
408 (!xFunc && (xFinal && !xStep)) ||
409 (!xFunc && (!xFinal && xStep)) ||
410 (nArg<-1 || nArg>127) ||
411 (255<(nName = strlen(zFunctionName))) ){
danielk197765904932004-05-26 06:18:37 +0000412 return SQLITE_ERROR;
413 }
danielk1977d8123362004-06-12 09:25:12 +0000414
danielk197793758c82005-01-21 08:13:14 +0000415#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +0000416 /* If SQLITE_UTF16 is specified as the encoding type, transform this
417 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
418 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
419 **
420 ** If SQLITE_ANY is specified, add three versions of the function
421 ** to the hash table.
422 */
423 if( enc==SQLITE_UTF16 ){
424 enc = SQLITE_UTF16NATIVE;
425 }else if( enc==SQLITE_ANY ){
426 int rc;
427 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF8,
danielk1977f9d64d22004-06-19 08:18:07 +0000428 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000429 if( rc!=SQLITE_OK ) return rc;
430 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF16LE,
danielk1977f9d64d22004-06-19 08:18:07 +0000431 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000432 if( rc!=SQLITE_OK ) return rc;
433 enc = SQLITE_UTF16BE;
434 }
danielk197793758c82005-01-21 08:13:14 +0000435#else
436 enc = SQLITE_UTF8;
437#endif
danielk19779636c4e2005-01-25 04:27:54 +0000438
439 /* Check if an existing function is being overridden or deleted. If so,
440 ** and there are active VMs, then return SQLITE_BUSY. If a function
441 ** is being overridden/deleted but there are no active VMs, allow the
442 ** operation to continue but invalidate all precompiled statements.
443 */
444 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0);
445 if( p && p->iPrefEnc==enc && p->nArg==nArg ){
446 if( db->activeVdbeCnt ){
447 sqlite3Error(db, SQLITE_BUSY,
448 "Unable to delete/modify user-function due to active statements");
449 return SQLITE_BUSY;
450 }else{
451 sqlite3ExpirePreparedStatements(db);
452 }
453 }
danielk197765904932004-05-26 06:18:37 +0000454
danielk1977d8123362004-06-12 09:25:12 +0000455 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);
danielk197713073932004-06-30 11:54:06 +0000456 if( p==0 ) return SQLITE_NOMEM;
drh8e0a2f92002-02-23 23:45:45 +0000457 p->xFunc = xFunc;
drh8e0a2f92002-02-23 23:45:45 +0000458 p->xStep = xStep;
danielk197765904932004-05-26 06:18:37 +0000459 p->xFinalize = xFinal;
drh1350b032002-02-27 19:00:20 +0000460 p->pUserData = pUserData;
danielk197765904932004-05-26 06:18:37 +0000461 return SQLITE_OK;
462}
danielk197793758c82005-01-21 08:13:14 +0000463#ifndef SQLITE_OMIT_UTF16
danielk197765904932004-05-26 06:18:37 +0000464int sqlite3_create_function16(
465 sqlite3 *db,
466 const void *zFunctionName,
467 int nArg,
468 int eTextRep,
danielk197765904932004-05-26 06:18:37 +0000469 void *pUserData,
470 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
471 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
472 void (*xFinal)(sqlite3_context*)
473){
474 int rc;
danielk1977bfd6cce2004-06-18 04:24:54 +0000475 char const *zFunc8;
drhc60d0442004-09-30 13:43:13 +0000476 sqlite3_value *pTmp;
danielk1977bfd6cce2004-06-18 04:24:54 +0000477
drhc60d0442004-09-30 13:43:13 +0000478 if( sqlite3SafetyCheck(db) ){
479 return SQLITE_MISUSE;
480 }
481 pTmp = sqlite3GetTransientValue(db);
danielk1977bfd6cce2004-06-18 04:24:54 +0000482 sqlite3ValueSetStr(pTmp, -1, zFunctionName, SQLITE_UTF16NATIVE,SQLITE_STATIC);
483 zFunc8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
484
485 if( !zFunc8 ){
danielk197765904932004-05-26 06:18:37 +0000486 return SQLITE_NOMEM;
487 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000488 rc = sqlite3_create_function(db, zFunc8, nArg, eTextRep,
danielk1977f9d64d22004-06-19 08:18:07 +0000489 pUserData, xFunc, xStep, xFinal);
danielk197765904932004-05-26 06:18:37 +0000490 return rc;
drh8e0a2f92002-02-23 23:45:45 +0000491}
danielk197793758c82005-01-21 08:13:14 +0000492#endif
drhc9b84a12002-06-20 11:36:48 +0000493
494/*
drh18de4822003-01-16 16:28:53 +0000495** Register a trace function. The pArg from the previously registered trace
496** is returned.
497**
498** A NULL trace function means that no tracing is executes. A non-NULL
499** trace is a pointer to a function that is invoked at the start of each
danielk197724b03fd2004-05-10 10:34:34 +0000500** sqlite3_exec().
drh18de4822003-01-16 16:28:53 +0000501*/
drh9bb575f2004-09-06 17:24:11 +0000502void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
drh18de4822003-01-16 16:28:53 +0000503 void *pOld = db->pTraceArg;
504 db->xTrace = xTrace;
505 db->pTraceArg = pArg;
506 return pOld;
drh0d1a6432003-04-03 15:46:04 +0000507}
paulb0208cc2003-04-13 18:26:49 +0000508
drhaa940ea2004-01-15 02:44:03 +0000509/*** EXPERIMENTAL ***
510**
511** Register a function to be invoked when a transaction comments.
512** If either function returns non-zero, then the commit becomes a
513** rollback.
514*/
danielk197724b03fd2004-05-10 10:34:34 +0000515void *sqlite3_commit_hook(
drh9bb575f2004-09-06 17:24:11 +0000516 sqlite3 *db, /* Attach the hook to this database */
drhaa940ea2004-01-15 02:44:03 +0000517 int (*xCallback)(void*), /* Function to invoke on each commit */
518 void *pArg /* Argument to the function */
519){
520 void *pOld = db->pCommitArg;
521 db->xCommitCallback = xCallback;
522 db->pCommitArg = pArg;
523 return pOld;
524}
525
526
paulb0208cc2003-04-13 18:26:49 +0000527/*
drh13bff812003-04-15 01:19:47 +0000528** This routine is called to create a connection to a database BTree
529** driver. If zFilename is the name of a file, then that file is
530** opened and used. If zFilename is the magic name ":memory:" then
531** the database is stored in memory (and is thus forgotten as soon as
532** the connection is closed.) If zFilename is NULL then the database
533** is for temporary use only and is deleted as soon as the connection
534** is closed.
drh90f5ecb2004-07-22 01:19:35 +0000535**
536** A temporary database can be either a disk file (that is automatically
537** deleted when the file is closed) or a set of red-black trees held in memory,
538** depending on the values of the TEMP_STORE compile-time macro and the
539** db->temp_store variable, according to the following chart:
540**
541** TEMP_STORE db->temp_store Location of temporary database
542** ---------- -------------- ------------------------------
543** 0 any file
544** 1 1 file
545** 1 2 memory
546** 1 0 file
547** 2 1 file
548** 2 2 memory
549** 2 0 memory
550** 3 any memory
paulb0208cc2003-04-13 18:26:49 +0000551*/
danielk19774adee202004-05-08 08:23:19 +0000552int sqlite3BtreeFactory(
drh9bb575f2004-09-06 17:24:11 +0000553 const sqlite3 *db, /* Main database when opening aux otherwise 0 */
paulb0208cc2003-04-13 18:26:49 +0000554 const char *zFilename, /* Name of the file containing the BTree database */
555 int omitJournal, /* if TRUE then do not journal this file */
556 int nCache, /* How many pages in the page cache */
danielk19774adee202004-05-08 08:23:19 +0000557 Btree **ppBtree /* Pointer to new Btree object written here */
558){
danielk19774adee202004-05-08 08:23:19 +0000559 int btree_flags = 0;
drh90f5ecb2004-07-22 01:19:35 +0000560 int rc;
danielk19774adee202004-05-08 08:23:19 +0000561
drheec983e2004-05-08 10:11:36 +0000562 assert( ppBtree != 0);
danielk19774adee202004-05-08 08:23:19 +0000563 if( omitJournal ){
564 btree_flags |= BTREE_OMIT_JOURNAL;
paulb0208cc2003-04-13 18:26:49 +0000565 }
drh7bec5052005-02-06 02:45:41 +0000566 if( db->flags & SQLITE_NoReadlock ){
567 btree_flags |= BTREE_NO_READLOCK;
568 }
drh90f5ecb2004-07-22 01:19:35 +0000569 if( zFilename==0 ){
drh90f5ecb2004-07-22 01:19:35 +0000570#if TEMP_STORE==0
drh22ac46d2004-08-14 18:34:54 +0000571 /* Do nothing */
drh90f5ecb2004-07-22 01:19:35 +0000572#endif
danielk197703aded42004-11-22 05:26:27 +0000573#ifndef SQLITE_OMIT_MEMORYDB
drh90f5ecb2004-07-22 01:19:35 +0000574#if TEMP_STORE==1
drh22ac46d2004-08-14 18:34:54 +0000575 if( db->temp_store==2 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000576#endif
577#if TEMP_STORE==2
drh22ac46d2004-08-14 18:34:54 +0000578 if( db->temp_store!=1 ) zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000579#endif
580#if TEMP_STORE==3
drh22ac46d2004-08-14 18:34:54 +0000581 zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +0000582#endif
danielk197703aded42004-11-22 05:26:27 +0000583#endif /* SQLITE_OMIT_MEMORYDB */
drh90f5ecb2004-07-22 01:19:35 +0000584 }
danielk19774adee202004-05-08 08:23:19 +0000585
drh90f5ecb2004-07-22 01:19:35 +0000586 rc = sqlite3BtreeOpen(zFilename, ppBtree, btree_flags);
587 if( rc==SQLITE_OK ){
588 sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler);
589 sqlite3BtreeSetCacheSize(*ppBtree, nCache);
590 }
591 return rc;
paulb0208cc2003-04-13 18:26:49 +0000592}
danielk19774adee202004-05-08 08:23:19 +0000593
danielk19774ad17132004-05-21 01:47:26 +0000594/*
595** Return UTF-8 encoded English language explanation of the most recent
596** error.
597*/
danielk19776622cce2004-05-20 11:00:52 +0000598const char *sqlite3_errmsg(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000599 const char *z;
600 if( sqlite3_malloc_failed ){
danielk1977f20b21c2004-05-31 23:56:42 +0000601 return sqlite3ErrStr(SQLITE_NOMEM);
danielk19774ad17132004-05-21 01:47:26 +0000602 }
drhc60d0442004-09-30 13:43:13 +0000603 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
danielk1977e35ee192004-06-26 09:50:11 +0000604 return sqlite3ErrStr(SQLITE_MISUSE);
605 }
drhc60d0442004-09-30 13:43:13 +0000606 z = sqlite3_value_text(db->pErr);
607 if( z==0 ){
608 z = sqlite3ErrStr(db->errCode);
danielk19776622cce2004-05-20 11:00:52 +0000609 }
drhc60d0442004-09-30 13:43:13 +0000610 return z;
danielk19776622cce2004-05-20 11:00:52 +0000611}
612
drh6c626082004-11-14 21:56:29 +0000613#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +0000614/*
615** Return UTF-16 encoded English language explanation of the most recent
616** error.
617*/
danielk19776622cce2004-05-20 11:00:52 +0000618const void *sqlite3_errmsg16(sqlite3 *db){
danielk1977bfd6cce2004-06-18 04:24:54 +0000619 /* Because all the characters in the string are in the unicode
620 ** range 0x00-0xFF, if we pad the big-endian string with a
621 ** zero byte, we can obtain the little-endian string with
622 ** &big_endian[1].
623 */
drh57196282004-10-06 15:41:16 +0000624 static const char outOfMemBe[] = {
danielk1977bfd6cce2004-06-18 04:24:54 +0000625 0, 'o', 0, 'u', 0, 't', 0, ' ',
626 0, 'o', 0, 'f', 0, ' ',
627 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0
628 };
drh57196282004-10-06 15:41:16 +0000629 static const char misuseBe [] = {
danielk1977e35ee192004-06-26 09:50:11 +0000630 0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ',
631 0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ',
632 0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ',
633 0, 'o', 0, 'u', 0, 't', 0, ' ',
634 0, 'o', 0, 'f', 0, ' ',
635 0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0
636 };
danielk19774ad17132004-05-21 01:47:26 +0000637
drhc60d0442004-09-30 13:43:13 +0000638 const void *z;
639 if( sqlite3_malloc_failed ){
640 return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
641 }
642 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
643 return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
644 }
645 z = sqlite3_value_text16(db->pErr);
646 if( z==0 ){
647 sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
648 SQLITE_UTF8, SQLITE_STATIC);
649 z = sqlite3_value_text16(db->pErr);
650 }
651 return z;
danielk19776622cce2004-05-20 11:00:52 +0000652}
drh6c626082004-11-14 21:56:29 +0000653#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +0000654
drhc60d0442004-09-30 13:43:13 +0000655/*
656** Return the most recent error code generated by an SQLite routine.
657*/
danielk19776622cce2004-05-20 11:00:52 +0000658int sqlite3_errcode(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000659 if( sqlite3_malloc_failed ){
660 return SQLITE_NOMEM;
661 }
662 if( sqlite3SafetyCheck(db) ){
663 return SQLITE_MISUSE;
664 }
danielk19776622cce2004-05-20 11:00:52 +0000665 return db->errCode;
666}
667
668/*
danielk19774ad17132004-05-21 01:47:26 +0000669** This routine does the work of opening a database on behalf of
670** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
drhee570fa2005-04-28 12:06:05 +0000671** is UTF-8 encoded.
danielk19774ad17132004-05-21 01:47:26 +0000672*/
673static int openDatabase(
674 const char *zFilename, /* Database filename UTF-8 encoded */
danielk19778e227872004-06-07 07:52:17 +0000675 sqlite3 **ppDb /* OUT: Returned database handle */
danielk19774ad17132004-05-21 01:47:26 +0000676){
677 sqlite3 *db;
678 int rc, i;
danielk19774ad17132004-05-21 01:47:26 +0000679
680 /* Allocate the sqlite data structure */
drh9bb575f2004-09-06 17:24:11 +0000681 db = sqliteMalloc( sizeof(sqlite3) );
danielk19774ad17132004-05-21 01:47:26 +0000682 if( db==0 ) goto opendb_out;
danielk19774ad17132004-05-21 01:47:26 +0000683 db->priorNewRowid = 0;
684 db->magic = SQLITE_MAGIC_BUSY;
685 db->nDb = 2;
686 db->aDb = db->aDbStatic;
danielk1977dc8453f2004-06-12 00:42:34 +0000687 db->enc = SQLITE_UTF8;
danielk19771d850a72004-05-31 08:26:49 +0000688 db->autoCommit = 1;
drh47a6db22005-01-18 16:02:40 +0000689 db->flags |= SQLITE_ShortColNames;
drhf9b596e2004-05-26 16:54:42 +0000690 sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
danielk19774ad17132004-05-21 01:47:26 +0000691 sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
692 for(i=0; i<db->nDb; i++){
693 sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
694 sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
695 sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);
696 sqlite3HashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1);
697 }
danielk19774ad17132004-05-21 01:47:26 +0000698
danielk19770202b292004-06-09 09:55:16 +0000699 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
700 ** and UTF-16, so add a version for each to avoid any unnecessary
701 ** conversions. The only error that can occur here is a malloc() failure.
702 */
danielk19772c336542005-01-13 02:14:23 +0000703 if( sqlite3_create_collation(db, "BINARY", SQLITE_UTF8, 0,binCollFunc) ||
704 sqlite3_create_collation(db, "BINARY", SQLITE_UTF16, 0,binCollFunc) ||
705 !(db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0)) ){
danielk19770202b292004-06-09 09:55:16 +0000706 rc = db->errCode;
707 assert( rc!=SQLITE_OK );
708 db->magic = SQLITE_MAGIC_CLOSED;
709 goto opendb_out;
710 }
711
712 /* Also add a UTF-8 case-insensitive collation sequence. */
danielk1977466be562004-06-10 02:16:01 +0000713 sqlite3_create_collation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc);
danielk19770202b292004-06-09 09:55:16 +0000714
danielk19774ad17132004-05-21 01:47:26 +0000715 /* Open the backend database driver */
danielk19774ad17132004-05-21 01:47:26 +0000716 rc = sqlite3BtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt);
717 if( rc!=SQLITE_OK ){
danielk19774ad17132004-05-21 01:47:26 +0000718 sqlite3Error(db, rc, 0);
719 db->magic = SQLITE_MAGIC_CLOSED;
720 goto opendb_out;
721 }
danielk19774ad17132004-05-21 01:47:26 +0000722
danielk197753c0f742005-03-29 03:10:59 +0000723 /* The default safety_level for the main database is 'full'; for the temp
724 ** database it is 'NONE'. This matches the pager layer defaults.
725 */
726 db->aDb[0].zName = "main";
danielk197791cf71b2004-06-26 06:37:06 +0000727 db->aDb[0].safety_level = 3;
danielk197753c0f742005-03-29 03:10:59 +0000728#ifndef SQLITE_OMIT_TEMPDB
729 db->aDb[1].zName = "temp";
danielk197791cf71b2004-06-26 06:37:06 +0000730 db->aDb[1].safety_level = 1;
danielk197753c0f742005-03-29 03:10:59 +0000731#endif
732
danielk197791cf71b2004-06-26 06:37:06 +0000733
danielk19778e227872004-06-07 07:52:17 +0000734 /* Register all built-in functions, but do not attempt to read the
735 ** database schema yet. This is delayed until the first time the database
736 ** is accessed.
737 */
danielk19774ad17132004-05-21 01:47:26 +0000738 sqlite3RegisterBuiltinFunctions(db);
danielk19774397de52005-01-12 12:44:03 +0000739 sqlite3Error(db, SQLITE_OK, 0);
740 db->magic = SQLITE_MAGIC_OPEN;
danielk19774ad17132004-05-21 01:47:26 +0000741
742opendb_out:
danielk197713073932004-06-30 11:54:06 +0000743 if( sqlite3_errcode(db)==SQLITE_OK && sqlite3_malloc_failed ){
744 sqlite3Error(db, SQLITE_NOMEM, 0);
745 }
danielk19774ad17132004-05-21 01:47:26 +0000746 *ppDb = db;
danielk19776b456a22005-03-21 04:04:02 +0000747#ifndef SQLITE_OMIT_GLOBALRECOVER
748 if( db ){
749 sqlite3OsEnterMutex();
750 db->pNext = pDbList;
751 pDbList = db;
752 sqlite3OsLeaveMutex();
753 }
754#endif
danielk19774ad17132004-05-21 01:47:26 +0000755 return sqlite3_errcode(db);
756}
757
758/*
759** Open a new database handle.
760*/
danielk197780290862004-05-22 09:21:21 +0000761int sqlite3_open(
danielk19774ad17132004-05-21 01:47:26 +0000762 const char *zFilename,
danielk19774f057f92004-06-08 00:02:33 +0000763 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +0000764){
danielk19778e227872004-06-07 07:52:17 +0000765 return openDatabase(zFilename, ppDb);
danielk197783ab5a82004-05-21 11:39:05 +0000766}
767
drh6c626082004-11-14 21:56:29 +0000768#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +0000769/*
770** Open a new database handle.
771*/
772int sqlite3_open16(
773 const void *zFilename,
danielk19774f057f92004-06-08 00:02:33 +0000774 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +0000775){
danielk1977bfd6cce2004-06-18 04:24:54 +0000776 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
777 int rc = SQLITE_NOMEM;
778 sqlite3_value *pVal;
danielk19774ad17132004-05-21 01:47:26 +0000779
780 assert( ppDb );
danielk1977bfd6cce2004-06-18 04:24:54 +0000781 *ppDb = 0;
782 pVal = sqlite3ValueNew();
783 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
784 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
785 if( zFilename8 ){
786 rc = openDatabase(zFilename8, ppDb);
787 if( rc==SQLITE_OK && *ppDb ){
788 sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
789 }
danielk19774ad17132004-05-21 01:47:26 +0000790 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000791 if( pVal ){
792 sqlite3ValueFree(pVal);
danielk19774ad17132004-05-21 01:47:26 +0000793 }
danielk19778e227872004-06-07 07:52:17 +0000794
danielk19774ad17132004-05-21 01:47:26 +0000795 return rc;
796}
drh6c626082004-11-14 21:56:29 +0000797#endif /* SQLITE_OMIT_UTF16 */
danielk19774ad17132004-05-21 01:47:26 +0000798
danielk1977106bb232004-05-21 10:08:53 +0000799/*
800** The following routine destroys a virtual machine that is created by
801** the sqlite3_compile() routine. The integer returned is an SQLITE_
802** success/failure code that describes the result of executing the virtual
803** machine.
804**
805** This routine sets the error code and string returned by
806** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
807*/
danielk1977fc57d7b2004-05-26 02:04:57 +0000808int sqlite3_finalize(sqlite3_stmt *pStmt){
drh1bcdb0c2004-08-28 14:49:46 +0000809 int rc;
810 if( pStmt==0 ){
811 rc = SQLITE_OK;
812 }else{
813 rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
814 }
815 return rc;
danielk1977106bb232004-05-21 10:08:53 +0000816}
817
818/*
819** Terminate the current execution of an SQL statement and reset it
820** back to its starting state so that it can be reused. A success code from
821** the prior execution is returned.
822**
823** This routine sets the error code and string returned by
824** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
825*/
danielk1977fc57d7b2004-05-26 02:04:57 +0000826int sqlite3_reset(sqlite3_stmt *pStmt){
drh1bcdb0c2004-08-28 14:49:46 +0000827 int rc;
828 if( pStmt==0 ){
829 rc = SQLITE_OK;
830 }else{
831 rc = sqlite3VdbeReset((Vdbe*)pStmt);
danielk1977b3bce662005-01-29 08:32:43 +0000832 sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0, 0, 0, 0);
drh1bcdb0c2004-08-28 14:49:46 +0000833 }
danielk1977106bb232004-05-21 10:08:53 +0000834 return rc;
835}
danielk19770202b292004-06-09 09:55:16 +0000836
danielk1977d8123362004-06-12 09:25:12 +0000837/*
838** Register a new collation sequence with the database handle db.
839*/
danielk19770202b292004-06-09 09:55:16 +0000840int sqlite3_create_collation(
841 sqlite3* db,
842 const char *zName,
danielk1977466be562004-06-10 02:16:01 +0000843 int enc,
danielk19770202b292004-06-09 09:55:16 +0000844 void* pCtx,
845 int(*xCompare)(void*,int,const void*,int,const void*)
846){
847 CollSeq *pColl;
848 int rc = SQLITE_OK;
danielk1977d8123362004-06-12 09:25:12 +0000849
drhc60d0442004-09-30 13:43:13 +0000850 if( sqlite3SafetyCheck(db) ){
851 return SQLITE_MISUSE;
852 }
853
danielk1977d8123362004-06-12 09:25:12 +0000854 /* If SQLITE_UTF16 is specified as the encoding type, transform this
855 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
856 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
857 */
858 if( enc==SQLITE_UTF16 ){
859 enc = SQLITE_UTF16NATIVE;
860 }
861
danielk1977466be562004-06-10 02:16:01 +0000862 if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16LE && enc!=SQLITE_UTF16BE ){
863 sqlite3Error(db, SQLITE_ERROR,
864 "Param 3 to sqlite3_create_collation() must be one of "
danielk1977d8123362004-06-12 09:25:12 +0000865 "SQLITE_UTF8, SQLITE_UTF16, SQLITE_UTF16LE or SQLITE_UTF16BE"
danielk1977466be562004-06-10 02:16:01 +0000866 );
867 return SQLITE_ERROR;
868 }
danielk1977a21c6b62005-01-24 10:25:59 +0000869
danielk19779636c4e2005-01-25 04:27:54 +0000870 /* Check if this call is removing or replacing an existing collation
871 ** sequence. If so, and there are active VMs, return busy. If there
872 ** are no active VMs, invalidate any pre-compiled statements.
danielk1977a21c6b62005-01-24 10:25:59 +0000873 */
danielk19779636c4e2005-01-25 04:27:54 +0000874 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 0);
875 if( pColl && pColl->xCmp ){
876 if( db->activeVdbeCnt ){
877 sqlite3Error(db, SQLITE_BUSY,
878 "Unable to delete/modify collation sequence due to active statements");
879 return SQLITE_BUSY;
880 }
danielk1977a21c6b62005-01-24 10:25:59 +0000881 sqlite3ExpirePreparedStatements(db);
882 }
883
danielk1977466be562004-06-10 02:16:01 +0000884 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 1);
danielk19770202b292004-06-09 09:55:16 +0000885 if( 0==pColl ){
886 rc = SQLITE_NOMEM;
danielk19770202b292004-06-09 09:55:16 +0000887 }else{
888 pColl->xCmp = xCompare;
889 pColl->pUser = pCtx;
danielk1977312d6b32004-06-29 13:18:23 +0000890 pColl->enc = enc;
danielk19770202b292004-06-09 09:55:16 +0000891 }
892 sqlite3Error(db, rc, 0);
danielk1977466be562004-06-10 02:16:01 +0000893 return rc;
danielk19770202b292004-06-09 09:55:16 +0000894}
895
drh6c626082004-11-14 21:56:29 +0000896#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +0000897/*
898** Register a new collation sequence with the database handle db.
899*/
danielk19770202b292004-06-09 09:55:16 +0000900int sqlite3_create_collation16(
901 sqlite3* db,
902 const char *zName,
danielk1977466be562004-06-10 02:16:01 +0000903 int enc,
danielk19770202b292004-06-09 09:55:16 +0000904 void* pCtx,
905 int(*xCompare)(void*,int,const void*,int,const void*)
906){
danielk1977bfd6cce2004-06-18 04:24:54 +0000907 char const *zName8;
drhc60d0442004-09-30 13:43:13 +0000908 sqlite3_value *pTmp;
909 if( sqlite3SafetyCheck(db) ){
910 return SQLITE_MISUSE;
911 }
912 pTmp = sqlite3GetTransientValue(db);
danielk1977bfd6cce2004-06-18 04:24:54 +0000913 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF16NATIVE, SQLITE_STATIC);
914 zName8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
915 return sqlite3_create_collation(db, zName8, enc, pCtx, xCompare);
danielk19770202b292004-06-09 09:55:16 +0000916}
drh6c626082004-11-14 21:56:29 +0000917#endif /* SQLITE_OMIT_UTF16 */
danielk19777cedc8d2004-06-10 10:50:08 +0000918
danielk1977d8123362004-06-12 09:25:12 +0000919/*
920** Register a collation sequence factory callback with the database handle
921** db. Replace any previously installed collation sequence factory.
922*/
danielk19777cedc8d2004-06-10 10:50:08 +0000923int sqlite3_collation_needed(
924 sqlite3 *db,
925 void *pCollNeededArg,
926 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
927){
drhc60d0442004-09-30 13:43:13 +0000928 if( sqlite3SafetyCheck(db) ){
929 return SQLITE_MISUSE;
930 }
danielk19777cedc8d2004-06-10 10:50:08 +0000931 db->xCollNeeded = xCollNeeded;
932 db->xCollNeeded16 = 0;
933 db->pCollNeededArg = pCollNeededArg;
934 return SQLITE_OK;
935}
danielk1977d8123362004-06-12 09:25:12 +0000936
drh6c626082004-11-14 21:56:29 +0000937#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +0000938/*
939** Register a collation sequence factory callback with the database handle
940** db. Replace any previously installed collation sequence factory.
941*/
danielk19777cedc8d2004-06-10 10:50:08 +0000942int sqlite3_collation_needed16(
943 sqlite3 *db,
944 void *pCollNeededArg,
945 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
946){
drhc60d0442004-09-30 13:43:13 +0000947 if( sqlite3SafetyCheck(db) ){
948 return SQLITE_MISUSE;
949 }
danielk19777cedc8d2004-06-10 10:50:08 +0000950 db->xCollNeeded = 0;
951 db->xCollNeeded16 = xCollNeeded16;
952 db->pCollNeededArg = pCollNeededArg;
953 return SQLITE_OK;
954}
drh6c626082004-11-14 21:56:29 +0000955#endif /* SQLITE_OMIT_UTF16 */
danielk19776b456a22005-03-21 04:04:02 +0000956
957#ifndef SQLITE_OMIT_GLOBALRECOVER
958/*
959** This function is called to recover from a malloc failure that occured
960** within SQLite.
961**
962** This function is *not* threadsafe. Calling this from within a threaded
963** application when threads other than the caller have used SQLite is
964** dangerous and will almost certainly result in malfunctions.
965*/
966int sqlite3_global_recover(){
967 int rc = SQLITE_OK;
968
969 if( sqlite3_malloc_failed ){
970 sqlite3 *db;
971 int i;
972 sqlite3_malloc_failed = 0;
973 for(db=pDbList; db; db=db->pNext ){
974 sqlite3ExpirePreparedStatements(db);
975 for(i=0; i<db->nDb; i++){
976 Btree *pBt = db->aDb[i].pBt;
977 if( pBt && (rc=sqlite3BtreeReset(pBt)) ){
978 goto recover_out;
979 }
980 }
981 db->autoCommit = 1;
982 }
983 }
984
985recover_out:
986 if( rc!=SQLITE_OK ){
987 sqlite3_malloc_failed = 1;
988 }
989 return rc;
990}
991#endif
drh3e1d8e62005-05-26 16:23:34 +0000992
993/*
994** Test to see whether or not the database connection is in autocommit
995** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
996** by default. Autocommit is disabled by a BEGIN statement and reenabled
997** by the next COMMIT or ROLLBACK.
998**
999******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
1000*/
1001int sqlite3_get_autocommit(sqlite3 *db){
1002 return db->autoCommit;
1003}