do_update_fountain.sql (3125B)
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_do_update_fountain; 18 CREATE FUNCTION merchant_do_update_fountain( 19 IN in_fountain_id TEXT 20 ,IN in_description TEXT -- can be NULL: keep previous value 21 ,IN in_poll_freq INT8 -- can be NULL: keep previous value 22 ,IN in_replace_grants BOOL 23 ,IN in_token_family_slugs TEXT[] 24 ,IN in_tokens_per_period_limits INT8[] 25 ,IN in_tokens_per_period_stashes INT8[] 26 ,IN in_key_window_sizes INT8[] 27 ,OUT out_not_found BOOL 28 ,OUT out_unknown_slug TEXT) 29 LANGUAGE plpgsql 30 AS $$ 31 DECLARE 32 my_tf_serials INT8[]; 33 my_tf_serial INT8; 34 my_fountain_serial INT8; 35 i INT4; 36 BEGIN 37 out_not_found = FALSE; 38 out_unknown_slug = NULL; 39 40 SELECT fountain_serial 41 INTO my_fountain_serial 42 FROM merchant_fountains 43 WHERE fountain_id = in_fountain_id; 44 IF NOT FOUND 45 THEN 46 out_not_found = TRUE; 47 RETURN; 48 END IF; 49 50 -- Resolve all token family slugs before modifying anything, so an 51 -- unknown slug leaves the fountain unchanged. 52 my_tf_serials = ARRAY[]::INT8[]; 53 IF in_replace_grants 54 THEN 55 FOR i IN 1..COALESCE(array_length (in_token_family_slugs, 1), 0) 56 LOOP 57 SELECT token_family_serial 58 INTO my_tf_serial 59 FROM merchant_token_families 60 WHERE slug = in_token_family_slugs[i]; 61 IF NOT FOUND 62 THEN 63 out_unknown_slug = in_token_family_slugs[i]; 64 RETURN; 65 END IF; 66 my_tf_serials = array_append (my_tf_serials, my_tf_serial); 67 END LOOP; 68 END IF; 69 70 UPDATE merchant_fountains 71 SET description = COALESCE(in_description, description) 72 ,poll_freq = COALESCE(in_poll_freq, poll_freq) 73 WHERE fountain_serial = my_fountain_serial; 74 75 IF in_replace_grants 76 THEN 77 -- Replace the grant set. Withdrawal counters in 78 -- merchant_fountain_withdrawals are deliberately left untouched: 79 -- consumed quota must survive a grant replacement. 80 DELETE FROM merchant_fountain_grants 81 WHERE fountain_serial = my_fountain_serial; 82 83 FOR i IN 1..COALESCE(array_length (my_tf_serials, 1), 0) 84 LOOP 85 INSERT INTO merchant_fountain_grants 86 (fountain_serial 87 ,token_family_serial 88 ,tokens_per_period_limit 89 ,tokens_per_period_stash 90 ,key_window_size 91 ) VALUES 92 (my_fountain_serial 93 ,my_tf_serials[i] 94 ,in_tokens_per_period_limits[i] 95 ,in_tokens_per_period_stashes[i] 96 ,in_key_window_sizes[i]); 97 END LOOP; 98 END IF; 99 100 END $$;