diff options
author | gramanas <anastasis.gramm2@gmail.com> | 2018-04-22 21:30:31 +0300 |
---|---|---|
committer | gramanas <anastasis.gramm2@gmail.com> | 2018-04-22 21:30:31 +0300 |
commit | 0a5257ea63cce0cebc93eb71e51dbbb3eb14a9a3 (patch) | |
tree | 6cdc4ddeec5727ebb0dd112ef01f8661e2447c0e /src | |
parent | 225600aadffc6fdb188fef230cdab67e1242e3a3 (diff) | |
download | ck-0a5257ea63cce0cebc93eb71e51dbbb3eb14a9a3.tar.gz ck-0a5257ea63cce0cebc93eb71e51dbbb3eb14a9a3.tar.bz2 ck-0a5257ea63cce0cebc93eb71e51dbbb3eb14a9a3.zip |
Add read, the actual linking remains
Diffstat (limited to 'src')
-rw-r--r-- | src/actionparser.h | 1 | ||||
-rw-r--r-- | src/actions.c | 93 | ||||
-rw-r--r-- | src/actions.h | 15 | ||||
-rw-r--r-- | src/ckutil.h | 3 | ||||
-rw-r--r-- | src/dbhelper.c | 68 | ||||
-rw-r--r-- | src/dbhelper.h | 11 | ||||
-rw-r--r-- | src/dblayer.c | 68 | ||||
-rw-r--r-- | src/dblayer.h | 3 |
8 files changed, 226 insertions, 36 deletions
diff --git a/src/actionparser.h b/src/actionparser.h index be7cf90..3defde8 100644 --- a/src/actionparser.h +++ b/src/actionparser.h @@ -66,7 +66,6 @@ struct UserOptions { char *argv[10]; // action's options }; - extern ParseResult parseAction(int argc, char* argv[], UserOpt *opt); extern void printParserError(); extern void printParserHelp(); diff --git a/src/actions.c b/src/actions.c index 4e9e4ca..77680eb 100644 --- a/src/actions.c +++ b/src/actions.c @@ -8,16 +8,14 @@ * GPLv3 (see LICENCE for the full notice) * * -------------------------------------------------------------------------- */ -#include <stdio.h> - #include "actions.h" #include "dblayer.h" - +#include "ckutil.h" int run_INIT(UserOpt * opt, Conf *conf) { if (db_exists(opt)) { - printf("conf dir: %s\n", opt->confDir); - printf("ck is already initialized.\n"); + printf("Current configuration file location: %s\n", opt->confDir); + PRINT_ERR("ck is already initialized.\n"); return 0; } if (init_create_config_file(opt)) { @@ -31,21 +29,89 @@ int run_INIT(UserOpt * opt, Conf *conf) { return 1; } +AddOpt make_add_options(const int argc, char **argv) { + AddOpt addOpt = { + .progName = argv[0], + .confPath = NULL, + .secret = 0, + .prime = 0, + .err = ADD_NO_ERR + }; + + /* the first two argumens have to exist since we are here */ + if (!util_is_file_rw(argv[1])) { + addOpt.err = ADD_ERR_WRONG_CONFIG; + return addOpt; + } + addOpt.confPath = argv[1]; + + if (argc == 3) { + if (strcmp(argv[2], "-s") == 0) { + addOpt.secret = 1; + } else if (strcmp(argv[2], "-p") == 0) { + addOpt.prime = 1; + } else { + addOpt.err = ADD_ERR_WRONG_FLAGS; + return addOpt; + } + } else if (argc == 4) { + if (strcmp(argv[2], "-s") == 0) { + addOpt.secret = 1; + } else if (strcmp(argv[2], "-p") == 0) { + addOpt.prime = 1; + } else { + addOpt.err = ADD_ERR_WRONG_FLAGS; + return addOpt; + } + if (strcmp(argv[3], "-s") == 0) { + addOpt.secret = 1; + } else if (strcmp(argv[3], "-p") == 0) { + addOpt.prime = 1; + } else { + addOpt.err = ADD_ERR_WRONG_FLAGS; + return addOpt; + } + } + return addOpt; +} + +void add_print_opts(AddOpt *opt) { + printf("Program:\t%s\nConfig:\t\t%s\n", opt->progName, opt->confPath); + if (opt->prime && opt->secret) { + printf("Options:\tsecret, primary\n"); + } else if (opt->prime) { + printf("Options:\tprimary\n"); + } else if (opt->secret) { + printf("Options:\tsecret\n"); + } +} + int run_ADD(UserOpt * opt, Conf *conf) { - printf("Running %s\n", "add"); DB db = open_DB(opt); if (db.ptr == NULL) { if (db.error == SQL_ERR_NO_TABLES) { - printf("no tables\n"); + PRINT_ERR("The database file is currupted. Run ck init anew.\n"); } } - for (int i = 0; i < opt->argc; i++) { - printf("[%d]: %s\n", i, opt->argv[i]); + AddOpt addOpt = make_add_options(opt->argc, opt->argv); + switch (addOpt.err) { + case ADD_NO_ERR: + break; + case ADD_ERR_WRONG_CONFIG: + PRINT_ERR("The config file specified doesn't exist.\n"); + close_DB(&db); + return 0; + case ADD_ERR_WRONG_FLAGS: + PRINT_ERR("Flags are: -s for secret and -p for primary.\n"); + close_DB(&db); + return 0; } - // figure out user opt - if (add_transaction_begin(&db, opt->argv[0], opt->argv[1], 0, 0) == 0) { + add_print_opts(&addOpt); + if (add_transaction_begin(&db, addOpt.progName, + addOpt.confPath, addOpt.secret, addOpt.prime) == 0) { return 0; } + // do the linking close_DB(&db); return 1; } @@ -88,16 +154,15 @@ int run_HELP(UserOpt * opt, Conf *conf) { void print_INIT_result(int ok) { if (ok) { printf("Initialized empty ckdb.\n"); - return; } } void print_ADD_result(int ok) { if (ok) { - printf("succes\n"); + printf("ckdb updated succesfully.\n"); return; } - printf("failure\n"); + printf("Could not complete add transaction.\n"); } void print_DEL_result(int ok) { diff --git a/src/actions.h b/src/actions.h index e7e5636..20677dd 100644 --- a/src/actions.h +++ b/src/actions.h @@ -28,4 +28,19 @@ CK_ACTIONS CK_ACTIONS #undef X +typedef enum AddOptErrors AddOptErr; +enum AddOptErrors { + ADD_NO_ERR = 0, + ADD_ERR_WRONG_CONFIG, + ADD_ERR_WRONG_FLAGS +}; + +typedef struct AddOptions AddOpt; +struct AddOptions { + char *progName; + char *confPath; + int secret; + int prime; + AddOptErr err; +}; #endif /* ACTIONS_H */ diff --git a/src/ckutil.h b/src/ckutil.h index eab7808..4c873d6 100644 --- a/src/ckutil.h +++ b/src/ckutil.h @@ -23,6 +23,9 @@ #include <stdlib.h> #include <string.h> +#define PRINT_ERR(ERROR) \ + printf("--[ Error ]--\n%s", ERROR); + /********************/ /* global constants */ /********************/ diff --git a/src/dbhelper.c b/src/dbhelper.c index 2be76ed..66bae7d 100644 --- a/src/dbhelper.c +++ b/src/dbhelper.c @@ -1,3 +1,13 @@ +/* dbhelper.c - Database layer for ck -----------------------------------*- C -*- + * + * This file is part of ck, the config keeper + * + * ----------------------------------------------------------------------------- + * + * Copyright (C) 2018 Anastasis Grammenos + * GPLv3 (see LICENCE for the full notice) + * + * -------------------------------------------------------------------------- */ #include "dbhelper.h" void dbh_form_query_make_tables(char *query) { @@ -18,7 +28,7 @@ void dbh_form_query_make_tables(char *query) { strcat(tmp, " TEXT NOT NULL, "); strcat(tmp, COL_CONFIG_SECRET); strcat(tmp, " INT NOT NULL, "); - strcat(tmp, COL_CONFIG_PRIME); + strcat(tmp, COL_CONFIG_PRIMARY); strcat(tmp, " INT NOT NULL); "); strcat(tmp, "CREATE TABLE "); @@ -103,3 +113,59 @@ void dhb_form_query_find_relationship(char *query) { strcpy(query, tmp); } + +void dbh_format_query_select_from_joined_eq(char *query, const char *selection, const char* condition) { + char tmp[STR_L] = "SELECT "; + strcat(tmp, selection); + strcat(tmp, " FROM "); + strcat(tmp, TBL_REL); + strcat(tmp, " JOIN "); + strcat(tmp, TBL_PROGRAM); + strcat(tmp, " ON "); + strcat(tmp, TBL_PROGRAM); + strcat(tmp, "."); + strcat(tmp, COL_PROGRAM_ID); + strcat(tmp, " = "); + strcat(tmp, COL_REL_PROGRAM_ID); + strcat(tmp, " JOIN "); + strcat(tmp, TBL_CONFIG); + strcat(tmp, " ON "); + strcat(tmp, TBL_CONFIG); + strcat(tmp, "."); + strcat(tmp, COL_CONFIG_ID); + strcat(tmp, " = "); + strcat(tmp, COL_REL_CONFIG_ID); + strcat(tmp, " WHERE "); + strcat(tmp, condition); + strcat(tmp, " = ?;"); + + strcpy(query, tmp); +} + +void dbh_format_query_select_from_joined_like(char *query, const char *selection, const char* condition) { + char tmp[STR_L] = "SELECT "; + strcat(tmp, selection); + strcat(tmp, " FROM "); + strcat(tmp, TBL_REL); + strcat(tmp, " JOIN "); + strcat(tmp, TBL_PROGRAM); + strcat(tmp, " ON "); + strcat(tmp, TBL_PROGRAM); + strcat(tmp, "."); + strcat(tmp, COL_PROGRAM_ID); + strcat(tmp, " = "); + strcat(tmp, COL_REL_PROGRAM_ID); + strcat(tmp, " JOIN "); + strcat(tmp, TBL_CONFIG); + strcat(tmp, " ON "); + strcat(tmp, TBL_CONFIG); + strcat(tmp, "."); + strcat(tmp, COL_CONFIG_ID); + strcat(tmp, " = "); + strcat(tmp, COL_REL_CONFIG_ID); + strcat(tmp, " WHERE "); + strcat(tmp, condition); + strcat(tmp, " LIKE '%' || ? || '%';"); + + strcpy(query, tmp); +} diff --git a/src/dbhelper.h b/src/dbhelper.h index 7385b16..df14ebf 100644 --- a/src/dbhelper.h +++ b/src/dbhelper.h @@ -9,7 +9,7 @@ * * ----------------------------------------------------------------------------- * - * Give access to the database. + * Help other db functions. * * -------------------------------------------------------------------------- */ #ifndef DBHELPER_H @@ -33,7 +33,7 @@ #define COL_CONFIG_ID "ID" #define COL_CONFIG_PATH "PATH" #define COL_CONFIG_SECRET "SECRET" -#define COL_CONFIG_PRIME "PRIME" +#define COL_CONFIG_PRIMARY "PRIME" #define COL_REL_PROGRAM_ID "PID" #define COL_REL_CONFIG_ID "CID" @@ -43,6 +43,10 @@ #define __END_TRANSACTION__ \ sqlite3_exec(db->ptr, "END TRANSACTION;", NULL, NULL, NULL); +/****************/ +/* form queries */ +/****************/ + extern void dbh_form_query_make_tables(char *query); extern void dbh_form_query_insert_program(char *query); extern void dbh_form_query_insert_config(char *query); @@ -52,5 +56,6 @@ extern void dhb_form_query_insert_relationship(char *query); extern void dhb_form_query_find_program(char *query); extern void dhb_form_query_find_config(char *query); extern void dhb_form_query_find_relationship(char *query); - +extern void dbh_format_query_select_from_joined_eq(char *query, const char *selection, const char* condition); +extern void dbh_format_query_select_from_joined_like(char *query, const char *selection, const char* condition); #endif /* DBHELPER_H */ diff --git a/src/dblayer.c b/src/dblayer.c index b55a760..b2be023 100644 --- a/src/dblayer.c +++ b/src/dblayer.c @@ -115,11 +115,9 @@ void init_make_tables(DB *db) { char sql[STR_L]; dbh_form_query_make_tables(sql); - char *err_msg = NULL; - int rc = sqlite3_exec(db->ptr, sql, 0, 0, &err_msg); + int rc = sqlite3_exec(db->ptr, sql, 0, 0, 0); if (rc != SQLITE_OK ) { - printf("SQL error: %s\n", err_msg); - sqlite3_free(err_msg); + PRINT_ERR("Could not create empry db."); db->error = SQL_ERR_SQLITE; return; } @@ -159,7 +157,7 @@ int insert_to_program_table(DB *db, const char *name) { rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); if (rc != SQLITE_OK) { - printf("Error while preparing insert to program sql.\n"); + PRINT_ERR("while preparing insert to program sql.\n"); db->error = SQL_ERR_SQLITE; return -1; } @@ -171,7 +169,7 @@ int insert_to_program_table(DB *db, const char *name) { sqlite3_bind_int(stmt, 1, id); sqlite3_bind_text(stmt, 2, name, strlen(name), 0); if (sqlite3_step(stmt) != SQLITE_DONE) { - printf("Error while excecuting insert to program sql.\n"); + PRINT_ERR("while excecuting insert to program sql.\n"); db->error = SQL_ERR_SQLITE; return -1; } @@ -187,7 +185,7 @@ int insert_to_config_table(DB *db, const char *path, const int secret, const int dbh_form_query_insert_config(sql); rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); if (rc != SQLITE_OK) { - printf("Error while preparing insert to config sql.\n"); + PRINT_ERR("Error while preparing insert to config sql.\n"); db->error = SQL_ERR_SQLITE; return -1; } @@ -201,7 +199,7 @@ int insert_to_config_table(DB *db, const char *path, const int secret, const int sqlite3_bind_int(stmt, 3, secret); sqlite3_bind_int(stmt, 4, prime); if (sqlite3_step(stmt) != SQLITE_DONE) { - printf("Error while excecuting insert to config sql.\n"); + PRINT_ERR("Error while excecuting insert to config sql.\n"); db->error = SQL_ERR_SQLITE; return-1; } @@ -218,14 +216,14 @@ int insert_to_rel_table(DB *db, const int pid, const int cid) { rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); if (rc != SQLITE_OK) { db->error = SQL_ERR_SQLITE; - printf("Error while preparing insert to rel sql.\n"); + PRINT_ERR("while preparing insert to rel sql.\n"); return -1; } sqlite3_bind_int(stmt, 1, pid); sqlite3_bind_int(stmt, 2, cid); if (sqlite3_step(stmt) != SQLITE_DONE) { db->error = SQL_ERR_SQLITE; - printf("Error while excecuting insert to rel sql.\n"); + PRINT_ERR("while excecuting insert to rel sql.\n"); return-1; } sqlite3_finalize(stmt); @@ -241,7 +239,7 @@ int program_exists(DB *db, const char* name) { rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); if (rc != SQLITE_OK) { - printf("Error while preparing program_exists sql.\n"); + PRINT_ERR("Error while preparing program_exists sql.\n"); return -2; } sqlite3_bind_text(stmt, 1, name, strlen(name), 0); @@ -263,7 +261,7 @@ int config_exists(DB *db, const int pid, const char* path) { rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); if (rc != SQLITE_OK) { - printf("Error while preparing config_exists sql.\n"); + PRINT_ERR("while preparing config_exists sql.\n"); return -2; } sqlite3_bind_text(stmt, 1, path, strlen(path), 0); @@ -281,6 +279,35 @@ int add_insert_relationship(DB *db, const int pid, const int cid) { return insert_to_rel_table(db, pid, cid); } +int program_has_primary_config(DB *db, const int pid) { + sqlite3_stmt *stmt; + int rc; + + char sql[STR_L]; + + char condition[STR_S] = TBL_PROGRAM; + strcat(condition, "."); + strcat(condition, COL_PROGRAM_ID); + + dbh_format_query_select_from_joined_eq(sql, COL_CONFIG_PRIMARY, condition); + + rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); + if (rc != SQLITE_OK) { + PRINT_ERR("while preparing program_has_primary_exists sql.\n"); + return -2; + } + sqlite3_bind_int(stmt, 1, pid); + int count = 0; + while (sqlite3_step(stmt) == SQLITE_ROW) { + count += sqlite3_column_int(stmt, 0); + } + sqlite3_finalize(stmt); + if (count == 0) { + return 0; + } + return 1; +} + 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, pid, path); if (cid == -2) { @@ -289,6 +316,10 @@ int add_get_or_insert_config_to_db(DB *db, const int pid, const char *path, cons } /* If config doesnt exist insert it and return it's cid */ if (cid == -1) { + if (program_has_primary_config(db, pid) && prime) { + db->error = SQL_ERR_PRIMARY_REDEFINITION; + return -1; + } return insert_to_config_table(db, path, secret, prime); } @@ -315,24 +346,29 @@ int add_transaction_begin(DB *db, const char *progName, __BEGIN_TRANSACTION__ int pid = add_get_or_insert_program_to_db(db, progName); if (db->error == SQL_ERR_SQLITE) { - printf("Could not insert program to db.\n"); + PRINT_ERR("Could not insert program to db.\n"); close_DB(db); return 0; } int cid = add_get_or_insert_config_to_db(db, pid, confPath, secret, prime); if (db->error == SQL_ERR_SQLITE) { - printf("Could not insert config to db.\n"); + PRINT_ERR("Could not insert config to db.\n"); close_DB(db); return 0; } else if (db->error == SQL_CONFIG_PATH_EXISTS) { - printf("This config already exists in the database.\n"); + PRINT_ERR("This config already exists in the database.\n"); + close_DB(db); + return 0; + } + else if (db->error == SQL_ERR_PRIMARY_REDEFINITION) { + PRINT_ERR("This program already has a primary config.\n"); close_DB(db); return 0; } add_insert_relationship(db, pid, cid); if (db->error == SQL_ERR_SQLITE) { - printf("Could not insert config to db.\n"); + PRINT_ERR("rel update failed\n"); close_DB(db); return 0; } diff --git a/src/dblayer.h b/src/dblayer.h index abeef83..40b7dfb 100644 --- a/src/dblayer.h +++ b/src/dblayer.h @@ -25,7 +25,8 @@ enum SqlErrors { SQL_ERR_NO_DB_FILE, SQL_ERR_NO_TABLES, SQL_ERR_SQLITE, - SQL_CONFIG_PATH_EXISTS + SQL_CONFIG_PATH_EXISTS, + SQL_ERR_PRIMARY_REDEFINITION }; typedef struct DBstruct DB; |