{#################################################}
{# This will be specific macro for pga_schedule. #}
{#################################################}
{% import 'macros/pga_exception.macros' as EXCEPTIONS %}
{% macro INSERT(jid, data) -%}
-- Inserting a schedule{% if jid %} (jobid: {{ jid|qtLiteral }}){% endif %}

INSERT INTO pgagent.pga_schedule(
    jscjobid, jscname, jscdesc, jscenabled,
    jscstart, {% if data.jscend %}jscend,{% endif %}
    jscminutes, jschours, jscweekdays, jscmonthdays, jscmonths
) VALUES (
    {% if jid %}{{ jid|qtLiteral }}{% else %}jid{% endif %}, {{ data.jscname|qtLiteral }}::text, {{ data.jscdesc|qtLiteral }}::text, {% if data.jscenabled %}true{% else %}false{% endif %},
    {{ data.jscstart|qtLiteral }}::timestamp with time zone, {% if data.jscend %}{{ data.jscend|qtLiteral }}::timestamp with time zone,{% endif %}

    -- Minutes
    {{ data.jscminutes|qtLiteral }}::boolean[],
    -- Hours
    {{ data.jschours|qtLiteral }}::boolean[],
    -- Week days
    {{ data.jscweekdays|qtLiteral }}::boolean[],
    -- Month days
    {{ data.jscmonthdays|qtLiteral }}::boolean[],
    -- Months
    {{ data.jscmonths|qtLiteral }}::boolean[]
) RETURNING jscid {% if not jid %}INTO scid;{% endif %}{% if 'jscexceptions' in data %}
{% for exc in data.jscexceptions %}

{{ EXCEPTIONS.INSERT(None, exc) }}{% endfor %}{% endif %}
{%- endmacro %}
{% macro UPDATE(jid, jscid, data) -%}
{% if 'jscname' in data or 'jscenabled' in data or 'jscdesc' in data or 'jscstart' in data or 'jscend' in data or 'jscmonths' in data or 'jscminutes' in data or 'jscmonthdays' in data or 'jschours' in data or 'jscweekdays' in data %}

-- Updating the schedule (id: {{ jscid|qtLiteral }}, jobid: {{ jid|qtLiteral }})
UPDATE pgagent.pga_schedule
SET{% if 'jscname' in data %}

    jscname={{ data.jscname|qtLiteral }}::text{% if 'jscdesc' in data or 'jscstart' in data or 'jscend' in data or 'jscmonths' in data or 'jscminutes' in data or 'jscmonthdays' in data or 'jschours' in data or 'jscweekdays' in data or 'jscenabled' in data %},{% endif %}{% endif %}{% if 'jscenabled' in data %}

    jscenabled={% if data.jscenabled %}true{% else %}false{% endif %}{% if 'jscdesc' in data or 'jscstart' in data or 'jscend' in data or 'jscmonths' in data or 'jscminutes' in data or 'jscmonthdays' in data or 'jschours' in data or 'jscweekdays' in data %},{% endif %}{% endif %}{% if 'jscdesc' in data %}

    jscdesc={% if data.jscdesc %}{{ data.jscdesc|qtLiteral }}::text{% else %}''::text{% endif %}{% if 'jscstart' in data or 'jscend' in data or 'jscmonths' in data or 'jscminutes' in data or 'jscmonthdays' in data or 'jschours' in data or 'jscweekdays' in data %},{% endif %}{% endif %}{% if 'jscstart' in data %}

    jscstart={{ data.jscstart|qtLiteral }}::timestamptz{% if 'jscend' in data or 'jscmonths' in data or 'jscminutes' in data or 'jscmonthdays' in data or 'jschours' in data or 'jscweekdays' in data %},{% endif %}{% endif %}{% if 'jscend' in data %}

    jscend={% if data.jscend %}{{ data.jscend|qtLiteral }}::timestamptz{% else %}NULL::timestamptz{% endif %}{% if 'jscmonths' in data or 'jscminutes' in data or 'jscmonthdays' in data or 'jschours' in data or 'jscweekdays' in data %},{% endif %}{% endif %}{% if 'jscmonths' in data %}

    jscmonths={{ data.jscmonths|qtLiteral }}::boolean[]{% if 'jscminutes' in data or 'jscmonthdays' in data or 'jschours' in data or 'jscweekdays' in data %},{% endif %}{% endif %}{% if 'jscminutes' in data %}

    jscminutes={{ data.jscminutes|qtLiteral }}::boolean[]{% if 'jscmonthdays' in data or 'jschours' in data or 'jscweekdays' in data %},{% endif %}{% endif %}{% if 'jscmonthdays' in data %}

    jscmonthdays={{ data.jscmonthdays|qtLiteral }}::boolean[]{% if 'jschours' in data or 'jscweekdays' in data %},{% endif %}{% endif %}{% if 'jschours' in data %}

    jschours={{ data.jschours|qtLiteral }}::boolean[]{% if 'jscweekdays' in data %},{% endif %}{% endif %}{% if 'jscweekdays' in data %}

    jscweekdays={{ data.jscweekdays|qtLiteral }}::boolean[]{% endif %}

WHERE jscid={{ jscid|qtLiteral }}::integer AND jscjobid={{ jid|qtLiteral }}::integer;{% endif %}{% if 'jscexceptions' in data %}
{% if 'added' in data.jscexceptions and data.jscexceptions.added|length > 0 %}

{% for exc in data.jscexceptions.added %}
{{ EXCEPTIONS.INSERT(jscid, exc) }}
{% endfor %}
{% endif %}
{% if 'deleted' in data.jscexceptions and data.jscexceptions.deleted|length > 0 %}

{% for exc in data.jscexceptions.deleted %}
{{ EXCEPTIONS.DELETE(jscid, exc) }}
{% endfor %}
{% endif %}
{% if 'changed' in data.jscexceptions and data.jscexceptions.changed|length > 0 %}

{% for exc in data.jscexceptions.changed %}
{{ EXCEPTIONS.UPDATE(jscid, exc) }}
{% endfor %}
{% endif %}
{% endif %}
{%- endmacro %}
{% macro DELETE(jid, jscid) -%}
-- Removing the existing schedule (id: {{ jscid|qtLiteral }}, jobid: {{ jid|qtLiteral }})
DELETE FROM pgagent.pga_schedule WHERE jscid={{ jscid|qtLiteral }}::integer AND jscjobid={{ jid|qtLiteral }}::integer;
{%- endmacro %}
{% macro FETCH_CURRENT() -%}
SELECT jscid FROM pgagent.pga_schedule WHERE xmin::text = (txid_current() % (2^32)::bigint)::text;
{%- endmacro %}
{% macro PROPERTIES(jid, jscid) -%}
SELECT
    jscid,  jscjobid, jscname, jscdesc, jscenabled, jscstart, jscend,
    jscminutes, jschours, jscweekdays, jscmonthdays, jscmonths,
    jexid, jexdate, jextime
FROM
    pgagent.pga_schedule s
    LEFT JOIN (
        SELECT
            jexscid, array_agg(jexid) AS jexid, array_agg(to_char(jexdate, 'YYYY-MM-DD')) AS jexdate,
            array_agg(jextime) AS jextime
        FROM
            pgagent.pga_exception ex
        GROUP BY
            jexscid
    ) e ON s.jscid = e.jexscid
WHERE
{% if jscid %}
   s.jscid = {{ jscid|qtLiteral }}::integer AND
{% endif %}
   s.jscjobid = {{ jid|qtLiteral }}::integer
ORDER BY jscname;
{%- endmacro %}
