donau

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

donau_do_amount_specific.sql (2578B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2024 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 --------------------------------------------------------------
     18 -- Taler amounts and helper functions
     19 -------------------------------------------------------------
     20 
     21 CREATE OR REPLACE FUNCTION amount_normalize(
     22     IN amount taler_amount
     23   ,OUT normalized taler_amount
     24 )
     25 LANGUAGE plpgsql
     26 AS $$
     27 BEGIN
     28   normalized.val = amount.val + amount.frac / 100000000;
     29   normalized.frac = amount.frac % 100000000;
     30 END $$;
     31 
     32 COMMENT ON FUNCTION amount_normalize
     33   IS 'Returns the normalized amount by adding to the .val the value of (.frac / 100000000) and removing the modulus 100000000 from .frac.';
     34 
     35 CREATE OR REPLACE FUNCTION amount_add(
     36    IN a taler_amount
     37   ,IN b taler_amount
     38   ,OUT sum taler_amount
     39 )
     40 LANGUAGE plpgsql
     41 AS $$
     42 BEGIN
     43   sum = (a.val + b.val, a.frac + b.frac);
     44   SELECT * FROM amount_normalize(sum) INTO sum;
     45 
     46   IF (sum.val > (1<<52))
     47   THEN
     48     RAISE EXCEPTION 'addition overflow';
     49   END IF;
     50 END $$;
     51 
     52 COMMENT ON FUNCTION amount_add
     53   IS 'Returns the normalized sum of two amounts. It raises an exception when the resulting .val is larger than 2^52';
     54 
     55 CREATE OR REPLACE FUNCTION amount_left_minus_right(
     56   IN  l taler_amount
     57  ,IN  r taler_amount
     58  ,OUT diff taler_amount
     59  ,OUT ok BOOLEAN
     60 )
     61 LANGUAGE plpgsql
     62 AS $$
     63 BEGIN
     64 
     65 IF (l.val > r.val)
     66 THEN
     67   ok = TRUE;
     68   IF (l.frac >= r.frac)
     69   THEN
     70     diff.val  = l.val  - r.val;
     71     diff.frac = l.frac - r.frac;
     72   ELSE
     73     diff.val  = l.val  - r.val - 1;
     74     diff.frac = l.frac + 100000000 - r.frac;
     75   END IF;
     76 ELSE
     77   IF (l.val = r.val) AND (l.frac >= r.frac)
     78   THEN
     79     diff.val  = 0;
     80     diff.frac = l.frac - r.frac;
     81     ok = TRUE;
     82   ELSE
     83     diff = (-1, -1);
     84     ok = FALSE;
     85   END IF;
     86 END IF;
     87 
     88 RETURN;
     89 END $$;
     90 
     91 COMMENT ON FUNCTION amount_left_minus_right
     92   IS 'Subtracts the right amount from the left and returns the difference and TRUE, if the left amount is larger than the right, or an invalid amount and FALSE otherwise.';