Code

store: Record the data-type of an expression.
authorSebastian Harl <sh@tokkee.org>
Fri, 24 Oct 2014 15:59:28 +0000 (17:59 +0200)
committerSebastian Harl <sh@tokkee.org>
Fri, 24 Oct 2014 15:59:28 +0000 (17:59 +0200)
src/core/store-private.h
src/core/store_expr.c
src/include/core/store.h

index 7ce807bfa3d9807111453c7f958a5dd994fbb72b..1e2c3ec5b4ef398be8a10f9420b26f53adac93b3 100644 (file)
@@ -117,6 +117,7 @@ struct sdb_store_expr {
        sdb_object_t super;
 
        int type; /* see above */
+       int data_type;
 
        sdb_store_expr_t *left;
        sdb_store_expr_t *right;
index ed0589bc28d1a37c32c6ea93400331410b01c532..d5b22c7e89b8c645c1da9f130e1435d90d023122 100644 (file)
@@ -75,6 +75,9 @@ expr_init(sdb_object_t *obj, va_list ap)
        expr->type  = type;
        expr->left  = left;
        expr->right = right;
+
+       /* unknown for now */
+       expr->data_type = -1;
        return 0;
 } /* expr_init */
 
@@ -103,29 +106,39 @@ sdb_store_expr_t *
 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right)
 {
        sdb_data_t value = SDB_DATA_INIT;
+       sdb_store_expr_t *e;
 
        if ((op < 0) || (SDB_DATA_CONCAT < op) || (! left) || (! right))
                return NULL;
 
-       if (left->type || right->type)
-               return SDB_STORE_EXPR(sdb_object_create("store-expr", expr_type,
+       if (left->type || right->type) {
+               e = SDB_STORE_EXPR(sdb_object_create("store-expr", expr_type,
                                        op, left, right, NULL));
+               e->data_type = sdb_data_expr_type(op, left->type, right->type);
+               return e;
+       }
        /* else: both expressions are constant values; evaluate now */
 
        if (sdb_data_expr_eval(op, &left->data, &right->data, &value))
                return NULL;
-       return SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type,
+       e = SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type,
                                0, NULL, NULL, &value));
+       e->data_type = value.type;
+       return e;
 } /* sdb_store_expr_create */
 
 sdb_store_expr_t *
 sdb_store_expr_fieldvalue(int field)
 {
        sdb_data_t value = { SDB_TYPE_INTEGER, { .integer = field } };
+       sdb_store_expr_t *e;
+
        if ((field < 0) || (SDB_FIELD_BACKEND < field))
                return NULL;
-       return SDB_STORE_EXPR(sdb_object_create("store-fieldvalue", expr_type,
+       e = SDB_STORE_EXPR(sdb_object_create("store-fieldvalue", expr_type,
                                FIELD_VALUE, NULL, NULL, &value));
+       e->data_type = SDB_FIELD_TYPE(field);
+       return e;
 } /* sdb_store_expr_fieldvalue */
 
 sdb_store_expr_t *
@@ -142,6 +155,7 @@ sdb_store_expr_attrvalue(const char *name)
                                ATTR_VALUE, NULL, NULL, &value));
        if (! expr)
                free(value.data.string);
+       expr->data_type = -1;
        return expr;
 } /* sdb_store_expr_attrvalue */
 
@@ -149,10 +163,14 @@ sdb_store_expr_t *
 sdb_store_expr_constvalue(const sdb_data_t *value)
 {
        sdb_data_t data = SDB_DATA_INIT;
+       sdb_store_expr_t *e;
+
        if (sdb_data_copy(&data, value))
                return NULL;
-       return SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type,
+       e = SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type,
                                0, NULL, NULL, &data));
+       e->data_type = data.type;
+       return e;
 } /* sdb_store_expr_constvalue */
 
 int
index b82ae2e9134cb41ca1f4cdb4b77626c454ab0cff..97bb6e65cd56be1afff6a6da035a6ab0ae18803b 100644 (file)
@@ -106,6 +106,14 @@ enum {
                : ((f) == SDB_FIELD_INTERVAL) ? "interval" \
                : ((f) == SDB_FIELD_BACKEND) ? "backend" : "unknown")
 
+#define SDB_FIELD_TYPE(f) \
+       (((f) == SDB_FIELD_NAME) ? SDB_TYPE_STRING \
+               : ((f) == SDB_FIELD_LAST_UPDATE) ? SDB_TYPE_DATETIME \
+               : ((f) == SDB_FIELD_AGE) ? SDB_TYPE_DATETIME \
+               : ((f) == SDB_FIELD_INTERVAL) ? SDB_TYPE_DATETIME \
+               : ((f) == SDB_FIELD_BACKEND) ? (SDB_TYPE_ARRAY | SDB_TYPE_STRING) \
+               : -1)
+
 /*
  * sdb_store_clear:
  * Clear the entire store and remove all stored objects.