diff options
Diffstat (limited to 'src/dblayer.c')
-rw-r--r-- | src/dblayer.c | 171 |
1 files changed, 129 insertions, 42 deletions
diff --git a/src/dblayer.c b/src/dblayer.c index f255764..41e8bfe 100644 --- a/src/dblayer.c +++ b/src/dblayer.c @@ -232,7 +232,7 @@ int insert_to_rel_table(DB *db, const int pid, const int cid) { /* Returns -2 or error, -1 if program doesn't exist * else the program ID */ -int program_exists(DB *db, const char* name) { +int get_program_id(DB *db, const char* name) { sqlite3_stmt *stmt; int rc; @@ -241,7 +241,7 @@ int program_exists(DB *db, const char* name) { rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); if (rc != SQLITE_OK) { - PRINT_ERR("Error while preparing program_exists sql."); + PRINT_ERR("Error while preparing get_program_id sql."); return -2; } sqlite3_bind_text(stmt, 1, name, strlen(name), 0); @@ -254,7 +254,7 @@ int program_exists(DB *db, const char* name) { return id; } -int config_exists(DB *db, const char* path) { +int get_config_id(DB *db, const char* path) { sqlite3_stmt *stmt; int rc; @@ -263,7 +263,7 @@ int config_exists(DB *db, const char* path) { rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); if (rc != SQLITE_OK) { - PRINT_ERR("while preparing config_exists sql."); + PRINT_ERR("while preparing get_config_id sql."); return -2; } sqlite3_bind_text(stmt, 1, path, strlen(path), 0); @@ -325,7 +325,7 @@ int program_has_primary_config(DB *db, const int pid, char *ret, int *sec) { } int add_get_or_insert_config_to_db(DB *db, const int pid, const char *path, const int secret, const int prime) { - int cid = config_exists(db, path); + int cid = get_config_id(db, path); if (cid == -2) { db->error = SQL_ERR_SQLITE; return -1; @@ -345,7 +345,7 @@ int add_get_or_insert_config_to_db(DB *db, const int pid, const char *path, cons } int add_get_or_insert_program_to_db(DB *db, const char *name) { - int pid = program_exists(db, name); + int pid = get_program_id(db, name); if (pid == -2) { db->error = SQL_ERR_SQLITE; return -1; @@ -387,7 +387,7 @@ int add_transaction_try(DB *db, const AddOpt * const opt) { } int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secret) { - int pid = program_exists(db, pName); + int pid = get_program_id(db, pName); /* error */ if (pid == -2) { return -1; @@ -526,12 +526,33 @@ int list_get_path_program_tree(DB *db, cklist *ckl, int attr) { return 1; } +int delete_prog(DB *db, int pid) { + sqlite3_stmt *stmt; + int rc; + + char sql[STR_M]; + dbh_form_query_delete_x_from_y(sql, COL_PROGRAM_ID, TBL_PROGRAM); + + rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); + if (rc != SQLITE_OK) { + return -1; + } + sqlite3_bind_int(stmt, 1, pid); + sqlite3_step(stmt); + if (rc != SQLITE_OK) { + return -1; + } + + sqlite3_finalize(stmt); + return 0; +} + 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); + dbh_form_query_delete_x_from_y(sql, COL_CONFIG_ID, TBL_CONFIG); rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); if (rc != SQLITE_OK) { @@ -547,79 +568,145 @@ int delete_conf(DB *db, int cid) { return 0; } -int remove_conf_rel(db, cid, &pid) { +int get_pid_from_cid(DB *db, int cid) { + int pid = -1; sqlite3_stmt *stmt; int rc; char sql[STR_M]; - dbh_form_query_delete_x_from_y(sql, COL_REL_CONFIG_ID, TBL_REL); + dbh_form_query_get_pid_from_cid(sql); 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); + + while (sqlite3_step(stmt) == SQLITE_ROW) { + pid = sqlite3_column_int(stmt, 0); + } + if (rc != SQLITE_OK) { return -1; } + sqlite3_finalize(stmt); + return pid; +} + +int get_program_relations(DB *db, int pid) { + int count = -1; + sqlite3_stmt *stmt; + int rc; + + char sql[STR_M]; + dbh_form_query_count_program_relations(sql); - char sql2[STR_M]; + rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); + if (rc != SQLITE_OK) { + return -1; + } + + sqlite3_bind_int(stmt, 1, pid); + while (sqlite3_step(stmt) == SQLITE_ROW) { + count = sqlite3_column_int(stmt, 0); + } + if (rc != SQLITE_OK) { + return -1; + } + sqlite3_finalize(stmt); + return count; +} +/* Removes the relationship of `cid` with the corresponding program. + * Returns the program's pid on succes, negative integer otherwise. + */ +int remove_conf_rel(DB *db, int cid) { + sqlite3_stmt *stmt; + int rc; + + int pid = get_pid_from_cid(db, cid); + + 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; + } + + sqlite3_finalize(stmt); + + return pid; +} + +int remove_all_configs(DB *db, int pid) { + sqlite3_stmt *stmt; + int rc; + + char sql[STR_M]; + dbh_form_query_select_from_joined_eq(sql, COL_REL_CONFIG_ID, COL_REL_PROGRAM_ID); + rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); + if (rc != SQLITE_OK) { + return -2; + } + + sqlite3_bind_int(stmt, 1, pid); + + while (sqlite3_step(stmt) == SQLITE_ROW) { + delete_conf(db, sqlite3_column_int(stmt, 0)); + remove_conf_rel(db, sqlite3_column_int(stmt, 0)); + } 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; + int cid = get_config_id(db, arg); + printf("cid: %d\n", cid); + if (cid < 0) { + PRINT_ERR("Config doesn't exist in the database.\n"); } - PRINT_ERR("Config doesn't exist in the database.\n"); - } - // del prog - if (pid < 0) { - pid = program_exists(db, arg); + if (delete_conf(db, cid)) { + PRINT_ERR("Could not delete config from db.\n"); + return -1; + } + // handle relations + pid = remove_conf_rel(db, cid); + printf("pid: %d\n", pid); + if (get_program_relations(db, pid) > 0) { + printf("More rels left\n"); + goto end; + } + } else { + pid = get_program_id(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; + if(delete_prog(db, pid)) { + PRINT_ERR("Could not delete program from db.\n"); } - else if (db->error == SQL_CONFIG_PATH_EXISTS) { - PRINT_ERR("This config doesn't exist in the database.\n"); + if (db->error == SQL_ERR_SQLITE) { return -1; } + end: __END_TRANSACTION__ - return 0; } |