diff --git a/src/utils/dbi.c b/src/utils/dbi.c
index ea846b0006c545e1a3e25b4196cd1bc23f5b5904..73fc6b77a4e2582b431ac109a1e9147f66535360 100644 (file)
--- a/src/utils/dbi.c
+++ b/src/utils/dbi.c
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif /* HAVE_CONFIG_H */
+
#include "utils/dbi.h"
+#include "utils/error.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
+/*
+ * pre 0.9 DBI compat layer
+ */
+
+#if LIBDBI_VERSION < 900
+#include <pthread.h>
+
+typedef void *dbi_inst;
+
+static pthread_mutex_t dbi_lock = PTHREAD_MUTEX_INITIALIZER;
+static int dbi_initialized = 0;
+
+static int
+initialize_r(const char *driverdir, dbi_inst __attribute__((unused)) *pInst)
+{
+ int status = 0;
+ pthread_mutex_lock(&dbi_lock);
+ if (! dbi_initialized)
+ status = dbi_initialize(driverdir);
+ dbi_initialized = 1;
+ pthread_mutex_unlock(&dbi_lock);
+ return status;
+} /* initialize_r */
+
+static dbi_driver
+driver_open_r(const char *name, dbi_inst __attribute__((unused)) Inst)
+{
+ dbi_driver driver;
+ pthread_mutex_lock(&dbi_lock);
+ driver = dbi_driver_open(name);
+ pthread_mutex_unlock(&dbi_lock);
+ return driver;
+} /* driver_open_r */
+
+static dbi_driver
+driver_list_r(dbi_driver Current, dbi_inst __attribute__((unused)) Inst)
+{
+ dbi_driver driver;
+ pthread_mutex_lock(&dbi_lock);
+ driver = dbi_driver_list(Current);
+ pthread_mutex_unlock(&dbi_lock);
+ return driver;
+} /* driver_list_r */
+
+static void
+shutdown_r(dbi_inst __attribute__((unused)) Inst)
+{
+ /* do nothing; we don't know if DBI is still in use */
+} /* shutdown_r */
+
+#define dbi_initialize_r initialize_r
+#define dbi_driver_open_r driver_open_r
+#define dbi_driver_list_r driver_list_r
+#define dbi_shutdown_r shutdown_r
+#endif /* LIBDBI_VERSION < 900 */
+
/*
* private data types
*/
char *database;
dbi_conn conn;
+ dbi_inst inst;
sdb_dbi_options_t *options;
};
data->data.decimal = dbi_result_get_double_idx(res, i);
break;
case SDB_TYPE_STRING:
- data->data.string = dbi_result_get_string_idx(res, i);
+ data->data.string = dbi_result_get_string_copy_idx(res, i);
break;
case SDB_TYPE_DATETIME:
{
case SDB_TYPE_BINARY:
{
size_t length = dbi_result_get_field_length_idx(res, i);
- const unsigned char *datum = dbi_result_get_binary_idx(res, i);
+ unsigned char *datum = dbi_result_get_binary_copy_idx(res, i);
data->data.binary.length = length;
data->data.binary.datum = datum;
}
break;
default:
- fprintf(stderr, "dbi: Unexpected type %i while "
- "parsing query result.\n", type);
+ sdb_log(SDB_LOG_ERR, "dbi: Unexpected type %i while "
+ "parsing query result.", type);
return -1;
}
for (i = 0; i < num_fields; ++i) {
types[i] = dbi_result_get_field_type_idx(res, i + 1);
if (types[i] == DBI_TYPE_ERROR) {
- fprintf(stderr, "dbi: failed to fetch data: %s\n",
+ sdb_log(SDB_LOG_ERR, "dbi: failed to fetch data: %s",
sdb_dbi_strerror(client->conn));
return -1;
}
- types[i] = DBI_TYPE_TO_SC(types[i]);
+ types[i] = DBI_TYPE_TO_SDB(types[i]);
}
num_rows = dbi_result_get_numrows(res);
return -1;
for (n = 0; n < num_rows; ++n) {
+ int status;
+
if (! dbi_result_seek_row(res, n + 1)) {
- fprintf(stderr, "dbi: Failed to retrieve row %llu: %s\n",
+ sdb_log(SDB_LOG_ERR, "dbi: Failed to retrieve row %llu: %s",
n, sdb_dbi_strerror(client->conn));
continue;
}
types[i], &data[i]))
continue;
- if (callback(client, num_fields, data, user_data))
+ status = callback(client, num_fields, data, user_data);
+ for (i = 0; i < num_fields; ++i)
+ sdb_data_free_datum(&data[i]);
+
+ if (status)
continue;
++success;
{
sdb_dbi_options_t *options;
- options = malloc(sizeof(options));
+ options = malloc(sizeof(*options));
if (! options)
return NULL;
client->conn = NULL;
client->options = NULL;
+ if (dbi_initialize_r(/* driverdir = */ NULL, &client->inst) < 0) {
+ free(client);
+ return NULL;
+ }
+
client->driver = strdup(driver);
client->database = strdup(database);
if ((! client->driver) || (! client->database)) {
if ((! client) || (! client->driver) || (! client->database))
return -1;
- if (client->conn)
+ if (client->conn) {
dbi_conn_close(client->conn);
+ client->conn = NULL;
+ }
- driver = dbi_driver_open(client->driver);
+ driver = dbi_driver_open_r(client->driver, client->inst);
if (! driver) {
- fprintf(stderr, "dbi: failed to open DBI driver '%s'; "
+ sdb_error_set("dbi: failed to open DBI driver '%s'; "
"possibly it's not installed.\n",
client->driver);
- fprintf(stderr, "dbi: known drivers:\n");
- for (driver = dbi_driver_list(NULL); driver;
- driver = dbi_driver_list(driver)) {
- fprintf(stderr, "\t- %s\n", dbi_driver_get_name(driver));
+ sdb_error_append("dbi: known drivers:\n");
+ for (driver = dbi_driver_list_r(NULL, client->inst); driver;
+ driver = dbi_driver_list_r(driver, client->inst)) {
+ sdb_error_append("\t- %s\n", dbi_driver_get_name(driver));
}
+ sdb_error_chomp();
+ sdb_error_log(SDB_LOG_ERR);
return -1;
}
client->conn = dbi_conn_open(driver);
if (! client->conn) {
- fprintf(stderr, "dbi: failed to open connection object.\n");
+ sdb_log(SDB_LOG_ERR, "dbi: failed to open connection "
+ "object.");
return -1;
}
continue;
/* else: error */
- fprintf(stderr, "dbi: failed to set option '%s': %s\n",
+ sdb_error_set("dbi: failed to set option '%s': %s\n",
client->options->options[i].key,
sdb_dbi_strerror(client->conn));
- fprintf(stderr, "dbi: known driver options:\n");
+ sdb_error_append("dbi: known driver options:\n");
for (opt = dbi_conn_get_option_list(client->conn, NULL); opt;
opt = dbi_conn_get_option_list(client->conn, opt))
- fprintf(stderr, "\t- %s\n", opt);
+ sdb_error_append("\t- %s\n", opt);
+ sdb_error_chomp();
+ sdb_error_log(SDB_LOG_ERR);
dbi_conn_close(client->conn);
+ client->conn = NULL;
return -1;
}
}
if (dbi_conn_set_option(client->conn, "dbname", client->database)) {
- fprintf(stderr, "dbi: failed to set option 'dbname': %s\n",
+ sdb_log(SDB_LOG_ERR, "dbi: failed to set option 'dbname': %s",
sdb_dbi_strerror(client->conn));
dbi_conn_close(client->conn);
+ client->conn = NULL;
return -1;
}
if (dbi_conn_connect(client->conn) < 0) {
- fprintf(stderr, "dbi: failed to connect to database '%s': %s\n",
+ sdb_log(SDB_LOG_ERR, "dbi: failed to connect to database '%s': %s",
client->database, sdb_dbi_strerror(client->conn));
dbi_conn_close(client->conn);
+ client->conn = NULL;
return -1;
}
return 0;
} /* sdb_dbi_client_connect */
+int
+sdb_dbi_client_check_conn(sdb_dbi_client_t *client)
+{
+ if (! client)
+ return -1;
+
+ if (! client->conn)
+ return sdb_dbi_client_connect(client);
+
+ if (dbi_conn_ping(client->conn))
+ return 0;
+ return sdb_dbi_client_connect(client);
+} /* sdb_dbi_client_check_conn */
+
int
sdb_dbi_exec_query(sdb_dbi_client_t *client, const char *query,
sdb_dbi_data_cb callback, sdb_object_t *user_data, int n, ...)
res = dbi_conn_query(client->conn, query);
if (! res) {
- fprintf(stderr, "dbi: failed to execute query '%s': %s\n",
+ sdb_log(SDB_LOG_ERR, "dbi: failed to execute query '%s': %s",
query, sdb_dbi_strerror(client->conn));
return -1;
}
if (dbi_result_get_numrows(res) == DBI_ROW_ERROR) {
- fprintf(stderr, "dbi: failed to fetch rows for query '%s': %s\n",
- query, sdb_dbi_strerror(client->conn));
+ sdb_log(SDB_LOG_ERR, "dbi: failed to fetch rows for query "
+ "'%s': %s", query, sdb_dbi_strerror(client->conn));
dbi_result_free(res);
return -1;
}
int i;
if (n != (int)num_fields) {
- fprintf(stderr, "dbi: number of returned fields (%i) does not "
- "match the number of requested fields (%i) "
- "for query '%s'.\n", num_fields, n, query);
+ sdb_log(SDB_LOG_ERR, "dbi: number of returned fields (%i) "
+ "does not match the number of requested fields (%i) "
+ "for query '%s'.", num_fields, n, query);
dbi_result_free(res);
return -1;
}
unsigned int type = va_arg(types, unsigned int);
- field_type = DBI_TYPE_TO_SC(field_type);
+ field_type = DBI_TYPE_TO_SDB(field_type);
/* column count starts at 1 */
if ((unsigned int)field_type != type) {
- fprintf(stderr, "dbi: type of column '%s' (%u) does not match "
- "requested type (%u).\n",
+ sdb_log(SDB_LOG_ERR, "dbi: type of column '%s' (%u) "
+ "does not match requested type (%u).",
dbi_result_get_field_name(res, (unsigned int)i + 1),
field_type, type);
status = -1;
if (client->conn)
dbi_conn_close(client->conn);
+ client->conn = NULL;
+
+ dbi_shutdown_r(client->inst);
if (client->options)
sdb_dbi_options_destroy(client->options);