adaptions to libgnunetpq api change (#5733)

This commit is contained in:
Christian Grothoff 2019-10-11 23:28:05 +02:00
parent 9adc4c9ffc
commit 316a77a245
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
3 changed files with 1392 additions and 1558 deletions

View File

@ -61,7 +61,7 @@ struct TALER_AUDITORDB_Session
/** /**
* Postgres connection handle. * Postgres connection handle.
*/ */
PGconn *conn; struct GNUNET_PQ_Context *conn;
}; };
@ -91,71 +91,6 @@ struct PostgresClosure
}; };
/**
* Function called by libpq whenever it wants to log something.
* We already log whenever we care, so this function does nothing
* and merely exists to silence the libpq logging.
*
* @param arg NULL
* @param res information about some libpq event
*/
static void
pq_notice_receiver_cb (void *arg,
const PGresult *res)
{
/* do nothing, intentionally */
}
/**
* Function called by libpq whenever it wants to log something.
* We log those using the Taler logger.
*
* @param arg NULL
* @param message information about some libpq event
*/
static void
pq_notice_processor_cb (void *arg,
const char *message)
{
LOG (GNUNET_ERROR_TYPE_INFO,
"%s",
message);
}
/**
* Establish connection to the Postgres database
* and initialize callbacks for logging.
*
* @param pc configuration to use
* @return NULL on error
*/
static PGconn *
connect_to_postgres (struct PostgresClosure *pc)
{
PGconn *conn;
conn = PQconnectdb (pc->connection_cfg_str);
if (CONNECTION_OK !=
PQstatus (conn))
{
TALER_LOG_ERROR ("Database connection failed: %s\n",
PQerrorMessage (conn));
GNUNET_break (0);
PQfinish (conn);
return NULL;
}
PQsetNoticeReceiver (conn,
&pq_notice_receiver_cb,
NULL);
PQsetNoticeProcessor (conn,
&pq_notice_processor_cb,
NULL);
return conn;
}
/** /**
* Drop all Taler tables. This should only be used by testcases. * Drop all Taler tables. This should only be used by testcases.
* *
@ -198,25 +133,23 @@ postgres_drop_tables (void *cls,
GNUNET_PQ_make_execute ("DROP TABLE IF EXISTS auditor_exchanges CASCADE;"), GNUNET_PQ_make_execute ("DROP TABLE IF EXISTS auditor_exchanges CASCADE;"),
GNUNET_PQ_EXECUTE_STATEMENT_END GNUNET_PQ_EXECUTE_STATEMENT_END
}; };
PGconn *conn; struct GNUNET_PQ_Context *conn;
int ret; int ret;
conn = connect_to_postgres (pc); conn = GNUNET_PQ_connect (pc->connection_cfg_str,
es,
NULL);
if (NULL == conn) if (NULL == conn)
return GNUNET_SYSERR; return GNUNET_SYSERR;
LOG (GNUNET_ERROR_TYPE_INFO, ret = GNUNET_OK;
"Dropping ALL tables\n"); if (drop_exchangelist)
ret = GNUNET_PQ_exec_statements (conn,
es);
if ( (ret >= 0) &&
(drop_exchangelist) )
ret = GNUNET_PQ_exec_statements (conn, ret = GNUNET_PQ_exec_statements (conn,
esx); esx);
/* TODO: we probably need a bit more fine-grained control /* TODO: we probably need a bit more fine-grained control
over drops for the '-r' option of taler-auditor; also, over drops for the '-r' option of taler-auditor; also,
for the testcase, we currently fail to drop the for the testcase, we currently fail to drop the
auditor_denominations table... */ auditor_denominations table... */
PQfinish (conn); GNUNET_PQ_disconnect (conn);
return ret; return ret;
} }
@ -479,28 +412,52 @@ postgres_create_tables (void *cls)
")"), ")"),
GNUNET_PQ_EXECUTE_STATEMENT_END GNUNET_PQ_EXECUTE_STATEMENT_END
}; };
PGconn *conn; struct GNUNET_PQ_Context *conn;
int ret;
conn = connect_to_postgres (pc); conn = GNUNET_PQ_connect (pc->connection_cfg_str,
es,
NULL);
if (NULL == conn) if (NULL == conn)
return GNUNET_SYSERR; return GNUNET_SYSERR;
ret = GNUNET_PQ_exec_statements (conn, GNUNET_PQ_disconnect (conn);
es); return GNUNET_OK;
PQfinish (conn);
return ret;
} }
/** /**
* Setup prepared statements. * Close thread-local database connection when a thread is destroyed.
* *
* @param db_conn connection handle to initialize * @param cls closure we get from pthreads (the db handle)
* @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
*/ */
static int static void
postgres_prepare (PGconn *db_conn) db_conn_destroy (void *cls)
{ {
struct TALER_AUDITORDB_Session *session = cls;
struct GNUNET_PQ_Context *db_conn;
if (NULL == session)
return;
db_conn = session->conn;
session->conn = NULL;
if (NULL != db_conn)
GNUNET_PQ_disconnect (db_conn);
GNUNET_free (session);
}
/**
* Get the thread-local database-handle.
* Connect to the db if the connection does not exist yet.
*
* @param cls the `struct PostgresClosure` with the plugin-specific state
* @return the database connection, or NULL on error
*/
static struct TALER_AUDITORDB_Session *
postgres_get_session (void *cls)
{
struct PostgresClosure *pc = cls;
struct GNUNET_PQ_Context *db_conn;
struct TALER_AUDITORDB_Session *session;
struct GNUNET_PQ_PreparedStatement ps[] = { struct GNUNET_PQ_PreparedStatement ps[] = {
/* used in #postgres_commit */ /* used in #postgres_commit */
GNUNET_PQ_make_prepare ("do_commit", GNUNET_PQ_make_prepare ("do_commit",
@ -1036,80 +993,23 @@ postgres_prepare (PGconn *db_conn)
GNUNET_PQ_PREPARED_STATEMENT_END GNUNET_PQ_PREPARED_STATEMENT_END
}; };
return GNUNET_PQ_prepare_statements (db_conn,
ps);
}
/**
* Close thread-local database connection when a thread is destroyed.
*
* @param cls closure we get from pthreads (the db handle)
*/
static void
db_conn_destroy (void *cls)
{
struct TALER_AUDITORDB_Session *session = cls;
PGconn *db_conn;
if (NULL == session)
return;
db_conn = session->conn;
if (NULL != db_conn)
PQfinish (db_conn);
GNUNET_free (session);
}
/**
* Get the thread-local database-handle.
* Connect to the db if the connection does not exist yet.
*
* @param cls the `struct PostgresClosure` with the plugin-specific state
* @return the database connection, or NULL on error
*/
static struct TALER_AUDITORDB_Session *
postgres_get_session (void *cls)
{
struct PostgresClosure *pc = cls;
PGconn *db_conn;
struct TALER_AUDITORDB_Session *session;
if (NULL != (session = pthread_getspecific (pc->db_conn_threadlocal))) if (NULL != (session = pthread_getspecific (pc->db_conn_threadlocal)))
{ {
if (CONNECTION_BAD == PQstatus (session->conn)) GNUNET_PQ_reconnect_if_down (session->conn);
{
/**
* Reset the thread-local database-handle. Disconnects from the
* DB. Needed after the database server restarts as we need to
* properly reconnect. */
GNUNET_assert (0 == pthread_setspecific (pc->db_conn_threadlocal,
NULL));
PQfinish (session->conn);
GNUNET_free (session);
}
else
{
return session; return session;
} }
} db_conn = GNUNET_PQ_connect (pc->connection_cfg_str,
db_conn = connect_to_postgres (pc); NULL,
ps);
if (NULL == db_conn) if (NULL == db_conn)
return NULL; return NULL;
if (GNUNET_OK !=
postgres_prepare (db_conn))
{
GNUNET_break (0);
PQfinish (db_conn);
return NULL;
}
session = GNUNET_new (struct TALER_AUDITORDB_Session); session = GNUNET_new (struct TALER_AUDITORDB_Session);
session->conn = db_conn; session->conn = db_conn;
if (0 != pthread_setspecific (pc->db_conn_threadlocal, if (0 != pthread_setspecific (pc->db_conn_threadlocal,
session)) session))
{ {
GNUNET_break (0); GNUNET_break (0);
PQfinish (db_conn); GNUNET_PQ_disconnect (db_conn);
GNUNET_free (session); GNUNET_free (session);
return NULL; return NULL;
} }
@ -1128,20 +1028,19 @@ static int
postgres_start (void *cls, postgres_start (void *cls,
struct TALER_AUDITORDB_Session *session) struct TALER_AUDITORDB_Session *session)
{ {
PGresult *result; struct GNUNET_PQ_ExecuteStatement es[] = {
GNUNET_PQ_make_execute ("START TRANSACTION ISOLATION LEVEL SERIALIZABLE"),
GNUNET_PQ_EXECUTE_STATEMENT_END
};
result = PQexec (session->conn, if (GNUNET_OK !=
"START TRANSACTION ISOLATION LEVEL SERIALIZABLE"); GNUNET_PQ_exec_statements (session->conn,
if (PGRES_COMMAND_OK != es))
PQresultStatus (result))
{ {
TALER_LOG_ERROR ("Failed to start transaction: %s\n", TALER_LOG_ERROR ("Failed to start transaction\n");
PQresultErrorMessage (result));
GNUNET_break (0); GNUNET_break (0);
PQclear (result);
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
PQclear (result);
return GNUNET_OK; return GNUNET_OK;
} }
@ -1157,13 +1056,14 @@ static void
postgres_rollback (void *cls, postgres_rollback (void *cls,
struct TALER_AUDITORDB_Session *session) struct TALER_AUDITORDB_Session *session)
{ {
PGresult *result; struct GNUNET_PQ_ExecuteStatement es[] = {
GNUNET_PQ_make_execute ("ROLLBACK"),
GNUNET_PQ_EXECUTE_STATEMENT_END
};
result = PQexec (session->conn, GNUNET_break (GNUNET_OK ==
"ROLLBACK"); GNUNET_PQ_exec_statements (session->conn,
GNUNET_break (PGRES_COMMAND_OK == es));
PQresultStatus (result));
PQclear (result);
} }
@ -1205,30 +1105,31 @@ postgres_gc (void *cls)
TALER_PQ_query_param_absolute_time (&now), TALER_PQ_query_param_absolute_time (&now),
GNUNET_PQ_query_param_end GNUNET_PQ_query_param_end
}; };
PGconn *conn; struct GNUNET_PQ_Context *conn;
enum GNUNET_DB_QueryStatus qs; enum GNUNET_DB_QueryStatus qs;
struct GNUNET_PQ_PreparedStatement ps[] = {
/* FIXME: this is obviously not going to be this easy... */
GNUNET_PQ_make_prepare ("gc_auditor",
"FIXME",
0),
GNUNET_PQ_PREPARED_STATEMENT_END
};
now = GNUNET_TIME_absolute_get (); now = GNUNET_TIME_absolute_get ();
conn = connect_to_postgres (pc); conn = GNUNET_PQ_connect (pc->connection_cfg_str,
NULL,
ps);
if (NULL == conn) if (NULL == conn)
return GNUNET_SYSERR; return GNUNET_SYSERR;
if (GNUNET_OK !=
postgres_prepare (conn))
{
PQfinish (conn);
return GNUNET_SYSERR;
}
/* FIXME: this is obviously not going to be this easy... */
qs = GNUNET_PQ_eval_prepared_non_select (conn, qs = GNUNET_PQ_eval_prepared_non_select (conn,
"gc_auditor", "gc_auditor",
params_time); params_time);
GNUNET_PQ_disconnect (conn);
if (0 > qs) if (0 > qs)
{ {
GNUNET_break (0); GNUNET_break (0);
PQfinish (conn);
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
PQfinish (conn);
return GNUNET_OK; return GNUNET_OK;
} }

View File

@ -85,7 +85,7 @@ struct TALER_EXCHANGEDB_Session
/** /**
* Postgres connection handle. * Postgres connection handle.
*/ */
PGconn *conn; struct GNUNET_PQ_Context *conn;
/** /**
* Name of the current transaction, for debugging. * Name of the current transaction, for debugging.
@ -104,7 +104,7 @@ struct PostgresClosure
/** /**
* Thread-local database connection. * Thread-local database connection.
* Contains a pointer to `PGconn` or NULL. * Contains a pointer to `struct GNUNET_PQ_Context` or NULL.
*/ */
pthread_key_t db_conn_threadlocal; pthread_key_t db_conn_threadlocal;
@ -168,19 +168,15 @@ postgres_drop_tables (void *cls)
GNUNET_PQ_make_execute ("DROP TABLE IF EXISTS denominations CASCADE;"), GNUNET_PQ_make_execute ("DROP TABLE IF EXISTS denominations CASCADE;"),
GNUNET_PQ_EXECUTE_STATEMENT_END GNUNET_PQ_EXECUTE_STATEMENT_END
}; };
PGconn *conn; struct GNUNET_PQ_Context *conn;
int ret;
/* FIXME: use GNUNET_PQ_connect_with_cfg instead? */ conn = GNUNET_PQ_connect (pc->connection_cfg_str,
conn = GNUNET_PQ_connect (pc->connection_cfg_str); es,
NULL);
if (NULL == conn) if (NULL == conn)
return GNUNET_SYSERR; return GNUNET_SYSERR;
GNUNET_log (GNUNET_ERROR_TYPE_INFO, GNUNET_PQ_disconnect (conn);
"Dropping ALL tables\n"); return GNUNET_OK;
ret = GNUNET_PQ_exec_statements (conn,
es);
PQfinish (conn);
return ret;
} }
@ -518,29 +514,74 @@ postgres_create_tables (void *cls)
"ON prewire(finished);"), "ON prewire(finished);"),
GNUNET_PQ_EXECUTE_STATEMENT_END GNUNET_PQ_EXECUTE_STATEMENT_END
}; };
PGconn *conn; struct GNUNET_PQ_Context *conn;
int ret;
/* FIXME: use GNUNET_PQ_connect_with_cfg instead? */ conn = GNUNET_PQ_connect (pc->connection_cfg_str,
conn = GNUNET_PQ_connect (pc->connection_cfg_str); es,
NULL);
if (NULL == conn) if (NULL == conn)
return GNUNET_SYSERR; return GNUNET_SYSERR;
ret = GNUNET_PQ_exec_statements (conn, GNUNET_PQ_disconnect (conn);
es); return GNUNET_OK;
PQfinish (conn);
return ret;
} }
/** /**
* Setup prepared statements. * Close thread-local database connection when a thread is destroyed.
* *
* @param db_conn connection handle to initialize * @param cls closure we get from pthreads (the db handle)
* @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
*/ */
static int static void
postgres_prepare (PGconn *db_conn) db_conn_destroy (void *cls)
{ {
struct TALER_EXCHANGEDB_Session *session = cls;
struct GNUNET_PQ_Context *db_conn;
if (NULL == session)
return;
db_conn = session->conn;
session->conn = NULL;
if (NULL != db_conn)
GNUNET_PQ_disconnect (session->conn);
GNUNET_free (session);
}
/**
* Get the thread-local database-handle.
* Connect to the db if the connection does not exist yet.
*
* @param cls the `struct PostgresClosure` with the plugin-specific state
* @return the database connection, or NULL on error
*/
static struct TALER_EXCHANGEDB_Session *
postgres_get_session (void *cls)
{
struct PostgresClosure *pc = cls;
struct GNUNET_PQ_Context *db_conn;
struct TALER_EXCHANGEDB_Session *session;
if (NULL != (session = pthread_getspecific (pc->db_conn_threadlocal)))
{
GNUNET_PQ_reconnect_if_down (session->conn);
return session;
}
{
#if AUTO_EXPLAIN
/* Enable verbose logging to see where queries do not
properly use indices */
struct GNUNET_PQ_ExecuteStatement es[] = {
GNUNET_PQ_make_try_execute ("LOAD 'auto_explain';"),
GNUNET_PQ_make_try_execute ("SET auto_explain.log_min_duration=50;"),
GNUNET_PQ_make_try_execute ("SET auto_explain.log_timing=TRUE;"),
GNUNET_PQ_make_try_execute ("SET auto_explain.log_analyze=TRUE;"),
GNUNET_PQ_make_try_execute ("SET enable_sort=OFF;"),
GNUNET_PQ_make_try_execute ("SET enable_seqscan=OFF;"),
GNUNET_PQ_EXECUTE_STATEMENT_END
};
#else
struct GNUNET_PQ_ExecuteStatement *es = NULL;
#endif
struct GNUNET_PQ_PreparedStatement ps[] = { struct GNUNET_PQ_PreparedStatement ps[] = {
/* Used in #postgres_insert_denomination_info() */ /* Used in #postgres_insert_denomination_info() */
GNUNET_PQ_make_prepare ("denomination_insert", GNUNET_PQ_make_prepare ("denomination_insert",
@ -1387,12 +1428,6 @@ postgres_prepare (PGconn *db_conn)
" WHERE (aggregation_tracking.deposit_serial_id = deposits.deposit_serial_id)))" " WHERE (aggregation_tracking.deposit_serial_id = deposits.deposit_serial_id)))"
" ORDER BY wire_deadline ASC", " ORDER BY wire_deadline ASC",
2), 2),
/* Used in #postgres_gc() */
GNUNET_PQ_make_prepare ("gc_prewire",
"DELETE"
" FROM prewire"
" WHERE finished=true;",
0),
/* Used in #postgres_select_wire_out_above_serial_id() */ /* Used in #postgres_select_wire_out_above_serial_id() */
GNUNET_PQ_make_prepare ("audit_get_wire_incr", GNUNET_PQ_make_prepare ("audit_get_wire_incr",
"SELECT" "SELECT"
@ -1658,122 +1693,22 @@ postgres_prepare (PGconn *db_conn)
GNUNET_PQ_make_prepare ("do_commit", GNUNET_PQ_make_prepare ("do_commit",
"COMMIT", "COMMIT",
0), 0),
GNUNET_PQ_make_prepare ("gc_denominations",
"DELETE"
" FROM denominations"
" WHERE expire_legal < $1;",
1),
GNUNET_PQ_make_prepare ("gc_reserves",
"DELETE"
" FROM reserves"
" WHERE gc_date < $1"
" AND current_balance_val = 0"
" AND current_balance_frac = 0;",
1),
GNUNET_PQ_make_prepare ("gc_wire_fee",
"DELETE"
" FROM wire_fee"
" WHERE end_date < $1;",
1),
GNUNET_PQ_PREPARED_STATEMENT_END GNUNET_PQ_PREPARED_STATEMENT_END
}; };
return GNUNET_PQ_prepare_statements (db_conn, db_conn = GNUNET_PQ_connect (pc->connection_cfg_str,
es,
ps); ps);
}
/**
* Close thread-local database connection when a thread is destroyed.
*
* @param cls closure we get from pthreads (the db handle)
*/
static void
db_conn_destroy (void *cls)
{
struct TALER_EXCHANGEDB_Session *session = cls;
PGconn *db_conn;
if (NULL == session)
return;
db_conn = session->conn;
if (NULL != db_conn)
PQfinish (db_conn);
GNUNET_free (session);
}
/**
* Get the thread-local database-handle.
* Connect to the db if the connection does not exist yet.
*
* @param cls the `struct PostgresClosure` with the plugin-specific state
* @return the database connection, or NULL on error
*/
static struct TALER_EXCHANGEDB_Session *
postgres_get_session (void *cls)
{
struct PostgresClosure *pc = cls;
PGconn *db_conn;
struct TALER_EXCHANGEDB_Session *session;
if (NULL != (session = pthread_getspecific (pc->db_conn_threadlocal)))
{
if (CONNECTION_BAD == PQstatus (session->conn))
{
/**
* Reset the thread-local database-handle. Disconnects from the
* DB. Needed after the database server restarts as we need to
* properly reconnect. */
GNUNET_assert (0 ==
pthread_setspecific (pc->db_conn_threadlocal,
NULL));
PQfinish (session->conn);
GNUNET_free (session);
} }
else
{
return session;
}
}
/* FIXME: use GNUNET_PQ_connect_with_cfg instead? */
db_conn = GNUNET_PQ_connect (pc->connection_cfg_str);
if (NULL == db_conn) if (NULL == db_conn)
return NULL; return NULL;
if (GNUNET_OK !=
postgres_prepare (db_conn))
{
GNUNET_break (0);
PQfinish (db_conn);
return NULL;
}
#if AUTO_EXPLAIN
/* Enable verbose logging to see where queries do not
properly use indices */
{
struct GNUNET_PQ_ExecuteStatement es[] = {
GNUNET_PQ_make_try_execute ("LOAD 'auto_explain';"),
GNUNET_PQ_make_try_execute ("SET auto_explain.log_min_duration=50;"),
GNUNET_PQ_make_try_execute ("SET auto_explain.log_timing=TRUE;"),
GNUNET_PQ_make_try_execute ("SET auto_explain.log_analyze=TRUE;"),
GNUNET_PQ_make_try_execute ("SET enable_sort=OFF;"),
GNUNET_PQ_make_try_execute ("SET enable_seqscan=OFF;"),
GNUNET_PQ_EXECUTE_STATEMENT_END
};
(void) GNUNET_PQ_exec_statements (db_conn,
es);
}
#endif
session = GNUNET_new (struct TALER_EXCHANGEDB_Session); session = GNUNET_new (struct TALER_EXCHANGEDB_Session);
session->conn = db_conn; session->conn = db_conn;
if (0 != pthread_setspecific (pc->db_conn_threadlocal, if (0 != pthread_setspecific (pc->db_conn_threadlocal,
session)) session))
{ {
GNUNET_break (0); GNUNET_break (0);
PQfinish (db_conn); GNUNET_PQ_disconnect (db_conn);
GNUNET_free (session); GNUNET_free (session);
return NULL; return NULL;
} }
@ -1795,25 +1730,22 @@ postgres_start (void *cls,
struct TALER_EXCHANGEDB_Session *session, struct TALER_EXCHANGEDB_Session *session,
const char *name) const char *name)
{ {
PGresult *result; struct GNUNET_PQ_ExecuteStatement es[] = {
ExecStatusType ex; GNUNET_PQ_make_execute ("START TRANSACTION ISOLATION LEVEL SERIALIZABLE"),
GNUNET_PQ_EXECUTE_STATEMENT_END
};
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Starting transaction on %p\n", "Starting transaction on %p\n",
session->conn); session->conn);
result = PQexec (session->conn, if (GNUNET_OK !=
"START TRANSACTION ISOLATION LEVEL SERIALIZABLE"); GNUNET_PQ_exec_statements (session->conn,
if (PGRES_COMMAND_OK != es))
(ex = PQresultStatus (result)))
{ {
TALER_LOG_ERROR ("Failed to start transaction (%s): %s\n", TALER_LOG_ERROR ("Failed to start transaction\n");
PQresStatus (ex),
PQerrorMessage (session->conn));
GNUNET_break (0); GNUNET_break (0);
PQclear (result);
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
PQclear (result);
session->transaction_name = name; session->transaction_name = name;
return GNUNET_OK; return GNUNET_OK;
} }
@ -1830,16 +1762,17 @@ static void
postgres_rollback (void *cls, postgres_rollback (void *cls,
struct TALER_EXCHANGEDB_Session *session) struct TALER_EXCHANGEDB_Session *session)
{ {
PGresult *result; struct GNUNET_PQ_ExecuteStatement es[] = {
GNUNET_PQ_make_execute ("ROLLBACK"),
GNUNET_PQ_EXECUTE_STATEMENT_END
};
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Rolling back transaction on %p\n", "Rolling back transaction on %p\n",
session->conn); session->conn);
result = PQexec (session->conn, GNUNET_break (GNUNET_OK ==
"ROLLBACK"); GNUNET_PQ_exec_statements (session->conn,
GNUNET_break (PGRES_COMMAND_OK == es));
PQresultStatus (result));
PQclear (result);
session->transaction_name = NULL; session->transaction_name = NULL;
} }
@ -1880,15 +1813,16 @@ static void
postgres_preflight (void *cls, postgres_preflight (void *cls,
struct TALER_EXCHANGEDB_Session *session) struct TALER_EXCHANGEDB_Session *session)
{ {
PGresult *result; struct GNUNET_PQ_ExecuteStatement es[] = {
ExecStatusType status; GNUNET_PQ_make_execute ("COMMIT"),
GNUNET_PQ_EXECUTE_STATEMENT_END
};
if (NULL == session->transaction_name) if (NULL == session->transaction_name)
return; /* all good */ return; /* all good */
result = PQexec (session->conn, if (GNUNET_OK ==
"COMMIT"); GNUNET_PQ_exec_statements (session->conn,
status = PQresultStatus (result); es))
if (PGRES_COMMAND_OK == status)
{ {
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"BUG: Preflight check committed transaction `%s'!\n", "BUG: Preflight check committed transaction `%s'!\n",
@ -1901,7 +1835,6 @@ postgres_preflight (void *cls,
session->transaction_name); session->transaction_name);
} }
session->transaction_name = NULL; session->transaction_name = NULL;
PQclear (result);
} }
@ -5038,7 +4971,7 @@ postgres_lookup_wire_transfer (void *cls,
* @param cb function to call with the result * @param cb function to call with the result
* @param cb_cls closure to pass to @a cb * @param cb_cls closure to pass to @a cb
* @return transaction status code * @return transaction status code
- */ - */
static enum GNUNET_DB_QueryStatus static enum GNUNET_DB_QueryStatus
postgres_wire_lookup_deposit_wtid (void *cls, postgres_wire_lookup_deposit_wtid (void *cls,
struct TALER_EXCHANGEDB_Session *session, struct TALER_EXCHANGEDB_Session *session,
@ -5618,8 +5551,10 @@ static int
postgres_start_deferred_wire_out (void *cls, postgres_start_deferred_wire_out (void *cls,
struct TALER_EXCHANGEDB_Session *session) struct TALER_EXCHANGEDB_Session *session)
{ {
PGresult *result; struct GNUNET_PQ_ExecuteStatement es[] = {
ExecStatusType ex; GNUNET_PQ_make_execute ("SET CONSTRAINTS wire_out_ref DEFERRED"),
GNUNET_PQ_EXECUTE_STATEMENT_END
};
postgres_preflight (cls, postgres_preflight (cls,
session); session);
@ -5628,22 +5563,17 @@ postgres_start_deferred_wire_out (void *cls,
session, session,
"deferred wire out")) "deferred wire out"))
return GNUNET_SYSERR; return GNUNET_SYSERR;
result = PQexec (session->conn, if (GNUNET_OK !=
"SET CONSTRAINTS wire_out_ref DEFERRED"); GNUNET_PQ_exec_statements (session->conn,
if (PGRES_COMMAND_OK != es))
(ex = PQresultStatus (result)))
{ {
TALER_LOG_ERROR ( TALER_LOG_ERROR (
"Failed to defer wire_out_ref constraint on transaction (%s): %s\n", "Failed to defer wire_out_ref constraint on transaction\n");
PQresStatus (ex),
PQerrorMessage (session->conn));
GNUNET_break (0); GNUNET_break (0);
PQclear (result);
postgres_rollback (cls, postgres_rollback (cls,
session); session);
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
PQclear (result);
return GNUNET_OK; return GNUNET_OK;
} }
@ -5711,7 +5641,7 @@ postgres_gc (void *cls)
TALER_PQ_query_param_absolute_time (&long_ago), TALER_PQ_query_param_absolute_time (&long_ago),
GNUNET_PQ_query_param_end GNUNET_PQ_query_param_end
}; };
PGconn *conn; struct GNUNET_PQ_Context *conn;
int ret; int ret;
now = GNUNET_TIME_absolute_get (); now = GNUNET_TIME_absolute_get ();
@ -5723,15 +5653,42 @@ postgres_gc (void *cls)
GNUNET_TIME_relative_multiply ( GNUNET_TIME_relative_multiply (
GNUNET_TIME_UNIT_YEARS, GNUNET_TIME_UNIT_YEARS,
10)); 10));
/* FIXME: use GNUNET_PQ_connect_with_cfg instead? */ {
conn = GNUNET_PQ_connect (pg->connection_cfg_str); struct GNUNET_PQ_PreparedStatement ps[] = {
/* Used in #postgres_gc() */
GNUNET_PQ_make_prepare ("gc_prewire",
"DELETE"
" FROM prewire"
" WHERE finished=true;",
0),
GNUNET_PQ_make_prepare ("gc_reserves",
"DELETE"
" FROM reserves"
" WHERE gc_date < $1"
" AND current_balance_val = 0"
" AND current_balance_frac = 0;",
1),
GNUNET_PQ_make_prepare ("gc_wire_fee",
"DELETE"
" FROM wire_fee"
" WHERE end_date < $1;",
1),
GNUNET_PQ_make_prepare ("gc_denominations",
"DELETE"
" FROM denominations"
" WHERE expire_legal < $1;",
1),
GNUNET_PQ_PREPARED_STATEMENT_END
};
conn = GNUNET_PQ_connect (pg->connection_cfg_str,
NULL,
ps);
}
if (NULL == conn) if (NULL == conn)
return GNUNET_SYSERR; return GNUNET_SYSERR;
ret = postgres_prepare (conn); ret = GNUNET_OK;
if (GNUNET_OK == ret) if ( (0 > GNUNET_PQ_eval_prepared_non_select (conn,
{
if (
(0 > GNUNET_PQ_eval_prepared_non_select (conn,
"gc_reserves", "gc_reserves",
params_time)) || params_time)) ||
(0 > GNUNET_PQ_eval_prepared_non_select (conn, (0 > GNUNET_PQ_eval_prepared_non_select (conn,
@ -5739,8 +5696,7 @@ postgres_gc (void *cls)
params_none)) || params_none)) ||
(0 > GNUNET_PQ_eval_prepared_non_select (conn, (0 > GNUNET_PQ_eval_prepared_non_select (conn,
"gc_wire_fee", "gc_wire_fee",
params_ancient_time)) params_ancient_time)) )
)
ret = GNUNET_SYSERR; ret = GNUNET_SYSERR;
/* This one may fail due to foreign key constraints from /* This one may fail due to foreign key constraints from
payback and reserves_out tables to known_coins; these payback and reserves_out tables to known_coins; these
@ -5750,8 +5706,7 @@ postgres_gc (void *cls)
(void) GNUNET_PQ_eval_prepared_non_select (conn, (void) GNUNET_PQ_eval_prepared_non_select (conn,
"gc_denominations", "gc_denominations",
params_time); params_time);
} GNUNET_PQ_disconnect (conn);
PQfinish (conn);
return ret; return ret;
} }

View File

@ -26,27 +26,14 @@
/** /**
* Setup prepared statements. * Setup prepared statements.
* *
* @param db_conn connection handle to initialize * @param db database handle to initialize
* @return #GNUNET_OK on success, #GNUNET_SYSERR on failure * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
*/ */
static int static int
postgres_prepare (PGconn *db_conn) postgres_prepare (struct GNUNET_PQ_Context *db)
{ {
PGresult *result; struct GNUNET_PQ_PreparedStatement ps[] = {
GNUNET_PQ_make_prepare ("test_insert",
#define PREPARE(name, sql, ...) \
do { \
result = PQprepare (db_conn, name, sql, __VA_ARGS__); \
if (PGRES_COMMAND_OK != PQresultStatus (result)) \
{ \
GNUNET_break (0); \
PQclear (result); result = NULL; \
return GNUNET_SYSERR; \
} \
PQclear (result); result = NULL; \
} while (0);
PREPARE ("test_insert",
"INSERT INTO test_pq (" "INSERT INTO test_pq ("
" hamount_val" " hamount_val"
",hamount_frac" ",hamount_frac"
@ -55,8 +42,8 @@ postgres_prepare (PGconn *db_conn)
",json" ",json"
") VALUES " ") VALUES "
"($1, $2, $3, $4, $5);", "($1, $2, $3, $4, $5);",
5, NULL); 5),
PREPARE ("test_select", GNUNET_PQ_make_prepare ("test_select",
"SELECT" "SELECT"
" hamount_val" " hamount_val"
",hamount_frac" ",hamount_frac"
@ -64,9 +51,12 @@ postgres_prepare (PGconn *db_conn)
",namount_frac" ",namount_frac"
",json" ",json"
" FROM test_pq;", " FROM test_pq;",
0, NULL); 0),
return GNUNET_OK; GNUNET_PQ_PREPARED_STATEMENT_END
#undef PREPARE };
return GNUNET_PQ_prepare_statements (db,
ps);
} }
@ -76,7 +66,7 @@ postgres_prepare (PGconn *db_conn)
* @return 0 on success * @return 0 on success
*/ */
static int static int
run_queries (PGconn *conn) run_queries (struct GNUNET_PQ_Context *conn)
{ {
struct TALER_Amount hamount; struct TALER_Amount hamount;
struct TALER_Amount hamount2; struct TALER_Amount hamount2;
@ -176,63 +166,51 @@ int
main (int argc, main (int argc,
const char *const argv[]) const char *const argv[])
{ {
PGconn *conn; struct GNUNET_PQ_ExecuteStatement es[] = {
PGresult *result; GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS test_pq ("
int ret;
GNUNET_log_setup ("test-pq",
"WARNING",
NULL);
conn = PQconnectdb ("postgres:///talercheck");
if (CONNECTION_OK != PQstatus (conn))
{
fprintf (stderr,
"Cannot run test, database connection failed: %s\n",
PQerrorMessage (conn));
GNUNET_break (0);
PQfinish (conn);
return 0; /* We ignore this type of error... */
}
result = PQexec (conn,
"CREATE TEMPORARY TABLE IF NOT EXISTS test_pq ("
" hamount_val INT8 NOT NULL" " hamount_val INT8 NOT NULL"
",hamount_frac INT4 NOT NULL" ",hamount_frac INT4 NOT NULL"
",namount_val INT8 NOT NULL" ",namount_val INT8 NOT NULL"
",namount_frac INT4 NOT NULL" ",namount_frac INT4 NOT NULL"
",json VARCHAR NOT NULL" ",json VARCHAR NOT NULL"
")"); ")"),
if (PGRES_COMMAND_OK != PQresultStatus (result)) GNUNET_PQ_EXECUTE_STATEMENT_END
{ };
fprintf (stderr,
"Failed to create table: %s\n", struct GNUNET_PQ_Context *conn;
PQerrorMessage (conn)); int ret;
PQclear (result);
PQfinish (conn); GNUNET_log_setup ("test-pq",
return 1; "WARNING",
} NULL);
PQclear (result); conn = GNUNET_PQ_connect ("postgres:///talercheck",
es,
NULL);
if (GNUNET_OK != if (GNUNET_OK !=
postgres_prepare (conn)) postgres_prepare (conn))
{ {
GNUNET_break (0); GNUNET_break (0);
PQfinish (conn); GNUNET_PQ_disconnect (conn);
return 1; return 1;
} }
ret = run_queries (conn); ret = run_queries (conn);
result = PQexec (conn, {
"DROP TABLE test_pq"); struct GNUNET_PQ_ExecuteStatement ds[] = {
if (PGRES_COMMAND_OK != PQresultStatus (result)) GNUNET_PQ_make_execute ("DROP TABLE test_pq"),
GNUNET_PQ_EXECUTE_STATEMENT_END
};
if (GNUNET_OK !=
GNUNET_PQ_exec_statements (conn,
ds))
{ {
fprintf (stderr, fprintf (stderr,
"Failed to create table: %s\n", "Failed to drop table\n");
PQerrorMessage (conn)); GNUNET_PQ_disconnect (conn);
PQclear (result);
PQfinish (conn);
return 1; return 1;
} }
PQclear (result); }
PQfinish (conn); GNUNET_PQ_disconnect (conn);
return ret; return ret;
} }