--
-- 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(); Type: FUNCTION; Schema: functions; Owner: -
--

CREATE FUNCTION __alt() RETURNS trigger
    LANGUAGE plpgsql IMMUTABLE
    AS $$
BEGIN
    new.result := functions._alt(new."0", new."1", new."2", new."3", new."4",
        new."5", new."6", new."7", new."8", new."9");
    RETURN new;
END;
$$;


--
-- Name: __label(); Type: FUNCTION; Schema: functions; Owner: -
--

CREATE FUNCTION __label() RETURNS trigger
    LANGUAGE plpgsql IMMUTABLE
    AS $$
BEGIN
    new.result := functions._label(new.label, new.value);
    RETURN new;
END;
$$;


--
-- Name: __merge(); Type: FUNCTION; Schema: functions; Owner: -
--

CREATE FUNCTION __merge() RETURNS trigger
    LANGUAGE plpgsql IMMUTABLE
    AS $$
BEGIN
    new.result := functions._merge(new."0", new."1", new."2", new."3", new."4",
        new."5", new."6", new."7", new."8", new."9");
    RETURN new;
END;
$$;


--
-- Name: __nullIf(); Type: FUNCTION; Schema: functions; Owner: -
--

CREATE FUNCTION "__nullIf"() RETURNS trigger
    LANGUAGE plpgsql IMMUTABLE
    AS $$
BEGIN
    new.result := functions."_nullIf"(new.value, new."null", new.type);
    RETURN new;
END;
$$;


--
-- 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)
    )
    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_(text, text, text); Type: FUNCTION; Schema: functions; Owner: -
--

CREATE FUNCTION join_strs_(state text, delim text, value text) RETURNS text
    LANGUAGE sql IMMUTABLE
    AS $_$
SELECT $1 || (CASE
WHEN $1 = '' OR $3 IS NULL OR $3 = ''
THEN ''
ELSE $2
END) || coalesce($3, '');
$_$;


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

CREATE AGGREGATE join_strs(text, text) (
    SFUNC = join_strs_,
    STYPE = text,
    INITCOND = ''
);


SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: _alt; Type: TABLE; Schema: functions; Owner: -; Tablespace: 
--

CREATE TABLE _alt (
    result text,
    not_null_col boolean DEFAULT true NOT NULL,
    "0" text,
    "1" text,
    "2" text,
    "3" text,
    "4" text,
    "5" text,
    "6" text,
    "7" text,
    "8" text,
    "9" text
);


--
-- Name: _label; Type: TABLE; Schema: functions; Owner: -; Tablespace: 
--

CREATE TABLE _label (
    result text,
    not_null_col boolean DEFAULT true NOT NULL,
    label text NOT NULL,
    value text
);


--
-- Name: _merge; Type: TABLE; Schema: functions; Owner: -; Tablespace: 
--

CREATE TABLE _merge (
    result text,
    not_null_col boolean DEFAULT true NOT NULL,
    "0" text,
    "1" text,
    "2" text,
    "3" text,
    "4" text,
    "5" text,
    "6" text,
    "7" text,
    "8" text,
    "9" text
);


--
-- Name: _nullIf; Type: TABLE; Schema: functions; Owner: -; Tablespace: 
--

CREATE TABLE "_nullIf" (
    result text,
    not_null_col boolean DEFAULT true NOT NULL,
    "null" text NOT NULL,
    type datatype DEFAULT 'str'::datatype NOT NULL,
    value text
);


--
-- Name: _alt_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX _alt_unique ON _alt USING btree ((COALESCE("0", '\\N'::text)), (COALESCE("1", '\\N'::text)), (COALESCE("2", '\\N'::text)), (COALESCE("3", '\\N'::text)), (COALESCE("4", '\\N'::text)), (COALESCE("5", '\\N'::text)), (COALESCE("6", '\\N'::text)), (COALESCE("7", '\\N'::text)), (COALESCE("8", '\\N'::text)), (COALESCE("9", '\\N'::text)));


--
-- Name: _label_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX _label_unique ON _label USING btree ((COALESCE(label, '\\N'::text)), (COALESCE(value, '\\N'::text)));


--
-- Name: _merge_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX _merge_unique ON _merge USING btree ((COALESCE("0", '\\N'::text)), (COALESCE("1", '\\N'::text)), (COALESCE("2", '\\N'::text)), (COALESCE("3", '\\N'::text)), (COALESCE("4", '\\N'::text)), (COALESCE("5", '\\N'::text)), (COALESCE("6", '\\N'::text)), (COALESCE("7", '\\N'::text)), (COALESCE("8", '\\N'::text)), (COALESCE("9", '\\N'::text)));


--
-- Name: _nullIf_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX "_nullIf_unique" ON "_nullIf" USING btree ("null", type, (COALESCE(value, '\\N'::text)));


--
-- Name: _alt; Type: TRIGGER; Schema: functions; Owner: -
--

CREATE TRIGGER _alt BEFORE INSERT OR UPDATE ON _alt FOR EACH ROW EXECUTE PROCEDURE __alt();


--
-- Name: _label; Type: TRIGGER; Schema: functions; Owner: -
--

CREATE TRIGGER _label BEFORE INSERT OR UPDATE ON _label FOR EACH ROW EXECUTE PROCEDURE __label();


--
-- Name: _merge; Type: TRIGGER; Schema: functions; Owner: -
--

CREATE TRIGGER _merge BEFORE INSERT OR UPDATE ON _merge FOR EACH ROW EXECUTE PROCEDURE __merge();


--
-- Name: _nullIf; Type: TRIGGER; Schema: functions; Owner: -
--

CREATE TRIGGER "_nullIf" BEFORE INSERT OR UPDATE ON "_nullIf" FOR EACH ROW EXECUTE PROCEDURE "__nullIf"();


--
-- PostgreSQL database dump complete
--

