ds@1952: /* ds@1952: ** 2001 September 15 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** Main file for the SQLite library. The routines in this file ds@1952: ** implement the programmer interface to the library. Routines in ds@1952: ** other files are for internal use by SQLite and should not be ds@1952: ** accessed by users of the library. ds@1952: ** ds@1952: ** $Id$ ds@1952: */ ds@1952: #include "sqliteInt.h" ds@1952: #include ds@1952: ds@1952: /* ds@1952: ** The version of the library ds@1952: */ ds@1952: const char sqlite3_version[] = SQLITE_VERSION; ds@1952: const char *sqlite3_libversion(void){ return sqlite3_version; } ds@1952: int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; } ds@1952: int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; } ds@1952: ds@1952: /* ds@1952: ** If the following function pointer is not NULL and if ds@1952: ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing ds@1952: ** I/O active are written using this function. These messages ds@1952: ** are intended for debugging activity only. ds@1952: */ ds@1952: void (*sqlite3_io_trace)(const char*, ...) = 0; ds@1952: ds@1952: /* ds@1952: ** If the following global variable points to a string which is the ds@1952: ** name of a directory, then that directory will be used to store ds@1952: ** temporary files. ds@1952: ** ds@1952: ** See also the "PRAGMA temp_store_directory" SQL command. ds@1952: */ ds@1952: char *sqlite3_temp_directory = 0; ds@1952: ds@1952: ds@1952: /* ds@1952: ** This is the default collating function named "BINARY" which is always ds@1952: ** available. ds@1952: */ ds@1952: static int binCollFunc( ds@1952: void *NotUsed, ds@1952: int nKey1, const void *pKey1, ds@1952: int nKey2, const void *pKey2 ds@1952: ){ ds@1952: int rc, n; ds@1952: n = nKey1lastRowid; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the number of changes in the most recent call to sqlite3_exec(). ds@1952: */ ds@1952: int sqlite3_changes(sqlite3 *db){ ds@1952: return db->nChange; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the number of changes since the database handle was opened. ds@1952: */ ds@1952: int sqlite3_total_changes(sqlite3 *db){ ds@1952: return db->nTotalChange; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Close an existing SQLite database ds@1952: */ ds@1952: int sqlite3_close(sqlite3 *db){ ds@1952: HashElem *i; ds@1952: int j; ds@1952: ds@1952: if( !db ){ ds@1952: return SQLITE_OK; ds@1952: } ds@1952: if( sqlite3SafetyCheck(db) ){ ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: ds@1952: #ifdef SQLITE_SSE ds@1952: { ds@1952: extern void sqlite3SseCleanup(sqlite3*); ds@1952: sqlite3SseCleanup(db); ds@1952: } ds@1952: #endif ds@1952: ds@1952: sqlite3ResetInternalSchema(db, 0); ds@1952: ds@1952: /* If a transaction is open, the ResetInternalSchema() call above ds@1952: ** will not have called the xDisconnect() method on any virtual ds@1952: ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback() ds@1952: ** call will do so. We need to do this before the check for active ds@1952: ** SQL statements below, as the v-table implementation may be storing ds@1952: ** some prepared statements internally. ds@1952: */ ds@1952: sqlite3VtabRollback(db); ds@1952: ds@1952: /* If there are any outstanding VMs, return SQLITE_BUSY. */ ds@1952: if( db->pVdbe ){ ds@1952: sqlite3Error(db, SQLITE_BUSY, ds@1952: "Unable to close due to unfinalised statements"); ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return SQLITE_BUSY; ds@1952: } ds@1952: assert( !sqlite3SafetyCheck(db) ); ds@1952: ds@1952: /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database ds@1952: ** cannot be opened for some reason. So this routine needs to run in ds@1952: ** that case. But maybe there should be an extra magic value for the ds@1952: ** "failed to open" state. ds@1952: ** ds@1952: ** TODO: Coverage tests do not test the case where this condition is ds@1952: ** true. It's hard to see how to cause it without messing with threads. ds@1952: */ ds@1952: if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){ ds@1952: /* printf("DID NOT CLOSE\n"); fflush(stdout); */ ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return SQLITE_ERROR; ds@1952: } ds@1952: ds@1952: for(j=0; jnDb; j++){ ds@1952: struct Db *pDb = &db->aDb[j]; ds@1952: if( pDb->pBt ){ ds@1952: sqlite3BtreeClose(pDb->pBt); ds@1952: pDb->pBt = 0; ds@1952: if( j!=1 ){ ds@1952: pDb->pSchema = 0; ds@1952: } ds@1952: } ds@1952: } ds@1952: sqlite3ResetInternalSchema(db, 0); ds@1952: assert( db->nDb<=2 ); ds@1952: assert( db->aDb==db->aDbStatic ); ds@1952: for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){ ds@1952: FuncDef *pFunc, *pNext; ds@1952: for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){ ds@1952: pNext = pFunc->pNext; ds@1952: sqlite3_free(pFunc); ds@1952: } ds@1952: } ds@1952: ds@1952: for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){ ds@1952: CollSeq *pColl = (CollSeq *)sqliteHashData(i); ds@1952: /* Invoke any destructors registered for collation sequence user data. */ ds@1952: for(j=0; j<3; j++){ ds@1952: if( pColl[j].xDel ){ ds@1952: pColl[j].xDel(pColl[j].pUser); ds@1952: } ds@1952: } ds@1952: sqlite3_free(pColl); ds@1952: } ds@1952: sqlite3HashClear(&db->aCollSeq); ds@1952: #ifndef SQLITE_OMIT_VIRTUALTABLE ds@1952: for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){ ds@1952: Module *pMod = (Module *)sqliteHashData(i); ds@1952: if( pMod->xDestroy ){ ds@1952: pMod->xDestroy(pMod->pAux); ds@1952: } ds@1952: sqlite3_free(pMod); ds@1952: } ds@1952: sqlite3HashClear(&db->aModule); ds@1952: #endif ds@1952: ds@1952: sqlite3HashClear(&db->aFunc); ds@1952: sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */ ds@1952: if( db->pErr ){ ds@1952: sqlite3ValueFree(db->pErr); ds@1952: } ds@1952: sqlite3CloseExtensions(db); ds@1952: ds@1952: db->magic = SQLITE_MAGIC_ERROR; ds@1952: ds@1952: /* The temp-database schema is allocated differently from the other schema ds@1952: ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()). ds@1952: ** So it needs to be freed here. Todo: Why not roll the temp schema into ds@1952: ** the same sqliteMalloc() as the one that allocates the database ds@1952: ** structure? ds@1952: */ ds@1952: sqlite3_free(db->aDb[1].pSchema); ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: sqlite3_mutex_free(db->mutex); ds@1952: sqlite3_free(db); ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Rollback all database files. ds@1952: */ ds@1952: void sqlite3RollbackAll(sqlite3 *db){ ds@1952: int i; ds@1952: int inTrans = 0; ds@1952: assert( sqlite3_mutex_held(db->mutex) ); ds@1952: sqlite3MallocEnterBenignBlock(1); /* Enter benign region */ ds@1952: for(i=0; inDb; i++){ ds@1952: if( db->aDb[i].pBt ){ ds@1952: if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){ ds@1952: inTrans = 1; ds@1952: } ds@1952: sqlite3BtreeRollback(db->aDb[i].pBt); ds@1952: db->aDb[i].inTrans = 0; ds@1952: } ds@1952: } ds@1952: sqlite3VtabRollback(db); ds@1952: sqlite3MallocLeaveBenignBlock(); /* Leave benign region */ ds@1952: ds@1952: if( db->flags&SQLITE_InternChanges ){ ds@1952: sqlite3ExpirePreparedStatements(db); ds@1952: sqlite3ResetInternalSchema(db, 0); ds@1952: } ds@1952: ds@1952: /* If one has been configured, invoke the rollback-hook callback */ ds@1952: if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){ ds@1952: db->xRollbackCallback(db->pRollbackArg); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return a static string that describes the kind of error specified in the ds@1952: ** argument. ds@1952: */ ds@1952: const char *sqlite3ErrStr(int rc){ ds@1952: const char *z; ds@1952: switch( rc & 0xff ){ ds@1952: case SQLITE_ROW: ds@1952: case SQLITE_DONE: ds@1952: case SQLITE_OK: z = "not an error"; break; ds@1952: case SQLITE_ERROR: z = "SQL logic error or missing database"; break; ds@1952: case SQLITE_PERM: z = "access permission denied"; break; ds@1952: case SQLITE_ABORT: z = "callback requested query abort"; break; ds@1952: case SQLITE_BUSY: z = "database is locked"; break; ds@1952: case SQLITE_LOCKED: z = "database table is locked"; break; ds@1952: case SQLITE_NOMEM: z = "out of memory"; break; ds@1952: case SQLITE_READONLY: z = "attempt to write a readonly database"; break; ds@1952: case SQLITE_INTERRUPT: z = "interrupted"; break; ds@1952: case SQLITE_IOERR: z = "disk I/O error"; break; ds@1952: case SQLITE_CORRUPT: z = "database disk image is malformed"; break; ds@1952: case SQLITE_FULL: z = "database or disk is full"; break; ds@1952: case SQLITE_CANTOPEN: z = "unable to open database file"; break; ds@1952: case SQLITE_EMPTY: z = "table contains no data"; break; ds@1952: case SQLITE_SCHEMA: z = "database schema has changed"; break; ds@1952: case SQLITE_TOOBIG: z = "String or BLOB exceeded size limit"; break; ds@1952: case SQLITE_CONSTRAINT: z = "constraint failed"; break; ds@1952: case SQLITE_MISMATCH: z = "datatype mismatch"; break; ds@1952: case SQLITE_MISUSE: z = "library routine called out of sequence";break; ds@1952: case SQLITE_NOLFS: z = "kernel lacks large file support"; break; ds@1952: case SQLITE_AUTH: z = "authorization denied"; break; ds@1952: case SQLITE_FORMAT: z = "auxiliary database format error"; break; ds@1952: case SQLITE_RANGE: z = "bind or column index out of range"; break; ds@1952: case SQLITE_NOTADB: z = "file is encrypted or is not a database";break; ds@1952: default: z = "unknown error"; break; ds@1952: } ds@1952: return z; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This routine implements a busy callback that sleeps and tries ds@1952: ** again until a timeout value is reached. The timeout value is ds@1952: ** an integer number of milliseconds passed in as the first ds@1952: ** argument. ds@1952: */ ds@1952: static int sqliteDefaultBusyCallback( ds@1952: void *ptr, /* Database connection */ ds@1952: int count /* Number of times table has been busy */ ds@1952: ){ ds@1952: #if OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP) ds@1952: static const u8 delays[] = ds@1952: { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 }; ds@1952: static const u8 totals[] = ds@1952: { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 }; ds@1952: # define NDELAY (sizeof(delays)/sizeof(delays[0])) ds@1952: sqlite3 *db = (sqlite3 *)ptr; ds@1952: int timeout = db->busyTimeout; ds@1952: int delay, prior; ds@1952: ds@1952: assert( count>=0 ); ds@1952: if( count < NDELAY ){ ds@1952: delay = delays[count]; ds@1952: prior = totals[count]; ds@1952: }else{ ds@1952: delay = delays[NDELAY-1]; ds@1952: prior = totals[NDELAY-1] + delay*(count-(NDELAY-1)); ds@1952: } ds@1952: if( prior + delay > timeout ){ ds@1952: delay = timeout - prior; ds@1952: if( delay<=0 ) return 0; ds@1952: } ds@1952: sqlite3OsSleep(db->pVfs, delay*1000); ds@1952: return 1; ds@1952: #else ds@1952: sqlite3 *db = (sqlite3 *)ptr; ds@1952: int timeout = ((sqlite3 *)ptr)->busyTimeout; ds@1952: if( (count+1)*1000 > timeout ){ ds@1952: return 0; ds@1952: } ds@1952: sqlite3OsSleep(db->pVfs, 1000000); ds@1952: return 1; ds@1952: #endif ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Invoke the given busy handler. ds@1952: ** ds@1952: ** This routine is called when an operation failed with a lock. ds@1952: ** If this routine returns non-zero, the lock is retried. If it ds@1952: ** returns 0, the operation aborts with an SQLITE_BUSY error. ds@1952: */ ds@1952: int sqlite3InvokeBusyHandler(BusyHandler *p){ ds@1952: int rc; ds@1952: if( p==0 || p->xFunc==0 || p->nBusy<0 ) return 0; ds@1952: rc = p->xFunc(p->pArg, p->nBusy); ds@1952: if( rc==0 ){ ds@1952: p->nBusy = -1; ds@1952: }else{ ds@1952: p->nBusy++; ds@1952: } ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This routine sets the busy callback for an Sqlite database to the ds@1952: ** given callback function with the given argument. ds@1952: */ ds@1952: int sqlite3_busy_handler( ds@1952: sqlite3 *db, ds@1952: int (*xBusy)(void*,int), ds@1952: void *pArg ds@1952: ){ ds@1952: if( sqlite3SafetyCheck(db) ){ ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: db->busyHandler.xFunc = xBusy; ds@1952: db->busyHandler.pArg = pArg; ds@1952: db->busyHandler.nBusy = 0; ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_PROGRESS_CALLBACK ds@1952: /* ds@1952: ** This routine sets the progress callback for an Sqlite database to the ds@1952: ** given callback function with the given argument. The progress callback will ds@1952: ** be invoked every nOps opcodes. ds@1952: */ ds@1952: void sqlite3_progress_handler( ds@1952: sqlite3 *db, ds@1952: int nOps, ds@1952: int (*xProgress)(void*), ds@1952: void *pArg ds@1952: ){ ds@1952: if( !sqlite3SafetyCheck(db) ){ ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: if( nOps>0 ){ ds@1952: db->xProgress = xProgress; ds@1952: db->nProgressOps = nOps; ds@1952: db->pProgressArg = pArg; ds@1952: }else{ ds@1952: db->xProgress = 0; ds@1952: db->nProgressOps = 0; ds@1952: db->pProgressArg = 0; ds@1952: } ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: } ds@1952: } ds@1952: #endif ds@1952: ds@1952: ds@1952: /* ds@1952: ** This routine installs a default busy handler that waits for the ds@1952: ** specified number of milliseconds before returning 0. ds@1952: */ ds@1952: int sqlite3_busy_timeout(sqlite3 *db, int ms){ ds@1952: if( sqlite3SafetyCheck(db) ){ ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: if( ms>0 ){ ds@1952: db->busyTimeout = ms; ds@1952: sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); ds@1952: }else{ ds@1952: sqlite3_busy_handler(db, 0, 0); ds@1952: } ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Cause any pending operation to stop at its earliest opportunity. ds@1952: */ ds@1952: void sqlite3_interrupt(sqlite3 *db){ ds@1952: if( db && (db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_BUSY) ){ ds@1952: db->u1.isInterrupted = 1; ds@1952: } ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** This function is exactly the same as sqlite3_create_function(), except ds@1952: ** that it is designed to be called by internal code. The difference is ds@1952: ** that if a malloc() fails in sqlite3_create_function(), an error code ds@1952: ** is returned and the mallocFailed flag cleared. ds@1952: */ ds@1952: int sqlite3CreateFunc( ds@1952: sqlite3 *db, ds@1952: const char *zFunctionName, ds@1952: int nArg, ds@1952: int enc, ds@1952: void *pUserData, ds@1952: void (*xFunc)(sqlite3_context*,int,sqlite3_value **), ds@1952: void (*xStep)(sqlite3_context*,int,sqlite3_value **), ds@1952: void (*xFinal)(sqlite3_context*) ds@1952: ){ ds@1952: FuncDef *p; ds@1952: int nName; ds@1952: ds@1952: assert( sqlite3_mutex_held(db->mutex) ); ds@1952: if( sqlite3SafetyCheck(db) ){ ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: if( zFunctionName==0 || ds@1952: (xFunc && (xFinal || xStep)) || ds@1952: (!xFunc && (xFinal && !xStep)) || ds@1952: (!xFunc && (!xFinal && xStep)) || ds@1952: (nArg<-1 || nArg>127) || ds@1952: (255<(nName = strlen(zFunctionName))) ){ ds@1952: sqlite3Error(db, SQLITE_ERROR, "bad parameters"); ds@1952: return SQLITE_ERROR; ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: /* If SQLITE_UTF16 is specified as the encoding type, transform this ds@1952: ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the ds@1952: ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. ds@1952: ** ds@1952: ** If SQLITE_ANY is specified, add three versions of the function ds@1952: ** to the hash table. ds@1952: */ ds@1952: if( enc==SQLITE_UTF16 ){ ds@1952: enc = SQLITE_UTF16NATIVE; ds@1952: }else if( enc==SQLITE_ANY ){ ds@1952: int rc; ds@1952: rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8, ds@1952: pUserData, xFunc, xStep, xFinal); ds@1952: if( rc==SQLITE_OK ){ ds@1952: rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE, ds@1952: pUserData, xFunc, xStep, xFinal); ds@1952: } ds@1952: if( rc!=SQLITE_OK ){ ds@1952: return rc; ds@1952: } ds@1952: enc = SQLITE_UTF16BE; ds@1952: } ds@1952: #else ds@1952: enc = SQLITE_UTF8; ds@1952: #endif ds@1952: ds@1952: /* Check if an existing function is being overridden or deleted. If so, ds@1952: ** and there are active VMs, then return SQLITE_BUSY. If a function ds@1952: ** is being overridden/deleted but there are no active VMs, allow the ds@1952: ** operation to continue but invalidate all precompiled statements. ds@1952: */ ds@1952: p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0); ds@1952: if( p && p->iPrefEnc==enc && p->nArg==nArg ){ ds@1952: if( db->activeVdbeCnt ){ ds@1952: sqlite3Error(db, SQLITE_BUSY, ds@1952: "Unable to delete/modify user-function due to active statements"); ds@1952: assert( !db->mallocFailed ); ds@1952: return SQLITE_BUSY; ds@1952: }else{ ds@1952: sqlite3ExpirePreparedStatements(db); ds@1952: } ds@1952: } ds@1952: ds@1952: p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1); ds@1952: assert(p || db->mallocFailed); ds@1952: if( !p ){ ds@1952: return SQLITE_NOMEM; ds@1952: } ds@1952: p->flags = 0; ds@1952: p->xFunc = xFunc; ds@1952: p->xStep = xStep; ds@1952: p->xFinalize = xFinal; ds@1952: p->pUserData = pUserData; ds@1952: p->nArg = nArg; ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Create new user functions. ds@1952: */ ds@1952: int sqlite3_create_function( ds@1952: sqlite3 *db, ds@1952: const char *zFunctionName, ds@1952: int nArg, ds@1952: int enc, ds@1952: void *p, ds@1952: void (*xFunc)(sqlite3_context*,int,sqlite3_value **), ds@1952: void (*xStep)(sqlite3_context*,int,sqlite3_value **), ds@1952: void (*xFinal)(sqlite3_context*) ds@1952: ){ ds@1952: int rc; ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: assert( !db->mallocFailed ); ds@1952: rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal); ds@1952: rc = sqlite3ApiExit(db, rc); ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: int sqlite3_create_function16( ds@1952: sqlite3 *db, ds@1952: const void *zFunctionName, ds@1952: int nArg, ds@1952: int eTextRep, ds@1952: void *p, ds@1952: void (*xFunc)(sqlite3_context*,int,sqlite3_value**), ds@1952: void (*xStep)(sqlite3_context*,int,sqlite3_value**), ds@1952: void (*xFinal)(sqlite3_context*) ds@1952: ){ ds@1952: int rc; ds@1952: char *zFunc8; ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: assert( !db->mallocFailed ); ds@1952: zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1); ds@1952: rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal); ds@1952: sqlite3_free(zFunc8); ds@1952: rc = sqlite3ApiExit(db, rc); ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return rc; ds@1952: } ds@1952: #endif ds@1952: ds@1952: ds@1952: /* ds@1952: ** Declare that a function has been overloaded by a virtual table. ds@1952: ** ds@1952: ** If the function already exists as a regular global function, then ds@1952: ** this routine is a no-op. If the function does not exist, then create ds@1952: ** a new one that always throws a run-time error. ds@1952: ** ds@1952: ** When virtual tables intend to provide an overloaded function, they ds@1952: ** should call this routine to make sure the global function exists. ds@1952: ** A global function must exist in order for name resolution to work ds@1952: ** properly. ds@1952: */ ds@1952: int sqlite3_overload_function( ds@1952: sqlite3 *db, ds@1952: const char *zName, ds@1952: int nArg ds@1952: ){ ds@1952: int nName = strlen(zName); ds@1952: int rc; ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ ds@1952: sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, ds@1952: 0, sqlite3InvalidFunction, 0, 0); ds@1952: } ds@1952: rc = sqlite3ApiExit(db, SQLITE_OK); ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_TRACE ds@1952: /* ds@1952: ** Register a trace function. The pArg from the previously registered trace ds@1952: ** is returned. ds@1952: ** ds@1952: ** A NULL trace function means that no tracing is executes. A non-NULL ds@1952: ** trace is a pointer to a function that is invoked at the start of each ds@1952: ** SQL statement. ds@1952: */ ds@1952: void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){ ds@1952: void *pOld; ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: pOld = db->pTraceArg; ds@1952: db->xTrace = xTrace; ds@1952: db->pTraceArg = pArg; ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return pOld; ds@1952: } ds@1952: /* ds@1952: ** Register a profile function. The pArg from the previously registered ds@1952: ** profile function is returned. ds@1952: ** ds@1952: ** A NULL profile function means that no profiling is executes. A non-NULL ds@1952: ** profile is a pointer to a function that is invoked at the conclusion of ds@1952: ** each SQL statement that is run. ds@1952: */ ds@1952: void *sqlite3_profile( ds@1952: sqlite3 *db, ds@1952: void (*xProfile)(void*,const char*,sqlite_uint64), ds@1952: void *pArg ds@1952: ){ ds@1952: void *pOld; ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: pOld = db->pProfileArg; ds@1952: db->xProfile = xProfile; ds@1952: db->pProfileArg = pArg; ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return pOld; ds@1952: } ds@1952: #endif /* SQLITE_OMIT_TRACE */ ds@1952: ds@1952: /*** EXPERIMENTAL *** ds@1952: ** ds@1952: ** Register a function to be invoked when a transaction comments. ds@1952: ** If the invoked function returns non-zero, then the commit becomes a ds@1952: ** rollback. ds@1952: */ ds@1952: void *sqlite3_commit_hook( ds@1952: sqlite3 *db, /* Attach the hook to this database */ ds@1952: int (*xCallback)(void*), /* Function to invoke on each commit */ ds@1952: void *pArg /* Argument to the function */ ds@1952: ){ ds@1952: void *pOld; ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: pOld = db->pCommitArg; ds@1952: db->xCommitCallback = xCallback; ds@1952: db->pCommitArg = pArg; ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return pOld; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Register a callback to be invoked each time a row is updated, ds@1952: ** inserted or deleted using this database connection. ds@1952: */ ds@1952: void *sqlite3_update_hook( ds@1952: sqlite3 *db, /* Attach the hook to this database */ ds@1952: void (*xCallback)(void*,int,char const *,char const *,sqlite_int64), ds@1952: void *pArg /* Argument to the function */ ds@1952: ){ ds@1952: void *pRet; ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: pRet = db->pUpdateArg; ds@1952: db->xUpdateCallback = xCallback; ds@1952: db->pUpdateArg = pArg; ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return pRet; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Register a callback to be invoked each time a transaction is rolled ds@1952: ** back by this database connection. ds@1952: */ ds@1952: void *sqlite3_rollback_hook( ds@1952: sqlite3 *db, /* Attach the hook to this database */ ds@1952: void (*xCallback)(void*), /* Callback function */ ds@1952: void *pArg /* Argument to the function */ ds@1952: ){ ds@1952: void *pRet; ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: pRet = db->pRollbackArg; ds@1952: db->xRollbackCallback = xCallback; ds@1952: db->pRollbackArg = pArg; ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return pRet; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This routine is called to create a connection to a database BTree ds@1952: ** driver. If zFilename is the name of a file, then that file is ds@1952: ** opened and used. If zFilename is the magic name ":memory:" then ds@1952: ** the database is stored in memory (and is thus forgotten as soon as ds@1952: ** the connection is closed.) If zFilename is NULL then the database ds@1952: ** is a "virtual" database for transient use only and is deleted as ds@1952: ** soon as the connection is closed. ds@1952: ** ds@1952: ** A virtual database can be either a disk file (that is automatically ds@1952: ** deleted when the file is closed) or it an be held entirely in memory, ds@1952: ** depending on the values of the TEMP_STORE compile-time macro and the ds@1952: ** db->temp_store variable, according to the following chart: ds@1952: ** ds@1952: ** TEMP_STORE db->temp_store Location of temporary database ds@1952: ** ---------- -------------- ------------------------------ ds@1952: ** 0 any file ds@1952: ** 1 1 file ds@1952: ** 1 2 memory ds@1952: ** 1 0 file ds@1952: ** 2 1 file ds@1952: ** 2 2 memory ds@1952: ** 2 0 memory ds@1952: ** 3 any memory ds@1952: */ ds@1952: int sqlite3BtreeFactory( ds@1952: const sqlite3 *db, /* Main database when opening aux otherwise 0 */ ds@1952: const char *zFilename, /* Name of the file containing the BTree database */ ds@1952: int omitJournal, /* if TRUE then do not journal this file */ ds@1952: int nCache, /* How many pages in the page cache */ ds@1952: int vfsFlags, /* Flags passed through to vfsOpen */ ds@1952: Btree **ppBtree /* Pointer to new Btree object written here */ ds@1952: ){ ds@1952: int btFlags = 0; ds@1952: int rc; ds@1952: ds@1952: assert( sqlite3_mutex_held(db->mutex) ); ds@1952: assert( ppBtree != 0); ds@1952: if( omitJournal ){ ds@1952: btFlags |= BTREE_OMIT_JOURNAL; ds@1952: } ds@1952: if( db->flags & SQLITE_NoReadlock ){ ds@1952: btFlags |= BTREE_NO_READLOCK; ds@1952: } ds@1952: if( zFilename==0 ){ ds@1952: #if TEMP_STORE==0 ds@1952: /* Do nothing */ ds@1952: #endif ds@1952: #ifndef SQLITE_OMIT_MEMORYDB ds@1952: #if TEMP_STORE==1 ds@1952: if( db->temp_store==2 ) zFilename = ":memory:"; ds@1952: #endif ds@1952: #if TEMP_STORE==2 ds@1952: if( db->temp_store!=1 ) zFilename = ":memory:"; ds@1952: #endif ds@1952: #if TEMP_STORE==3 ds@1952: zFilename = ":memory:"; ds@1952: #endif ds@1952: #endif /* SQLITE_OMIT_MEMORYDB */ ds@1952: } ds@1952: ds@1952: if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (zFilename==0 || *zFilename==0) ){ ds@1952: vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB; ds@1952: } ds@1952: rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btFlags, vfsFlags); ds@1952: if( rc==SQLITE_OK ){ ds@1952: sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler); ds@1952: sqlite3BtreeSetCacheSize(*ppBtree, nCache); ds@1952: } ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return UTF-8 encoded English language explanation of the most recent ds@1952: ** error. ds@1952: */ ds@1952: const char *sqlite3_errmsg(sqlite3 *db){ ds@1952: const char *z; ds@1952: if( !db ){ ds@1952: return sqlite3ErrStr(SQLITE_NOMEM); ds@1952: } ds@1952: if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){ ds@1952: return sqlite3ErrStr(SQLITE_MISUSE); ds@1952: } ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: assert( !db->mallocFailed ); ds@1952: z = (char*)sqlite3_value_text(db->pErr); ds@1952: if( z==0 ){ ds@1952: z = sqlite3ErrStr(db->errCode); ds@1952: } ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return z; ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: /* ds@1952: ** Return UTF-16 encoded English language explanation of the most recent ds@1952: ** error. ds@1952: */ ds@1952: const void *sqlite3_errmsg16(sqlite3 *db){ ds@1952: /* Because all the characters in the string are in the unicode ds@1952: ** range 0x00-0xFF, if we pad the big-endian string with a ds@1952: ** zero byte, we can obtain the little-endian string with ds@1952: ** &big_endian[1]. ds@1952: */ ds@1952: static const char outOfMemBe[] = { ds@1952: 0, 'o', 0, 'u', 0, 't', 0, ' ', ds@1952: 0, 'o', 0, 'f', 0, ' ', ds@1952: 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0 ds@1952: }; ds@1952: static const char misuseBe [] = { ds@1952: 0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ', ds@1952: 0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ', ds@1952: 0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ', ds@1952: 0, 'o', 0, 'u', 0, 't', 0, ' ', ds@1952: 0, 'o', 0, 'f', 0, ' ', ds@1952: 0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0 ds@1952: }; ds@1952: ds@1952: const void *z; ds@1952: if( !db ){ ds@1952: return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]); ds@1952: } ds@1952: if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){ ds@1952: return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]); ds@1952: } ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: assert( !db->mallocFailed ); ds@1952: z = sqlite3_value_text16(db->pErr); ds@1952: if( z==0 ){ ds@1952: sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode), ds@1952: SQLITE_UTF8, SQLITE_STATIC); ds@1952: z = sqlite3_value_text16(db->pErr); ds@1952: } ds@1952: sqlite3ApiExit(0, 0); ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return z; ds@1952: } ds@1952: #endif /* SQLITE_OMIT_UTF16 */ ds@1952: ds@1952: /* ds@1952: ** Return the most recent error code generated by an SQLite routine. If NULL is ds@1952: ** passed to this function, we assume a malloc() failed during sqlite3_open(). ds@1952: */ ds@1952: int sqlite3_errcode(sqlite3 *db){ ds@1952: if( !db || db->mallocFailed ){ ds@1952: return SQLITE_NOMEM; ds@1952: } ds@1952: if( sqlite3SafetyCheck(db) ){ ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: return db->errCode & db->errMask; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Create a new collating function for database "db". The name is zName ds@1952: ** and the encoding is enc. ds@1952: */ ds@1952: static int createCollation( ds@1952: sqlite3* db, ds@1952: const char *zName, ds@1952: int enc, ds@1952: void* pCtx, ds@1952: int(*xCompare)(void*,int,const void*,int,const void*), ds@1952: void(*xDel)(void*) ds@1952: ){ ds@1952: CollSeq *pColl; ds@1952: int enc2; ds@1952: ds@1952: if( sqlite3SafetyCheck(db) ){ ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: assert( sqlite3_mutex_held(db->mutex) ); ds@1952: ds@1952: /* If SQLITE_UTF16 is specified as the encoding type, transform this ds@1952: ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the ds@1952: ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. ds@1952: */ ds@1952: enc2 = enc & ~SQLITE_UTF16_ALIGNED; ds@1952: if( enc2==SQLITE_UTF16 ){ ds@1952: enc2 = SQLITE_UTF16NATIVE; ds@1952: } ds@1952: ds@1952: if( (enc2&~3)!=0 ){ ds@1952: sqlite3Error(db, SQLITE_ERROR, "unknown encoding"); ds@1952: return SQLITE_ERROR; ds@1952: } ds@1952: ds@1952: /* Check if this call is removing or replacing an existing collation ds@1952: ** sequence. If so, and there are active VMs, return busy. If there ds@1952: ** are no active VMs, invalidate any pre-compiled statements. ds@1952: */ ds@1952: pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, strlen(zName), 0); ds@1952: if( pColl && pColl->xCmp ){ ds@1952: if( db->activeVdbeCnt ){ ds@1952: sqlite3Error(db, SQLITE_BUSY, ds@1952: "Unable to delete/modify collation sequence due to active statements"); ds@1952: return SQLITE_BUSY; ds@1952: } ds@1952: sqlite3ExpirePreparedStatements(db); ds@1952: ds@1952: /* If collation sequence pColl was created directly by a call to ds@1952: ** sqlite3_create_collation, and not generated by synthCollSeq(), ds@1952: ** then any copies made by synthCollSeq() need to be invalidated. ds@1952: ** Also, collation destructor - CollSeq.xDel() - function may need ds@1952: ** to be called. ds@1952: */ ds@1952: if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){ ds@1952: CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, strlen(zName)); ds@1952: int j; ds@1952: for(j=0; j<3; j++){ ds@1952: CollSeq *p = &aColl[j]; ds@1952: if( p->enc==pColl->enc ){ ds@1952: if( p->xDel ){ ds@1952: p->xDel(p->pUser); ds@1952: } ds@1952: p->xCmp = 0; ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, strlen(zName), 1); ds@1952: if( pColl ){ ds@1952: pColl->xCmp = xCompare; ds@1952: pColl->pUser = pCtx; ds@1952: pColl->xDel = xDel; ds@1952: pColl->enc = enc2 | (enc & SQLITE_UTF16_ALIGNED); ds@1952: } ds@1952: sqlite3Error(db, SQLITE_OK, 0); ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** This routine does the work of opening a database on behalf of ds@1952: ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" ds@1952: ** is UTF-8 encoded. ds@1952: */ ds@1952: static int openDatabase( ds@1952: const char *zFilename, /* Database filename UTF-8 encoded */ ds@1952: sqlite3 **ppDb, /* OUT: Returned database handle */ ds@1952: unsigned flags, /* Operational flags */ ds@1952: const char *zVfs /* Name of the VFS to use */ ds@1952: ){ ds@1952: sqlite3 *db; ds@1952: int rc; ds@1952: CollSeq *pColl; ds@1952: ds@1952: /* Allocate the sqlite data structure */ ds@1952: db = sqlite3MallocZero( sizeof(sqlite3) ); ds@1952: if( db==0 ) goto opendb_out; ds@1952: db->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE); ds@1952: if( db->mutex==0 ){ ds@1952: sqlite3_free(db); ds@1952: db = 0; ds@1952: goto opendb_out; ds@1952: } ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: db->errMask = 0xff; ds@1952: db->priorNewRowid = 0; ds@1952: db->nDb = 2; ds@1952: db->magic = SQLITE_MAGIC_BUSY; ds@1952: db->aDb = db->aDbStatic; ds@1952: db->autoCommit = 1; ds@1952: db->flags |= SQLITE_ShortColNames ds@1952: #if SQLITE_DEFAULT_FILE_FORMAT<4 ds@1952: | SQLITE_LegacyFileFmt ds@1952: #endif ds@1952: #ifdef SQLITE_ENABLE_LOAD_EXTENSION ds@1952: | SQLITE_LoadExtension ds@1952: #endif ds@1952: ; ds@1952: sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0); ds@1952: sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0); ds@1952: #ifndef SQLITE_OMIT_VIRTUALTABLE ds@1952: sqlite3HashInit(&db->aModule, SQLITE_HASH_STRING, 0); ds@1952: #endif ds@1952: ds@1952: db->pVfs = sqlite3_vfs_find(zVfs); ds@1952: if( !db->pVfs ){ ds@1952: rc = SQLITE_ERROR; ds@1952: db->magic = SQLITE_MAGIC_CLOSED; ds@1952: sqlite3Error(db, rc, "no such vfs: %s", (zVfs?zVfs:"(null)")); ds@1952: goto opendb_out; ds@1952: } ds@1952: ds@1952: /* Add the default collation sequence BINARY. BINARY works for both UTF-8 ds@1952: ** and UTF-16, so add a version for each to avoid any unnecessary ds@1952: ** conversions. The only error that can occur here is a malloc() failure. ds@1952: */ ds@1952: if( createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0) || ds@1952: createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0) || ds@1952: createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0) || ds@1952: (db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0))==0 ds@1952: ){ ds@1952: assert( db->mallocFailed ); ds@1952: db->magic = SQLITE_MAGIC_CLOSED; ds@1952: goto opendb_out; ds@1952: } ds@1952: ds@1952: /* Also add a UTF-8 case-insensitive collation sequence. */ ds@1952: createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0); ds@1952: ds@1952: /* Set flags on the built-in collating sequences */ ds@1952: db->pDfltColl->type = SQLITE_COLL_BINARY; ds@1952: pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "NOCASE", 6, 0); ds@1952: if( pColl ){ ds@1952: pColl->type = SQLITE_COLL_NOCASE; ds@1952: } ds@1952: ds@1952: /* Open the backend database driver */ ds@1952: db->openFlags = flags; ds@1952: rc = sqlite3BtreeFactory(db, zFilename, 0, SQLITE_DEFAULT_CACHE_SIZE, ds@1952: flags | SQLITE_OPEN_MAIN_DB, ds@1952: &db->aDb[0].pBt); ds@1952: if( rc!=SQLITE_OK ){ ds@1952: sqlite3Error(db, rc, 0); ds@1952: db->magic = SQLITE_MAGIC_CLOSED; ds@1952: goto opendb_out; ds@1952: } ds@1952: db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt); ds@1952: db->aDb[1].pSchema = sqlite3SchemaGet(db, 0); ds@1952: ds@1952: ds@1952: /* The default safety_level for the main database is 'full'; for the temp ds@1952: ** database it is 'NONE'. This matches the pager layer defaults. ds@1952: */ ds@1952: db->aDb[0].zName = "main"; ds@1952: db->aDb[0].safety_level = 3; ds@1952: #ifndef SQLITE_OMIT_TEMPDB ds@1952: db->aDb[1].zName = "temp"; ds@1952: db->aDb[1].safety_level = 1; ds@1952: #endif ds@1952: ds@1952: db->magic = SQLITE_MAGIC_OPEN; ds@1952: if( db->mallocFailed ){ ds@1952: goto opendb_out; ds@1952: } ds@1952: ds@1952: /* Register all built-in functions, but do not attempt to read the ds@1952: ** database schema yet. This is delayed until the first time the database ds@1952: ** is accessed. ds@1952: */ ds@1952: sqlite3Error(db, SQLITE_OK, 0); ds@1952: sqlite3RegisterBuiltinFunctions(db); ds@1952: ds@1952: /* Load automatic extensions - extensions that have been registered ds@1952: ** using the sqlite3_automatic_extension() API. ds@1952: */ ds@1952: (void)sqlite3AutoLoadExtensions(db); ds@1952: if( sqlite3_errcode(db)!=SQLITE_OK ){ ds@1952: goto opendb_out; ds@1952: } ds@1952: ds@1952: #ifdef SQLITE_ENABLE_FTS1 ds@1952: if( !db->mallocFailed ){ ds@1952: extern int sqlite3Fts1Init(sqlite3*); ds@1952: rc = sqlite3Fts1Init(db); ds@1952: } ds@1952: #endif ds@1952: ds@1952: #ifdef SQLITE_ENABLE_FTS2 ds@1952: if( !db->mallocFailed && rc==SQLITE_OK ){ ds@1952: extern int sqlite3Fts2Init(sqlite3*); ds@1952: rc = sqlite3Fts2Init(db); ds@1952: } ds@1952: #endif ds@1952: ds@1952: #ifdef SQLITE_ENABLE_FTS3 ds@1952: if( !db->mallocFailed && rc==SQLITE_OK ){ ds@1952: extern int sqlite3Fts3Init(sqlite3*); ds@1952: rc = sqlite3Fts3Init(db); ds@1952: } ds@1952: #endif ds@1952: ds@1952: #ifdef SQLITE_ENABLE_ICU ds@1952: if( !db->mallocFailed && rc==SQLITE_OK ){ ds@1952: extern int sqlite3IcuInit(sqlite3*); ds@1952: rc = sqlite3IcuInit(db); ds@1952: } ds@1952: #endif ds@1952: sqlite3Error(db, rc, 0); ds@1952: ds@1952: /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking ds@1952: ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking ds@1952: ** mode. Doing nothing at all also makes NORMAL the default. ds@1952: */ ds@1952: #ifdef SQLITE_DEFAULT_LOCKING_MODE ds@1952: db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE; ds@1952: sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt), ds@1952: SQLITE_DEFAULT_LOCKING_MODE); ds@1952: #endif ds@1952: ds@1952: opendb_out: ds@1952: if( db && db->mutex ){ ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: } ds@1952: if( SQLITE_NOMEM==(rc = sqlite3_errcode(db)) ){ ds@1952: sqlite3_close(db); ds@1952: db = 0; ds@1952: } ds@1952: *ppDb = db; ds@1952: return sqlite3ApiExit(0, rc); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Open a new database handle. ds@1952: */ ds@1952: int sqlite3_open( ds@1952: const char *zFilename, ds@1952: sqlite3 **ppDb ds@1952: ){ ds@1952: return openDatabase(zFilename, ppDb, ds@1952: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); ds@1952: } ds@1952: int sqlite3_open_v2( ds@1952: const char *filename, /* Database filename (UTF-8) */ ds@1952: sqlite3 **ppDb, /* OUT: SQLite db handle */ ds@1952: int flags, /* Flags */ ds@1952: const char *zVfs /* Name of VFS module to use */ ds@1952: ){ ds@1952: return openDatabase(filename, ppDb, flags, zVfs); ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: /* ds@1952: ** Open a new database handle. ds@1952: */ ds@1952: int sqlite3_open16( ds@1952: const void *zFilename, ds@1952: sqlite3 **ppDb ds@1952: ){ ds@1952: char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ ds@1952: sqlite3_value *pVal; ds@1952: int rc = SQLITE_NOMEM; ds@1952: ds@1952: assert( zFilename ); ds@1952: assert( ppDb ); ds@1952: *ppDb = 0; ds@1952: pVal = sqlite3ValueNew(0); ds@1952: sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC); ds@1952: zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); ds@1952: if( zFilename8 ){ ds@1952: rc = openDatabase(zFilename8, ppDb, ds@1952: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); ds@1952: if( rc==SQLITE_OK && *ppDb ){ ds@1952: rc = sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0); ds@1952: if( rc!=SQLITE_OK ){ ds@1952: sqlite3_close(*ppDb); ds@1952: *ppDb = 0; ds@1952: } ds@1952: } ds@1952: } ds@1952: sqlite3ValueFree(pVal); ds@1952: ds@1952: return sqlite3ApiExit(0, rc); ds@1952: } ds@1952: #endif /* SQLITE_OMIT_UTF16 */ ds@1952: ds@1952: /* ds@1952: ** Register a new collation sequence with the database handle db. ds@1952: */ ds@1952: int sqlite3_create_collation( ds@1952: sqlite3* db, ds@1952: const char *zName, ds@1952: int enc, ds@1952: void* pCtx, ds@1952: int(*xCompare)(void*,int,const void*,int,const void*) ds@1952: ){ ds@1952: int rc; ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: assert( !db->mallocFailed ); ds@1952: rc = createCollation(db, zName, enc, pCtx, xCompare, 0); ds@1952: rc = sqlite3ApiExit(db, rc); ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Register a new collation sequence with the database handle db. ds@1952: */ ds@1952: int sqlite3_create_collation_v2( ds@1952: sqlite3* db, ds@1952: const char *zName, ds@1952: int enc, ds@1952: void* pCtx, ds@1952: int(*xCompare)(void*,int,const void*,int,const void*), ds@1952: void(*xDel)(void*) ds@1952: ){ ds@1952: int rc; ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: assert( !db->mallocFailed ); ds@1952: rc = createCollation(db, zName, enc, pCtx, xCompare, xDel); ds@1952: rc = sqlite3ApiExit(db, rc); ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: /* ds@1952: ** Register a new collation sequence with the database handle db. ds@1952: */ ds@1952: int sqlite3_create_collation16( ds@1952: sqlite3* db, ds@1952: const char *zName, ds@1952: int enc, ds@1952: void* pCtx, ds@1952: int(*xCompare)(void*,int,const void*,int,const void*) ds@1952: ){ ds@1952: int rc = SQLITE_OK; ds@1952: char *zName8; ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: assert( !db->mallocFailed ); ds@1952: zName8 = sqlite3Utf16to8(db, zName, -1); ds@1952: if( zName8 ){ ds@1952: rc = createCollation(db, zName8, enc, pCtx, xCompare, 0); ds@1952: sqlite3_free(zName8); ds@1952: } ds@1952: rc = sqlite3ApiExit(db, rc); ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return rc; ds@1952: } ds@1952: #endif /* SQLITE_OMIT_UTF16 */ ds@1952: ds@1952: /* ds@1952: ** Register a collation sequence factory callback with the database handle ds@1952: ** db. Replace any previously installed collation sequence factory. ds@1952: */ ds@1952: int sqlite3_collation_needed( ds@1952: sqlite3 *db, ds@1952: void *pCollNeededArg, ds@1952: void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) ds@1952: ){ ds@1952: if( sqlite3SafetyCheck(db) ){ ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: db->xCollNeeded = xCollNeeded; ds@1952: db->xCollNeeded16 = 0; ds@1952: db->pCollNeededArg = pCollNeededArg; ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: /* ds@1952: ** Register a collation sequence factory callback with the database handle ds@1952: ** db. Replace any previously installed collation sequence factory. ds@1952: */ ds@1952: int sqlite3_collation_needed16( ds@1952: sqlite3 *db, ds@1952: void *pCollNeededArg, ds@1952: void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) ds@1952: ){ ds@1952: if( sqlite3SafetyCheck(db) ){ ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: db->xCollNeeded = 0; ds@1952: db->xCollNeeded16 = xCollNeeded16; ds@1952: db->pCollNeededArg = pCollNeededArg; ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return SQLITE_OK; ds@1952: } ds@1952: #endif /* SQLITE_OMIT_UTF16 */ ds@1952: ds@1952: #ifndef SQLITE_OMIT_GLOBALRECOVER ds@1952: /* ds@1952: ** This function is now an anachronism. It used to be used to recover from a ds@1952: ** malloc() failure, but SQLite now does this automatically. ds@1952: */ ds@1952: int sqlite3_global_recover(){ ds@1952: return SQLITE_OK; ds@1952: } ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Test to see whether or not the database connection is in autocommit ds@1952: ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on ds@1952: ** by default. Autocommit is disabled by a BEGIN statement and reenabled ds@1952: ** by the next COMMIT or ROLLBACK. ds@1952: ** ds@1952: ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ****** ds@1952: */ ds@1952: int sqlite3_get_autocommit(sqlite3 *db){ ds@1952: return db->autoCommit; ds@1952: } ds@1952: ds@1952: #ifdef SQLITE_DEBUG ds@1952: /* ds@1952: ** The following routine is subtituted for constant SQLITE_CORRUPT in ds@1952: ** debugging builds. This provides a way to set a breakpoint for when ds@1952: ** corruption is first detected. ds@1952: */ ds@1952: int sqlite3Corrupt(void){ ds@1952: return SQLITE_CORRUPT; ds@1952: } ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** This is a convenience routine that makes sure that all thread-specific ds@1952: ** data for this thread has been deallocated. ds@1952: ** ds@1952: ** SQLite no longer uses thread-specific data so this routine is now a ds@1952: ** no-op. It is retained for historical compatibility. ds@1952: */ ds@1952: void sqlite3_thread_cleanup(void){ ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return meta information about a specific column of a database table. ds@1952: ** See comment in sqlite3.h (sqlite.h.in) for details. ds@1952: */ ds@1952: #ifdef SQLITE_ENABLE_COLUMN_METADATA ds@1952: int sqlite3_table_column_metadata( ds@1952: sqlite3 *db, /* Connection handle */ ds@1952: const char *zDbName, /* Database name or NULL */ ds@1952: const char *zTableName, /* Table name */ ds@1952: const char *zColumnName, /* Column name */ ds@1952: char const **pzDataType, /* OUTPUT: Declared data type */ ds@1952: char const **pzCollSeq, /* OUTPUT: Collation sequence name */ ds@1952: int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ ds@1952: int *pPrimaryKey, /* OUTPUT: True if column part of PK */ ds@1952: int *pAutoinc /* OUTPUT: True if colums is auto-increment */ ds@1952: ){ ds@1952: int rc; ds@1952: char *zErrMsg = 0; ds@1952: Table *pTab = 0; ds@1952: Column *pCol = 0; ds@1952: int iCol; ds@1952: ds@1952: char const *zDataType = 0; ds@1952: char const *zCollSeq = 0; ds@1952: int notnull = 0; ds@1952: int primarykey = 0; ds@1952: int autoinc = 0; ds@1952: ds@1952: /* Ensure the database schema has been loaded */ ds@1952: if( sqlite3SafetyOn(db) ){ ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: rc = sqlite3Init(db, &zErrMsg); ds@1952: if( SQLITE_OK!=rc ){ ds@1952: goto error_out; ds@1952: } ds@1952: ds@1952: /* Locate the table in question */ ds@1952: pTab = sqlite3FindTable(db, zTableName, zDbName); ds@1952: if( !pTab || pTab->pSelect ){ ds@1952: pTab = 0; ds@1952: goto error_out; ds@1952: } ds@1952: ds@1952: /* Find the column for which info is requested */ ds@1952: if( sqlite3IsRowid(zColumnName) ){ ds@1952: iCol = pTab->iPKey; ds@1952: if( iCol>=0 ){ ds@1952: pCol = &pTab->aCol[iCol]; ds@1952: } ds@1952: }else{ ds@1952: for(iCol=0; iColnCol; iCol++){ ds@1952: pCol = &pTab->aCol[iCol]; ds@1952: if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){ ds@1952: break; ds@1952: } ds@1952: } ds@1952: if( iCol==pTab->nCol ){ ds@1952: pTab = 0; ds@1952: goto error_out; ds@1952: } ds@1952: } ds@1952: ds@1952: /* The following block stores the meta information that will be returned ds@1952: ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey ds@1952: ** and autoinc. At this point there are two possibilities: ds@1952: ** ds@1952: ** 1. The specified column name was rowid", "oid" or "_rowid_" ds@1952: ** and there is no explicitly declared IPK column. ds@1952: ** ds@1952: ** 2. The table is not a view and the column name identified an ds@1952: ** explicitly declared column. Copy meta information from *pCol. ds@1952: */ ds@1952: if( pCol ){ ds@1952: zDataType = pCol->zType; ds@1952: zCollSeq = pCol->zColl; ds@1952: notnull = (pCol->notNull?1:0); ds@1952: primarykey = (pCol->isPrimKey?1:0); ds@1952: autoinc = ((pTab->iPKey==iCol && pTab->autoInc)?1:0); ds@1952: }else{ ds@1952: zDataType = "INTEGER"; ds@1952: primarykey = 1; ds@1952: } ds@1952: if( !zCollSeq ){ ds@1952: zCollSeq = "BINARY"; ds@1952: } ds@1952: ds@1952: error_out: ds@1952: if( sqlite3SafetyOff(db) ){ ds@1952: rc = SQLITE_MISUSE; ds@1952: } ds@1952: ds@1952: /* Whether the function call succeeded or failed, set the output parameters ds@1952: ** to whatever their local counterparts contain. If an error did occur, ds@1952: ** this has the effect of zeroing all output parameters. ds@1952: */ ds@1952: if( pzDataType ) *pzDataType = zDataType; ds@1952: if( pzCollSeq ) *pzCollSeq = zCollSeq; ds@1952: if( pNotNull ) *pNotNull = notnull; ds@1952: if( pPrimaryKey ) *pPrimaryKey = primarykey; ds@1952: if( pAutoinc ) *pAutoinc = autoinc; ds@1952: ds@1952: if( SQLITE_OK==rc && !pTab ){ ds@1952: sqlite3SetString(&zErrMsg, "no such table column: ", zTableName, ".", ds@1952: zColumnName, 0); ds@1952: rc = SQLITE_ERROR; ds@1952: } ds@1952: sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg); ds@1952: sqlite3_free(zErrMsg); ds@1952: rc = sqlite3ApiExit(db, rc); ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return rc; ds@1952: } ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Sleep for a little while. Return the amount of time slept. ds@1952: */ ds@1952: int sqlite3_sleep(int ms){ ds@1952: sqlite3_vfs *pVfs; ds@1952: int rc; ds@1952: pVfs = sqlite3_vfs_find(0); ds@1952: ds@1952: /* This function works in milliseconds, but the underlying OsSleep() ds@1952: ** API uses microseconds. Hence the 1000's. ds@1952: */ ds@1952: rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Enable or disable the extended result codes. ds@1952: */ ds@1952: int sqlite3_extended_result_codes(sqlite3 *db, int onoff){ ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: db->errMask = onoff ? 0xffffffff : 0xff; ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Invoke the xFileControl method on a particular database. ds@1952: */ ds@1952: int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ ds@1952: int rc = SQLITE_ERROR; ds@1952: int iDb; ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: if( zDbName==0 ){ ds@1952: iDb = 0; ds@1952: }else{ ds@1952: for(iDb=0; iDbnDb; iDb++){ ds@1952: if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break; ds@1952: } ds@1952: } ds@1952: if( iDbnDb ){ ds@1952: Btree *pBtree = db->aDb[iDb].pBt; ds@1952: if( pBtree ){ ds@1952: Pager *pPager; ds@1952: sqlite3BtreeEnter(pBtree); ds@1952: pPager = sqlite3BtreePager(pBtree); ds@1952: if( pPager ){ ds@1952: sqlite3_file *fd = sqlite3PagerFile(pPager); ds@1952: if( fd ){ ds@1952: rc = sqlite3OsFileControl(fd, op, pArg); ds@1952: } ds@1952: } ds@1952: sqlite3BtreeLeave(pBtree); ds@1952: } ds@1952: } ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return rc; ds@1952: }