Tema: Re: plpgsql suma zodziu
Autorius: Laimis
Data: 2011-01-17 03:55:00
-- Function: num_to_text_ce(integer, integer, text[])
/*
   Copyright (c) 2011, Laimis <wiela@centras.lt>
   All rights reserved.

   Licensed under the BSD license.
*/

-- DROP FUNCTION num_to_text_ce(integer, integer, text[]);

CREATE OR REPLACE FUNCTION num_to_text_ce(cv integer, df integer, ce text[])
  RETURNS character varying AS
$BODY$
DECLARE
    d INTEGER := df % 10;
    k INTEGER := 1;
    
    /*	
        XXX: PG/PLSQL (as of v.8.4) multi dimensional array slicing, subarray 
        (de)referencing mess...	
        [{*]{x, y, z, ...}[}*] -> {x, y, z, ...}; 
    */
    -- re TEXT[]:= ARRAY(SELECT unnest(ce));

BEGIN

    /* 
        Two numeral case variants: 
            k = 1 (cv|tn = 1,3,4,5,6,7,...): 
                šimt(-as,-ai,-ų), milijon(-as,-ai,-ų), milijard(-as,-ai,-ų), ...;

            k = 2 (cv|tn = 2): 
                tūkstan(-tis,-čiai,-čių).
    */
    IF cv = 2 THEN
	k := cv;
    END IF;
    

    CASE
	-- [0-9][0], [11-19]: pl. genitive (lt: dgs. kilmininkas; -ų, -čių)
        WHEN  d = 0 OR df BETWEEN 11 AND 19 THEN
	    return ce[k][3];

	-- [0-9][1]: sg. nominative (lt: vns. vardininkas; -as, -tis)
	WHEN  d = 1 THEN
	    return ce[k][1];
	
	-- [0,2-9][2-9]: pl. nominative (lt: dgs. vardininkas; -ai, -čiai)
	ELSE 
	    return ce[k][2];
	    
    END CASE;

END;
$BODY$
  LANGUAGE 'plpgsql' IMMUTABLE
  COST 100;
ALTER FUNCTION num_to_text_ce(integer, integer, text[]) OWNER TO postgres;
COMMENT ON FUNCTION num_to_text_ce(integer, integer, text[]) IS 'Used by num_to_text().
Returns ending for the particular case according to numeral (case variant -- cv; triad number), 
double-figure (df) number value and case endings (ce).

/*
   Copyright (c) 2011, Laimis <wiela@centras.lt>
   All rights reserved.

   Licensed under the BSD license.
*/

';