libmpm

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

commit f39c72bd43f0cfddd273208257cff01f6ac0b379
parent 91ce586c48df81055d3ead6be5ad7d23f18d38ce
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Thu, 26 Jan 2017 10:29:44 +0100

Add(Database): Add new functions:

mpm_database_open: Open a connection to a database
mpm_database_close: Close a connection to a database
Add some tests with it too.

Diffstat:
M.gitignore | 3+++
Ainc/database.h | 31+++++++++++++++++++++++++++++++
Minc/libmpm.h | 1+
Asrc/database.c | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtests/Makefile | 2+-
Mtests/main.c | 1+
Mtests/test.h | 1+
Atests/test_database.c | 17+++++++++++++++++
8 files changed, 118 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore @@ -46,3 +46,6 @@ tests/.gdb_history docs/doxygen_sqlite3.db docs/html docs/man + +# Databases +*.db diff --git a/inc/database.h b/inc/database.h @@ -0,0 +1,31 @@ +/*********************************** 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. * +\******************************************************************************/ + +#ifndef DATABASE_H +# define DATABASE_H +# include <sqlite3.h> +# include <morphux.h> + +# define DB_FN "test.db" + +typedef struct s_database { + sqlite3 *sql; +} mdatabase_t; + +mdatabase_t *mpm_database_open(u8_t *ret); +u8_t mpm_database_close(mdatabase_t *ptr); + +#endif /* DATABASE_H */ diff --git a/inc/libmpm.h b/inc/libmpm.h @@ -19,5 +19,6 @@ # include <morphux.h> # include <flags.h> +# include <database.h> #endif /* LIBMPM_H */ diff --git a/src/database.c b/src/database.c @@ -0,0 +1,63 @@ +/*********************************** 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 <database.h> + +/*! + * \brief Open a connection to a database + * \param ret Return code, if any error + * \return A fresh mdatabase_t structure + * + * This function will create a new mdatabase_t struct, allocate it, and open + * a new database connection. In case of any error, the return value will be + * NULL, and the ret pointer set to an error code. + */ +mdatabase_t *mpm_database_open(u8_t *ret) { + mdatabase_t *ptr; + u8_t error = 0; + + ptr = malloc(sizeof(mdatabase_t)); + assert(ptr != NULL); + error = sqlite3_open(DB_FN, &ptr->sql); + if (error != 0) + goto error; + return ptr; + +error: + free(ptr); + *ret = error; + return NULL; +} + +/*! + * \brief Close a connection to an existing database + * \param ptr An open database + * \return The error code + * + * This function will close a database connection and free the passed struct. + * On any error, this function will return the error code. + */ +u8_t mpm_database_close(mdatabase_t *ptr) { + u8_t error; + + if (ptr != NULL) { + error = sqlite3_close(ptr->sql); + if (error != 0) + return error; + free(ptr); + } + return 0; +} diff --git a/tests/Makefile b/tests/Makefile @@ -19,7 +19,7 @@ SFLAGS = -Wall -Wextra -Werror -Wno-unused-result #-L ../lib/curl/.libs/ -lcurl \ #-L ../lib/json-c/.libs -ljson-c \ -LFLAGS= -L ../ -lmpm +LFLAGS= -L ../ -lmpm -pthread -ldl CFLAGS = $(SFLAGS) $(IFLAGS) $(OFLAGS) $(LFLAGS) diff --git a/tests/main.c b/tests/main.c @@ -4,6 +4,7 @@ int main(void) { int ret; register_flags_test(); + register_test_database(); ret = test_all(); test_free(); return ret; diff --git a/tests/test.h b/tests/test.h @@ -4,5 +4,6 @@ # include <libmpm.h> void register_flags_test(void); +void register_test_database(void); #endif /* TEST_H */ diff --git a/tests/test_database.c b/tests/test_database.c @@ -0,0 +1,17 @@ +#include "test.h" + +TEST(database_open_1) { + mdatabase_t *ptr = NULL; + u8_t ret = 0; + + ptr = mpm_database_open(&ret); + TEST_ASSERT(ret == 0, "Database can't be open"); + TEST_ASSERT((ptr != NULL), "Database can't be open"); + ret = mpm_database_close(ptr); + TEST_ASSERT(ret == 0, "Database can't be close") + return TEST_SUCCESS; +} + +void register_test_database(void) { + reg_test("database", database_open_1); +}