2018-03-01 16:19:13 +00:00
|
|
|
#include "recipedatabase.h"
|
|
|
|
|
|
|
|
RecipeDatabase::RecipeDatabase(string filename) : Database(filename){
|
|
|
|
this->ensureTablesExist();
|
|
|
|
}
|
|
|
|
|
2018-03-01 16:28:18 +00:00
|
|
|
void RecipeDatabase::storeRecipe(Recipe recipe){
|
|
|
|
///TODO: Implement this in a smart way using transaction.
|
2018-03-02 10:30:16 +00:00
|
|
|
ResultTable t = this->executeSQL("SELECT * FROM recipe WHERE name='"+recipe.getName()+"';");
|
|
|
|
if (!t.isEmpty()){
|
|
|
|
fprintf(stderr, "Error storing recipe: Recipe with name %s already exists.\n", recipe.getName().c_str());
|
|
|
|
} else {
|
|
|
|
bool success = this->insertInto("recipe",
|
|
|
|
vector<string>({
|
|
|
|
"name",
|
|
|
|
"createdDate",
|
|
|
|
"cookTime",
|
|
|
|
"prepTime",
|
|
|
|
"servingCount"
|
|
|
|
}),
|
|
|
|
vector<string>({
|
|
|
|
recipe.getName(),
|
|
|
|
recipe.getCreatedDate().toString().toStdString(),
|
|
|
|
recipe.getCookTime().toString().toStdString(),
|
|
|
|
recipe.getPrepTime().toString().toStdString(),
|
|
|
|
std::to_string(recipe.getServings())
|
|
|
|
}));
|
|
|
|
if (success){
|
|
|
|
//If successful, proceed to insert instructions, image, and ingredients.
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecipeDatabase::storeRecipeIngredient(RecipeIngredient ri){
|
2018-03-02 08:32:40 +00:00
|
|
|
|
2018-03-01 16:28:18 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 10:30:16 +00:00
|
|
|
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->insertInto("ingredient", vector<string>({"foodGroup", "name"}), vector<string>({ingredient.getFoodGroup(), ingredient.getName()}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 16:19:13 +00:00
|
|
|
void RecipeDatabase::ensureTablesExist(){
|
|
|
|
//Make sure that foreign keys are enabled.
|
|
|
|
this->executeSQL("PRAGMA foreign_keys = ON;");
|
|
|
|
//Ingredients table.
|
|
|
|
this->executeSQL("CREATE TABLE IF NOT EXISTS ingredient("
|
2018-03-02 08:32:40 +00:00
|
|
|
"ingredientId INTEGER PRIMARY KEY,"
|
2018-03-01 16:19:13 +00:00
|
|
|
"foodGroup varchar,"
|
2018-03-02 08:32:40 +00:00
|
|
|
"name varchar);");
|
2018-03-02 10:30:16 +00:00
|
|
|
//Recipe table. Each recipe can have at most one instruction, and one image.
|
2018-03-01 16:19:13 +00:00
|
|
|
this->executeSQL("CREATE TABLE IF NOT EXISTS recipe("
|
2018-03-02 08:32:40 +00:00
|
|
|
"recipeId INTEGER PRIMARY KEY,"
|
2018-03-02 10:30:16 +00:00
|
|
|
"createdDate date,"
|
2018-03-01 16:19:13 +00:00
|
|
|
"name varchar,"
|
|
|
|
"cookTime time,"
|
|
|
|
"prepTime time,"
|
2018-03-02 10:30:16 +00:00
|
|
|
"servingCount real);");
|
2018-03-01 16:19:13 +00:00
|
|
|
//Recipe tags table.
|
|
|
|
this->executeSQL("CREATE TABLE IF NOT EXISTS recipeTag("
|
2018-03-02 08:32:40 +00:00
|
|
|
"recipeId INTEGER PRIMARY KEY,"
|
2018-03-01 16:19:13 +00:00
|
|
|
"tagName varchar,"
|
|
|
|
"FOREIGN KEY (recipeId) REFERENCES recipe(recipeId));");
|
|
|
|
//RecipeIngredient table.
|
|
|
|
this->executeSQL("CREATE TABLE IF NOT EXISTS recipeIngredient("
|
|
|
|
"ingredientId int,"
|
2018-03-02 08:32:40 +00:00
|
|
|
"recipeId INTEGER PRIMARY KEY,"
|
2018-03-01 16:19:13 +00:00
|
|
|
"quantity real,"
|
|
|
|
"unitName varchar,"
|
|
|
|
"comment varchar,"
|
|
|
|
"FOREIGN KEY (ingredientId) REFERENCES ingredient(ingredientId),"
|
|
|
|
"FOREIGN KEY (recipeId) REFERENCES recipe(recipeId));");
|
2018-03-02 08:32:40 +00:00
|
|
|
}
|