Code

data: Let CONCAT return NULL if any of its operands is NULL.
authorSebastian Harl <sh@tokkee.org>
Sun, 19 Oct 2014 12:00:41 +0000 (14:00 +0200)
committerSebastian Harl <sh@tokkee.org>
Sun, 19 Oct 2014 12:00:41 +0000 (14:00 +0200)
This mimics behavior in SQL and also allows for missing values to be reported
as NULL and propagate that information.

src/core/data.c
t/unit/core/data_test.c

index 557dfd6a5fb3011d276c22ccb31442edaaf59ff1..4840736896f55a03b4cf0716e18e3dec7cb9baec 100644 (file)
@@ -200,20 +200,21 @@ data_concat(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
        else
                return -1;
 
-       if (s1 || s2) {
+       if (s1 && s2) {
                new = malloc(len1 + len2 + 1);
                if (! new)
                        return -1;
-       }
-       else
-               new = NULL;
 
-       if (len1)
-               memcpy(new, s1, len1);
-       if (len2)
-               memcpy(new + len1, s2, len2);
-       if (new)
+               if (len1)
+                       memcpy(new, s1, len1);
+               if (len2)
+                       memcpy(new + len1, s2, len2);
                new[len1 + len2] = '\0';
+       }
+       else {
+               len1 = len2 = 0;
+               new = NULL;
+       }
 
        res->type = d1->type;
        if (res->type == SDB_TYPE_STRING) {
index a1b4caa59965c7b307dcf848d40f0011ea4ea1b8..2336c5e98d335714f510797c31f745126bb938ee 100644 (file)
@@ -694,7 +694,7 @@ START_TEST(test_expr_eval)
                        SDB_DATA_INIT,
                        SDB_DATA_INIT,
                        SDB_DATA_INIT,
-                       { SDB_TYPE_STRING, { .string = "" } },
+                       { SDB_TYPE_STRING, { .string = NULL } },
                },
                {
                        { SDB_TYPE_STRING, { .string = NULL } },
@@ -714,7 +714,7 @@ START_TEST(test_expr_eval)
                        SDB_DATA_INIT,
                        SDB_DATA_INIT,
                        SDB_DATA_INIT,
-                       { SDB_TYPE_STRING, { .string = "" } },
+                       { SDB_TYPE_STRING, { .string = NULL } },
                },
                {
                        { SDB_TYPE_STRING, { .string = "a" } },
@@ -744,7 +744,7 @@ START_TEST(test_expr_eval)
                        SDB_DATA_INIT,
                        SDB_DATA_INIT,
                        SDB_DATA_INIT,
-                       { SDB_TYPE_BINARY, { .binary = { 1, (unsigned char *)"a" } } },
+                       { SDB_TYPE_BINARY, { .binary = { 0, NULL } } },
                },
                {
                        { SDB_TYPE_BINARY, { .binary = { 0, NULL } } },
@@ -764,7 +764,7 @@ START_TEST(test_expr_eval)
                        SDB_DATA_INIT,
                        SDB_DATA_INIT,
                        SDB_DATA_INIT,
-                       { SDB_TYPE_BINARY, { .binary = { 1, (unsigned char *)"a" } } },
+                       { SDB_TYPE_BINARY, { .binary = { 0, NULL } } },
                },
                {
                        {