donau

Donation authority for GNU Taler (experimental)
Log | Files | Refs | Submodules | README | LICENSE

donau_do_insert_charity.sql (1818B)


      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 
     17 DROP FUNCTION IF EXISTS do_insert_charity;
     18 CREATE FUNCTION do_insert_charity (
     19    IN in_charity_pub BYTEA
     20   ,IN in_charity_name TEXT
     21   ,IN in_charity_url TEXT
     22   ,IN in_max_per_year taler_amount
     23   ,IN in_current_year INT4
     24   ,OUT out_charity_id INT8
     25 )
     26 LANGUAGE plpgsql
     27 AS $$
     28 BEGIN
     29   INSERT INTO charities
     30     (charity_pub
     31     ,charity_name
     32     ,charity_url
     33     ,max_per_year
     34     ,current_year
     35    ) VALUES (
     36     in_charity_pub
     37    ,in_charity_name
     38    ,in_charity_url
     39    ,in_max_per_year
     40    ,in_current_year)
     41    ON CONFLICT DO NOTHING
     42    RETURNING charity_id
     43         INTO out_charity_id;
     44   IF NOT FOUND
     45   THEN
     46     SELECT charity_id
     47       INTO out_charity_id
     48       FROM charities
     49      WHERE charity_pub=in_charity_pub
     50        AND charity_url=in_charity_url
     51        AND charity_name=in_charity_name
     52        AND max_per_year=in_max_per_year;
     53    IF NOT FOUND
     54    THEN
     55      out_charity_id = 0;
     56    END IF;
     57  END IF;
     58 END $$;
     59 COMMIT;
     60 
     61 COMMENT ON FUNCTION do_insert_charity
     62   IS 'Insert a charity. Also succeeds if a charity with the same parameters already exists. If a conflicting charity exists, the out_charity_id is set to 0.';