From f60e38077c7e2ddd2ac6ae3e35c4b27f3225512a Mon Sep 17 00:00:00 2001
From: Joseph <Joseph.xu@efrei.net>
Date: Wed, 16 Nov 2022 11:44:48 -0500
Subject: some modifications on batch_test for reserves_in

---
 src/exchangedb/procedures.sql | 58 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

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

diff --git a/src/exchangedb/procedures.sql b/src/exchangedb/procedures.sql
index a732ef75..a9d90294 100644
--- a/src/exchangedb/procedures.sql
+++ b/src/exchangedb/procedures.sql
@@ -2521,3 +2521,61 @@ BEGIN
 END $$;
 
 COMMIT;
+
+/*************************************************************/
+
+
+CREATE OR REPLACE FUNCTION bash_reserves_in(
+  IN amount_val INT8,
+  IN amount_frac INT4,
+  IN rpub BYTEA,
+  IN now INT8,
+  IN min_reserve_gc INT8,
+  OUT reserve_found BOOLEAN,
+  OUT ruuid INT8)
+LANGUAGE plpgsql
+AS $$
+DECLARE
+  existed BOOLEAN;
+  not_existed BOOLEAN;
+BEGIN
+  SELECT reserves.reserve_uuid into ruuid from reserves
+  where reserves.reserve_pub = rpub;
+  IF ruuid IS NOT NULL
+  THEN
+    existed = TRUE;
+    UPDATE reserves
+     SET (current_balance_val
+     	  ,current_balance_frac
+   	   ,expiration_date
+   	    ,gc_date) =
+   	     (amount_val
+   	      ,amount_frac
+   	       ,now
+   	        ,min_reserve_gc)
+      WHERE
+      reserve_pub = rpub
+      RETURNING existed into reserve_found;
+  END IF;
+  IF NOT FOUND
+  THEN
+    SELECT MAX(reserve_uuid)+1 into ruuid from reserves;
+    existed = FALSE;
+    INSERT INTO reserves
+    (reserve_uuid
+    ,reserve_pub
+    ,current_balance_val
+    ,current_balance_frac
+    ,expiration_date
+    ,gc_date)
+    VALUES
+    (ruuid
+    ,rpub
+    ,amount_val
+    ,amount_frac
+    ,now
+    ,min_reserve_gc) RETURNING existed into reserve_found;
+
+  END IF;
+
+END $$;
-- 
cgit v1.2.3

