diff options
-rw-r--r-- | src/actionhelper.c | 9 | ||||
-rw-r--r-- | src/actions.c | 10 | ||||
-rw-r--r-- | src/actions.h | 3 | ||||
-rw-r--r-- | src/dbhelper.c | 2 | ||||
-rw-r--r-- | src/dblayer.c | 171 | ||||
-rw-r--r-- | src/dblayer.h | 5 |
6 files changed, 142 insertions, 58 deletions
diff --git a/src/actionhelper.c b/src/actionhelper.c index 596a7db..667b898 100644 --- a/src/actionhelper.c +++ b/src/actionhelper.c @@ -82,8 +82,7 @@ AddOpt add_make_options(cklist *args) { DelOpt del_make_options(cklist *args) { list_rewind(args); DelOpt delOpt = { - .prog = NULL, - .path = "", + .arg = NULL, .isConf = 0, .err = DEL_NO_ERR }; @@ -94,13 +93,13 @@ DelOpt del_make_options(cklist *args) { delOpt.err = DEL_ERR_WRONG_ARGS; return delOpt; } - realpath(list_get(args), delOpt.path); - if (!util_is_file_rw(delOpt.path)) { + delOpt.arg = list_get(args); + if (!util_is_file_rw(delOpt.arg)) { delOpt.err = DEL_ERR_WRONG_PATH; return delOpt; } } else { - delOpt.prog = list_get(args); + delOpt.arg = list_get(args); } list_rewind(args); diff --git a/src/actions.c b/src/actions.c index f07d1ae..59343ab 100644 --- a/src/actions.c +++ b/src/actions.c @@ -84,20 +84,14 @@ int run_DEL(UserOpt * opt, Conf *conf) { DelOpt delOpt = del_make_options(opt->args); switch(delOpt.err) { case DEL_NO_ERR: - if (delOpt.isConf) { - printf("deleting conf %s\n", delOpt.path); - // del_transaction_try(&db, delOpt.path, delOpt.isConf); - } else { - printf("Deleting program %s\n", delOpt.prog); - // del_transaction_try(&db, delOpt.path, delOpt.isConf); - } + del_transaction_try(&db, delOpt.arg, delOpt.isConf); close_DB(&db); return 0; case DEL_ERR_WRONG_ARGS: printf("wrong del args\n"); break; case DEL_ERR_WRONG_PATH: - printf("path %s doesnt exist\n", delOpt.path); + printf("path %s doesnt exist\n", delOpt.arg); } error: close_DB(&db); diff --git a/src/actions.h b/src/actions.h index 0ca7838..c9da4fb 100644 --- a/src/actions.h +++ b/src/actions.h @@ -49,8 +49,7 @@ enum DelErrors { typedef struct DelOptions DelOpt; struct DelOptions { - char *prog; - char path[STR_L]; + char *arg; int isConf; DelErr err; }; diff --git a/src/dbhelper.c b/src/dbhelper.c index 8e4d9ad..9de780e 100644 --- a/src/dbhelper.c +++ b/src/dbhelper.c @@ -204,7 +204,7 @@ void dbh_form_query_delete_x_from_y(char *query, const char *x, const char *y) { strcpy(query, tmp); } -void dbh_from_query_count_program_relations(char *query) { +void dbh_form_query_count_program_relations(char *query) { char tmp[STR_M] = "SELECT COUNT(*) FROM "; strcat(tmp, TBL_REL); strcat(tmp, " WHERE "); 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; } diff --git a/src/dblayer.h b/src/dblayer.h index 6ba4e27..cf01ccf 100644 --- a/src/dblayer.h +++ b/src/dblayer.h @@ -72,4 +72,9 @@ extern int list_get_paths(DB *db, cklist *ckl, int attr); extern int list_get_programs(DB *db, cklist *ckl); extern int list_get_path_program_tree(DB *db, cklist *ckl, int attr); +/*******/ +/* del */ +/*******/ + +extern int del_transaction_try(DB *db, char *arg, int conf); #endif /* DBLAYER_H */ |