pg_uri_escape.sql (1254B)
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 DROP FUNCTION IF EXISTS uri_escape; 17 CREATE FUNCTION uri_escape(input TEXT) 18 RETURNS TEXT 19 LANGUAGE sql 20 IMMUTABLE 21 PARALLEL SAFE 22 AS $$ 23 SELECT COALESCE(string_agg( 24 CASE 25 WHEN chr(get_byte(b, i)) ~ '[A-Za-z0-9._~-]' 26 THEN chr(get_byte(b, i)) 27 ELSE '%' || lpad(upper(to_hex(get_byte(b, i))), 2, '0') 28 END, 29 '' 30 ), '') 31 FROM ( 32 SELECT convert_to(input, 'UTF8') AS b 33 ) s, 34 generate_series(0, length(b) - 1) AS i; 35 $$; 36 COMMENT ON FUNCTION uri_escape(TEXT) 37 IS 'Percent-encodes all characters except RFC 3986 unreserved chars: A-Z a-z 0-9 - . _ ~';