From 2030d6491005cd6e714b4a4ef40b13a7dc1bdc74 Mon Sep 17 00:00:00 2001
From: Joseph <Joseph.xu@efrei.net>
Date: Wed, 21 Dec 2022 07:28:46 -0500
Subject: update of batch 2 test

---
 .../exchange_do_batch2_reserves_in_insert.sql      | 55 ++++++++++------------
 1 file changed, 25 insertions(+), 30 deletions(-)

(limited to 'src/exchangedb/exchange_do_batch2_reserves_in_insert.sql')

diff --git a/src/exchangedb/exchange_do_batch2_reserves_in_insert.sql b/src/exchangedb/exchange_do_batch2_reserves_in_insert.sql
index 6a0cc6d78..0e2d37d86 100644
--- a/src/exchangedb/exchange_do_batch2_reserves_in_insert.sql
+++ b/src/exchangedb/exchange_do_batch2_reserves_in_insert.sql
@@ -46,20 +46,12 @@ LANGUAGE plpgsql
 AS $$
 DECLARE
   curs_reserve_exist REFCURSOR;
-
---  FOR SELECT reserve_pub
---  FROM reserves
---  WHERE in_reserve_pub = reserves.reserve_pub
---  OR in2_reserve_pub = reserves.reserve_pub;
 DECLARE
-  curs_transaction_exist CURSOR
-  FOR SELECT reserve_pub
-  FROM reserves_in
-  WHERE in_reserve_pub = reserves_in.reserve_pub
-  OR in2_reserve_pub = reserves_in.reserve_pub;
+  curs_transaction_exist refcursor;
 DECLARE
   i RECORD;
-
+DECLARE
+  r RECORD;
 BEGIN
   --SIMPLE INSERT ON CONFLICT DO NOTHING
   transaction_duplicate=FALSE;
@@ -138,6 +130,8 @@ BEGIN
   PERFORM pg_notify(in_notify, NULL);
   PERFORM pg_notify(in2_notify, NULL);
 
+  OPEN curs_transaction_exist FOR
+  WITH reserve_in_exist AS (
   INSERT INTO reserves_in
     (reserve_pub
     ,wire_reference
@@ -161,33 +155,34 @@ BEGIN
     ,in2_exchange_account_name
     ,in2_wire_source_h_payto
     ,in_expiration_date)
-    ON CONFLICT DO NOTHING;
+    ON CONFLICT DO NOTHING
+    RETURNING reserve_pub)
+  SELECT * FROM reserve_in_exist;
+  FETCH FROM curs_transaction_exist INTO r;
   IF FOUND
   THEN
-    transaction_duplicate = FALSE;  /*HAPPY PATH THERE IS NO DUPLICATE TRANS AND NEW RESERVE*/
-    transaction_duplicate2 = FALSE;
-    RETURN;
-  ELSE
-    FOR l IN curs_transaction_exist
-    LOOP
-      IF in_reserve_pub = l.reserve_pub
-      THEN
-         transaction_duplicate = TRUE;
-      END IF;
-
-      IF in2_reserve_pub = l.reserve_pub
+    IF in_reserve_pub = r.reserve_pub
+    THEN
+       transaction_duplicate = TRUE;
+    END IF;
+    IF in2_reserve_pub = i.reserve_pub
+    THEN
+       transaction_duplicate = TRUE;
+    END IF;
+    FETCH FROM curs_transaction_exist INTO r;
+    IF FOUND
+    THEN
+      IF in_reserve_pub = r.reserve_pub
       THEN
-         transaction_duplicate2 = TRUE;
+        transaction_duplicate = TRUE;
       END IF;
-
-      IF transaction_duplicate AND transaction_duplicate2
+      IF in2_reserve_pub = i.reserve_pub
       THEN
-        RETURN;
+        transaction_duplicate = TRUE;
       END IF;
-    END LOOP;
+    END IF;
   END IF;
 
-  CLOSE curs_reserve_exist;
   CLOSE curs_transaction_exist;
   RETURN;
 END $$;
-- 
cgit v1.2.3

