summaryrefslogtreecommitdiff
path: root/src/backend-lib/merchant_db.c
diff options
context:
space:
mode:
authorMarcello Stanisci <marcello.stanisci@inria.fr>2015-09-30 09:00:21 +0200
committerMarcello Stanisci <marcello.stanisci@inria.fr>2015-09-30 09:00:21 +0200
commitae89e08fd7d59cd81c69221c723c64c05dfacf3f (patch)
tree8fbe1d2dbf94a9e5c0f19a62be1d57418e4ac902 /src/backend-lib/merchant_db.c
parentd28fc4e6fc458573e3dc58f3f68b694337bc2384 (diff)
downloadmerchant-ae89e08fd7d59cd81c69221c723c64c05dfacf3f.tar.gz
merchant-ae89e08fd7d59cd81c69221c723c64c05dfacf3f.tar.bz2
merchant-ae89e08fd7d59cd81c69221c723c64c05dfacf3f.zip
adding error code for primary key violation
Diffstat (limited to 'src/backend-lib/merchant_db.c')
-rw-r--r--src/backend-lib/merchant_db.c44
1 files changed, 37 insertions, 7 deletions
diff --git a/src/backend-lib/merchant_db.c b/src/backend-lib/merchant_db.c
index 6c67eb9d..befb2c48 100644
--- a/src/backend-lib/merchant_db.c
+++ b/src/backend-lib/merchant_db.c
@@ -208,22 +208,25 @@ MERCHANT_DB_initialize (PGconn *conn, int tmp)
/**
-* Inserts a contract record into the database and if successfull returns the
-* serial number of the inserted row.
+* Insert a contract record into the database and if successfull
+* return the serial number of the inserted row.
*
* @param conn the database connection
* @param timestamp the timestamp of this contract
* @param expiry the time when the contract will expire
-* @param edate when the merchant wants to receive the wire transfer corresponding
-* to this deal (this value is also a field inside the 'wire' JSON format)
-* @param refund deadline until which the merchant can return the paid amount
+* @param edate when the merchant wants to receive the wire transfer
+* corresponding to this deal (this value is also a field inside the
+* 'wire' JSON format)
+* @param refund deadline until which the merchant can return the paid
+* amount
* @param amount the taler amount corresponding to the contract
* @param hash of the stringified JSON corresponding to this contract
* @param c_id contract's id
* @param desc descripition of the contract
* @param nounce a random 64-bit nounce
* @param product description to identify a product
-* @return GNUNET_OK on success, GNUNET_SYSERR upon error
+* @return GNUNET_OK on success, GNUNET_NO if attempting to insert an
+* already inserted @a c_id, GNUNET_SYSERR for other errors.
*/
uint32_t
@@ -268,7 +271,34 @@ MERCHANT_DB_contract_create (PGconn *conn,
/* NOTE: the statement is prepared by MERCHANT_DB_initialize function */
res = TALER_PQ_exec_prepared (conn, "contract_create", params);
status = PQresultStatus (res);
- EXITIF (PGRES_COMMAND_OK != status);
+
+ if (PGRES_COMMAND_OK != status)
+ {
+ const char *sqlstate;
+
+ sqlstate = PQresultErrorField (res, PG_DIAG_SQLSTATE);
+ if (NULL == sqlstate)
+ {
+ /* very unexpected... */
+ GNUNET_break (0);
+ PQclear (res);
+ return GNUNET_SYSERR;
+ }
+ /* 40P01: deadlock, 40001: serialization failure */
+ if ( (0 == strcmp (sqlstate,
+ "23505")))
+ {
+ /* Primary key violation */
+ PQclear (res);
+ return GNUNET_NO;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Database commit failure: %s\n",
+ sqlstate);
+ PQclear (res);
+ return GNUNET_SYSERR;
+ }
+
PQclear (res);
return GNUNET_OK;