Commit 23439795 authored by Matthieu Tanon's avatar Matthieu Tanon

Adapt database code to soci4 and fix build error

parent 111f3c92
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <memory> //smart ptrs #include <memory> //smart ptrs
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include <functional>
namespace lime { namespace lime {
......
...@@ -46,27 +46,29 @@ namespace lime { ...@@ -46,27 +46,29 @@ namespace lime {
* *
* @param[in] filename The path to DB file * @param[in] filename The path to DB file
*/ */
Db::Db(std::string filename) : sql{sqlite3, filename}{ Db::Db(string filename) : sql{sqlite3, filename}{
constexpr int db_module_table_not_in_base = -2;
constexpr int db_module_table_not_holding_lime_row = -1; constexpr int db_module_table_not_holding_lime_row = -1;
int userVersion=db_module_table_not_holding_lime_row; int userVersion=db_module_table_not_holding_lime_row;
try { sql<<"PRAGMA foreign_keys = ON;"; // make sure this connection enable foreign keys
transaction tr(sql);
// CREATE OR INGORE TABLE db_module_version(
sql<<"CREATE TABLE IF NOT EXISTS db_module_version("
"name VARCHAR(16) PRIMARY KEY,"
"version UNSIGNED INTEGER NOT NULL"
")";
sql<<"SELECT version FROM db_module_version WHERE name='lime'", into(userVersion); sql<<"SELECT version FROM db_module_version WHERE name='lime'", into(userVersion);
} catch(...) {
// the select generates an exception is the table doesn't exist
userVersion = db_module_table_not_in_base;
}
sql<<"PRAGMA foreign_keys = ON;"; // make sure this connection enable foreign keys if (userVersion == lime::settings::DBuserVersion)
if (userVersion != lime::settings::DBuserVersion) { return;
if (userVersion > lime::settings::DBuserVersion) { /* nothing to do if we encounter a superior version number than expected, just hope it is compatible */ if (userVersion > lime::settings::DBuserVersion) { /* nothing to do if we encounter a superior version number than expected, just hope it is compatible */
BCTBX_SLOGE<<"Lime module database schema version found in DB(v "<<userVersion<<") is more recent than the one currently supported by the lime module(v "<<static_cast<unsigned int>(lime::settings::DBuserVersion)<<")"; BCTBX_SLOGE<<"Lime module database schema version found in DB(v "<<userVersion<<") is more recent than the one currently supported by the lime module(v "<<static_cast<unsigned int>(lime::settings::DBuserVersion)<<")";
} else { /* Perform update if needed */ return;
transaction tr(sql); // begin a transaction which will hold all the create table queries }
/* Perform update if needed */
// update the schema version in DB // update the schema version in DB
if (userVersion!=db_module_table_not_in_base) { // db_module_table exists
if (userVersion == db_module_table_not_holding_lime_row) { // but not any lime row in it if (userVersion == db_module_table_not_holding_lime_row) { // but not any lime row in it
sql<<"INSERT INTO db_module_version(name,version) VALUES('lime',:DbVersion)", use(lime::settings::DBuserVersion); sql<<"INSERT INTO db_module_version(name,version) VALUES('lime',:DbVersion)", use(lime::settings::DBuserVersion);
} else { // and we had an older version } else { // and we had an older version
...@@ -75,12 +77,6 @@ Db::Db(std::string filename) : sql{sqlite3, filename}{ ...@@ -75,12 +77,6 @@ Db::Db(std::string filename) : sql{sqlite3, filename}{
tr.commit(); // commit all the previous queries tr.commit(); // commit all the previous queries
return; return;
} }
} else { // The db_module_version table doen't exist, create it
sql<<"CREATE TABLE db_module_version( \
name VARCHAR(16) PRIMARY KEY, \
version UNSIGNED INTEGER NOT NULL)";
sql<<"INSERT INTO db_module_version(name,version) VALUES('lime',:DbVersion)", use(lime::settings::DBuserVersion);
}
// create the lime DB: // create the lime DB:
...@@ -205,8 +201,6 @@ Db::Db(std::string filename) : sql{sqlite3, filename}{ ...@@ -205,8 +201,6 @@ Db::Db(std::string filename) : sql{sqlite3, filename}{
FOREIGN KEY(Uid) REFERENCES lime_LocalUsers(Uid) ON UPDATE CASCADE ON DELETE CASCADE);"; FOREIGN KEY(Uid) REFERENCES lime_LocalUsers(Uid) ON UPDATE CASCADE ON DELETE CASCADE);";
tr.commit(); // commit all the previous queries tr.commit(); // commit all the previous queries
}
}
}; };
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment