db: Use macro while creating prepared statements

This commit is contained in:
Sree Harsha Totakura 2015-03-05 17:05:51 +01:00
parent 3f7a1a5c8a
commit 51929fe759

View File

@ -272,7 +272,19 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
{
PGresult *result;
result = PQprepare (db_conn, "get_reserve",
#define PREPARE(name, sql, ...) \
do { \
result = PQprepare (db_conn, name, sql, __VA_ARGS__); \
if (PGRES_COMMAND_OK != PQresultStatus (result)) \
{ \
break_db_err (result); \
PQclear (result); result = NULL; \
return GNUNET_SYSERR; \
} \
PQclear (result); result = NULL; \
} while (0);
PREPARE ("get_reserve",
"SELECT "
"current_balance_value"
",current_balance_fraction"
@ -282,15 +294,7 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
"WHERE reserve_pub=$1 "
"LIMIT 1; ",
1, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "update_reserve",
PREPARE ("update_reserve",
"UPDATE reserves "
"SET"
" current_balance_value=$2 "
@ -298,44 +302,22 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
",expiration_date=$4 "
"WHERE reserve_pub=$1 ",
4, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "insert_collectable_blindcoins",
PREPARE ("insert_collectable_blindcoins",
"INSERT INTO collectable_blindcoins ( "
" blind_ev, blind_ev_sig "
",denom_pub, reserve_pub, reserve_sig) "
"VALUES ($1, $2, $3, $4, $5)",
6, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "get_collectable_blindcoins",
PREPARE ("get_collectable_blindcoins",
"SELECT "
"blind_ev_sig, denom_pub, reserve_sig, reserve_pub "
"FROM collectable_blindcoins "
"WHERE blind_ev = $1",
1, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
/* FIXME: does it make sense to store these computed values in the DB? */
#if 0
result = PQprepare (db_conn, "get_refresh_session",
PREPARE ("get_refresh_session",
"SELECT "
" (SELECT count(*) FROM refresh_melt WHERE session_pub = $1)::INT2 as num_oldcoins "
",(SELECT count(*) FROM refresh_blind_session_keys "
@ -348,16 +330,9 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
"FROM refresh_sessions "
"WHERE session_pub = $1",
1, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
#endif
result = PQprepare (db_conn, "get_known_coin",
PREPARE ("get_known_coin",
"SELECT "
" coin_pub, denom_pub, denom_sig "
",expended_value, expended_fraction, expended_currency "
@ -365,15 +340,7 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
"FROM known_coins "
"WHERE coin_pub = $1",
1, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "update_known_coin",
PREPARE ("update_known_coin",
"UPDATE known_coins "
"SET "
" denom_pub = $2 "
@ -385,15 +352,7 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
"WHERE "
" coin_pub = $1 ",
7, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "insert_known_coin",
PREPARE ("insert_known_coin",
"INSERT INTO known_coins ("
" coin_pub"
",denom_pub"
@ -405,45 +364,21 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
")"
"VALUES ($1,$2,$3,$4,$5,$6,$7)",
7, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "get_refresh_commit_link",
PREPARE ("get_refresh_commit_link",
"SELECT "
" transfer_pub "
",link_secret_enc "
"FROM refresh_commit_link "
"WHERE session_pub = $1 AND cnc_index = $2 AND oldcoin_index = $3",
3, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "get_refresh_commit_coin",
PREPARE ("get_refresh_commit_coin",
"SELECT "
" link_vector_enc "
",coin_ev "
"FROM refresh_commit_coin "
"WHERE session_pub = $1 AND cnc_index = $2 AND newcoin_index = $3",
3, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "insert_refresh_order",
PREPARE ("insert_refresh_order",
"INSERT INTO refresh_order ( "
" newcoin_index "
",session_pub "
@ -451,15 +386,7 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
") "
"VALUES ($1, $2, $3) ",
3, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "insert_refresh_melt",
PREPARE ("insert_refresh_melt",
"INSERT INTO refresh_melt ( "
" session_pub "
",oldcoin_index "
@ -468,69 +395,29 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
") "
"VALUES ($1, $2, $3, $4) ",
3, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "get_refresh_order",
PREPARE ("get_refresh_order",
"SELECT denom_pub "
"FROM refresh_order "
"WHERE session_pub = $1 AND newcoin_index = $2",
2, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "get_refresh_collectable",
PREPARE ("get_refresh_collectable",
"SELECT ev_sig "
"FROM refresh_collectable "
"WHERE session_pub = $1 AND newcoin_index = $2",
2, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "get_refresh_melt",
PREPARE ("get_refresh_melt",
"SELECT coin_pub "
"FROM refresh_melt "
"WHERE session_pub = $1 AND oldcoin_index = $2",
2, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "insert_refresh_session",
PREPARE ("insert_refresh_session",
"INSERT INTO refresh_sessions ( "
" session_pub "
",noreveal_index "
") "
"VALUES ($1, $2) ",
2, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "insert_refresh_commit_link",
PREPARE ("insert_refresh_commit_link",
"INSERT INTO refresh_commit_link ( "
" session_pub "
",transfer_pub "
@ -540,15 +427,7 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
") "
"VALUES ($1, $2, $3, $4, $5) ",
5, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "insert_refresh_commit_coin",
PREPARE ("insert_refresh_commit_coin",
"INSERT INTO refresh_commit_coin ( "
" session_pub "
",coin_ev "
@ -558,15 +437,7 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
") "
"VALUES ($1, $2, $3, $4, $5) ",
5, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "insert_refresh_collectable",
PREPARE ("insert_refresh_collectable",
"INSERT INTO refresh_collectable ( "
" session_pub "
",newcoin_index "
@ -574,28 +445,12 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
") "
"VALUES ($1, $2, $3) ",
3, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "set_reveal_ok",
PREPARE ("set_reveal_ok",
"UPDATE refresh_sessions "
"SET reveal_ok = TRUE "
"WHERE session_pub = $1 ",
1, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "get_link",
PREPARE ("get_link",
"SELECT link_vector_enc, ro.denom_pub, ev_sig "
"FROM refresh_melt rm "
" JOIN refresh_order ro USING (session_pub) "
@ -610,15 +465,7 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
" WHERE rcc2.newcoin_index = 0 AND rcc2.session_pub = rs.session_pub "
" ) ",
1, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "get_transfer",
PREPARE ("get_transfer",
"SELECT transfer_pub, link_secret_enc "
"FROM refresh_melt rm "
" JOIN refresh_commit_link rcl USING (session_pub) "
@ -630,15 +477,7 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
" WHERE newcoin_index = 0 AND rcc2.session_pub = rm.session_pub "
" ) ",
1, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "insert_deposit",
PREPARE ("insert_deposit",
"INSERT INTO deposits ("
"coin_pub,"
"denom_pub,"
@ -655,10 +494,7 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
"$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11"
")",
11, NULL);
EXITIF (PGRES_COMMAND_OK != PQresultStatus(result));
PQclear (result);
result = PQprepare (db_conn, "get_deposit",
PREPARE ("get_deposit",
"SELECT "
"coin_pub,"
"denom_pub,"
@ -674,15 +510,8 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
"coin_pub = $1"
")",
1, NULL);
EXITIF (PGRES_COMMAND_OK != PQresultStatus(result));
PQclear (result);
return GNUNET_OK;
EXITIF_exit:
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
#undef PREPARE
}