Added fileutils class, added ability to store ingredients in database.

This commit is contained in:
Andrew Lalis 2018-03-02 09:32:40 +01:00
parent b893372b01
commit e714a2fc6f
6 changed files with 61 additions and 24 deletions

View File

@ -24,7 +24,8 @@ SOURCES += model/recipe/instruction.cpp \
model/recipe/tags/recipetag.cpp \ model/recipe/tags/recipetag.cpp \
SQLite/sqlite3.c \ SQLite/sqlite3.c \
model/database/resulttable.cpp \ model/database/resulttable.cpp \
model/database/recipedatabase.cpp model/database/recipedatabase.cpp \
utils/fileutils.cpp
HEADERS += model/recipe/instruction.h \ HEADERS += model/recipe/instruction.h \
model/recipe/recipe.h \ model/recipe/recipe.h \
@ -38,7 +39,8 @@ HEADERS += model/recipe/instruction.h \
SQLite/sqlite3.h \ SQLite/sqlite3.h \
SQLite/sqlite3ext.h \ SQLite/sqlite3ext.h \
model/database/resulttable.h \ model/database/resulttable.h \
model/database/recipedatabase.h model/database/recipedatabase.h \
utils/fileutils.h
LIBS += -ldl \ LIBS += -ldl \

View File

@ -11,13 +11,16 @@ int main(int argc, char *argv[])
w.show(); w.show();
//TESTING CODE //TESTING CODE
Database db("test.db"); // Database db("test.db");
printf("Table exists: %d\n", db.tableExists("ingredients")); // printf("Table exists: %d\n", db.tableExists("ingredients"));
db.executeSQL("SELECT * FROM ingredients;").printData(); // db.executeSQL("SELECT * FROM ingredients;").printData();
db.executeSQL("PRAGMA table_info('ingredients');").printData(); // db.executeSQL("PRAGMA table_info('ingredients');").printData();
db.executeSQL("SELECT name FROM ingredients WHERE foodGroup == 'fruit';").printData(); // db.executeSQL("SELECT name FROM ingredients WHERE foodGroup == 'fruit';").printData();
RecipeDatabase recipeDB("recipes"); RecipeDatabase recipeDB("recipes");
recipeDB.storeIngredient(Ingredient("Apple", "Fruit"));
recipeDB.storeIngredient(Ingredient("Corn", "Vegetable"));
recipeDB.executeSQL("SELECT ingredientId FROM ingredient;").printData();
return a.exec(); return a.exec();
} }

View File

@ -6,6 +6,7 @@ RecipeDatabase::RecipeDatabase(string filename) : Database(filename){
void RecipeDatabase::storeRecipe(Recipe recipe){ void RecipeDatabase::storeRecipe(Recipe recipe){
///TODO: Implement this in a smart way using transaction. ///TODO: Implement this in a smart way using transaction.
} }
void RecipeDatabase::ensureTablesExist(){ void RecipeDatabase::ensureTablesExist(){
@ -13,52 +14,62 @@ void RecipeDatabase::ensureTablesExist(){
this->executeSQL("PRAGMA foreign_keys = ON;"); this->executeSQL("PRAGMA foreign_keys = ON;");
//Ingredients table. //Ingredients table.
this->executeSQL("CREATE TABLE IF NOT EXISTS ingredient(" this->executeSQL("CREATE TABLE IF NOT EXISTS ingredient("
"ingredientId int," "ingredientId INTEGER PRIMARY KEY,"
"foodGroup varchar," "foodGroup varchar,"
"name varchar," "name varchar);");
"PRIMARY KEY (ingredientId));");
//Images table. //Images table.
this->executeSQL("CREATE TABLE IF NOT EXISTS image(" this->executeSQL("CREATE TABLE IF NOT EXISTS image("
"imageNr int," "imageNr INTEGER PRIMARY KEY,"
"contentURL varchar," "contentURL varchar);");
"PRIMARY KEY (imageNr));");
//Instructions table. //Instructions table.
this->executeSQL("CREATE TABLE IF NOT EXISTS instruction(" this->executeSQL("CREATE TABLE IF NOT EXISTS instruction("
"instructionNr int," "instructionNr INTEGER PRIMARY KEY,"
"contentURL varchar," "contentURL varchar);");
"PRIMARY KEY (instructionNr));");
//Recipe table. //Recipe table.
this->executeSQL("CREATE TABLE IF NOT EXISTS recipe(" this->executeSQL("CREATE TABLE IF NOT EXISTS recipe("
"recipeId int," "recipeId INTEGER PRIMARY KEY,"
"date date," "date date,"
"name varchar," "name varchar,"
"imageNr int," "imageNr int,"
"cookTime time," "cookTime time,"
"prepTime time," "prepTime time,"
"servingCount real," "servingCount real,"
"PRIMARY KEY (recipeId),"
"FOREIGN KEY (imageNr) REFERENCES image(imageNr));"); "FOREIGN KEY (imageNr) REFERENCES image(imageNr));");
//Recipe tags table. //Recipe tags table.
this->executeSQL("CREATE TABLE IF NOT EXISTS recipeTag(" this->executeSQL("CREATE TABLE IF NOT EXISTS recipeTag("
"recipeId int," "recipeId INTEGER PRIMARY KEY,"
"tagName varchar," "tagName varchar,"
"PRIMARY KEY (recipeId),"
"FOREIGN KEY (recipeId) REFERENCES recipe(recipeId));"); "FOREIGN KEY (recipeId) REFERENCES recipe(recipeId));");
//RecipeIngredient table. //RecipeIngredient table.
this->executeSQL("CREATE TABLE IF NOT EXISTS recipeIngredient(" this->executeSQL("CREATE TABLE IF NOT EXISTS recipeIngredient("
"ingredientId int," "ingredientId int,"
"recipeId int," "recipeId INTEGER PRIMARY KEY,"
"quantity real," "quantity real,"
"unitName varchar," "unitName varchar,"
"comment varchar," "comment varchar,"
"PRIMARY KEY (recipeId),"
"FOREIGN KEY (ingredientId) REFERENCES ingredient(ingredientId)," "FOREIGN KEY (ingredientId) REFERENCES ingredient(ingredientId),"
"FOREIGN KEY (recipeId) REFERENCES recipe(recipeId));"); "FOREIGN KEY (recipeId) REFERENCES recipe(recipeId));");
//Recipe Instruction mapping table. //Recipe Instruction mapping table.
this->executeSQL("CREATE TABLE IF NOT EXISTS recipeInstruction(" this->executeSQL("CREATE TABLE IF NOT EXISTS recipeInstruction("
"instructionNr int," "instructionNr int,"
"recipeId int," "recipeId INTEGER PRIMARY KEY,"
"PRIMARY KEY (recipeId),"
"FOREIGN KEY (instructionNr) REFERENCES instruction(instructionNr)," "FOREIGN KEY (instructionNr) REFERENCES instruction(instructionNr),"
"FOREIGN KEY (recipeId) REFERENCES recipe(recipeId));"); "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() +"');");
}
}

View File

@ -19,6 +19,11 @@ class RecipeDatabase : public Database
//Stores a full recipe in the database. //Stores a full recipe in the database.
void storeRecipe(Recipe recipe); void storeRecipe(Recipe recipe);
//SQL Helper methods.
void storeInstruction(Instruction instruction);
void storeImage(QImage image);
void storeIngredient(Ingredient ingredient);
private: private:
//Utility methods. //Utility methods.

5
utils/fileutils.cpp Normal file
View File

@ -0,0 +1,5 @@
#include "fileutils.h"
fileUtils::fileUtils(){
}

11
utils/fileutils.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef FILEUTILS_H
#define FILEUTILS_H
class fileUtils
{
public:
fileUtils();
};
#endif // FILEUTILS_H