Compare commits
2 Commits
e25fb66dbc
...
c0c6959774
Author | SHA1 | Date | |
---|---|---|---|
c0c6959774 | |||
08b4834d9e |
@ -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:")) != -1) {
|
while((opt = getopt(argc, argv, "i:t:")) not_eq -1) {
|
||||||
switch(opt) {
|
switch(opt) {
|
||||||
case 'i':
|
case 'i':
|
||||||
ingredients = split(optarg, ",");
|
ingredients = split(optarg, ",");
|
||||||
|
16
src/db.cpp
16
src/db.cpp
@ -47,7 +47,6 @@ 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;
|
||||||
@ -55,9 +54,8 @@ int db_open(void) {
|
|||||||
|
|
||||||
rc = sqlite3_open(db_path.c_str(), &db);
|
rc = sqlite3_open(db_path.c_str(), &db);
|
||||||
|
|
||||||
if(rc == SQLITE_OK && new_db) {
|
if(rc == SQLITE_OK and 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);
|
||||||
@ -95,7 +93,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) != SQLITE_OK)
|
query_id_cb, &id, nullptr) not_eq SQLITE_OK)
|
||||||
return -2;
|
return -2;
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
@ -106,7 +104,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) != SQLITE_OK)
|
nullptr, nullptr, nullptr) not_eq SQLITE_OK)
|
||||||
return -2;
|
return -2;
|
||||||
|
|
||||||
return db_get_recipe_id(name);
|
return db_get_recipe_id(name);
|
||||||
@ -250,7 +248,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) != SQLITE_OK)
|
nullptr, nullptr, nullptr) not_eq SQLITE_OK)
|
||||||
return -2;
|
return -2;
|
||||||
|
|
||||||
return db_get_ingredient_id(name);
|
return db_get_ingredient_id(name);
|
||||||
@ -268,7 +266,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) != SQLITE_OK)
|
nullptr, nullptr, nullptr) not_eq SQLITE_OK)
|
||||||
return -2;
|
return -2;
|
||||||
|
|
||||||
return db_get_tag_id(name);
|
return db_get_tag_id(name);
|
||||||
@ -286,7 +284,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) != SQLITE_OK)
|
nullptr, nullptr, nullptr) not_eq SQLITE_OK)
|
||||||
return -2;
|
return -2;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
@ -297,7 +295,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) != SQLITE_OK)
|
nullptr, nullptr, nullptr) not_eq SQLITE_OK)
|
||||||
return -2;
|
return -2;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user