aboutsummaryrefslogtreecommitdiff
path: root/src/exchangedb/exchange_do_batch_reserves_in_insert.sql
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2023-01-10 13:36:02 +0100
committerChristian Grothoff <christian@grothoff.org>2023-01-10 13:36:02 +0100
commit1df72de087db8e2a0696fc5d8b8c3b1f8f3c8e80 (patch)
treea923182e74f864f2400d8ea64198cfba3e4883ca /src/exchangedb/exchange_do_batch_reserves_in_insert.sql
parentb41ffd1a1af24efc244f7ed95dcc13c4b77bc56e (diff)
parent0cf46d8e5995f84a642795ddf214776ed8d4077f (diff)
downloadexchange-1df72de087db8e2a0696fc5d8b8c3b1f8f3c8e80.tar.xz
Merge branch 'master' of git+ssh://git.taler.net/exchange
Diffstat (limited to 'src/exchangedb/exchange_do_batch_reserves_in_insert.sql')
-rw-r--r--src/exchangedb/exchange_do_batch_reserves_in_insert.sql68
1 files changed, 42 insertions, 26 deletions
diff --git a/src/exchangedb/exchange_do_batch_reserves_in_insert.sql b/src/exchangedb/exchange_do_batch_reserves_in_insert.sql
index 4e0383f6c..c95f75c3c 100644
--- a/src/exchangedb/exchange_do_batch_reserves_in_insert.sql
+++ b/src/exchangedb/exchange_do_batch_reserves_in_insert.sql
@@ -32,7 +32,12 @@ CREATE OR REPLACE FUNCTION exchange_do_batch_reserves_in_insert(
OUT ruuid INT8)
LANGUAGE plpgsql
AS $$
-
+DECLARE
+ curs refcursor;
+DECLARE
+ i RECORD;
+DECLARE
+ curs_trans refcursor;
BEGIN
ruuid= 0;
out_reserve_found = TRUE;
@@ -46,29 +51,38 @@ transaction_duplicate= TRUE;
,in_payto_uri)
ON CONFLICT DO NOTHING;
- INSERT INTO reserves
- (reserve_pub
- ,current_balance_val
- ,current_balance_frac
- ,expiration_date
- ,gc_date)
- VALUES
- (in_reserve_pub
- ,in_credit_val
- ,in_credit_frac
- ,in_expiration_date
- ,in_gc_date)
- ON CONFLICT DO NOTHING
- RETURNING reserves.reserve_uuid INTO ruuid;
+ OPEN curs FOR
+ WITH reserve_changes AS (
+ INSERT INTO reserves
+ (reserve_pub
+ ,current_balance_val
+ ,current_balance_frac
+ ,expiration_date
+ ,gc_date)
+ VALUES
+ (in_reserve_pub
+ ,in_credit_val
+ ,in_credit_frac
+ ,in_expiration_date
+ ,in_gc_date)
+ ON CONFLICT DO NOTHING
+ RETURNING reserve_uuid, reserve_pub)
+ SELECT * FROM reserve_changes;
+ FETCH FROM curs INTO i;
IF FOUND
THEN
-- We made a change, so the reserve did not previously exist.
- out_reserve_found = FALSE;
- ELSE
- -- We made no change, which means the reserve existed.
- out_reserve_found = TRUE;
+ IF in_reserve_pub = i.reserve_pub
+ THEN
+ out_reserve_found = FALSE;
+ ruuid = i.reserve_uuid;
+ END IF;
END IF;
+ CLOSE curs;
+
PERFORM pg_notify(in_notify, NULL);
+ OPEN curs_trans FOR
+ WITH reserve_transaction AS(
INSERT INTO reserves_in
(reserve_pub
,wire_reference
@@ -85,16 +99,18 @@ transaction_duplicate= TRUE;
,in_exchange_account_name
,in_wire_source_h_payto
,in_expiration_date)
- ON CONFLICT DO NOTHING;
+ ON CONFLICT DO NOTHING
+ RETURNING reserve_pub)
+ SELECT * FROM reserve_transaction;
+ FETCH FROM curs_trans INTO i;
IF FOUND
THEN
+ IF i.reserve_pub = in_reserve_pub
+ THEN
-- HAPPY PATH THERE IS NO DUPLICATE TRANS
- transaction_duplicate = FALSE;
- ELSE
- -- Unhappy...
--- RAISE EXCEPTION 'Reserve did not exist, but INSERT into reserves_in gave conflict';
- transaction_duplicate = TRUE;
--- ROLLBACK;
+ transaction_duplicate = FALSE;
+ END IF;
END IF;
+ CLOSE curs_trans;
RETURN;
END $$;