Code

utils dbi + unixsock: Pass user-data object to data processing callbacks.
authorSebastian Harl <sh@tokkee.org>
Wed, 12 Dec 2012 05:15:11 +0000 (06:15 +0100)
committerSebastian Harl <sh@tokkee.org>
Wed, 12 Dec 2012 05:15:11 +0000 (06:15 +0100)
This may be used to access, for example, state information throughout each
call.

src/backend/puppet-storeconfigs.c
src/include/utils/dbi.h
src/include/utils/unixsock.h
src/utils/dbi.c
src/utils/unixsock.c

index 6cc68d3f44c102952a18e102aa30bf6dca697125..4397e8760ed5c6d402cec0a5e1b796c52228041e 100644 (file)
@@ -49,7 +49,8 @@ SC_PLUGIN_MAGIC;
 
 static int
 sc_puppet_stcfg_get_data(sc_dbi_client_t __attribute__((unused)) *client,
-               size_t n, sc_data_t *data)
+               size_t n, sc_data_t *data,
+               sc_object_t __attribute__((unused)) *user_data)
 {
        sc_host_t host = SC_HOST_INIT;
 
@@ -112,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,
+                               sc_puppet_stcfg_get_data, NULL, /* #columns = */ 2,
                                /* col types = */ SC_TYPE_STRING, SC_TYPE_DATETIME)) {
                fprintf(stderr, "puppet storeconfigs backend: Failed to retrieve "
                                "hosts from the storeconfigs DB.\n");
index 0f2a157c28dca42ab965a075e77a126bd94cb7ee..b910b977551558e97dc77105b631acc70412044d 100644 (file)
@@ -28,6 +28,7 @@
 #ifndef SC_UTILS_DBI_H
 #define SC_UTILS_DBI_H 1
 
+#include "core/object.h"
 #include "utils/data.h"
 
 #include <stddef.h>
@@ -55,7 +56,8 @@ 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_data_t *);
+typedef int (*sc_dbi_data_cb)(sc_dbi_client_t *,
+               size_t, sc_data_t *, sc_object_t *);
 
 /*
  * sc_dbi_options_t:
@@ -133,7 +135,7 @@ sc_dbi_client_connect(sc_dbi_client_t *client);
  */
 int
 sc_dbi_exec_query(sc_dbi_client_t *client, const char *query,
-               sc_dbi_data_cb callback, int n, ...);
+               sc_dbi_data_cb callback, sc_object_t *user_data, int n, ...);
 
 /*
  * sc_dbi_client_destroy:
index f1f7691a9a0490d6c2107871f8343ac284272806..31a423efb9907597308e4c691c38998b8219da65 100644 (file)
@@ -28,6 +28,7 @@
 #ifndef SC_UTILS_UNIXSOCK_H
 #define SC_UTILS_UNIXSOCK_H 1
 
+#include "core/object.h"
 #include "utils/data.h"
 
 #include <sys/socket.h>
@@ -42,7 +43,7 @@ struct sc_unixsock_client;
 typedef struct sc_unixsock_client sc_unixsock_client_t;
 
 typedef int (*sc_unixsock_client_data_cb)(sc_unixsock_client_t *,
-               size_t, sc_data_t *);
+               size_t, sc_data_t *, sc_object_t *);
 
 sc_unixsock_client_t *
 sc_unixsock_client_create(const char *path);
@@ -73,8 +74,8 @@ sc_unixsock_client_recv(sc_unixsock_client_t *client, char *buffer, size_t bufle
  */
 int
 sc_unixsock_client_process_lines(sc_unixsock_client_t *client,
-               sc_unixsock_client_data_cb callback, long int max_lines,
-               const char *delim, int n_cols, ...);
+               sc_unixsock_client_data_cb callback, sc_object_t *user_data,
+               long int max_lines, const char *delim, int n_cols, ...);
 
 /*
  * sc_unixsock_client_shutdown:
index bd58ce7b4b9d12d7a6226da3dc061e2582f0712d..d7398de15821725af5420ce5e7de8a422bf24c1f 100644 (file)
@@ -112,7 +112,8 @@ sc_dbi_get_field(dbi_result res, unsigned int i,
 
 static int
 sc_dbi_get_data(sc_dbi_client_t *client, dbi_result res,
-               unsigned int num_fields, sc_dbi_data_cb callback)
+               unsigned int num_fields, sc_dbi_data_cb callback,
+               sc_object_t *user_data)
 {
        sc_data_t data[num_fields];
        int types[num_fields];
@@ -150,7 +151,7 @@ sc_dbi_get_data(sc_dbi_client_t *client, dbi_result res,
                                                types[i], &data[i]))
                                continue;
 
-               if (callback(client, num_fields, data))
+               if (callback(client, num_fields, data, user_data))
                        continue;
 
                ++success;
@@ -347,7 +348,7 @@ sc_dbi_client_connect(sc_dbi_client_t *client)
 
 int
 sc_dbi_exec_query(sc_dbi_client_t *client, const char *query,
-               sc_dbi_data_cb callback, int n, ...)
+               sc_dbi_data_cb callback, sc_object_t *user_data, int n, ...)
 {
        dbi_result res;
        unsigned int num_fields;
@@ -424,7 +425,7 @@ sc_dbi_exec_query(sc_dbi_client_t *client, const char *query,
                return 0;
        }
 
-       status = sc_dbi_get_data(client, res, num_fields, callback);
+       status = sc_dbi_get_data(client, res, num_fields, callback, user_data);
 
        dbi_result_free(res);
        return status;
index db025f80f9f654ffc70d528c8b870b3c62b97768..da2ca1e4a71928ab3c356d7df506d7261fab6310 100644 (file)
@@ -143,7 +143,8 @@ sc_unixsock_parse_cell(char *string, int type, sc_data_t *data)
 static int
 sc_unixsock_client_process_one_line(sc_unixsock_client_t *client,
                char *line, sc_unixsock_client_data_cb callback,
-               const char *delim, int column_count, int *types)
+               sc_object_t *user_data, const char *delim,
+               int column_count, int *types)
 {
        sc_data_t data[column_count];
        char *orig_line = line;
@@ -179,7 +180,7 @@ sc_unixsock_client_process_one_line(sc_unixsock_client_t *client,
                line = next;
        }
 
-       if (callback(client, (size_t)column_count, data))
+       if (callback(client, (size_t)column_count, data, user_data))
                return -1;
        return 0;
 } /* sc_unixsock_client_process_one_line */
@@ -310,8 +311,8 @@ sc_unixsock_client_recv(sc_unixsock_client_t *client, char *buffer, size_t bufle
 
 int
 sc_unixsock_client_process_lines(sc_unixsock_client_t *client,
-               sc_unixsock_client_data_cb callback, long int max_lines,
-               const char *delim, int n_cols, ...)
+               sc_unixsock_client_data_cb callback, sc_object_t *user_data,
+               long int max_lines, const char *delim, int n_cols, ...)
 {
        int *types = NULL;
        int success = 0;
@@ -377,7 +378,7 @@ sc_unixsock_client_process_lines(sc_unixsock_client_t *client,
                        continue;
 
                if (! sc_unixsock_client_process_one_line(client, line, callback,
-                                       delim, column_count, types))
+                                       user_data, delim, column_count, types))
                        ++success;
        }