driver-pgsql.c revision d1a0845aed2bbbe9435e96bd10bd774ed194ca4b
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2004-2015 Dovecot authors, see the included COPYING file */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen bool (*next_callback)(void *);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen unsigned char *value;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen const char **fields;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen const char **values;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen ARRAY(struct pgsql_binary_value) binary_values;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenextern const struct sql_result driver_pgsql_result;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void result_finish(struct pgsql_result *result);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainentransaction_update_callback(struct sql_result *result,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic const char *pgsql_prefix(struct pgsql_db *db)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void driver_pgsql_set_state(struct pgsql_db *db, enum sql_db_state state)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen i_assert(state == SQL_DB_STATE_BUSY || db->cur_result == NULL);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* switch back to original ioloop in case the caller wants to
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic bool driver_pgsql_next_callback(struct pgsql_db *db)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen bool (*next_callback)(void *) = db->next_callback;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void driver_pgsql_stop_io(struct pgsql_db *db)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void driver_pgsql_close(struct pgsql_db *db)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen driver_pgsql_set_state(db, SQL_DB_STATE_DISCONNECTED);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* running a sync query, stop it */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic const char *last_error(struct pgsql_db *db)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen const char *msg;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen return "(no error set)";
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* Error message should contain trailing \n, we don't want it */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void connect_callback(struct pgsql_db *db)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen while ((ret = PQconnectPoll(db->pg)) == PGRES_POLLING_ACTIVE)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen i_error("%s: Connect failed to database %s: %s (state: %s)",
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen pgsql_prefix(db), PQdb(db->pg), last_error(db), db->connect_state);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen db->io = io_add(PQsocket(db->pg), io_dir, connect_callback, db);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen driver_pgsql_set_state(db, SQL_DB_STATE_IDLE);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* driver_pgsql_sync_init() waiting for connection to
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void driver_pgsql_connect_timeout(struct pgsql_db *db)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen unsigned int secs = ioloop_time - db->api.last_connect_try;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen i_error("%s: Connect failed: Timeout after %u seconds (state: %s)",
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic int driver_pgsql_connect(struct sql_db *_db)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen i_assert(db->api.state == SQL_DB_STATE_DISCONNECTED);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen i_fatal("%s: PQconnectStart() failed (out of memory)",
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen i_error("%s: Connect failed to database %s: %s",
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen pgsql_prefix(db), PQdb(db->pg), last_error(db));
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* PQconnectStart() blocks on host name resolving. Log a warning if
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen it takes too long. Also don't include time spent on that in the
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen connect timeout (by refreshing ioloop time). */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen msecs = timeval_diff_msecs(&ioloop_timeval, &tv_start);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* nonblocking connecting begins. */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen i_error("%s: PQsetnonblocking() failed", pgsql_prefix(db));
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen db->to_connect = timeout_add(SQL_CONNECT_TIMEOUT_SECS * 1000,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen db->io = io_add(PQsocket(db->pg), IO_WRITE, connect_callback, db);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen driver_pgsql_set_state(db, SQL_DB_STATE_CONNECTING);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void driver_pgsql_disconnect(struct sql_db *_db)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen if (db->cur_result != NULL && db->cur_result->to != NULL) {
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic struct sql_db *driver_pgsql_init_v(const char *connect_string)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen db->connect_string = i_strdup(connect_string);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen const char *const *arg = t_strsplit(connect_string, " ");
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void driver_pgsql_deinit_v(struct sql_db *_db)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void driver_pgsql_set_idle(struct pgsql_db *db)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen driver_pgsql_set_state(db, SQL_DB_STATE_IDLE);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void consume_results(struct pgsql_db *db)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void driver_pgsql_result_free(struct sql_result *_result)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_db *db = (struct pgsql_db *)_result->db;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_result *result = (struct pgsql_result *)_result;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen success = result->pgres != NULL && !db->fatal_error;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* we'll have to read the rest of the results as well */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen if (array_is_created(&result->binary_values)) {
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen array_foreach_modifiable(&result->binary_values, value)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void result_finish(struct pgsql_result *result)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_db *db = (struct pgsql_db *)result->api.db;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* if connection to server was lost, we don't yet see that the
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen connection is bad. we only see the fatal error, so assume it also
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen means disconnection. */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen if (PQstatus(db->pg) == CONNECTION_BAD || result->pgres == NULL ||
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen PQresultStatus(result->pgres) == PGRES_FATAL_ERROR)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen result->callback(&result->api, result->context);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen free_result = db->sync_result != &result->api;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen i_assert(!free_result || result->api.refcount > 0);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void get_result(struct pgsql_result *result)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_db *db = (struct pgsql_db *)result->api.db;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void flush_callback(struct pgsql_result *result)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_db *db = (struct pgsql_db *)result->api.db;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* all flushed */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void query_timeout(struct pgsql_result *result)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_db *db = (struct pgsql_db *)result->api.db;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen i_error("%s: Query timed out, aborting", pgsql_prefix(db));
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void do_query(struct pgsql_result *result, const char *query)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_db *db = (struct pgsql_db *)result->api.db;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen driver_pgsql_set_state(db, SQL_DB_STATE_BUSY);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen result->to = timeout_add(SQL_QUERY_TIMEOUT_SECS * 1000,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* failed to send query */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* write blocks */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic const char *
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainendriver_pgsql_escape_string(struct sql_db *_db, const char *string)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen if (db->api.state == SQL_DB_STATE_DISCONNECTED) {
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* try connecting again */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen if (db->api.state != SQL_DB_STATE_DISCONNECTED) {
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen len = PQescapeStringConn(db->pg, to, string, len, &error);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void exec_callback(struct sql_result *_result,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_db *db = (struct pgsql_db *)_result->db;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen i_error("%s: sql_exec() failed: %s", pgsql_prefix(db), last_error(db));
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void driver_pgsql_exec(struct sql_db *db, const char *query)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void driver_pgsql_query(struct sql_db *db, const char *query,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen sql_query_callback_t *callback, void *context)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void pgsql_query_s_callback(struct sql_result *result, void *context)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void driver_pgsql_sync_init(struct pgsql_db *db)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen i_assert(db->api.state == SQL_DB_STATE_CONNECTING);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* have to move our existing I/O and timeout handlers to new I/O loop */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen db->to_connect = timeout_add(SQL_CONNECT_TIMEOUT_SECS * 1000,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen db->io = io_add(PQsocket(db->pg), db->io_dir, connect_callback, db);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* wait for connecting to finish */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void driver_pgsql_sync_deinit(struct pgsql_db *db)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic struct sql_result *
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainendriver_pgsql_sync_query(struct pgsql_db *db, const char *query)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen driver_pgsql_query(&db->api, query, pgsql_query_s_callback, db);
39413f6b07f7e4f4c1aeeecab73a2c454c84e308Timo Sirainen /* we don't end up in pgsql's free function, so sync_result
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen won't be set to NULL if we don't do it here. */
39413f6b07f7e4f4c1aeeecab73a2c454c84e308Timo Sirainenstatic struct sql_result *
39413f6b07f7e4f4c1aeeecab73a2c454c84e308Timo Sirainendriver_pgsql_query_s(struct sql_db *_db, const char *query)
39413f6b07f7e4f4c1aeeecab73a2c454c84e308Timo Sirainenstatic int driver_pgsql_result_next_row(struct sql_result *_result)
39413f6b07f7e4f4c1aeeecab73a2c454c84e308Timo Sirainen struct pgsql_result *result = (struct pgsql_result *)_result;
39413f6b07f7e4f4c1aeeecab73a2c454c84e308Timo Sirainen struct pgsql_db *db = (struct pgsql_db *)_result->db;
39413f6b07f7e4f4c1aeeecab73a2c454c84e308Timo Sirainen /* second time we're here */
39413f6b07f7e4f4c1aeeecab73a2c454c84e308Timo Sirainen /* end of this packet. see if there's more. FIXME: this may
39413f6b07f7e4f4c1aeeecab73a2c454c84e308Timo Sirainen block, but the current API doesn't provide a non-blocking
39413f6b07f7e4f4c1aeeecab73a2c454c84e308Timo Sirainen way to do this.. */
39413f6b07f7e4f4c1aeeecab73a2c454c84e308Timo Sirainen /* no rows returned */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* nonfatal error */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* treat as fatal error */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic void driver_pgsql_result_fetch_fields(struct pgsql_result *result)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen unsigned int i;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* @UNSAFE */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen result->fields_count = PQnfields(result->pgres);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen result->fields = i_new(const char *, result->fields_count);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen result->fields[i] = PQfname(result->pgres, i);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic unsigned int
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainendriver_pgsql_result_get_fields_count(struct sql_result *_result)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_result *result = (struct pgsql_result *)_result;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic const char *
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainendriver_pgsql_result_get_field_name(struct sql_result *_result, unsigned int idx)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_result *result = (struct pgsql_result *)_result;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic int driver_pgsql_result_find_field(struct sql_result *_result,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_result *result = (struct pgsql_result *)_result;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen unsigned int i;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen if (strcmp(result->fields[i], field_name) == 0)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic const char *
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainendriver_pgsql_result_get_field_value(struct sql_result *_result,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen unsigned int idx)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_result *result = (struct pgsql_result *)_result;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen if (PQgetisnull(result->pgres, result->rownum, idx))
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen return PQgetvalue(result->pgres, result->rownum, idx);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic const unsigned char *
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainendriver_pgsql_result_get_field_value_binary(struct sql_result *_result,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_result *result = (struct pgsql_result *)_result;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen if (PQgetisnull(result->pgres, result->rownum, idx)) {
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen value = PQgetvalue(result->pgres, result->rownum, idx);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen if (!array_is_created(&result->binary_values))
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen i_array_init(&result->binary_values, idx + 1);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen binary_value = array_idx_modifiable(&result->binary_values, idx);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic const char *
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainendriver_pgsql_result_find_field_value(struct sql_result *result,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen idx = driver_pgsql_result_find_field(result, field_name);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen return driver_pgsql_result_get_field_value(result, idx);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic const char *const *
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainendriver_pgsql_result_get_values(struct sql_result *_result)
b6dff2ba7a4640c1c4fa8fcad5602d236c31a2e4Timo Sirainen struct pgsql_result *result = (struct pgsql_result *)_result;
b6dff2ba7a4640c1c4fa8fcad5602d236c31a2e4Timo Sirainen unsigned int i;
b6dff2ba7a4640c1c4fa8fcad5602d236c31a2e4Timo Sirainen result->values = i_new(const char *, result->fields_count);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* @UNSAFE */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen driver_pgsql_result_get_field_value(_result, i);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic const char *driver_pgsql_result_get_error(struct sql_result *_result)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_result *result = (struct pgsql_result *)_result;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_db *db = (struct pgsql_db *)_result->db;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen const char *msg;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* connection error */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen return "(no error set)";
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* Error message should contain trailing \n, we don't want it */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainendriver_pgsql_transaction_begin(struct sql_db *db)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen ctx = i_new(struct pgsql_transaction_context, 1);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* we need to be able to handle multiple open transactions, so at least
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen for now just keep them in memory until commit time. */
b6dff2ba7a4640c1c4fa8fcad5602d236c31a2e4Timo Sirainen ctx->query_pool = pool_alloconly_create("pgsql transaction", 1024);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainendriver_pgsql_transaction_free(struct pgsql_transaction_context *ctx)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainentransaction_commit_callback(struct sql_result *result,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen ctx->callback(sql_result_get_error(result), ctx->context);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic bool transaction_send_next(void *context)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_transaction_context *ctx = context;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen if (ctx->ctx.db->state == SQL_DB_STATE_BUSY) {
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* kludgy.. */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainentransaction_begin_callback(struct sql_result *result,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_db *db = (struct pgsql_db *)result->db;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen ctx->callback(sql_result_get_error(result), ctx->context);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainentransaction_update_callback(struct sql_result *result,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen (struct pgsql_transaction_context *)query->trans;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_db *db = (struct pgsql_db *)result->db;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen ctx->callback(sql_result_get_error(result), ctx->context);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_result *pg_result = (struct pgsql_result *)result;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen if (str_to_uint(PQcmdTuples(pg_result->pgres),
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainentransaction_trans_query_callback(struct sql_result *result,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen (struct pgsql_transaction_context *)query->trans;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen ctx->callback(sql_result_get_error(result), ctx->context);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_result *pg_result = (struct pgsql_result *)result;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen if (str_to_uint(PQcmdTuples(pg_result->pgres),
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainendriver_pgsql_transaction_commit(struct sql_transaction_context *_ctx,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen sql_commit_callback_t *callback, void *context)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen callback(ctx->failed ? ctx->error : NULL, context);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* just a single query, send it */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen transaction_trans_query_callback, _ctx->head);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* multiple queries, use a transaction */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen i_assert(_ctx->db->v.query == driver_pgsql_query);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen sql_query(_ctx->db, "BEGIN", transaction_begin_callback, ctx);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainencommit_multi_fail(struct pgsql_transaction_context *ctx,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen ctx->error = t_strdup_printf("%s (query: %s)",
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic struct sql_result *
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainendriver_pgsql_transaction_commit_multi(struct pgsql_transaction_context *ctx)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_db *db = (struct pgsql_db *)ctx->ctx.db;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen result = driver_pgsql_sync_query(db, "BEGIN");
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* send queries */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen for (query = ctx->ctx.head; query != NULL; query = query->next) {
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen result = driver_pgsql_sync_query(db, query->query);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen if (str_to_uint(PQcmdTuples(pg_result->pgres),
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen return driver_pgsql_sync_query(db, ctx->failed ?
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainendriver_pgsql_try_commit_s(struct pgsql_transaction_context *ctx,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen const char **error_r)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct sql_transaction_context *_ctx = &ctx->ctx;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_db *db = (struct pgsql_db *)_ctx->db;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct sql_transaction_query *single_query = NULL;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* just a single query, send it */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen result = sql_query_s(_ctx->db, single_query->query);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* multiple queries, use a transaction */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen result = driver_pgsql_transaction_commit_multi(ctx);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen if (str_to_uint(PQcmdTuples(pg_result->pgres),
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainendriver_pgsql_transaction_commit_s(struct sql_transaction_context *_ctx,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen const char **error_r)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct pgsql_db *db = (struct pgsql_db *)_ctx->db;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen if (_ctx->db->state == SQL_DB_STATE_DISCONNECTED) {
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainendriver_pgsql_transaction_rollback(struct sql_transaction_context *_ctx)
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainendriver_pgsql_update(struct sql_transaction_context *_ctx, const char *query,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen sql_transaction_add_query(_ctx, ctx->query_pool, query, affected_rows);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstatic const char *
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainendriver_pgsql_escape_blob(struct sql_db *_db ATTR_UNUSED,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenconst struct sql_result driver_pgsql_result = {