libmpm

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

commit 3a32a1a86a0738a8fcf37ac805ffb6c47dea3c2e
parent e5a0e66a91aeb5f174fce07eed9a3c638097ac1e
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed,  1 Feb 2017 11:17:31 +0100

Add(File Table): Add files table init and defines

Diffstat:
Minc/database.h | 1+
Minc/files.h | 8++++++++
Msrc/database.c | 10++++++++++
Mtests/test_database.c | 30++++++++++++++++++++++++++++++
4 files changed, 49 insertions(+), 0 deletions(-)

diff --git a/inc/database.h b/inc/database.h @@ -19,6 +19,7 @@ # include <sqlite3.h> # include <morphux.h> # include <package.h> +# include <files.h> # define DB_FN "test.db" # define SQL_CALLBACK_PTR(name) int (*name)(void *, int, char**, char**) diff --git a/inc/files.h b/inc/files.h @@ -20,6 +20,14 @@ # include <morphux.h> # include <package.h> +# define FILE_TABLE "files" +# define FILE_COL_ID "id" +# define FILE_COL_PATH "path" +# define FILE_COL_TYPE "type" +# define FILE_COL_PARENT "parent" +# define FILE_COL_PARENT_NAME "parent_name" +# define FILE_COL_HASH "hash" + typedef struct s_package package_t; enum { diff --git a/src/database.c b/src/database.c @@ -221,6 +221,7 @@ package_t *sql_to_package(package_t *ptr, char *name, char *val) { */ u8_t mpm_database_init(database_t *ptr) { static const char *query_table[] = { \ + /* Package table */ SQL_CREATE_TABLE PKG_TABLE "(" \ PKG_COL_ID SQL_TYPE_INT SQL_TYPE_PRIMARY_KEY "," \ PKG_COL_NAME SQL_TYPE_TEXT SQL_TYPE_NOT_NULL "," \ @@ -233,6 +234,15 @@ u8_t mpm_database_init(database_t *ptr) { PKG_COL_BINARIES SQL_TYPE_TEXT "," \ PKG_COL_CONFIG SQL_TYPE_TEXT "," \ PKG_COL_DOCS SQL_TYPE_TEXT \ + ");", \ + /* Files table */ + SQL_CREATE_TABLE FILE_TABLE "(" \ + FILE_COL_ID SQL_TYPE_INT SQL_TYPE_PRIMARY_KEY "," \ + FILE_COL_PATH SQL_TYPE_TEXT SQL_TYPE_NOT_NULL "," \ + FILE_COL_TYPE SQL_TYPE_INT SQL_TYPE_NOT_NULL "," \ + FILE_COL_PARENT SQL_TYPE_INT SQL_TYPE_NOT_NULL "," \ + FILE_COL_PARENT_NAME SQL_TYPE_TEXT SQL_TYPE_NOT_NULL "," \ + FILE_COL_HASH SQL_TYPE_TEXT SQL_TYPE_NOT_NULL \ ");" \ }; char *err = NULL; diff --git a/tests/test_database.c b/tests/test_database.c @@ -133,6 +133,35 @@ TEST(database_init_test_pkg_table) { return TEST_SUCCESS; } +TEST(database_init_test_files_table) { + database_t *ptr = NULL; + u8_t ret = 0; + mlist_t *res = NULL, *tmp, *tmp2, *tmp3, *test = NULL; + char *err = NULL; + sql_result_t *result; + + 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_exec(ptr, "PRAGMA table_info([" FILE_TABLE "])", + &exec_callback, &res, &err); + TEST_ASSERT((ret == 0), "An error happened"); + TEST_ASSERT((err == NULL), "An error happened"); + mpm_database_close(ptr); + + list_for_each(res, tmp, tmp2) { + list_for_each(tmp2, tmp3, result) { + if (!strcmp(result->name, "name")) + list_add(test, result->value, strlen(result->value)); + } + } + TEST_ASSERT((list_size(test) == 6), "Number of columns is wrong"); + list_free(test, NULL); + free_sql_results(res); + return TEST_SUCCESS; +} + + TEST(database_init_3) { database_t *ptr = NULL; u8_t ret = 0; @@ -314,6 +343,7 @@ void register_test_database(void) { reg_test("database", database_init_1); reg_test("database", database_init_2); reg_test("database", database_init_test_pkg_table); + reg_test("database", database_init_test_files_table); reg_test("database", database_init_3); reg_test("database", database_add_pkg_1); reg_test("database", database_add_pkg_2);