aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2018-11-16 00:04:51 +0200
committergramanas <anastasis.gramm2@gmail.com>2018-11-16 00:04:51 +0200
commit97e14c73be6684259e235a92cc575ea39a04fc7e (patch)
tree6aa652453a118b9863687ba80b5c3c3c425ed1cd
parent967ad7898243e083f485a03414fce0018bfce881 (diff)
downloadck-97e14c73be6684259e235a92cc575ea39a04fc7e.tar.gz
ck-97e14c73be6684259e235a92cc575ea39a04fc7e.tar.bz2
ck-97e14c73be6684259e235a92cc575ea39a04fc7e.zip
[ckdb] simplify open_DB and make_DB
-rw-r--r--src/actions.c47
-rw-r--r--src/dblayer.c44
-rw-r--r--src/dblayer.h6
3 files changed, 41 insertions, 56 deletions
diff --git a/src/actions.c b/src/actions.c
index 4620c0d..882fa30 100644
--- a/src/actions.c
+++ b/src/actions.c
@@ -27,8 +27,8 @@ int run_INIT(UserOpt * opt, Conf *conf) {
sERR("Cound not create config file.");
return -1;
}
- DB db = init_make_DB(opt);
- if (db.error == SQL_NO_ERR) {
+ DB db;
+ if (!init_make_DB(&db, opt)) {
init_make_tables(&db);
}
sqlite3_close(db.ptr);
@@ -36,12 +36,9 @@ int run_INIT(UserOpt * opt, Conf *conf) {
}
int run_ADD(UserOpt * opt, Conf *conf) {
- DB db = open_DB(opt);
- if (db.ptr == NULL) {
- if (db.error == SQL_ERR_NO_TABLES) {
- ERR("The database file is currupted. Run ck init anew.");
- }
- goto error;
+ DB db;
+ if (open_DB(&db, opt)) {
+ return -1;
}
AddOpt addOpt = add_make_options(opt->args);
switch (addOpt.err) {
@@ -74,11 +71,7 @@ int run_ADD(UserOpt * opt, Conf *conf) {
int run_DEL(UserOpt * opt, Conf *conf) {
UNUSED(conf);
DB db;
- db = open_DB(opt);
- if (db.ptr == NULL) {
- if (db.error == SQL_ERR_NO_TABLES) {
- ERR("The database file is currupted. Run ck init anew.");
- }
+ if (open_DB(&db, opt)) {
return -1;
}
@@ -109,11 +102,8 @@ int run_DEL(UserOpt * opt, Conf *conf) {
}
int run_EDIT(UserOpt *opt, Conf *conf) {
- DB db = open_DB(opt);
- if (db.ptr == NULL) {
- if (db.error == SQL_ERR_NO_TABLES) {
- ERR("The database file is currupted. Run ck init anew.");
- }
+ DB db;
+ if (open_DB(&db, opt)) {
return -1;
}
@@ -183,11 +173,8 @@ int run_EDIT(UserOpt *opt, Conf *conf) {
}
int run_LIST(UserOpt *opt, Conf *conf) {
- DB db = open_DB(opt);
- if (db.ptr == NULL) {
- if (db.error == SQL_ERR_NO_TABLES) {
- ERR("The database file is currupted. Run ck init anew.");
- }
+ DB db;
+ if (open_DB(&db, opt)) {
return -1;
}
@@ -257,11 +244,8 @@ int run_SEARCH(UserOpt *opt, Conf *conf) {
ERR("No grep avaliable. Please make sure you have grep installed.");
return -1;
}
- DB db = open_DB(opt);
- if (db.ptr == NULL) {
- if (db.error == SQL_ERR_NO_TABLES) {
- ERR("The database file is currupted. Run ck init anew.");
- }
+ DB db;
+ if (open_DB(&db, opt)) {
return -1;
}
@@ -319,11 +303,8 @@ int run_HELP(UserOpt *opt, Conf *conf) {
}
int run_RESTORE(UserOpt *opt, Conf *conf) {
- DB db = open_DB(opt);
- if (db.ptr == NULL) {
- if (db.error == SQL_ERR_NO_TABLES) {
- ERR("The database file is currupted. Run ck init anew.");
- }
+ DB db;
+ if (open_DB(&db, opt)) {
return -1;
}
cklist *from = list_make_new();
diff --git a/src/dblayer.c b/src/dblayer.c
index 97a93db..a6e6193 100644
--- a/src/dblayer.c
+++ b/src/dblayer.c
@@ -79,37 +79,41 @@ void close_DB(DB *db) {
sqlite3_close(db->ptr);
}
-DB init_make_DB(const UserOpt *opt) {
- sqlite3 *db;
- char db_path[STR_L] = "";
+int open_DB(DB *db, const UserOpt *opt) {
+ if (!db || !opt) {
+ return -1;
+ }
int rc;
+ char db_path[STR_L] = "";
make_db_name(db_path, opt->confDir);
- rc = sqlite3_open(db_path, &db);
- if (rc != SQLITE_OK) {
- return empty_DB(SQL_ERR_NO_DB_FILE);
+ rc = sqlite3_open(db_path, &db->ptr);
+
+ if (rc) {
+ ERR("%s is not a path to an sqlite3 database.", db_path);
+ return -1;
+ }
+
+ if (check_initialized_DB(db->ptr)) {
+ ERR("The database file is currupted. Run ck init anew.");
+ return -1;
}
- return new_DB(db);
+ LOG("Opened %s", db_path);
+ return 0;
}
-DB open_DB(const UserOpt *opt) {
- sqlite3 *db;
- int rc;
+int init_make_DB(DB *db, const UserOpt *opt) {
char db_path[STR_L] = "";
+ int rc;
- make_db_name(db_path, opt->confDir);
- rc = sqlite3_open(db_path, &db);
-
- if (rc) {
- return empty_DB(SQL_ERR_NO_DB_FILE);
- }
-
- if (check_initialized_DB(db)) {
- return empty_DB(SQL_ERR_NO_TABLES);
+ make_db_name(db_path, opt->confDir);
+ rc = sqlite3_open(db_path, &db->ptr);
+ if (rc != SQLITE_OK) {
+ return -1;
}
- return new_DB(db);
+ return 0;
}
void init_make_tables(DB *db) {
diff --git a/src/dblayer.h b/src/dblayer.h
index 0d012bb..2e42ad0 100644
--- a/src/dblayer.h
+++ b/src/dblayer.h
@@ -37,9 +37,9 @@ struct DBstruct {
int db_exists(const UserOpt *opt);
-/* Open the db file. On fail return null pointer to db
+/* Open the db file. On fail return null pointer to db->ptr
* and the corresponding SQL error (NO_DB_FILE | NO_TABLES)*/
-DB open_DB(const UserOpt *opt);
+int open_DB(DB *db, const UserOpt *opt);
void close_DB(DB *db);
int program_exists(DB *db, const char *pName);
@@ -51,7 +51,7 @@ int get_program_paths(DB *db, cklist *ckl, const char* pName, int bName, int att
/* Create the tables required for the ckdb */
void init_make_tables(DB *db);
-DB init_make_DB(const UserOpt *opt);
+int init_make_DB(DB *db, const UserOpt *opt);
/*******/
/* add */