勇芳软件工作室.汉化:  SQLite3 API Functions > General >

sqlite3_config

Previous pageReturn to chapter overviewNext page

描述

 

sqlite3_config功能用于对SQLite进行全局配置更改,以便将SQLite调整为应用程序的特定需求。建议大多数应用程序使用默认配置,因此通常不需要此函数。它提供用于支持具有不寻常需求的罕见应用程序。

 

C / C ++语法

 

int sqlite3_config(int, ...);

 

PB语法

 

FUNCTION sqlite3_config ( _

BYVAL op AS LONG, _

OPTIONAL BYREF ANY, BYREF ANY, BYREF ANY, BYREF ANY, _

BYREF ANY, BYREF ANY, BYREF ANY, BYREF ANY, BYREF ANY, _

BYREF ANY, BYREF ANY, BYREF ANY, BYREF ANY, BYREF ANY, _

BYREF ANY, BYREF ANY _

) AS LONG

 

参数

 

op

 

[in]一个整数配置选项,用于确定要配置SQLite的什么属性。后续参数因第一个参数中的配置选项而异。

 

返回值

 

设置配置选项时,sqlite3_config返回SQLITE_OK。如果该选项未知或SQLite无法设置该选项,则该函数返回非零错误代码。

 

备注

 

sqlite3_config功能不是线程安全的。应用程序必须确保在运行sqlite3_config时,其他线程不会调用其他SQLite  函数。此外,sqlite3_config只能在库初始化之前使用sqlite3_initialize或关闭sqlite3_shutdown后调用。如果sqlite3_configsqlite3_initialize之后和sqlite3_shutdown之前调用,那么它将返回SQLITE_MISUSE。但是请注意,sqlite3_config可以作为应用程序定义的sqlite3_os_init的一部分进行调用。

 

C ++实现代码

 

/*

** This API allows applications to modify the global configuration of

** the SQLite library at run-time.

**

** This function should only be called when there are no outstanding

** database connections or memory allocations.  This function is not

** threadsafe.  Failure to heed these warnings can lead to unpredictable

** behavior.

*/

SQLITE_API int sqlite3_config(int op, ...){

va_list ap;

int rc = SQLITE_OK;

 

/* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while

** the SQLite library is in use. */

if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;

 

va_start(ap, op);

switch( op ){

 

  /* Mutex configuration options are only available in a threadsafe

  ** compile.

  */

#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0

  case SQLITE_CONFIG_SINGLETHREAD: {

    /* Disable all mutexing */

    sqlite3GlobalConfig.bCoreMutex = 0;

    sqlite3GlobalConfig.bFullMutex = 0;

    break;

  }

  case SQLITE_CONFIG_MULTITHREAD: {

    /* Disable mutexing of database connections */

    /* Enable mutexing of core data structures */

    sqlite3GlobalConfig.bCoreMutex = 1;

    sqlite3GlobalConfig.bFullMutex = 0;

    break;

  }

  case SQLITE_CONFIG_SERIALIZED: {

    /* Enable all mutexing */

    sqlite3GlobalConfig.bCoreMutex = 1;

    sqlite3GlobalConfig.bFullMutex = 1;

    break;

  }

  case SQLITE_CONFIG_MUTEX: {

    /* Specify an alternative mutex implementation */

    sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);

    break;

  }

  case SQLITE_CONFIG_GETMUTEX: {

    /* Retrieve the current mutex implementation */

    *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;

    break;

  }

#endif

 

 

  case SQLITE_CONFIG_MALLOC: {

    /* Specify an alternative malloc implementation */

    sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);

    break;

  }

  case SQLITE_CONFIG_GETMALLOC: {

    /* Retrieve the current malloc() implementation */

    if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();

    *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;

    break;

  }

  case SQLITE_CONFIG_MEMSTATUS: {

    /* Enable or disable the malloc status collection */

    sqlite3GlobalConfig.bMemstat = va_arg(ap, int);

    break;

  }

  case SQLITE_CONFIG_SCRATCH: {

    /* Designate a buffer for scratch memory space */

    sqlite3GlobalConfig.pScratch = va_arg(ap, void*);

    sqlite3GlobalConfig.szScratch = va_arg(ap, int);

    sqlite3GlobalConfig.nScratch = va_arg(ap, int);

    break;

  }

  case SQLITE_CONFIG_PAGECACHE: {

    /* Designate a buffer for page cache memory space */

    sqlite3GlobalConfig.pPage = va_arg(ap, void*);

    sqlite3GlobalConfig.szPage = va_arg(ap, int);

    sqlite3GlobalConfig.nPage = va_arg(ap, int);

    break;

  }

 

  case SQLITE_CONFIG_PCACHE: {

    /* no-op */

    break;

  }

  case SQLITE_CONFIG_GETPCACHE: {

    /* now an error */

    rc = SQLITE_ERROR;

    break;

  }

 

  case SQLITE_CONFIG_PCACHE2: {

    /* Specify an alternative page cache implementation */

    sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);

    break;

  }

  case SQLITE_CONFIG_GETPCACHE2: {

    if( sqlite3GlobalConfig.pcache2.xInit==0 ){

      sqlite3PCacheSetDefault();

    }

    *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;

    break;

  }

 

#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)

  case SQLITE_CONFIG_HEAP: {

    /* Designate a buffer for heap memory space */

    sqlite3GlobalConfig.pHeap = va_arg(ap, void*);

    sqlite3GlobalConfig.nHeap = va_arg(ap, int);

    sqlite3GlobalConfig.mnReq = va_arg(ap, int);

 

    if( sqlite3GlobalConfig.mnReq<1 ){

      sqlite3GlobalConfig.mnReq = 1;

    }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){

      /* cap min request size at 2^12 */

      sqlite3GlobalConfig.mnReq = (1<<12);

    }

 

    if( sqlite3GlobalConfig.pHeap==0 ){

      /* If the heap pointer is NULL, then restore the malloc implementation

      ** back to NULL pointers too.  This will cause the malloc to go

      ** back to its default implementation when sqlite3_initialize() is

      ** run.

      */

      memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));

    }else{

      /* The heap pointer is not NULL, then install one of the

      ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor

      ** ENABLE_MEMSYS5 is defined, return an error.

      */

#ifdef SQLITE_ENABLE_MEMSYS3

      sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();

#endif

#ifdef SQLITE_ENABLE_MEMSYS5

      sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();

#endif

    }

    break;

  }

#endif

 

  case SQLITE_CONFIG_LOOKASIDE: {

    sqlite3GlobalConfig.szLookaside = va_arg(ap, int);

    sqlite3GlobalConfig.nLookaside = va_arg(ap, int);

    break;

  }

 

  /* Record a pointer to the logger funcction and its first argument.

  ** The default is NULL.  Logging is disabled if the function pointer is

  ** NULL.

  */

  case SQLITE_CONFIG_LOG: {

    /* MSVC is picky about pulling func ptrs from va lists.

    ** http://support.microsoft.com/kb/47961

    ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));

    */

    typedef void(*LOGFUNC_t)(void*,int,const char*);

    sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);

    sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);

    break;

  }

 

  case SQLITE_CONFIG_URI: {

    sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);

    break;

  }

 

  default: {

    rc = SQLITE_ERROR;

    break;

  }

}

va_end(ap);

return rc;

}