1 #ifndef DB_SQLITE_SQLITEWRAPP__HPP
2 #define DB_SQLITE_SQLITEWRAPP__HPP
45 #ifdef HAVE_SQLITE3ASYNC_H
46 # include <sqlite3async.h>
101 #ifdef HAVE_SQLITE3ASYNC_H
212 #ifdef HAVE_SQLITE3ASYNC_H
222 #ifdef HAVE_SQLITE3ASYNC_H
292 void Vacuum(
size_t max_free_size);
315 bool shared =
false);
376 void Bind (
int index,
int val);
379 void Bind (
int index,
unsigned int val);
382 void Bind (
int index,
long val);
385 void Bind (
int index,
unsigned long val);
386 #if !NCBI_INT8_IS_LONG
396 void Bind (
int index,
double val);
448 int GetInt (
int col_ind)
const;
713 m_ConnHandle(conn_handle),
740 #if !NCBI_INT8_IS_LONG
759 Bind(index,
static_cast<long>(
val));
765 Bind(index,
static_cast<unsigned long>(
val));
Object reading and writing blob directly (mostly without executing select or update statements),...
size_t GetPosition(void)
Get current position inside the blob.
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.
CSQLITE_Blob(const CSQLITE_Blob &)
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,...
void SetAppendStatement(CSQLITE_Statement *stmt)
Set statement to use when appending to existing blob if necessary.
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.
CSQLITE_Blob & operator=(const CSQLITE_Blob &)
void ResetPosition(size_t position=0)
Set current position inside the blob to desired value.
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.
@ fDefaultWrites
Default value for writes mode group of flags.
@ fWritesAsync
All writes are asynchronous - updates return as quick as they can, actual writes to database happen i...
@ fDefaultSync
Default value for synchronization group of flags.
@ 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.
@ fDefaultVacuum
Default value for vacuuming group of flags.
@ fJournalOff
Journaling is completely off (not recommended - transactions cannot be rollbacked unless they consist...
@ eDefaultFlags
Default value of all flags.
@ fInternalMT
Object and all statements and blobs created on top of it should support internal synchronization betw...
@ 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.
@ fDefaultTemp
Default value for "temporary" group of flags.
@ fDefaultJournal
Default value for journaling group of flags.
@ fDefaultMT
Default value for multi-threading group of flags.
@ 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.
@ fWritesSync
Mode of doing all writes to the database.
@ fJournalMemory
Journaling is entirely in-memory.
@ fTempToMemory
Mode of storing temporary data.
unsigned int m_CacheSize
Recommended size of cache for the database.
CObjPool< sqlite3, CSQLITE_HandleFactory > THandlePool
CSQLITE_Connection(const CSQLITE_Connection &)
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.
void SetCacheSize(unsigned int size)
Set recommended size of the database cache (number of pages).
const string & GetFileName(void) const
Get database file name for the connection.
void Vacuum(size_t max_free_size)
Try to shrink database and free up to max_free_size bytes of disk space.
string m_FileName
File name of SQLite database.
unsigned int GetCacheSize(void)
Get recommended size of the database cache.
void SetPageSize(unsigned int size)
Set page size for the database (in bytes).
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.
CSQLITE_Connection & operator=(const CSQLITE_Connection &)
unsigned int GetPageSize(void)
Get page size used in the database (in bytes)
void x_ExecuteSql(sqlite3 *handle, CTempString sql)
Execute sql statement on given low-level connection handle.
void ExecuteSql(CTempString sql)
Execute sql statement without returning any results.
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.
NCBI_EXCEPTION_DEFAULT(CSQLITE_Exception, CException)
@ eBadCall
Method called when there's no enough capabilities to finish it successfully.
@ 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.
@ 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.
@ eWrongFlags
Incorrect set of flags in connection constructor.
@ eStmtFinalize
Error finalizing statement.
Utility class for some global-purpose functions tuning SQLite as a whole as opposed to tuning connect...
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.
CSQLITE_Global & operator=(CSQLITE_Global &)
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.
CSQLITE_Global(CSQLITE_Global &)
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.
"Scoped" statement activity object.
CSQLITE_Statement * m_Stmt
Statement this object is bound to.
CSQLITE_StatementLock(const CSQLITE_StatementLock &)
~CSQLITE_StatementLock(void)
CSQLITE_StatementLock(CSQLITE_Statement *stmt)
Bind activity control to the given statement.
CSQLITE_Statement & operator*(void)
Smart pointer's transformation.
CSQLITE_StatementLock & operator=(const CSQLITE_StatementLock &)
CSQLITE_Statement * operator->(void)
Smart pointer's transformation.
SQL statement executing on SQLite database.
CSQLITE_Statement(const CSQLITE_Statement &)
void x_Prepare(CTempString sql)
Prepare new statement if it's not empty.
double GetDouble(int col_ind) const
Get double value from column col_ind in current row.
Int8 GetInt8(int col_ind) const
Get 64-bit integer value from column col_ind in current row.
void Bind(int index, int val)
Bind integer value to parameter index.
int GetColumnsCount(void) const
Get number of columns in result set.
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.
CSQLITE_Statement & operator=(const CSQLITE_Statement &)
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.
Int8 GetLastInsertedRowid(void) const
Get rowid of the row inserted in last statement execution.
int GetInt(int col_ind) const
Get integer value from column col_ind in current row.
int GetChangedRowsCount(void) const
Get number of rows changed during last insert, delete or update statement.
void SetSql(CTempString sql)
Change sql text for the object and prepare new statement.
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.
CSQLITE_Statement(CSQLITE_Connection *conn, CTempString sql=kEmptyStr)
Create and prepare statement for given database connection.
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 ...
@ eNoOwnership
No ownership is assumed.
EErrCode
Error types that an application can generate.
int64_t Int8
8-byte (64-bit) signed integer
uint64_t Uint8
8-byte (64-bit) unsigned integer
void Return(TObjType *obj)
Return object to the pool for future use.
TObjType * Get(void)
Get object from the pool, create if necessary.
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.
unsigned int
A callback function used to compare two keys in a database.
<!DOCTYPE HTML >< html > n< header > n< title > PubSeq Gateway Help Page</title > n< style > n table
const struct ncbi::grid::netcache::search::fields::SIZE size
Defines NCBI C++ exception handling.
Miscellaneous common-use basic types and functionality.
Multi-threading – mutexes; rw-locks; semaphore.
Helper structure for closing the blob from CGuard<>
Helper structure for opening the blob from CGuard<>