<< sqlite3_errcode(handle) << "] " << sqlite3_errmsg(handle)
66 #define SQLITE_THROW(type, handle, msg) \ 68 CNcbiOstrstream ostr; \ 69 ostr << SQLITE_ERRMSG_STRM(handle, msg); \ 70 throw CSQLITE_Exception(DIAG_COMPILE_INFO, NULL, type, \ 71 CNcbiOstrstreamToString(ostr)); \ 83 #define SQLITE_LOG_ERROR(err_subcode, handle, msg) \ 84 ERR_POST_X(err_subcode, \ 85 SQLITE_ERRMSG_STRM(handle, msg)) 88 #ifdef HAVE_SQLITE3ASYNC_H 95 virtual void*
Main(
void)
104 #ifdef HAVE_SQLITE3_UNLOCK_NOTIFY 110CLockWaiter(
void) : m_WaitSem(new
CSemaphore(0, 1))
119 voidSignalUnlock(
void)
133s_OnUnlockNotify(
void** p_waiters,
intn_waiters)
135 for(
int i= 0;
i< n_waiters; ++
i) {
136CLockWaiter* waiter =
reinterpret_cast<CLockWaiter*
>(p_waiters[
i]);
137waiter->SignalUnlock();
161 switch(sqlite3_code) {
169 #ifdef HAVE_SQLITE3_UNLOCK_NOTIFY 171 caseSQLITE_LOCKED_SHAREDCACHE:
176waiter = &s_LockWaiters[handle];
178ret = sqlite3_unlock_notify(handle, &s_OnUnlockNotify, waiter);
179 if(ret == SQLITE_LOCKED) {
181 "Database is deadlocked");
186sqlite3_code = SQLITE_BUSY;
189 caseSQLITE_CONSTRAINT:
191 "Constraint violation in statement");
214 #define SQLITE_SAFE_CALL_EX(call, conn_handle, ex_type, err_msg, retry) \ 215 int sql_ret = SQLITE_ERROR; \ 219 sql_ret = s_ProcessErrorCode(conn_handle, sql_ret, \ 220 CSQLITE_Exception::ex_type); \ 221 if (sql_ret == SQLITE_BUSY) { \ 225 catch (CSQLITE_Exception& ex) { \ 226 CNcbiOstrstream ostr; \ 228 NCBI_RETHROW_SAME(ex, CNcbiOstrstreamToString(ostr)); \ 231 while (sql_ret == SQLITE_BUSY) 235 #define SQLITE_SAFE_CALL(sqlite_call, conn_handle, ex_type, err_msg) \ 236 SQLITE_SAFE_CALL_EX((sqlite_call), conn_handle, ex_type, err_msg, \ 247 case eDBOpen:
return "eDBOpen";
262 #ifdef HAVE_SQLITE3ASYNC_H 275(*s_AsyncWritesThread)->Run();
283 if(sqlite3async_control(SQLITEASYNC_LOCKFILES,
int(lock_files))
286 ERR_POST_X(7,
"File locking for asynchronous writing mode is not set " 287 "because of an error");
301 #ifdef HAVE_SQLITE3ASYNC_H 302 _VERIFY(sqlite3async_control(SQLITEASYNC_HALT, SQLITEASYNC_HALT_IDLE)
307(*s_AsyncWritesThread)->Join();
311sqlite3async_shutdown();
314 _VERIFY(sqlite3_shutdown() == SQLITE_OK);
323 _VERIFY(sqlite3_config(SQLITE_CONFIG_MEMSTATUS, 0) == SQLITE_OK);
324 _VERIFY(sqlite3_initialize() == SQLITE_OK);
325 #ifdef HAVE_SQLITE3ASYNC_H 326 _VERIFY(sqlite3async_initialize(
NULL, 0) == SQLITE_OK);
327 _VERIFY(sqlite3async_control(SQLITEASYNC_HALT, SQLITEASYNC_HALT_NEVER)
336 #if defined(NCBI_THREADS) && !defined(HAVE_SQLITE3_UNLOCK_NOTIFY) 339 "Cannot turn on shared cache because of lack of capabilities");
342 if(sqlite3_enable_shared_cache(enable) != SQLITE_OK) {
343 ERR_POST_X(9,
"Setting for sharing cache is not set because of an error");
351 intres = sqlite3_config(SQLITE_CONFIG_PCACHE, methods);
352 if(res != SQLITE_OK) {
354 "Custom page cache is not set, err_code = "<< res);
361 returnsqlite3_vfs_find(
NULL);
367 intres = sqlite3_vfs_register(vfs, set_default);
368 if(res != SQLITE_OK) {
370 "Custom VFS is not registered, err_code = "<< res);
384 const char* vfs =
NULL;
385 #ifdef HAVE_SQLITE3ASYNC_H 389vfs = SQLITEASYNC_VFSNAME;
394? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
397 flags| SQLITE_OPEN_NOMUTEX,
400 "Error opening database '" 403 _ASSERT(sql_ret == SQLITE_OK);
417 intret = sqlite3_close(handle);
418 if(ret != SQLITE_OK) {
420 "Cannot close connection");
422 while(ret == SQLITE_BUSY || ret == SQLITE_LOCKED
423#ifdef HAVE_SQLITE3_UNLOCK_NOTIFY
424|| ret == SQLITE_LOCKED_SHAREDCACHE
428sqlite3_stmt *
stmt;
429 while( (
stmt= sqlite3_next_stmt(handle, 0)) !=
NULL) {
430ret = sqlite3_finalize(
stmt);
431 if(ret != SQLITE_OK) {
433 "Cannot finalize statement");
437ret = sqlite3_close(handle);
438 if(ret != SQLITE_OK) {
440 "Failed retry closing connection");
445 if(ret != SQLITE_OK) {
447 "Error closing database connection, " 459 if(fls & (fls - 1)) {
461 "Incorrect flags in CSQLITE_Connection: 0x" 478m_CacheSize((unsigned
int)(-1)),
502sqlite3_extended_result_codes(handle, 1);
512 x_ExecuteSql(handle,
"PRAGMA legacy_file_format = OFF");
539 x_ExecuteSql(handle,
"PRAGMA journal_mode = PERSIST");
542 x_ExecuteSql(handle,
"PRAGMA journal_mode = TRUNCATE");
570 x_ExecuteSql(handle,
"PRAGMA auto_vacuum = INCREMENTAL");
586 "Cannot change vacuuming flags after database creation");
596 string sql(
"PRAGMA incremental_vacuum(");
620 string dbname= shared ?
"file::memory:?cache=shared":
":memory:";
626sqlite3* mh = mdb->m_HandlePool.Get();
628sqlite3_backup* backup = sqlite3_backup_init(mh,
"main", fh,
"main");
630sqlite3_backup_step(backup, -1);
631sqlite3_backup_finish(backup);
634 if(sqlite3_errcode(mh) != SQLITE_OK) {
635mdb->m_HandlePool.Return(mh);
638mdb->m_InMemory = mh;
642mdb->m_HandlePool.Return(mh);
645 returnmdb.release();
675 "Error preparing statement for \""<<
sql<<
"\"" 677 _ASSERT(sql_ret == SQLITE_OK);
686 "Cannot finalize statement");
690 #define STMT_BIND_IMPL(sql_type, str_type, index_and_val) \ 692 _ASSERT(m_StmtHandle); \ 693 SQLITE_SAFE_CALL((NCBI_NAME2(sqlite3_bind_, sql_type) \ 694 (m_StmtHandle, index_and_val)), \ 695 m_ConnHandle, eStmtBind, \ 696 "Error binding "<< #str_type \
697<< " parameter N " << index \
699_ASSERT(sql_ret == SQLITE_OK); \
705 #define STMT_BIND_NO_VAL(sql_type, str_type, index) \ 706 STMT_BIND_IMPL(sql_type, str_type, index) 708 #define STMT_BIND(sql_type, str_type, index, val) \ 709 STMT_BIND_IMPL(sql_type, str_type, index COMMA val) 711 #define STMT_BIND3(sql_type, str_type, index, val1, val2, val3) \ 712 STMT_BIND_IMPL(sql_type, str_type, index COMMA val1 COMMA val2 COMMA val3) 731 val.data(),
int(
val.size()), SQLITE_TRANSIENT);
764 "Error stepping through statement results",
766 _ASSERT(sql_ret == SQLITE_ROW || sql_ret == SQLITE_DONE);
767 returnsql_ret == SQLITE_ROW;
779 "Error reseting statement");
780 _ASSERT(sql_ret == SQLITE_OK);
792 "Error clearing bindings");
793 _ASSERT(sql_ret == SQLITE_OK);
803 "Error requesting column name");
813 const unsigned char*
buf= sqlite3_column_text(
m_StmtHandle, col_ind);
814 size_t size=
static_cast<size_t>(
816 result.append(
reinterpret_cast<const char*
>(
buf),
size);
823 return(
size_t)sqlite3_column_bytes(
m_StmtHandle, col_ind);
831 size_tbuf_size =
static_cast<size_t>(
833buf_size =
min(buf_size,
size);
844 catch(exception& ex) {
845 ERR_POST(
"Error resetting statement: "<< ex.what());
852 template<
boolreadwrite>
880 #define BLOB_SAFE_CALL(blob_call, ex_type, err_msg) \ 881 SQLITE_SAFE_CALL((blob_call), m_ConnHandle, ex_type, \ 882 err_msg << " "<< m_Table << "." << m_Column \
883<< " where rowid = " << m_Rowid)
900m_Database(
"main"),
936eBlobOpen,
"Error openning blob" 938 _ASSERT(sql_ret == SQLITE_OK);
952eBlobClose,
"Error closing blob");
953 _ASSERT(sql_ret == SQLITE_OK);
982 static_cast<int>(
size),
985 "Error reading from position "<<
m_Position<<
" of blob" 987 _ASSERT(sql_ret == SQLITE_OK);
1008 data,
int(to_write),
1012<<
" "<< to_write <<
" bytes for blob" 1014 _ASSERT(sql_ret == SQLITE_OK);
1016 data=
static_cast<const char*
>(
data) + to_write;
1024 string sql(
"update ");
1032 sql+=
"||?2 where rowid=?1";
ncbi::TMaskedQueryRegions mask
Background thread for executing all asynchronous writes to database if they exist.
virtual void * Main(void)
Derived (user-created) class must provide a real thread function.
Object reading and writing blob directly (mostly without executing select or update statements),...
size_t m_Size
Size of the blob.
Int8 m_Rowid
Rowid for the row where blob is located.
size_t Read(void *buffer, size_t size)
Read from blob at current position.
void x_CloseBlob(void)
Close low-level blob object.
void x_Init(void)
Initialize the object.
sqlite3 * m_ConnHandle
Low-level connection handle provided for this blob object.
void Write(const void *data, size_t size)
Write to blob at current position.
void x_OpenBlob(bool readwrite=false)
Open low-level blob object.
CSQLITE_Connection * m_Conn
Connection this blob object belongs to.
CSQLITE_Blob(CSQLITE_Connection *conn, CTempString table, CTempString column, Int8 rowid)
Create object reading and writing blob Identified row with blob should exist in database,...
size_t GetSize(void)
Get size of the blob.
string m_Column
Column name for blob.
sqlite3_blob * m_BlobHandle
Low-level handle of the blob.
string m_Database
Database name for blob.
string m_Table
Table name for blob.
size_t m_Position
Current position inside the blob.
AutoPtr< CSQLITE_Statement > m_AppendStmt
Statement used for appending to existing blob value.
Connection to SQLite database.
TOperationFlags m_Flags
Flags for object operation.
TOperationFlags GetFlags(void) const
Get flags controlling database connection operation.
int TOperationFlags
Bit mask of EOperationFlags.
EOperationFlags
Flags controlling specifics of database connection operation.
@ fWritesAsync
All writes are asynchronous - updates return as quick as they can, actual writes to database happen i...
@ fTempToFile
Use actual disk file for temporary storage.
@ fJournalTruncate
Journal on disk, size of journal file is nullified after each transaction.
@ fJournalPersist
Journal on disk, journal file can only grow, never shrinks.
@ fExternalMT
Object and all statements and blobs created on top of it will not be used from different threads simu...
@ fVacuumOn
Vacuuming is on, database file has always the minimum size possible.
@ fJournalOff
Journaling is completely off (not recommended - transactions cannot be rollbacked unless they consist...
@ fSyncOff
Synchronization is off, database can be corrupted on OS crash or power outage.
@ fReadOnly
The DB is read-only. Also forces fVacuumOff flag.
@ fSyncFull
Full synchronization, database cannot be corrupted.
@ fVacuumManual
Vacuuming is only by request.
@ fSyncOn
Synchronization is on, there is a slight chance of database corruption when OS crashes.
@ fJournalDelete
Journal on disk, journal file is deleted after each transaction.
@ fVacuumOff
Vacuuming is off, database file can only grow.
@ fJournalMemory
Journaling is entirely in-memory.
@ fTempToMemory
Mode of storing temporary data.
unsigned int m_CacheSize
Recommended size of cache for the database.
sqlite3 * LockHandle(void)
Lock and return low-level connection handle.
void x_CheckFlagsValidity(TOperationFlags flags, EOperationFlags mask)
Check that only one flag in specific group of flags is given.
CSQLITE_Statement * CreateVacuumStmt(size_t max_free_size)
Create statement for executing manual vacuuming.
const string & GetFileName(void) const
Get database file name for the connection.
string m_FileName
File name of SQLite database.
static CSQLITE_Connection * CreateInMemoryDatabase(CTempString file_name, bool shared=false)
Create a read-only copy of the database in memory.
~CSQLITE_Connection(void)
CSQLITE_Connection(CTempString file_name, TOperationFlags flags=eDefaultFlags)
Connect to SQLite database.
void SetupNewConnection(sqlite3 *handle)
Setup newly created low-level connection handle.
void UnlockHandle(sqlite3 *handle)
Unlock unused low-level connection handle.
THandlePool m_HandlePool
Pool of low-level database connections.
unsigned int m_PageSize
Page size inside the database.
void DeleteDatabase(void)
Delete database under this connection.
void x_ExecuteSql(sqlite3 *handle, CTempString sql)
Execute sql statement on given low-level connection handle.
void SetFlags(TOperationFlags flags)
Change flags controlling database connection operation.
Exception thrown from all SQLite-related objects and functions.
virtual const char * GetErrCodeString(void) const override
Get error code interpreted as text.
@ eDeadLock
SQLite detected possible deadlock between different threads.
@ eStmtStep
Error stepping through the statement.
@ eStmtBind
Error binding statement parameters.
@ eStmtPrepare
Error preparing statement.
@ eConstraint
Constraint violation during statement execution.
@ eUnknown
Unknown error.
@ eDBOpen
Error during database opening.
@ eBlobClose
Error closing blob object.
@ eBlobWrite
Error writing directly to blob.
@ eBlobOpen
Error opening blob object.
@ eBlobRead
Error reading directly from blob.
@ eStmtReset
Error reseting statement.
@ eStmtFinalize
Error finalizing statement.
static void Initialize(void)
Initialization of SQLite and tuning some default parameters.
static void RunAsyncWritesThread(void)
Launch background thread doing all asynchronous writes to databases.
static sqlite3_vfs * GetDefaultVFS(void)
Get default virtual file system installed in SQLite.
static void SetAsyncWritesFileLocking(bool lock_files)
Setup the option for asynchronous file system to do the actual locking of database files on disk.
static void Finalize(void)
Finish all SQLite operations.
static void RegisterCustomVFS(sqlite3_vfs *vfs, bool set_default=true)
Register new virtual file system in SQLite.
static void EnableSharedCache(bool enable=true)
Enable use of the same cache by different connections to the same database file.
static void SetCustomPageCache(sqlite3_pcache_methods *methods)
Install non-default cache for all SQLite databases.
Object factory creating sqlite3* handles.
CSQLITE_HandleFactory(CSQLITE_Connection *conn)
CSQLITE_Connection * m_Conn
Connection object this factory is bound to.
void DeleteObject(sqlite3 *handle)
Destroy database handle.
sqlite3 * CreateObject(void)
Create new database handle.
CSQLITE_Statement * m_Stmt
Statement this object is bound to.
~CSQLITE_StatementLock(void)
SQL statement executing on SQLite database.
void x_Prepare(CTempString sql)
Prepare new statement if it's not empty.
void Bind(int index, int val)
Bind integer value to parameter index.
CSQLITE_Connection * m_Conn
Connection this statement belongs to.
size_t GetBlob(int col_ind, void *buffer, size_t size) const
Read blob value from column col_ind in current row.
void ClearBindings(void)
Reset all bindings to the statement to their initial NULL values.
void Reset(void)
Reset the statement to release all locks and to be ready to execute again.
bool Step(void)
Step through results of the statement.
sqlite3 * m_ConnHandle
Low-level connection handle provided for this statement.
CStringUTF8 GetColumnName(int col_ind) const
Get name of column at index col_ind in result set.
size_t GetBlobSize(int col_ind) const
Get size of blob value from column col_ind in current row.
sqlite3_stmt * m_StmtHandle
Low-level statement handle.
void BindNull(int index)
Bind null value to parameter index.
void Execute(void)
Execute statement without returning any result.
void BindZeroedBlob(int index, size_t size)
Bind blob value of given size containing only zeros to parameter index.
string GetString(int col_ind) const
Get text value from column col_ind in current row.
void x_Finalize(void)
Finalize current statement.
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
The NCBI C++ standard methods for dealing with std::string.
static CS_CONNECTION * conn
static const char * column
void reset(element_type *p=0, EOwnership ownership=eTakeOwnership)
Reset will delete the old pointer (if owned), set content to the new value, and assume the ownership ...
element_type * get(void) const
Get pointer.
#define ERR_POST_X(err_subcode, message)
Error posting with default error code and given error subcode.
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
TErrCode GetErrCode(void) const
Get error code.
#define STD_CATCH_ALL_X(err_subcode, message)
Standard handling of "exception"-derived exceptions; catches non-standard exceptions and generates "u...
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
#define NCBI_THROW_FMT(exception_class, err_code, message)
The same as NCBI_THROW but with message processed as output to ostream.
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
virtual bool Remove(TRemoveFlags flags=eRecursive) const
Remove a directory entry.
int64_t Int8
8-byte (64-bit) signed integer
void Return(TObjType *obj)
Return object to the pool for future use.
void Clear(void)
Delete all objects returned to the pool so far and clean it.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
static CStringUTF8 AsUTF8(const CTempString &src, EEncoding encoding, EValidate validate=eNoValidate)
Convert into UTF8 from a C/C++ string.
static string UInt8ToString(Uint8 value, TNumToStringFlags flags=0, int base=10)
Convert UInt8 to string.
char * dbname(DBPROCESS *dbproc)
Get name of current database.
unsigned int
A callback function used to compare two keys in a database.
Definition of all error codes used in bdb library (bdb.lib and ncbi_xcache_bdb.lib).
<!DOCTYPE HTML >< html > n< header > n< title > PubSeq Gateway Help Page</title > n< style > n table
static void text(MDB_val *v)
const struct ncbi::grid::netcache::search::fields::SIZE size
void SleepMilliSec(unsigned long ml_sec, EInterruptOnSignal onsignal=eRestartOnSignal)
NCBI C++ auxiliary debug macros.
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
static int s_ProcessErrorCode(sqlite3 *handle, int sqlite3_code, CSQLITE_Exception::EErrCode ex_code)
Unified handling of return codes from sqlite.
CGuard< CSQLITE_Blob, SSQLITE_BlobOpen< false >, SSQLITE_BlobClose > TBlobReadGuard
Guard opening blob for read-only access at construction and closing it at destruction.
#define BLOB_SAFE_CALL(blob_call, ex_type, err_msg)
#define STMT_BIND(sql_type, str_type, index, val)
#define SQLITE_SAFE_CALL(sqlite_call, conn_handle, ex_type, err_msg)
The same as SQLITE_SAFE_CALL_EX but without additional calls between retries.
#define SQLITE_THROW(type, handle, msg)
Convenient macro for throwing CSQLITE_Exception containing error message from SQLite.
const int kSQLITE_DefPageSize
Default size of database pages (maximum allowed in SQLite)
static CSafeStatic< CRef< CThread > > s_AsyncWritesThread
Background thread for executing all asynchronous writings to databases.
#define SQLITE_SAFE_CALL_EX(call, conn_handle, ex_type, err_msg, retry)
Make call to sqlite3 function with automatic retry mechanism.
#define STMT_BIND3(sql_type, str_type, index, val1, val2, val3)
CGuard< CSQLITE_Blob, SSQLITE_BlobOpen< true >, SSQLITE_BlobClose > TBlobWriteGuard
Guard opening blob for read-write access at construction and closing it at destruction.
DEFINE_STATIC_FAST_MUTEX(s_AsyncThreadInitMutex)
#define SQLITE_LOG_ERROR(err_subcode, handle, msg)
Same as SQLITE_THROW but for logging errors in places where throwing exceptions is impossible.
#define STMT_BIND_NO_VAL(sql_type, str_type, index)
static bool s_SQLITE3_Initialized
Helper structure for closing the blob from CGuard<>
void operator()(CSQLITE_Blob &blob)
Helper structure for opening the blob from CGuard<>
void operator()(CSQLITE_Blob &blob)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4