From: Sebastian Harl Date: Tue, 21 Oct 2014 05:36:41 +0000 (+0200) Subject: store: Added sdb_store_in_matcher(). X-Git-Tag: sysdb-0.6.0~88 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=e4925baf923191b68f211354f57e743b7500d0c8;p=sysdb.git store: Added sdb_store_in_matcher(). This function creates a matcher which matches if the right value evaluates to an array value and the left value is included in that array. --- diff --git a/src/core/store-private.h b/src/core/store-private.h index 3db771b..750a0b3 100644 --- a/src/core/store-private.h +++ b/src/core/store-private.h @@ -178,6 +178,7 @@ enum { MATCHER_CMP_NE, MATCHER_CMP_GE, MATCHER_CMP_GT, + MATCHER_IN, MATCHER_REGEX, MATCHER_NREGEX, MATCHER_ISNULL, @@ -199,6 +200,7 @@ enum { : ((t) == MATCHER_CMP_NE) ? "!=" \ : ((t) == MATCHER_GE) ? ">=" \ : ((t) == MATCHER_GT) ? ">" \ + : ((t) == MATCHER_IN) ? "IN" \ : ((t) == MATCHER_REGEX) ? "=~" \ : ((t) == MATCHER_NREGEX) ? "!~" \ : ((t) == MATCHER_ISNULL) ? "IS NULL" \ diff --git a/src/core/store_lookup.c b/src/core/store_lookup.c index 21e0ba4..274ca02 100644 --- a/src/core/store_lookup.c +++ b/src/core/store_lookup.c @@ -465,6 +465,27 @@ match_cmp_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj, return (status != INT_MAX) && (status > 0); } /* match_cmp_gt */ +static int +match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj, + sdb_store_matcher_t *filter) +{ + sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT; + int status = 1; + + assert(m->type == MATCHER_IN); + + if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &value, filter)) + || (sdb_store_expr_eval(CMP_M(m)->right, obj, &array, filter))) + status = 0; + + if (status) + status = sdb_data_inarray(&value, &array); + + sdb_data_free_datum(&value); + sdb_data_free_datum(&array); + return status; +} /* match_in */ + static int match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj, sdb_store_matcher_t *filter) @@ -573,6 +594,7 @@ matchers[] = { match_cmp_ne, match_cmp_ge, match_cmp_gt, + match_in, match_regex, match_regex, match_isnull, @@ -1066,6 +1088,7 @@ matchers_tostring[] = { cmp_tostring, cmp_tostring, cmp_tostring, + cmp_tostring, isnull_tostring, isnull_tostring, }; @@ -1218,6 +1241,13 @@ sdb_store_cmp_gt(sdb_store_expr_t *left, sdb_store_expr_t *right) MATCHER_CMP_GT, left, right)); } /* sdb_store_cmp_gt */ +sdb_store_matcher_t * +sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right) +{ + return M(sdb_object_create("in-matcher", cmp_type, + MATCHER_IN, left, right)); +} /* sdb_store_in_matcher */ + sdb_store_matcher_t * sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right) { diff --git a/src/include/core/store.h b/src/include/core/store.h index b1d15f5..2e1e558 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -459,6 +459,15 @@ sdb_store_cmp_ge(sdb_store_expr_t *left, sdb_store_expr_t *right); sdb_store_matcher_t * sdb_store_cmp_gt(sdb_store_expr_t *left, sdb_store_expr_t *right); +/* + * sdb_store_in_matcher: + * Creates a matcher which matches if the right value evaluates to an array + * value and the left value is included in that array. See sdb_data_inarray + * for more details. + */ +sdb_store_matcher_t * +sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right); + /* * sdb_store_regex_matcher: * Creates a matcher which matches the string value the left expression