libmpm

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

commit e553fb16c97e635e37cf2ea993243f069ad9fc1f
parent 7e9e84be34d8979462a76481e81bb2df867c5fe8
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Thu,  9 Feb 2017 19:37:37 +0100

Add(Category): Add get_categ_by_name function:

Tests with it.

Diffstat:
Minc/database.h | 4+++-
Msrc/database.c | 31+++++++++++++++++++++++++++++++
Mtests/test_database.c | 44++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 78 insertions(+), 1 deletion(-)

diff --git a/inc/database.h b/inc/database.h @@ -42,11 +42,12 @@ # define QUERY_GET_FILES_BY_PARENT_NAME(name) "SELECT * FROM " FILE_TABLE \ " WHERE parent_name = \"%s\"", \ name -/* TODO: type */ /* Category search queries */ # define QUERY_GET_CATEG_BY_ID(id) "SELECT * FROM " CAT_TABLE \ " WHERE id = %lld", id +# define QUERY_GET_CATEG_BY_NAME(name) "SELECT * FROM " CAT_TABLE \ + " WHERE name = \"%s\"", name # define SQL_CREATE_TABLE "CREATE table " # define SQL_INSERT_TABLE "INSERT INTO " @@ -88,6 +89,7 @@ file_t *sql_to_file(file_t *ptr, char *name, char *val); u8_t mpm_database_add_file(database_t *ptr, file_t *file); category_t *sql_to_category(category_t *ptr, char *name, char *val); u8_t mpm_get_categ_by_id(database_t *ptr, u64_t id, mlist_t **cat); +u8_t mpm_get_categ_by_name(database_t *ptr, const char *name, mlist_t **cat); u8_t mpm_database_add_categ(database_t *ptr, category_t *cat); #endif /* DATABASE_H */ diff --git a/src/database.c b/src/database.c @@ -571,6 +571,37 @@ u8_t mpm_get_categ_by_id(database_t *ptr, u64_t id, mlist_t **cat) { return ret; } +/*! + * \brief Get a category by his name + * \param ptr Opened Database connection + * \param name Name to search for + * \param files Pointer on a list, used to store the results + * \return Error code + * + * This function will search in an already opened database a category with a + * given name. + * A sql QUERY is constructed in this function, with the following content: + * SELECT * FROM categ WHERE name = %s, where %s is the given name + * This function will call list_add to add results to the given list, + * caller should properly free this list. + * + * \note This function will set files to NULL before filling it with the results. + * You should not call this function with an existing files list. + */ + +u8_t mpm_get_categ_by_name(database_t *ptr, const char *name, mlist_t **cat) { + char *query; + u8_t ret; + + if (ptr == NULL) + return 1; + *cat = NULL; + asprintf(&query, QUERY_GET_CATEG_BY_NAME(name)); + ret = sqlite3_exec(ptr->sql, query, &callback_categ, cat, NULL); + free(query); + return ret; +} + /** * int name(void *context, int col_num, char **col_txt, char **col_name) */ diff --git a/tests/test_database.c b/tests/test_database.c @@ -368,6 +368,47 @@ TEST(database_get_pkg_by_id_2) { return TEST_SUCCESS; } +TEST(database_get_category_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_categ_by_name(ptr, "test", &lst); + TEST_ASSERT((list_size(lst) == 1), "Can't find the category"); + mpm_database_close(ptr); + list_free(lst, &mpm_category_free); + return TEST_SUCCESS; +} + +TEST(database_get_category_by_name_2) { + mlist_t *lst; + u8_t ret; + + ret = mpm_get_categ_by_name(NULL, NULL, &lst); + TEST_ASSERT((ret == 1), "Can't handle NULL pointer"); + return TEST_SUCCESS; +} + +TEST(database_get_category_by_name_3) { + 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_categ_by_name(ptr, "non sense", &lst); + TEST_ASSERT((list_size(lst) == 0), "Find the category !?"); + mpm_database_close(ptr); + return TEST_SUCCESS; +} + + TEST(database_get_pkg_by_name_1) { mlist_t *lst; database_t *ptr = NULL; @@ -726,5 +767,8 @@ void register_test_database(void) { reg_test("database", database_add_category_2); reg_test("database", database_get_category_by_id_1); reg_test("database", database_get_category_by_id_2); + reg_test("database", database_get_category_by_name_1); + reg_test("database", database_get_category_by_name_2); + reg_test("database", database_get_category_by_name_3); reg_test("database", database_sql_to_categ); }