diff options
author | OpenGnSys Support Team <soporte-og@soleta.eu> | 2019-05-17 12:26:26 +0200 |
---|---|---|
committer | OpenGnSys Support Team <soporte-og@soleta.eu> | 2020-03-04 14:53:01 +0100 |
commit | 2629906b6d961064ad566f90adfe8d28a7315d8a (patch) | |
tree | e0b4b4f254f6589d2cb04b73b4efb83164edc83f /sources | |
parent | a245411ab23f9fa1e5fd43ce5ce0d1391e6ed6e6 (diff) |
#941 add basic database-independent abstraction (dbi)
Add basic infrastructure to support for the independent database layer.
Diffstat (limited to 'sources')
-rw-r--r-- | sources/dbi.cpp | 37 | ||||
-rw-r--r-- | sources/dbi.h | 21 |
2 files changed, 58 insertions, 0 deletions
diff --git a/sources/dbi.cpp b/sources/dbi.cpp new file mode 100644 index 0000000..cc5c5b5 --- /dev/null +++ b/sources/dbi.cpp @@ -0,0 +1,37 @@ +#include "dbi.h" + +struct og_dbi *og_dbi_open(struct og_dbi_config *config) +{ + struct og_dbi *dbi; + + dbi = (struct og_dbi *)malloc(sizeof(struct og_dbi)); + if (!dbi) + return NULL; + + dbi_initialize_r(NULL, &dbi->inst); + dbi->conn = dbi_conn_new_r("mysql", dbi->inst); + if (!dbi->conn) { + free(dbi); + return NULL; + } + + dbi_conn_set_option(dbi->conn, "host", config->host); + dbi_conn_set_option(dbi->conn, "username", config->user); + dbi_conn_set_option(dbi->conn, "password", config->passwd); + dbi_conn_set_option(dbi->conn, "dbname", config->database); + dbi_conn_set_option(dbi->conn, "encoding", "UTF-8"); + + if (dbi_conn_connect(dbi->conn) < 0) { + free(dbi); + return NULL; + } + + return dbi; +} + +void og_dbi_close(struct og_dbi *dbi) +{ + dbi_conn_close(dbi->conn); + dbi_shutdown_r(dbi->inst); + free(dbi); +} diff --git a/sources/dbi.h b/sources/dbi.h new file mode 100644 index 0000000..882550f --- /dev/null +++ b/sources/dbi.h @@ -0,0 +1,21 @@ +#ifndef __OG_DBI +#define __OG_DBI + +#include <dbi/dbi.h> + +struct og_dbi_config { + const char *user; + const char *passwd; + const char *host; + const char *database; +}; + +struct og_dbi { + dbi_conn conn; + dbi_inst inst; +}; + +struct og_dbi *og_dbi_open(struct og_dbi_config *config); +void og_dbi_close(struct og_dbi *db); + +#endif |