Code

Renamed the project to SysDB (System DataBase).
[sysdb.git] / src / include / utils / dbi.h
index 2371286abe5e3953f2b208ed0fd0518939f5bc17..77d004296d333acaefc7460677a246765e182423 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * syscollector - src/include/utils/dbi.h
+ * SysDB - src/include/utils/dbi.h
  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
  * All rights reserved.
  *
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef SC_UTILS_DBI_H
-#define SC_UTILS_DBI_H 1
+#ifndef SDB_UTILS_DBI_H
+#define SDB_UTILS_DBI_H 1
 
-#include "utils/time.h"
+#include "core/object.h"
+#include "utils/data.h"
 
-#include <inttypes.h>
 #include <stddef.h>
 
+/* translate libdbi types to SysDB types */
+#define DBI_TYPE_TO_SC(dt) \
+       (((dt) == DBI_TYPE_INTEGER) \
+               ? SDB_TYPE_INTEGER \
+               : ((dt) == DBI_TYPE_DECIMAL) \
+                       ? SDB_TYPE_DECIMAL \
+                       : ((dt) == DBI_TYPE_STRING) \
+                               ? SDB_TYPE_STRING \
+                               : ((dt) == DBI_TYPE_DATETIME) \
+                                       ? SDB_TYPE_DATETIME \
+                                       : ((dt) == DBI_TYPE_BINARY) \
+                                               ? SDB_TYPE_BINARY : 0)
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-/*
- * sc_dbi_data_t:
- * A datum retrieved from a single field of a query result row.
- *
- * The string and binary objects are managed by libdbi, thus, they must not be
- * freed or modified. If you want to keep them, make sure to make a copy.
- */
-typedef struct {
-       int type;
-       union {
-               int64_t     integer;  /* DBI_TYPE_INTEGER */
-               double      decimal;  /* DBI_TYPE_DECIMAL */
-               const char *string;   /* DBI_TYPE_STRING  */
-               sc_time_t   datetime; /* DBI_TYPE_DATETIME */
-               struct {
-                       size_t length;
-                       const unsigned char *datum;
-               } binary;             /* DBI_TYPE_BINARY */
-       } data;
-} sc_dbi_data_t;
-
-struct sc_dbi_options;
-typedef struct sc_dbi_options sc_dbi_options_t;
-
-struct sc_dbi_client;
-typedef struct sc_dbi_client sc_dbi_client_t;
-
-typedef int (*sc_dbi_data_cb)(sc_dbi_client_t *, size_t, sc_dbi_data_t *);
+struct sdb_dbi_options;
+typedef struct sdb_dbi_options sdb_dbi_options_t;
+
+struct sdb_dbi_client;
+typedef struct sdb_dbi_client sdb_dbi_client_t;
+
+typedef int (*sdb_dbi_data_cb)(sdb_dbi_client_t *,
+               size_t, sdb_data_t *, sdb_object_t *);
 
 /*
- * sc_dbi_options_t:
+ * sdb_dbi_options_t:
  * This object stores DBI connection options (key/value) (e.g. host, dbname,
  * etc.). It may be used to dynamically create the list of options before
  * applying it to some client object.
  */
-sc_dbi_options_t *
-sc_dbi_options_create(void);
+sdb_dbi_options_t *
+sdb_dbi_options_create(void);
 
 int
-sc_dbi_options_add(sc_dbi_options_t *options,
+sdb_dbi_options_add(sdb_dbi_options_t *options,
                const char *key, const char *value);
 
 void
-sc_dbi_options_destroy(sc_dbi_options_t *options);
+sdb_dbi_options_destroy(sdb_dbi_options_t *options);
 
 /*
- * sc_dbi_client_create:
+ * sdb_dbi_client_create:
  * Creates a new DBI client object using the specified DBI / DBD 'driver' and
  * connecting to the specified 'database'.
  *
@@ -91,24 +84,24 @@ sc_dbi_options_destroy(sc_dbi_options_t *options);
  *  - the client object on success
  *  - NULL else
  */
-sc_dbi_client_t *
-sc_dbi_client_create(const char *driver, const char *database);
+sdb_dbi_client_t *
+sdb_dbi_client_create(const char *driver, const char *database);
 
 /*
- * sc_dbi_client_set_options:
+ * sdb_dbi_client_set_options:
  * Apply connection options to an existing client object. This has to be done
- * before actually connecting to the database using sc_dbi_client_connect().
+ * before actually connecting to the database using sdb_dbi_client_connect().
  *
  * Returns:
  *  - 0 on success
  *  - a negative value else
  */
 int
-sc_dbi_client_set_options(sc_dbi_client_t *client,
-               sc_dbi_options_t *options);
+sdb_dbi_client_set_options(sdb_dbi_client_t *client,
+               sdb_dbi_options_t *options);
 
 /*
- * sc_dbi_client_connect:
+ * sdb_dbi_client_connect:
  * Connect to the database using the options registered beforehand.
  *
  * This function may also be used to reconnect to the database.
@@ -118,17 +111,17 @@ sc_dbi_client_set_options(sc_dbi_client_t *client,
  *  - a negative value else
  */
 int
-sc_dbi_client_connect(sc_dbi_client_t *client);
+sdb_dbi_client_connect(sdb_dbi_client_t *client);
 
 /*
- * sc_dbi_exec_query:
+ * sdb_dbi_exec_query:
  * Execute an SQL query on the database. The specified 'callback' will be
  * called for each row returned from the query. If 'n' is a value equal to or
  * greater than zero, it specifies the number of columns that are expected in
  * the query result. For each column, the caller then needs to also specify
  * the requested type (see the DBI_TYPE_* constants). If the number or types
  * do not match, an error will be reported and the query will fail. That is,
- * this allows to let sc_dbi_exec_query() do basic verification of the
+ * this allows to let sdb_dbi_exec_query() do basic verification of the
  * returned values.
  *
  * The callback will receive the client object and an array containing the
@@ -141,21 +134,21 @@ sc_dbi_client_connect(sc_dbi_client_t *client);
  *  - a negative value else
  */
 int
-sc_dbi_exec_query(sc_dbi_client_t *client, const char *query,
-               sc_dbi_data_cb callback, int n, ...);
+sdb_dbi_exec_query(sdb_dbi_client_t *client, const char *query,
+               sdb_dbi_data_cb callback, sdb_object_t *user_data, int n, ...);
 
 /*
- * sc_dbi_client_destroy:
+ * sdb_dbi_client_destroy:
  * Disconnect from the database and destroy the client object.
  */
 void
-sc_dbi_client_destroy(sc_dbi_client_t *client);
+sdb_dbi_client_destroy(sdb_dbi_client_t *client);
 
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
 
-#endif /* ! SC_UTILS_DBI_H */
+#endif /* ! SDB_UTILS_DBI_H */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */