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();
|
|
|
|
if (tableExists("ingredients")){
|
|
|
|
printf("Ingredients table already exists.\n");
|
|
|
|
} else {
|
|
|
|
printf("Couldn't find the ingredients table.\n");
|
|
|
|
}
|
|
|
|
}
|
2018-02-02 12:27:57 +00:00
|
|
|
|
2018-02-12 13:24:11 +00:00
|
|
|
Database::~Database(){
|
|
|
|
closeConnection();
|
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){
|
|
|
|
if (this->dbIsOpen){
|
|
|
|
this->sql = "SELECT name FROM sqlite_master WHERE type='table' AND name='"+tableName+"';";
|
|
|
|
const char* str = this->sql.c_str();
|
|
|
|
this->returnCode = sqlite3_exec(this->db, str, NULL, 0, &this->errorMsg);
|
|
|
|
if (this->returnCode == SQLITE_ERROR){
|
|
|
|
fprintf(stderr, "Unable to select name from master table list: %s\n", this->errorMsg);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-02 12:27:57 +00:00
|
|
|
}
|
2018-02-12 13:24:11 +00:00
|
|
|
return false;
|
2018-02-02 12:27:57 +00:00
|
|
|
}
|