aboutsummaryrefslogtreecommitdiffstats
path: root/src/base
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/base/database.cpp36
-rw-r--r--src/base/database.hpp12
2 files changed, 30 insertions, 18 deletions
diff --git a/src/base/database.cpp b/src/base/database.cpp
index 36971bd4..3d1580e2 100644
--- a/src/base/database.cpp
+++ b/src/base/database.cpp
@@ -62,6 +62,19 @@ namespace usdx
return instance;
}
+ sqlite3_stmt *StatDatabase::sqlite_prepare(const std::string sqlStatement)
+ {
+ sqlite3_stmt *sqliteStatement;
+ if (SQLITE_OK != sqlite3_prepare_v2(database, sqlStatement.c_str(), sqlStatement.length(), &sqliteStatement, NULL)) {
+ sqlite3_finalize(sqliteStatement);
+
+ LOG4CXX_ERROR(log, "Error '" << sqlite3_errmsg(database) << "' in SQL '" << sqlStatement << "'");
+ throw "Error preparing statement.";
+ }
+
+ return sqliteStatement;
+ }
+
void StatDatabase::init(const std::string filename)
{
LOG4CXX_DEBUG(log, "Initializing Database: " << filename);
@@ -167,16 +180,8 @@ namespace usdx
int StatDatabase::get_version(void)
{
- char sqlStatement[] = "PRAGMA user_version;";
- sqlite3_stmt *sqliteStatement;
int result = -1;
-
- if (SQLITE_OK != sqlite3_prepare_v2(database, sqlStatement, strlen(sqlStatement), &sqliteStatement, NULL)) {
- sqlite3_finalize(sqliteStatement);
-
- LOG4CXX_ERROR(log, "Error '" << sqlite3_errmsg(database) << "' in SQL '" << sqlStatement << "'");
- throw "Error preparing statement.";
- }
+ sqlite3_stmt *sqliteStatement = sqlite_prepare("PRAGMA user_version;");
int rc = sqlite3_step(sqliteStatement);
if (rc == SQLITE_DONE || rc == SQLITE_ROW) {
@@ -395,18 +400,13 @@ namespace usdx
*/
time_t StatDatabase::get_stat_reset(void)
{
- std::string sqlStatement = "SELECT [ResetTime] FROM [";
- sqlStatement += usdx_statistics_info + std::string("];");
-
- sqlite3_stmt *sqliteStatement;
int result = -1;
- if (SQLITE_OK != sqlite3_prepare_v2(database, sqlStatement.c_str(), sqlStatement.length(), &sqliteStatement, NULL)) {
- sqlite3_finalize(sqliteStatement);
+ std::string sqlStatement = "SELECT [ResetTime] FROM [";
+ sqlStatement += usdx_statistics_info;
+ sqlStatement += "];";
- LOG4CXX_ERROR(log, "Error '" << sqlite3_errmsg(database) << "' in SQL '" << sqlStatement << "'");
- throw "Error preparing statement.";
- }
+ sqlite3_stmt *sqliteStatement = sqlite_prepare(sqlStatement);
int rc = sqlite3_step(sqliteStatement);
if (rc == SQLITE_DONE || rc == SQLITE_ROW) {
diff --git a/src/base/database.hpp b/src/base/database.hpp
index 8e589feb..c984fcfd 100644
--- a/src/base/database.hpp
+++ b/src/base/database.hpp
@@ -54,6 +54,18 @@ namespace usdx
*/
sqlite3 *database;
+ /**
+ * Wrapper arround the sqlite_prepare_v2 function with propper
+ * logging and exception throwing on error.
+ *
+ * @param sqlStatement SQL Statement for preparing to
+ * sqlite3_stmt
+ * @return Pointner to a sqlite3_stmt used for binding
+ * parameters and excuting the statement. Need to be freed
+ * with sqlite3_finalize.
+ */
+ sqlite3_stmt *sqlite_prepare(const std::string sqlStatement);
+
// Singleton
StatDatabase(std::string filename);