Code

core/data: Make string and binary data not constant.
authorSebastian Harl <sh@tokkee.org>
Tue, 28 Jan 2014 19:12:22 +0000 (20:12 +0100)
committerSebastian Harl <sh@tokkee.org>
Tue, 28 Jan 2014 19:12:22 +0000 (20:12 +0100)
This will allow for more flexibility. However, it also requires to create a
copy of data queried using libdbi.

src/include/core/data.h
src/utils/dbi.c
src/utils/unixsock.c

index 4a4dd2d3385e5f2fbf9e17fbd39cf0e2161772b4..b657486b794b16cdb9bc00560cb0b2f4ab2090dc 100644 (file)
@@ -58,11 +58,11 @@ typedef struct {
        union {
                int64_t     integer;  /* SDB_TYPE_INTEGER */
                double      decimal;  /* SDB_TYPE_DECIMAL */
-               const char *string;   /* SDB_TYPE_STRING  */
+               char       *string;   /* SDB_TYPE_STRING  */
                sdb_time_t  datetime; /* SDB_TYPE_DATETIME */
                struct {
                        size_t length;
-                       const unsigned char *datum;
+                       unsigned char *datum;
                } binary;             /* SDB_TYPE_BINARY */
        } data;
 } sdb_data_t;
index 1de2f1abe678b74dae092b4b51b40a888b68ceb0..c8eb81616454a3078eb768f16b1affd32b81f1d9 100644 (file)
@@ -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,7 +96,7 @@ 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;
                        }
@@ -141,6 +141,8 @@ 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, sdb_dbi_strerror(client->conn));
@@ -152,7 +154,16 @@ 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) {
+                       if ((data[i].type == SDB_TYPE_STRING) && (data[i].data.string))
+                               free(data[i].data.string);
+                       else if ((data[i].type == SDB_TYPE_BINARY)
+                                       && (data[i].data.binary.datum))
+                               free(data[i].data.binary.datum);
+               }
+
+               if (status)
                        continue;
 
                ++success;
index 62b6f45aa404f360ed1f623c07362dcb2c2e26b0..9a5c3fab8c91de6c510a99d8740e4ae2301e0a01 100644 (file)
@@ -113,7 +113,7 @@ sdb_unixsock_parse_cell(char *string, int type, sdb_data_t *data)
                case SDB_TYPE_BINARY:
                        /* we don't support any binary information containing 0-bytes */
                        data->data.binary.length = strlen(string);
-                       data->data.binary.datum = (const unsigned char *)string;
+                       data->data.binary.datum = (unsigned char *)string;
                        break;
                default:
                        sdb_log(SDB_LOG_ERR, "unixsock: Unexpected type %i while "