insert_unit.sql (2404B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2025 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 -- @file insert_unit.sql 17 -- @brief SQL for inserting units 18 -- @author Bohdan Potuzhnyi 19 20 DROP FUNCTION IF EXISTS merchant_do_insert_unit; 21 CREATE FUNCTION merchant_do_insert_unit ( 22 IN in_unit TEXT, 23 IN in_unit_name_long TEXT, 24 IN in_unit_name_short TEXT, 25 IN in_unit_name_long_i18n BYTEA, 26 IN in_unit_name_short_i18n BYTEA, 27 IN in_unit_allow_fraction BOOL, 28 IN in_unit_precision_level INT4, 29 IN in_unit_active BOOL, 30 OUT out_conflict BOOL, 31 OUT out_unit_serial INT8) 32 LANGUAGE plpgsql 33 AS $$ 34 BEGIN 35 -- Reject attempts to shadow builtin identifiers. 36 IF EXISTS ( 37 SELECT 1 38 FROM merchant.merchant_builtin_units bu 39 WHERE bu.unit = in_unit 40 ) THEN 41 out_conflict := TRUE; 42 out_unit_serial := NULL; 43 RETURN; 44 END IF; 45 46 INSERT INTO merchant_custom_units ( 47 unit, 48 unit_name_long, 49 unit_name_short, 50 unit_name_long_i18n, 51 unit_name_short_i18n, 52 unit_allow_fraction, 53 unit_precision_level, 54 unit_active) 55 VALUES ( 56 in_unit, 57 in_unit_name_long, 58 in_unit_name_short, 59 in_unit_name_long_i18n, 60 in_unit_name_short_i18n, 61 in_unit_allow_fraction, 62 in_unit_precision_level, 63 in_unit_active) 64 ON CONFLICT (unit) 65 DO NOTHING 66 RETURNING unit_serial 67 INTO out_unit_serial; 68 69 IF FOUND THEN 70 out_conflict := FALSE; 71 RETURN; 72 END IF; 73 74 -- Conflict: custom unit already exists. 75 SELECT unit_serial 76 INTO out_unit_serial 77 FROM merchant_custom_units 78 WHERE unit = in_unit; 79 80 out_conflict := TRUE; 81 END $$;