From: Timo Sirainen Date: Wed, 11 Jun 2008 20:48:34 +0000 (+0300) Subject: Fixed dict iteration with SQL backend. X-Git-Tag: 1.2.alpha1~342 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8c7afcde47e85a661da2a58f8b71f95323262f9a;p=thirdparty%2Fdovecot%2Fcore.git Fixed dict iteration with SQL backend. --HG-- branch : HEAD --- diff --git a/src/lib-dict/dict-sql.c b/src/lib-dict/dict-sql.c index 86015c463e..92290574e0 100644 --- a/src/lib-dict/dict-sql.c +++ b/src/lib-dict/dict-sql.c @@ -209,7 +209,8 @@ sql_dict_iterate_init(struct dict *_dict, const char *path, enum dict_iterate_flags flags) { struct sql_dict *dict = (struct sql_dict *)_dict; - struct sql_dict_iterate_context *ctx; + struct sql_dict_iterate_context *ctx; + unsigned int len; bool priv; ctx = i_new(struct sql_dict_iterate_context, 1); @@ -221,26 +222,41 @@ sql_dict_iterate_init(struct dict *_dict, const char *path, } T_BEGIN { string_t *query = t_str_new(256); - str_printfa(query, "SELECT %s, %s FROM %s " - "WHERE %s LIKE '%s/%%'", + str_printfa(query, "SELECT %s, %s FROM %s WHERE ", dict->where_field, dict->select_field, - dict->table, dict->where_field, - sql_escape_string(dict->db, path)); + dict->table); + len = str_len(query); + + if (*path != '\0') { + str_printfa(query, "%s LIKE '%s/%%' AND ", + dict->where_field, + sql_escape_string(dict->db, path)); + } if (priv) { - str_printfa(query, " AND %s = '%s'", + str_printfa(query, "%s = '%s' AND ", dict->username_field, sql_escape_string(dict->db, dict->username)); } - if ((flags & DICT_ITERATE_FLAG_RECURSE) == 0) { - str_printfa(query, " AND %s NOT LIKE '%s/%%/%%'", + if ((flags & DICT_ITERATE_FLAG_RECURSE) != 0) { + /* get everything */ + } else if (*path == '\0') { + str_printfa(query, "%s NOT LIKE '%%/%%' AND ", + dict->where_field); + } else { + str_printfa(query, "%s NOT LIKE '%s/%%/%%' AND ", dict->where_field, sql_escape_string(dict->db, path)); } + if (str_len(query) == len) + str_truncate(query, str_len(query) - 6); + else + str_truncate(query, str_len(query) - 4); + if ((flags & DICT_ITERATE_FLAG_SORT_BY_KEY) != 0) - str_printfa(query, " ORDER BY %s", dict->where_field); + str_printfa(query, "ORDER BY %s", dict->where_field); else if ((flags & DICT_ITERATE_FLAG_SORT_BY_VALUE) != 0) - str_printfa(query, " ORDER BY %s", dict->select_field); + str_printfa(query, "ORDER BY %s", dict->select_field); ctx->result = sql_query_s(dict->db, str_c(query)); } T_END;