adaptions to libgnunetpq api change (#5733)
This commit is contained in:
parent
9adc4c9ffc
commit
316a77a245
@ -61,7 +61,7 @@ struct TALER_AUDITORDB_Session
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
@ -198,25 +133,23 @@ postgres_drop_tables (void *cls,
|
||||
GNUNET_PQ_make_execute ("DROP TABLE IF EXISTS auditor_exchanges CASCADE;"),
|
||||
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)
|
||||
return GNUNET_SYSERR;
|
||||
LOG (GNUNET_ERROR_TYPE_INFO,
|
||||
"Dropping ALL tables\n");
|
||||
ret = GNUNET_PQ_exec_statements (conn,
|
||||
es);
|
||||
if ( (ret >= 0) &&
|
||||
(drop_exchangelist) )
|
||||
ret = GNUNET_OK;
|
||||
if (drop_exchangelist)
|
||||
ret = GNUNET_PQ_exec_statements (conn,
|
||||
esx);
|
||||
/* TODO: we probably need a bit more fine-grained control
|
||||
over drops for the '-r' option of taler-auditor; also,
|
||||
for the testcase, we currently fail to drop the
|
||||
auditor_denominations table... */
|
||||
PQfinish (conn);
|
||||
GNUNET_PQ_disconnect (conn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -479,28 +412,52 @@ postgres_create_tables (void *cls)
|
||||
")"),
|
||||
GNUNET_PQ_EXECUTE_STATEMENT_END
|
||||
};
|
||||
PGconn *conn;
|
||||
int ret;
|
||||
struct GNUNET_PQ_Context *conn;
|
||||
|
||||
conn = connect_to_postgres (pc);
|
||||
conn = GNUNET_PQ_connect (pc->connection_cfg_str,
|
||||
es,
|
||||
NULL);
|
||||
if (NULL == conn)
|
||||
return GNUNET_SYSERR;
|
||||
ret = GNUNET_PQ_exec_statements (conn,
|
||||
es);
|
||||
PQfinish (conn);
|
||||
return ret;
|
||||
GNUNET_PQ_disconnect (conn);
|
||||
return GNUNET_OK;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Setup prepared statements.
|
||||
* Close thread-local database connection when a thread is destroyed.
|
||||
*
|
||||
* @param db_conn connection handle to initialize
|
||||
* @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
|
||||
* @param cls closure we get from pthreads (the db handle)
|
||||
*/
|
||||
static int
|
||||
postgres_prepare (PGconn *db_conn)
|
||||
static void
|
||||
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[] = {
|
||||
/* used in #postgres_commit */
|
||||
GNUNET_PQ_make_prepare ("do_commit",
|
||||
@ -1036,80 +993,23 @@ postgres_prepare (PGconn *db_conn)
|
||||
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 (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
|
||||
{
|
||||
GNUNET_PQ_reconnect_if_down (session->conn);
|
||||
return session;
|
||||
}
|
||||
}
|
||||
db_conn = connect_to_postgres (pc);
|
||||
db_conn = GNUNET_PQ_connect (pc->connection_cfg_str,
|
||||
NULL,
|
||||
ps);
|
||||
if (NULL == db_conn)
|
||||
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->conn = db_conn;
|
||||
if (0 != pthread_setspecific (pc->db_conn_threadlocal,
|
||||
session))
|
||||
{
|
||||
GNUNET_break (0);
|
||||
PQfinish (db_conn);
|
||||
GNUNET_PQ_disconnect (db_conn);
|
||||
GNUNET_free (session);
|
||||
return NULL;
|
||||
}
|
||||
@ -1128,20 +1028,19 @@ static int
|
||||
postgres_start (void *cls,
|
||||
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,
|
||||
"START TRANSACTION ISOLATION LEVEL SERIALIZABLE");
|
||||
if (PGRES_COMMAND_OK !=
|
||||
PQresultStatus (result))
|
||||
if (GNUNET_OK !=
|
||||
GNUNET_PQ_exec_statements (session->conn,
|
||||
es))
|
||||
{
|
||||
TALER_LOG_ERROR ("Failed to start transaction: %s\n",
|
||||
PQresultErrorMessage (result));
|
||||
TALER_LOG_ERROR ("Failed to start transaction\n");
|
||||
GNUNET_break (0);
|
||||
PQclear (result);
|
||||
return GNUNET_SYSERR;
|
||||
}
|
||||
PQclear (result);
|
||||
return GNUNET_OK;
|
||||
}
|
||||
|
||||
@ -1157,13 +1056,14 @@ static void
|
||||
postgres_rollback (void *cls,
|
||||
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,
|
||||
"ROLLBACK");
|
||||
GNUNET_break (PGRES_COMMAND_OK ==
|
||||
PQresultStatus (result));
|
||||
PQclear (result);
|
||||
GNUNET_break (GNUNET_OK ==
|
||||
GNUNET_PQ_exec_statements (session->conn,
|
||||
es));
|
||||
}
|
||||
|
||||
|
||||
@ -1205,30 +1105,31 @@ postgres_gc (void *cls)
|
||||
TALER_PQ_query_param_absolute_time (&now),
|
||||
GNUNET_PQ_query_param_end
|
||||
};
|
||||
PGconn *conn;
|
||||
struct GNUNET_PQ_Context *conn;
|
||||
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 ();
|
||||
conn = connect_to_postgres (pc);
|
||||
conn = GNUNET_PQ_connect (pc->connection_cfg_str,
|
||||
NULL,
|
||||
ps);
|
||||
if (NULL == conn)
|
||||
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,
|
||||
"gc_auditor",
|
||||
params_time);
|
||||
GNUNET_PQ_disconnect (conn);
|
||||
if (0 > qs)
|
||||
{
|
||||
GNUNET_break (0);
|
||||
PQfinish (conn);
|
||||
return GNUNET_SYSERR;
|
||||
}
|
||||
PQfinish (conn);
|
||||
return GNUNET_OK;
|
||||
}
|
||||
|
||||
|
@ -85,7 +85,7 @@ struct TALER_EXCHANGEDB_Session
|
||||
/**
|
||||
* Postgres connection handle.
|
||||
*/
|
||||
PGconn *conn;
|
||||
struct GNUNET_PQ_Context *conn;
|
||||
|
||||
/**
|
||||
* Name of the current transaction, for debugging.
|
||||
@ -104,7 +104,7 @@ struct PostgresClosure
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
@ -168,19 +168,15 @@ postgres_drop_tables (void *cls)
|
||||
GNUNET_PQ_make_execute ("DROP TABLE IF EXISTS denominations CASCADE;"),
|
||||
GNUNET_PQ_EXECUTE_STATEMENT_END
|
||||
};
|
||||
PGconn *conn;
|
||||
int ret;
|
||||
struct GNUNET_PQ_Context *conn;
|
||||
|
||||
/* 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)
|
||||
return GNUNET_SYSERR;
|
||||
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
|
||||
"Dropping ALL tables\n");
|
||||
ret = GNUNET_PQ_exec_statements (conn,
|
||||
es);
|
||||
PQfinish (conn);
|
||||
return ret;
|
||||
GNUNET_PQ_disconnect (conn);
|
||||
return GNUNET_OK;
|
||||
}
|
||||
|
||||
|
||||
@ -518,29 +514,74 @@ postgres_create_tables (void *cls)
|
||||
"ON prewire(finished);"),
|
||||
GNUNET_PQ_EXECUTE_STATEMENT_END
|
||||
};
|
||||
PGconn *conn;
|
||||
int ret;
|
||||
struct GNUNET_PQ_Context *conn;
|
||||
|
||||
/* 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)
|
||||
return GNUNET_SYSERR;
|
||||
ret = GNUNET_PQ_exec_statements (conn,
|
||||
es);
|
||||
PQfinish (conn);
|
||||
return ret;
|
||||
GNUNET_PQ_disconnect (conn);
|
||||
return GNUNET_OK;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Setup prepared statements.
|
||||
* Close thread-local database connection when a thread is destroyed.
|
||||
*
|
||||
* @param db_conn connection handle to initialize
|
||||
* @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
|
||||
* @param cls closure we get from pthreads (the db handle)
|
||||
*/
|
||||
static int
|
||||
postgres_prepare (PGconn *db_conn)
|
||||
static void
|
||||
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[] = {
|
||||
/* Used in #postgres_insert_denomination_info() */
|
||||
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)))"
|
||||
" ORDER BY wire_deadline ASC",
|
||||
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() */
|
||||
GNUNET_PQ_make_prepare ("audit_get_wire_incr",
|
||||
"SELECT"
|
||||
@ -1658,122 +1693,22 @@ postgres_prepare (PGconn *db_conn)
|
||||
GNUNET_PQ_make_prepare ("do_commit",
|
||||
"COMMIT",
|
||||
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
|
||||
};
|
||||
|
||||
return GNUNET_PQ_prepare_statements (db_conn,
|
||||
db_conn = GNUNET_PQ_connect (pc->connection_cfg_str,
|
||||
es,
|
||||
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)
|
||||
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->conn = db_conn;
|
||||
if (0 != pthread_setspecific (pc->db_conn_threadlocal,
|
||||
session))
|
||||
{
|
||||
GNUNET_break (0);
|
||||
PQfinish (db_conn);
|
||||
GNUNET_PQ_disconnect (db_conn);
|
||||
GNUNET_free (session);
|
||||
return NULL;
|
||||
}
|
||||
@ -1795,25 +1730,22 @@ postgres_start (void *cls,
|
||||
struct TALER_EXCHANGEDB_Session *session,
|
||||
const char *name)
|
||||
{
|
||||
PGresult *result;
|
||||
ExecStatusType ex;
|
||||
struct GNUNET_PQ_ExecuteStatement es[] = {
|
||||
GNUNET_PQ_make_execute ("START TRANSACTION ISOLATION LEVEL SERIALIZABLE"),
|
||||
GNUNET_PQ_EXECUTE_STATEMENT_END
|
||||
};
|
||||
|
||||
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
|
||||
"Starting transaction on %p\n",
|
||||
session->conn);
|
||||
result = PQexec (session->conn,
|
||||
"START TRANSACTION ISOLATION LEVEL SERIALIZABLE");
|
||||
if (PGRES_COMMAND_OK !=
|
||||
(ex = PQresultStatus (result)))
|
||||
if (GNUNET_OK !=
|
||||
GNUNET_PQ_exec_statements (session->conn,
|
||||
es))
|
||||
{
|
||||
TALER_LOG_ERROR ("Failed to start transaction (%s): %s\n",
|
||||
PQresStatus (ex),
|
||||
PQerrorMessage (session->conn));
|
||||
TALER_LOG_ERROR ("Failed to start transaction\n");
|
||||
GNUNET_break (0);
|
||||
PQclear (result);
|
||||
return GNUNET_SYSERR;
|
||||
}
|
||||
PQclear (result);
|
||||
session->transaction_name = name;
|
||||
return GNUNET_OK;
|
||||
}
|
||||
@ -1830,16 +1762,17 @@ static void
|
||||
postgres_rollback (void *cls,
|
||||
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,
|
||||
"Rolling back transaction on %p\n",
|
||||
session->conn);
|
||||
result = PQexec (session->conn,
|
||||
"ROLLBACK");
|
||||
GNUNET_break (PGRES_COMMAND_OK ==
|
||||
PQresultStatus (result));
|
||||
PQclear (result);
|
||||
GNUNET_break (GNUNET_OK ==
|
||||
GNUNET_PQ_exec_statements (session->conn,
|
||||
es));
|
||||
session->transaction_name = NULL;
|
||||
}
|
||||
|
||||
@ -1880,15 +1813,16 @@ static void
|
||||
postgres_preflight (void *cls,
|
||||
struct TALER_EXCHANGEDB_Session *session)
|
||||
{
|
||||
PGresult *result;
|
||||
ExecStatusType status;
|
||||
struct GNUNET_PQ_ExecuteStatement es[] = {
|
||||
GNUNET_PQ_make_execute ("COMMIT"),
|
||||
GNUNET_PQ_EXECUTE_STATEMENT_END
|
||||
};
|
||||
|
||||
if (NULL == session->transaction_name)
|
||||
return; /* all good */
|
||||
result = PQexec (session->conn,
|
||||
"COMMIT");
|
||||
status = PQresultStatus (result);
|
||||
if (PGRES_COMMAND_OK == status)
|
||||
if (GNUNET_OK ==
|
||||
GNUNET_PQ_exec_statements (session->conn,
|
||||
es))
|
||||
{
|
||||
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
||||
"BUG: Preflight check committed transaction `%s'!\n",
|
||||
@ -1901,7 +1835,6 @@ postgres_preflight (void *cls,
|
||||
session->transaction_name);
|
||||
}
|
||||
session->transaction_name = NULL;
|
||||
PQclear (result);
|
||||
}
|
||||
|
||||
|
||||
@ -5618,8 +5551,10 @@ static int
|
||||
postgres_start_deferred_wire_out (void *cls,
|
||||
struct TALER_EXCHANGEDB_Session *session)
|
||||
{
|
||||
PGresult *result;
|
||||
ExecStatusType ex;
|
||||
struct GNUNET_PQ_ExecuteStatement es[] = {
|
||||
GNUNET_PQ_make_execute ("SET CONSTRAINTS wire_out_ref DEFERRED"),
|
||||
GNUNET_PQ_EXECUTE_STATEMENT_END
|
||||
};
|
||||
|
||||
postgres_preflight (cls,
|
||||
session);
|
||||
@ -5628,22 +5563,17 @@ postgres_start_deferred_wire_out (void *cls,
|
||||
session,
|
||||
"deferred wire out"))
|
||||
return GNUNET_SYSERR;
|
||||
result = PQexec (session->conn,
|
||||
"SET CONSTRAINTS wire_out_ref DEFERRED");
|
||||
if (PGRES_COMMAND_OK !=
|
||||
(ex = PQresultStatus (result)))
|
||||
if (GNUNET_OK !=
|
||||
GNUNET_PQ_exec_statements (session->conn,
|
||||
es))
|
||||
{
|
||||
TALER_LOG_ERROR (
|
||||
"Failed to defer wire_out_ref constraint on transaction (%s): %s\n",
|
||||
PQresStatus (ex),
|
||||
PQerrorMessage (session->conn));
|
||||
"Failed to defer wire_out_ref constraint on transaction\n");
|
||||
GNUNET_break (0);
|
||||
PQclear (result);
|
||||
postgres_rollback (cls,
|
||||
session);
|
||||
return GNUNET_SYSERR;
|
||||
}
|
||||
PQclear (result);
|
||||
return GNUNET_OK;
|
||||
}
|
||||
|
||||
@ -5711,7 +5641,7 @@ postgres_gc (void *cls)
|
||||
TALER_PQ_query_param_absolute_time (&long_ago),
|
||||
GNUNET_PQ_query_param_end
|
||||
};
|
||||
PGconn *conn;
|
||||
struct GNUNET_PQ_Context *conn;
|
||||
int ret;
|
||||
|
||||
now = GNUNET_TIME_absolute_get ();
|
||||
@ -5723,15 +5653,42 @@ postgres_gc (void *cls)
|
||||
GNUNET_TIME_relative_multiply (
|
||||
GNUNET_TIME_UNIT_YEARS,
|
||||
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)
|
||||
return GNUNET_SYSERR;
|
||||
ret = postgres_prepare (conn);
|
||||
if (GNUNET_OK == ret)
|
||||
{
|
||||
if (
|
||||
(0 > GNUNET_PQ_eval_prepared_non_select (conn,
|
||||
ret = GNUNET_OK;
|
||||
if ( (0 > GNUNET_PQ_eval_prepared_non_select (conn,
|
||||
"gc_reserves",
|
||||
params_time)) ||
|
||||
(0 > GNUNET_PQ_eval_prepared_non_select (conn,
|
||||
@ -5739,8 +5696,7 @@ postgres_gc (void *cls)
|
||||
params_none)) ||
|
||||
(0 > GNUNET_PQ_eval_prepared_non_select (conn,
|
||||
"gc_wire_fee",
|
||||
params_ancient_time))
|
||||
)
|
||||
params_ancient_time)) )
|
||||
ret = GNUNET_SYSERR;
|
||||
/* This one may fail due to foreign key constraints from
|
||||
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,
|
||||
"gc_denominations",
|
||||
params_time);
|
||||
}
|
||||
PQfinish (conn);
|
||||
GNUNET_PQ_disconnect (conn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
106
src/pq/test_pq.c
106
src/pq/test_pq.c
@ -26,27 +26,14 @@
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
static int
|
||||
postgres_prepare (PGconn *db_conn)
|
||||
postgres_prepare (struct GNUNET_PQ_Context *db)
|
||||
{
|
||||
PGresult *result;
|
||||
|
||||
#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",
|
||||
struct GNUNET_PQ_PreparedStatement ps[] = {
|
||||
GNUNET_PQ_make_prepare ("test_insert",
|
||||
"INSERT INTO test_pq ("
|
||||
" hamount_val"
|
||||
",hamount_frac"
|
||||
@ -55,8 +42,8 @@ postgres_prepare (PGconn *db_conn)
|
||||
",json"
|
||||
") VALUES "
|
||||
"($1, $2, $3, $4, $5);",
|
||||
5, NULL);
|
||||
PREPARE ("test_select",
|
||||
5),
|
||||
GNUNET_PQ_make_prepare ("test_select",
|
||||
"SELECT"
|
||||
" hamount_val"
|
||||
",hamount_frac"
|
||||
@ -64,9 +51,12 @@ postgres_prepare (PGconn *db_conn)
|
||||
",namount_frac"
|
||||
",json"
|
||||
" FROM test_pq;",
|
||||
0, NULL);
|
||||
return GNUNET_OK;
|
||||
#undef PREPARE
|
||||
0),
|
||||
GNUNET_PQ_PREPARED_STATEMENT_END
|
||||
};
|
||||
|
||||
return GNUNET_PQ_prepare_statements (db,
|
||||
ps);
|
||||
}
|
||||
|
||||
|
||||
@ -76,7 +66,7 @@ postgres_prepare (PGconn *db_conn)
|
||||
* @return 0 on success
|
||||
*/
|
||||
static int
|
||||
run_queries (PGconn *conn)
|
||||
run_queries (struct GNUNET_PQ_Context *conn)
|
||||
{
|
||||
struct TALER_Amount hamount;
|
||||
struct TALER_Amount hamount2;
|
||||
@ -176,63 +166,51 @@ int
|
||||
main (int argc,
|
||||
const char *const argv[])
|
||||
{
|
||||
PGconn *conn;
|
||||
PGresult *result;
|
||||
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 ("
|
||||
struct GNUNET_PQ_ExecuteStatement es[] = {
|
||||
GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS test_pq ("
|
||||
" hamount_val INT8 NOT NULL"
|
||||
",hamount_frac INT4 NOT NULL"
|
||||
",namount_val INT8 NOT NULL"
|
||||
",namount_frac INT4 NOT NULL"
|
||||
",json VARCHAR NOT NULL"
|
||||
")");
|
||||
if (PGRES_COMMAND_OK != PQresultStatus (result))
|
||||
{
|
||||
fprintf (stderr,
|
||||
"Failed to create table: %s\n",
|
||||
PQerrorMessage (conn));
|
||||
PQclear (result);
|
||||
PQfinish (conn);
|
||||
return 1;
|
||||
}
|
||||
PQclear (result);
|
||||
")"),
|
||||
GNUNET_PQ_EXECUTE_STATEMENT_END
|
||||
};
|
||||
|
||||
struct GNUNET_PQ_Context *conn;
|
||||
int ret;
|
||||
|
||||
GNUNET_log_setup ("test-pq",
|
||||
"WARNING",
|
||||
NULL);
|
||||
conn = GNUNET_PQ_connect ("postgres:///talercheck",
|
||||
es,
|
||||
NULL);
|
||||
if (GNUNET_OK !=
|
||||
postgres_prepare (conn))
|
||||
{
|
||||
GNUNET_break (0);
|
||||
PQfinish (conn);
|
||||
GNUNET_PQ_disconnect (conn);
|
||||
return 1;
|
||||
}
|
||||
ret = run_queries (conn);
|
||||
result = PQexec (conn,
|
||||
"DROP TABLE test_pq");
|
||||
if (PGRES_COMMAND_OK != PQresultStatus (result))
|
||||
{
|
||||
struct GNUNET_PQ_ExecuteStatement ds[] = {
|
||||
GNUNET_PQ_make_execute ("DROP TABLE test_pq"),
|
||||
GNUNET_PQ_EXECUTE_STATEMENT_END
|
||||
};
|
||||
|
||||
if (GNUNET_OK !=
|
||||
GNUNET_PQ_exec_statements (conn,
|
||||
ds))
|
||||
{
|
||||
fprintf (stderr,
|
||||
"Failed to create table: %s\n",
|
||||
PQerrorMessage (conn));
|
||||
PQclear (result);
|
||||
PQfinish (conn);
|
||||
"Failed to drop table\n");
|
||||
GNUNET_PQ_disconnect (conn);
|
||||
return 1;
|
||||
}
|
||||
PQclear (result);
|
||||
PQfinish (conn);
|
||||
}
|
||||
GNUNET_PQ_disconnect (conn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user