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,417 +272,246 @@ TALER_MINT_DB_prepare (PGconn *db_conn)
{ {
PGresult *result; PGresult *result;
result = PQprepare (db_conn, "get_reserve", #define PREPARE(name, sql, ...) \
"SELECT " do { \
"current_balance_value" result = PQprepare (db_conn, name, sql, __VA_ARGS__); \
",current_balance_fraction" if (PGRES_COMMAND_OK != PQresultStatus (result)) \
",balance_currency " { \
",expiration_date " break_db_err (result); \
"FROM reserves " PQclear (result); result = NULL; \
"WHERE reserve_pub=$1 " return GNUNET_SYSERR; \
"LIMIT 1; ", } \
1, NULL); PQclear (result); result = NULL; \
if (PGRES_COMMAND_OK != PQresultStatus(result)) } while (0);
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
result = PQprepare (db_conn, "update_reserve", PREPARE ("get_reserve",
"UPDATE reserves " "SELECT "
"SET" "current_balance_value"
" current_balance_value=$2 " ",current_balance_fraction"
",current_balance_fraction=$3 " ",balance_currency "
",expiration_date=$4 " ",expiration_date "
"WHERE reserve_pub=$1 ", "FROM reserves "
4, NULL); "WHERE reserve_pub=$1 "
if (PGRES_COMMAND_OK != PQresultStatus(result)) "LIMIT 1; ",
{ 1, NULL);
break_db_err (result); PREPARE ("update_reserve",
PQclear (result); "UPDATE reserves "
return GNUNET_SYSERR; "SET"
} " current_balance_value=$2 "
PQclear (result); ",current_balance_fraction=$3 "
result = PQprepare (db_conn, "insert_collectable_blindcoins", ",expiration_date=$4 "
"INSERT INTO collectable_blindcoins ( " "WHERE reserve_pub=$1 ",
" blind_ev, blind_ev_sig " 4, NULL);
",denom_pub, reserve_pub, reserve_sig) " PREPARE ("insert_collectable_blindcoins",
"VALUES ($1, $2, $3, $4, $5)", "INSERT INTO collectable_blindcoins ( "
6, NULL); " blind_ev, blind_ev_sig "
if (PGRES_COMMAND_OK != PQresultStatus(result)) ",denom_pub, reserve_pub, reserve_sig) "
{ "VALUES ($1, $2, $3, $4, $5)",
break_db_err (result); 6, NULL);
PQclear (result); PREPARE ("get_collectable_blindcoins",
return GNUNET_SYSERR; "SELECT "
} "blind_ev_sig, denom_pub, reserve_sig, reserve_pub "
PQclear (result); "FROM collectable_blindcoins "
"WHERE blind_ev = $1",
result = PQprepare (db_conn, "get_collectable_blindcoins", 1, NULL);
"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? */ /* FIXME: does it make sense to store these computed values in the DB? */
#if 0 #if 0
result = PQprepare (db_conn, "get_refresh_session", PREPARE ("get_refresh_session",
"SELECT " "SELECT "
" (SELECT count(*) FROM refresh_melt WHERE session_pub = $1)::INT2 as num_oldcoins " " (SELECT count(*) FROM refresh_melt WHERE session_pub = $1)::INT2 as num_oldcoins "
",(SELECT count(*) FROM refresh_blind_session_keys " ",(SELECT count(*) FROM refresh_blind_session_keys "
" WHERE session_pub = $1 and cnc_index = 0)::INT2 as num_newcoins " " WHERE session_pub = $1 and cnc_index = 0)::INT2 as num_newcoins "
",(SELECT count(*) FROM refresh_blind_session_keys " ",(SELECT count(*) FROM refresh_blind_session_keys "
" WHERE session_pub = $1 and newcoin_index = 0)::INT2 as kappa " " WHERE session_pub = $1 and newcoin_index = 0)::INT2 as kappa "
",noreveal_index" ",noreveal_index"
",session_commit_sig " ",session_commit_sig "
",reveal_ok " ",reveal_ok "
"FROM refresh_sessions " "FROM refresh_sessions "
"WHERE session_pub = $1", "WHERE session_pub = $1",
1, NULL); 1, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result))
{
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
}
PQclear (result);
#endif #endif
result = PQprepare (db_conn, "get_known_coin", PREPARE ("get_known_coin",
"SELECT " "SELECT "
" coin_pub, denom_pub, denom_sig " " coin_pub, denom_pub, denom_sig "
",expended_value, expended_fraction, expended_currency " ",expended_value, expended_fraction, expended_currency "
",refresh_session_pub " ",refresh_session_pub "
"FROM known_coins " "FROM known_coins "
"WHERE coin_pub = $1", "WHERE coin_pub = $1",
1, NULL); 1, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result)) PREPARE ("update_known_coin",
{ "UPDATE known_coins "
break_db_err (result); "SET "
PQclear (result); " denom_pub = $2 "
return GNUNET_SYSERR; ",denom_sig = $3 "
} ",expended_value = $4 "
PQclear (result); ",expended_fraction = $5 "
",expended_currency = $6 "
result = PQprepare (db_conn, "update_known_coin", ",refresh_session_pub = $7 "
"UPDATE known_coins " "WHERE "
"SET " " coin_pub = $1 ",
" denom_pub = $2 " 7, NULL);
",denom_sig = $3 " PREPARE ("insert_known_coin",
",expended_value = $4 " "INSERT INTO known_coins ("
",expended_fraction = $5 " " coin_pub"
",expended_currency = $6 " ",denom_pub"
",refresh_session_pub = $7 " ",denom_sig"
"WHERE " ",expended_value"
" coin_pub = $1 ", ",expended_fraction"
7, NULL); ",expended_currency"
if (PGRES_COMMAND_OK != PQresultStatus(result)) ",refresh_session_pub"
{ ")"
break_db_err (result); "VALUES ($1,$2,$3,$4,$5,$6,$7)",
PQclear (result); 7, NULL);
return GNUNET_SYSERR; PREPARE ("get_refresh_commit_link",
} "SELECT "
PQclear (result); " transfer_pub "
",link_secret_enc "
result = PQprepare (db_conn, "insert_known_coin", "FROM refresh_commit_link "
"INSERT INTO known_coins (" "WHERE session_pub = $1 AND cnc_index = $2 AND oldcoin_index = $3",
" coin_pub" 3, NULL);
",denom_pub" PREPARE ("get_refresh_commit_coin",
",denom_sig" "SELECT "
",expended_value" " link_vector_enc "
",expended_fraction" ",coin_ev "
",expended_currency" "FROM refresh_commit_coin "
",refresh_session_pub" "WHERE session_pub = $1 AND cnc_index = $2 AND newcoin_index = $3",
")" 3, NULL);
"VALUES ($1,$2,$3,$4,$5,$6,$7)", PREPARE ("insert_refresh_order",
7, NULL); "INSERT INTO refresh_order ( "
if (PGRES_COMMAND_OK != PQresultStatus(result)) " newcoin_index "
{ ",session_pub "
break_db_err (result); ",denom_pub "
PQclear (result); ") "
return GNUNET_SYSERR; "VALUES ($1, $2, $3) ",
} 3, NULL);
PQclear (result); PREPARE ("insert_refresh_melt",
"INSERT INTO refresh_melt ( "
result = PQprepare (db_conn, "get_refresh_commit_link", " session_pub "
"SELECT " ",oldcoin_index "
" transfer_pub " ",coin_pub "
",link_secret_enc " ",denom_pub "
"FROM refresh_commit_link " ") "
"WHERE session_pub = $1 AND cnc_index = $2 AND oldcoin_index = $3", "VALUES ($1, $2, $3, $4) ",
3, NULL); 3, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result)) PREPARE ("get_refresh_order",
{ "SELECT denom_pub "
break_db_err (result); "FROM refresh_order "
PQclear (result); "WHERE session_pub = $1 AND newcoin_index = $2",
return GNUNET_SYSERR; 2, NULL);
} PREPARE ("get_refresh_collectable",
PQclear (result); "SELECT ev_sig "
"FROM refresh_collectable "
result = PQprepare (db_conn, "get_refresh_commit_coin", "WHERE session_pub = $1 AND newcoin_index = $2",
"SELECT " 2, NULL);
" link_vector_enc " PREPARE ("get_refresh_melt",
",coin_ev " "SELECT coin_pub "
"FROM refresh_commit_coin " "FROM refresh_melt "
"WHERE session_pub = $1 AND cnc_index = $2 AND newcoin_index = $3", "WHERE session_pub = $1 AND oldcoin_index = $2",
3, NULL); 2, NULL);
if (PGRES_COMMAND_OK != PQresultStatus(result)) PREPARE ("insert_refresh_session",
{ "INSERT INTO refresh_sessions ( "
break_db_err (result); " session_pub "
PQclear (result); ",noreveal_index "
return GNUNET_SYSERR; ") "
} "VALUES ($1, $2) ",
PQclear (result); 2, NULL);
PREPARE ("insert_refresh_commit_link",
result = PQprepare (db_conn, "insert_refresh_order", "INSERT INTO refresh_commit_link ( "
"INSERT INTO refresh_order ( " " session_pub "
" newcoin_index " ",transfer_pub "
",session_pub " ",cnc_index "
",denom_pub " ",oldcoin_index "
") " ",link_secret_enc "
"VALUES ($1, $2, $3) ", ") "
3, NULL); "VALUES ($1, $2, $3, $4, $5) ",
if (PGRES_COMMAND_OK != PQresultStatus(result)) 5, NULL);
{ PREPARE ("insert_refresh_commit_coin",
break_db_err (result); "INSERT INTO refresh_commit_coin ( "
PQclear (result); " session_pub "
return GNUNET_SYSERR; ",coin_ev "
} ",cnc_index "
PQclear (result); ",newcoin_index "
",link_vector_enc "
result = PQprepare (db_conn, "insert_refresh_melt", ") "
"INSERT INTO refresh_melt ( " "VALUES ($1, $2, $3, $4, $5) ",
" session_pub " 5, NULL);
",oldcoin_index " PREPARE ("insert_refresh_collectable",
",coin_pub " "INSERT INTO refresh_collectable ( "
",denom_pub " " session_pub "
") " ",newcoin_index "
"VALUES ($1, $2, $3, $4) ", ",ev_sig "
3, NULL); ") "
if (PGRES_COMMAND_OK != PQresultStatus(result)) "VALUES ($1, $2, $3) ",
{ 3, NULL);
break_db_err (result); PREPARE ("set_reveal_ok",
PQclear (result); "UPDATE refresh_sessions "
return GNUNET_SYSERR; "SET reveal_ok = TRUE "
} "WHERE session_pub = $1 ",
PQclear (result); 1, NULL);
PREPARE ("get_link",
result = PQprepare (db_conn, "get_refresh_order", "SELECT link_vector_enc, ro.denom_pub, ev_sig "
"SELECT denom_pub " "FROM refresh_melt rm "
"FROM refresh_order " " JOIN refresh_order ro USING (session_pub) "
"WHERE session_pub = $1 AND newcoin_index = $2", " JOIN refresh_commit_coin rcc USING (session_pub) "
2, NULL); " JOIN refresh_sessions rs USING (session_pub) "
if (PGRES_COMMAND_OK != PQresultStatus(result)) " JOIN refresh_collectable rc USING (session_pub) "
{ "WHERE rm.coin_pub = $1 "
break_db_err (result); "AND ro.newcoin_index = rcc.newcoin_index "
PQclear (result); "AND ro.newcoin_index = rc.newcoin_index "
return GNUNET_SYSERR; "AND rcc.cnc_index = rs.noreveal_index % ( "
} " SELECT count(*) FROM refresh_commit_coin rcc2 "
PQclear (result); " WHERE rcc2.newcoin_index = 0 AND rcc2.session_pub = rs.session_pub "
" ) ",
result = PQprepare (db_conn, "get_refresh_collectable", 1, NULL);
"SELECT ev_sig " PREPARE ("get_transfer",
"FROM refresh_collectable " "SELECT transfer_pub, link_secret_enc "
"WHERE session_pub = $1 AND newcoin_index = $2", "FROM refresh_melt rm "
2, NULL); " JOIN refresh_commit_link rcl USING (session_pub) "
if (PGRES_COMMAND_OK != PQresultStatus(result)) " JOIN refresh_sessions rs USING (session_pub) "
{ "WHERE rm.coin_pub = $1 "
break_db_err (result); "AND rm.oldcoin_index = rcl.oldcoin_index "
PQclear (result); "AND rcl.cnc_index = rs.noreveal_index % ( "
return GNUNET_SYSERR; " SELECT count(*) FROM refresh_commit_coin rcc2 "
} " WHERE newcoin_index = 0 AND rcc2.session_pub = rm.session_pub "
PQclear (result); " ) ",
1, NULL);
result = PQprepare (db_conn, "get_refresh_melt", PREPARE ("insert_deposit",
"SELECT coin_pub " "INSERT INTO deposits ("
"FROM refresh_melt " "coin_pub,"
"WHERE session_pub = $1 AND oldcoin_index = $2", "denom_pub,"
2, NULL); "transaction_id,"
if (PGRES_COMMAND_OK != PQresultStatus(result)) "amount_value,"
{ "amount_fraction,"
break_db_err (result); "amount_currency,"
PQclear (result); "merchant_pub,"
return GNUNET_SYSERR; "h_contract,"
} "h_wire,"
PQclear (result); "coin_sig,"
"wire"
result = PQprepare (db_conn, "insert_refresh_session", ") VALUES ("
"INSERT INTO refresh_sessions ( " "$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11"
" session_pub " ")",
",noreveal_index " 11, NULL);
") " PREPARE ("get_deposit",
"VALUES ($1, $2) ", "SELECT "
2, NULL); "coin_pub,"
if (PGRES_COMMAND_OK != PQresultStatus(result)) "denom_pub,"
{ "transaction_id,"
break_db_err (result); "amount_value,"
PQclear (result); "amount_fraction,"
return GNUNET_SYSERR; "amount_currency,"
} "merchant_pub,"
PQclear (result); "h_contract,"
"h_wire,"
result = PQprepare (db_conn, "insert_refresh_commit_link", "coin_sig"
"INSERT INTO refresh_commit_link ( " " FROM deposits WHERE ("
" session_pub " "coin_pub = $1"
",transfer_pub " ")",
",cnc_index " 1, NULL);
",oldcoin_index "
",link_secret_enc "
") "
"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",
"INSERT INTO refresh_commit_coin ( "
" session_pub "
",coin_ev "
",cnc_index "
",newcoin_index "
",link_vector_enc "
") "
"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",
"INSERT INTO refresh_collectable ( "
" session_pub "
",newcoin_index "
",ev_sig "
") "
"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",
"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",
"SELECT link_vector_enc, ro.denom_pub, ev_sig "
"FROM refresh_melt rm "
" JOIN refresh_order ro USING (session_pub) "
" JOIN refresh_commit_coin rcc USING (session_pub) "
" JOIN refresh_sessions rs USING (session_pub) "
" JOIN refresh_collectable rc USING (session_pub) "
"WHERE rm.coin_pub = $1 "
"AND ro.newcoin_index = rcc.newcoin_index "
"AND ro.newcoin_index = rc.newcoin_index "
"AND rcc.cnc_index = rs.noreveal_index % ( "
" SELECT count(*) FROM refresh_commit_coin rcc2 "
" 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",
"SELECT transfer_pub, link_secret_enc "
"FROM refresh_melt rm "
" JOIN refresh_commit_link rcl USING (session_pub) "
" JOIN refresh_sessions rs USING (session_pub) "
"WHERE rm.coin_pub = $1 "
"AND rm.oldcoin_index = rcl.oldcoin_index "
"AND rcl.cnc_index = rs.noreveal_index % ( "
" SELECT count(*) FROM refresh_commit_coin rcc2 "
" 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",
"INSERT INTO deposits ("
"coin_pub,"
"denom_pub,"
"transaction_id,"
"amount_value,"
"amount_fraction,"
"amount_currency,"
"merchant_pub,"
"h_contract,"
"h_wire,"
"coin_sig,"
"wire"
") VALUES ("
"$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",
"SELECT "
"coin_pub,"
"denom_pub,"
"transaction_id,"
"amount_value,"
"amount_fraction,"
"amount_currency,"
"merchant_pub,"
"h_contract,"
"h_wire,"
"coin_sig"
" FROM deposits WHERE ("
"coin_pub = $1"
")",
1, NULL);
EXITIF (PGRES_COMMAND_OK != PQresultStatus(result));
PQclear (result);
return GNUNET_OK; return GNUNET_OK;
#undef PREPARE
EXITIF_exit:
break_db_err (result);
PQclear (result);
return GNUNET_SYSERR;
} }