From: Timo Sirainen Date: Tue, 12 May 2009 23:34:06 +0000 (-0400) Subject: SQL API change: SQL results can be now refed/unrefed. X-Git-Tag: 2.0.alpha1~782 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3656c91dcb8336814bebd4500e81c3dde25233e6;p=thirdparty%2Fdovecot%2Fcore.git SQL API change: SQL results can be now refed/unrefed. --HG-- branch : HEAD --- diff --git a/src/lib-dict/dict-sql.c b/src/lib-dict/dict-sql.c index 8475047ca8..10c740d5f3 100644 --- a/src/lib-dict/dict-sql.c +++ b/src/lib-dict/dict-sql.c @@ -290,7 +290,7 @@ static int sql_dict_lookup(struct dict *_dict, pool_t pool, p_strdup(pool, sql_result_get_field_value(result, 0)); } - sql_result_free(result); + sql_result_unref(result); return ret; } @@ -444,7 +444,7 @@ static void sql_dict_iterate_deinit(struct dict_iterate_context *_ctx) (struct sql_dict_iterate_context *)_ctx; if (ctx->result != NULL) - sql_result_free(ctx->result); + sql_result_unref(ctx->result); str_free(&ctx->key); i_free(ctx->path); i_free(ctx); diff --git a/src/lib-sql/driver-mysql.c b/src/lib-sql/driver-mysql.c index e4bd52ee16..19402be89e 100644 --- a/src/lib-sql/driver-mysql.c +++ b/src/lib-sql/driver-mysql.c @@ -419,7 +419,7 @@ static void driver_mysql_query(struct sql_db *db, const char *query, result->callback = TRUE; callback(result, context); result->callback = FALSE; - sql_result_free(result); + sql_result_unref(result); } static struct sql_result * @@ -450,6 +450,7 @@ driver_mysql_query_s(struct sql_db *_db, const char *query) break; } + result->api.refcount = 1; result->conn = conn; return &result->api; } @@ -458,7 +459,8 @@ static void driver_mysql_result_free(struct sql_result *_result) { struct mysql_result *result = (struct mysql_result *)_result; - if (_result == &sql_not_connected_result || _result->callback) + i_assert(_result != &sql_not_connected_result); + if (_result->callback) return; if (result->result != NULL) @@ -607,7 +609,7 @@ static int transaction_send_query(struct mysql_transaction_context *ctx, ctx->failed = TRUE; ret = -1; } - sql_result_free(result); + sql_result_unref(result); return ret; } diff --git a/src/lib-sql/driver-pgsql.c b/src/lib-sql/driver-pgsql.c index e4a8996e07..b00f988c9b 100644 --- a/src/lib-sql/driver-pgsql.c +++ b/src/lib-sql/driver-pgsql.c @@ -351,7 +351,7 @@ static void result_finish(struct pgsql_result *result) } } if (free_result) - driver_pgsql_result_free(&result->api); + sql_result_unref(&result->api); } static void get_result(struct pgsql_result *result) @@ -582,6 +582,7 @@ static void driver_pgsql_exec_full(struct sql_db *db, const char *query, result = i_new(struct pgsql_result, 1); result->api = driver_pgsql_result; result->api.db = db; + result->api.refcount = 1; result->callback = exec_callback; if (retry_query) { result->query = i_strdup(query); @@ -605,6 +606,7 @@ driver_pgsql_query_full(struct sql_db *db, const char *query, result = i_new(struct pgsql_result, 1); result->api = driver_pgsql_result; result->api.db = db; + result->api.refcount = 1; result->callback = callback; result->context = context; if (retry_query) { @@ -995,7 +997,7 @@ driver_pgsql_transaction_commit_s(struct sql_transaction_context *_ctx, *error_r = sql_result_get_error(result); } if (result != NULL) - sql_result_free(result); + sql_result_unref(result); i_assert(ctx->refcount == 1); driver_pgsql_transaction_unref(ctx); diff --git a/src/lib-sql/driver-sqlite.c b/src/lib-sql/driver-sqlite.c index fc0e1d7880..3b590e8860 100644 --- a/src/lib-sql/driver-sqlite.c +++ b/src/lib-sql/driver-sqlite.c @@ -143,7 +143,7 @@ static void driver_sqlite_query(struct sql_db *db, const char *query, result->callback = TRUE; callback(result, context); result->callback = FALSE; - sql_result_free(result); + sql_result_unref(result); } static struct sql_result * @@ -172,6 +172,7 @@ driver_sqlite_query_s(struct sql_db *_db, const char *query) } } result->api.db = _db; + result->api.refcount = 1; return &result->api; } diff --git a/src/lib-sql/sql-api-private.h b/src/lib-sql/sql-api-private.h index 5ad53c47d5..d5d2cfcbbf 100644 --- a/src/lib-sql/sql-api-private.h +++ b/src/lib-sql/sql-api-private.h @@ -73,6 +73,7 @@ struct sql_field_map { struct sql_result { struct sql_result_vfuncs v; + int refcount; struct sql_db *db; const struct sql_field_def *fields; diff --git a/src/lib-sql/sql-api.c b/src/lib-sql/sql-api.c index 2456b04acd..408d1aa9ac 100644 --- a/src/lib-sql/sql-api.c +++ b/src/lib-sql/sql-api.c @@ -97,8 +97,17 @@ struct sql_result *sql_query_s(struct sql_db *db, const char *query) return db->v.query_s(db, query); } -void sql_result_free(struct sql_result *result) +void sql_result_ref(struct sql_result *result) { + result->refcount++; +} + +void sql_result_unref(struct sql_result *result) +{ + i_assert(result->refcount > 0); + if (--result->refcount > 0) + return; + i_free(result->map); result->v.free(result); } diff --git a/src/lib-sql/sql-api.h b/src/lib-sql/sql-api.h index dbecafcae5..ad6a1cd661 100644 --- a/src/lib-sql/sql-api.h +++ b/src/lib-sql/sql-api.h @@ -94,8 +94,10 @@ void sql_result_setup_fetch(struct sql_result *result, occurred. This needs to be the first call for result. */ int sql_result_next_row(struct sql_result *result); -/* Needs to be called only with sql_query_s(). */ -void sql_result_free(struct sql_result *result); +void sql_result_ref(struct sql_result *result); +/* Needs to be called only with sql_query_s() or when result has been + explicitly referenced. */ +void sql_result_unref(struct sql_result *result); /* Return number of fields in result. */ unsigned int sql_result_get_fields_count(struct sql_result *result);