OpenM++ runtime library (libopenm)
openm::DbExecSqlite Class Reference

db connection wrapper for SQLite. More...

#include <dbExecSqlite.h>

Inheritance diagram for openm::DbExecSqlite:
Collaboration diagram for openm::DbExecSqlite:

Public Member Functions

 DbExecSqlite (const string &i_connectionStr)
 create new db-connection. More...
 
 ~DbExecSqlite (void) noexcept
 close db-connection and cleanup connection resources.
 
string provider (void) const override
 return sql provider name, e.g. More...
 
int selectToInt (const string &i_sql, int i_default) override
 select integer value of first (row,column) or default if no rows or value IS NULL. More...
 
long long selectToLong (const string &i_sql, long long i_default) override
 select long value of first (row,column) or default if no rows or value IS NULL. More...
 
string selectToStr (const string &i_sql) override
 select string value of first (row,column) or empty "" string if no rows or value IS NULL. More...
 
bool selectToBool (const string &i_sql) override
 select boolean value of first (row,column) or false if no rows or value IS NULL. More...
 
double selectToDouble (const string &i_sql, double i_default) override
 select double value of first (row,column) or default if no rows or value IS NULL. More...
 
vector< string > selectRowStr (const string &i_sql) override
 select string vector of first row or empty vector if no rows, empty "" string used for NULLs. More...
 
IRowBaseVec selectRowVector (const string &i_sql, const IRowAdapter &i_adapter) override
 select vector of rows, each row created and field values set by row adapter. More...
 
IRowBaseList selectRowList (const string &i_sql, const IRowAdapter &i_adapter) override
 select list of rows, each row created and field values set by row adapter. More...
 
void selectToRowProcessor (const string &i_sql, const IRowAdapter &i_adapter, IRowProcessor &i_processor) override
 select and process rows: each row created by row adapter and passed to processor. More...
 
size_t selectColumn (const string &i_sql, int i_column, const type_info &i_type, size_t i_size, void *io_valueArr) override
 select column into io_valueArray[i_size] buffer and return row count. More...
 
size_t update (const string &i_sql) override
 execute sql statement (update, insert, delete, create, etc). More...
 
void beginTransaction (void) override
 begin transaction, throw exception if transaction already active or statement is active. More...
 
unique_lock< recursive_mutex > beginTransactionThreaded (void) override
 begin transaction in multi-threaded environment, throw exception if transaction already active or statement is active. More...
 
void commit (void) override
 commit transaction, does nothing if no active transaction, throw exception if statement is active. More...
 
void rollback (void) override
 rollback transaction, does nothing if no active transaction, throw exception if statement is active. More...
 
bool isTransaction (void) override
 return true in transaction scope. More...
 
void createStatement (const string &i_sql, int i_paramCount, const type_info **i_typeArr) override
 create new statement with specified parameters. More...
 
void releaseStatement (void) noexcept override
 release statement resources. More...
 
void executeStatement (int i_paramCount, const DbValue *i_valueArr) override
 execute statement with parameters. More...
 
void runSqlScript (const string &i_sqlScript) override
 parse and execute list of sql statements. More...
 
- Public Member Functions inherited from openm::DbExecBase
 DbExecBase (const string &i_connectionStr)
 prepare to open new db-connection.
 
 ~DbExecBase (void) noexcept
 cleanup connection resources.
 
bool isTransaction (void)
 return true in transaction scope.
 
- Public Member Functions inherited from openm::IDbExec
virtual ~IDbExec (void) noexcept=0
 close db-connection and cleanup connection resources. More...
 
virtual string provider (void) const =0
 return sql provider name, e.g. More...
 
virtual int selectToInt (const string &i_sql, int i_default)=0
 select integer value of first (row,column) or default if no rows or value IS NULL. More...
 
virtual long long selectToLong (const string &i_sql, long long i_default)=0
 select long value of first (row,column) or default if no rows or value IS NULL. More...
 
virtual string selectToStr (const string &i_sql)=0
 select string value of first (row,column) or empty "" string if no rows or value IS NULL. More...
 
virtual bool selectToBool (const string &i_sql)=0
 select boolean value of first (row,column) or false if no rows or value IS NULL. More...
 
virtual double selectToDouble (const string &i_sql, double i_default)=0
 select double value of first (row,column) or default if no rows or value IS NULL. More...
 
virtual vector< string > selectRowStr (const string &i_sql)=0
 select string vector of first row or empty vector if no rows, empty "" string used for NULLs. More...
 
virtual IRowBaseVec selectRowVector (const string &i_sql, const IRowAdapter &i_adapter)=0
 select vector of rows, each row created and field values set by row adapter. More...
 
virtual IRowBaseList selectRowList (const string &i_sql, const IRowAdapter &i_adapter)=0
 select list of rows, each row created and field values set by row adapter. More...
 
virtual void selectToRowProcessor (const string &i_sql, const IRowAdapter &i_adapter, IRowProcessor &i_processor)=0
 select and process rows: each row created by row adapter and passed to processor. More...
 
virtual size_t selectColumn (const string &i_sql, int i_column, const type_info &i_type, size_t i_size, void *io_valueArr)=0
 select column into io_valueArray[i_size] buffer and return row count. More...
 
virtual size_t update (const string &i_sql)=0
 execute sql statement (update, insert, delete, create, etc). More...
 
virtual void beginTransaction (void)=0
 begin transaction. More...
 
virtual unique_lock< recursive_mutex > beginTransactionThreaded (void)=0
 begin transaction in multi-threaded environment. More...
 
virtual void commit (void)=0
 commit transaction, does nothing if no active transaction. More...
 
virtual void rollback (void)=0
 rollback transaction. More...
 
virtual bool isTransaction (void)=0
 return true in transaction scope. More...
 
virtual void createStatement (const string &i_sql, int i_paramCount, const type_info **i_typeArr)=0
 create new statement with specified parameters. More...
 
virtual void releaseStatement (void) noexcept=0
 release statement resources. More...
 
virtual void executeStatement (int i_paramCount, const DbValue *i_valueArr)=0
 execute statement with parameters. More...
 
virtual void runSqlScript (const string &i_sqlScript)=0
 parse and execute list of sql statements. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from openm::IDbExec
static IDbExeccreate (const string &i_sqlProvider, const string &i_connectionStr)
 db-connection factory: create new db-connection. More...
 
static list< string > parseListOfProviderNames (const string &i_sqlProviderNames)
 return list of provider names from supplied comma or semicolon separated string or exception on invalid name.
 
static bool isValidProviderName (const char *i_sqlProvider)
 check if provider name is valid.
 
static bool isSqlKeyword (const char *i_keyword, size_t i_length=0)
 return true if i_keword matched one of SQL reserved keywords, comparison is case neutral and limited by i_length chars
 
static const string makeDbNamePrefix (int i_id, const string &i_src)
 make prefix part of db table name by shorten source name, ie: ageSexProvince = > ageSexPr
 
static const string makeDbNameSuffix (int i_id, const string &i_src, const string i_digest)
 make unique part of db table name by using digest or crc32(digest)
 
static string bigIntTypeName (const string &i_sqlProvider)
 return type name for BIGINT sql type
 
static string floatTypeName (const string &i_sqlProvider)
 return type name for FLOAT standard sql type
 
static string textTypeName (const string &i_sqlProvider, int i_size)
 return column type DDL for long VARCHAR columns, use it for len > 255.
 
static string valueDbType (const string &i_sqlProvider, const string &i_typeName, int i_typeId)
 return db type name by model type for specific db provider
 
static string makeSqlBeginTransaction (const string &i_sqlProvider)
 return sql statement to begin transaction (db-provider specific).
 
static string makeSqlCommitTransaction (const string &i_sqlProvider)
 return sql statement to commit transaction (db-provider specific).
 
static string makeSqlCreateTableIfNotExist (const string &i_sqlProvider, const string &i_tableName, const string &i_tableBodySql)
 make sql statement to create table if not exists. More...
 
static string makeSqlCreateViewReplace (const string &i_sqlProvider, const string &i_viewName, const string &i_viewBodySql)
 make sql statement to create or replace view. More...
 
- Static Public Attributes inherited from openm::IDbExec
static const int maxDbTableNameSize = 63
 max length of db table or view name. More...
 
- Protected Member Functions inherited from openm::DbExecBase
bool isTransactionNonOwn (void)
 return true if other thread have active transaction.
 
void setTransactionActive (void)
 set transaction ownership status.
 
void releaseTransaction (void)
 release transaction: clean active transaction status.
 
string strConnProperty (const string &i_key) const
 return string value of connection property by key (case neutral) or empty "" string if key not found.
 
bool boolConnProperty (const string &i_key) const
 return true if value of connection property is 'true', 'yes', '1' (case neutral).
 
long long longConnProperty (const string &i_key, long i_default) const
 return long value of connection property or default value if key not found (case neutral).
 
- Static Protected Member Functions inherited from openm::DbExecBase
static void runSqlScript (IDbExec *i_dbExec, const string &i_sqlScript)
 parse and execute list of sql statements.
 
- Protected Attributes inherited from openm::DbExecBase
thread::id trxThreadId
 if true then transaction is active
 
NoCaseMap connProps
 connection properties as key+value string pairs (case neutral).
 

Detailed Description

db connection wrapper for SQLite.

Constructor & Destructor Documentation

◆ DbExecSqlite()

DbExecSqlite::DbExecSqlite ( const string &  i_connectionStr)

create new db-connection.

Parameters
[in]i_connectionStrdatabase connection string.

connection string example:
Database=fileName.sqlite; Timeout=86400; OpenMode=ReadWrite; DeleteExisting=true;
connection string options:
Database (required) database file name or URI, it can be empty
Timeout (optional) timeout in seconds for table lock, default=0
OpenMode (optional) open mode: ReadOnly, ReadWrite, Create, default=ReadOnly
DeleteExisting (optional) if true the delete existing database file, default=false

Member Function Documentation

◆ beginTransaction()

void DbExecSqlite::beginTransaction ( void  )
overridevirtual

begin transaction, throw exception if transaction already active or statement is active.

Implements openm::IDbExec.

◆ beginTransactionThreaded()

unique_lock< recursive_mutex > DbExecSqlite::beginTransactionThreaded ( void  )
overridevirtual

begin transaction in multi-threaded environment, throw exception if transaction already active or statement is active.

Implements openm::IDbExec.

◆ commit()

void DbExecSqlite::commit ( void  )
overridevirtual

commit transaction, does nothing if no active transaction, throw exception if statement is active.

Implements openm::IDbExec.

◆ createStatement()

void DbExecSqlite::createStatement ( const string &  i_sql,
int  i_paramCount,
const type_info **  i_typeArr 
)
overridevirtual

create new statement with specified parameters.

Parameters
[in]i_sqlsql to create statement
[in]i_paramCountnumber of parameters
[in]i_typeArrarray of parameters type, use char * for strings

usage example:

while(...) {
for (int k = 0; k < paramCount; k++) {
valueArr[k] = some value;
}
executeStatement(paramCount, valueArr);
}
void createStatement(const string &i_sql, int i_paramCount, const type_info **i_typeArr) override
create new statement with specified parameters.
Definition: dbExecSqlite.cpp:768
void executeStatement(int i_paramCount, const DbValue *i_valueArr) override
execute statement with parameters.
Definition: dbExecSqlite.cpp:860
void releaseStatement(void) noexcept override
release statement resources.
Definition: dbExecSqlite.cpp:116

Implements openm::IDbExec.

◆ executeStatement()

void DbExecSqlite::executeStatement ( int  i_paramCount,
const DbValue i_valueArr 
)
overridevirtual

execute statement with parameters.

Parameters
[in]i_paramCountnumber of parameters
[in]i_valueArrarray of parameters value

this method can be used only after createStatement() call
usage example:

while(...) {
for (int k = 0; k < paramCount; k++) {
valueArr[k] = some value;
}
executeStatement(paramCount, valueArr);
}

Implements openm::IDbExec.

◆ isTransaction()

bool openm::DbExecSqlite::isTransaction ( void  )
inlineoverridevirtual

return true in transaction scope.

Implements openm::IDbExec.

◆ provider()

string openm::DbExecSqlite::provider ( void  ) const
inlineoverridevirtual

return sql provider name, e.g.

: SQLITE

Implements openm::IDbExec.

◆ releaseStatement()

void DbExecSqlite::releaseStatement ( void  )
overridevirtualnoexcept

release statement resources.

usage example:

while(...) {
for (int k = 0; k < paramCount; k++) {
valueArr[k] = some value;
}
executeStatement(paramCount, valueArr);
}

Implements openm::IDbExec.

◆ rollback()

void DbExecSqlite::rollback ( void  )
overridevirtual

rollback transaction, does nothing if no active transaction, throw exception if statement is active.

Implements openm::IDbExec.

◆ runSqlScript()

void openm::DbExecSqlite::runSqlScript ( const string &  i_sqlScript)
inlineoverridevirtual

parse and execute list of sql statements.

Implements openm::IDbExec.

◆ selectColumn()

size_t DbExecSqlite::selectColumn ( const string &  i_sql,
int  i_column,
const type_info &  i_type,
size_t  i_size,
void *  io_valueArr 
)
overridevirtual

select column into io_valueArray[i_size] buffer and return row count.

Parameters
[in]i_sqlselect sql query
[in]i_columnzero-based column index
[in]i_typetype of io_valueArr array (target type of values)
[in]i_sizesize of io_valueArr array
[in,out]io_valueArrone dimensional array to put selected values
Returns
row count, may throw exception if row count not equal i_size

convert db-values into target type and put into io_valueArr
if db-value in current row IS NULL then do not change io_valueArray at this index
if target type is string then io_valueArray expected to be string[]
it may throw exception if selected row count not equal i_size

Implements openm::IDbExec.

◆ selectRowList()

IRowBaseList DbExecSqlite::selectRowList ( const string &  i_sql,
const IRowAdapter i_adapter 
)
overridevirtual

select list of rows, each row created and field values set by row adapter.

Parameters
[in]i_sqlselect sql query
[in]i_adapterrow adapter class to create rows and set values
Returns
list of rows

Implements openm::IDbExec.

◆ selectRowStr()

vector< string > DbExecSqlite::selectRowStr ( const string &  i_sql)
overridevirtual

select string vector of first row or empty vector if no rows, empty "" string used for NULLs.

Parameters
[in]i_sqlselect sql query
Returns
string vector of first row or empty vector if no rows, empty "" string used for NULLs

Implements openm::IDbExec.

◆ selectRowVector()

IRowBaseVec DbExecSqlite::selectRowVector ( const string &  i_sql,
const IRowAdapter i_adapter 
)
overridevirtual

select vector of rows, each row created and field values set by row adapter.

Parameters
[in]i_sqlselect sql query
[in]i_adapterrow adapter class to create rows and set values
Returns
vector of rows

Implements openm::IDbExec.

◆ selectToBool()

bool DbExecSqlite::selectToBool ( const string &  i_sql)
overridevirtual

select boolean value of first (row,column) or false if no rows or value IS NULL.

Parameters
[in]i_sqlselect sql query
Returns
boolean value of first (row,column) or false if no rows or value IS NULL

Implements openm::IDbExec.

◆ selectToDouble()

double DbExecSqlite::selectToDouble ( const string &  i_sql,
double  i_default 
)
overridevirtual

select double value of first (row,column) or default if no rows or value IS NULL.

Parameters
[in]i_sqlselect sql query
[in]i_defaultdefault value if no row(s) selected or db-value IS NULL
Returns
double value of first (row,column) or default if no rows or value IS NULL

Implements openm::IDbExec.

◆ selectToInt()

int DbExecSqlite::selectToInt ( const string &  i_sql,
int  i_default 
)
overridevirtual

select integer value of first (row,column) or default if no rows or value IS NULL.

Parameters
[in]i_sqlselect sql query
[in]i_defaultdefault value if no row(s) selected or db-value IS NULL
Returns
integer value of first (row,column) or default if no rows or value IS NULL

Implements openm::IDbExec.

◆ selectToLong()

long long DbExecSqlite::selectToLong ( const string &  i_sql,
long long  i_default 
)
overridevirtual

select long value of first (row,column) or default if no rows or value IS NULL.

Parameters
[in]i_sqlselect sql query
[in]i_defaultdefault value if no row(s) selected or db-value IS NULL
Returns
long value of first (row,column) or default if no rows or value IS NULL

Implements openm::IDbExec.

◆ selectToRowProcessor()

void DbExecSqlite::selectToRowProcessor ( const string &  i_sql,
const IRowAdapter i_adapter,
IRowProcessor i_processor 
)
overridevirtual

select and process rows: each row created by row adapter and passed to processor.

Implements openm::IDbExec.

◆ selectToStr()

string DbExecSqlite::selectToStr ( const string &  i_sql)
overridevirtual

select string value of first (row,column) or empty "" string if no rows or value IS NULL.

Parameters
[in]i_sqlselect sql query
Returns
string value of first (row,column) or empty "" string if no rows or value IS NULL

Implements openm::IDbExec.

◆ update()

size_t DbExecSqlite::update ( const string &  i_sql)
overridevirtual

execute sql statement (update, insert, delete, create, etc).

Parameters
[in]i_sqlsql statement of "update" type (update, insert, delete, create, etc.)
Returns
number of affected rows (db-vendor specific).

Implements openm::IDbExec.


The documentation for this class was generated from the following files: