libmpm

morphux C package management library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/libmpm

commit 0827c555d6be539bc0c92be896ca5909e25299cd
parent 4d989d5e9295e27fdddcfd9fdd1633e33c61e395
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 30 Jan 2017 17:52:57 +0100

Add(Database): Package add functions:

With tests

Diffstat:
Minc/category.h | 2++
Minc/database.h | 10++++++----
Minc/package.h | 3+++
Asrc/category.c | 30++++++++++++++++++++++++++++++
Msrc/database.c | 64++++++++++++++++++++++++++++++++++++++++++++++++++--------------
Asrc/package.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtests/test_database.c | 109+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
7 files changed, 252 insertions(+), 20 deletions(-)

diff --git a/inc/category.h b/inc/category.h @@ -23,4 +23,6 @@ typedef struct s_category { char *name; } category_t; +int mpm_free_category(void *tmp); + #endif /* CATEGORY_H */ diff --git a/inc/database.h b/inc/database.h @@ -25,13 +25,14 @@ # define SQL_CALLBACK_DEF(name) int name(void *context, int col_num, char **col_txt, char **col_name) # define QUERY_GET_PACKAGE_BY_ID(id) "SELECT * FROM " PKG_TABLE " WHERE id = %lld", id -# define QUERY_GET_PACKAGE_BY_NAME(name) "SELECT * FROM " PKG_TABLE " WHERE name = %s", name +# define QUERY_GET_PACKAGE_BY_NAME(name) "SELECT * FROM " PKG_TABLE " WHERE name = \"%s\"", name # define SQL_CREATE_TABLE "CREATE table " -# define SQL_TYPE_PRIMARY_KEY " primary key " +# define SQL_INSERT_TABLE "INSERT INTO " +# define SQL_TYPE_PRIMARY_KEY " primary key " # define SQL_TYPE_NOT_NULL " not null " -# define SQL_TYPE_INT " integer " -# define SQL_TYPE_TEXT " text " +# define SQL_TYPE_INT " integer " +# define SQL_TYPE_TEXT " text " typedef struct s_database { sqlite3 *sql; @@ -48,5 +49,6 @@ u8_t mpm_get_package_by_name(database_t *ptr, const char *name, mlist_t **pkg); package_t *sql_to_package(package_t *ptr, char *name, char *val); u8_t mpm_database_init(database_t *ptr); +u8_t mpm_database_add_pkg(database_t *ptr, package_t *pkg); #endif /* DATABASE_H */ diff --git a/inc/package.h b/inc/package.h @@ -54,4 +54,7 @@ typedef struct s_package { mlist_t *docs; } package_t; +int mpm_package_free(void *tmp); +void mpm_package_init(package_t *ptr); + #endif /* PACKAGE_H */ diff --git a/src/category.c b/src/category.c @@ -0,0 +1,30 @@ +/*********************************** LICENSE **********************************\ +* Copyright 2017 Morphux * +* * +* Licensed under the Apache License, Version 2.0 (the "License"); * +* you may not use this file except in compliance with the License. * +* You may obtain a copy of the License at * +* * +* http://www.apache.org/licenses/LICENSE-2.0 * +* * +* Unless required by applicable law or agreed to in writing, software * +* distributed under the License is distributed on an "AS IS" BASIS, * +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * +* See the License for the specific language governing permissions and * +* limitations under the License. * +\******************************************************************************/ + +# include <category.h> + +/*! + * \brief Free a category + * \note This function can be used as a list_free callback + */ +int mpm_free_category(void *tmp) { + category_t *ptr = tmp; + + if (ptr) { + free(ptr->name); + } + return 1; +} diff --git a/src/database.c b/src/database.c @@ -97,19 +97,18 @@ u8_t mpm_database_exec(database_t *ptr, const char *query, * int name(void *context, int col_num, char **col_txt, char **col_name) */ SQL_CALLBACK_DEF(callback_package) { - mlist_t *head = context; + mlist_t **head = context; package_t *ptr; - if (col_num <= 0) - return 0; - ptr = malloc(sizeof(package_t)); assert(ptr != NULL); + mpm_package_init(ptr); for (u8_t i = 0; i < col_num; i++) sql_to_package(ptr, col_name[i], col_txt[i]); - list_add(head, ptr, sizeof(ptr)); + list_add(*(head), ptr, sizeof(package_t)); + free(ptr); return 0; } @@ -140,7 +139,7 @@ u8_t mpm_get_package_by_id(database_t *ptr, u64_t id, *pkg = NULL; asprintf(&query, QUERY_GET_PACKAGE_BY_ID(id)); - ret = sqlite3_exec(ptr->sql, query, &callback_package, *pkg, NULL); + ret = sqlite3_exec(ptr->sql, query, &callback_package, pkg, NULL); free(query); return ret; } @@ -172,7 +171,7 @@ u8_t mpm_get_package_by_name(database_t *ptr, const char *name, *pkg = NULL; asprintf(&query, QUERY_GET_PACKAGE_BY_NAME(name)); - ret = sqlite3_exec(ptr->sql, query, &callback_package, *pkg, NULL); + ret = sqlite3_exec(ptr->sql, query, &callback_package, pkg, NULL); free(query); return ret; } @@ -196,15 +195,15 @@ package_t *sql_to_package(package_t *ptr, char *name, char *val) { if (strcmp(name, PKG_COL_ID) == 0) { ptr->id = strtoull(val, (char **)NULL, 10); - } else if (strcmp(name, PKG_COL_NAME) == 0 || - strcmp(name, PKG_COL_VERSION) == 0 || - strcmp(name, PKG_COL_DESC) == 0) { + } else if (strcmp(name, PKG_COL_NAME) == 0) { + /*strcmp(name, PKG_COL_VERSION) == 0 ||*/ + /*strcmp(name, PKG_COL_DESC) == 0) {*/ ptr->name = strdup(val); } else if (strcmp(name, PKG_COL_STATE) == 0) { - ptr->id = val[0] - '0'; - } else { - m_panic("Unknown column '%s' in get_package\n", name); - } + ptr->state = val[0] - '0'; + } /*else {*/ + /*m_panic("Unknown column '%s' in get_package\n", name);*/ + /*}*/ return ptr; } @@ -227,6 +226,7 @@ u8_t mpm_database_init(database_t *ptr) { PKG_COL_NAME SQL_TYPE_TEXT SQL_TYPE_NOT_NULL "," \ PKG_COL_VERSION SQL_TYPE_TEXT SQL_TYPE_NOT_NULL "," \ PKG_COL_CATEG SQL_TYPE_TEXT SQL_TYPE_NOT_NULL "," \ + PKG_COL_DESC SQL_TYPE_TEXT SQL_TYPE_NOT_NULL "," \ PKG_COL_STATE SQL_TYPE_INT SQL_TYPE_NOT_NULL "," \ PKG_COL_DEPS SQL_TYPE_TEXT "," \ PKG_COL_FILES SQL_TYPE_TEXT SQL_TYPE_NOT_NULL "," \ @@ -254,3 +254,39 @@ error: sqlite3_free(err); return ret; } + +/*! + * \bried Add a package entry in the database + * \param ptr Opened connection to a database + * \param pkg Package to add + * + * This function will add a package entry to an already opened connection. + */ +u8_t mpm_database_add_pkg(database_t *ptr, package_t *pkg) { + char *query, *err; + char *deps, *files, *binaries, *config, *docs; + u8_t ret; + + if (pkg == NULL || ptr == NULL) + return 1; + + deps = NULL; + files = NULL; + binaries = NULL; + config = NULL; + docs = NULL; + asprintf(&query, SQL_INSERT_TABLE PKG_TABLE \ + " (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) " \ + "VALUES (\"%s\", \"%s\", \"%s\", \"%s\", %d, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\");", + PKG_COL_NAME, PKG_COL_VERSION, PKG_COL_CATEG, PKG_COL_DESC, + PKG_COL_STATE, PKG_COL_DEPS, PKG_COL_FILES, PKG_COL_BINARIES, + PKG_COL_CONFIG, PKG_COL_DOCS, + pkg->name, pkg->version, pkg->categ->name, pkg->desc, + pkg->state, deps, files, binaries, config, docs); + + ret = mpm_database_exec(ptr, query, NULL, NULL, &err); + free(query); + assert(ret == 0 && err == NULL); + sqlite3_free(err); + return ret; +} diff --git a/src/package.c b/src/package.c @@ -0,0 +1,54 @@ +/*********************************** LICENSE **********************************\ +* Copyright 2017 Morphux * +* * +* Licensed under the Apache License, Version 2.0 (the "License"); * +* you may not use this file except in compliance with the License. * +* You may obtain a copy of the License at * +* * +* http://www.apache.org/licenses/LICENSE-2.0 * +* * +* Unless required by applicable law or agreed to in writing, software * +* distributed under the License is distributed on an "AS IS" BASIS, * +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * +* See the License for the specific language governing permissions and * +* limitations under the License. * +\******************************************************************************/ + +# include <package.h> + +/*! + * \brief Free a package entry + * \note Can be used as list_free callback + */ +int mpm_package_free(void *tmp) { + package_t *ptr = tmp; + + if (ptr) { + free(ptr->name); + free(ptr->version); + free(ptr->desc); + mpm_free_category(ptr->categ); + free(ptr->categ); + /*list_free(ptr->deps, &package_free);*/ + /* TODO: Files free */ + } + return 1; +} + +/*! + * \brief Initialize a package entry + * \param ptr Pointer to an allocated ptr + */ +void mpm_package_init(package_t *ptr) { + if (ptr) { + ptr->name = NULL; + ptr->version = NULL; + ptr->categ = NULL; + ptr->desc = NULL; + ptr->deps = NULL; + ptr->files = NULL; + ptr->binaries = NULL; + ptr->config = NULL; + ptr->docs = NULL; + } +} diff --git a/tests/test_database.c b/tests/test_database.c @@ -127,7 +127,7 @@ TEST(database_init_test_pkg_table) { list_add(test, result->value, strlen(result->value)); } } - TEST_ASSERT((list_size(test) == 10), "Number of columns is wrong"); + TEST_ASSERT((list_size(test) == 11), "Number of columns is wrong"); list_free(test, NULL); free_sql_results(res); return TEST_SUCCESS; @@ -151,13 +151,111 @@ TEST(database_init_3) { TEST_ASSERT((ret != 0), "Error did not trigger"); } else { WAIT_AND_CLOSE(pid, st, fd); - TEST_ASSERT((WEXITSTATUS(st) == 0), "Exit status is wrong"); } mpm_database_close(ptr); clean_db(DB_FN); return TEST_SUCCESS; } +TEST(database_add_pkg_1) { + package_t *pkg = NULL; + category_t *cat = NULL; + database_t *ptr = NULL; + u8_t ret = 0; + + ptr = mpm_database_open(&ret, NULL); + TEST_ASSERT((ret == 0), "Can't open the database"); + TEST_ASSERT((ptr != NULL), "Can't open the database"); + + ret = mpm_database_init(ptr); + TEST_ASSERT((ret == 0), "Can't init database"); + + pkg = malloc(sizeof(package_t)); + cat = malloc(sizeof(category_t)); + + assert(pkg != NULL && cat != NULL); + pkg->name = strdup("test"); + pkg->version = strdup("1.0"); + cat->name = strdup("test"); + pkg->categ = cat; + pkg->desc = strdup("This a test package."); + pkg->state = PACKAGE_STATE_USER_INSTALLED; + pkg->deps = pkg->files = pkg->binaries = pkg->docs = pkg->config = NULL; + ret = mpm_database_add_pkg(ptr, pkg); + TEST_ASSERT((ret == 0), "Can't add a package in the database"); + mpm_database_close(ptr); + mpm_package_free(pkg); + free(pkg); + return TEST_SUCCESS; +} + +TEST(database_add_pkg_2) { + u8_t ret = 0; + + ret = mpm_database_add_pkg(NULL, NULL); + TEST_ASSERT((ret == 1), "Can't handle NULL pointer"); + return TEST_SUCCESS; +} + +TEST(database_get_pkg_by_id_1) { + mlist_t *lst; + database_t *ptr = NULL; + u8_t ret = 0; + + ptr = mpm_database_open(&ret, NULL); + TEST_ASSERT((ret == 0), "Can't open the database"); + TEST_ASSERT((ptr != NULL), "Can't open the database"); + + ret = mpm_get_package_by_id(ptr, 1, &lst); + TEST_ASSERT((list_size(lst) == 1), "Can't find the package"); + mpm_database_close(ptr); + list_free(lst, &mpm_package_free); + return TEST_SUCCESS; +} + +TEST(database_get_pkg_by_id_2) { + mlist_t *lst; + u8_t ret = 0; + + ret = mpm_get_package_by_id(NULL, 1, &lst); + TEST_ASSERT((ret == 1), "Can't handle NULL pointer"); + return TEST_SUCCESS; +} + +TEST(database_get_pkg_by_name_1) { + mlist_t *lst; + database_t *ptr = NULL; + u8_t ret = 0; + + ptr = mpm_database_open(&ret, NULL); + TEST_ASSERT((ret == 0), "Can't open the database"); + TEST_ASSERT((ptr != NULL), "Can't open the database"); + + ret = mpm_get_package_by_name(ptr, "test", &lst); + TEST_ASSERT((list_size(lst) == 1), "Can't find the package"); + mpm_database_close(ptr); + list_free(lst, &mpm_package_free); + return TEST_SUCCESS; +} + +TEST(database_get_pkg_by_name_2) { + mlist_t *lst; + u8_t ret = 0; + + ret = mpm_get_package_by_name(NULL, "test", &lst); + TEST_ASSERT((ret == 1), "Can't handle NULL pointer"); + clean_db(DB_FN); + return TEST_SUCCESS; +} + +TEST(database_sql_to_package) { + package_t *ptr = NULL; + + ptr = sql_to_package(NULL, NULL, NULL); + TEST_ASSERT((ptr == NULL), "Pointer is not NULL"); + return TEST_SUCCESS; +} + void clean_db(const char *name) { int st, fd[2]; @@ -215,4 +313,11 @@ void register_test_database(void) { reg_test("database", database_init_2); reg_test("database", database_init_test_pkg_table); reg_test("database", database_init_3); + reg_test("database", database_add_pkg_1); + reg_test("database", database_add_pkg_2); + reg_test("database", database_get_pkg_by_id_1); + reg_test("database", database_get_pkg_by_id_2); + reg_test("database", database_get_pkg_by_name_1); + reg_test("database", database_get_pkg_by_name_2); + reg_test("database", database_sql_to_package); }