X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Futils%2Fdbi.c;h=a30f548db2c6661242970d5db6c639b93aa480c8;hb=9ee6c7fc9932e3fcb6d8aebe5364b1da3116af05;hp=60b47c46faeaa840beaac97519b81a09dcbcfb41;hpb=848efadda124a4778f08a84a082ff2436504d22c;p=sysdb.git diff --git a/src/utils/dbi.c b/src/utils/dbi.c index 60b47c4..a30f548 100644 --- a/src/utils/dbi.c +++ b/src/utils/dbi.c @@ -25,8 +25,8 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "utils/error.h" #include "utils/dbi.h" +#include "utils/error.h" #include @@ -84,7 +84,7 @@ sdb_dbi_get_field(dbi_result res, unsigned int i, 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: { @@ -96,14 +96,14 @@ sdb_dbi_get_field(dbi_result res, unsigned int i, 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: sdb_log(SDB_LOG_ERR, "dbi: Unexpected type %i while " - "parsing query result.\n", type); + "parsing query result.", type); return -1; } @@ -129,7 +129,7 @@ sdb_dbi_get_data(sdb_dbi_client_t *client, dbi_result res, for (i = 0; i < num_fields; ++i) { types[i] = dbi_result_get_field_type_idx(res, i + 1); if (types[i] == DBI_TYPE_ERROR) { - sdb_log(SDB_LOG_ERR, "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; } @@ -141,9 +141,11 @@ sdb_dbi_get_data(sdb_dbi_client_t *client, dbi_result res, return -1; for (n = 0; n < num_rows; ++n) { + int status; + if (! dbi_result_seek_row(res, n + 1)) { - sdb_log(SDB_LOG_ERR, "dbi: Failed to retrieve row %llu: " - "%s\n", n, sdb_dbi_strerror(client->conn)); + sdb_log(SDB_LOG_ERR, "dbi: Failed to retrieve row %llu: %s", + n, sdb_dbi_strerror(client->conn)); continue; } @@ -152,7 +154,11 @@ sdb_dbi_get_data(sdb_dbi_client_t *client, dbi_result res, 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; @@ -172,7 +178,7 @@ sdb_dbi_options_create(void) { sdb_dbi_options_t *options; - options = malloc(sizeof(options)); + options = malloc(sizeof(*options)); if (! options) return NULL; @@ -284,8 +290,10 @@ sdb_dbi_client_connect(sdb_dbi_client_t *client) 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); if (! driver) { @@ -298,6 +306,7 @@ sdb_dbi_client_connect(sdb_dbi_client_t *client) driver = dbi_driver_list(driver)) { sdb_error_append("\t- %s\n", dbi_driver_get_name(driver)); } + sdb_error_chomp(); sdb_error_log(SDB_LOG_ERR); return -1; } @@ -305,7 +314,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.\n"); + "object."); return -1; } @@ -319,32 +328,36 @@ sdb_dbi_client_connect(sdb_dbi_client_t *client) continue; /* else: error */ - sdb_error_set("dbi: failed to set option '%s': " - "%s\n", client->options->options[i].key, + sdb_error_set("dbi: failed to set option '%s': %s\n", + client->options->options[i].key, sdb_dbi_strerror(client->conn)); 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)) 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)) { - sdb_log(SDB_LOG_ERR, "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) { - sdb_log(SDB_LOG_ERR, "dbi: failed to connect to database '%s': " - "%s\n", client->database, sdb_dbi_strerror(client->conn)); + 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; @@ -378,14 +391,14 @@ sdb_dbi_exec_query(sdb_dbi_client_t *client, const char *query, res = dbi_conn_query(client->conn, query); if (! res) { - sdb_log(SDB_LOG_ERR, "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) { sdb_log(SDB_LOG_ERR, "dbi: failed to fetch rows for query " - "'%s': %s\n", query, sdb_dbi_strerror(client->conn)); + "'%s': %s", query, sdb_dbi_strerror(client->conn)); dbi_result_free(res); return -1; } @@ -404,7 +417,7 @@ sdb_dbi_exec_query(sdb_dbi_client_t *client, const char *query, if (n != (int)num_fields) { sdb_log(SDB_LOG_ERR, "dbi: number of returned fields (%i) " "does not match the number of requested fields (%i) " - "for query '%s'.\n", num_fields, n, query); + "for query '%s'.", num_fields, n, query); dbi_result_free(res); return -1; } @@ -423,7 +436,7 @@ sdb_dbi_exec_query(sdb_dbi_client_t *client, const char *query, /* column count starts at 1 */ if ((unsigned int)field_type != type) { sdb_log(SDB_LOG_ERR, "dbi: type of column '%s' (%u) " - "does not match requested type (%u).\n", + "does not match requested type (%u).", dbi_result_get_field_name(res, (unsigned int)i + 1), field_type, type); status = -1; @@ -465,6 +478,7 @@ sdb_dbi_client_destroy(sdb_dbi_client_t *client) if (client->conn) dbi_conn_close(client->conn); + client->conn = NULL; if (client->options) sdb_dbi_options_destroy(client->options);