Compare commits

..

No commits in common. "c0c695977418575a53ebde1e80564add059d575f" and "e25fb66dbc06c4825a95bf8e1ba9a8da2479af69" have entirely different histories.

2 changed files with 10 additions and 8 deletions

View File

@ -74,7 +74,7 @@ int command_list(int argc, char *argv[]) {
std::vector<std::string> ingredients, tags; std::vector<std::string> ingredients, tags;
int opt; int opt;
while((opt = getopt(argc, argv, "i:t:")) not_eq -1) { while((opt = getopt(argc, argv, "i:t:")) != -1) {
switch(opt) { switch(opt) {
case 'i': case 'i':
ingredients = split(optarg, ","); ingredients = split(optarg, ",");

View File

@ -47,6 +47,7 @@ int db_open(void) {
db_path += "/recipes.db"; db_path += "/recipes.db";
//if(access(db_path, F_OK) != 0) {
if(not std::filesystem::exists(db_path)) { if(not std::filesystem::exists(db_path)) {
std::cout << "Creating database: " << db_path << std::endl; std::cout << "Creating database: " << db_path << std::endl;
new_db = true; new_db = true;
@ -54,8 +55,9 @@ int db_open(void) {
rc = sqlite3_open(db_path.c_str(), &db); rc = sqlite3_open(db_path.c_str(), &db);
if(rc == SQLITE_OK and new_db) { if(rc == SQLITE_OK && new_db) {
sqlite3_exec(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);
//snprintf(insert_version_stmt, 64, "INSERT INTO db_version VALUES(%d);", DB_VERSION);
sqlite3_exec(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(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(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);
@ -93,7 +95,7 @@ int table_get_id_by_name(const std::string &table, const std::string &name) {
int id = 0; int id = 0;
if(sqlite3_exec(db, std::format("SELECT id FROM {} WHERE lower(name)=lower('{}');", table, name).c_str(), if(sqlite3_exec(db, std::format("SELECT id FROM {} WHERE lower(name)=lower('{}');", table, name).c_str(),
query_id_cb, &id, nullptr) not_eq SQLITE_OK) query_id_cb, &id, nullptr) != SQLITE_OK)
return -2; return -2;
return id; return id;
@ -104,7 +106,7 @@ int db_add_recipe(const std::string &name, const std::string &description) {
return -1; return -1;
if(sqlite3_exec(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) != SQLITE_OK)
return -2; return -2;
return db_get_recipe_id(name); return db_get_recipe_id(name);
@ -248,7 +250,7 @@ int db_add_ingredient(const std::string &name) {
return -1; return -1;
if(sqlite3_exec(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) != SQLITE_OK)
return -2; return -2;
return db_get_ingredient_id(name); return db_get_ingredient_id(name);
@ -266,7 +268,7 @@ int db_add_tag(const std::string &name) {
return -1; return -1;
if(sqlite3_exec(db, std::format("INSERT INTO tags(name) VALUES('{}');", name).c_str(), if(sqlite3_exec(db, std::format("INSERT INTO tags(name) VALUES('{}');", name).c_str(),
nullptr, nullptr, nullptr) not_eq SQLITE_OK) nullptr, nullptr, nullptr) != SQLITE_OK)
return -2; return -2;
return db_get_tag_id(name); return db_get_tag_id(name);
@ -284,7 +286,7 @@ int db_conn_recipe_ingredient(int recipe_id, int ingredient_id) {
return -1; return -1;
if(sqlite3_exec(db, std::format("INSERT INTO recipe_ingredient(recipe_id, ingredient_id) VALUES({},{});", recipe_id, ingredient_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) != SQLITE_OK)
return -2; return -2;
return 1; return 1;
@ -295,7 +297,7 @@ int db_conn_recipe_tag(int recipe_id, int tag_id) {
return -1; return -1;
if(sqlite3_exec(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_tag(recipe_id, tag_id) VALUES({},{});", recipe_id, tag_id).c_str(),
nullptr, nullptr, nullptr) not_eq SQLITE_OK) nullptr, nullptr, nullptr) != SQLITE_OK)
return -2; return -2;
return 1; return 1;