dovecot-2.2: dict-sql: Added support for extensible field types.

dovecot at dovecot.org dovecot at dovecot.org
Wed Sep 30 15:13:37 UTC 2015


details:   http://hg.dovecot.org/dovecot-2.2/rev/6785752f1e25
changeset: 19256:6785752f1e25
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Sep 30 17:38:12 2015 +0300
description:
dict-sql: Added support for extensible field types.

diffstat:

 src/lib-dict/dict-sql-settings.c |   2 +-
 src/lib-dict/dict-sql-settings.h |   7 ++++++-
 src/lib-dict/dict-sql.c          |  28 ++++++++++++++++------------
 3 files changed, 23 insertions(+), 14 deletions(-)

diffs (121 lines):

diff -r 202c236f7581 -r 6785752f1e25 src/lib-dict/dict-sql-settings.c
--- a/src/lib-dict/dict-sql-settings.c	Tue Sep 29 21:02:37 2015 +0300
+++ b/src/lib-dict/dict-sql-settings.c	Wed Sep 30 17:38:12 2015 +0300
@@ -176,7 +176,7 @@
 		    value[value_len-1] == '}') {
 			field->variable = p_strndup(ctx->pool, value + 10,
 						    value_len-10-1);
-			field->sql_field.value_is_hexblob = TRUE;
+			field->sql_field.value_type = DICT_SQL_TYPE_HEXBLOB;
 		} else {
 			field->variable = p_strdup(ctx->pool, value + 1);
 		}
diff -r 202c236f7581 -r 6785752f1e25 src/lib-dict/dict-sql-settings.h
--- a/src/lib-dict/dict-sql-settings.h	Tue Sep 29 21:02:37 2015 +0300
+++ b/src/lib-dict/dict-sql-settings.h	Wed Sep 30 17:38:12 2015 +0300
@@ -1,9 +1,14 @@
 #ifndef DICT_SQL_SETTINGS_H
 #define DICT_SQL_SETTINGS_H
 
+enum dict_sql_type {
+	DICT_SQL_TYPE_STRING = 0,
+	DICT_SQL_TYPE_HEXBLOB
+};
+
 struct dict_sql_field {
 	const char *name;
-	bool value_is_hexblob;
+	enum dict_sql_type value_type;
 };
 
 struct dict_sql_map {
diff -r 202c236f7581 -r 6785752f1e25 src/lib-dict/dict-sql.c
--- a/src/lib-dict/dict-sql.c	Tue Sep 29 21:02:37 2015 +0300
+++ b/src/lib-dict/dict-sql.c	Wed Sep 30 17:38:12 2015 +0300
@@ -213,13 +213,13 @@
 
 static int
 sql_dict_value_escape(string_t *str, struct sql_dict *dict,
-		      bool value_is_hexblob, const char *field_name,
+		      enum dict_sql_type value_type, const char *field_name,
 		      const char *value, const char *value_suffix,
 		      const char **error_r)
 {
 	buffer_t *buf;
 
-	if (!value_is_hexblob) {
+	if (value_type == DICT_SQL_TYPE_STRING) {
 		str_printfa(str, "'%s%s'", sql_escape_string(dict->db, value),
 			    value_suffix);
 		return 0;
@@ -244,7 +244,7 @@
 			    const char *value, const char *value_suffix,
 			    const char **error_r)
 {
-	return sql_dict_value_escape(str, dict, field->value_is_hexblob,
+	return sql_dict_value_escape(str, dict, field->value_type,
 				     field->name, value, value_suffix, error_r);
 }
 
@@ -346,14 +346,14 @@
 }
 
 static const char *
-sql_dict_result_unescape(bool hexblob, pool_t pool, struct sql_result *result,
-			 unsigned int result_idx)
+sql_dict_result_unescape(enum dict_sql_type type, pool_t pool,
+			 struct sql_result *result, unsigned int result_idx)
 {
 	const unsigned char *data;
 	size_t size;
 	string_t *str;
 
-	if (!hexblob)
+	if (type == DICT_SQL_TYPE_STRING)
 		return p_strdup(pool, sql_result_get_field_value(result, result_idx));
 
 	data = sql_result_get_field_value_binary(result, result_idx, &size);
@@ -366,7 +366,9 @@
 sql_dict_result_unescape_value(const struct dict_sql_map *map, pool_t pool,
 			       struct sql_result *result)
 {
-	return sql_dict_result_unescape(map->value_hexblob, pool, result, 0);
+	enum dict_sql_type value_type = map->value_hexblob ?
+		DICT_SQL_TYPE_HEXBLOB : DICT_SQL_TYPE_STRING;
+	return sql_dict_result_unescape(value_type, pool, result, 0);
 }
 
 static const char *
@@ -377,7 +379,7 @@
 	const struct dict_sql_field *sql_field;
 
 	sql_field = array_idx(&map->sql_fields, sql_field_idx);
-	return sql_dict_result_unescape(sql_field->value_is_hexblob, pool,
+	return sql_dict_result_unescape(sql_field->value_type, pool,
 					result, result_idx);
 }
 
@@ -856,8 +858,9 @@
 		if (build->inc)
 			str_append(suffix, fields[i].value);
 		else {
-			if (sql_dict_value_escape(suffix, dict,
-				fields[i].map->value_hexblob,
+			enum dict_sql_type value_type = fields[i].map->value_hexblob ?
+				DICT_SQL_TYPE_HEXBLOB : DICT_SQL_TYPE_STRING;
+			if (sql_dict_value_escape(suffix, dict, value_type,
 				"value", fields[i].value, "", error_r) < 0)
 				return -1;
 		}
@@ -898,8 +901,9 @@
 				    fields[i].map->value_field,
 				    fields[i].value);
 		} else {
-			if (sql_dict_value_escape(prefix, dict,
-				fields[i].map->value_hexblob,
+			enum dict_sql_type value_type = fields[i].map->value_hexblob ?
+				DICT_SQL_TYPE_HEXBLOB : DICT_SQL_TYPE_STRING;
+			if (sql_dict_value_escape(prefix, dict, value_type,
 				"value", fields[i].value, "", error_r) < 0)
 				return -1;
 		}


More information about the dovecot-cvs mailing list