lookup_reports_pending.sql (2911B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2026 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 17 DROP FUNCTION IF EXISTS merchant.lookup_reports_pending(); 18 CREATE FUNCTION merchant.lookup_reports_pending() 19 RETURNS TABLE( 20 out_merchant_id TEXT, 21 out_report_serial INT8, 22 out_report_program_section TEXT, 23 out_report_description TEXT, 24 out_mime_type TEXT, 25 out_report_token BYTEA, 26 out_target_address TEXT, 27 out_frequency INT8, 28 out_frequency_shift INT8, 29 out_next_transmission INT8, 30 out_one_shot_hidden BOOLEAN) 31 LANGUAGE plpgsql 32 AS $FN$ 33 DECLARE 34 rec RECORD; 35 s TEXT; 36 inner_rec RECORD; 37 xfound BOOLEAN := FALSE; 38 BEGIN 39 FOR rec IN 40 SELECT merchant_serial 41 ,merchant_id 42 FROM merchant.merchant_instances 43 LOOP 44 s := 'merchant_instance_' || rec.merchant_serial::TEXT; 45 BEGIN 46 EXECUTE format( 47 'SELECT' 48 ' report_serial AS rs' 49 ' ,report_program_section AS rps' 50 ' ,report_description AS rd' 51 ' ,mime_type AS mt' 52 ' ,report_token AS rt' 53 ' ,target_address AS ta' 54 ' ,frequency AS f' 55 ' ,frequency_shift AS fs' 56 ' ,next_transmission AS nt' 57 ' ,one_shot_hidden AS osh' 58 ' FROM %I.merchant_reports' 59 ' ORDER BY next_transmission ASC LIMIT 1', s) 60 INTO inner_rec; 61 IF inner_rec IS NULL 62 THEN 63 CONTINUE; 64 END IF; 65 IF (NOT found) OR (inner_rec.nt < out_next_transmission) 66 THEN 67 out_merchant_id := rec.merchant_id; 68 out_report_serial := inner_rec.rs; 69 out_report_program_section := inner_rec.rps; 70 out_report_description := inner_rec.rd; 71 out_mime_type := inner_rec.mt; 72 out_report_token := inner_rec.rt; 73 out_target_address := inner_rec.ta; 74 out_frequency := inner_rec.f; 75 out_frequency_shift := inner_rec.fs; 76 out_next_transmission := inner_rec.nt; 77 out_one_shot_hidden := inner_rec.osh; 78 xfound := TRUE; 79 END IF; 80 EXCEPTION 81 WHEN undefined_table 82 THEN 83 NULL; 84 END; 85 END LOOP; 86 IF xfound THEN 87 RETURN NEXT; 88 END IF; 89 END 90 $FN$; 91 COMMENT ON FUNCTION merchant.lookup_reports_pending() 92 IS 'Returns the single next-due report (smallest next_transmission)' 93 ' across all instance schemas, or no row if no reports exist.';