Code

utils data: Added module providing a data-type to store arbitrary backend data
authorSebastian Harl <sh@tokkee.org>
Tue, 11 Dec 2012 18:40:04 +0000 (19:40 +0100)
committerSebastian Harl <sh@tokkee.org>
Tue, 11 Dec 2012 18:40:04 +0000 (19:40 +0100)
This has been ripped out of the DBI module to make the functionality available
to other modules as well through a uniform interface.

src/Makefile.am
src/backend/puppet-storeconfigs.c
src/include/utils/data.h [new file with mode: 0644]
src/include/utils/dbi.h
src/utils/dbi.c

index 5785c5523008905d3375fb770614bd343ae2360c..2e3eb4520ce0022b158d6c132d60769541e3db23 100644 (file)
@@ -23,6 +23,7 @@ libsyscollector_la_SOURCES = \
                core/object.c include/core/object.h \
                core/plugin.c include/core/plugin.h \
                core/store.c include/core/store.h \
+               include/utils/data.h \
                utils/llist.c include/utils/llist.h \
                utils/string.c include/utils/string.h \
                utils/time.c include/utils/time.h \
index 0d02ba0ecb70ed52de2cd55d3b9aafca0a250b60..6cc68d3f44c102952a18e102aa30bf6dca697125 100644 (file)
@@ -49,15 +49,15 @@ SC_PLUGIN_MAGIC;
 
 static int
 sc_puppet_stcfg_get_data(sc_dbi_client_t __attribute__((unused)) *client,
-               size_t n, sc_dbi_data_t *data)
+               size_t n, sc_data_t *data)
 {
        sc_host_t host = SC_HOST_INIT;
 
        int status;
 
        assert(n == 2);
-       assert((data[0].type == DBI_TYPE_STRING)
-                       && (data[1].type == DBI_TYPE_DATETIME));
+       assert((data[0].type == SC_TYPE_STRING)
+                       && (data[1].type == SC_TYPE_DATETIME));
 
        host.host_name = strdup(data[0].data.string);
        host.host_last_update = data[1].data.datetime;
@@ -113,7 +113,7 @@ sc_puppet_stcfg_collect(sc_object_t *user_data)
        client = SC_OBJ_WRAPPER(user_data)->data;
        if (sc_dbi_exec_query(client, "SELECT name, updated_at FROM hosts;",
                                sc_puppet_stcfg_get_data, /* #columns = */ 2,
-                               /* col types = */ DBI_TYPE_STRING, DBI_TYPE_DATETIME)) {
+                               /* col types = */ SC_TYPE_STRING, SC_TYPE_DATETIME)) {
                fprintf(stderr, "puppet storeconfigs backend: Failed to retrieve "
                                "hosts from the storeconfigs DB.\n");
                return -1;
diff --git a/src/include/utils/data.h b/src/include/utils/data.h
new file mode 100644 (file)
index 0000000..a18d658
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * syscollector - src/include/utils/data.h
+ * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef SC_UTILS_DATA_H
+#define SC_UTILS_DATA_H 1
+
+#include "utils/time.h"
+
+#include <inttypes.h>
+#include <stddef.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum {
+       SC_TYPE_INTEGER = 1,
+       SC_TYPE_DECIMAL,
+       SC_TYPE_STRING,
+       SC_TYPE_DATETIME,
+       SC_TYPE_BINARY,
+};
+
+/*
+ * sc_data_t:
+ * A datum retrieved from an arbitrary data source.
+ *
+ * The string and binary objects are managed by whoever creates the data
+ * object, 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;  /* SC_TYPE_INTEGER */
+               double      decimal;  /* SC_TYPE_DECIMAL */
+               const char *string;   /* SC_TYPE_STRING  */
+               sc_time_t   datetime; /* SC_TYPE_DATETIME */
+               struct {
+                       size_t length;
+                       const unsigned char *datum;
+               } binary;             /* SC_TYPE_BINARY */
+       } data;
+} sc_data_t;
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* ! SC_UTILS_DATA_H */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+
index 2371286abe5e3953f2b208ed0fd0518939f5bc17..0f2a157c28dca42ab965a075e77a126bd94cb7ee 100644 (file)
 #ifndef SC_UTILS_DBI_H
 #define SC_UTILS_DBI_H 1
 
-#include "utils/time.h"
+#include "utils/data.h"
 
-#include <inttypes.h>
 #include <stddef.h>
 
+/* translate libdbi types to syscollector types */
+#define DBI_TYPE_TO_SC(dt) \
+       (((dt) == DBI_TYPE_INTEGER) \
+               ? SC_TYPE_INTEGER \
+               : ((dt) == DBI_TYPE_DECIMAL) \
+                       ? SC_TYPE_DECIMAL \
+                       : ((dt) == DBI_TYPE_STRING) \
+                               ? SC_TYPE_STRING \
+                               : ((dt) == DBI_TYPE_DATETIME) \
+                                       ? SC_TYPE_DATETIME \
+                                       : ((dt) == DBI_TYPE_BINARY) \
+                                               ? SC_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 *);
+typedef int (*sc_dbi_data_cb)(sc_dbi_client_t *, size_t, sc_data_t *);
 
 /*
  * sc_dbi_options_t:
index 3aef35e2213514839e9b12eb3d0625270e49b008..bd58ce7b4b9d12d7a6226da3dc061e2582f0712d 100644 (file)
@@ -73,26 +73,26 @@ sc_dbi_strerror(dbi_conn conn)
 
 static int
 sc_dbi_get_field(dbi_result res, unsigned int i,
-               int type, sc_dbi_data_t *data)
+               int type, sc_data_t *data)
 {
        switch (type) {
-               case DBI_TYPE_INTEGER:
+               case SC_TYPE_INTEGER:
                        data->data.integer = dbi_result_get_longlong_idx(res, i);
                        break;
-               case DBI_TYPE_DECIMAL:
+               case SC_TYPE_DECIMAL:
                        data->data.decimal = dbi_result_get_double_idx(res, i);
                        break;
-               case DBI_TYPE_STRING:
+               case SC_TYPE_STRING:
                        data->data.string = dbi_result_get_string_idx(res, i);
                        break;
-               case DBI_TYPE_DATETIME:
+               case SC_TYPE_DATETIME:
                        {
                                /* libdbi does not provide any higher resolutions than that */
                                time_t datetime = dbi_result_get_datetime_idx(res, i);
                                data->data.datetime = SECS_TO_SC_TIME(datetime);
                        }
                        break;
-               case DBI_TYPE_BINARY:
+               case SC_TYPE_BINARY:
                        {
                                size_t length = dbi_result_get_field_length_idx(res, i);
                                const unsigned char *datum = dbi_result_get_binary_idx(res, i);
@@ -114,7 +114,7 @@ static int
 sc_dbi_get_data(sc_dbi_client_t *client, dbi_result res,
                unsigned int num_fields, sc_dbi_data_cb callback)
 {
-       sc_dbi_data_t data[num_fields];
+       sc_data_t data[num_fields];
        int types[num_fields];
        unsigned int i;
 
@@ -131,6 +131,7 @@ sc_dbi_get_data(sc_dbi_client_t *client, dbi_result res,
                                        sc_dbi_strerror(client->conn));
                        return -1;
                }
+               types[i] = DBI_TYPE_TO_SC(types[i]);
        }
 
        num_rows = dbi_result_get_numrows(res);
@@ -398,6 +399,8 @@ sc_dbi_exec_query(sc_dbi_client_t *client, const char *query,
 
                        unsigned int type = va_arg(types, unsigned int);
 
+                       field_type = DBI_TYPE_TO_SC(field_type);
+
                        /* column count starts at 1 */
                        if ((unsigned int)field_type != type) {
                                fprintf(stderr, "dbi: type of column '%s' (%u) does not match "