Compare commits
No commits in common. "d30f8df5c1b82c4bd5649dabe6fd1e071dd37355" and "1a590b477ef6b2cba8ac6e12313eafe571e01860" have entirely different histories.
d30f8df5c1
...
1a590b477e
@ -36,7 +36,7 @@ static const std::map<enum cmd_id, std::vector<std::string>> commands = {
|
|||||||
{ CMD_ADD, {"add", "new"} },
|
{ CMD_ADD, {"add", "new"} },
|
||||||
{ CMD_LIST, {"list", "ls"} },
|
{ CMD_LIST, {"list", "ls"} },
|
||||||
{ CMD_DEL, {"del", "rm"} },
|
{ CMD_DEL, {"del", "rm"} },
|
||||||
{ CMD_INFO, {"info", "i"} },
|
{ CMD_INFO, { "info" } },
|
||||||
{ CMD_HELP, {"help", "-h", "--help"} },
|
{ CMD_HELP, {"help", "-h", "--help"} },
|
||||||
{ CMD_VERSION, {"version", "-v", "--version"} },
|
{ CMD_VERSION, {"version", "-v", "--version"} },
|
||||||
};
|
};
|
||||||
|
78
src/cmd.cpp
78
src/cmd.cpp
@ -19,14 +19,13 @@
|
|||||||
#include "db.hpp"
|
#include "db.hpp"
|
||||||
#include "util.hpp"
|
#include "util.hpp"
|
||||||
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <cstdlib>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unistd.h>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
int cmd_add(void) {
|
int command_add(void) {
|
||||||
db db;
|
|
||||||
std::string name, description, ingredients, tags;
|
std::string name, description, ingredients, tags;
|
||||||
int recipe_id, ingredient_id, tag_id;
|
int recipe_id, ingredient_id, tag_id;
|
||||||
|
|
||||||
@ -42,34 +41,36 @@ int cmd_add(void) {
|
|||||||
std::cout << "Tags (comma separated): ";
|
std::cout << "Tags (comma separated): ";
|
||||||
getline(std::cin, tags);
|
getline(std::cin, tags);
|
||||||
|
|
||||||
db.open();
|
if(not db_open()) {
|
||||||
|
std::cerr << "Failed to open database. Cannot add new entry." << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
if((recipe_id = db.get_recipe_id(name)) <= 0)
|
if((recipe_id = db_get_recipe_id(name)) <= 0)
|
||||||
recipe_id = db.add_recipe(name, description);
|
recipe_id = db_add_recipe(name, description);
|
||||||
|
|
||||||
for(auto &ingredient : split(ingredients, ",")) {
|
for(auto &ingredient : split(ingredients, ",")) {
|
||||||
trim(ingredient);
|
trim(ingredient);
|
||||||
|
|
||||||
if((ingredient_id = db.get_ingredient_id(ingredient)) <= 0)
|
if((ingredient_id = db_get_ingredient_id(ingredient)) <= 0)
|
||||||
ingredient_id = db.add_ingredient(ingredient);
|
ingredient_id = db_add_ingredient(ingredient);
|
||||||
db.conn_recipe_ingredient(recipe_id, ingredient_id);
|
db_conn_recipe_ingredient(recipe_id, ingredient_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(auto &tag : split(tags, ",")) {
|
for(auto &tag : split(tags, ",")) {
|
||||||
trim(tag);
|
trim(tag);
|
||||||
|
|
||||||
if((tag_id = db.get_tag_id(tag)) <= 0)
|
if((tag_id = db_get_tag_id(tag)) <= 0)
|
||||||
tag_id = db.add_tag(tag);
|
tag_id = db_add_tag(tag);
|
||||||
db.conn_recipe_tag(recipe_id, tag_id);
|
db_conn_recipe_tag(recipe_id, tag_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
db.close();
|
db_close();
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cmd_list(int argc, char *argv[]) {
|
int command_list(int argc, char *argv[]) {
|
||||||
db db;
|
|
||||||
std::vector<std::string> ingredients, tags;
|
std::vector<std::string> ingredients, tags;
|
||||||
int opt;
|
int opt;
|
||||||
|
|
||||||
@ -92,18 +93,21 @@ int cmd_list(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
db.open();
|
if(not db_open()) {
|
||||||
|
std::cerr << "Failed to open database. Cannot add new entry." << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
for(const auto &recipe : db.get_recipes(ingredients, tags))
|
for(const auto &recipe : db_get_recipes(ingredients, tags))
|
||||||
std::cout << recipe.id << " | " << recipe.name << " | " << recipe.description << std::endl;
|
std::cout << recipe.id << " | " << recipe.name << " | " << recipe.description << std::endl;
|
||||||
|
|
||||||
db.close();
|
db_close();
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cmd_delete(int argc, char *argv[]) {
|
int command_delete(int argc, char *argv[]) {
|
||||||
db db;
|
int ret = EXIT_SUCCESS;
|
||||||
std::vector<int> recipe_ids;
|
std::vector<int> recipe_ids;
|
||||||
|
|
||||||
if(argc < 1) {
|
if(argc < 1) {
|
||||||
@ -111,12 +115,15 @@ int cmd_delete(int argc, char *argv[]) {
|
|||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
db.open();
|
if(not db_open()) {
|
||||||
|
std::cerr << "Failed to open database. Cannot add new entry." << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
for(int i = 0; i < argc; ++i) {
|
for(int i = 0; i < argc; ++i) {
|
||||||
const int id = std::stoi(argv[i]);
|
const int id = std::stoi(argv[i]);
|
||||||
|
|
||||||
if(not db.recipe_exists(id)) {
|
if(not db_recipe_exists(id)) {
|
||||||
std::cerr << "No recipe exists with ID " << id << "." << std::endl;
|
std::cerr << "No recipe exists with ID " << id << "." << std::endl;
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
} else {
|
} else {
|
||||||
@ -124,30 +131,33 @@ int cmd_delete(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
db.del_recipes(recipe_ids);
|
ret = (db_del_recipes(recipe_ids)) ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||||
db.close();
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
db_close();
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cmd_info(const int id) {
|
int command_info(const int id) {
|
||||||
db db;
|
|
||||||
struct recipe recipe;
|
struct recipe recipe;
|
||||||
std::vector<std::string> ingredients, tags;
|
std::vector<std::string> ingredients, tags;
|
||||||
int ret = EXIT_SUCCESS;
|
int ret = EXIT_SUCCESS;
|
||||||
|
|
||||||
db.open();
|
if(not db_open()) {
|
||||||
|
std::cerr << "Failed to open database. Cannot add new entry." << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
if(not db.recipe_exists(id)) {
|
if(not db_recipe_exists(id)) {
|
||||||
std::cerr << "No recipe with ID '" << id << "'";
|
std::cerr << "No recipe with ID '" << id << "'";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
recipe = db.get_recipe(id);
|
recipe = db_get_recipe(id);
|
||||||
ingredients = db.get_recipe_ingredients(id);
|
ingredients = db_get_recipe_ingredients(id);
|
||||||
tags = db.get_recipe_tags(id);
|
tags = db_get_recipe_tags(id);
|
||||||
|
|
||||||
db.close();
|
db_close();
|
||||||
|
|
||||||
std::cout << "Name: " << recipe.name << "\n"
|
std::cout << "Name: " << recipe.name << "\n"
|
||||||
<< "Description: " << recipe.description << "\n"
|
<< "Description: " << recipe.description << "\n"
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
int cmd_add(void);
|
int command_add(void);
|
||||||
int cmd_list(int argc, char *argv[]);
|
int command_list(int argc, char *argv[]);
|
||||||
int cmd_delete(int argc, char *argv[]);
|
int command_delete(int argc, char *argv[]);
|
||||||
int cmd_info(const int id);
|
int command_info(const int id);
|
||||||
|
298
src/db.cpp
298
src/db.cpp
@ -17,22 +17,28 @@
|
|||||||
*/
|
*/
|
||||||
#include "db.hpp"
|
#include "db.hpp"
|
||||||
|
|
||||||
|
#include <sqlite3.h>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <iostream>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <iostream>
|
#include <string>
|
||||||
#include <sqlite3.h>
|
#include <vector>
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
#define DB_VERSION 1
|
#define DB_VERSION 1
|
||||||
|
static sqlite3 *db = nullptr;
|
||||||
|
|
||||||
void db::open(void) {
|
int db_open(void) {
|
||||||
std::string xdg_data_home;
|
std::string xdg_data_home;
|
||||||
std::string db_path;
|
std::string db_path;
|
||||||
bool new_db = false;
|
bool new_db = false;
|
||||||
|
int rc;
|
||||||
|
|
||||||
if((xdg_data_home = std::getenv("XDG_DATA_HOME")).empty())
|
if((xdg_data_home = std::getenv("XDG_DATA_HOME")).empty()) {
|
||||||
throw std::runtime_error("Cannot find environment variable XDG_DATA_HOME. Please define it before continuing.");
|
std::cerr << "Cannot find environment variable XDG_DATA_HOME. Please define it before continuing. E.g.:\n"
|
||||||
|
"export XDG_DATA_HOME=\"$HOME/.local/share\"" << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
db_path = xdg_data_home + "/menu-helper";
|
db_path = xdg_data_home + "/menu-helper";
|
||||||
|
|
||||||
@ -46,26 +52,26 @@ void db::open(void) {
|
|||||||
new_db = true;
|
new_db = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(sqlite3_open(db_path.c_str(), &sqlite_db) not_eq SQLITE_OK)
|
rc = sqlite3_open(db_path.c_str(), &db);
|
||||||
throw std::runtime_error("Failed to open database file " + db_path);
|
|
||||||
|
|
||||||
if(new_db) {
|
if(rc == SQLITE_OK and new_db) {
|
||||||
sqlite3_exec(sqlite_db, "CREATE TABLE db_version(version INTEGER UNIQUE NOT nullptr);", nullptr, nullptr, nullptr);
|
sqlite3_exec(db, "CREATE TABLE db_version(version INTEGER UNIQUE NOT nullptr);", nullptr, nullptr, nullptr);
|
||||||
sqlite3_exec(sqlite_db, std::format("INSERT INTO db_version VALUES({});", DB_VERSION).c_str(), nullptr, nullptr, nullptr);
|
sqlite3_exec(db, std::format("INSERT INTO db_version VALUES({});", DB_VERSION).c_str(), nullptr, nullptr, nullptr);
|
||||||
sqlite3_exec(sqlite_db, "CREATE TABLE tags(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE);", nullptr, nullptr, nullptr);
|
sqlite3_exec(db, "CREATE TABLE tags(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE);", nullptr, nullptr, nullptr);
|
||||||
sqlite3_exec(sqlite_db, "CREATE TABLE ingredients(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE);", nullptr, nullptr, nullptr);
|
sqlite3_exec(db, "CREATE TABLE ingredients(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE);", nullptr, nullptr, nullptr);
|
||||||
sqlite3_exec(sqlite_db, "CREATE TABLE recipes(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE, description STRING);", nullptr, nullptr, nullptr);
|
sqlite3_exec(db, "CREATE TABLE recipes(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE, description STRING);", nullptr, nullptr, nullptr);
|
||||||
sqlite3_exec(sqlite_db, "CREATE TABLE recipe_tag(recipe_id INTEGER REFERENCES recipes(id) ON DELETE CASCADE, tag_id INTEGER REFERENCES tags(id) ON DELETE CASCADE);", nullptr, nullptr, nullptr);
|
sqlite3_exec(db, "CREATE TABLE recipe_tag(recipe_id INTEGER REFERENCES recipes(id) ON DELETE CASCADE, tag_id INTEGER REFERENCES tags(id) ON DELETE CASCADE);", nullptr, nullptr, nullptr);
|
||||||
sqlite3_exec(sqlite_db, "CREATE TABLE recipe_ingredient(recipe_id INTEGER REFERENCES recipes(id) ON DELETE CASCADE, ingredient_id INTEGER REFERENCES ingredients(id) ON DELETE CASCADE);", nullptr, nullptr, nullptr);
|
sqlite3_exec(db, "CREATE TABLE recipe_ingredient(recipe_id INTEGER REFERENCES recipes(id) ON DELETE CASCADE, ingredient_id INTEGER REFERENCES ingredients(id) ON DELETE CASCADE);", nullptr, nullptr, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return rc == SQLITE_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void db::close(void) {
|
void db_close(void) {
|
||||||
if(not sqlite_db)
|
if(not db)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
sqlite3_close(sqlite_db);
|
sqlite3_close(db);
|
||||||
sqlite_db = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int query_id_cb(void *recipe_id_var, int col_num, char **col_data, char **col_name) {
|
int query_id_cb(void *recipe_id_var, int col_num, char **col_data, char **col_name) {
|
||||||
@ -83,47 +89,43 @@ int query_id_cb(void *recipe_id_var, int col_num, char **col_data, char **col_na
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int db::table_get_id_by_name(const std::string &table, const std::string &name) {
|
int table_get_id_by_name(const std::string &table, const std::string &name) {
|
||||||
int id = 0;
|
int id = 0;
|
||||||
|
|
||||||
if(not sqlite_db)
|
if(sqlite3_exec(db, std::format("SELECT id FROM {} WHERE lower(name)=lower('{}');", table, name).c_str(),
|
||||||
throw std::runtime_error(std::format("{}: Database not open! Please contact a developer.", __PRETTY_FUNCTION__));
|
query_id_cb, &id, nullptr) not_eq SQLITE_OK)
|
||||||
|
return -2;
|
||||||
if(sqlite3_exec(sqlite_db, std::format("SELECT id FROM {} WHERE lower(name)=lower('{}');", table, name).c_str(),
|
|
||||||
query_id_cb, &id, nullptr) not_eq SQLITE_OK) {
|
|
||||||
throw std::runtime_error(std::format("Failed to get ID of '{}' from table '{}'.", name, table));
|
|
||||||
}
|
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
int db::add_recipe(const std::string &name, const std::string &description) {
|
int db_add_recipe(const std::string &name, const std::string &description) {
|
||||||
if(not sqlite_db)
|
if(not db)
|
||||||
throw std::runtime_error(std::format("{}: Database not open! Please contact a developer.", __PRETTY_FUNCTION__));
|
return -1;
|
||||||
|
|
||||||
if(sqlite3_exec(sqlite_db, std::format("INSERT INTO recipes(name,description) VALUES('{}','{}');", name, description).c_str(),
|
if(sqlite3_exec(db, std::format("INSERT INTO recipes(name,description) VALUES('{}','{}');", name, description).c_str(),
|
||||||
nullptr, nullptr, nullptr) not_eq SQLITE_OK) {
|
nullptr, nullptr, nullptr) not_eq SQLITE_OK)
|
||||||
throw std::runtime_error("Failed to insert new recipe into database.");
|
return -2;
|
||||||
}
|
|
||||||
|
|
||||||
return get_recipe_id(name);
|
return db_get_recipe_id(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void db::del_recipe(const int id) {
|
bool db_del_recipe(const int id) {
|
||||||
if(not sqlite_db)
|
if(not db)
|
||||||
throw std::runtime_error(std::format("{}: Database not open! Please contact a developer.", __PRETTY_FUNCTION__));
|
return false;
|
||||||
|
|
||||||
if(sqlite3_exec(sqlite_db, std::format("DELETE FROM recipes WHERE id={}", id).c_str(),
|
if(sqlite3_exec(db, std::format("DELETE FROM recipes WHERE id={}", id).c_str(),
|
||||||
nullptr, nullptr, nullptr) not_eq SQLITE_OK) {
|
nullptr, nullptr, nullptr) not_eq SQLITE_OK)
|
||||||
throw std::runtime_error(std::format("Failed to delete recipe with ID {} from database.", id));
|
return false;
|
||||||
}
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void db::del_recipes(const std::vector<int> &ids) {
|
bool db_del_recipes(const std::vector<int> &ids) {
|
||||||
std::string stmt = "DELETE FROM recipes WHERE id IN (";
|
std::string stmt = "DELETE FROM recipes WHERE id IN (";
|
||||||
|
|
||||||
if(not sqlite_db)
|
if(not db)
|
||||||
throw std::runtime_error(std::format("{}: Database not open! Please contact a developer.", __PRETTY_FUNCTION__));
|
return false;
|
||||||
|
|
||||||
bool first = true;
|
bool first = true;
|
||||||
for(auto id : ids) {
|
for(auto id : ids) {
|
||||||
@ -137,54 +139,53 @@ void db::del_recipes(const std::vector<int> &ids) {
|
|||||||
|
|
||||||
stmt += ");";
|
stmt += ");";
|
||||||
|
|
||||||
if(sqlite3_exec(sqlite_db, stmt.c_str(), nullptr, nullptr, nullptr) not_eq SQLITE_OK)
|
if(sqlite3_exec(db, stmt.c_str(), nullptr, nullptr, nullptr) not_eq SQLITE_OK)
|
||||||
throw std::runtime_error("Failed to delete recipes from database.");
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool db::recipe_exists(const int id) {
|
bool db_recipe_exists(const int id) {
|
||||||
bool exists = false;
|
bool exists = false;
|
||||||
|
|
||||||
if(not sqlite_db)
|
if(not db)
|
||||||
throw std::runtime_error(std::format("{}: Database not open! Please contact a developer.", __PRETTY_FUNCTION__));
|
return false;
|
||||||
|
|
||||||
if(sqlite3_exec(sqlite_db, std::format("SELECT id FROM recipes WHERE id={}", id).c_str(),
|
sqlite3_exec(db, std::format("SELECT id FROM recipes WHERE id={}", id).c_str(),
|
||||||
[](void *found,int,char**,char**) {
|
[](void *found,int,char**,char**) {
|
||||||
*static_cast<bool*>(found) = true;
|
*static_cast<bool*>(found) = true;
|
||||||
return 0;
|
return 0;
|
||||||
}, &exists, nullptr) not_eq SQLITE_OK) {
|
}, &exists, nullptr);
|
||||||
throw std::runtime_error("Failed to select from database.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return exists;
|
return exists;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct recipe db::get_recipe(const int id) {
|
int db_get_recipe_id(const std::string &name) {
|
||||||
|
if(not db)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return table_get_id_by_name("recipes", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct recipe db_get_recipe(const int id) {
|
||||||
struct recipe recipe;
|
struct recipe recipe;
|
||||||
|
|
||||||
if(not sqlite_db)
|
sqlite3_exec(db, std::format("SELECT * FROM recipes WHERE id={};", id).c_str(),
|
||||||
throw std::runtime_error(std::format("{}: Database not open! Please contact a developer.", __PRETTY_FUNCTION__));
|
[](void *recipe, int, char **col_data, char**) {
|
||||||
|
*static_cast<struct recipe*>(recipe) = { std::atoi(col_data[0]), col_data[1], col_data[2] };
|
||||||
if(sqlite3_exec(sqlite_db, std::format("SELECT * FROM recipes WHERE id={};", id).c_str(),
|
return 0;
|
||||||
[](void *recipe, int, char **col_data, char**) {
|
}, &recipe, nullptr);
|
||||||
*static_cast<struct recipe*>(recipe) = { std::atoi(col_data[0]), col_data[1], col_data[2] };
|
|
||||||
return 0;
|
|
||||||
}, &recipe, nullptr) not_eq SQLITE_OK) {
|
|
||||||
throw std::runtime_error("Failed to select from database.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return recipe;
|
return recipe;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<struct recipe> db::get_recipes(const std::vector<std::string> &ingredients,
|
std::vector<struct recipe> db_get_recipes(const std::vector<std::string> &ingredients,
|
||||||
const std::vector<std::string> &tags)
|
const std::vector<std::string> &tags)
|
||||||
{
|
{
|
||||||
std::vector<struct recipe> recipes;
|
std::vector<struct recipe> recipes;
|
||||||
std::string stmt = "SELECT id,name,description FROM recipes";
|
std::string stmt = "SELECT id,name,description FROM recipes";
|
||||||
std::string filters;
|
std::string filters;
|
||||||
|
|
||||||
if(not sqlite_db)
|
|
||||||
throw std::runtime_error(std::format("{}: Database not open! Please contact a developer.", __PRETTY_FUNCTION__));
|
|
||||||
|
|
||||||
if(not ingredients.empty() or not tags.empty())
|
if(not ingredients.empty() or not tags.empty())
|
||||||
filters += " WHERE";
|
filters += " WHERE";
|
||||||
|
|
||||||
@ -200,10 +201,14 @@ std::vector<struct recipe> db::get_recipes(const std::vector<std::string> &ingre
|
|||||||
|
|
||||||
filters += " id IN (SELECT recipe_id FROM recipe_ingredient WHERE ingredient_id=";
|
filters += " id IN (SELECT recipe_id FROM recipe_ingredient WHERE ingredient_id=";
|
||||||
|
|
||||||
if((id = get_ingredient_id(i)) <= 0)
|
// TODO: use throw?
|
||||||
throw std::runtime_error(std::format("Failed to find ingredient '{}'", i));
|
if((id = db_get_ingredient_id(i)) < 0) {
|
||||||
|
std::cerr << "Failed to find ingredient '" << i << "'" << std::endl;
|
||||||
|
return std::vector<struct recipe>();
|
||||||
|
} else {
|
||||||
|
filters += std::to_string(id);
|
||||||
|
}
|
||||||
|
|
||||||
filters += std::to_string(id);
|
|
||||||
filters += ")";
|
filters += ")";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -223,106 +228,111 @@ std::vector<struct recipe> db::get_recipes(const std::vector<std::string> &ingre
|
|||||||
|
|
||||||
filters += " id IN (SELECT recipe_id FROM recipe_tag WHERE tag_id=";
|
filters += " id IN (SELECT recipe_id FROM recipe_tag WHERE tag_id=";
|
||||||
|
|
||||||
if((id = get_tag_id(i)) <= 0)
|
// TODO: use throw?
|
||||||
throw std::runtime_error("Failed to find tag '{}'");
|
if((id = db_get_tag_id(i)) < 0) {
|
||||||
|
std::cerr << "Failed to find tag '" << i << "'" << std::endl;
|
||||||
|
return std::vector<struct recipe>();
|
||||||
|
} else {
|
||||||
|
filters += std::to_string(id);
|
||||||
|
}
|
||||||
|
|
||||||
filters += std::to_string(id);
|
|
||||||
filters += ")";
|
filters += ")";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stmt += filters + ";";
|
stmt += filters + ";";
|
||||||
|
|
||||||
if(sqlite3_exec(sqlite_db, stmt.c_str(),
|
sqlite3_exec(db, stmt.c_str(),
|
||||||
[](void *recipe_list, int, char **col_data, char**) {
|
[](void *recipe_list, int, char **col_data, char**) {
|
||||||
static_cast<std::vector<struct recipe>*>(recipe_list)->push_back({
|
auto recipe_vec = static_cast<std::vector<struct recipe>*>(recipe_list);
|
||||||
std::atoi(col_data[0]),
|
recipe_vec->push_back({
|
||||||
col_data[1],
|
std::atoi(col_data[0]),
|
||||||
col_data[2] });
|
col_data[1],
|
||||||
|
col_data[2] });
|
||||||
return 0;
|
return 0;
|
||||||
}, &recipes, nullptr) not_eq SQLITE_OK) {
|
}, &recipes, nullptr);
|
||||||
throw std::runtime_error("Failed to select recipes.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return recipes;
|
return recipes;
|
||||||
}
|
}
|
||||||
|
|
||||||
int db::add_ingredient(const std::string &name) {
|
int db_add_ingredient(const std::string &name) {
|
||||||
if(not sqlite_db)
|
if(not db)
|
||||||
throw std::runtime_error(std::format("{}: Database not open! Please contact a developer.", __PRETTY_FUNCTION__));
|
return -1;
|
||||||
|
|
||||||
if(sqlite3_exec(sqlite_db, std::format("INSERT INTO ingredients(name) VALUES(lower('{}'));", name).c_str(),
|
if(sqlite3_exec(db, std::format("INSERT INTO ingredients(name) VALUES(lower('{}'));", name).c_str(),
|
||||||
nullptr, nullptr, nullptr) not_eq SQLITE_OK) {
|
nullptr, nullptr, nullptr) not_eq SQLITE_OK)
|
||||||
throw std::runtime_error(std::format("Failed to instert ingredient '{}'.", name));
|
return -2;
|
||||||
}
|
|
||||||
|
|
||||||
return get_ingredient_id(name);
|
return db_get_ingredient_id(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> db::get_recipe_ingredients(const int id) {
|
std::vector<std::string> db_get_recipe_ingredients(const int id) {
|
||||||
std::vector<std::string> ingredients;
|
std::vector<std::string> ingredients;
|
||||||
|
|
||||||
if(not sqlite_db)
|
sqlite3_exec(db, std::format("SELECT name FROM ingredients WHERE id IN (SELECT ingredient_id FROM recipe_ingredient WHERE recipe_id={});", id).c_str(),
|
||||||
throw std::runtime_error(std::format("{}: Database not open! Please contact a developer.", __PRETTY_FUNCTION__));
|
[](void *ingredients, int, char **col_data, char**) {
|
||||||
|
static_cast<std::vector<std::string>*>(ingredients)->push_back(col_data[0]);
|
||||||
if(sqlite3_exec(sqlite_db, std::format("SELECT name FROM ingredients WHERE id IN (SELECT ingredient_id FROM recipe_ingredient WHERE recipe_id={});", id).c_str(),
|
return 0;
|
||||||
[](void *ingredients, int, char **col_data, char**) {
|
}, &ingredients, nullptr);
|
||||||
static_cast<std::vector<std::string>*>(ingredients)->push_back(col_data[0]);
|
|
||||||
return 0;
|
|
||||||
}, &ingredients, nullptr) not_eq SQLITE_OK) {
|
|
||||||
throw std::runtime_error(std::format("Failed to select ingredients from recipe with ID {}", id));
|
|
||||||
}
|
|
||||||
|
|
||||||
return ingredients;
|
return ingredients;
|
||||||
}
|
}
|
||||||
|
|
||||||
int db::add_tag(const std::string &name) {
|
int db_get_ingredient_id(const std::string &name) {
|
||||||
if(not sqlite_db)
|
if(not db)
|
||||||
throw std::runtime_error(std::format("{}: Database not open! Please contact a developer.", __PRETTY_FUNCTION__));
|
return -1;
|
||||||
|
|
||||||
if(sqlite3_exec(sqlite_db, std::format("INSERT INTO tags(name) VALUES('{}');", name).c_str(),
|
return table_get_id_by_name("ingredients", name);
|
||||||
nullptr, nullptr, nullptr) not_eq SQLITE_OK) {
|
|
||||||
throw std::runtime_error(std::format("Failed to insert tag '{}'", name));
|
|
||||||
}
|
|
||||||
|
|
||||||
return get_tag_id(name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> db::get_recipe_tags(const int id) {
|
int db_add_tag(const std::string &name) {
|
||||||
|
if(not db)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if(sqlite3_exec(db, std::format("INSERT INTO tags(name) VALUES('{}');", name).c_str(),
|
||||||
|
nullptr, nullptr, nullptr) not_eq SQLITE_OK)
|
||||||
|
return -2;
|
||||||
|
|
||||||
|
return db_get_tag_id(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> db_get_recipe_tags(const int id) {
|
||||||
std::vector<std::string> tags;
|
std::vector<std::string> tags;
|
||||||
|
|
||||||
if(not sqlite_db)
|
sqlite3_exec(db, std::format("SELECT name FROM tags WHERE id IN (SELECT tag_id FROM recipe_tag WHERE recipe_id={});", id).c_str(),
|
||||||
throw std::runtime_error(std::format("{}: Database not open! Please contact a developer.", __PRETTY_FUNCTION__));
|
[](void *tags, int, char **col_data, char**) {
|
||||||
|
static_cast<std::vector<std::string>*>(tags)->push_back(col_data[0]);
|
||||||
if(sqlite3_exec(sqlite_db, std::format("SELECT name FROM tags WHERE id IN (SELECT tag_id FROM recipe_tag WHERE recipe_id={});", id).c_str(),
|
return 0;
|
||||||
[](void *tags, int, char **col_data, char**) {
|
}, &tags, nullptr);
|
||||||
static_cast<std::vector<std::string>*>(tags)->push_back(col_data[0]);
|
|
||||||
return 0;
|
|
||||||
}, &tags, nullptr) not_eq SQLITE_OK) {
|
|
||||||
throw std::runtime_error(std::format("Failed to select tags for recipe with ID {}", id));
|
|
||||||
}
|
|
||||||
|
|
||||||
return tags;
|
return tags;
|
||||||
}
|
}
|
||||||
|
|
||||||
void db::conn_recipe_ingredient(int recipe_id, int ingredient_id) {
|
int db_get_tag_id(const std::string &name) {
|
||||||
if(not sqlite_db)
|
if(not db)
|
||||||
throw std::runtime_error(std::format("{}: Database not open! Please contact a developer.", __PRETTY_FUNCTION__));
|
return -1;
|
||||||
|
|
||||||
if(sqlite3_exec(sqlite_db, std::format("INSERT INTO recipe_ingredient(recipe_id, ingredient_id) VALUES({},{});", recipe_id, ingredient_id).c_str(),
|
return table_get_id_by_name("tags", name);
|
||||||
nullptr, nullptr, nullptr) not_eq SQLITE_OK) {
|
|
||||||
throw std::runtime_error(std::format("Failed to connect recipe with ID {} to ingredient with ID {}",
|
|
||||||
recipe_id, ingredient_id));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void db::conn_recipe_tag(int recipe_id, int tag_id) {
|
int db_conn_recipe_ingredient(int recipe_id, int ingredient_id) {
|
||||||
if(not sqlite_db)
|
if(not db)
|
||||||
throw std::runtime_error(std::format("{}: Database not open! Please contact a developer.", __PRETTY_FUNCTION__));
|
return -1;
|
||||||
|
|
||||||
if(sqlite3_exec(sqlite_db, std::format("INSERT INTO recipe_tag(recipe_id, tag_id) VALUES({},{});", recipe_id, tag_id).c_str(),
|
if(sqlite3_exec(db, std::format("INSERT INTO recipe_ingredient(recipe_id, ingredient_id) VALUES({},{});", recipe_id, ingredient_id).c_str(),
|
||||||
nullptr, nullptr, nullptr) not_eq SQLITE_OK) {
|
nullptr, nullptr, nullptr) not_eq SQLITE_OK)
|
||||||
throw std::runtime_error(std::format("Failed to connect recipe with ID {} to tag with ID {}",
|
return -2;
|
||||||
recipe_id, tag_id));
|
|
||||||
}
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int db_conn_recipe_tag(int recipe_id, int tag_id) {
|
||||||
|
if(not db)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if(sqlite3_exec(db, std::format("INSERT INTO recipe_tag(recipe_id, tag_id) VALUES({},{});", recipe_id, tag_id).c_str(),
|
||||||
|
nullptr, nullptr, nullptr) not_eq SQLITE_OK)
|
||||||
|
return -2;
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
116
src/db.hpp
116
src/db.hpp
@ -17,7 +17,6 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <sqlite3.h>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -27,73 +26,56 @@ struct recipe {
|
|||||||
std::string description;
|
std::string description;
|
||||||
};
|
};
|
||||||
|
|
||||||
class db {
|
int db_open(void);
|
||||||
private:
|
void db_close(void);
|
||||||
sqlite3 *sqlite_db;
|
|
||||||
int table_get_id_by_name(const std::string &table, const std::string &name);
|
|
||||||
|
|
||||||
public:
|
/**
|
||||||
db() : sqlite_db(nullptr) {}
|
* @brief Add a new recipe to the database.
|
||||||
~db() {
|
*
|
||||||
sqlite3_close(sqlite_db);
|
* @param name Name of the new recipe.
|
||||||
}
|
* @param description Short description.
|
||||||
void open(void);
|
*
|
||||||
void close(void);
|
* @return ID of newly created recipe, -1 if DB isn't open, -2 on other failure.
|
||||||
|
*/
|
||||||
|
int db_add_recipe(const std::string &name, const std::string &description);
|
||||||
|
bool db_del_recipe(const int id);
|
||||||
|
bool db_del_recipes(const std::vector<int> &ids);
|
||||||
|
int db_get_recipe_id(const std::string &name);
|
||||||
|
bool db_recipe_exists(const int id);
|
||||||
|
static inline bool db_recipe_exists(const std::string &name) {
|
||||||
|
return (db_get_recipe_id(name) > 0);
|
||||||
|
}
|
||||||
|
struct recipe db_get_recipe(const int id);
|
||||||
|
std::vector<struct recipe> db_get_recipes(const std::vector<std::string> &ingredients,
|
||||||
|
const std::vector<std::string> &tags);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Add a new recipe to the database.
|
* @brief Add a new ingredient to the database.
|
||||||
*
|
*
|
||||||
* @param name Name of the new recipe.
|
* @param name Name of the new ingredient.
|
||||||
* @param description Short description.
|
*
|
||||||
*
|
* @return ID of newly created ingredient, -1 if DB isn't open, -2 on other failure.
|
||||||
* @return ID of newly created recipe.
|
*/
|
||||||
*/
|
int db_add_ingredient(const std::string &name);
|
||||||
int add_recipe(const std::string &name, const std::string &description);
|
std::vector<std::string> db_get_recipe_ingredients(const int id);
|
||||||
void del_recipe(const int id);
|
int db_get_ingredient_id(const std::string &name);
|
||||||
void del_recipes(const std::vector<int> &ids);
|
static inline bool db_ingredient_exists(const std::string &name) {
|
||||||
inline int get_recipe_id(const std::string &name) {
|
return (db_get_ingredient_id(name) > 0);
|
||||||
return table_get_id_by_name("recipes", name);
|
}
|
||||||
}
|
|
||||||
inline bool db_recipe_exists(const std::string &name) {
|
|
||||||
return (get_recipe_id(name) > 0);
|
|
||||||
}
|
|
||||||
bool recipe_exists(const int id);
|
|
||||||
struct recipe get_recipe(const int id);
|
|
||||||
std::vector<struct recipe> get_recipes(const std::vector<std::string> &ingredients,
|
|
||||||
const std::vector<std::string> &tags);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Add a new ingredient to the database.
|
* @brief Add a new tag to the database.
|
||||||
*
|
*
|
||||||
* @param name Name of the new ingredient.
|
* @param name Name of the new tag.
|
||||||
*
|
*
|
||||||
* @return ID of newly created ingredient.
|
* @return ID of newly created tag, -1 if DB isn't open, -2 on other failure.
|
||||||
*/
|
*/
|
||||||
int add_ingredient(const std::string &name);
|
int db_add_tag(const std::string &name);
|
||||||
std::vector<std::string> get_recipe_ingredients(const int id);
|
std::vector<std::string> db_get_recipe_tags(const int id);
|
||||||
inline int get_ingredient_id(const std::string &name) {
|
int db_get_tag_id(const std::string &name);
|
||||||
return table_get_id_by_name("ingredients", name);
|
static inline bool db_tag_exists(const std::string &name) {
|
||||||
}
|
return (db_get_tag_id(name) > 0);
|
||||||
inline bool db_ingredient_exists(const std::string &name) {
|
}
|
||||||
return (get_ingredient_id(name) > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
int db_conn_recipe_ingredient(int recipe_id, int ingredient_id);
|
||||||
* @brief Add a new tag to the database.
|
int db_conn_recipe_tag(int recipe_id, int tag_id);
|
||||||
*
|
|
||||||
* @param name Name of the new tag.
|
|
||||||
*
|
|
||||||
* @return ID of newly created tag, -1 if DB isn't open, -2 on other failure.
|
|
||||||
*/
|
|
||||||
int add_tag(const std::string &name);
|
|
||||||
std::vector<std::string> get_recipe_tags(const int id);
|
|
||||||
inline int get_tag_id(const std::string &name) {
|
|
||||||
return table_get_id_by_name("tags", name);
|
|
||||||
}
|
|
||||||
inline bool tag_exists(const std::string &name) {
|
|
||||||
return (get_tag_id(name) > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void conn_recipe_ingredient(int recipe_id, int ingredient_id);
|
|
||||||
void conn_recipe_tag(int recipe_id, int tag_id);
|
|
||||||
};
|
|
||||||
|
55
src/main.cpp
55
src/main.cpp
@ -16,9 +16,8 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <exception>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <cstdlib>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "arg_parse.hpp"
|
#include "arg_parse.hpp"
|
||||||
@ -36,35 +35,29 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
id = parse_args(argv[1]);
|
id = parse_args(argv[1]);
|
||||||
|
|
||||||
try {
|
switch(id) {
|
||||||
switch(id) {
|
case CMD_ADD:
|
||||||
case CMD_ADD:
|
ret = command_add();
|
||||||
ret = cmd_add();
|
break;
|
||||||
break;
|
case CMD_LIST:
|
||||||
case CMD_LIST:
|
ret = command_list(argc - 1, argv + 1);
|
||||||
ret = cmd_list(argc - 1, argv + 1);
|
break;
|
||||||
break;
|
case CMD_DEL:
|
||||||
case CMD_DEL:
|
ret = command_delete(argc - 2, argv + 2);
|
||||||
ret = cmd_delete(argc - 2, argv + 2);
|
break;
|
||||||
break;
|
case CMD_INFO:
|
||||||
case CMD_INFO:
|
ret = command_info(std::stoi(argv[2]));
|
||||||
ret = cmd_info(std::stoi(argv[2]));
|
break;
|
||||||
break;
|
case CMD_HELP:
|
||||||
case CMD_HELP:
|
print_help();
|
||||||
print_help();
|
break;
|
||||||
break;
|
case CMD_VERSION:
|
||||||
case CMD_VERSION:
|
print_version();
|
||||||
print_version();
|
break;
|
||||||
break;
|
default:
|
||||||
default:
|
std::cerr << "No such command '" << argv[1] << "'. Use 'help' sub-command." << std::endl;
|
||||||
std::cerr << "No such command '" << argv[1] << "'. Use 'help' sub-command." << std::endl;
|
print_usage();
|
||||||
print_usage();
|
return EXIT_FAILURE;
|
||||||
ret = EXIT_FAILURE;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} catch(const std::exception &e) {
|
|
||||||
std::cerr << e.what() << std::endl;
|
|
||||||
ret = EXIT_FAILURE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
Loading…
Reference in New Issue
Block a user