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

db connection wrapper for SQLite. More...

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. 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. More...
 
 ~DbExecBase (void) noexcept
 cleanup connection resources. More...
 
bool isTransaction (void)
 return true in transaction scope. More...
 
- Public Member Functions inherited from openm::IDbExec
virtual ~IDbExec (void) noexcept=0
 close db-connection and cleanup connection resources. 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. More...
 
static bool isValidProviderName (const char *i_sqlProvider)
 check if provider name is valid. More...
 
static int maxDbTableNameSize (const string &i_sqlProvider)
 return max length of db table or view name. More...
 
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. More...
 
static string makeSqlBeginTransaction (const string &i_sqlProvider)
 return sql statement to begin transaction (db-provider specific). More...
 
static string makeSqlCommitTransaction (const string &i_sqlProvider)
 return sql statement to commit transaction (db-provider specific). More...
 
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...
 
- Protected Member Functions inherited from openm::DbExecBase
bool isTransactionNonOwn (void)
 return true if other thread have active transaction. More...
 
void setTransactionActive (void)
 set transaction ownership status. More...
 
void releaseTransaction (void)
 release transaction: clean active transaction status. More...
 
string strConnProperty (const string &i_key) const
 return string value of connection property by key (case neutral) or empty "" string if key not found. More...
 
bool boolConnProperty (const string &i_key) const
 return true if value of connection property is 'true', 'yes', '1' (case neutral). More...
 
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). More...
 
- 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. More...
 
- 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). More...
 

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

◆ ~DbExecSqlite()

DbExecSqlite::~DbExecSqlite ( void  )
noexcept

close db-connection and cleanup connection resources.

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);
}

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.

◆ 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: