aboutsummaryrefslogtreecommitdiffstats
path: root/src/dblayer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dblayer.c')
-rw-r--r--src/dblayer.c100
1 files changed, 99 insertions, 1 deletions
diff --git a/src/dblayer.c b/src/dblayer.c
index 128ad2c..f255764 100644
--- a/src/dblayer.c
+++ b/src/dblayer.c
@@ -303,7 +303,7 @@ int program_has_primary_config(DB *db, const int pid, char *ret, int *sec) {
rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
if (rc != SQLITE_OK) {
- PRINT_ERR("while preparing program_has_primary_exists sql.");
+ PRINT_ERR("whinnle preparing program_has_primary_exists sql.");
return -2;
}
@@ -525,3 +525,101 @@ int list_get_path_program_tree(DB *db, cklist *ckl, int attr) {
return 1;
}
+
+int delete_conf(DB *db, int cid) {
+ sqlite3_stmt *stmt;
+ int rc;
+
+ char sql[STR_M];
+ dbh_form_query_delete_x_from_y(sql, COL_ID_CONFIG, TBL_CONFIG);
+
+ rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
+ if (rc != SQLITE_OK) {
+ return -1;
+ }
+ sqlite3_bind_int(stmt, 1, cid);
+ sqlite3_step(stmt);
+ if (rc != SQLITE_OK) {
+ return -1;
+ }
+
+ sqlite3_finalize(stmt);
+ return 0;
+}
+
+int remove_conf_rel(db, cid, &pid) {
+ sqlite3_stmt *stmt;
+ int rc;
+
+ char sql[STR_M];
+ dbh_form_query_delete_x_from_y(sql, COL_REL_CONFIG_ID, TBL_REL);
+
+ rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
+ if (rc != SQLITE_OK) {
+ return -1;
+ }
+ sqlite3_bind_int(stmt, 1, cid);
+ sqlite3_step(stmt);
+ if (rc != SQLITE_OK) {
+ return -1;
+ }
+
+ char sql2[STR_M];
+
+
+ sqlite3_finalize(stmt);
+ return 0;
+}
+
+int del_transaction_try(DB *db, char *arg, int conf) {
+ __BEGIN_TRANSACTION__
+ int pid = -1;
+ int empty_prog = 0;
+ if (conf) {
+ // del conf
+ cid = config_exists(db, arg);
+ if (cid >= 0) {
+ if (!delete_conf(db, cid)) {
+ PRINT_ERR("Could not delete config from db.\n");
+ return -1;
+ }
+ // handle relations
+ /* Removes the relationship of `cid` with the corresponding program,
+ * then checks if the program has no other relations and if it's true
+ * returns that program's id in `pid` and the number of relations the program
+ * has */
+ int relations_left = remove_conf_rel(db, cid, &pid);
+ if (relations_left > 0) {
+ return 0;
+ }
+ empty_prog = 1;
+ }
+ PRINT_ERR("Config doesn't exist in the database.\n");
+ }
+ // del prog
+ if (pid < 0) {
+ pid = program_exists(db, arg);
+ }
+ if (pid < 0) {
+ PRINT_ERR("Program not found in the db.\n");
+ return -1;
+ }
+ if(!delete_program(db, pid)) {
+ PRINT_ERR("Could not delete program from db.\n");
+ }
+ /* If we are deleting a proram we should delete everything that
+ * refferences it (configs and relationships) */
+ if (!conf) {
+ remove_all_configs(db, pid);
+ }
+ if (db->error == SQL_ERR_SQLITE) {
+ return -1;
+ }
+ else if (db->error == SQL_CONFIG_PATH_EXISTS) {
+ PRINT_ERR("This config doesn't exist in the database.\n");
+ return -1;
+ }
+ __END_TRANSACTION__
+
+ return 0;
+}