libmpm

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

commit 7cc2ced2d47f4d67378f5859ec9fd15eac697e25
parent ac5938e5006d10ab7a96663ad64b7cc9f54af669
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed,  8 Feb 2017 19:02:00 +0100

Add(Files): Add multiples get files functions:

Fix insert query for files
Fix free for file_T
Add tests for those functions

Diffstat:
Minc/database.h | 7+++++++
Msrc/database.c | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Msrc/files.c | 5++++-
Mtests/test_database.c | 114++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
4 files changed, 223 insertions(+), 4 deletions(-)

diff --git a/inc/database.h b/inc/database.h @@ -61,6 +61,7 @@ typedef struct s_database { /* SQL callbacks */ +SQL_CALLBACK_DEF(callback_package); SQL_CALLBACK_DEF(callback_files); SQL_CALLBACK_DEF(callback_categ); @@ -77,6 +78,12 @@ u8_t mpm_database_init(database_t *ptr); u8_t mpm_database_add_pkg(database_t *ptr, package_t *pkg); u8_t mpm_get_file_by_id(database_t *ptr, u64_t id, mlist_t **files); +u8_t mpm_get_file_by_path(database_t *ptr, const char *path, + mlist_t **files); +u8_t mpm_get_file_by_parent_id(database_t *ptr, u64_t id, + mlist_t **files); +u8_t mpm_get_file_by_parent_name(database_t *ptr, const char *name, + mlist_t **files); 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); diff --git a/src/database.c b/src/database.c @@ -340,6 +340,103 @@ u8_t mpm_get_file_by_id(database_t *ptr, u64_t id, return ret; } +/*! + * \brief Get a file by his path + * \param ptr Opened Database connection + * \param path Path 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 file with a + * given path. + * A sql QUERY is constructed in this function, with the following content: + * SELECT * FROM files WHERE path = "%s", where %s is the given path + * 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_file_by_path(database_t *ptr, const char *path, + mlist_t **files) { + char *query; + u8_t ret; + + if (ptr == NULL || path == NULL) + return 1; + + *files = NULL; + asprintf(&query, QUERY_GET_FILES_BY_PATH(path)); + ret = sqlite3_exec(ptr->sql, query, &callback_files, files, NULL); + free(query); + return ret; +} + +/*! + * \brief Get a file by his parent id + * \param ptr Opened Database connection + * \param id Parent ID 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 file with a + * given parent id. + * A sql QUERY is constructed in this function, with the following content: + * SELECT * FROM files WHERE parent_id = %d, where %d is the given id + * 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_file_by_parent_id(database_t *ptr, u64_t id, + mlist_t **files) { + char *query; + u8_t ret; + + if (ptr == NULL) + return 1; + + *files = NULL; + asprintf(&query, QUERY_GET_FILES_BY_PARENT_ID(id)); + ret = sqlite3_exec(ptr->sql, query, &callback_files, files, NULL); + free(query); + return ret; +} + +/*! + * \brief Get a file by his parent name + * \param ptr Opened Database connection + * \param name Parent name + * \param files Pointer on a list, used to store the results + * \return Error code + * + * This function will search in an already opened database a file with a + * given parent name. + * A sql QUERY is constructed in this function, with the following content: + * SELECT * FROM files WHERE parent_name = "%s", where %s is the given parent 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_file_by_parent_name(database_t *ptr, const char *name, + mlist_t **files) { + char *query; + u8_t ret; + + if (ptr == NULL || name == NULL) + return 1; + + *files = NULL; + asprintf(&query, QUERY_GET_FILES_BY_PARENT_NAME(name)); + ret = sqlite3_exec(ptr->sql, query, &callback_files, files, NULL); + free(query); + return ret; +} + + /** * int name(void *context, int col_num, char **col_txt, char **col_name) */ @@ -407,10 +504,10 @@ u8_t mpm_database_add_file(database_t *ptr, file_t *file) { asprintf(&query, SQL_INSERT_TABLE FILE_TABLE \ " (%s, %s, %s, %s, %s) " \ - "VALUES (\"%s\", \"%d\", \"%d\", \"%s\", \"%s\");", + "VALUES (\"%s\", \"%d\", \"%lld\", \"%s\", \"%s\");", FILE_COL_PATH, FILE_COL_TYPE, FILE_COL_PARENT, FILE_COL_PARENT_NAME, FILE_COL_HASH, - file->path, file->type, /* TMP */0, file->parent_name, file->hash); + file->path, file->type, file->parent->id, file->parent_name, file->hash); ret = mpm_database_exec(ptr, query, NULL, NULL, &err); free(query); assert(ret == 0 && err == NULL); diff --git a/src/files.c b/src/files.c @@ -39,8 +39,11 @@ int mpm_file_free(void *tmp) { if (ptr) { free(ptr->path); free(ptr->hash); - mpm_package_free(ptr->parent); free(ptr->parent_name); + if (ptr->parent != NULL) { + mpm_package_free(ptr->parent); + free(ptr->parent); + } } return 1; } diff --git a/tests/test_database.c b/tests/test_database.c @@ -273,13 +273,17 @@ TEST(database_add_file_1) { TEST_ASSERT((ret == 0), "Can't init database"); file = malloc(sizeof(file_t)); + parent = malloc(sizeof(package_t)); - assert(file != NULL); + assert(file != NULL && parent != NULL); mpm_file_init(file); + mpm_package_init(parent); file->path = strdup("/tmp/test"); file->hash = strdup("Totally not a hash."); file->parent_name = strdup("Some Star Wars joke"); file->type = FILE_TYPE_BIN; + parent->id = 1; + file->parent = parent; ret = mpm_database_add_file(ptr, file); TEST_ASSERT((ret == 0), "Can't add a file to the database"); @@ -425,6 +429,107 @@ TEST(database_get_file_by_id_2) { return TEST_SUCCESS; } +TEST(database_get_file_by_id_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_file_by_id(ptr, 999, &lst); + TEST_ASSERT((list_size(lst) == 0), "Found the package ?!"); + mpm_database_close(ptr); + return TEST_SUCCESS; +} + +TEST(database_get_file_by_parent_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_file_by_parent_id(ptr, 1, &lst); + TEST_ASSERT((list_size(lst) == 1), "Can't find the package"); + mpm_database_close(ptr); + list_free(lst, &mpm_file_free); + return TEST_SUCCESS; +} + +TEST(database_get_file_by_parent_id_2) { + mlist_t *lst; + u8_t ret = 0; + + ret = mpm_get_file_by_parent_id(NULL, 1, &lst); + TEST_ASSERT((ret == 1), "Can't handle NULL pointer"); + + return TEST_SUCCESS; +} + +TEST(database_get_file_by_parent_id_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_file_by_parent_id(ptr, 999, &lst); + TEST_ASSERT((list_size(lst) == 0), "Found the package ?!"); + mpm_database_close(ptr); + return TEST_SUCCESS; +} + + + +TEST(database_get_file_by_path_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_file_by_path(ptr, "/tmp/test", &lst); + TEST_ASSERT((list_size(lst) == 1), "Can't find the package"); + mpm_database_close(ptr); + list_free(lst, &mpm_file_free); + return TEST_SUCCESS; +} + +TEST(database_get_file_by_path_2) { + mlist_t *lst; + u8_t ret = 0; + + ret = mpm_get_file_by_path(NULL, NULL, &lst); + TEST_ASSERT((ret == 1), "Can't handle NULL pointer"); + + return TEST_SUCCESS; +} + +TEST(database_get_file_by_path_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_file_by_path(ptr, "/non/sense", &lst); + TEST_ASSERT((list_size(lst) == 0), "Found the package ?!"); + mpm_database_close(ptr); + return TEST_SUCCESS; +} + + + TEST(database_get_category_by_id_1) { mlist_t *lst; database_t *ptr = NULL; @@ -567,6 +672,13 @@ void register_test_database(void) { reg_test("database", database_add_file_2); reg_test("database", database_get_file_by_id_1); reg_test("database", database_get_file_by_id_2); + reg_test("database", database_get_file_by_id_3); + reg_test("database", database_get_file_by_path_1); + reg_test("database", database_get_file_by_path_2); + reg_test("database", database_get_file_by_path_3); + reg_test("database", database_get_file_by_parent_id_1); + reg_test("database", database_get_file_by_parent_id_2); + reg_test("database", database_get_file_by_parent_id_3); reg_test("database", database_sql_to_file); reg_test("database", database_add_category_1); reg_test("database", database_add_category_2);