aboutsummaryrefslogtreecommitdiff
path: root/src/exchangedb/exchange_do_reserves_in_insert.sql
diff options
context:
space:
mode:
authorÖzgür Kesim <oec-taler@kesim.org>2023-06-03 10:45:31 +0200
committerÖzgür Kesim <oec-taler@kesim.org>2023-06-03 10:45:31 +0200
commit80a1b8f5240e8307e1c73862fb8810d7c2bdc874 (patch)
tree5661068ec9976aa8711dc931355f6bd87ab35fd6 /src/exchangedb/exchange_do_reserves_in_insert.sql
parent4a31a180a4595aa040060e91ccde4f839aa02f72 (diff)
parent2ea3ae1008020589b43a51663c45556a08547212 (diff)
downloadexchange-80a1b8f5240e8307e1c73862fb8810d7c2bdc874.tar.xz
Merge branch 'master' into age-withdraw
Diffstat (limited to 'src/exchangedb/exchange_do_reserves_in_insert.sql')
-rw-r--r--src/exchangedb/exchange_do_reserves_in_insert.sql136
1 files changed, 136 insertions, 0 deletions
diff --git a/src/exchangedb/exchange_do_reserves_in_insert.sql b/src/exchangedb/exchange_do_reserves_in_insert.sql
index dffcd8b55..cf57d8e81 100644
--- a/src/exchangedb/exchange_do_reserves_in_insert.sql
+++ b/src/exchangedb/exchange_do_reserves_in_insert.sql
@@ -963,3 +963,139 @@ BEGIN
CLOSE curs_transaction_exist;
RETURN;
END $$;
+
+
+
+
+
+
+
+DO $$
+BEGIN
+ CREATE TYPE exchange_do_array_reserve_insert_return_type
+ AS
+ (transaction_duplicate BOOLEAN
+ ,ruuid INT8);
+EXCEPTION
+ WHEN duplicate_object THEN null;
+END
+$$;
+
+CREATE OR REPLACE FUNCTION exchange_do_array_reserves_insert(
+ IN in_gc_date INT8,
+ IN in_reserve_expiration INT8,
+ IN ina_reserve_pub BYTEA[],
+ IN ina_wire_ref INT8[],
+ IN ina_credit_val INT8[],
+ IN ina_credit_frac INT4[],
+ IN ina_exchange_account_name VARCHAR[],
+ IN ina_execution_date INT8[],
+ IN ina_wire_source_h_payto BYTEA[],
+ IN ina_payto_uri VARCHAR[],
+ IN ina_notify TEXT[])
+RETURNS SETOF exchange_do_array_reserve_insert_return_type
+LANGUAGE plpgsql
+AS $$
+DECLARE
+ curs REFCURSOR;
+DECLARE
+ conflict BOOL;
+DECLARE
+ dup BOOL;
+DECLARE
+ uuid INT8;
+DECLARE
+ i RECORD;
+BEGIN
+
+ INSERT INTO wire_targets
+ (wire_target_h_payto
+ ,payto_uri)
+ SELECT
+ wire_source_h_payto
+ ,payto_uri
+ FROM
+ UNNEST (ina_wire_source_h_payto) AS wire_source_h_payto
+ ,UNNEST (ina_payto_uri) AS payto_uri
+ ON CONFLICT DO NOTHING;
+
+ FOR i IN
+ SELECT
+ reserve_pub
+ ,wire_ref
+ ,credit_val
+ ,credit_frac
+ ,exchange_account_name
+ ,execution_date
+ ,wire_source_h_payto
+ ,payto_uri
+ ,notify
+ FROM
+ UNNEST (ina_reserve_pub) AS reserve_pub
+ ,UNNEST (ina_wire_ref) AS wire_ref
+ ,UNNEST (ina_credit_val) AS credit_val
+ ,UNNEST (ina_credit_frac) AS credit_frac
+ ,UNNEST (ina_exchange_account_name) AS exchange_account_name
+ ,UNNEST (ina_execution_date) AS execution_date
+ ,UNNEST (ina_wire_source_h_payto) AS wire_source_h_payto
+ ,UNNEST (ina_notify) AS notify
+ LOOP
+ INSERT INTO reserves
+ (reserve_pub
+ ,current_balance_val
+ ,current_balance_frac
+ ,expiration_date
+ ,gc_date
+ ) VALUES (
+ i.reserve_pub
+ ,i.credit_val
+ ,i.credit_frac
+ ,in_reserve_expiration
+ ,in_gc_date
+ )
+ ON CONFLICT DO NOTHING
+ RETURNING reserve_uuid
+ INTO uuid;
+ conflict = NOT FOUND;
+
+ INSERT INTO reserves_in
+ (reserve_pub
+ ,wire_reference
+ ,credit_val
+ ,credit_frac
+ ,exchange_account_section
+ ,wire_source_h_payto
+ ,execution_date
+ ) VALUES (
+ i.reserve_pub
+ ,i.wire_reference
+ ,i.credit_val
+ ,i.credit_frac
+ ,i.exchange_account_section
+ ,i.wire_source_h_payto
+ ,i.execution_date
+ )
+ ON CONFLICT DO NOTHING;
+
+ IF NOT FOUND
+ THEN
+ IF conflict
+ THEN
+ dup = TRUE;
+ else
+ dup = FALSE;
+ END IF;
+ ELSE
+ IF NOT conflict
+ THEN
+ EXECUTE FORMAT (
+ 'NOTIFY %s'
+ ,i.notify);
+ END IF;
+ dup = FALSE;
+ END IF;
+ RETURN NEXT (dup,uuid);
+ END LOOP;
+
+ RETURN;
+END $$;