aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/sqlite/win32/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/sqlite/win32/main.c')
-rwxr-xr-xlibraries/sqlite/win32/main.c1485
1 files changed, 1485 insertions, 0 deletions
diff --git a/libraries/sqlite/win32/main.c b/libraries/sqlite/win32/main.c
new file mode 100755
index 0000000..f61fe80
--- /dev/null
+++ b/libraries/sqlite/win32/main.c
@@ -0,0 +1,1485 @@
1/*
2** 2001 September 15
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** 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.
10**
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**
17** $Id: main.c,v 1.406 2007/10/03 21:10:58 drh Exp $
18*/
19#include "sqliteInt.h"
20#include <ctype.h>
21
22/*
23** The version of the library
24*/
25const char sqlite3_version[] = SQLITE_VERSION;
26const char *sqlite3_libversion(void){ return sqlite3_version; }
27int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
28int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
29
30/*
31** If the following function pointer is not NULL and if
32** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
33** I/O active are written using this function. These messages
34** are intended for debugging activity only.
35*/
36void (*sqlite3_io_trace)(const char*, ...) = 0;
37
38/*
39** If the following global variable points to a string which is the
40** name of a directory, then that directory will be used to store
41** temporary files.
42**
43** See also the "PRAGMA temp_store_directory" SQL command.
44*/
45char *sqlite3_temp_directory = 0;
46
47
48/*
49** This is the default collating function named "BINARY" which is always
50** available.
51*/
52static int binCollFunc(
53 void *NotUsed,
54 int nKey1, const void *pKey1,
55 int nKey2, const void *pKey2
56){
57 int rc, n;
58 n = nKey1<nKey2 ? nKey1 : nKey2;
59 rc = memcmp(pKey1, pKey2, n);
60 if( rc==0 ){
61 rc = nKey1 - nKey2;
62 }
63 return rc;
64}
65
66/*
67** Another built-in collating sequence: NOCASE.
68**
69** This collating sequence is intended to be used for "case independant
70** comparison". SQLite's knowledge of upper and lower case equivalents
71** extends only to the 26 characters used in the English language.
72**
73** At the moment there is only a UTF-8 implementation.
74*/
75static int nocaseCollatingFunc(
76 void *NotUsed,
77 int nKey1, const void *pKey1,
78 int nKey2, const void *pKey2
79){
80 int r = sqlite3StrNICmp(
81 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
82 if( 0==r ){
83 r = nKey1-nKey2;
84 }
85 return r;
86}
87
88/*
89** Return the ROWID of the most recent insert
90*/
91sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
92 return db->lastRowid;
93}
94
95/*
96** Return the number of changes in the most recent call to sqlite3_exec().
97*/
98int sqlite3_changes(sqlite3 *db){
99 return db->nChange;
100}
101
102/*
103** Return the number of changes since the database handle was opened.
104*/
105int sqlite3_total_changes(sqlite3 *db){
106 return db->nTotalChange;
107}
108
109/*
110** Close an existing SQLite database
111*/
112int sqlite3_close(sqlite3 *db){
113 HashElem *i;
114 int j;
115
116 if( !db ){
117 return SQLITE_OK;
118 }
119 if( sqlite3SafetyCheck(db) ){
120 return SQLITE_MISUSE;
121 }
122 sqlite3_mutex_enter(db->mutex);
123
124#ifdef SQLITE_SSE
125 {
126 extern void sqlite3SseCleanup(sqlite3*);
127 sqlite3SseCleanup(db);
128 }
129#endif
130
131 sqlite3ResetInternalSchema(db, 0);
132
133 /* If a transaction is open, the ResetInternalSchema() call above
134 ** will not have called the xDisconnect() method on any virtual
135 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
136 ** call will do so. We need to do this before the check for active
137 ** SQL statements below, as the v-table implementation may be storing
138 ** some prepared statements internally.
139 */
140 sqlite3VtabRollback(db);
141
142 /* If there are any outstanding VMs, return SQLITE_BUSY. */
143 if( db->pVdbe ){
144 sqlite3Error(db, SQLITE_BUSY,
145 "Unable to close due to unfinalised statements");
146 sqlite3_mutex_leave(db->mutex);
147 return SQLITE_BUSY;
148 }
149 assert( !sqlite3SafetyCheck(db) );
150
151 /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database
152 ** cannot be opened for some reason. So this routine needs to run in
153 ** that case. But maybe there should be an extra magic value for the
154 ** "failed to open" state.
155 **
156 ** TODO: Coverage tests do not test the case where this condition is
157 ** true. It's hard to see how to cause it without messing with threads.
158 */
159 if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){
160 /* printf("DID NOT CLOSE\n"); fflush(stdout); */
161 sqlite3_mutex_leave(db->mutex);
162 return SQLITE_ERROR;
163 }
164
165 for(j=0; j<db->nDb; j++){
166 struct Db *pDb = &db->aDb[j];
167 if( pDb->pBt ){
168 sqlite3BtreeClose(pDb->pBt);
169 pDb->pBt = 0;
170 if( j!=1 ){
171 pDb->pSchema = 0;
172 }
173 }
174 }
175 sqlite3ResetInternalSchema(db, 0);
176 assert( db->nDb<=2 );
177 assert( db->aDb==db->aDbStatic );
178 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
179 FuncDef *pFunc, *pNext;
180 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
181 pNext = pFunc->pNext;
182 sqlite3_free(pFunc);
183 }
184 }
185
186 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
187 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
188 /* Invoke any destructors registered for collation sequence user data. */
189 for(j=0; j<3; j++){
190 if( pColl[j].xDel ){
191 pColl[j].xDel(pColl[j].pUser);
192 }
193 }
194 sqlite3_free(pColl);
195 }
196 sqlite3HashClear(&db->aCollSeq);
197#ifndef SQLITE_OMIT_VIRTUALTABLE
198 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
199 Module *pMod = (Module *)sqliteHashData(i);
200 if( pMod->xDestroy ){
201 pMod->xDestroy(pMod->pAux);
202 }
203 sqlite3_free(pMod);
204 }
205 sqlite3HashClear(&db->aModule);
206#endif
207
208 sqlite3HashClear(&db->aFunc);
209 sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
210 if( db->pErr ){
211 sqlite3ValueFree(db->pErr);
212 }
213 sqlite3CloseExtensions(db);
214
215 db->magic = SQLITE_MAGIC_ERROR;
216
217 /* The temp-database schema is allocated differently from the other schema
218 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
219 ** So it needs to be freed here. Todo: Why not roll the temp schema into
220 ** the same sqliteMalloc() as the one that allocates the database
221 ** structure?
222 */
223 sqlite3_free(db->aDb[1].pSchema);
224 sqlite3_mutex_leave(db->mutex);
225 sqlite3_mutex_free(db->mutex);
226 sqlite3_free(db);
227 return SQLITE_OK;
228}
229
230/*
231** Rollback all database files.
232*/
233void sqlite3RollbackAll(sqlite3 *db){
234 int i;
235 int inTrans = 0;
236 assert( sqlite3_mutex_held(db->mutex) );
237 sqlite3MallocEnterBenignBlock(1); /* Enter benign region */
238 for(i=0; i<db->nDb; i++){
239 if( db->aDb[i].pBt ){
240 if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){
241 inTrans = 1;
242 }
243 sqlite3BtreeRollback(db->aDb[i].pBt);
244 db->aDb[i].inTrans = 0;
245 }
246 }
247 sqlite3VtabRollback(db);
248 sqlite3MallocLeaveBenignBlock(); /* Leave benign region */
249
250 if( db->flags&SQLITE_InternChanges ){
251 sqlite3ExpirePreparedStatements(db);
252 sqlite3ResetInternalSchema(db, 0);
253 }
254
255 /* If one has been configured, invoke the rollback-hook callback */
256 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
257 db->xRollbackCallback(db->pRollbackArg);
258 }
259}
260
261/*
262** Return a static string that describes the kind of error specified in the
263** argument.
264*/
265const char *sqlite3ErrStr(int rc){
266 const char *z;
267 switch( rc & 0xff ){
268 case SQLITE_ROW:
269 case SQLITE_DONE:
270 case SQLITE_OK: z = "not an error"; break;
271 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
272 case SQLITE_PERM: z = "access permission denied"; break;
273 case SQLITE_ABORT: z = "callback requested query abort"; break;
274 case SQLITE_BUSY: z = "database is locked"; break;
275 case SQLITE_LOCKED: z = "database table is locked"; break;
276 case SQLITE_NOMEM: z = "out of memory"; break;
277 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
278 case SQLITE_INTERRUPT: z = "interrupted"; break;
279 case SQLITE_IOERR: z = "disk I/O error"; break;
280 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
281 case SQLITE_FULL: z = "database or disk is full"; break;
282 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
283 case SQLITE_EMPTY: z = "table contains no data"; break;
284 case SQLITE_SCHEMA: z = "database schema has changed"; break;
285 case SQLITE_TOOBIG: z = "String or BLOB exceeded size limit"; break;
286 case SQLITE_CONSTRAINT: z = "constraint failed"; break;
287 case SQLITE_MISMATCH: z = "datatype mismatch"; break;
288 case SQLITE_MISUSE: z = "library routine called out of sequence";break;
289 case SQLITE_NOLFS: z = "kernel lacks large file support"; break;
290 case SQLITE_AUTH: z = "authorization denied"; break;
291 case SQLITE_FORMAT: z = "auxiliary database format error"; break;
292 case SQLITE_RANGE: z = "bind or column index out of range"; break;
293 case SQLITE_NOTADB: z = "file is encrypted or is not a database";break;
294 default: z = "unknown error"; break;
295 }
296 return z;
297}
298
299/*
300** This routine implements a busy callback that sleeps and tries
301** again until a timeout value is reached. The timeout value is
302** an integer number of milliseconds passed in as the first
303** argument.
304*/
305static int sqliteDefaultBusyCallback(
306 void *ptr, /* Database connection */
307 int count /* Number of times table has been busy */
308){
309#if OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
310 static const u8 delays[] =
311 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
312 static const u8 totals[] =
313 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
314# define NDELAY (sizeof(delays)/sizeof(delays[0]))
315 sqlite3 *db = (sqlite3 *)ptr;
316 int timeout = db->busyTimeout;
317 int delay, prior;
318
319 assert( count>=0 );
320 if( count < NDELAY ){
321 delay = delays[count];
322 prior = totals[count];
323 }else{
324 delay = delays[NDELAY-1];
325 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
326 }
327 if( prior + delay > timeout ){
328 delay = timeout - prior;
329 if( delay<=0 ) return 0;
330 }
331 sqlite3OsSleep(db->pVfs, delay*1000);
332 return 1;
333#else
334 sqlite3 *db = (sqlite3 *)ptr;
335 int timeout = ((sqlite3 *)ptr)->busyTimeout;
336 if( (count+1)*1000 > timeout ){
337 return 0;
338 }
339 sqlite3OsSleep(db->pVfs, 1000000);
340 return 1;
341#endif
342}
343
344/*
345** Invoke the given busy handler.
346**
347** This routine is called when an operation failed with a lock.
348** If this routine returns non-zero, the lock is retried. If it
349** returns 0, the operation aborts with an SQLITE_BUSY error.
350*/
351int sqlite3InvokeBusyHandler(BusyHandler *p){
352 int rc;
353 if( p==0 || p->xFunc==0 || p->nBusy<0 ) return 0;
354 rc = p->xFunc(p->pArg, p->nBusy);
355 if( rc==0 ){
356 p->nBusy = -1;
357 }else{
358 p->nBusy++;
359 }
360 return rc;
361}
362
363/*
364** This routine sets the busy callback for an Sqlite database to the
365** given callback function with the given argument.
366*/
367int sqlite3_busy_handler(
368 sqlite3 *db,
369 int (*xBusy)(void*,int),
370 void *pArg
371){
372 if( sqlite3SafetyCheck(db) ){
373 return SQLITE_MISUSE;
374 }
375 sqlite3_mutex_enter(db->mutex);
376 db->busyHandler.xFunc = xBusy;
377 db->busyHandler.pArg = pArg;
378 db->busyHandler.nBusy = 0;
379 sqlite3_mutex_leave(db->mutex);
380 return SQLITE_OK;
381}
382
383#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
384/*
385** This routine sets the progress callback for an Sqlite database to the
386** given callback function with the given argument. The progress callback will
387** be invoked every nOps opcodes.
388*/
389void sqlite3_progress_handler(
390 sqlite3 *db,
391 int nOps,
392 int (*xProgress)(void*),
393 void *pArg
394){
395 if( !sqlite3SafetyCheck(db) ){
396 sqlite3_mutex_enter(db->mutex);
397 if( nOps>0 ){
398 db->xProgress = xProgress;
399 db->nProgressOps = nOps;
400 db->pProgressArg = pArg;
401 }else{
402 db->xProgress = 0;
403 db->nProgressOps = 0;
404 db->pProgressArg = 0;
405 }
406 sqlite3_mutex_leave(db->mutex);
407 }
408}
409#endif
410
411
412/*
413** This routine installs a default busy handler that waits for the
414** specified number of milliseconds before returning 0.
415*/
416int sqlite3_busy_timeout(sqlite3 *db, int ms){
417 if( sqlite3SafetyCheck(db) ){
418 return SQLITE_MISUSE;
419 }
420 if( ms>0 ){
421 db->busyTimeout = ms;
422 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
423 }else{
424 sqlite3_busy_handler(db, 0, 0);
425 }
426 return SQLITE_OK;
427}
428
429/*
430** Cause any pending operation to stop at its earliest opportunity.
431*/
432void sqlite3_interrupt(sqlite3 *db){
433 if( db && (db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_BUSY) ){
434 db->u1.isInterrupted = 1;
435 }
436}
437
438
439/*
440** This function is exactly the same as sqlite3_create_function(), except
441** that it is designed to be called by internal code. The difference is
442** that if a malloc() fails in sqlite3_create_function(), an error code
443** is returned and the mallocFailed flag cleared.
444*/
445int sqlite3CreateFunc(
446 sqlite3 *db,
447 const char *zFunctionName,
448 int nArg,
449 int enc,
450 void *pUserData,
451 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
452 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
453 void (*xFinal)(sqlite3_context*)
454){
455 FuncDef *p;
456 int nName;
457
458 assert( sqlite3_mutex_held(db->mutex) );
459 if( sqlite3SafetyCheck(db) ){
460 return SQLITE_MISUSE;
461 }
462 if( zFunctionName==0 ||
463 (xFunc && (xFinal || xStep)) ||
464 (!xFunc && (xFinal && !xStep)) ||
465 (!xFunc && (!xFinal && xStep)) ||
466 (nArg<-1 || nArg>127) ||
467 (255<(nName = strlen(zFunctionName))) ){
468 sqlite3Error(db, SQLITE_ERROR, "bad parameters");
469 return SQLITE_ERROR;
470 }
471
472#ifndef SQLITE_OMIT_UTF16
473 /* If SQLITE_UTF16 is specified as the encoding type, transform this
474 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
475 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
476 **
477 ** If SQLITE_ANY is specified, add three versions of the function
478 ** to the hash table.
479 */
480 if( enc==SQLITE_UTF16 ){
481 enc = SQLITE_UTF16NATIVE;
482 }else if( enc==SQLITE_ANY ){
483 int rc;
484 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
485 pUserData, xFunc, xStep, xFinal);
486 if( rc==SQLITE_OK ){
487 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
488 pUserData, xFunc, xStep, xFinal);
489 }
490 if( rc!=SQLITE_OK ){
491 return rc;
492 }
493 enc = SQLITE_UTF16BE;
494 }
495#else
496 enc = SQLITE_UTF8;
497#endif
498
499 /* Check if an existing function is being overridden or deleted. If so,
500 ** and there are active VMs, then return SQLITE_BUSY. If a function
501 ** is being overridden/deleted but there are no active VMs, allow the
502 ** operation to continue but invalidate all precompiled statements.
503 */
504 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0);
505 if( p && p->iPrefEnc==enc && p->nArg==nArg ){
506 if( db->activeVdbeCnt ){
507 sqlite3Error(db, SQLITE_BUSY,
508 "Unable to delete/modify user-function due to active statements");
509 assert( !db->mallocFailed );
510 return SQLITE_BUSY;
511 }else{
512 sqlite3ExpirePreparedStatements(db);
513 }
514 }
515
516 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);
517 assert(p || db->mallocFailed);
518 if( !p ){
519 return SQLITE_NOMEM;
520 }
521 p->flags = 0;
522 p->xFunc = xFunc;
523 p->xStep = xStep;
524 p->xFinalize = xFinal;
525 p->pUserData = pUserData;
526 p->nArg = nArg;
527 return SQLITE_OK;
528}
529
530/*
531** Create new user functions.
532*/
533int sqlite3_create_function(
534 sqlite3 *db,
535 const char *zFunctionName,
536 int nArg,
537 int enc,
538 void *p,
539 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
540 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
541 void (*xFinal)(sqlite3_context*)
542){
543 int rc;
544 sqlite3_mutex_enter(db->mutex);
545 assert( !db->mallocFailed );
546 rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal);
547 rc = sqlite3ApiExit(db, rc);
548 sqlite3_mutex_leave(db->mutex);
549 return rc;
550}
551
552#ifndef SQLITE_OMIT_UTF16
553int sqlite3_create_function16(
554 sqlite3 *db,
555 const void *zFunctionName,
556 int nArg,
557 int eTextRep,
558 void *p,
559 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
560 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
561 void (*xFinal)(sqlite3_context*)
562){
563 int rc;
564 char *zFunc8;
565 sqlite3_mutex_enter(db->mutex);
566 assert( !db->mallocFailed );
567 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1);
568 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal);
569 sqlite3_free(zFunc8);
570 rc = sqlite3ApiExit(db, rc);
571 sqlite3_mutex_leave(db->mutex);
572 return rc;
573}
574#endif
575
576
577/*
578** Declare that a function has been overloaded by a virtual table.
579**
580** If the function already exists as a regular global function, then
581** this routine is a no-op. If the function does not exist, then create
582** a new one that always throws a run-time error.
583**
584** When virtual tables intend to provide an overloaded function, they
585** should call this routine to make sure the global function exists.
586** A global function must exist in order for name resolution to work
587** properly.
588*/
589int sqlite3_overload_function(
590 sqlite3 *db,
591 const char *zName,
592 int nArg
593){
594 int nName = strlen(zName);
595 int rc;
596 sqlite3_mutex_enter(db->mutex);
597 if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
598 sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
599 0, sqlite3InvalidFunction, 0, 0);
600 }
601 rc = sqlite3ApiExit(db, SQLITE_OK);
602 sqlite3_mutex_leave(db->mutex);
603 return rc;
604}
605
606#ifndef SQLITE_OMIT_TRACE
607/*
608** Register a trace function. The pArg from the previously registered trace
609** is returned.
610**
611** A NULL trace function means that no tracing is executes. A non-NULL
612** trace is a pointer to a function that is invoked at the start of each
613** SQL statement.
614*/
615void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
616 void *pOld;
617 sqlite3_mutex_enter(db->mutex);
618 pOld = db->pTraceArg;
619 db->xTrace = xTrace;
620 db->pTraceArg = pArg;
621 sqlite3_mutex_leave(db->mutex);
622 return pOld;
623}
624/*
625** Register a profile function. The pArg from the previously registered
626** profile function is returned.
627**
628** A NULL profile function means that no profiling is executes. A non-NULL
629** profile is a pointer to a function that is invoked at the conclusion of
630** each SQL statement that is run.
631*/
632void *sqlite3_profile(
633 sqlite3 *db,
634 void (*xProfile)(void*,const char*,sqlite_uint64),
635 void *pArg
636){
637 void *pOld;
638 sqlite3_mutex_enter(db->mutex);
639 pOld = db->pProfileArg;
640 db->xProfile = xProfile;
641 db->pProfileArg = pArg;
642 sqlite3_mutex_leave(db->mutex);
643 return pOld;
644}
645#endif /* SQLITE_OMIT_TRACE */
646
647/*** EXPERIMENTAL ***
648**
649** Register a function to be invoked when a transaction comments.
650** If the invoked function returns non-zero, then the commit becomes a
651** rollback.
652*/
653void *sqlite3_commit_hook(
654 sqlite3 *db, /* Attach the hook to this database */
655 int (*xCallback)(void*), /* Function to invoke on each commit */
656 void *pArg /* Argument to the function */
657){
658 void *pOld;
659 sqlite3_mutex_enter(db->mutex);
660 pOld = db->pCommitArg;
661 db->xCommitCallback = xCallback;
662 db->pCommitArg = pArg;
663 sqlite3_mutex_leave(db->mutex);
664 return pOld;
665}
666
667/*
668** Register a callback to be invoked each time a row is updated,
669** inserted or deleted using this database connection.
670*/
671void *sqlite3_update_hook(
672 sqlite3 *db, /* Attach the hook to this database */
673 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
674 void *pArg /* Argument to the function */
675){
676 void *pRet;
677 sqlite3_mutex_enter(db->mutex);
678 pRet = db->pUpdateArg;
679 db->xUpdateCallback = xCallback;
680 db->pUpdateArg = pArg;
681 sqlite3_mutex_leave(db->mutex);
682 return pRet;
683}
684
685/*
686** Register a callback to be invoked each time a transaction is rolled
687** back by this database connection.
688*/
689void *sqlite3_rollback_hook(
690 sqlite3 *db, /* Attach the hook to this database */
691 void (*xCallback)(void*), /* Callback function */
692 void *pArg /* Argument to the function */
693){
694 void *pRet;
695 sqlite3_mutex_enter(db->mutex);
696 pRet = db->pRollbackArg;
697 db->xRollbackCallback = xCallback;
698 db->pRollbackArg = pArg;
699 sqlite3_mutex_leave(db->mutex);
700 return pRet;
701}
702
703/*
704** This routine is called to create a connection to a database BTree
705** driver. If zFilename is the name of a file, then that file is
706** opened and used. If zFilename is the magic name ":memory:" then
707** the database is stored in memory (and is thus forgotten as soon as
708** the connection is closed.) If zFilename is NULL then the database
709** is a "virtual" database for transient use only and is deleted as
710** soon as the connection is closed.
711**
712** A virtual database can be either a disk file (that is automatically
713** deleted when the file is closed) or it an be held entirely in memory,
714** depending on the values of the TEMP_STORE compile-time macro and the
715** db->temp_store variable, according to the following chart:
716**
717** TEMP_STORE db->temp_store Location of temporary database
718** ---------- -------------- ------------------------------
719** 0 any file
720** 1 1 file
721** 1 2 memory
722** 1 0 file
723** 2 1 file
724** 2 2 memory
725** 2 0 memory
726** 3 any memory
727*/
728int sqlite3BtreeFactory(
729 const sqlite3 *db, /* Main database when opening aux otherwise 0 */
730 const char *zFilename, /* Name of the file containing the BTree database */
731 int omitJournal, /* if TRUE then do not journal this file */
732 int nCache, /* How many pages in the page cache */
733 int vfsFlags, /* Flags passed through to vfsOpen */
734 Btree **ppBtree /* Pointer to new Btree object written here */
735){
736 int btFlags = 0;
737 int rc;
738
739 assert( sqlite3_mutex_held(db->mutex) );
740 assert( ppBtree != 0);
741 if( omitJournal ){
742 btFlags |= BTREE_OMIT_JOURNAL;
743 }
744 if( db->flags & SQLITE_NoReadlock ){
745 btFlags |= BTREE_NO_READLOCK;
746 }
747 if( zFilename==0 ){
748#if TEMP_STORE==0
749 /* Do nothing */
750#endif
751#ifndef SQLITE_OMIT_MEMORYDB
752#if TEMP_STORE==1
753 if( db->temp_store==2 ) zFilename = ":memory:";
754#endif
755#if TEMP_STORE==2
756 if( db->temp_store!=1 ) zFilename = ":memory:";
757#endif
758#if TEMP_STORE==3
759 zFilename = ":memory:";
760#endif
761#endif /* SQLITE_OMIT_MEMORYDB */
762 }
763
764 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (zFilename==0 || *zFilename==0) ){
765 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
766 }
767 rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btFlags, vfsFlags);
768 if( rc==SQLITE_OK ){
769 sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler);
770 sqlite3BtreeSetCacheSize(*ppBtree, nCache);
771 }
772 return rc;
773}
774
775/*
776** Return UTF-8 encoded English language explanation of the most recent
777** error.
778*/
779const char *sqlite3_errmsg(sqlite3 *db){
780 const char *z;
781 if( !db ){
782 return sqlite3ErrStr(SQLITE_NOMEM);
783 }
784 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
785 return sqlite3ErrStr(SQLITE_MISUSE);
786 }
787 sqlite3_mutex_enter(db->mutex);
788 assert( !db->mallocFailed );
789 z = (char*)sqlite3_value_text(db->pErr);
790 if( z==0 ){
791 z = sqlite3ErrStr(db->errCode);
792 }
793 sqlite3_mutex_leave(db->mutex);
794 return z;
795}
796
797#ifndef SQLITE_OMIT_UTF16
798/*
799** Return UTF-16 encoded English language explanation of the most recent
800** error.
801*/
802const void *sqlite3_errmsg16(sqlite3 *db){
803 /* Because all the characters in the string are in the unicode
804 ** range 0x00-0xFF, if we pad the big-endian string with a
805 ** zero byte, we can obtain the little-endian string with
806 ** &big_endian[1].
807 */
808 static const char outOfMemBe[] = {
809 0, 'o', 0, 'u', 0, 't', 0, ' ',
810 0, 'o', 0, 'f', 0, ' ',
811 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0
812 };
813 static const char misuseBe [] = {
814 0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ',
815 0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ',
816 0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ',
817 0, 'o', 0, 'u', 0, 't', 0, ' ',
818 0, 'o', 0, 'f', 0, ' ',
819 0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0
820 };
821
822 const void *z;
823 if( !db ){
824 return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
825 }
826 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
827 return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
828 }
829 sqlite3_mutex_enter(db->mutex);
830 assert( !db->mallocFailed );
831 z = sqlite3_value_text16(db->pErr);
832 if( z==0 ){
833 sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
834 SQLITE_UTF8, SQLITE_STATIC);
835 z = sqlite3_value_text16(db->pErr);
836 }
837 sqlite3ApiExit(0, 0);
838 sqlite3_mutex_leave(db->mutex);
839 return z;
840}
841#endif /* SQLITE_OMIT_UTF16 */
842
843/*
844** Return the most recent error code generated by an SQLite routine. If NULL is
845** passed to this function, we assume a malloc() failed during sqlite3_open().
846*/
847int sqlite3_errcode(sqlite3 *db){
848 if( !db || db->mallocFailed ){
849 return SQLITE_NOMEM;
850 }
851 if( sqlite3SafetyCheck(db) ){
852 return SQLITE_MISUSE;
853 }
854 return db->errCode & db->errMask;
855}
856
857/*
858** Create a new collating function for database "db". The name is zName
859** and the encoding is enc.
860*/
861static int createCollation(
862 sqlite3* db,
863 const char *zName,
864 int enc,
865 void* pCtx,
866 int(*xCompare)(void*,int,const void*,int,const void*),
867 void(*xDel)(void*)
868){
869 CollSeq *pColl;
870 int enc2;
871
872 if( sqlite3SafetyCheck(db) ){
873 return SQLITE_MISUSE;
874 }
875 assert( sqlite3_mutex_held(db->mutex) );
876
877 /* If SQLITE_UTF16 is specified as the encoding type, transform this
878 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
879 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
880 */
881 enc2 = enc & ~SQLITE_UTF16_ALIGNED;
882 if( enc2==SQLITE_UTF16 ){
883 enc2 = SQLITE_UTF16NATIVE;
884 }
885
886 if( (enc2&~3)!=0 ){
887 sqlite3Error(db, SQLITE_ERROR, "unknown encoding");
888 return SQLITE_ERROR;
889 }
890
891 /* Check if this call is removing or replacing an existing collation
892 ** sequence. If so, and there are active VMs, return busy. If there
893 ** are no active VMs, invalidate any pre-compiled statements.
894 */
895 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, strlen(zName), 0);
896 if( pColl && pColl->xCmp ){
897 if( db->activeVdbeCnt ){
898 sqlite3Error(db, SQLITE_BUSY,
899 "Unable to delete/modify collation sequence due to active statements");
900 return SQLITE_BUSY;
901 }
902 sqlite3ExpirePreparedStatements(db);
903
904 /* If collation sequence pColl was created directly by a call to
905 ** sqlite3_create_collation, and not generated by synthCollSeq(),
906 ** then any copies made by synthCollSeq() need to be invalidated.
907 ** Also, collation destructor - CollSeq.xDel() - function may need
908 ** to be called.
909 */
910 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
911 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, strlen(zName));
912 int j;
913 for(j=0; j<3; j++){
914 CollSeq *p = &aColl[j];
915 if( p->enc==pColl->enc ){
916 if( p->xDel ){
917 p->xDel(p->pUser);
918 }
919 p->xCmp = 0;
920 }
921 }
922 }
923 }
924
925 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, strlen(zName), 1);
926 if( pColl ){
927 pColl->xCmp = xCompare;
928 pColl->pUser = pCtx;
929 pColl->xDel = xDel;
930 pColl->enc = enc2 | (enc & SQLITE_UTF16_ALIGNED);
931 }
932 sqlite3Error(db, SQLITE_OK, 0);
933 return SQLITE_OK;
934}
935
936
937/*
938** This routine does the work of opening a database on behalf of
939** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
940** is UTF-8 encoded.
941*/
942static int openDatabase(
943 const char *zFilename, /* Database filename UTF-8 encoded */
944 sqlite3 **ppDb, /* OUT: Returned database handle */
945 unsigned flags, /* Operational flags */
946 const char *zVfs /* Name of the VFS to use */
947){
948 sqlite3 *db;
949 int rc;
950 CollSeq *pColl;
951
952 /* Allocate the sqlite data structure */
953 db = sqlite3MallocZero( sizeof(sqlite3) );
954 if( db==0 ) goto opendb_out;
955 db->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
956 if( db->mutex==0 ){
957 sqlite3_free(db);
958 db = 0;
959 goto opendb_out;
960 }
961 sqlite3_mutex_enter(db->mutex);
962 db->errMask = 0xff;
963 db->priorNewRowid = 0;
964 db->nDb = 2;
965 db->magic = SQLITE_MAGIC_BUSY;
966 db->aDb = db->aDbStatic;
967 db->autoCommit = 1;
968 db->flags |= SQLITE_ShortColNames
969#if SQLITE_DEFAULT_FILE_FORMAT<4
970 | SQLITE_LegacyFileFmt
971#endif
972#ifdef SQLITE_ENABLE_LOAD_EXTENSION
973 | SQLITE_LoadExtension
974#endif
975 ;
976 sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
977 sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
978#ifndef SQLITE_OMIT_VIRTUALTABLE
979 sqlite3HashInit(&db->aModule, SQLITE_HASH_STRING, 0);
980#endif
981
982 db->pVfs = sqlite3_vfs_find(zVfs);
983 if( !db->pVfs ){
984 rc = SQLITE_ERROR;
985 db->magic = SQLITE_MAGIC_CLOSED;
986 sqlite3Error(db, rc, "no such vfs: %s", (zVfs?zVfs:"(null)"));
987 goto opendb_out;
988 }
989
990 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
991 ** and UTF-16, so add a version for each to avoid any unnecessary
992 ** conversions. The only error that can occur here is a malloc() failure.
993 */
994 if( createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0) ||
995 createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0) ||
996 createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0) ||
997 (db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0))==0
998 ){
999 assert( db->mallocFailed );
1000 db->magic = SQLITE_MAGIC_CLOSED;
1001 goto opendb_out;
1002 }
1003
1004 /* Also add a UTF-8 case-insensitive collation sequence. */
1005 createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
1006
1007 /* Set flags on the built-in collating sequences */
1008 db->pDfltColl->type = SQLITE_COLL_BINARY;
1009 pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "NOCASE", 6, 0);
1010 if( pColl ){
1011 pColl->type = SQLITE_COLL_NOCASE;
1012 }
1013
1014 /* Open the backend database driver */
1015 db->openFlags = flags;
1016 rc = sqlite3BtreeFactory(db, zFilename, 0, SQLITE_DEFAULT_CACHE_SIZE,
1017 flags | SQLITE_OPEN_MAIN_DB,
1018 &db->aDb[0].pBt);
1019 if( rc!=SQLITE_OK ){
1020 sqlite3Error(db, rc, 0);
1021 db->magic = SQLITE_MAGIC_CLOSED;
1022 goto opendb_out;
1023 }
1024 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
1025 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
1026
1027
1028 /* The default safety_level for the main database is 'full'; for the temp
1029 ** database it is 'NONE'. This matches the pager layer defaults.
1030 */
1031 db->aDb[0].zName = "main";
1032 db->aDb[0].safety_level = 3;
1033#ifndef SQLITE_OMIT_TEMPDB
1034 db->aDb[1].zName = "temp";
1035 db->aDb[1].safety_level = 1;
1036#endif
1037
1038 db->magic = SQLITE_MAGIC_OPEN;
1039 if( db->mallocFailed ){
1040 goto opendb_out;
1041 }
1042
1043 /* Register all built-in functions, but do not attempt to read the
1044 ** database schema yet. This is delayed until the first time the database
1045 ** is accessed.
1046 */
1047 sqlite3Error(db, SQLITE_OK, 0);
1048 sqlite3RegisterBuiltinFunctions(db);
1049
1050 /* Load automatic extensions - extensions that have been registered
1051 ** using the sqlite3_automatic_extension() API.
1052 */
1053 (void)sqlite3AutoLoadExtensions(db);
1054 if( sqlite3_errcode(db)!=SQLITE_OK ){
1055 goto opendb_out;
1056 }
1057
1058#ifdef SQLITE_ENABLE_FTS1
1059 if( !db->mallocFailed ){
1060 extern int sqlite3Fts1Init(sqlite3*);
1061 rc = sqlite3Fts1Init(db);
1062 }
1063#endif
1064
1065#ifdef SQLITE_ENABLE_FTS2
1066 if( !db->mallocFailed && rc==SQLITE_OK ){
1067 extern int sqlite3Fts2Init(sqlite3*);
1068 rc = sqlite3Fts2Init(db);
1069 }
1070#endif
1071
1072#ifdef SQLITE_ENABLE_FTS3
1073 if( !db->mallocFailed && rc==SQLITE_OK ){
1074 extern int sqlite3Fts3Init(sqlite3*);
1075 rc = sqlite3Fts3Init(db);
1076 }
1077#endif
1078
1079#ifdef SQLITE_ENABLE_ICU
1080 if( !db->mallocFailed && rc==SQLITE_OK ){
1081 extern int sqlite3IcuInit(sqlite3*);
1082 rc = sqlite3IcuInit(db);
1083 }
1084#endif
1085 sqlite3Error(db, rc, 0);
1086
1087 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
1088 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
1089 ** mode. Doing nothing at all also makes NORMAL the default.
1090 */
1091#ifdef SQLITE_DEFAULT_LOCKING_MODE
1092 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
1093 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
1094 SQLITE_DEFAULT_LOCKING_MODE);
1095#endif
1096
1097opendb_out:
1098 if( db && db->mutex ){
1099 sqlite3_mutex_leave(db->mutex);
1100 }
1101 if( SQLITE_NOMEM==(rc = sqlite3_errcode(db)) ){
1102 sqlite3_close(db);
1103 db = 0;
1104 }
1105 *ppDb = db;
1106 return sqlite3ApiExit(0, rc);
1107}
1108
1109/*
1110** Open a new database handle.
1111*/
1112int sqlite3_open(
1113 const char *zFilename,
1114 sqlite3 **ppDb
1115){
1116 return openDatabase(zFilename, ppDb,
1117 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
1118}
1119int sqlite3_open_v2(
1120 const char *filename, /* Database filename (UTF-8) */
1121 sqlite3 **ppDb, /* OUT: SQLite db handle */
1122 int flags, /* Flags */
1123 const char *zVfs /* Name of VFS module to use */
1124){
1125 return openDatabase(filename, ppDb, flags, zVfs);
1126}
1127
1128#ifndef SQLITE_OMIT_UTF16
1129/*
1130** Open a new database handle.
1131*/
1132int sqlite3_open16(
1133 const void *zFilename,
1134 sqlite3 **ppDb
1135){
1136 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
1137 sqlite3_value *pVal;
1138 int rc = SQLITE_NOMEM;
1139
1140 assert( zFilename );
1141 assert( ppDb );
1142 *ppDb = 0;
1143 pVal = sqlite3ValueNew(0);
1144 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1145 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
1146 if( zFilename8 ){
1147 rc = openDatabase(zFilename8, ppDb,
1148 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
1149 if( rc==SQLITE_OK && *ppDb ){
1150 rc = sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
1151 if( rc!=SQLITE_OK ){
1152 sqlite3_close(*ppDb);
1153 *ppDb = 0;
1154 }
1155 }
1156 }
1157 sqlite3ValueFree(pVal);
1158
1159 return sqlite3ApiExit(0, rc);
1160}
1161#endif /* SQLITE_OMIT_UTF16 */
1162
1163/*
1164** Register a new collation sequence with the database handle db.
1165*/
1166int sqlite3_create_collation(
1167 sqlite3* db,
1168 const char *zName,
1169 int enc,
1170 void* pCtx,
1171 int(*xCompare)(void*,int,const void*,int,const void*)
1172){
1173 int rc;
1174 sqlite3_mutex_enter(db->mutex);
1175 assert( !db->mallocFailed );
1176 rc = createCollation(db, zName, enc, pCtx, xCompare, 0);
1177 rc = sqlite3ApiExit(db, rc);
1178 sqlite3_mutex_leave(db->mutex);
1179 return rc;
1180}
1181
1182/*
1183** Register a new collation sequence with the database handle db.
1184*/
1185int sqlite3_create_collation_v2(
1186 sqlite3* db,
1187 const char *zName,
1188 int enc,
1189 void* pCtx,
1190 int(*xCompare)(void*,int,const void*,int,const void*),
1191 void(*xDel)(void*)
1192){
1193 int rc;
1194 sqlite3_mutex_enter(db->mutex);
1195 assert( !db->mallocFailed );
1196 rc = createCollation(db, zName, enc, pCtx, xCompare, xDel);
1197 rc = sqlite3ApiExit(db, rc);
1198 sqlite3_mutex_leave(db->mutex);
1199 return rc;
1200}
1201
1202#ifndef SQLITE_OMIT_UTF16
1203/*
1204** Register a new collation sequence with the database handle db.
1205*/
1206int sqlite3_create_collation16(
1207 sqlite3* db,
1208 const char *zName,
1209 int enc,
1210 void* pCtx,
1211 int(*xCompare)(void*,int,const void*,int,const void*)
1212){
1213 int rc = SQLITE_OK;
1214 char *zName8;
1215 sqlite3_mutex_enter(db->mutex);
1216 assert( !db->mallocFailed );
1217 zName8 = sqlite3Utf16to8(db, zName, -1);
1218 if( zName8 ){
1219 rc = createCollation(db, zName8, enc, pCtx, xCompare, 0);
1220 sqlite3_free(zName8);
1221 }
1222 rc = sqlite3ApiExit(db, rc);
1223 sqlite3_mutex_leave(db->mutex);
1224 return rc;
1225}
1226#endif /* SQLITE_OMIT_UTF16 */
1227
1228/*
1229** Register a collation sequence factory callback with the database handle
1230** db. Replace any previously installed collation sequence factory.
1231*/
1232int sqlite3_collation_needed(
1233 sqlite3 *db,
1234 void *pCollNeededArg,
1235 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
1236){
1237 if( sqlite3SafetyCheck(db) ){
1238 return SQLITE_MISUSE;
1239 }
1240 sqlite3_mutex_enter(db->mutex);
1241 db->xCollNeeded = xCollNeeded;
1242 db->xCollNeeded16 = 0;
1243 db->pCollNeededArg = pCollNeededArg;
1244 sqlite3_mutex_leave(db->mutex);
1245 return SQLITE_OK;
1246}
1247
1248#ifndef SQLITE_OMIT_UTF16
1249/*
1250** Register a collation sequence factory callback with the database handle
1251** db. Replace any previously installed collation sequence factory.
1252*/
1253int sqlite3_collation_needed16(
1254 sqlite3 *db,
1255 void *pCollNeededArg,
1256 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
1257){
1258 if( sqlite3SafetyCheck(db) ){
1259 return SQLITE_MISUSE;
1260 }
1261 sqlite3_mutex_enter(db->mutex);
1262 db->xCollNeeded = 0;
1263 db->xCollNeeded16 = xCollNeeded16;
1264 db->pCollNeededArg = pCollNeededArg;
1265 sqlite3_mutex_leave(db->mutex);
1266 return SQLITE_OK;
1267}
1268#endif /* SQLITE_OMIT_UTF16 */
1269
1270#ifndef SQLITE_OMIT_GLOBALRECOVER
1271/*
1272** This function is now an anachronism. It used to be used to recover from a
1273** malloc() failure, but SQLite now does this automatically.
1274*/
1275int sqlite3_global_recover(){
1276 return SQLITE_OK;
1277}
1278#endif
1279
1280/*
1281** Test to see whether or not the database connection is in autocommit
1282** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
1283** by default. Autocommit is disabled by a BEGIN statement and reenabled
1284** by the next COMMIT or ROLLBACK.
1285**
1286******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
1287*/
1288int sqlite3_get_autocommit(sqlite3 *db){
1289 return db->autoCommit;
1290}
1291
1292#ifdef SQLITE_DEBUG
1293/*
1294** The following routine is subtituted for constant SQLITE_CORRUPT in
1295** debugging builds. This provides a way to set a breakpoint for when
1296** corruption is first detected.
1297*/
1298int sqlite3Corrupt(void){
1299 return SQLITE_CORRUPT;
1300}
1301#endif
1302
1303/*
1304** This is a convenience routine that makes sure that all thread-specific
1305** data for this thread has been deallocated.
1306**
1307** SQLite no longer uses thread-specific data so this routine is now a
1308** no-op. It is retained for historical compatibility.
1309*/
1310void sqlite3_thread_cleanup(void){
1311}
1312
1313/*
1314** Return meta information about a specific column of a database table.
1315** See comment in sqlite3.h (sqlite.h.in) for details.
1316*/
1317#ifdef SQLITE_ENABLE_COLUMN_METADATA
1318int sqlite3_table_column_metadata(
1319 sqlite3 *db, /* Connection handle */
1320 const char *zDbName, /* Database name or NULL */
1321 const char *zTableName, /* Table name */
1322 const char *zColumnName, /* Column name */
1323 char const **pzDataType, /* OUTPUT: Declared data type */
1324 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
1325 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
1326 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
1327 int *pAutoinc /* OUTPUT: True if colums is auto-increment */
1328){
1329 int rc;
1330 char *zErrMsg = 0;
1331 Table *pTab = 0;
1332 Column *pCol = 0;
1333 int iCol;
1334
1335 char const *zDataType = 0;
1336 char const *zCollSeq = 0;
1337 int notnull = 0;
1338 int primarykey = 0;
1339 int autoinc = 0;
1340
1341 /* Ensure the database schema has been loaded */
1342 if( sqlite3SafetyOn(db) ){
1343 return SQLITE_MISUSE;
1344 }
1345 sqlite3_mutex_enter(db->mutex);
1346 rc = sqlite3Init(db, &zErrMsg);
1347 if( SQLITE_OK!=rc ){
1348 goto error_out;
1349 }
1350
1351 /* Locate the table in question */
1352 pTab = sqlite3FindTable(db, zTableName, zDbName);
1353 if( !pTab || pTab->pSelect ){
1354 pTab = 0;
1355 goto error_out;
1356 }
1357
1358 /* Find the column for which info is requested */
1359 if( sqlite3IsRowid(zColumnName) ){
1360 iCol = pTab->iPKey;
1361 if( iCol>=0 ){
1362 pCol = &pTab->aCol[iCol];
1363 }
1364 }else{
1365 for(iCol=0; iCol<pTab->nCol; iCol++){
1366 pCol = &pTab->aCol[iCol];
1367 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
1368 break;
1369 }
1370 }
1371 if( iCol==pTab->nCol ){
1372 pTab = 0;
1373 goto error_out;
1374 }
1375 }
1376
1377 /* The following block stores the meta information that will be returned
1378 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
1379 ** and autoinc. At this point there are two possibilities:
1380 **
1381 ** 1. The specified column name was rowid", "oid" or "_rowid_"
1382 ** and there is no explicitly declared IPK column.
1383 **
1384 ** 2. The table is not a view and the column name identified an
1385 ** explicitly declared column. Copy meta information from *pCol.
1386 */
1387 if( pCol ){
1388 zDataType = pCol->zType;
1389 zCollSeq = pCol->zColl;
1390 notnull = (pCol->notNull?1:0);
1391 primarykey = (pCol->isPrimKey?1:0);
1392 autoinc = ((pTab->iPKey==iCol && pTab->autoInc)?1:0);
1393 }else{
1394 zDataType = "INTEGER";
1395 primarykey = 1;
1396 }
1397 if( !zCollSeq ){
1398 zCollSeq = "BINARY";
1399 }
1400
1401error_out:
1402 if( sqlite3SafetyOff(db) ){
1403 rc = SQLITE_MISUSE;
1404 }
1405
1406 /* Whether the function call succeeded or failed, set the output parameters
1407 ** to whatever their local counterparts contain. If an error did occur,
1408 ** this has the effect of zeroing all output parameters.
1409 */
1410 if( pzDataType ) *pzDataType = zDataType;
1411 if( pzCollSeq ) *pzCollSeq = zCollSeq;
1412 if( pNotNull ) *pNotNull = notnull;
1413 if( pPrimaryKey ) *pPrimaryKey = primarykey;
1414 if( pAutoinc ) *pAutoinc = autoinc;
1415
1416 if( SQLITE_OK==rc && !pTab ){
1417 sqlite3SetString(&zErrMsg, "no such table column: ", zTableName, ".",
1418 zColumnName, 0);
1419 rc = SQLITE_ERROR;
1420 }
1421 sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
1422 sqlite3_free(zErrMsg);
1423 rc = sqlite3ApiExit(db, rc);
1424 sqlite3_mutex_leave(db->mutex);
1425 return rc;
1426}
1427#endif
1428
1429/*
1430** Sleep for a little while. Return the amount of time slept.
1431*/
1432int sqlite3_sleep(int ms){
1433 sqlite3_vfs *pVfs;
1434 int rc;
1435 pVfs = sqlite3_vfs_find(0);
1436
1437 /* This function works in milliseconds, but the underlying OsSleep()
1438 ** API uses microseconds. Hence the 1000's.
1439 */
1440 rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
1441 return rc;
1442}
1443
1444/*
1445** Enable or disable the extended result codes.
1446*/
1447int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
1448 sqlite3_mutex_enter(db->mutex);
1449 db->errMask = onoff ? 0xffffffff : 0xff;
1450 sqlite3_mutex_leave(db->mutex);
1451 return SQLITE_OK;
1452}
1453
1454/*
1455** Invoke the xFileControl method on a particular database.
1456*/
1457int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
1458 int rc = SQLITE_ERROR;
1459 int iDb;
1460 sqlite3_mutex_enter(db->mutex);
1461 if( zDbName==0 ){
1462 iDb = 0;
1463 }else{
1464 for(iDb=0; iDb<db->nDb; iDb++){
1465 if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break;
1466 }
1467 }
1468 if( iDb<db->nDb ){
1469 Btree *pBtree = db->aDb[iDb].pBt;
1470 if( pBtree ){
1471 Pager *pPager;
1472 sqlite3BtreeEnter(pBtree);
1473 pPager = sqlite3BtreePager(pBtree);
1474 if( pPager ){
1475 sqlite3_file *fd = sqlite3PagerFile(pPager);
1476 if( fd ){
1477 rc = sqlite3OsFileControl(fd, op, pArg);
1478 }
1479 }
1480 sqlite3BtreeLeave(pBtree);
1481 }
1482 }
1483 sqlite3_mutex_leave(db->mutex);
1484 return rc;
1485}