]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
SQL API change: SQL results can be now refed/unrefed.
authorTimo Sirainen <tss@iki.fi>
Tue, 12 May 2009 23:34:06 +0000 (19:34 -0400)
committerTimo Sirainen <tss@iki.fi>
Tue, 12 May 2009 23:34:06 +0000 (19:34 -0400)
--HG--
branch : HEAD

src/lib-dict/dict-sql.c
src/lib-sql/driver-mysql.c
src/lib-sql/driver-pgsql.c
src/lib-sql/driver-sqlite.c
src/lib-sql/sql-api-private.h
src/lib-sql/sql-api.c
src/lib-sql/sql-api.h

index 8475047ca80f889b66e75b438c944721fd9b1564..10c740d5f3ecd9ca21b3bfeac92412f22d2c51ff 100644 (file)
@@ -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);
index e4bd52ee166aa1c46bae576191a604d4c83c44f3..19402be89e2bb20ea8f17196c2b46e3251002b6d 100644 (file)
@@ -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;
 }
 
index e4a8996e0797fc3ccdbe8e8a948cb37122a11119..b00f988c9b1eb7316a50341b0ede9bf5bb2c98d4 100644 (file)
@@ -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);
index fc0e1d7880efebbeb6c0e0998efeab7267edcf1a..3b590e886071a3d949c8f9debe292850b9123a7e 100644 (file)
@@ -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;
 }
index 5ad53c47d595a7d57e10463a2b2ad790794f59a7..d5d2cfcbbfe1348d6d0d3864c81dc5a8be8478f9 100644 (file)
@@ -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;
index 2456b04acdc00494879f804d288fc5044edf1c21..408d1aa9ac5a1bc7415c76290efafc4f1f0bc2db 100644 (file)
@@ -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);
 }
index dbecafcae5ee0b1a1c996e3afe2d794269d3d340..ad6a1cd66115e43ac9546ea675cab63fb89b5e98 100644 (file)
@@ -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);