From e714a2fc6f5416981decb4600a8d47eaf45514b7 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Fri, 2 Mar 2018 09:32:40 +0100 Subject: [PATCH] Added fileutils class, added ability to store ingredients in database. --- RecipeDB.pro | 6 +++-- main.cpp | 13 +++++---- model/database/recipedatabase.cpp | 45 +++++++++++++++++++------------ model/database/recipedatabase.h | 5 ++++ utils/fileutils.cpp | 5 ++++ utils/fileutils.h | 11 ++++++++ 6 files changed, 61 insertions(+), 24 deletions(-) create mode 100644 utils/fileutils.cpp create mode 100644 utils/fileutils.h diff --git a/RecipeDB.pro b/RecipeDB.pro index eb02709..2c957a5 100644 --- a/RecipeDB.pro +++ b/RecipeDB.pro @@ -24,7 +24,8 @@ SOURCES += model/recipe/instruction.cpp \ model/recipe/tags/recipetag.cpp \ SQLite/sqlite3.c \ model/database/resulttable.cpp \ - model/database/recipedatabase.cpp + model/database/recipedatabase.cpp \ + utils/fileutils.cpp HEADERS += model/recipe/instruction.h \ model/recipe/recipe.h \ @@ -38,7 +39,8 @@ HEADERS += model/recipe/instruction.h \ SQLite/sqlite3.h \ SQLite/sqlite3ext.h \ model/database/resulttable.h \ - model/database/recipedatabase.h + model/database/recipedatabase.h \ + utils/fileutils.h LIBS += -ldl \ diff --git a/main.cpp b/main.cpp index 5e98fcf..085d24a 100644 --- a/main.cpp +++ b/main.cpp @@ -11,13 +11,16 @@ int main(int argc, char *argv[]) w.show(); //TESTING CODE - Database db("test.db"); - printf("Table exists: %d\n", db.tableExists("ingredients")); - db.executeSQL("SELECT * FROM ingredients;").printData(); - db.executeSQL("PRAGMA table_info('ingredients');").printData(); - db.executeSQL("SELECT name FROM ingredients WHERE foodGroup == 'fruit';").printData(); +// Database db("test.db"); +// printf("Table exists: %d\n", db.tableExists("ingredients")); +// db.executeSQL("SELECT * FROM ingredients;").printData(); +// db.executeSQL("PRAGMA table_info('ingredients');").printData(); +// db.executeSQL("SELECT name FROM ingredients WHERE foodGroup == 'fruit';").printData(); RecipeDatabase recipeDB("recipes"); + recipeDB.storeIngredient(Ingredient("Apple", "Fruit")); + recipeDB.storeIngredient(Ingredient("Corn", "Vegetable")); + recipeDB.executeSQL("SELECT ingredientId FROM ingredient;").printData(); return a.exec(); } diff --git a/model/database/recipedatabase.cpp b/model/database/recipedatabase.cpp index f034f4c..ed312e8 100644 --- a/model/database/recipedatabase.cpp +++ b/model/database/recipedatabase.cpp @@ -6,6 +6,7 @@ RecipeDatabase::RecipeDatabase(string filename) : Database(filename){ void RecipeDatabase::storeRecipe(Recipe recipe){ ///TODO: Implement this in a smart way using transaction. + } void RecipeDatabase::ensureTablesExist(){ @@ -13,52 +14,62 @@ void RecipeDatabase::ensureTablesExist(){ this->executeSQL("PRAGMA foreign_keys = ON;"); //Ingredients table. this->executeSQL("CREATE TABLE IF NOT EXISTS ingredient(" - "ingredientId int," + "ingredientId INTEGER PRIMARY KEY," "foodGroup varchar," - "name varchar," - "PRIMARY KEY (ingredientId));"); + "name varchar);"); //Images table. this->executeSQL("CREATE TABLE IF NOT EXISTS image(" - "imageNr int," - "contentURL varchar," - "PRIMARY KEY (imageNr));"); + "imageNr INTEGER PRIMARY KEY," + "contentURL varchar);"); //Instructions table. this->executeSQL("CREATE TABLE IF NOT EXISTS instruction(" - "instructionNr int," - "contentURL varchar," - "PRIMARY KEY (instructionNr));"); + "instructionNr INTEGER PRIMARY KEY," + "contentURL varchar);"); //Recipe table. this->executeSQL("CREATE TABLE IF NOT EXISTS recipe(" - "recipeId int," + "recipeId INTEGER PRIMARY KEY," "date date," "name varchar," "imageNr int," "cookTime time," "prepTime time," "servingCount real," - "PRIMARY KEY (recipeId)," "FOREIGN KEY (imageNr) REFERENCES image(imageNr));"); //Recipe tags table. this->executeSQL("CREATE TABLE IF NOT EXISTS recipeTag(" - "recipeId int," + "recipeId INTEGER PRIMARY KEY," "tagName varchar," - "PRIMARY KEY (recipeId)," "FOREIGN KEY (recipeId) REFERENCES recipe(recipeId));"); //RecipeIngredient table. this->executeSQL("CREATE TABLE IF NOT EXISTS recipeIngredient(" "ingredientId int," - "recipeId int," + "recipeId INTEGER PRIMARY KEY," "quantity real," "unitName varchar," "comment varchar," - "PRIMARY KEY (recipeId)," "FOREIGN KEY (ingredientId) REFERENCES ingredient(ingredientId)," "FOREIGN KEY (recipeId) REFERENCES recipe(recipeId));"); //Recipe Instruction mapping table. this->executeSQL("CREATE TABLE IF NOT EXISTS recipeInstruction(" "instructionNr int," - "recipeId int," - "PRIMARY KEY (recipeId)," + "recipeId INTEGER PRIMARY KEY," "FOREIGN KEY (instructionNr) REFERENCES instruction(instructionNr)," "FOREIGN KEY (recipeId) REFERENCES recipe(recipeId));"); } + +void RecipeDatabase::storeInstruction(Instruction instruction){ + +} + +void RecipeDatabase::storeImage(QImage image){ + +} + +void RecipeDatabase::storeIngredient(Ingredient ingredient){ + ResultTable t = this->executeSQL("SELECT * FROM ingredient WHERE name='"+ingredient.getName()+"';"); + if (!t.isEmpty()){ + fprintf(stderr, "Error during storeIngredient: ingredient with name %s already exists.\n", ingredient.getName().c_str()); + } else { + this->executeSQL("INSERT INTO ingredient (foodGroup, name) VALUES ('"+ ingredient.getFoodGroup() +"', '"+ ingredient.getName() +"');"); + } +} diff --git a/model/database/recipedatabase.h b/model/database/recipedatabase.h index 2771017..a0d311a 100644 --- a/model/database/recipedatabase.h +++ b/model/database/recipedatabase.h @@ -19,6 +19,11 @@ class RecipeDatabase : public Database //Stores a full recipe in the database. void storeRecipe(Recipe recipe); + + //SQL Helper methods. + void storeInstruction(Instruction instruction); + void storeImage(QImage image); + void storeIngredient(Ingredient ingredient); private: //Utility methods. diff --git a/utils/fileutils.cpp b/utils/fileutils.cpp new file mode 100644 index 0000000..0fa0670 --- /dev/null +++ b/utils/fileutils.cpp @@ -0,0 +1,5 @@ +#include "fileutils.h" + +fileUtils::fileUtils(){ + +} diff --git a/utils/fileutils.h b/utils/fileutils.h new file mode 100644 index 0000000..76bdfc2 --- /dev/null +++ b/utils/fileutils.h @@ -0,0 +1,11 @@ +#ifndef FILEUTILS_H +#define FILEUTILS_H + + +class fileUtils +{ + public: + fileUtils(); +}; + +#endif // FILEUTILS_H \ No newline at end of file