libmpm

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

commit b9e10f229b8beb6553e389c17c754a624a6085da
parent 8ba23a504e76d16e936daac8d5f474cf5f5df6e8
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri, 27 Jan 2017 19:07:23 +0100

Add(Database): Add function mpm_database_exec:

Tests with it too

Diffstat:
Minc/database.h | 6++++++
Msrc/database.c | 27+++++++++++++++++++++++++++
Mtests/test_database.c | 62+++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
3 files changed, 90 insertions(+), 5 deletions(-)

diff --git a/inc/database.h b/inc/database.h @@ -21,11 +21,17 @@ # define DB_FN "test.db" +# define SQL_CALLBACK_PTR(name) int (*name)(void *, int, char**, char**) +# define SQL_CALLBACK_DEF(name) int name(void *context, int col_num, char **col_txt, char **col_name) + typedef struct s_database { sqlite3 *sql; } mdatabase_t; + mdatabase_t *mpm_database_open(u8_t *ret, const char *fn); u8_t mpm_database_close(mdatabase_t *ptr); +u8_t mpm_database_exec(mdatabase_t *ptr, const char *query, + SQL_CALLBACK_PTR(cl), void *ct, char **err); #endif /* DATABASE_H */ diff --git a/src/database.c b/src/database.c @@ -43,6 +43,7 @@ mdatabase_t *mpm_database_open(u8_t *ret, const char *fn) { return ptr; error: + sqlite3_close(ptr->sql); free(ptr); *ret = error; return NULL; @@ -67,3 +68,29 @@ u8_t mpm_database_close(mdatabase_t *ptr) { } return 0; } + +/*! + * \brief Execute a SQL query on an already opened database + * \param ptr Opened database + * \param query SQL Query + * \param cl Query callback + * \param ct First parameter of the callback (Context) + * \param err Pointer on a string, to be filled with a string error. + * + * This function will execute an SQL query on an already opened database. + * If the ptr or query parameter is NULL, the function will not do anything + * and return 1. + * The callback is used to get the result of a query. + * The callback is defined and used trough defines, for readibilty purposes. + * see SQL_CALLBACK_PTR and SQL_CALLBACK_DEF for more information. + * If an SQL error happens, the err pointer is filled with a text error. + * This string is allocated by the sqlite library, and should be freed by the + * caller. + */ +u8_t mpm_database_exec(mdatabase_t *ptr, const char *query, + SQL_CALLBACK_PTR(cl), void *ct, char **err) { + if (ptr == NULL || query == NULL) + return 1; + + return sqlite3_exec(ptr->sql, query, cl, ct, err); +} diff --git a/tests/test_database.c b/tests/test_database.c @@ -5,10 +5,10 @@ TEST(database_open_1) { u8_t ret = 0; ptr = mpm_database_open(&ret, NULL); - TEST_ASSERT(ret == 0, "Database can't be open"); - TEST_ASSERT((ptr != NULL), "Database can't be open"); + TEST_ASSERT(ret == 0, "Database can't be opened"); + TEST_ASSERT((ptr != NULL), "Database can't be opened"); ret = mpm_database_close(ptr); - TEST_ASSERT(ret == 0, "Database can't be close") + TEST_ASSERT(ret == 0, "Database can't be closed") return TEST_SUCCESS; } @@ -17,8 +17,9 @@ TEST(database_open_2) { u8_t ret = 0; ptr = mpm_database_open(&ret, "/illogical/path"); - TEST_ASSERT((ret == 14), "Database has be open ?!"); - TEST_ASSERT((ptr == NULL), "Database can't be open"); + TEST_ASSERT((ret == 14), "Database has been opened ?!"); + TEST_ASSERT((ptr == NULL), "Database can't be opened"); + mpm_database_close(ptr); return TEST_SUCCESS; } @@ -38,8 +39,59 @@ TEST(database_close_1) { return TEST_SUCCESS; } +/** + * int name(void *context, int col_num, char **col_txt, char **col_name) + */ +SQL_CALLBACK_DEF(exec_callback) { + (void)context; + fprintf(stdout, "\n\nCol_num: %d\n", col_num); + if (col_num == 0) + return 0; + while (*col_txt) { + fprintf(stdout, "> %s\n", *col_txt); + col_txt++; + } + + while (*col_name) { + fprintf(stdout, "> %s\n", *col_name); + col_name++; + } + + return 0; +} + +TEST(database_exec_1) { + mdatabase_t *ptr = NULL; + u8_t ret = 0; + char *err = NULL; + + 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, ";", &exec_callback, ptr, &err); + TEST_ASSERT((err == NULL), "An error occured."); + mpm_database_close(ptr); + return TEST_SUCCESS; +} + +TEST(database_exec_2) { + mdatabase_t *ptr = NULL; + u8_t ret = 0; + char *err = NULL; + + 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(NULL, NULL, &exec_callback, ptr, &err); + TEST_ASSERT((ret == 1), "NULL not handled correctly."); + mpm_database_close(ptr); + return TEST_SUCCESS; +} + void register_test_database(void) { reg_test("database", database_open_1); reg_test("database", database_open_2); reg_test("database", database_close_1); + reg_test("database", database_exec_1); + reg_test("database", database_exec_2); }