summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: c5168cb)
raw | patch | inline | side by side (parent: c5168cb)
author | Sebastian Harl <sh@tokkee.org> | |
Tue, 14 Oct 2014 07:13:11 +0000 (09:13 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Tue, 14 Oct 2014 07:13:11 +0000 (09:13 +0200) |
This function allows to use attribute values in an expression.
src/core/store_expr.c | patch | blob | history | |
src/include/core/store.h | patch | blob | history |
diff --git a/src/core/store_expr.c b/src/core/store_expr.c
index caf3aa6d79721d120bf3dbaa7b4cd0df74e76b08..ca7b72b52f6a246de610c73de1d0672736057ee0 100644 (file)
--- a/src/core/store_expr.c
+++ b/src/core/store_expr.c
#include "core/object.h"
#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
/*
* 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;
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)
{
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;
index df4d853106bac891ae96e887a24354e58895a726..b9a8d0896eae6a3cad344464696f0479994a13a4 100644 (file)
--- 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.