menu-helper/src/db.cpp

178 lines
5.1 KiB
C++
Raw Normal View History

2024-09-18 15:31:47 +02:00
/*
* Copyright (C) 2024 Nicolás Ortega Froysa <nicolas@ortegas.org>
* Nicolás Ortega Froysa <nicolas@ortegas.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2024-10-09 19:50:53 +02:00
#include "db.hpp"
2024-09-18 15:31:47 +02:00
#include <sqlite3.h>
2024-10-09 21:12:16 +02:00
#include <cstdlib>
#include <iostream>
#include <filesystem>
#include <format>
#include <string>
2024-09-18 15:31:47 +02:00
2024-10-09 21:12:16 +02:00
#define DB_VERSION 1
2024-09-18 15:31:47 +02:00
static sqlite3 *db = NULL;
int db_open(void) {
2024-10-09 21:12:16 +02:00
std::string xdg_data_home;
std::string db_path;
bool new_db = false;
2024-09-18 15:31:47 +02:00
int rc;
2024-10-09 21:12:16 +02:00
if((xdg_data_home = std::getenv("XDG_DATA_HOME")).empty()) {
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;
2024-09-18 15:31:47 +02:00
return 0;
}
2024-10-09 21:12:16 +02:00
db_path = xdg_data_home + "/menu-helper";
2024-09-18 15:31:47 +02:00
2024-10-09 21:12:16 +02:00
if(not std::filesystem::exists(db_path))
std::filesystem::create_directories(db_path);
2024-10-09 21:12:16 +02:00
db_path += "/recipes.db";
2024-10-09 21:12:16 +02:00
//if(access(db_path, F_OK) != 0) {
if(not std::filesystem::exists(db_path)) {
std::cout << "Creating database: " << db_path << std::endl;
new_db = true;
2024-09-18 15:31:47 +02:00
}
2024-10-09 21:12:16 +02:00
rc = sqlite3_open(db_path.c_str(), &db);
2024-09-18 15:31:47 +02:00
if(rc == SQLITE_OK && new_db) {
sqlite3_exec(db, "CREATE TABLE db_version(version INTEGER UNIQUE NOT NULL);", NULL, NULL, NULL);
2024-10-09 21:12:16 +02:00
//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(), NULL, NULL, NULL);
2024-09-18 15:31:47 +02:00
sqlite3_exec(db, "CREATE TABLE tags(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE);", NULL, NULL, NULL);
sqlite3_exec(db, "CREATE TABLE ingredients(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE);", NULL, NULL, NULL);
sqlite3_exec(db, "CREATE TABLE recipes(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE, description STRING);", NULL, NULL, NULL);
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);", NULL, NULL, NULL);
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);", NULL, NULL, NULL);
}
return rc == SQLITE_OK;
}
void db_close(void) {
2024-10-09 21:12:16 +02:00
if(not db)
2024-09-18 15:31:47 +02:00
return;
sqlite3_close(db);
}
int query_id_cb(void *recipe_id_var, int col_num, char **col_data, char **col_name) {
int *recipe_id_ptr = (int*)recipe_id_var;
int ret = 1;
for(int i = 0; i < col_num; ++i) {
2024-10-09 21:12:16 +02:00
if(std::string(col_name[i]) == "id") {
*recipe_id_ptr = std::atoi(col_data[i]);
ret = 0;
break;
}
}
return ret;
}
2024-10-09 21:12:16 +02:00
int table_get_id_by_name(const std::string &table, const std::string &name) {
int id = 0;
2024-10-09 21:12:16 +02:00
if(sqlite3_exec(db, std::format("SELECT id FROM {} WHERE lower(name)=lower('{}');", table, name).c_str(),
query_id_cb, &id, NULL) != SQLITE_OK)
return -2;
return id;
}
2024-10-09 21:12:16 +02:00
int db_add_recipe(const std::string &name, const std::string &description) {
if(not db)
return -1;
2024-10-09 21:12:16 +02:00
if(sqlite3_exec(db, std::format("INSERT INTO recipes(name,description) VALUES('{}','{}');", name, description).c_str(),
NULL, NULL, NULL) != SQLITE_OK)
return -2;
return db_get_recipe_id(name);
}
2024-10-09 21:12:16 +02:00
int db_get_recipe_id(const std::string &name) {
if(not db)
return -1;
return table_get_id_by_name("recipes", name);
}
2024-10-09 21:12:16 +02:00
int db_add_ingredient(const std::string &name) {
if(not db)
return -1;
2024-10-09 21:12:16 +02:00
if(sqlite3_exec(db, std::format("INSERT INTO ingredients(name) VALUES(lower('{}'));", name).c_str(),
NULL, NULL, NULL) != SQLITE_OK)
return -2;
return db_get_ingredient_id(name);
}
2024-10-09 21:12:16 +02:00
int db_get_ingredient_id(const std::string &name) {
if(not db)
return -1;
return table_get_id_by_name("ingredients", name);
}
2024-10-09 21:12:16 +02:00
int db_add_tag(const std::string &name) {
if(not db)
return -1;
2024-10-09 21:12:16 +02:00
if(sqlite3_exec(db, std::format("INSERT INTO tags(name) VALUES('{}');", name).c_str(),
NULL, NULL, NULL) != SQLITE_OK)
return -2;
return db_get_tag_id(name);
}
2024-10-09 21:12:16 +02:00
int db_get_tag_id(const std::string &name) {
if(not db)
return -1;
return table_get_id_by_name("tags", name);
}
int db_conn_recipe_ingredient(int recipe_id, int ingredient_id) {
2024-10-09 21:12:16 +02:00
if(not db)
return -1;
2024-10-09 21:12:16 +02:00
if(sqlite3_exec(db, std::format("INSERT INTO recipe_ingredient(recipe_id, ingredient_id) VALUES({},{});", recipe_id, ingredient_id).c_str(),
NULL, NULL, NULL) != SQLITE_OK)
return -2;
return 1;
}
int db_conn_recipe_tag(int recipe_id, int tag_id) {
2024-10-09 21:12:16 +02:00
if(not db)
return -1;
2024-10-09 21:12:16 +02:00
if(sqlite3_exec(db, std::format("INSERT INTO recipe_tag(recipe_id, tag_id) VALUES({},{});", recipe_id, tag_id).c_str(),
NULL, NULL, NULL) != SQLITE_OK)
return -2;
return 1;
}