changeset 18638:bb7d35fa9b43

lib-dict: Added DICT_ITERATE_FLAG_EXACT_KEY flag. This is mainly useful with SQL for iterating through a result that has multiple rows.
author Timo Sirainen <tss@iki.fi>
date Mon, 11 May 2015 19:12:45 +0300
parents 44a87e17f988
children 7abdcf42b63b
files src/lib-dict/dict-file.c src/lib-dict/dict-fs.c src/lib-dict/dict-sql.c src/lib-dict/dict.h
diffstat 4 files changed, 25 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-dict/dict-file.c	Mon May 11 16:01:13 2015 +0300
+++ b/src/lib-dict/dict-file.c	Mon May 11 19:12:45 2015 +0300
@@ -261,9 +261,15 @@
 		if (path == NULL)
 			continue;
 
-		if ((ctx->flags & DICT_ITERATE_FLAG_RECURSE) == 0 &&
-		    strchr(key + path->len, '/') != NULL)
-			continue;
+		if ((ctx->flags & DICT_ITERATE_FLAG_RECURSE) != 0) {
+			/* match everything */
+		} else if ((ctx->flags & DICT_ITERATE_FLAG_EXACT_KEY) != 0) {
+			if (key[path->len] != '\0')
+				continue;
+		} else {
+			if (strchr(key + path->len, '/') != NULL)
+				continue;
+		}
 
 		*key_r = key;
 		*value_r = value;
--- a/src/lib-dict/dict-fs.c	Mon May 11 16:01:13 2015 +0300
+++ b/src/lib-dict/dict-fs.c	Mon May 11 19:12:45 2015 +0300
@@ -125,6 +125,7 @@
 
 	/* these flags are not supported for now */
 	i_assert((flags & DICT_ITERATE_FLAG_RECURSE) == 0);
+	i_assert((flags & DICT_ITERATE_FLAG_EXACT_KEY) == 0);
 	i_assert((flags & (DICT_ITERATE_FLAG_SORT_BY_KEY |
 			   DICT_ITERATE_FLAG_SORT_BY_VALUE)) == 0);
 
--- a/src/lib-dict/dict-sql.c	Mon May 11 16:01:13 2015 +0300
+++ b/src/lib-dict/dict-sql.c	Mon May 11 19:12:45 2015 +0300
@@ -377,8 +377,12 @@
 
 		str_printfa(query, " FROM %s", map->table);
 
-		recurse_type = (ctx->flags & DICT_ITERATE_FLAG_RECURSE) == 0 ?
-			SQL_DICT_RECURSE_ONE : SQL_DICT_RECURSE_FULL;
+		if ((ctx->flags & DICT_ITERATE_FLAG_RECURSE) != 0)
+			recurse_type = SQL_DICT_RECURSE_FULL;
+		else if ((ctx->flags & DICT_ITERATE_FLAG_EXACT_KEY) != 0)
+			recurse_type = SQL_DICT_RECURSE_NONE;
+		else
+			recurse_type = SQL_DICT_RECURSE_ONE;
 		sql_dict_where_build(dict, map, &values,
 				     ctx->paths[ctx->path_idx][0],
 				     recurse_type, query);
--- a/src/lib-dict/dict.h	Mon May 11 16:01:13 2015 +0300
+++ b/src/lib-dict/dict.h	Mon May 11 19:12:45 2015 +0300
@@ -7,10 +7,18 @@
 struct dict;
 
 enum dict_iterate_flags {
+	/* Recurse to all the sub-hierarchies (e.g. iterating "foo/" will
+	   return "foo/a", but should it return "foo/a/b"?) */
 	DICT_ITERATE_FLAG_RECURSE             = 0x01,
+	/* Sort returned results by key */
 	DICT_ITERATE_FLAG_SORT_BY_KEY         = 0x02,
+	/* Sort returned results by value */
 	DICT_ITERATE_FLAG_SORT_BY_VALUE       = 0x04,
-	DICT_ITERATE_FLAG_NO_VALUE            = 0x08
+	/* Don't return values, only keys */
+	DICT_ITERATE_FLAG_NO_VALUE            = 0x08,
+	/* Don't recurse at all. This is basically the same as dict_lookup(),
+	   but it'll return all the rows instead of only the first one. */
+	DICT_ITERATE_FLAG_EXACT_KEY           = 0x10
 };
 
 enum dict_data_type {