From fe781e5ae7116733e5b335a0ac016af97266db5f Mon Sep 17 00:00:00 2001 From: Anastasis Grammenos Date: Mon, 8 Oct 2018 23:47:07 +0300 Subject: Way better Edit --- src/dblayer.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 127 insertions(+), 3 deletions(-) (limited to 'src/dblayer.c') diff --git a/src/dblayer.c b/src/dblayer.c index 8ec062c..4290a7a 100644 --- a/src/dblayer.c +++ b/src/dblayer.c @@ -12,6 +12,8 @@ * Give access to the database. * * -------------------------------------------------------------------------- */ +#include + #include "dblayer.h" #include "dbhelper.h" #include "ckutil.h" @@ -254,6 +256,13 @@ int get_program_id(DB *db, const char* name) { return id; } +int program_exists(DB *db, const char *pName) { + if (get_program_id(db, pName) == -1) { + return 0; + } + return 1; +} + int get_config_id(DB *db, const char* path) { sqlite3_stmt *stmt; int rc; @@ -281,7 +290,6 @@ int add_insert_relationship(DB *db, const int pid, const int cid) { return insert_to_rel_table(db, pid, cid); } - /* Returns the path of the found config via *ret */ int program_has_primary_config(DB *db, const int pid, char *ret, int *sec) { sqlite3_stmt *stmt; @@ -403,13 +411,116 @@ int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secr str_make_ck_config_name(confName, path, pName); strcpy(ret, confName); } - return 1; + return 0; } } } /* No prime config found */ - return 0; + return -1; +} + +int edit_get_config(DB *db, const char *pName, char *ret, const char *cName, int *sec) { + int pid = get_program_id(db, pName); + /* error */ + if (pid == -2) { + return -1; + } + + /* program exists */ + if (pid > -1) { + sqlite3_stmt *stmt; + int rc; + + char selection[STR_M] = COL_CONFIG_PATH; + strcat(selection, ", "); + strcat(selection, COL_CONFIG_SECRET); + char condition[STR_M] = TBL_PROGRAM; + strcat(condition, "."); + strcat(condition, COL_PROGRAM_ID); + + char sql[STR_L]; + dbh_form_query_select_from_joined_eq(sql, selection, condition); + + rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); + sqlite3_bind_int(stmt, 1, pid); + if (rc != SQLITE_OK) { + return -2; + } + + int flag = -1; + while (sqlite3_step(stmt) == SQLITE_ROW) { + char confName[STR_M]; + if (cName) { + char *tmp = strdup((char *)sqlite3_column_text(stmt, 0)); + if (strcmp(cName, basename(tmp)) == 0) { + flag = 0; + char confName[STR_M]; + str_make_ck_config_name(confName, (char *)sqlite3_column_text(stmt, 0), pName); + strcpy(ret, confName); + if (sec) { + *sec = sqlite3_column_int(stmt, 1); + } + } + free(tmp); + } + else { + char confName[STR_M]; + str_make_ck_config_name(confName, (char *)sqlite3_column_text(stmt, 0), pName); + strcpy(ret, confName); + flag = 0; + if (sec) { + *sec = sqlite3_column_int(stmt, 1); + } + break; + } + } + sqlite3_finalize(stmt); + return flag; + } +} + +int edit_get_avaliable_paths(DB *db, cklist *ckl, const char* pName) { + int pid = get_program_id(db, pName); + /* error */ + if (pid == -2) { + return -1; + } + + /* program exists */ + if (pid > -1) { + sqlite3_stmt *stmt; + int rc; + + char selection[STR_M] = COL_CONFIG_PATH; + char condition[STR_M] = TBL_PROGRAM; + strcat(condition, "."); + strcat(condition, COL_PROGRAM_ID); + + char sql[STR_L]; + dbh_form_query_select_from_joined_eq(sql, selection, condition); + + rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); + sqlite3_bind_int(stmt, 1, pid); + if (rc != SQLITE_OK) { + return -2; + } + + char name[STR_M] = ""; + strcat(name, pName); + strcat(name, ":"); + list_add(ckl, name); + while (sqlite3_step(stmt) == SQLITE_ROW) { + char *tmp = strdup((char *)sqlite3_column_text(stmt, 0)); + char entry[STR_M] = ""; + strcat(entry, "|- "); + strcat(entry, basename(tmp)); + list_add(ckl, entry); + free(tmp); + } + sqlite3_finalize(stmt); + return 0; + } } int list_get_paths(DB *db, cklist *ckl, int attr) { @@ -619,6 +730,19 @@ int get_program_relations(DB *db, int pid) { return count; } +int get_config_number(DB *db, char* pName) { + int pid = get_program_id(db, pName); + /* error */ + if (pid == -2) { + return -1; + } + + /* program exists */ + if (pid > -1) { + return get_program_relations(db, pid); + } +} + /* Removes the relationship of `cid` with the corresponding program. * Returns the program's pid on succes, negative integer otherwise. */ -- cgit v1.2.3