X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fcore%2Fdata.c;h=88c7f8246953d58f1af76a01a04e1919eb96f8c2;hb=30b45eeb6c56e6ffca79910c9dd67977e2a41c6d;hp=1baeadafc2dfb79e65c933964e3111f157f36cca;hpb=3d8d3baf2658eec2ccb96fbe666ef7cdfc2a4376;p=sysdb.git diff --git a/src/core/data.c b/src/core/data.c index 1baeada..88c7f82 100644 --- a/src/core/data.c +++ b/src/core/data.c @@ -46,6 +46,77 @@ #include +/* + * Operator support maxtrix. + * -> op_matrix[][][] + */ + +/* add, sub, mul, div, mod, concat */ + +/* integer, decimal, string, datetime, binary, regex */ + +static int op_matrix[6][6][6] = { + /* SDB_DATA_ADD */ + { + { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 }, + { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + }, + + /* SDB_DATA_SUB */ + { + { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 }, + { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + }, + + /* SDB_DATA_MUL */ + { + { SDB_TYPE_INTEGER, -1, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, SDB_TYPE_DECIMAL, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + }, + + /* SDB_DATA_DIV */ + { + { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 }, + { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + }, + + /* SDB_DATA_MOD */ + { + { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 }, + { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + }, + + /* SDB_DATA_CONCAT */ + { + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, SDB_TYPE_STRING, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, SDB_TYPE_BINARY, -1 }, + { -1, -1, -1, -1, -1, -1 }, + }, +}; + /* * private helper functions */ @@ -97,6 +168,52 @@ free_array_values(sdb_data_t *datum) } } /* free_array_values */ +/* compare two arrays element-by-element returning how the first non-equal + * elements compare to each other */ +static int +array_cmp(const sdb_data_t *a1, const sdb_data_t *a2) +{ + int type = a1->type & 0xff; + size_t len, i; + + assert((a1->type == a2->type) && (a1->type & SDB_TYPE_ARRAY)); + + len = SDB_MIN(a1->data.array.length, a2->data.array.length); + + if (type == SDB_TYPE_INTEGER) { + int64_t *v1 = a1->data.array.values; + int64_t *v2 = a2->data.array.values; + + for (i = 0; i < len; ++i) + if (v1[i] != v2[i]) + return SDB_CMP(v1[i], v2[i]); + } + else if (type == SDB_TYPE_DECIMAL) { + double *v1 = a1->data.array.values; + double *v2 = a2->data.array.values; + + for (i = 0; i < len; ++i) + if (v1[i] != v2[i]) + return SDB_CMP(v1[i], v2[i]); + } + else if (type == SDB_TYPE_STRING) { + char **v1 = a1->data.array.values; + char **v2 = a2->data.array.values; + + for (i = 0; i < len; ++i) { + int diff = strcasecmp(v1[i], v2[i]); + if (diff) + return diff; + } + } + else { + /* TODO */ + errno = ENOTSUP; + /* but fall through to ensure stable sorting: */ + } + return SDB_CMP(a1->data.array.length, a2->data.array.length); +} /* array_cmp */ + /* Calculate the linear function 'd1 + n * d2'. */ static int data_lin(const sdb_data_t *d1, int n, const sdb_data_t *d2, sdb_data_t *res) @@ -228,18 +345,41 @@ static int data_concat(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res) { unsigned char *new; - unsigned char *s1, *s2; - size_t len1, len2; + const unsigned char *s1, *s2; + size_t len1, len2, array1_len = 0, array2_len = 0; - /* TODO: support array plus element */ - if (d1->type != d2->type) + if ((d1->type & 0xff) != (d2->type & 0xff)) return -1; - if (d1->type == SDB_TYPE_STRING) { + if ((d1->type & SDB_TYPE_ARRAY) || (d2->type & SDB_TYPE_ARRAY)) { + size_t elem_size = sdb_data_sizeof(d1->type & 0xff); + if (d1->type & SDB_TYPE_ARRAY) { + s1 = (const unsigned char *)d1->data.array.values; + array1_len = d1->data.array.length; + } + else { + /* As per C99, section 6.7.2.1, paragraph 14: + * "A pointer to a union object, suitably converted, points to + * each of its members" */ + s1 = (const unsigned char *)&d1->data; + array1_len = 1; + } + if (d2->type & SDB_TYPE_ARRAY) { + s2 = (const unsigned char *)d2->data.array.values; + array2_len = d2->data.array.length; + } + else { + s2 = (const unsigned char *)&d2->data; + array2_len = 1; + } + len1 = array1_len * elem_size; + len2 = array2_len * elem_size; + } + else if (d1->type == SDB_TYPE_STRING) { s1 = (unsigned char *)d1->data.string; s2 = (unsigned char *)d2->data.string; - len1 = s1 ? strlen((char *)s1) : 0; - len2 = s2 ? strlen((char *)s2) : 0; + len1 = s1 ? strlen((const char *)s1) : 0; + len2 = s2 ? strlen((const char *)s2) : 0; } else if (d1->type == SDB_TYPE_BINARY) { s1 = d1->data.binary.datum; @@ -247,13 +387,6 @@ data_concat(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res) len1 = d1->data.binary.length; len2 = d2->data.binary.length; } - else if (d1->type & SDB_TYPE_ARRAY) { - size_t elem_size = sdb_data_sizeof(d1->type & 0xff); - s1 = (unsigned char *)d1->data.array.values; - s2 = (unsigned char *)d2->data.array.values; - len1 = d1->data.array.length * elem_size; - len2 = d2->data.array.length * elem_size; - } else return -1; @@ -269,7 +402,9 @@ data_concat(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res) memcpy(new + len1, s2, len2); new[len1 + len2] = '\0'; - res->type = d1->type; + /* element types match and if either datum is an array, + * the result is an array as well */ + res->type = d1->type | d2->type; if (res->type == SDB_TYPE_STRING) { res->data.string = (char *)new; } @@ -277,9 +412,9 @@ data_concat(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res) res->data.binary.datum = new; res->data.binary.length = len1 + len2; } - else if (d1->type & SDB_TYPE_ARRAY) { + else if (res->type & SDB_TYPE_ARRAY) { res->data.array.values = new; - res->data.array.length = d1->data.array.length + d2->data.array.length; + res->data.array.length = array1_len + array2_len; if (copy_array_values(res, res, sdb_data_sizeof(res->type & 0xff))) { /* this leaks already copied values but there's not much we can * do and this should only happen if we're in trouble anyway */ @@ -443,9 +578,8 @@ sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2) return strcmp(d1->data.re.raw, d2->data.re.raw); } else if (d1->type & SDB_TYPE_ARRAY) { - /* TODO */ - errno = ENOTSUP; - return -1; + CMP_NULL(d1->data.array.values, d2->data.array.values); + return array_cmp(d1, d2); } return -1; } /* sdb_data_cmp */ @@ -491,11 +625,103 @@ sdb_data_isnull(const sdb_data_t *datum) return 1; if ((datum->type == SDB_TYPE_REGEX) && (! datum->data.re.raw)) return 1; - if ((datum->type & SDB_TYPE_ARRAY) && (! datum->data.array.values)) - return 1; return 0; } /* sdb_data_isnull */ +_Bool +sdb_data_inarray(const sdb_data_t *value, const sdb_data_t *array) +{ + const void *values; + size_t length, i; + int type = value->type & 0xff; + + if (sdb_data_isnull(value) || sdb_data_isnull(array)) + return 0; + if (! (array->type & SDB_TYPE_ARRAY)) + return 0; + if ((value->type & 0xff) != (array->type & 0xff)) + return 0; + + if (value->type & SDB_TYPE_ARRAY) { + values = value->data.array.values; + length = value->data.array.length; + } + else { + values = &value->data; + length = 1; + } + + for (i = 0; i < length; ++i) { + size_t j; + + if (type == SDB_TYPE_INTEGER) { + int64_t *v = array->data.array.values; + for (j = 0; j < array->data.array.length; ++j) + if (((const int64_t *)values)[i] == v[j]) + break; + } + else if (type == SDB_TYPE_DECIMAL) { + double *v = array->data.array.values; + for (j = 0; j < array->data.array.length; ++j) + if (((const double *)values)[i] == v[j]) + break; + } + else if (type == SDB_TYPE_STRING) { + char **v = array->data.array.values; + for (j = 0; j < array->data.array.length; ++j) + if (!strcasecmp(((const char * const*)values)[i], v[j])) + break; + } + else { + /* TODO */ + errno = ENOTSUP; + return 0; + } + + if (j >= array->data.array.length) + /* value not found */ + return 0; + } + return 1; +} /* sdb_data_inarray */ + +int +sdb_data_array_get(const sdb_data_t *array, size_t i, sdb_data_t *value) +{ + sdb_data_t tmp = SDB_DATA_INIT; + int type; + + if ((! array) || (! (array->type & SDB_TYPE_ARRAY))) + return -1; + if (i >= array->data.array.length) + return -1; + + type = array->type & 0xff; + if (type == SDB_TYPE_INTEGER) { + int64_t *v = array->data.array.values; + tmp.data.integer = v[i]; + } + else if (type == SDB_TYPE_DECIMAL) { + double *v = array->data.array.values; + tmp.data.decimal = v[i]; + } + else if (type == SDB_TYPE_STRING) { + char **v = array->data.array.values; + tmp.data.string = v[i]; + } + else { + /* TODO */ + errno = ENOTSUP; + return -1; + } + + if (value) { + *value = tmp; + value->type = type; + } + return 0; +} /* sdb_data_array_get */ + int sdb_data_parse_op(const char *op) { @@ -525,22 +751,42 @@ sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2, return 0; } switch (op) { - case SDB_DATA_CONCAT: - return data_concat(d1, d2, res); - case SDB_DATA_ADD: - return data_lin(d1, 1, d2, res); - case SDB_DATA_SUB: - return data_lin(d1, -1, d2, res); - case SDB_DATA_MUL: - return data_mul(d1, d2, res); - case SDB_DATA_DIV: - return data_div(d1, d2, res, NULL); - case SDB_DATA_MOD: - return data_div(d1, d2, NULL, res); + case SDB_DATA_CONCAT: return data_concat(d1, d2, res); + case SDB_DATA_ADD: return data_lin(d1, 1, d2, res); + case SDB_DATA_SUB: return data_lin(d1, -1, d2, res); + case SDB_DATA_MUL: return data_mul(d1, d2, res); + case SDB_DATA_DIV: return data_div(d1, d2, res, NULL); + case SDB_DATA_MOD: return data_div(d1, d2, NULL, res); } return -1; } /* sdb_data_expr_eval */ +int +sdb_data_expr_type(int op, int type1, int type2) +{ + int types_num = (int)SDB_STATIC_ARRAY_LEN(op_matrix[0]); + + assert(SDB_STATIC_ARRAY_LEN(op_matrix[0]) + == SDB_STATIC_ARRAY_LEN(op_matrix[0][0])); + + if ((op <= 0) || (SDB_STATIC_ARRAY_LEN(op_matrix) < (size_t)op)) + return -1; + + /* arrays only support concat; element type has to match */ + if ((type1 & SDB_TYPE_ARRAY) || (type2 & SDB_TYPE_ARRAY)) { + if (((type1 & 0xff) != (type2 & 0xff)) || (op != SDB_DATA_CONCAT)) + return -1; + return type1 | SDB_TYPE_ARRAY; + } + if ((type1 < 0) || (types_num < type1) + || (type2 < 0) || (types_num < type2)) + return -1; + + if ((type1 == SDB_TYPE_NULL) || (type2 == SDB_TYPE_NULL)) + return SDB_TYPE_NULL; + return op_matrix[op - 1][type1 - 1][type2 - 1]; +} /* sdb_data_expr_type */ + size_t sdb_data_strlen(const sdb_data_t *datum) { @@ -557,7 +803,7 @@ sdb_data_strlen(const sdb_data_t *datum) } else if (datum->type == SDB_TYPE_STRING) { if (! datum->data.string) - return 8; /* "" */ + return 6; /* NULL */ /* in the worst case, each character needs to be escaped */ return 2 * strlen(datum->data.string) + 2; } @@ -567,20 +813,25 @@ sdb_data_strlen(const sdb_data_t *datum) } else if (datum->type == SDB_TYPE_BINARY) { if (! datum->data.binary.datum) - return 8; /* "" */ + return 6; /* NULL */ /* "\xNN" */ return 4 * datum->data.binary.length + 2; } else if (datum->type == SDB_TYPE_REGEX) { if (! datum->data.re.raw) - return 8; /* "" */ + return 6; /* NULL */ /* "/.../" */ return strlen(datum->data.re.raw) + 4; } else if (datum->type & SDB_TYPE_ARRAY) { - /* TODO */ - errno = ENOTSUP; - return 0; + size_t len = 2; /* [] */ + size_t i; + for (i = 0; i < datum->data.array.length; ++i) { + sdb_data_t v = SDB_DATA_INIT; + sdb_data_array_get(datum, i, &v); + len += sdb_data_strlen(&v) + 1; + } + return len; } return 0; } /* sdb_data_strlen */ @@ -590,11 +841,12 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted) { char tmp[sdb_data_strlen(datum) + 1]; char *data = NULL; + _Bool is_null = 0; int ret = -1; size_t i, pos; - if ((! datum) || (! buf)) + if ((! datum) || (! buf) || (! buflen)) return -1; if (datum->type == SDB_TYPE_INTEGER) { @@ -605,7 +857,7 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted) } else if (datum->type == SDB_TYPE_STRING) { if (! datum->data.string) - data = ""; + is_null = 1; else { pos = 0; for (i = 0; i < strlen(datum->data.string); ++i) { @@ -652,23 +904,51 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted) data = tmp; } else - data = ""; + is_null = 1; } else if (datum->type == SDB_TYPE_REGEX) { if (! datum->data.re.raw) - data = ""; + is_null = 1; else { snprintf(tmp, sizeof(tmp), "/%s/", datum->data.re.raw); data = tmp; } } else if (datum->type & SDB_TYPE_ARRAY) { - /* TODO */ - errno = ENOTSUP; - return -1; + ret = 1; + buf[0] = '['; + for (i = 0; i < datum->data.array.length; ++i) { + sdb_data_t v = SDB_DATA_INIT; + int n; + if ((size_t)ret >= buflen - 1) + break; + + if (ret > 1) { + buf[ret] = ','; + buf[ret + 1] = ' '; + ret += 2; + } + + sdb_data_array_get(datum, i, &v); + n = sdb_data_format(&v, buf + ret, buflen - ret, quoted); + if (n > 0) + ret += n; + else + break; + } + if ((size_t)ret < buflen - 1) { + buf[ret] = ']'; + buf[ret + 1] = '\0'; + ++ret; + } } - if (data) { + if (is_null) { + /* never quote NULL */ + strncpy(buf, "NULL", buflen); + ret = (int)SDB_MIN(buflen, 4); + } + else if (data) { if (quoted == SDB_UNQUOTED) ret = snprintf(buf, buflen, "%s", data); else if (quoted == SDB_SINGLE_QUOTED)