From: Sebastian Harl Date: Wed, 12 Dec 2012 05:15:11 +0000 (+0100) Subject: utils dbi + unixsock: Pass user-data object to data processing callbacks. X-Git-Tag: sysdb-0.1.0~453 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=170488932e1d1d77971095c4edda3969ee619d07;p=sysdb.git utils dbi + unixsock: Pass user-data object to data processing callbacks. This may be used to access, for example, state information throughout each call. --- diff --git a/src/backend/puppet-storeconfigs.c b/src/backend/puppet-storeconfigs.c index 6cc68d3..4397e87 100644 --- a/src/backend/puppet-storeconfigs.c +++ b/src/backend/puppet-storeconfigs.c @@ -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"); diff --git a/src/include/utils/dbi.h b/src/include/utils/dbi.h index 0f2a157..b910b97 100644 --- a/src/include/utils/dbi.h +++ b/src/include/utils/dbi.h @@ -28,6 +28,7 @@ #ifndef SC_UTILS_DBI_H #define SC_UTILS_DBI_H 1 +#include "core/object.h" #include "utils/data.h" #include @@ -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: diff --git a/src/include/utils/unixsock.h b/src/include/utils/unixsock.h index f1f7691..31a423e 100644 --- a/src/include/utils/unixsock.h +++ b/src/include/utils/unixsock.h @@ -28,6 +28,7 @@ #ifndef SC_UTILS_UNIXSOCK_H #define SC_UTILS_UNIXSOCK_H 1 +#include "core/object.h" #include "utils/data.h" #include @@ -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: diff --git a/src/utils/dbi.c b/src/utils/dbi.c index bd58ce7..d7398de 100644 --- a/src/utils/dbi.c +++ b/src/utils/dbi.c @@ -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; diff --git a/src/utils/unixsock.c b/src/utils/unixsock.c index db025f8..da2ca1e 100644 --- a/src/utils/unixsock.c +++ b/src/utils/unixsock.c @@ -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; }