libmpm

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

commit 9c4989e2aa2bfb4c57383f7afe7b1ed6da7b4b1d
parent ffa2585d08f91c0922c9f0a95b38c8fe0f30d6bf
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 30 Jan 2017 12:30:54 +0100

Add(Database): Some new functions in database, used for getting packages entry

Diffstat:
Ainc/category.h | 26++++++++++++++++++++++++++
Minc/database.h | 8++++++--
Ainc/files.h | 39+++++++++++++++++++++++++++++++++++++++
Ainc/package.h | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/database.c | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 209 insertions(+), 2 deletions(-)

diff --git a/inc/category.h b/inc/category.h @@ -0,0 +1,26 @@ +/*********************************** 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 CATEGORY_H +# define CATEGORY_H + +# include <morphux.h> + +typedef struct s_category { + char *name; +} category_t; + +#endif /* CATEGORY_H */ diff --git a/inc/database.h b/inc/database.h @@ -18,11 +18,12 @@ # define DATABASE_H # include <sqlite3.h> # include <morphux.h> +# include <package.h> # 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) +# define QUERY_GET_PACKAGE_BY_ID(id) "SELECT * FROM pkgs WHERE id = %lld", id typedef struct s_database { sqlite3 *sql; @@ -32,6 +33,9 @@ typedef struct s_database { 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); + SQL_CALLBACK_PTR(cl), void *ct, char **err); +u8_t mpm_get_package_by_id(mdatabase_t *ptr, u64_t id, + mlist_t **pkg); +package_t *sql_to_package(package_t *ptr, char *name, char *val); #endif /* DATABASE_H */ diff --git a/inc/files.h b/inc/files.h @@ -0,0 +1,39 @@ +/*********************************** 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 FILES_H +# define FILES_H + +# include <morphux.h> +# include <package.h> + +typedef struct s_package package_t; + +enum { + FILE_TYPE_CONFIG, + FILE_TYPE_BIN, + FILE_TYPE_LIB, + FILE_TYPE_OTHER +}; + +typedef struct s_file { + char *path; + u8_t type; + package_t *parent; + char *hash; +} file_t; + +#endif /* FILES_H */ diff --git a/inc/package.h b/inc/package.h @@ -0,0 +1,56 @@ +/*********************************** 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 PACKAGE_H +# define PACKAGE_H + +# include <morphux.h> +# include <category.h> +# include <files.h> + +enum { + PACKAGE_STATE_USER_INSTALLED, + PACKAGE_STATE_DEPENDENCY, + PACKAGE_STATE_ORPHAN +}; + +# define PKG_COL_ID "id" +# define PKG_COL_NAME "name" +# define PKG_COL_VERSION "version" +# define PKG_COL_CATEG "categ" +# define PKG_COL_DESC "desc" +# define PKG_COL_STATE "state" +# define PKG_COL_DEPS "deps" +# define PKG_COL_FILES "files" +# define PKG_COL_BINARIES "binaries" +# define PKG_COL_CONFIG "config" +# define PKG_COL_DOCS "docs" + +typedef struct s_package { + u64_t id; + char *name; + char *version; + category_t *categ; + char *desc; + u8_t state; + mlist_t *deps; + mlist_t *files; + mlist_t *binaries; + mlist_t *config; + mlist_t *docs; +} package_t; + +#endif /* PACKAGE_H */ diff --git a/src/database.c b/src/database.c @@ -92,3 +92,85 @@ u8_t mpm_database_exec(mdatabase_t *ptr, const char *query, return sqlite3_exec(ptr->sql, query, cl, ct, err); } + +/** + * int name(void *context, int col_num, char **col_txt, char **col_name) + */ +SQL_CALLBACK_DEF(callback_package_id) { + mlist_t *head = context; + package_t *ptr; + + if (col_num <= 0) + return 0; + + ptr = malloc(sizeof(package_t)); + assert(ptr != NULL); + + for (u8_t i = 0; i < col_num; i++) + sql_to_package(ptr, col_name[i], col_txt[i]); + + list_add(head, ptr, sizeof(ptr)); + return 0; +} + +/*! + * \brief Get a package by his Id + * \param ptr Opened Database connection + * \param id ID to search for + * \param pkg Pointer on a list, used to store the results + * \return Error code + * + * This function will search in an already opened database a package with a + * given id. + * A sql QUERY is constructed in this function, with the following content: + * SELECT * FROM pkgs WHERE 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 pkg to NULL before filling it with the results. + * You should not call this function with an existing package list. + */ +u8_t mpm_get_package_by_id(mdatabase_t *ptr, u64_t id, + mlist_t **pkg) { + char *query; + u8_t ret; + + if (ptr == NULL) + return 1; + + *pkg = NULL; + asprintf(&query, QUERY_GET_PACKAGE_BY_ID(id)); + ret = sqlite3_exec(ptr->sql, query, &callback_package_id, *pkg, NULL); + free(query); + return ret; +} + +/*! + * \brief Fill a package_t structure with a SQL result + * \param ptr Pointer to package_t. Must be NULL. + * \param name Name of the column + * \param val Value of the column + * + * This function will transform an SQL result, given through an sqlite callback, + * and fill a package_t structure with it. + * All the types conversion needed (strint -> *) are done in this function. + * + * \note If a unknown column is passed to this function, a panic will be throwed. + */ +package_t *sql_to_package(package_t *ptr, char *name, char *val) { + if (ptr == NULL) + return ptr; + + if (strcmp(name, PKG_COL_ID) == 0) { + ptr->id = strtoull(val, (char **)NULL, 10); + } else if (strcmp(name, PKG_COL_NAME) == 0 || + strcmp(name, PKG_COL_VERSION) == 0 || + strcmp(name, PKG_COL_DESC) == 0) { + ptr->name = strdup(val); + } else if (strcmp(name, PKG_COL_STATE) == 0) { + ptr->id = val[0] - '0'; + } else { + m_panic("Unknown column '%s' in get_package\n", name); + } + return ptr; +}