author | Sebastian Harl <sh@tokkee.org> | |
Mon, 23 Jun 2014 18:34:43 +0000 (20:34 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Mon, 23 Jun 2014 18:34:43 +0000 (20:34 +0200) |
src/core/store_lookup.c | patch | blob | history | |
src/frontend/grammar.y | patch | blob | history | |
src/include/core/store.h | patch | blob | history | |
t/unit/core/store_lookup_test.c | patch | blob | history |
index 100942ac8ab652fc21c9d2691e9517ca28564c81..658f931ab30bc3fe64dd30b53e439416ad6a9ae6 100644 (file)
--- a/src/core/store_lookup.c
+++ b/src/core/store_lookup.c
MATCHER_GT, cond));
} /* sdb_store_gt_matcher */
+static sdb_store_matcher_t *
+parse_attr_cmp(const char *attr, const char *op, const sdb_data_t *value)
+{
+ sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
+ sdb_store_matcher_t *m;
+ sdb_store_cond_t *cond;
+
+ /* TODO: this will reject any attributes called "name";
+ * use a different syntax for querying objects by name */
+ if (! strcasecmp(attr, "name"))
+ return NULL;
+
+ if (! strcasecmp(op, "<"))
+ matcher = sdb_store_lt_matcher;
+ else if (! strcasecmp(op, "<="))
+ matcher = sdb_store_le_matcher;
+ else if (! strcasecmp(op, "="))
+ /* XXX: this is still handled by sdb_store_matcher_parse_cmp */
+ matcher = sdb_store_eq_matcher;
+ else if (! strcasecmp(op, ">="))
+ matcher = sdb_store_ge_matcher;
+ else if (! strcasecmp(op, ">"))
+ matcher = sdb_store_gt_matcher;
+ else
+ return NULL;
+
+ cond = sdb_store_attr_cond(attr, value);
+ if (! cond)
+ return NULL;
+
+ m = matcher(cond);
+ /* pass ownership to 'm' or destroy in case of an error */
+ sdb_object_deref(SDB_OBJ(cond));
+ return m;
+} /* parse_attr_cmp */
+
sdb_store_matcher_t *
sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
- const char *op, const char *value)
+ const char *op, const sdb_data_t *value)
{
int type = -1;
_Bool inv = 0;
type = SDB_SERVICE;
else if (! strcasecmp(obj_type, "attribute"))
type = SDB_ATTRIBUTE;
+ else
+ return NULL;
- /* TODO: support other operators */
+ /* XXX: this code sucks! */
if (! strcasecmp(op, "=")) {
/* nothing to do */
}
inv = 1;
re = 1;
}
+ else if (type == SDB_ATTRIBUTE)
+ return parse_attr_cmp(attr, op, value);
else
return NULL;
+ if (value->type != SDB_TYPE_STRING)
+ return NULL;
+
if (! strcasecmp(attr, "name"))
- m = sdb_store_name_matcher(type, value, re);
+ m = sdb_store_name_matcher(type, value->data.string, re);
else if (type == SDB_ATTRIBUTE)
- m = sdb_store_attr_matcher(attr, value, re);
+ m = sdb_store_attr_matcher(attr, value->data.string, re);
if (! m)
return NULL;
diff --git a/src/frontend/grammar.y b/src/frontend/grammar.y
index 8e5ddf6e5eff7a1e32646ad0d62795109a2b61f9..402e17087f692744f2dea73068654daced1d1134 100644 (file)
--- a/src/frontend/grammar.y
+++ b/src/frontend/grammar.y
compare_matcher:
IDENTIFIER '.' IDENTIFIER op STRING
{
- $$ = sdb_store_matcher_parse_cmp($1, $3, $4, $5);
+ sdb_data_t data = { SDB_TYPE_STRING, { .string = $5 } };
+ $$ = sdb_store_matcher_parse_cmp($1, $3, $4, &data);
free($1); $1 = NULL;
free($3); $3 = NULL;
free($5); $5 = NULL;
index ecaf21d0bbd6b974483290a94dce38916dd285c4..59200815482e9d5540837dfd1fce07e19fdfd743 100644 (file)
--- a/src/include/core/store.h
+++ b/src/include/core/store.h
*/
sdb_store_matcher_t *
sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
- const char *op, const char *value);
+ const char *op, const sdb_data_t *value);
/*
* sdb_store_dis_matcher:
index e90dd06c1acee054d6388dcbd0ef7e5b99c4e4d0..4a935429923bb4dd1118c092756387cf1393fc0e 100644 (file)
START_TEST(test_parse_cmp)
{
+ sdb_data_t hostname = { SDB_TYPE_STRING, { .string = "hostname" } };
+ sdb_data_t srvname = { SDB_TYPE_STRING, { .string = "srvname" } };
+ sdb_data_t attrname = { SDB_TYPE_STRING, { .string = "attrname" } };
+
sdb_store_matcher_t *check;
size_t i;
const char *obj_type;
const char *attr;
const char *op;
- const char *value;
+ const sdb_data_t value;
int expected;
} golden_data[] = {
- { "host", "name", "=", "hostname", MATCHER_NAME },
- { "host", "name", "!=", "hostname", MATCHER_NOT },
- { "host", "name", "=~", "hostname", MATCHER_NAME },
- { "host", "name", "!~", "hostname", MATCHER_NOT },
- { "host", "attr", "=", "hostname", -1 },
- { "host", "attr", "!=", "hostname", -1 },
- { "host", "name", "&^", "hostname", -1 },
- { "service", "name", "=", "srvname", MATCHER_NAME },
- { "service", "name", "!=", "srvname", MATCHER_NOT },
- { "service", "name", "=~", "srvname", MATCHER_NAME },
- { "service", "name", "!~", "srvname", MATCHER_NOT },
- { "service", "attr", "=", "srvname", -1 },
- { "service", "attr", "!=", "srvname", -1 },
- { "service", "name", "&^", "srvname", -1 },
- { "attribute", "name", "=", "attrname", MATCHER_NAME },
- { "attribute", "name", "!=", "attrname", MATCHER_NOT },
- { "attribute", "name", "=~", "attrname", MATCHER_NAME },
- { "attribute", "name", "!~", "attrname", MATCHER_NOT },
- { "attribute", "attr", "=", "attrname", MATCHER_ATTR },
- { "attribute", "attr", "!=", "attrname", MATCHER_NOT },
- { "attribute", "attr", "=~", "attrname", MATCHER_ATTR },
- { "attribute", "attr", "!~", "attrname", MATCHER_NOT },
- { "attribute", "attr", "&^", "attrname", -1 },
+ { "host", "name", "=", hostname, MATCHER_NAME },
+ { "host", "name", "!=", hostname, MATCHER_NOT },
+ { "host", "name", "=~", hostname, MATCHER_NAME },
+ { "host", "name", "!~", hostname, MATCHER_NOT },
+ { "host", "attr", "=", hostname, -1 },
+ { "host", "attr", "!=", hostname, -1 },
+ { "host", "name", "&^", hostname, -1 },
+ { "host", "name", "<", hostname, -1 },
+ { "host", "name", "<=", hostname, -1 },
+ { "host", "name", ">=", hostname, -1 },
+ { "host", "name", ">", hostname, -1 },
+ { "service", "name", "=", srvname, MATCHER_NAME },
+ { "service", "name", "!=", srvname, MATCHER_NOT },
+ { "service", "name", "=~", srvname, MATCHER_NAME },
+ { "service", "name", "!~", srvname, MATCHER_NOT },
+ { "service", "attr", "=", srvname, -1 },
+ { "service", "attr", "!=", srvname, -1 },
+ { "service", "name", "&^", srvname, -1 },
+ { "service", "name", "<", srvname, -1 },
+ { "service", "name", "<=", srvname, -1 },
+ { "service", "name", ">=", srvname, -1 },
+ { "service", "name", ">", srvname, -1 },
+ { "attribute", "name", "=", attrname, MATCHER_NAME },
+ { "attribute", "name", "!=", attrname, MATCHER_NOT },
+ { "attribute", "name", "=~", attrname, MATCHER_NAME },
+ { "attribute", "name", "!~", attrname, MATCHER_NOT },
+ { "attribute", "name", "<", attrname, -1 },
+ { "attribute", "name", "<=", attrname, -1 },
+ { "attribute", "name", ">=", attrname, -1 },
+ { "attribute", "name", ">", attrname, -1 },
+ { "attribute", "attr", "=", attrname, MATCHER_ATTR },
+ { "attribute", "attr", "!=", attrname, MATCHER_NOT },
+ { "attribute", "attr", "=~", attrname, MATCHER_ATTR },
+ { "attribute", "attr", "!~", attrname, MATCHER_NOT },
+ { "attribute", "attr", "&^", attrname, -1 },
+ { "attribute", "attr", "<", attrname, MATCHER_LT },
+ { "attribute", "attr", "<=", attrname, MATCHER_LE },
+/* { "attribute", "attr", "=", attrname, MATCHER_EQ }, */
+ { "attribute", "attr", ">=", attrname, MATCHER_GE },
+ { "attribute", "attr", ">", attrname, MATCHER_GT },
+ { "foo", "name", "=", attrname, -1 },
+ { "foo", "attr", "=", attrname, -1 },
};
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+ char buf[1024];
+
check = sdb_store_matcher_parse_cmp(golden_data[i].obj_type,
- golden_data[i].attr, golden_data[i].op, golden_data[i].value);
+ golden_data[i].attr, golden_data[i].op,
+ &golden_data[i].value);
+
+ if (sdb_data_format(&golden_data[i].value,
+ buf, sizeof(buf), SDB_UNQUOTED) < 0)
+ snprintf(buf, sizeof(buf), "ERR");
if (golden_data[i].expected == -1) {
fail_unless(check == NULL,
"sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
"expected: NULL", golden_data[i].obj_type,
- golden_data[i].attr, golden_data[i].op,
- golden_data[i].value, check);
+ golden_data[i].attr, golden_data[i].op, buf, check);
continue;
}
fail_unless(check != NULL,
"sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
"expected: NULL", golden_data[i].obj_type,
- golden_data[i].attr, golden_data[i].op,
- golden_data[i].value, check);
+ golden_data[i].attr, golden_data[i].op, buf, check);
fail_unless(M(check)->type == golden_data[i].expected,
"sdb_store_matcher_parse_cmp(%s, %s, %s, %s) returned matcher "
"of type %d; expected: %d", golden_data[i].obj_type,
- golden_data[i].attr, golden_data[i].op, golden_data[i].value,
+ golden_data[i].attr, golden_data[i].op, buf,
M(check)->type, golden_data[i].expected);
sdb_object_deref(SDB_OBJ(check));