diff options
author | Anastasis Grammenos <anastasis.gramm2@gmail.com> | 2018-10-03 13:25:21 +0300 |
---|---|---|
committer | Anastasis Grammenos <anastasis.gramm2@gmail.com> | 2018-10-03 13:25:21 +0300 |
commit | 1cfbedd0b199d77ed53e5224435a76978875e2fb (patch) | |
tree | 8bfbb856ab4e02a8c5d0085ff6b75e09cbac8f18 | |
parent | 6d162956409c0fa394920ba707109afdbff041e5 (diff) | |
download | ck-1cfbedd0b199d77ed53e5224435a76978875e2fb.tar.gz ck-1cfbedd0b199d77ed53e5224435a76978875e2fb.tar.bz2 ck-1cfbedd0b199d77ed53e5224435a76978875e2fb.zip |
Work on delete
-rw-r--r-- | src/actions.c | 2 | ||||
-rw-r--r-- | src/dbhelper.c | 32 | ||||
-rw-r--r-- | src/dbhelper.h | 9 | ||||
-rw-r--r-- | src/dblayer.c | 100 |
4 files changed, 138 insertions, 5 deletions
diff --git a/src/actions.c b/src/actions.c index e6eb3d3..f07d1ae 100644 --- a/src/actions.c +++ b/src/actions.c @@ -86,8 +86,10 @@ int run_DEL(UserOpt * opt, Conf *conf) { 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); } close_DB(&db); return 0; diff --git a/src/dbhelper.c b/src/dbhelper.c index b183770..8e4d9ad 100644 --- a/src/dbhelper.c +++ b/src/dbhelper.c @@ -193,3 +193,35 @@ void dbh_form_query_select_programs(char *query) { strcpy(query, tmp); } + +void dbh_form_query_delete_x_from_y(char *query, const char *x, const char *y) { + char tmp[STR_M] = "DELETE FROM "; + strcat(tmp, y); + strcat(tmp, " WHERE "); + strcat(tmp, x); + strcat(tmp, " = ?;"); + + strcpy(query, tmp); +} + +void dbh_from_query_count_program_relations(char *query) { + char tmp[STR_M] = "SELECT COUNT(*) FROM "; + strcat(tmp, TBL_REL); + strcat(tmp, " WHERE "); + strcat(tmp, COL_REL_PROGRAM_ID); + strcat(tmp, " = ?;"); + + strcpy(query, tmp); +} + +void dbh_form_query_get_pid_from_cid(char *query) { + char tmp[STR_M] = "SELECT "; + strcat(tmp, COL_REL_PROGRAM_ID); + strcat(tmp, " FROM "); + strcat(tmp, TBL_REL); + strcat(tmp, " WHERE "); + strcat(tmp, COL_REL_CONFIG_ID); + strcat(tmp, " = ?;"); + + strcpy(query, tmp); +} diff --git a/src/dbhelper.h b/src/dbhelper.h index b2f4abe..a8d3292 100644 --- a/src/dbhelper.h +++ b/src/dbhelper.h @@ -38,10 +38,8 @@ #define COL_REL_PROGRAM_ID "PID" #define COL_REL_CONFIG_ID "CID" -#define __BEGIN_TRANSACTION__ \ - sqlite3_exec(db->ptr, "BEGIN TRANSACTION;", NULL, NULL, NULL); -#define __END_TRANSACTION__ \ - sqlite3_exec(db->ptr, "END TRANSACTION;", NULL, NULL, NULL); +#define __BEGIN_TRANSACTION__ sqlite3_exec(db->ptr, "BEGIN TRANSACTION;", NULL, NULL, NULL); +#define __END_TRANSACTION__ sqlite3_exec(db->ptr, "END TRANSACTION;", NULL, NULL, NULL); /****************/ /* form queries */ @@ -60,4 +58,7 @@ extern void dbh_form_query_select_paths_with_attributes(char *query); extern void dbh_form_query_select_programs(char *query); extern void dbh_form_query_select_from_joined_eq(char *query, const char *selection, const char* condition); extern void dbh_form_query_select_from_joined_like(char *query, const char *selection, const char* condition); +extern void dbh_form_query_delete_x_from_y(char *query, const char *x, const char *y); +extern void dbh_form_query_count_program_relations(char *query); +extern void dbh_form_query_get_pid_from_cid(char *query); #endif /* DBHELPER_H */ 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; +} |