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);
{ return session;
/**
* 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;
}
} }
db_conn = connect_to_postgres (pc); db_conn = GNUNET_PQ_connect (pc->connection_cfg_str,
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;
} }

File diff suppressed because it is too large Load Diff

View File

@ -26,47 +26,37 @@
/** /**
* 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",
"INSERT INTO test_pq ("
" hamount_val"
",hamount_frac"
",namount_val"
",namount_frac"
",json"
") VALUES "
"($1, $2, $3, $4, $5);",
5),
GNUNET_PQ_make_prepare ("test_select",
"SELECT"
" hamount_val"
",hamount_frac"
",namount_val"
",namount_frac"
",json"
" FROM test_pq;",
0),
GNUNET_PQ_PREPARED_STATEMENT_END
};
#define PREPARE(name, sql, ...) \ return GNUNET_PQ_prepare_statements (db,
do { \ ps);
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 ("
" hamount_val"
",hamount_frac"
",namount_val"
",namount_frac"
",json"
") VALUES "
"($1, $2, $3, $4, $5);",
5, NULL);
PREPARE ("test_select",
"SELECT"
" hamount_val"
",hamount_frac"
",namount_val"
",namount_frac"
",json"
" FROM test_pq;",
0, NULL);
return GNUNET_OK;
#undef PREPARE
} }
@ -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 ("
" hamount_val INT8 NOT NULL"
",hamount_frac INT4 NOT NULL"
",namount_val INT8 NOT NULL"
",namount_frac INT4 NOT NULL"
",json VARCHAR NOT NULL"
")"),
GNUNET_PQ_EXECUTE_STATEMENT_END
};
struct GNUNET_PQ_Context *conn;
int ret; int ret;
GNUNET_log_setup ("test-pq", GNUNET_log_setup ("test-pq",
"WARNING", "WARNING",
NULL); NULL);
conn = PQconnectdb ("postgres:///talercheck"); conn = GNUNET_PQ_connect ("postgres:///talercheck",
if (CONNECTION_OK != PQstatus (conn)) es,
{ NULL);
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_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);
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");
if (PGRES_COMMAND_OK != PQresultStatus (result))
{ {
fprintf (stderr, struct GNUNET_PQ_ExecuteStatement ds[] = {
"Failed to create table: %s\n", GNUNET_PQ_make_execute ("DROP TABLE test_pq"),
PQerrorMessage (conn)); GNUNET_PQ_EXECUTE_STATEMENT_END
PQclear (result); };
PQfinish (conn);
return 1; if (GNUNET_OK !=
GNUNET_PQ_exec_statements (conn,
ds))
{
fprintf (stderr,
"Failed to drop table\n");
GNUNET_PQ_disconnect (conn);
return 1;
}
} }
PQclear (result); GNUNET_PQ_disconnect (conn);
PQfinish (conn);
return ret; return ret;
} }