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;
|
|
|
|
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);
|
|
|
|
ResultTable t;
|
|
|
|
if (this->returnCode != SQLITE_OK){
|
|
|
|
fprintf(stderr, "Unable to successfully prepare SQL statement. Error code: %d\nError Message: %s\n", this->returnCode, sqlite3_errmsg(this->db));
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
t.extractData(stmt);
|
|
|
|
|
|
|
|
this->returnCode = sqlite3_finalize(stmt);
|
|
|
|
|
|
|
|
return t;
|
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);
|
|
|
|
this->dbIsOpen = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Database::tableExists(string tableName){
|
2018-03-01 16:19:13 +00:00
|
|
|
if (tableName.empty() || this->db == NULL || !this->dbIsOpen){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ResultTable t = executeSQL("SELECT name FROM sqlite_master WHERE type='table' AND name='"+tableName+"';");
|
|
|
|
return !t.isEmpty();
|
2018-02-02 12:27:57 +00:00
|
|
|
}
|