--
-- PostgreSQL database dump
--

SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;

--
-- Name: functions; Type: SCHEMA; Schema: -; Owner: -
--

CREATE SCHEMA functions;


SET search_path = functions, pg_catalog;

--
-- Name: datatype; Type: TYPE; Schema: functions; Owner: -
--

CREATE TYPE datatype AS ENUM (
    'str',
    'float'
);


--
-- Name: _alt(text, text, text, text, text, text, text, text, text, text); Type: FUNCTION; Schema: functions; Owner: -
--

CREATE FUNCTION _alt("0" text DEFAULT NULL::text, "1" text DEFAULT NULL::text, "2" text DEFAULT NULL::text, "3" text DEFAULT NULL::text, "4" text DEFAULT NULL::text, "5" text DEFAULT NULL::text, "6" text DEFAULT NULL::text, "7" text DEFAULT NULL::text, "8" text DEFAULT NULL::text, "9" text DEFAULT NULL::text) RETURNS text
    LANGUAGE sql IMMUTABLE
    AS $_$
SELECT coalesce($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
$_$;


--
-- Name: _label(text, text); Type: FUNCTION; Schema: functions; Owner: -
--

CREATE FUNCTION _label(label text, value text) RETURNS text
    LANGUAGE plpgsql IMMUTABLE
    AS $$
DECLARE
    label text NOT NULL := label; -- add NOT NULL
BEGIN
    RETURN label||': '||value;
END;
$$;


--
-- Name: _merge(text, text, text, text, text, text, text, text, text, text); Type: FUNCTION; Schema: functions; Owner: -
--

CREATE FUNCTION _merge("0" text DEFAULT NULL::text, "1" text DEFAULT NULL::text, "2" text DEFAULT NULL::text, "3" text DEFAULT NULL::text, "4" text DEFAULT NULL::text, "5" text DEFAULT NULL::text, "6" text DEFAULT NULL::text, "7" text DEFAULT NULL::text, "8" text DEFAULT NULL::text, "9" text DEFAULT NULL::text) RETURNS text
    LANGUAGE sql IMMUTABLE
    AS $_$
SELECT functions.join_strs('; ', value)
FROM
(
    SELECT *
    FROM
    (
        SELECT
        DISTINCT ON (value)
        *
        FROM
        (VALUES
              (0, $1)
            , (1, $2)
            , (2, $3)
            , (3, $4)
            , (4, $5)
            , (5, $6)
            , (6, $7)
            , (7, $8)
            , (8, $9)
            , (9, $10)
        )
        AS v (sort_order, value)
        WHERE value IS NOT NULL
    )
    AS v
    ORDER BY sort_order
)
AS v
$_$;


--
-- Name: _nullIf(text, text, datatype); Type: FUNCTION; Schema: functions; Owner: -
--

CREATE FUNCTION "_nullIf"(value text, "null" text, type datatype DEFAULT 'str'::datatype) RETURNS text
    LANGUAGE plpgsql IMMUTABLE
    AS $$
DECLARE
    "null" text NOT NULL := "null"; -- add NOT NULL
    type functions.datatype NOT NULL := type; -- add NOT NULL
BEGIN
    IF type = 'str' THEN RETURN nullif(value, "null"); -- no cast needed
    -- Invalid value is ignored, but invalid null value generates error
    ELSIF type = 'float' THEN
        DECLARE
            -- Outside the try block so that invalid null value generates error
            "null" double precision := "null"::double precision;
        BEGIN
            RETURN nullif(value::double precision, "null");
        EXCEPTION
            WHEN data_exception THEN RETURN value; -- ignore invalid value
        END;
    END IF;
END;
$$;


--
-- Name: join_strs_transform_fold_empty(text, text, text); Type: FUNCTION; Schema: functions; Owner: -
--

CREATE FUNCTION join_strs_transform_fold_empty(state text, delim text, value text) RETURNS text
    LANGUAGE sql IMMUTABLE
    AS $_$
SELECT functions.join_strs_transform_preserve_empty($1, $2, NULLIF($3, ''))
$_$;


--
-- Name: join_strs_transform_preserve_empty(text, text, text); Type: FUNCTION; Schema: functions; Owner: -
--

CREATE FUNCTION join_strs_transform_preserve_empty(state text, delim text, value text) RETURNS text
    LANGUAGE sql IMMUTABLE
    AS $_$
SELECT (CASE
    WHEN $1 IS NOT NULL AND $3 IS NOT NULL
    THEN $1 || $2 || $3
    ELSE COALESCE($1, $3)
    END)
$_$;


--
-- Name: join_strs(text, text); Type: AGGREGATE; Schema: functions; Owner: -
--

CREATE AGGREGATE join_strs(text, text) (
    SFUNC = join_strs_transform_fold_empty,
    STYPE = text
);


--
-- PostgreSQL database dump complete
--

