diff --git a/main.cpp b/main.cpp index 0deb267..e8814ef 100644 --- a/main.cpp +++ b/main.cpp @@ -32,6 +32,8 @@ int main(int argc, char *argv[]) recipeDB.executeSQL("SELECT * FROM recipeIngredient;").printData(); + Recipe reloadRec = recipeDB.retrieveRecipe("Example"); + w.loadFromRecipe(rec); return a.exec(); diff --git a/model/database/database.cpp b/model/database/database.cpp index 0e2c0ed..6fb4394 100644 --- a/model/database/database.cpp +++ b/model/database/database.cpp @@ -13,7 +13,7 @@ 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; + ResultTable t(statement); 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)); return t; diff --git a/model/database/recipedatabase.cpp b/model/database/recipedatabase.cpp index 5edddbb..99bb92c 100644 --- a/model/database/recipedatabase.cpp +++ b/model/database/recipedatabase.cpp @@ -93,6 +93,16 @@ bool RecipeDatabase::storeImage(QImage image, int recipeId){ 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(){ //Make sure that foreign keys are enabled. this->executeSQL("PRAGMA foreign_keys = ON;"); diff --git a/model/database/recipedatabase.h b/model/database/recipedatabase.h index 1514cea..4c4d420 100644 --- a/model/database/recipedatabase.h +++ b/model/database/recipedatabase.h @@ -27,7 +27,7 @@ class RecipeDatabase : public Database bool storeInstruction(Instruction instruction, int recipeId); bool storeImage(QImage image, int recipeId); - vector retrieveRecipe(string name); + Recipe retrieveRecipe(string name); private: //Utility methods. diff --git a/model/database/resulttable.cpp b/model/database/resulttable.cpp index bef5028..9811d64 100644 --- a/model/database/resulttable.cpp +++ b/model/database/resulttable.cpp @@ -4,6 +4,10 @@ ResultTable::ResultTable(){ } +ResultTable::ResultTable(string query){ + this->originalQuery = query; +} + void ResultTable::extractData(sqlite3_stmt *stmt){ this->values.clear(); int res = sqlite3_step(stmt); @@ -30,7 +34,7 @@ void ResultTable::printData(){ printf("Result table is empty.\n"); 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 col = 0; col < this->columnCount(); col++){ printf("| %s ", this->values[row][col].c_str()); @@ -56,6 +60,10 @@ int ResultTable::getReturnCode(){ return this->queryCode; } +string ResultTable::getOriginalQuery(){ + return this->originalQuery; +} + unsigned int ResultTable::columnCount(){ if (this->isEmpty()){ return 0; diff --git a/model/database/resulttable.h b/model/database/resulttable.h index dd92ead..a138ea3 100644 --- a/model/database/resulttable.h +++ b/model/database/resulttable.h @@ -17,6 +17,8 @@ class ResultTable public: //Constructs an empty table. 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. void extractData(sqlite3_stmt* stmt); @@ -28,11 +30,13 @@ class ResultTable bool isEmpty(); string valueAt(unsigned int row, unsigned int col); int getReturnCode(); + string getOriginalQuery(); unsigned int columnCount(); unsigned int rowCount(); private: vector> values; int queryCode; + string originalQuery; //Utility methods. string convertToString(sqlite3_value* val);