changeset 22523:e7d2548700b9

dict-sql: Cleanup - change query generator functions to return statement Instead of query+params. This is in preparation for the following changes.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Sat, 26 Aug 2017 23:17:45 +0300
parents 486ddf6a61eb
children c5a2743428a0
files src/lib-dict/dict-sql.c
diffstat 1 files changed, 38 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-dict/dict-sql.c	Tue Aug 15 16:50:16 2017 +0300
+++ b/src/lib-dict/dict-sql.c	Sat Aug 26 23:17:45 2017 +0300
@@ -408,8 +408,9 @@
 
 static int
 sql_lookup_get_query(struct sql_dict *dict, const char *key,
-		     string_t *query, const struct dict_sql_map **map_r,
-		     ARRAY_TYPE(sql_dict_param) *params, const char **error_r)
+		     const struct dict_sql_map **map_r,
+		     struct sql_statement **stmt_r,
+		     const char **error_r)
 {
 	const struct dict_sql_map *map;
 	ARRAY_TYPE(const_string) values;
@@ -421,15 +422,20 @@
 			"sql dict lookup: Invalid/unmapped key: %s", key);
 		return -1;
 	}
+
+	string_t *query = t_str_new(256);
+	ARRAY_TYPE(sql_dict_param) params;
+	t_array_init(&params, 4);
 	str_printfa(query, "SELECT %s FROM %s",
 		    map->value_field, map->table);
 	if (sql_dict_where_build(dict, map, &values, key[0],
 				 SQL_DICT_RECURSE_NONE, query,
-				 params, &error) < 0) {
+				 &params, &error) < 0) {
 		*error_r = t_strdup_printf(
 			"sql dict lookup: Failed to lookup key %s: %s", key, error);
 		return -1;
 	}
+	*stmt_r = sql_dict_statement_init(dict->db, str_c(query), &params);
 	return 0;
 }
 
@@ -495,22 +501,18 @@
 {
 	struct sql_dict *dict = (struct sql_dict *)_dict;
 	const struct dict_sql_map *map;
+	struct sql_statement *stmt;
 	struct sql_result *result = NULL;
-	string_t *query = t_str_new(256);
-	ARRAY_TYPE(sql_dict_param) params;
 	const char *error;
 	int ret;
 
 	*value_r = NULL;
 
-	t_array_init(&params, 4);
-	if (sql_lookup_get_query(dict, key, query, &map, &params, &error) < 0) {
+	if (sql_lookup_get_query(dict, key, &map, &stmt, &error) < 0) {
 		i_error("%s", error);
 		return -1;
 	}
 
-	struct sql_statement *stmt =
-		sql_dict_statement_init(dict->db, str_c(query), &params);
 	result = sql_statement_query_s(&stmt);
 	ret = sql_result_next_row(result);
 	if (ret <= 0) {
@@ -566,12 +568,10 @@
 	struct sql_dict *dict = (struct sql_dict *)_dict;
 	const struct dict_sql_map *map;
 	struct sql_dict_lookup_context *ctx;
-	string_t *query = t_str_new(256);
-	ARRAY_TYPE(sql_dict_param) params;
+	struct sql_statement *stmt;
 	const char *error;
 
-	t_array_init(&params, 4);
-	if (sql_lookup_get_query(dict, key, query, &map, &params, &error) < 0) {
+	if (sql_lookup_get_query(dict, key, &map, &stmt, &error) < 0) {
 		struct dict_lookup_result result;
 
 		i_zero(&result);
@@ -583,8 +583,6 @@
 		ctx->callback = callback;
 		ctx->context = context;
 		ctx->map = map;
-		struct sql_statement *stmt =
-			sql_dict_statement_init(dict->db, str_c(query), &params);
 		sql_statement_query(&stmt, sql_dict_lookup_async_callback, ctx);
 	}
 }
@@ -625,8 +623,7 @@
 
 static int
 sql_dict_iterate_build_next_query(struct sql_dict_iterate_context *ctx,
-				  string_t *query,
-				  ARRAY_TYPE(sql_dict_param) *params,
+				  struct sql_statement **stmt_r,
 				  const char **error_r)
 {
 	struct sql_dict *dict = (struct sql_dict *)ctx->ctx.dict;
@@ -651,6 +648,7 @@
 		ctx->result = NULL;
 	}
 
+	string_t *query = t_str_new(256);
 	str_append(query, "SELECT ");
 	if ((ctx->flags & DICT_ITERATE_FLAG_NO_VALUE) == 0)
 		str_printfa(query, "%s,", map->value_field);
@@ -677,9 +675,12 @@
 		recurse_type = SQL_DICT_RECURSE_NONE;
 	else
 		recurse_type = SQL_DICT_RECURSE_ONE;
+
+	ARRAY_TYPE(sql_dict_param) params;
+	t_array_init(&params, 4);
 	if (sql_dict_where_build(dict, map, &values,
 				 ctx->paths[ctx->path_idx][0],
-				 recurse_type, query, params, error_r) < 0)
+				 recurse_type, query, &params, error_r) < 0)
 		return -1;
 
 	if ((ctx->flags & DICT_ITERATE_FLAG_SORT_BY_KEY) != 0) {
@@ -698,6 +699,7 @@
 			(unsigned long long)(ctx->ctx.max_rows - ctx->ctx.row_count));
 	}
 
+	*stmt_r = sql_dict_statement_init(dict->db, str_c(query), &params);
 	ctx->map = map;
 	return 1;
 }
@@ -713,15 +715,12 @@
 
 static int sql_dict_iterate_next_query(struct sql_dict_iterate_context *ctx)
 {
-	struct sql_dict *dict = (struct sql_dict *)ctx->ctx.dict;
-	string_t *query = t_str_new(256);
-	ARRAY_TYPE(sql_dict_param) params;
+	struct sql_statement *stmt;
 	const char *error;
 	unsigned int path_idx = ctx->path_idx;
 	int ret;
 
-	t_array_init(&params, 4);
-	ret = sql_dict_iterate_build_next_query(ctx, query, &params, &error);
+	ret = sql_dict_iterate_build_next_query(ctx, &stmt, &error);
 	if (ret < 0) {
 		/* failed */
 		i_error("sql dict iterate failed for %s: %s",
@@ -732,8 +731,6 @@
 		return 0;
 	}
 
-	struct sql_statement *stmt =
-		sql_dict_statement_init(dict->db, str_c(query), &params);
 	if ((ctx->flags & DICT_ITERATE_FLAG_ASYNC) == 0) {
 		ctx->result = sql_statement_query_s(&stmt);
 	} else {
@@ -1012,14 +1009,15 @@
 	char key1;
 };
 
-static int sql_dict_set_query(const struct dict_sql_build_query *build,
-			      const char **query_r,
-			      ARRAY_TYPE(sql_dict_param) *params,
+static int sql_dict_set_query(struct sql_dict_transaction_context *ctx,
+			      const struct dict_sql_build_query *build,
+			      struct sql_statement **stmt_r,
 			      const char **error_r)
 {
 	struct sql_dict *dict = build->dict;
 	const struct dict_sql_build_query_field *fields;
 	const struct dict_sql_field *sql_fields;
+	ARRAY_TYPE(sql_dict_param) params;
 	const char *const *extra_values;
 	unsigned int i, field_count, count, count2;
 	string_t *prefix, *suffix;
@@ -1027,6 +1025,7 @@
 	fields = array_get(&build->fields, &field_count);
 	i_assert(field_count > 0);
 
+	t_array_init(&params, 4);
 	prefix = t_str_new(64);
 	suffix = t_str_new(256);
 	str_printfa(prefix, "INSERT INTO %s", fields[0].map->table);
@@ -1044,7 +1043,7 @@
 		str_append_c(suffix, '?');
 		if (sql_dict_value_get(fields[i].map,
 				       value_type, "value", fields[i].value,
-				       "", params, error_r) < 0)
+				       "", &params, error_r) < 0)
 			return -1;
 	}
 	if (build->key1 == DICT_PATH_PRIVATE[0]) {
@@ -1062,14 +1061,14 @@
 		str_append(suffix, ",?");
 		if (sql_dict_field_get_value(fields[0].map, &sql_fields[i],
 					     extra_values[i], "",
-					     params, error_r) < 0)
+					     &params, error_r) < 0)
 			return -1;
 	}
 
 	str_append_str(prefix, suffix);
 	str_append_c(prefix, ')');
 	if (!dict->has_on_duplicate_key) {
-		*query_r = str_c(prefix);
+		*stmt_r = sql_dict_transaction_stmt_init(ctx, str_c(prefix), &params);
 		return 0;
 	}
 
@@ -1087,10 +1086,10 @@
 		str_append_c(prefix, '?');
 		if (sql_dict_value_get(fields[i].map,
 				       value_type, "value", fields[i].value,
-				       "", params, error_r) < 0)
+				       "", &params, error_r) < 0)
 			return -1;
 	}
-	*query_r = str_c(prefix);
+	*stmt_r = sql_dict_transaction_stmt_init(ctx, str_c(prefix), &params);
 	return 0;
 }
 
@@ -1133,11 +1132,11 @@
 		(struct sql_dict_transaction_context *)_ctx;
 	struct sql_dict *dict = (struct sql_dict *)_ctx->dict;
 	const struct dict_sql_map *map;
+	struct sql_statement *stmt;
 	ARRAY_TYPE(const_string) values;
 	struct dict_sql_build_query build;
 	struct dict_sql_build_query_field field;
-	ARRAY_TYPE(sql_dict_param) params;
-	const char *query, *error;
+	const char *error;
 
 	map = sql_dict_find_map(dict, key, &values);
 	if (map == NULL) {
@@ -1156,13 +1155,11 @@
 	build.extra_values = &values;
 	build.key1 = key[0];
 
-	if (sql_dict_set_query(&build, &query, &params, &error) < 0) {
+	if (sql_dict_set_query(ctx, &build, &stmt, &error) < 0) {
 		i_error("dict-sql: Failed to set %s=%s: %s",
 			key, value, error);
 		ctx->failed = TRUE;
 	} else {
-		struct sql_statement *stmt =
-			sql_dict_transaction_stmt_init(ctx, query, &params);
 		sql_update_stmt(ctx->sql_ctx, &stmt);
 	}
 }
@@ -1359,8 +1356,8 @@
 	} else {
 		struct dict_sql_build_query build;
 		struct dict_sql_build_query_field *field;
-		ARRAY_TYPE(sql_dict_param) params;
-		const char *query, *error;
+		struct sql_statement *stmt;
+		const char *error;
 
 		i_zero(&build);
 		build.dict = dict;
@@ -1375,13 +1372,10 @@
 		field->map = map;
 		field->value = value;
 
-		t_array_init(&params, 4);
-		if (sql_dict_set_query(&build, &query, &params, &error) < 0) {
+		if (sql_dict_set_query(ctx, &build, &stmt, &error) < 0) {
 			i_error("dict-sql: Failed to set %s: %s", key, error);
 			ctx->failed = TRUE;
 		} else {
-			struct sql_statement *stmt =
-				sql_dict_transaction_stmt_init(ctx, query, &params);
 			sql_update_stmt(ctx->sql_ctx, &stmt);
 		}
 		i_free_and_null(ctx->prev_set_value);