exchange_do_select_aggregations_above_serial.sql (2126B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2023 Taler Systems SA 4 -- 5 -- TALER is free software; you can redistribute it and/or modify it under the 6 -- terms of the GNU General Public License as published by the Free Software 7 -- Foundation; either version 3, or (at your option) any later version. 8 -- 9 -- TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 -- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 -- A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 -- 13 -- You should have received a copy of the GNU General Public License along with 14 -- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 -- 16 -- @author: Christian Grothoff 17 18 CREATE OR REPLACE FUNCTION exchange_do_select_aggregations_above_serial( 19 IN in_min_serial_id INT8) 20 RETURNS SETOF exchange_do_select_aggregations_above_serial_return_type 21 LANGUAGE plpgsql 22 AS $$ 23 DECLARE 24 aggregation CURSOR 25 FOR 26 SELECT 27 batch_deposit_serial_id 28 ,aggregation_serial_id 29 FROM aggregation_tracking 30 WHERE aggregation_serial_id >= in_min_serial_id 31 ORDER BY aggregation_serial_id ASC; 32 DECLARE 33 my_total_val INT8; -- all deposits without wire 34 DECLARE 35 my_total_frac INT8; -- all deposits without wire (fraction, not normalized) 36 DECLARE 37 my_total taler_amount; -- amount that was originally deposited 38 DECLARE 39 my_batch_record RECORD; 40 DECLARE 41 i RECORD; 42 BEGIN 43 44 OPEN aggregation; 45 LOOP 46 FETCH NEXT FROM aggregation INTO i; 47 EXIT WHEN NOT FOUND; 48 49 SELECT 50 SUM((cdep.amount_with_fee).val) AS total_val 51 ,SUM((cdep.amount_with_fee).frac::INT8) AS total_frac 52 INTO 53 my_batch_record 54 FROM coin_deposits cdep 55 WHERE cdep.batch_deposit_serial_id = i.batch_deposit_serial_id; 56 57 my_total_val=my_batch_record.total_val; 58 my_total_frac=my_batch_record.total_frac; 59 60 -- Normalize total amount 61 my_total.val = my_total_val + my_total_frac / 100000000; 62 my_total.frac = my_total_frac % 100000000; 63 RETURN NEXT ( 64 i.batch_deposit_serial_id 65 ,i.aggregation_serial_id 66 ,my_total 67 ); 68 69 END LOOP; 70 CLOSE aggregation; 71 RETURN; 72 END $$;