2018-02-02 12:27:57 +00:00
|
|
|
#include "model/database/database.h"
|
|
|
|
|
2018-02-12 13:24:11 +00:00
|
|
|
Database::Database(string filename){
|
|
|
|
this->filename = filename;
|
2018-03-30 12:33:48 +00:00
|
|
|
this->queryCount = 0;
|
2018-02-12 13:24:11 +00:00
|
|
|
openConnection();
|
|
|
|
}
|
2018-02-02 12:27:57 +00:00
|
|
|
|
2018-02-12 13:24:11 +00:00
|
|
|
Database::~Database(){
|
2018-03-01 16:19:13 +00:00
|
|
|
closeConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultTable Database::executeSQL(string statement){
|
|
|
|
sqlite3_stmt* stmt;
|
|
|
|
this->sql = statement;
|
|
|
|
this->returnCode = sqlite3_prepare_v2(this->db, statement.c_str(), -1, &stmt, NULL);
|
|
|
|
if (this->returnCode != SQLITE_OK){
|
2018-03-02 10:30:16 +00:00
|
|
|
fprintf(stderr, "Unable to successfully prepare SQL statement. Error code: %d\n\tError Message: %s\n", this->returnCode, sqlite3_errmsg(this->db));
|
2018-03-31 10:53:31 +00:00
|
|
|
return ResultTable(this->returnCode);
|
2018-03-01 16:19:13 +00:00
|
|
|
}
|
2018-03-31 10:53:31 +00:00
|
|
|
ResultTable t(statement);
|
2018-03-01 16:19:13 +00:00
|
|
|
t.extractData(stmt);
|
|
|
|
|
|
|
|
this->returnCode = sqlite3_finalize(stmt);
|
2018-03-30 12:33:48 +00:00
|
|
|
this->queryCount++;
|
2018-03-01 16:19:13 +00:00
|
|
|
|
|
|
|
return t;
|
2018-02-02 12:27:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 10:30:16 +00:00
|
|
|
bool Database::insertInto(string tableName, vector<string> columnNames, vector<string> values){
|
|
|
|
if (columnNames.size() != values.size() || columnNames.empty()){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
string query = "INSERT INTO "+tableName+" (";
|
|
|
|
string cols = combineVector(columnNames, ", ");
|
|
|
|
string vals = combineVector(values, ", ");
|
|
|
|
query += cols + ") VALUES (" + vals + ");";
|
2018-03-03 07:38:32 +00:00
|
|
|
ResultTable t = this->executeSQL(query);
|
|
|
|
return (t.getReturnCode() == SQLITE_DONE);
|
|
|
|
}
|
|
|
|
|
2018-05-22 21:06:11 +00:00
|
|
|
bool Database::insertInto(string tableName, string columnName, string value){
|
|
|
|
if (columnName.empty() || value.empty() || tableName.empty()){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
string query = "INSERT INTO " + tableName + " (" + columnName + ") VALUES (" + value + ");";
|
|
|
|
ResultTable t = this->executeSQL(query);
|
|
|
|
return (t.getReturnCode() == SQLITE_DONE);
|
|
|
|
}
|
|
|
|
|
2018-03-03 07:38:32 +00:00
|
|
|
ResultTable Database::selectFrom(string tableName, string columnNames, string conditions){
|
|
|
|
if (columnNames.size() == 0 || tableName.empty()){
|
|
|
|
return ResultTable();
|
|
|
|
}
|
2018-03-10 07:51:17 +00:00
|
|
|
string query = "SELECT " + columnNames + " FROM " + tableName + " " + conditions + ";";
|
2018-03-03 07:38:32 +00:00
|
|
|
return this->executeSQL(query);
|
2018-03-02 10:30:16 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 09:21:00 +00:00
|
|
|
bool Database::deleteFrom(string tableName, string conditions){
|
|
|
|
if (tableName.empty()){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
string query = "DELETE FROM " + tableName + " " + conditions + ";";
|
|
|
|
ResultTable t = this->executeSQL(query);
|
|
|
|
if (t.getReturnCode() != SQLITE_DONE){
|
|
|
|
fprintf(stderr, "Can't delete from table %s.Return code: %d\n%s\n", tableName.c_str(), t.getReturnCode(), sqlite3_errmsg(this->db));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 12:27:57 +00:00
|
|
|
void Database::openConnection(){
|
|
|
|
this->returnCode = sqlite3_open(this->filename.c_str(), &this->db);
|
2018-02-12 13:24:11 +00:00
|
|
|
if (this->returnCode || this->db == NULL){
|
|
|
|
this->dbIsOpen = false;
|
2018-02-02 12:27:57 +00:00
|
|
|
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
|
|
|
|
exit(EXIT_FAILURE);
|
2018-02-12 13:24:11 +00:00
|
|
|
} else {
|
|
|
|
this->dbIsOpen = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Database::closeConnection(){
|
|
|
|
this->returnCode = sqlite3_close(this->db);
|
2018-03-02 10:30:16 +00:00
|
|
|
this->dbIsOpen = false;
|
|
|
|
}
|
|
|
|
|
2018-03-31 10:53:31 +00:00
|
|
|
void Database::beginTransaction(){
|
|
|
|
this->executeSQL("BEGIN;");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Database::commitTransaction(){
|
|
|
|
this->executeSQL("COMMIT;");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Database::rollbackTransaction(){
|
|
|
|
this->executeSQL("ROLLBACK;");
|
|
|
|
}
|
|
|
|
|
2018-03-02 10:30:16 +00:00
|
|
|
string Database::combineVector(std::vector<string> strings, string mid){
|
|
|
|
if (strings.empty()){
|
|
|
|
return "";
|
|
|
|
}
|
2018-03-03 07:38:32 +00:00
|
|
|
std::string result = surroundString(strings[0], "'");
|
2018-03-02 10:30:16 +00:00
|
|
|
for (std::vector<std::string>::iterator it = strings.begin() + 1; it != strings.end(); ++it){
|
2018-03-03 07:38:32 +00:00
|
|
|
result += mid + surroundString((*it), "'");
|
2018-03-02 10:30:16 +00:00
|
|
|
}
|
|
|
|
return result;
|
2018-02-12 13:24:11 +00:00
|
|
|
}
|
|
|
|
|
2018-03-03 07:38:32 +00:00
|
|
|
string Database::surroundString(string s, string surround){
|
|
|
|
return surround+s+surround;
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:24:11 +00:00
|
|
|
bool Database::tableExists(string tableName){
|
2018-03-01 16:19:13 +00:00
|
|
|
if (tableName.empty() || this->db == NULL || !this->dbIsOpen){
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-10 07:51:17 +00:00
|
|
|
ResultTable t = this->selectFrom("sqlite_master", "name", "WHERE type='table' AND name='"+tableName+"'");
|
2018-03-01 16:19:13 +00:00
|
|
|
return !t.isEmpty();
|
2018-02-02 12:27:57 +00:00
|
|
|
}
|
2018-03-02 10:30:16 +00:00
|
|
|
|
2018-03-30 12:33:48 +00:00
|
|
|
int Database::getLastInsertedRowId() const{
|
2018-03-02 10:30:16 +00:00
|
|
|
return sqlite3_last_insert_rowid(this->db);
|
|
|
|
}
|
2018-03-30 12:33:48 +00:00
|
|
|
|
|
|
|
unsigned long Database::getQueryCount() const{
|
|
|
|
return this->queryCount;
|
|
|
|
}
|