Shift to master. Not done but database abstraction is nearly done. #3

Merged
andrewlalis merged 10 commits from development into master 2018-03-03 09:20:11 +00:00
6 changed files with 27 additions and 3 deletions
Showing only changes of commit 77dddef201 - Show all commits

View File

@ -32,6 +32,8 @@ int main(int argc, char *argv[])
recipeDB.executeSQL("SELECT * FROM recipeIngredient;").printData(); recipeDB.executeSQL("SELECT * FROM recipeIngredient;").printData();
Recipe reloadRec = recipeDB.retrieveRecipe("Example");
w.loadFromRecipe(rec); w.loadFromRecipe(rec);
return a.exec(); return a.exec();

View File

@ -13,7 +13,7 @@ ResultTable Database::executeSQL(string statement){
sqlite3_stmt* stmt; sqlite3_stmt* stmt;
this->sql = statement; this->sql = statement;
this->returnCode = sqlite3_prepare_v2(this->db, statement.c_str(), -1, &stmt, NULL); this->returnCode = sqlite3_prepare_v2(this->db, statement.c_str(), -1, &stmt, NULL);
ResultTable t; ResultTable t(statement);
if (this->returnCode != SQLITE_OK){ if (this->returnCode != SQLITE_OK){
fprintf(stderr, "Unable to successfully prepare SQL statement. Error code: %d\n\tError Message: %s\n", this->returnCode, sqlite3_errmsg(this->db)); fprintf(stderr, "Unable to successfully prepare SQL statement. Error code: %d\n\tError Message: %s\n", this->returnCode, sqlite3_errmsg(this->db));
return t; return t;

View File

@ -93,6 +93,16 @@ bool RecipeDatabase::storeImage(QImage image, int recipeId){
return FileUtils::saveImage(recipeId, image); return FileUtils::saveImage(recipeId, image);
} }
Recipe RecipeDatabase::retrieveRecipe(string name){
ResultTable t = this->selectFrom("recipe", "*", "name="+surroundString(name, "'"));
if (t.isEmpty()){
fprintf(stderr, "Error: No recipe with name %s found!\n", name.c_str());
return Recipe();
}
t.printData();
return Recipe();
}
void RecipeDatabase::ensureTablesExist(){ void RecipeDatabase::ensureTablesExist(){
//Make sure that foreign keys are enabled. //Make sure that foreign keys are enabled.
this->executeSQL("PRAGMA foreign_keys = ON;"); this->executeSQL("PRAGMA foreign_keys = ON;");

View File

@ -27,7 +27,7 @@ class RecipeDatabase : public Database
bool storeInstruction(Instruction instruction, int recipeId); bool storeInstruction(Instruction instruction, int recipeId);
bool storeImage(QImage image, int recipeId); bool storeImage(QImage image, int recipeId);
vector<Recipe> retrieveRecipe(string name); Recipe retrieveRecipe(string name);
private: private:
//Utility methods. //Utility methods.

View File

@ -4,6 +4,10 @@ ResultTable::ResultTable(){
} }
ResultTable::ResultTable(string query){
this->originalQuery = query;
}
void ResultTable::extractData(sqlite3_stmt *stmt){ void ResultTable::extractData(sqlite3_stmt *stmt){
this->values.clear(); this->values.clear();
int res = sqlite3_step(stmt); int res = sqlite3_step(stmt);
@ -30,7 +34,7 @@ void ResultTable::printData(){
printf("Result table is empty.\n"); printf("Result table is empty.\n");
return; return;
} }
printf("Printing table: %d by %d\n", this->rowCount(), this->columnCount()); printf("Printing table: [%d x %d]\t%s\n", this->rowCount(), this->columnCount(), this->originalQuery.c_str());
for (unsigned int row = 0; row < this->rowCount(); row++){ for (unsigned int row = 0; row < this->rowCount(); row++){
for (unsigned int col = 0; col < this->columnCount(); col++){ for (unsigned int col = 0; col < this->columnCount(); col++){
printf("| %s ", this->values[row][col].c_str()); printf("| %s ", this->values[row][col].c_str());
@ -56,6 +60,10 @@ int ResultTable::getReturnCode(){
return this->queryCode; return this->queryCode;
} }
string ResultTable::getOriginalQuery(){
return this->originalQuery;
}
unsigned int ResultTable::columnCount(){ unsigned int ResultTable::columnCount(){
if (this->isEmpty()){ if (this->isEmpty()){
return 0; return 0;

View File

@ -17,6 +17,8 @@ class ResultTable
public: public:
//Constructs an empty table. //Constructs an empty table.
ResultTable(); ResultTable();
//Constructs a table with the original query saved.
ResultTable(string query);
//Gets all the data from the result set and stores it internally as strings. //Gets all the data from the result set and stores it internally as strings.
void extractData(sqlite3_stmt* stmt); void extractData(sqlite3_stmt* stmt);
@ -28,11 +30,13 @@ class ResultTable
bool isEmpty(); bool isEmpty();
string valueAt(unsigned int row, unsigned int col); string valueAt(unsigned int row, unsigned int col);
int getReturnCode(); int getReturnCode();
string getOriginalQuery();
unsigned int columnCount(); unsigned int columnCount();
unsigned int rowCount(); unsigned int rowCount();
private: private:
vector<vector<string>> values; vector<vector<string>> values;
int queryCode; int queryCode;
string originalQuery;
//Utility methods. //Utility methods.
string convertToString(sqlite3_value* val); string convertToString(sqlite3_value* val);