From 33d51d5f84495bc8d7805c2370af45948e46c30b Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Mon, 21 Jul 2014 18:51:48 +0200 Subject: [PATCH] store, frontend: Switch from .name to . This feels a bit more natural and no longer shadows attributes called "name". --- src/core/store_lookup.c | 6 +- src/frontend/grammar.y | 7 ++ t/integration/simple_query.sh | 6 +- t/unit/core/store_lookup_test.c | 74 ++++++++--------- t/unit/frontend/parser_test.c | 135 ++++++++++++++++---------------- 5 files changed, 116 insertions(+), 112 deletions(-) diff --git a/src/core/store_lookup.c b/src/core/store_lookup.c index da9f550..a3efb46 100644 --- a/src/core/store_lookup.c +++ b/src/core/store_lookup.c @@ -732,9 +732,7 @@ parse_attr_cmp(const char *attr, const char *op, const sdb_data_t *value) sdb_store_cond_t *cond; _Bool inv = 0; - /* TODO: this will reject any attributes called "name"; - * use a different syntax for querying objects by name */ - if (! strcasecmp(attr, "name")) + if (! attr) return NULL; if (! strcasecmp(op, "IS")) { @@ -826,7 +824,7 @@ sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr, return NULL; } - if (! strcasecmp(attr, "name")) + if (! attr) m = sdb_store_name_matcher(type, value->data.string, re); else if (type == SDB_ATTRIBUTE) m = sdb_store_attr_matcher(attr, value->data.string, re); diff --git a/src/frontend/grammar.y b/src/frontend/grammar.y index 2173e6d..d505485 100644 --- a/src/frontend/grammar.y +++ b/src/frontend/grammar.y @@ -302,6 +302,13 @@ matcher: * Parse matchers comparing object attributes with a value. */ compare_matcher: + IDENTIFIER op data + { + $$ = sdb_store_matcher_parse_cmp($1, NULL, $2, &$3); + free($1); $1 = NULL; + sdb_data_free_datum(&$3); + } + | IDENTIFIER '.' IDENTIFIER op data { $$ = sdb_store_matcher_parse_cmp($1, $3, $4, &$5); diff --git a/t/integration/simple_query.sh b/t/integration/simple_query.sh index a474a52..ef12bf8 100755 --- a/t/integration/simple_query.sh +++ b/t/integration/simple_query.sh @@ -98,7 +98,7 @@ echo "$output" | grep -F 'other.host.name' && exit 1 echo "$output" | grep -F 'some.host.name' && exit 1 output="$( run_sysdb -H "$SOCKET_FILE" \ - -c "LOOKUP hosts WHERE attribute.name != 'architecture'" )" + -c "LOOKUP hosts WHERE attribute != 'architecture'" )" echo "$output" \ | grep -F '"some.host.name"' \ | grep -F '"localhost"' @@ -107,7 +107,7 @@ echo "$output" | grep -F 'host1.example.com' && exit 1 echo "$output" | grep -F 'host2.example.com' && exit 1 output="$( run_sysdb -H "$SOCKET_FILE" \ - -c "LOOKUP hosts WHERE service.name = 'sysdbd'" )" + -c "LOOKUP hosts WHERE service = 'sysdbd'" )" echo "$output" | grep -F '"localhost"' echo "$output" | grep -F 'some.host.name' && exit 1 echo "$output" | grep -F 'other.host.name' && exit 1 @@ -115,7 +115,7 @@ echo "$output" | grep -F 'host1.example.com' && exit 1 echo "$output" | grep -F 'host2.example.com' && exit 1 output="$( run_sysdb -H "$SOCKET_FILE" \ - -c "LOOKUP hosts WHERE host.name =~ 'example.com'" )" + -c "LOOKUP hosts WHERE host =~ 'example.com'" )" echo "$output" \ | grep -F '"host1.example.com"' \ | grep -F '"host2.example.com"' diff --git a/t/unit/core/store_lookup_test.c b/t/unit/core/store_lookup_test.c index 7536f39..7ecaaf0 100644 --- a/t/unit/core/store_lookup_test.c +++ b/t/unit/core/store_lookup_test.c @@ -410,36 +410,36 @@ START_TEST(test_parse_cmp) 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", NULL, "=", &hostname, MATCHER_NAME }, + { "host", NULL, "!=", &hostname, MATCHER_NOT }, + { "host", NULL, "=~", &hostname, MATCHER_NAME }, + { "host", NULL, "!~", &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 }, + { "host", NULL, "&^", &hostname, -1 }, + { "host", NULL, "<", &hostname, -1 }, + { "host", NULL, "<=", &hostname, -1 }, + { "host", NULL, ">=", &hostname, -1 }, + { "host", NULL, ">", &hostname, -1 }, + { "service", NULL, "=", &srvname, MATCHER_NAME }, + { "service", NULL, "!=", &srvname, MATCHER_NOT }, + { "service", NULL, "=~", &srvname, MATCHER_NAME }, + { "service", NULL, "!~", &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 }, + { "service", NULL, "&^", &srvname, -1 }, + { "service", NULL, "<", &srvname, -1 }, + { "service", NULL, "<=", &srvname, -1 }, + { "service", NULL, ">=", &srvname, -1 }, + { "service", NULL, ">", &srvname, -1 }, + { "attribute", NULL, "=", &attrname, MATCHER_NAME }, + { "attribute", NULL, "!=", &attrname, MATCHER_NOT }, + { "attribute", NULL, "=~", &attrname, MATCHER_NAME }, + { "attribute", NULL, "!~", &attrname, MATCHER_NOT }, + { "attribute", NULL, "<", &attrname, -1 }, + { "attribute", NULL, "<=", &attrname, -1 }, + { "attribute", NULL, ">=", &attrname, -1 }, + { "attribute", NULL, ">", &attrname, -1 }, { "attribute", "attr", "=", &attrname, MATCHER_ATTR }, { "attribute", "attr", "!=", &attrname, MATCHER_NOT }, { "attribute", "attr", "=~", &attrname, MATCHER_ATTR }, @@ -452,7 +452,7 @@ START_TEST(test_parse_cmp) { "attribute", "attr", ">", &attrname, MATCHER_GT }, { "attribute", "attr", "IS", NULL, MATCHER_ISNULL }, { "attribute", "attr", "IS", &attrname, -1 }, - { "foo", "name", "=", &attrname, -1 }, + { "foo", NULL, "=", &attrname, -1 }, { "foo", "attr", "=", &attrname, -1 }, }; @@ -514,23 +514,23 @@ START_TEST(test_lookup) int expected; const char *tostring_re; } golden_data[] = { - { "host.name = 'a'", 1, + { "host = 'a'", 1, "OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" }, - { "host.name =~ 'a|b'", 2, + { "host =~ 'a|b'", 2, "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" }, - { "host.name =~ 'host'", 0, + { "host =~ 'host'", 0, "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" }, - { "host.name =~ '.'", 3, + { "host =~ '.'", 3, "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" }, - { "service.name = 's1'", 2, + { "service = 's1'", 2, "OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" }, - { "service.name =~ 's'", 2, + { "service =~ 's'", 2, "OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}" }, - { "service.name !~ 's'", 1, + { "service !~ 's'", 1, "\\(NOT, OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}\\)" }, - { "attribute.name = 'k1'", 1, + { "attribute = 'k1'", 1, "OBJ\\[attribute\\]\\{ NAME\\{ 'k1', \\(nil\\) \\} " }, - { "attribute.name = 'x'", 0, + { "attribute = 'x'", 0, "OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\}" }, { "attribute.k1 = 'v1'", 1, "ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}" }, @@ -558,7 +558,7 @@ START_TEST(test_lookup) "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}\\)" }, { "attribute.k1 != 'v2'", 3, "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v2', \\(nil\\) \\} \\}\\)" }, - { "attribute.name != 'x' " + { "attribute != 'x' " "AND attribute.y !~ 'x'", 3, "\\(AND, " "\\(NOT, OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\} \\}\\), " diff --git a/t/unit/frontend/parser_test.c b/t/unit/frontend/parser_test.c index 4b9b672..f1e6569 100644 --- a/t/unit/frontend/parser_test.c +++ b/t/unit/frontend/parser_test.c @@ -59,23 +59,23 @@ START_TEST(test_parse) { "LIST; INVALID", 5, 1, CONNECTION_LIST }, { "LOOKUP hosts WHERE " - "host.name = 'host'", -1, 1, CONNECTION_LOOKUP }, + "host = 'host'", -1, 1, CONNECTION_LOOKUP }, { "LOOKUP hosts WHERE NOT " - "host.name = 'host'", -1, 1, CONNECTION_LOOKUP }, + "host = 'host'", -1, 1, CONNECTION_LOOKUP }, { "LOOKUP hosts WHERE " - "host.name =~ 'p' AND " - "service.name =~ 'p'", -1, 1, CONNECTION_LOOKUP }, + "host =~ 'p' AND " + "service =~ 'p'", -1, 1, CONNECTION_LOOKUP }, { "LOOKUP hosts WHERE NOT " - "host.name =~ 'p' AND " - "service.name =~ 'p'", -1, 1, CONNECTION_LOOKUP }, + "host =~ 'p' AND " + "service =~ 'p'", -1, 1, CONNECTION_LOOKUP }, { "LOOKUP hosts WHERE " - "host.name =~ 'p' AND " - "service.name =~ 'p' OR " - "service.name =~ 'r'", -1, 1, CONNECTION_LOOKUP }, + "host =~ 'p' AND " + "service =~ 'p' OR " + "service =~ 'r'", -1, 1, CONNECTION_LOOKUP }, { "LOOKUP hosts WHERE NOT " - "host.name =~ 'p' AND " - "service.name =~ 'p' OR " - "service.name =~ 'r'", -1, 1, CONNECTION_LOOKUP }, + "host =~ 'p' AND " + "service =~ 'p' OR " + "service =~ 'r'", -1, 1, CONNECTION_LOOKUP }, /* numeric constants */ { "LOOKUP hosts WHERE " @@ -117,10 +117,9 @@ START_TEST(test_parse) "NOT attribute.foo " "IS NULL", -1, 1, CONNECTION_LOOKUP }, { "LOOKUP hosts WHERE " - "host.name IS NULL", -1, -1, 0 }, + "host IS NULL", -1, -1, 0 }, { "LOOKUP hosts WHERE " - "service.name " - "IS NULL", -1, -1, 0 }, + "service IS NULL", -1, -1, 0 }, /* invalid numeric constants */ { "LOOKUP hosts WHERE " @@ -152,7 +151,7 @@ START_TEST(test_parse) { "LOOKUP hosts", -1, -1, 0 }, { "LOOKUP foo WHERE " - "host.name = 'host'", -1, -1, 0 }, + "host = 'host'", -1, -1, 0 }, }; size_t i; @@ -200,70 +199,70 @@ START_TEST(test_parse_matcher) int expected; } golden_data[] = { /* empty expressions */ - { NULL, -1, -1 }, - { "", -1, -1 }, + { NULL, -1, -1 }, + { "", -1, -1 }, /* valid expressions */ - { "host.name = 'localhost'", -1, MATCHER_NAME }, - { "host.name != 'localhost'", -1, MATCHER_NOT }, - { "host.name =~ 'host'", -1, MATCHER_NAME }, - { "host.name !~ 'host'", -1, MATCHER_NOT }, - { "host.name = 'localhost' -- foo", -1, MATCHER_NAME }, - { "host.name = 'host' ", 18, MATCHER_NAME }, + { "host = 'localhost'", -1, MATCHER_NAME }, + { "host != 'localhost'", -1, MATCHER_NOT }, + { "host =~ 'host'", -1, MATCHER_NAME }, + { "host !~ 'host'", -1, MATCHER_NOT }, + { "host = 'localhost' -- foo", -1, MATCHER_NAME }, + { "host = 'host' ", 13, MATCHER_NAME }, /* match hosts by service */ - { "service.name = 'name'", -1, MATCHER_NAME }, - { "service.name != 'name'", -1, MATCHER_NOT }, - { "service.name =~ 'pattern'", -1, MATCHER_NAME }, - { "service.name !~ 'pattern'", -1, MATCHER_NOT }, + { "service = 'name'", -1, MATCHER_NAME }, + { "service != 'name'", -1, MATCHER_NOT }, + { "service =~ 'pattern'", -1, MATCHER_NAME }, + { "service !~ 'pattern'", -1, MATCHER_NOT }, /* match hosts by attribute */ - { "attribute.name = 'name'", -1, MATCHER_NAME }, - { "attribute.name != 'name'", -1, MATCHER_NOT }, - { "attribute.name =~ 'pattern'", -1, MATCHER_NAME }, - { "attribute.name !~ 'pattern'", -1, MATCHER_NOT }, + { "attribute = 'name'", -1, MATCHER_NAME }, + { "attribute != 'name'", -1, MATCHER_NOT }, + { "attribute =~ 'pattern'", -1, MATCHER_NAME }, + { "attribute !~ 'pattern'", -1, MATCHER_NOT }, /* composite expressions */ - { "host.name =~ 'pattern' AND " - "service.name =~ 'pattern'", -1, MATCHER_AND }, - { "host.name =~ 'pattern' OR " - "service.name =~ 'pattern'", -1, MATCHER_OR }, - { "NOT host.name = 'host'", -1, MATCHER_NOT }, + { "host =~ 'pattern' AND " + "service =~ 'pattern'", -1, MATCHER_AND }, + { "host =~ 'pattern' OR " + "service =~ 'pattern'", -1, MATCHER_OR }, + { "NOT host = 'host'", -1, MATCHER_NOT }, /* numeric expressions */ - { "attribute.foo < 123", -1, MATCHER_LT }, - { "attribute.foo <= 123", -1, MATCHER_LE }, - { "attribute.foo = 123", -1, MATCHER_EQ }, - { "attribute.foo >= 123", -1, MATCHER_GE }, - { "attribute.foo > 123", -1, MATCHER_GT }, + { "attribute.foo < 123", -1, MATCHER_LT }, + { "attribute.foo <= 123", -1, MATCHER_LE }, + { "attribute.foo = 123", -1, MATCHER_EQ }, + { "attribute.foo >= 123", -1, MATCHER_GE }, + { "attribute.foo > 123", -1, MATCHER_GT }, /* NULL; while this is an implementation detail, * IS NULL currently maps to an equality matcher */ - { "attribute.foo IS NULL", -1, MATCHER_ISNULL }, - { "attribute.foo IS NOT NULL", -1, MATCHER_NOT }, + { "attribute.foo IS NULL", -1, MATCHER_ISNULL }, + { "attribute.foo IS NOT NULL", -1, MATCHER_NOT }, /* check operator precedence */ - { "host.name = 'name' OR " - "service.name = 'name' AND " - "attribute.name = 'name' OR " - "attribute.foo = 'bar'", -1, MATCHER_OR }, - { "host.name = 'name' AND " - "service.name = 'name' AND " - "attribute.name = 'name' OR " - "attribute.foo = 'bar'", -1, MATCHER_OR }, - { "host.name = 'name' AND " - "service.name = 'name' OR " - "attribute.name = 'name' AND " - "attribute.foo = 'bar'", -1, MATCHER_OR }, - { "(host.name = 'name' OR " - "service.name = 'name') AND " - "(attribute.name = 'name' OR " - "attribute.foo = 'bar')", -1, MATCHER_AND }, - { "NOT host.name = 'name' OR " - "service.name = 'name'", -1, MATCHER_OR }, - { "NOT host.name = 'name' OR " - "NOT service.name = 'name'", -1, MATCHER_OR }, - { "NOT (host.name = 'name' OR " - "NOT service.name = 'name')", -1, MATCHER_NOT }, + { "host = 'name' OR " + "service = 'name' AND " + "attribute = 'name' OR " + "attribute.foo = 'bar'", -1, MATCHER_OR }, + { "host = 'name' AND " + "service = 'name' AND " + "attribute = 'name' OR " + "attribute.foo = 'bar'", -1, MATCHER_OR }, + { "host = 'name' AND " + "service = 'name' OR " + "attribute = 'name' AND " + "attribute.foo = 'bar'", -1, MATCHER_OR }, + { "(host = 'name' OR " + "service = 'name') AND " + "(attribute = 'name' OR " + "attribute.foo = 'bar')", -1, MATCHER_AND }, + { "NOT host = 'name' OR " + "service = 'name'", -1, MATCHER_OR }, + { "NOT host = 'name' OR " + "NOT service = 'name'", -1, MATCHER_OR }, + { "NOT (host = 'name' OR " + "NOT service = 'name')", -1, MATCHER_NOT }, /* syntax errors */ - { "LIST", -1, -1 }, - { "foo &^ bar", -1, -1 }, + { "LIST", -1, -1 }, + { "foo &^ bar", -1, -1 }, }; size_t i; -- 2.30.2