{# ============================ Update View ========================= #}
{% import 'macros/schemas/security.macros' as SECLABEL %}
{% import 'macros/schemas/privilege.macros' as PRIVILEGE %}
{% if data %}
{% set view_name = data.name if data.name else o_data.name %}
{% set view_schema = data.schema if data.schema else o_data.schema %}
{% set def = data.definition.rstrip(';') if data.definition %}
{% if data.name and data.name != o_data.name %}
ALTER VIEW {{ conn|qtIdent(o_data.schema, o_data.name) }}
    RENAME TO {{ conn|qtIdent(data.name) }};
{% endif %}
{% if data.schema and data.schema != o_data.schema %}
ALTER VIEW {{ conn|qtIdent(o_data.schema, view_name ) }}
    SET SCHEMA {{ conn|qtIdent(data.schema) }};
{% endif %}
{% if def and def != o_data.definition.rstrip(';') %}
{% if data.del_sql %}
DROP VIEW {{ conn|qtIdent(view_schema, view_name) }};
{% endif %}
CREATE OR REPLACE VIEW {{ conn|qtIdent(view_schema, view_name) }}
{% if ((data.check_option and data.check_option.lower() != 'no') or data.security_barrier) %}
    WITH ({% if (data.check_option or o_data.check_option) %}check_option={{ data.check_option if data.check_option else o_data.check_option }}{{', ' }}{% endif %}security_barrier={{ data.security_barrier|lower if data.security_barrier is defined else o_data.security_barrier|default('false', 'true')|lower }})
{% endif %}
    AS
    {{ def }};
{% else %}
{% if (data.security_barrier is defined and data.security_barrier|lower !=  o_data.security_barrier|lower) %}
ALTER VIEW {{ conn|qtIdent(view_schema, view_name) }}
    SET (security_barrier={{ data.security_barrier|lower }});
{% endif %}
{% if (data.check_option and data.check_option != o_data.check_option and data.check_option != 'no') %}
ALTER VIEW {{ conn|qtIdent(view_schema, view_name) }}
    SET (check_option={{ data.check_option }});
{% elif (data.check_option and data.check_option != o_data.check_option and data.check_option == 'no') %}
ALTER VIEW {{ conn|qtIdent(view_schema, view_name) }} RESET (check_option);
{% endif %}
{% endif %}
{% if data.owner and data.owner != o_data.owner %}
ALTER TABLE {{ conn|qtIdent(view_schema, view_name) }}
    OWNER TO {{ conn|qtIdent(data.owner) }};
{% endif %}
{% set old_comment = o_data.comment|default('', true) %}
{% if (data.comment is defined and (data.comment != old_comment)) %}

COMMENT ON VIEW {{ conn|qtIdent(view_schema, view_name) }}
    IS {{ data.comment|qtLiteral }};
{% elif  data.del_sql == True and old_comment != '' %}
COMMENT ON VIEW {{ conn|qtIdent(view_schema, view_name) }}
    IS {{ old_comment|qtLiteral }};
{% endif %}
{# The SQL generated below will change privileges #}
{% if o_data.acl_sql and o_data.acl_sql != '' %}
{{o_data['acl_sql']}}
{% endif %}
{% if data.datacl %}
{% if 'deleted' in data.datacl %}
{% for priv in data.datacl.deleted %}
{{ PRIVILEGE.UNSETALL(conn, 'TABLE', priv.grantee, data.name, data.schema) }}
{% endfor %}
{% endif %}
{% if 'changed' in data.datacl %}
{% for priv in data.datacl.changed %}
{{ PRIVILEGE.UNSETALL(conn, 'TABLE', priv.grantee, data.name, data.schema) }}
{{ PRIVILEGE.SET(conn, 'TABLE', priv.grantee, data.name, priv.without_grant, priv.with_grant, data.schema) }}
{% endfor %}
{% endif %}
{% if 'added' in data.datacl %}
{% for priv in data.datacl.added %}
{{ PRIVILEGE.SET(conn, 'TABLE', priv.grantee, data.name, priv.without_grant, priv.with_grant, data.schema) }}
{% endfor %}
{% endif %}
{% endif %}
{# The SQL generated below will change Security Label #}
{% if data.seclabels is not none and data.seclabels|length > 0 %}
{% set seclabels = data.seclabels %}
{% if 'deleted' in seclabels and seclabels.deleted|length > 0 %}
{% for r in seclabels.deleted %}
{{ SECLABEL.UNSET(conn, 'VIEW', data.name, r.provider, data.schema) }}
{% endfor %}
{% endif %}
{% if 'added' in seclabels and seclabels.added|length > 0 %}
{% for r in seclabels.added %}
{{ SECLABEL.SET(conn, 'VIEW', data.name, r.provider, r.label, data.schema) }}
{% endfor %}
{% endif %}
{% if 'changed' in seclabels and seclabels.changed|length > 0 %}
{% for r in seclabels.changed %}
{{ SECLABEL.SET(conn, 'VIEW', data.name, r.provider, r.label, data.schema) }}
{% endfor %}
{% endif %}
{% endif %}
{% endif %}
