Code

SSL utils: Add sdb_ssl_free_options().
[sysdb.git] / src / utils / dbi.c
index de2348e713d34e82924bbf4870d252d186626803..261d3b249e5265b089db34f9e4c54e64de34d017 100644 (file)
 #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
  */
@@ -138,7 +194,7 @@ sdb_dbi_get_data(sdb_dbi_client_t *client, dbi_result res,
                                        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);
@@ -323,8 +379,7 @@ sdb_dbi_client_connect(sdb_dbi_client_t *client)
 
        client->conn = dbi_conn_open(driver);
        if (! client->conn) {
-               sdb_log(SDB_LOG_ERR, "dbi: failed to open connection "
-                               "object.");
+               sdb_log(SDB_LOG_ERR, "dbi: failed to open connection object.");
                return -1;
        }
 
@@ -441,7 +496,7 @@ sdb_dbi_exec_query(sdb_dbi_client_t *client, const char *query,
 
                        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) {
@@ -490,7 +545,9 @@ sdb_dbi_client_destroy(sdb_dbi_client_t *client)
                dbi_conn_close(client->conn);
        client->conn = NULL;
 
-       dbi_shutdown_r(client->inst);
+       if (client->inst)
+               dbi_shutdown_r(client->inst);
+       client->inst = NULL;
 
        if (client->options)
                sdb_dbi_options_destroy(client->options);