OpenASIP 2.2
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
SQLiteConnection Class Reference

#include <SQLiteConnection.hh>

Inheritance diagram for SQLiteConnection:
Inheritance graph
Collaboration diagram for SQLiteConnection:
Collaboration graph

Public Member Functions

 SQLiteConnection (sqlite3 *connection)
 
virtual ~SQLiteConnection ()
 
virtual int updateQuery (const std::string &queryString)
 
virtual void DDLQuery (const std::string &queryString)
 
virtual RelationalDBQueryResultquery (const std::string &queryString, bool init=true)
 
virtual void beginTransaction ()
 
virtual void rollback ()
 
virtual void commit ()
 
virtual RowID lastInsertRowID ()
 
virtual bool tableExistsInDB (const std::string &tableName)
 
virtual int rowCountInTable (const std::string &tableName)
 
void throwIfSQLiteError (int result)
 
void finalizeQuery (sqlite3_stmt *statement)
 
virtual int version ()
 
virtual void updateVersion (int version)
 
- Public Member Functions inherited from RelationalDBConnection
virtual ~RelationalDBConnection ()
 

Private Member Functions

sqlite3_stmt * compileQuery (const std::string &queryString)
 

Private Attributes

sqlite3 * connection_
 SQLite connection handle is saved to this.
 
bool transactionActive_
 

Detailed Description

Implementation of RelationalDBConnection interface for SQLite library.

Definition at line 47 of file SQLiteConnection.hh.

Constructor & Destructor Documentation

◆ SQLiteConnection()

SQLiteConnection::SQLiteConnection ( sqlite3 *  connection)

Constructor.

Parameters
connectionA pointer to a SQLite connection handle.

Definition at line 45 of file SQLiteConnection.cc.

45 :
46 connection_(connection), transactionActive_(false) {
47}
sqlite3 * connection_
SQLite connection handle is saved to this.

◆ ~SQLiteConnection()

SQLiteConnection::~SQLiteConnection ( )
virtual

Destructor.

Closes the connection.

Definition at line 54 of file SQLiteConnection.cc.

54 {
55 sqlite3_close(connection_);
56}

References connection_.

Member Function Documentation

◆ beginTransaction()

void SQLiteConnection::beginTransaction ( )
virtual

Starts a new database transaction.

Also ends the possible previous transaction successfully (commit).

Exceptions
RelationalDBExceptionIn case a database error occured.

Implements RelationalDBConnection.

Definition at line 124 of file SQLiteConnection.cc.

124 {
125 if (transactionActive_) {
126 commit();
127 }
128 updateQuery("BEGIN;");
129 transactionActive_ = true;
130}
virtual void commit()
virtual int updateQuery(const std::string &queryString)

References commit(), transactionActive_, and updateQuery().

Here is the call graph for this function:

◆ commit()

void SQLiteConnection::commit ( )
virtual

Ends the current database transaction successfully and commits all the changes in the transaction to the database.

Exceptions
RelationalDBExceptionIn case a database error occured.

Implements RelationalDBConnection.

Definition at line 151 of file SQLiteConnection.cc.

151 {
152 updateQuery("COMMIT;");
153 transactionActive_ = false;
154}

References transactionActive_, and updateQuery().

Referenced by beginTransaction().

Here is the call graph for this function:

◆ compileQuery()

sqlite3_stmt * SQLiteConnection::compileQuery ( const std::string &  queryString)
private

Compiles a SQLite query.

Parameters
queryStringThe SQL statement to compile.
Returns
The SQLite virtual machine that should be used to execute the query.
Exceptions
RelationalDBExceptionIn case a database error occured.

Definition at line 270 of file SQLiteConnection.cc.

270 {
271 // "virtual machine" used by SQLite to execute the statements
272 sqlite3_stmt* stmt = NULL;
273 const char* dummy = NULL;
274
275 throwIfSQLiteError(sqlite3_prepare(
276 connection_, queryString.c_str(), queryString.length(),
277 &stmt, &dummy));
278 return stmt;
279}
SimValue dummy(32)
a dummy simvalue which is given for operands that are not bound
void throwIfSQLiteError(int result)

References connection_, dummy, and throwIfSQLiteError().

Referenced by query(), and updateQuery().

Here is the call graph for this function:

◆ DDLQuery()

void SQLiteConnection::DDLQuery ( const std::string &  queryString)
virtual

Performs a SQL Data Definition Language query, that is a query that may change the structure of the database (CREATE TABLE, etc.).

Does not autocommit the changes in case the update is in the middle of transaction.

Parameters
queryStringThe query string.
Exceptions
RelationalDBExceptionIn case a database error occured.

Implements RelationalDBConnection.

Definition at line 98 of file SQLiteConnection.cc.

98 {
99 updateQuery(queryString);
100}

References updateQuery().

Here is the call graph for this function:

◆ finalizeQuery()

void SQLiteConnection::finalizeQuery ( sqlite3_stmt *  statement)

Finalizes a SQLite query, frees the virtual machine.

Does nothing if the statement is NULL.

Parameters
statementThe SQLite statement to free.
Exceptions
RelationalDBExceptionIn case there was errors (can be cumulated from a previous sqlite3_step() call).

Definition at line 291 of file SQLiteConnection.cc.

291 {
292 if (statement == NULL) {
293 return;
294 }
295
296 throwIfSQLiteError(sqlite3_finalize(statement));
297}

References throwIfSQLiteError().

Referenced by updateQuery(), and SQLiteQueryResult::~SQLiteQueryResult().

Here is the call graph for this function:

◆ lastInsertRowID()

RowID SQLiteConnection::lastInsertRowID ( )
virtual

Returns the row ID of the most recent insert in the database.

Returns
The row ID.

Implements RelationalDBConnection.

Definition at line 162 of file SQLiteConnection.cc.

162 {
163 return sqlite3_last_insert_rowid(connection_);
164}

References connection_.

◆ query()

RelationalDBQueryResult * SQLiteConnection::query ( const std::string &  queryString,
bool  init = true 
)
virtual

Performs a data retrieval query (SELECT).

Parameters
queryStringThe query string.
Returns
A handle to the query result set.
Exceptions
RelationalDBExceptionIn case a database error occured.

Implements RelationalDBConnection.

Definition at line 110 of file SQLiteConnection.cc.

110 {
111 sqlite3_stmt* stmt = compileQuery(queryString);
112 SQLiteQueryResult* result = new SQLiteQueryResult(stmt, this, init);
113 return result;
114}
sqlite3_stmt * compileQuery(const std::string &queryString)

References compileQuery().

Referenced by rowCountInTable(), tableExistsInDB(), updateVersion(), and version().

Here is the call graph for this function:

◆ rollback()

void SQLiteConnection::rollback ( )
virtual

Ends the current database transaction unsuccessfully and rollbacks all the changes the queries in the transaction would have done.

Exceptions
RelationalDBExceptionIn case a database error occured.

Implements RelationalDBConnection.

Definition at line 139 of file SQLiteConnection.cc.

139 {
140 updateQuery("ROLLBACK;");
141 transactionActive_ = false;
142}

References transactionActive_, and updateQuery().

Here is the call graph for this function:

◆ rowCountInTable()

int SQLiteConnection::rowCountInTable ( const std::string &  tableName)
virtual

Return number of entries in the given table.

Parameters
tableNameName of the table.
Returns
Number of entries in table.
Exceptions
RelationalDBExceptionIn case a database error occurred, call was made in the middle of an active transaction or the table does not exists.

Implements RelationalDBConnection.

Definition at line 218 of file SQLiteConnection.cc.

218 {
219 if (!tableExistsInDB(tableName)) {
220 throw RelationalDBException(__FILE__, __LINE__,
221 "SQLiteConnection::rowCountInTable()",
222 "Table was not found.");
223 }
224 string query = "SELECT count(*) FROM " + tableName + ";";
225
226 RelationalDBQueryResult* result = this->query(query, false);
227 assert(result->hasNext());
228 result->next();
229 const DataObject& count = result->data(0);
230 int countAsInt = -1;
231
232 try {
233 countAsInt = count.integerValue(); // boolValue is zero if DataObject
234 // has NULL value.
235 } catch (NumberFormatException& e) {
236 throw RelationalDBException(__FILE__, __LINE__,
237 "SQLiteConnection::tableExistsInDB()",
238 "Exception from DataObject: " + e.errorMessage());
239 }
240
241 delete result;
242 assert(countAsInt > -1);
243 return countAsInt;
244}
#define assert(condition)
virtual int integerValue() const
std::string errorMessage() const
Definition Exception.cc:123
virtual const DataObject & data(std::size_t column) const =0
virtual bool tableExistsInDB(const std::string &tableName)
virtual RelationalDBQueryResult * query(const std::string &queryString, bool init=true)

References assert, RelationalDBQueryResult::data(), Exception::errorMessage(), RelationalDBQueryResult::hasNext(), DataObject::integerValue(), RelationalDBQueryResult::next(), query(), and tableExistsInDB().

Here is the call graph for this function:

◆ tableExistsInDB()

bool SQLiteConnection::tableExistsInDB ( const std::string &  tableName)
virtual

Checks if database has given table by name.

Parameters
tableNameName of the table
Returns
True if db has the table. Otherwise false.
Exceptions
RelationalDBExceptionIn case a database error occurred or call was made in the middle of an active transaction.

Implements RelationalDBConnection.

Definition at line 177 of file SQLiteConnection.cc.

177 {
178 if (transactionActive_) {
179 throw RelationalDBException(__FILE__, __LINE__,
180 "SQLiteConnection::tableExistsInDB()",
181 "Illegal call during active transaction.");
182 }
183
184 string query = "SELECT count(*) "
185 "FROM sqlite_master "
186 "WHERE type = 'table' and name = '" + tableName + "';";
187
188 RelationalDBQueryResult* result = this->query(query, false);
189 assert(result->hasNext());
190 result->next();
191 const DataObject& count = result->data(0);
192
193 int intBoolValue = 0;
194
195 try {
196 intBoolValue = count.integerValue(); // boolValue is zero if DataObject
197 // has NULL value.
198 } catch (NumberFormatException& e) {
199 throw RelationalDBException(__FILE__, __LINE__,
200 "SQLiteConnection::tableExistsInDB()",
201 "Exception from DataObject: " + e.errorMessage());
202 }
203
204 delete result;
205 return intBoolValue;
206}

References assert, RelationalDBQueryResult::data(), Exception::errorMessage(), RelationalDBQueryResult::hasNext(), DataObject::integerValue(), RelationalDBQueryResult::next(), query(), and transactionActive_.

Referenced by rowCountInTable().

Here is the call graph for this function:

◆ throwIfSQLiteError()

void SQLiteConnection::throwIfSQLiteError ( int  result)

Throws a RelationalDBException if result value indicates an SQLite error.

Parameters
resultThe value from a SQLite API call.
Exceptions
RelationalDBExceptionThrown if result is not SQLITE_OK.

Definition at line 253 of file SQLiteConnection.cc.

253 {
254 if (result != SQLITE_OK && result != SQLITE_ROW &&
255 result != SQLITE_DONE) {
256
257 string error = sqlite3_errmsg(connection_);
258 throw RelationalDBException(__FILE__, __LINE__, "", error);
259 }
260}

References connection_.

Referenced by SQLiteQueryResult::bindInt(), SQLiteQueryResult::bindString(), compileQuery(), finalizeQuery(), SQLiteQueryResult::reset(), and updateQuery().

◆ updateQuery()

int SQLiteConnection::updateQuery ( const std::string &  queryString)
virtual

Performs a query that changes the database (UPDATE/INSERT/DELETE).

Does not autocommit the changes in case the update is in the middle of transaction.

Parameters
queryStringThe query string.
Returns
Number of rows affected by the change.
Exceptions
RelationalDBExceptionIn case a database error occured.

Implements RelationalDBConnection.

Definition at line 69 of file SQLiteConnection.cc.

69 {
70 if (connection_ == NULL) {
72 __FILE__, __LINE__, "SQLiteConnection::updateQuery()",
73 "Not connected!");
74 }
75
76 // "virtual machine" used by SQLite to execute the statements
77 sqlite3_stmt* stmt = compileQuery(queryString);
78
79 assert(stmt != NULL);
80
81 throwIfSQLiteError(sqlite3_step(stmt));
82 finalizeQuery(stmt);
83
84 return sqlite3_changes(connection_);
85}
void finalizeQuery(sqlite3_stmt *statement)

References assert, compileQuery(), connection_, finalizeQuery(), and throwIfSQLiteError().

Referenced by beginTransaction(), commit(), DDLQuery(), rollback(), and updateVersion().

Here is the call graph for this function:

◆ updateVersion()

void SQLiteConnection::updateVersion ( int  version)
virtual

Set database version number.

Parameters
versionNew database version.

Implements RelationalDBConnection.

Definition at line 335 of file SQLiteConnection.cc.

335 {
336 string query = "pragma user_version = " +
337 std::to_string(version) + ";";
338 this->updateQuery(query);
339}
virtual int version()

References query(), updateQuery(), and version().

Here is the call graph for this function:

◆ version()

int SQLiteConnection::version ( )
virtual

Return database version number.

Returns
Database version.

Implements RelationalDBConnection.

Definition at line 305 of file SQLiteConnection.cc.

305 {
306 int version = 0;
307
308 string query = "pragma user_version;";
309
310 RelationalDBQueryResult* result = this->query(query, false);
311
312
313 assert(result->hasNext());
314 result->next();
315 const DataObject& count = result->data(0);
316
317 try {
318 version = count.integerValue();
319 } catch (NumberFormatException& e) {
320 throw RelationalDBException(__FILE__, __LINE__,
321 "SQLiteConnection::version()",
322 "Exception from DataObject: " + e.errorMessage());
323 }
324
325 delete result;
326 return version;
327}

References assert, RelationalDBQueryResult::data(), Exception::errorMessage(), RelationalDBQueryResult::hasNext(), DataObject::integerValue(), RelationalDBQueryResult::next(), query(), and version().

Referenced by updateVersion(), and version().

Here is the call graph for this function:

Member Data Documentation

◆ connection_

sqlite3* SQLiteConnection::connection_
private

SQLite connection handle is saved to this.

Definition at line 78 of file SQLiteConnection.hh.

Referenced by compileQuery(), lastInsertRowID(), throwIfSQLiteError(), updateQuery(), and ~SQLiteConnection().

◆ transactionActive_

bool SQLiteConnection::transactionActive_
private

Definition at line 80 of file SQLiteConnection.hh.

Referenced by beginTransaction(), commit(), rollback(), and tableExistsInDB().


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