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 \
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 \

View File

@ -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();
}

View File

@ -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() +"');");
}
}

View File

@ -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.

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