aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2018-04-22 18:07:50 +0300
committergramanas <anastasis.gramm2@gmail.com>2018-04-22 18:07:50 +0300
commit225600aadffc6fdb188fef230cdab67e1242e3a3 (patch)
tree026d4017b40906a0aa3b2044cf6b52049a053a1c
parenteeca7f0151d85d563d1db229e3b7ca936323ffa2 (diff)
downloadck-225600aadffc6fdb188fef230cdab67e1242e3a3.tar.gz
ck-225600aadffc6fdb188fef230cdab67e1242e3a3.tar.bz2
ck-225600aadffc6fdb188fef230cdab67e1242e3a3.zip
Add almost done, opt parsing remains
-rw-r--r--CMakeLists.txt2
-rw-r--r--src/actionparser.c30
-rw-r--r--src/actions.c17
-rw-r--r--src/ckutil.h29
-rw-r--r--src/confparser.c31
-rw-r--r--src/dbhelper.c105
-rw-r--r--src/dbhelper.h56
-rw-r--r--src/dblayer.c227
-rw-r--r--src/dblayer.h9
9 files changed, 427 insertions, 79 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c797532..b0eeb7e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -24,6 +24,7 @@ set(ckLib_src
${SRC_DIR}/actions.c
${SRC_DIR}/confparser.c
${SRC_DIR}/dblayer.c
+ ${SRC_DIR}/dbhelper.c
${SRC_DIR}/ckutil.c
)
set(ckLib_hdr
@@ -31,6 +32,7 @@ set(ckLib_hdr
${SRC_DIR}/actions.h
${SRC_DIR}/confparser.h
${SRC_DIR}/dblayer.h
+ ${SRC_DIR}/dbhelper.h
${SRC_DIR}/ckutil.h
)
diff --git a/src/actionparser.c b/src/actionparser.c
index 811f0c4..ac01b84 100644
--- a/src/actionparser.c
+++ b/src/actionparser.c
@@ -13,11 +13,6 @@
* and report any errors that might come up.
*
* -------------------------------------------------------------------------- */
-#define _GNU_SOURCE
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
#include "actionparser.h"
#include "ckutil.h"
@@ -243,7 +238,7 @@ void getPossibleActionNames(char * dest, CkAction ckAction) {
return;
}
- char buf[30];
+ char buf[STR_S];
switch (ckAction) {
#define X(ACTION) \
@@ -266,44 +261,43 @@ void getPossibleActionNames(char * dest, CkAction ckAction) {
}
void printParserError(UserOpt *opt) {
- char *errStr = NULL;
- char names[30];
+ char errStr[STR_M];
+ char names[STR_S];
getPossibleActionNames(names, opt->action);
switch (opt->err) {
case PERR_NOERR:
return;
case PERR_UNKONW_ACTION:
- asprintf(&errStr, "Unknown action: %s", token);
+ sprintf(errStr, "Unknown action: %s", token);
break;
case PERR_INIT_WRONG:
- asprintf(&errStr, "Initialize database\nUsage: %s version_control_dir secret_dir", names);
+ sprintf(errStr, "Initialize database\nUsage: %s version_control_dir secret_dir", names);
break;
case PERR_ADD_WRONG:
- asprintf(&errStr, "Add config (new or existing)\nUsage: %s ProgramName ConfigPath [-s](secret) [-p](primary)", names);
+ sprintf(errStr, "Add config (new or existing)\nUsage: %s ProgramName ConfigPath [-s](secret) [-p](primary)", names);
break;
case PERR_DEL_WRONG:
- asprintf(&errStr, "Delete config or program\nUsage: %s ProgramName ConfigPath [-s](secret) [-p](primary)", names);
+ sprintf(errStr, "Delete config or program\nUsage: %s ProgramName ConfigPath [-s](secret) [-p](primary)", names);
break;
case PERR_EDIT_WRONG:
- asprintf(&errStr, "Edit config\nUsage: add ProgramName ConfigPath [-s](secret) [-p](primary)");
+ sprintf(errStr, "Edit config\nUsage: add ProgramName ConfigPath [-s](secret) [-p](primary)");
break;
case PERR_LIST_WRONG:
- asprintf(&errStr, "Usage: add ProgramName ConfigPath [-s](secret) [-p](primary)");
+ sprintf(errStr, "Usage: .........)");
break;
case PERR_SEARCH_WRONG:
- asprintf(&errStr, "Usage: add ProgramName ConfigPath [-s](secret) [-p](primary)");
+ sprintf(errStr, "Usage: .............");
break;
case PERR_HELP_WRONG:
- asprintf(&errStr, "Usage: add ProgramName ConfigPath [-s](secret) [-p](primary)");
+ sprintf(errStr, "Usage: ........");
break;
}
printf("Parsing error\n%s\n", errStr);
- free(errStr);
}
void printParserHelp() {
- char names[30];
+ char names[STR_S];
printf("ck - the config keeper\n");
printf("Usage:\n");
getPossibleActionNames(names, CKA_INIT);
diff --git a/src/actions.c b/src/actions.c
index 87c0a6c..4e9e4ca 100644
--- a/src/actions.c
+++ b/src/actions.c
@@ -42,9 +42,12 @@ int run_ADD(UserOpt * opt, Conf *conf) {
for (int i = 0; i < opt->argc; i++) {
printf("[%d]: %s\n", i, opt->argv[i]);
}
- add_insert_program_to_db(&db, opt->argv[1]);
+ // figure out user opt
+ if (add_transaction_begin(&db, opt->argv[0], opt->argv[1], 0, 0) == 0) {
+ return 0;
+ }
close_DB(&db);
- return 0;
+ return 1;
}
int run_DEL(UserOpt * opt, Conf *conf) {
@@ -59,6 +62,16 @@ int run_EDIT(UserOpt * opt, Conf *conf) {
int run_LIST(UserOpt * opt, Conf *conf) {
printf("Running %s\n", "list");
+ DB db = open_DB(opt);
+ if (db.ptr == NULL) {
+ if (db.error == SQL_ERR_NO_TABLES) {
+ printf("no tables\n");
+ }
+ }
+ for (int i = 0; i < opt->argc; i++) {
+ printf("[%d]: %s\n", i, opt->argv[i]);
+ }
+ close_DB(&db);
return 0;
}
diff --git a/src/ckutil.h b/src/ckutil.h
index c99798d..eab7808 100644
--- a/src/ckutil.h
+++ b/src/ckutil.h
@@ -7,10 +7,39 @@
* Copyright (C) 2018 Anastasis Grammenos
* GPLv3 (see LICENCE for the full notice)
*
+ * -----------------------------------------------------------------------------
+ *
+ * Utility funtions and global constants.
+ *
* -------------------------------------------------------------------------- */
#ifndef CKUTIL_H
#define CKUTIL_H
+/*******************/
+/* global includes */
+/*******************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/********************/
+/* global constants */
+/********************/
+
+/* String length - Small */
+#define STR_S 30
+
+/* String length -Medium */
+#define STR_M 100
+
+/* String length - Large */
+#define STR_L 400
+
+/*********************/
+/* utility functions */
+/*********************/
+
/* Returns 1 if path is a directory, else returns 0. */
extern int util_is_dir(const char *path);
diff --git a/src/confparser.c b/src/confparser.c
index ef1de61..0f193e0 100644
--- a/src/confparser.c
+++ b/src/confparser.c
@@ -34,8 +34,8 @@ int remove_newline(char buff[]) {
}
int read_next_line(char *line, FILE *f) {
- char nextLine[200];
- if (fgets(nextLine, 200, f) == NULL) {
+ char nextLine[STR_L];
+ if (fgets(nextLine, STR_L, f) == NULL) {
return -1;
}
strcpy(line, nextLine);
@@ -55,29 +55,27 @@ ConfVar match_variables(char *line, char matched[]) {
return -1;
}
-char *make_config_name(char * confPath) {
- char *db_path = strdup(confPath);
+void make_config_name(char * ret, const char *confPath) {
+ char tmp[STR_L];
+ strcpy(tmp, confPath);
+ strcat(tmp, CONFIG_NAME);
- db_path = realloc(db_path, strlen(confPath) + strlen(CONFIG_NAME)+1);
- strcat(db_path, CONFIG_NAME);
-
- return db_path;
+ strcpy(ret, tmp);
}
ConfigParserResult parse(Conf *conf, UserOpt *opt) {
conf_values_initialize(conf);
FILE *confPtr;
- char *confName = make_config_name(opt->confDir);
+ char confName[STR_L];
+ make_config_name(confName, opt->confDir);
if ((confPtr = fopen(confName, "r")) == NULL) {
- free(confName);
return CPR_NO_CONFIG_FILE;
}
- free(confName);
int flag = 1;
- char line[200];
- char matched[200];
+ char line[STR_L];
+ char matched[STR_L];
while (read_next_line(line, confPtr)) {
- if (strlen(line) > 200) {
+ if (strlen(line) > STR_L) {
return CPR_WRONG_CONFIG;
}
switch(match_variables(line, matched)) {
@@ -146,10 +144,10 @@ int init_create_config_file(UserOpt *opt) {
util_mkdir(opt->confDir);
}
- char *confName = make_config_name(opt->confDir);
+ char confName[STR_L];
+ make_config_name(confName, opt->confDir);
FILE *f;
if ((f = fopen(confName, "w")) == NULL) {
- free(confName);
return 1;
}
@@ -164,6 +162,5 @@ int init_create_config_file(UserOpt *opt) {
fputs(tmp, f);
fclose(f);
- free(confName);
return 0;
}
diff --git a/src/dbhelper.c b/src/dbhelper.c
new file mode 100644
index 0000000..2be76ed
--- /dev/null
+++ b/src/dbhelper.c
@@ -0,0 +1,105 @@
+#include "dbhelper.h"
+
+void dbh_form_query_make_tables(char *query) {
+ char tmp[STR_L] = "CREATE TABLE ";
+ strcat(tmp, TBL_PROGRAM);
+ strcat(tmp, "(");
+ strcat(tmp, COL_PROGRAM_ID);
+ strcat(tmp, " INT NOT NULL PRIMARY KEY, ");
+ strcat(tmp, COL_PROGRAM_NAME);
+ strcat(tmp, " TEXT NOT NULL); ");
+
+ strcat(tmp, "CREATE TABLE ");
+ strcat(tmp, TBL_CONFIG);
+ strcat(tmp, "(");
+ strcat(tmp, COL_CONFIG_ID);
+ strcat(tmp, " INT NOT NULL PRIMARY KEY, ");
+ strcat(tmp, COL_CONFIG_PATH);
+ strcat(tmp, " TEXT NOT NULL, ");
+ strcat(tmp, COL_CONFIG_SECRET);
+ strcat(tmp, " INT NOT NULL, ");
+ strcat(tmp, COL_CONFIG_PRIME);
+ strcat(tmp, " INT NOT NULL); ");
+
+ strcat(tmp, "CREATE TABLE ");
+ strcat(tmp, TBL_REL);
+ strcat(tmp, "(");
+ strcat(tmp, COL_REL_PROGRAM_ID);
+ strcat(tmp, " INT NOT NULL, ");
+ strcat(tmp, COL_REL_CONFIG_ID);
+ strcat(tmp, " INT NOT NULL);");
+
+ strcpy(query, tmp);
+}
+
+void dbh_form_query_insert_program(char *query) {
+ char tmp[STR_L] = "INSERT INTO ";
+ strcat(tmp, TBL_PROGRAM);
+ strcat(tmp, " VALUES(?, ?);");
+
+ strcpy(query, tmp);
+}
+
+void dbh_form_query_insert_config(char *query) {
+ char tmp[STR_L] = "INSERT INTO ";
+ strcat(tmp, TBL_CONFIG);
+ strcat(tmp, " VALUES(?, ?, ?, ?);");
+
+ strcpy(query, tmp);
+}
+
+void dbh_form_query_select_id_from(char *query, const char* tableName) {
+ char tmp[STR_M] = "SELECT ID FROM ";
+ strcat(tmp, tableName);
+ strcat(tmp, " ORDER BY ID;");
+
+ strcpy(query, tmp);
+}
+
+void dbh_form_query_select_all_tables(char *query) {
+ strcpy(query, "SELECT * FROM SQLITE_MASTER WHERE type='table';");
+}
+
+void dhb_form_query_insert_relationship(char *query) {
+ char tmp[STR_M] = "INSERT INTO ";
+ strcat(tmp, TBL_REL);
+ strcat(tmp, " VALUES(?, ?);");
+
+ strcpy(query, tmp);
+}
+
+void dhb_form_query_find_program(char *query) {
+ char tmp[STR_M] = "SELECT ID FROM ";
+ strcat(tmp, TBL_PROGRAM);
+ strcat(tmp, " WHERE ");
+ strcat(tmp, COL_PROGRAM_NAME);
+ strcat(tmp, " = ?;");
+
+ strcpy(query, tmp);
+}
+
+void dhb_form_query_find_config(char *query) {
+ char tmp[STR_M] = "SELECT ID FROM ";
+ strcat(tmp, TBL_CONFIG);
+ strcat(tmp, " WHERE ");
+ strcat(tmp, COL_CONFIG_PATH);
+ strcat(tmp, " = ?;");
+
+ strcpy(query, tmp);
+}
+
+void dhb_form_query_find_relationship(char *query) {
+ char tmp[STR_M] = "SELECT ";
+ strcat(tmp, COL_REL_PROGRAM_ID);
+ strcat(tmp, ", ");
+ strcat(tmp, COL_REL_CONFIG_ID);
+ strcat(tmp, " FROM ");
+ strcat(tmp, TBL_REL);
+ strcat(tmp, " WHERE ");
+ strcat(tmp, COL_REL_PROGRAM_ID);
+ strcat(tmp, " = ? AND ");
+ strcat(tmp, COL_REL_CONFIG_ID);
+ strcat(tmp, " = ?;");
+
+ strcpy(query, tmp);
+}
diff --git a/src/dbhelper.h b/src/dbhelper.h
new file mode 100644
index 0000000..7385b16
--- /dev/null
+++ b/src/dbhelper.h
@@ -0,0 +1,56 @@
+/* dbhelper.h - 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)
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Give access to the database.
+ *
+ * -------------------------------------------------------------------------- */
+#ifndef DBHELPER_H
+#define DBHELPER_H
+
+#include <sqlite3.h>
+
+#include "actionparser.h"
+#include "ckutil.h"
+
+/********************/
+/* sqlite constants */
+/********************/
+#define TBL_PROGRAM "PROGRAM"
+#define TBL_CONFIG "CONFIG"
+#define TBL_REL "REL"
+
+#define COL_PROGRAM_ID "ID"
+#define COL_PROGRAM_NAME "NAME"
+
+#define COL_CONFIG_ID "ID"
+#define COL_CONFIG_PATH "PATH"
+#define COL_CONFIG_SECRET "SECRET"
+#define COL_CONFIG_PRIME "PRIME"
+
+#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);
+
+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);
+extern void dbh_form_query_select_id_from(char *query, const char* tableName);
+extern void dbh_form_query_select_all_tables(char *query);
+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);
+
+#endif /* DBHELPER_H */
diff --git a/src/dblayer.c b/src/dblayer.c
index 98a4d64..b55a760 100644
--- a/src/dblayer.c
+++ b/src/dblayer.c
@@ -12,18 +12,15 @@
* Give access to the database.
*
* -------------------------------------------------------------------------- */
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
#include "dblayer.h"
+#include "dbhelper.h"
#include "ckutil.h"
const char * const DB_NAME = "/ckdb";
/* figure out the database name */
void make_db_name(char *ret, const char *confPath) {
- char db_path[200];
+ char db_path[STR_L];
strcpy(db_path, confPath);
strcat(db_path, DB_NAME);
@@ -32,14 +29,15 @@ void make_db_name(char *ret, const char *confPath) {
/* Check if the db file exists*/
int db_exists(const UserOpt *opt) {
- char db_path[200];
+ char db_path[STR_L];
make_db_name(db_path, opt->confDir);
return util_is_file_rw(db_path);
}
/* check if db has the correct tables */
int check_initialized_DB(sqlite3 *db) {
- char *sql = "SELECT * FROM SQLITE_MASTER WHERE type='table';";
+ char sql[STR_M];
+ dbh_form_query_select_all_tables(sql);
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL);
@@ -47,13 +45,13 @@ int check_initialized_DB(sqlite3 *db) {
int program_table_ok, config_table_ok, rel_table_ok = 0;
while (sqlite3_step(stmt) != SQLITE_DONE) {
const unsigned char *tmpbuf = sqlite3_column_text(stmt, 1);
- if (strcmp((char *)tmpbuf, "PROGRAM") == 0) {
+ if (strcmp((char *)tmpbuf, TBL_PROGRAM) == 0) {
program_table_ok = 1;
}
- if (strcmp((char *)tmpbuf, "CONFIG") == 0) {
+ if (strcmp((char *)tmpbuf, TBL_CONFIG) == 0) {
config_table_ok = 1;
}
- if (strcmp((char *)tmpbuf, "REL") == 0) {
+ if (strcmp((char *)tmpbuf, TBL_REL) == 0) {
rel_table_ok = 1;
}
}
@@ -82,7 +80,7 @@ void close_DB(DB *db) {
DB init_make_DB(const UserOpt *opt) {
sqlite3 *db;
- char db_path[200];
+ char db_path[STR_L];
int rc;
make_db_name(db_path, opt->confDir);
@@ -97,7 +95,7 @@ DB init_make_DB(const UserOpt *opt) {
DB open_DB(const UserOpt *opt) {
sqlite3 *db;
int rc;
- char db_path[200];
+ char db_path[STR_L];
make_db_name(db_path, opt->confDir);
rc = sqlite3_open(db_path, &db);
@@ -114,18 +112,8 @@ DB open_DB(const UserOpt *opt) {
}
void init_make_tables(DB *db) {
- char * sql =
- "CREATE TABLE REL("
- "pId INT NOT NULL,"
- "cId INT NOT NULL);"
- "CREATE TABLE PROGRAM("
- "id INT NOT NULL PRIMARY KEY,"
- "name TEXT NOT NULL);"
- "CREATE TABLE CONFIG("
- "id INT NOT NULL PRIMARY KEY,"
- "path TEXT NOT NULL,"
- "secret INT NOT NULL,"
- "prime INT NOT NULL);";
+ 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);
@@ -141,10 +129,9 @@ int get_next_valid_id_from_table(DB *db, const char* tableName) {
sqlite3_stmt *stmt;
int rc;
- char sql[100] = "SELECT id FROM ";
- strcat(sql, tableName);
- strcat(sql, " ORDER BY id;");
-
+ char sql[STR_M];
+ dbh_form_query_select_id_from(sql, tableName);
+
rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
if (rc != SQLITE_OK) {
return -1;
@@ -163,33 +150,193 @@ int get_next_valid_id_from_table(DB *db, const char* tableName) {
return id;
}
-void insert_to_program_table(DB *db, const char *name) {
+int insert_to_program_table(DB *db, const char *name) {
sqlite3_stmt *stmt;
int rc;
- char * sql =
- "INSERT INTO PROGRAM VALUES(?, ?);";
+ char sql[STR_L];
+ dbh_form_query_insert_program(sql);
rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
if (rc != SQLITE_OK) {
- printf("Error\n");
- return;
+ printf("Error while preparing insert to program sql.\n");
+ db->error = SQL_ERR_SQLITE;
+ return -1;
}
- int id = get_next_valid_id_from_table(db, "PROGRAM");
+ int id = get_next_valid_id_from_table(db, TBL_PROGRAM);
if (id == -1) {
db->error = SQL_ERR_SQLITE;
- return;
+ return -1;
}
sqlite3_bind_int(stmt, 1, id);
sqlite3_bind_text(stmt, 2, name, strlen(name), 0);
if (sqlite3_step(stmt) != SQLITE_DONE) {
- printf("Error\n");
- return;
+ printf("Error while excecuting insert to program sql.\n");
+ db->error = SQL_ERR_SQLITE;
+ return -1;
}
sqlite3_finalize(stmt);
+ return id;
}
-int add_insert_program_to_db(DB *db, const char *name) {
- insert_to_program_table(db,name);
- return 0;
+int insert_to_config_table(DB *db, const char *path, const int secret, const int prime) {
+ sqlite3_stmt *stmt;
+ int rc;
+
+ char sql[STR_L];
+ 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");
+ db->error = SQL_ERR_SQLITE;
+ return -1;
+ }
+ int id = get_next_valid_id_from_table(db, TBL_CONFIG);
+ if (id == -1) {
+ db->error = SQL_ERR_SQLITE;
+ return -1;
+ }
+ sqlite3_bind_int(stmt, 1, id);
+ sqlite3_bind_text(stmt, 2, path, strlen(path), 0);
+ 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");
+ db->error = SQL_ERR_SQLITE;
+ return-1;
+ }
+ sqlite3_finalize(stmt);
+ return id;
+}
+
+int insert_to_rel_table(DB *db, const int pid, const int cid) {
+ sqlite3_stmt *stmt;
+ int rc;
+
+ char sql[STR_M];
+ dhb_form_query_insert_relationship(sql);
+ 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");
+ 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");
+ return-1;
+ }
+ sqlite3_finalize(stmt);
+ return 1;
+}
+
+int program_exists(DB *db, const char* name) {
+ sqlite3_stmt *stmt;
+ int rc;
+
+ char sql[STR_M];
+ dhb_form_query_find_program(sql);
+
+ rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
+ if (rc != SQLITE_OK) {
+ printf("Error while preparing program_exists sql.\n");
+ return -2;
+ }
+ sqlite3_bind_text(stmt, 1, name, strlen(name), 0);
+ int id = -1;
+ while (sqlite3_step(stmt) == SQLITE_ROW) {
+ id = sqlite3_column_int(stmt, 0);
+ break;
+ }
+ sqlite3_finalize(stmt);
+ return id;
+}
+
+int config_exists(DB *db, const int pid, const char* path) {
+ sqlite3_stmt *stmt;
+ int rc;
+
+ char sql[STR_M];
+ dhb_form_query_find_config(sql);
+
+ rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
+ if (rc != SQLITE_OK) {
+ printf("Error while preparing config_exists sql.\n");
+ return -2;
+ }
+ sqlite3_bind_text(stmt, 1, path, strlen(path), 0);
+ int id = -1;
+ while (sqlite3_step(stmt) == SQLITE_ROW) {
+ id = sqlite3_column_int(stmt, 0);
+ break;
+ }
+ sqlite3_finalize(stmt);
+ return id;
+}
+
+int add_insert_relationship(DB *db, const int pid, const int cid) {
+ // do checks
+ return insert_to_rel_table(db, pid, cid);
+}
+
+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) {
+ db->error = SQL_ERR_SQLITE;
+ return -1;
+ }
+ /* If config doesnt exist insert it and return it's cid */
+ if (cid == -1) {
+ return insert_to_config_table(db, path, secret, prime);
+ }
+
+ /* If it exist it means the user has inserted the same path twice */
+ db->error = SQL_CONFIG_PATH_EXISTS;
+ return -1;
+}
+
+int add_get_or_insert_program_to_db(DB *db, const char *name) {
+ int pid = program_exists(db, name);
+ if (pid == -2) {
+ db->error = SQL_ERR_SQLITE;
+ return -1;
+ }
+ if (pid == -1) {
+ return insert_to_program_table(db,name);
+ }
+ return pid;
+}
+
+int add_transaction_begin(DB *db, const char *progName,
+ const char* confPath, const int secret,
+ const int prime) {
+ __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");
+ 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");
+ close_DB(db);
+ return 0;
+ }
+ else if (db->error == SQL_CONFIG_PATH_EXISTS) {
+ printf("This config already exists in the database.\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");
+ close_DB(db);
+ return 0;
+ }
+ __END_TRANSACTION__
+
+ return 1;
}
diff --git a/src/dblayer.h b/src/dblayer.h
index de73aac..abeef83 100644
--- a/src/dblayer.h
+++ b/src/dblayer.h
@@ -24,7 +24,8 @@ enum SqlErrors {
SQL_NO_ERR = 0,
SQL_ERR_NO_DB_FILE,
SQL_ERR_NO_TABLES,
- SQL_ERR_SQLITE
+ SQL_ERR_SQLITE,
+ SQL_CONFIG_PATH_EXISTS
};
typedef struct DBstruct DB;
@@ -53,5 +54,9 @@ extern DB init_make_DB(const UserOpt *opt);
/* add */
/*******/
+extern int add_transaction_begin(DB *db, const char *progName,
+ const char* confPath, const int secret,
+ const int prime);
extern int add_insert_program_to_db(DB *db, const char * name);
-#endif /* DBLAEYR_H */
+extern int add_insert_config_to_db(DB *db, const int pid, const char *path, const int secret, const int prime);
+#endif /* DBLAYER_H */