From 50904afb0bcedae856a7140ccc11d538437a8758 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Tue, 14 Oct 2014 09:13:11 +0200 Subject: [PATCH] store: Added sdb_store_expr_attrvalue(). This function allows to use attribute values in an expression. --- src/core/store_expr.c | 43 +++++++++++++++++++++++++++++++--------- src/include/core/store.h | 12 +++++++++++ 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/core/store_expr.c b/src/core/store_expr.c index caf3aa6..ca7b72b 100644 --- a/src/core/store_expr.c +++ b/src/core/store_expr.c @@ -39,21 +39,27 @@ #include "core/object.h" #include +#include +#include /* * private data types */ +/* + * expression types: + */ +enum { + ATTR_VALUE = -2, /* attr name stored in data.data.string */ + FIELD_VALUE = -1, /* field type stored in data.data.integer */ + /* 0: const value (stored in data) */ + /* >0: operator id */ +}; + struct sdb_store_expr { sdb_object_t super; - /* - * type: - * -1: field value (field type store in data.data.integer) - * 0: const value (stored in data) - * >0: operator id - */ - int type; + int type; /* see above */ sdb_store_expr_t *left; sdb_store_expr_t *right; @@ -144,9 +150,26 @@ sdb_store_expr_fieldvalue(int field) if ((field < 0) || (SDB_FIELD_BACKEND < field)) return NULL; return SDB_STORE_EXPR(sdb_object_create("store-fieldvalue", expr_type, - -1, NULL, NULL, &value)); + FIELD_VALUE, NULL, NULL, &value)); } /* sdb_store_expr_fieldvalue */ +sdb_store_expr_t * +sdb_store_expr_attrvalue(const char *name) +{ + sdb_data_t value = { SDB_TYPE_STRING, { .string = NULL} }; + sdb_store_expr_t *expr; + + value.data.string = strdup(name); + if (! value.data.string) + return NULL; + + expr = SDB_STORE_EXPR(sdb_object_create("store-attrvalue", expr_type, + ATTR_VALUE, NULL, NULL, &value)); + if (! expr) + free(value.data.string); + return expr; +} /* sdb_store_expr_attrvalue */ + sdb_store_expr_t * sdb_store_expr_constvalue(const sdb_data_t *value) { @@ -169,8 +192,10 @@ sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj, if (! expr->type) return sdb_data_copy(res, &expr->data); - else if (expr->type < 0) + else if (expr->type == FIELD_VALUE) return sdb_store_get_field(obj, (int)expr->data.data.integer, res); + else if (expr->type == ATTR_VALUE) + return sdb_store_get_attr(obj, expr->data.data.string, res); if (sdb_store_expr_eval(expr->left, obj, &v1)) return -1; diff --git a/src/include/core/store.h b/src/include/core/store.h index df4d853..b9a8d08 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -301,6 +301,18 @@ sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right); sdb_store_expr_t * sdb_store_expr_fieldvalue(int field); +/* + * sdb_store_expr_attrvalue: + * Creates an expression which evaluates to the value of the specified + * attribute of a stored object. + * + * Returns: + * - an expression object on success + * - NULL else + */ +sdb_store_expr_t * +sdb_store_expr_attrvalue(const char *name); + /* * sdb_store_expr_constvalue: * Creates an expression which evaluates to the specified constant value. -- 2.30.2