{% macro CREATE_TYPE_SQL(conn, type_name, type_length, type_precision, is_type_array) %}
{% if type_name.startswith('time') and type_length %}
{#############################################################}
{###### Need to check against separate time types - START ######}
{#############################################################}
{% if type_name == "timestamp without time zone" %}
timestamp({{ type_length }}) without time zone{% elif type_name == "timestamp with time zone" %}
timestamp({{ type_length }}) with time zone{% elif type_name == "time without time zone" %}
time({{ type_length }}) without time zone{% elif type_name == "time with time zone" %}
time({{ type_length }}) with time zone{% endif %}{% if is_type_array %}
[]{% endif %}
{#############################################################}
{###### Need to check against separate time types - END ######}
{#############################################################}
{% else %}
{{ type_name }}{% if type_length %}
({{ type_length }}{% if type_precision%}, {{ type_precision }}{% endif %}){% endif %}{% if is_type_array %}
[]{% endif %}
{% endif %}
{% endmacro %}
{######################################################}
{##### BELOW MACRO IS USED FOR COLUMN TYPE UPDATE #####}
{######################################################}
{% macro UPDATE_TYPE_SQL(conn, data, o_data) %}
{% if data.attprecision is defined and data.attprecision is none %}
{% set old_precision = none %}
{% elif data.attprecision is defined and data.attprecision is not none %}
{% set data_precision = data.attprecision %}
{% set old_precision = o_data.attprecision %}
{% else %}
{% set old_precision = o_data.attprecision %}
{% endif %}
{% if data.attlen is defined and data.attlen is none %}
{% set old_length = none %}
{% set old_precision = none %}
{% set data_precision = none %}
{% else %}
{% set old_length = o_data.attlen %}
{% endif %}
{% if data.cltype and data.cltype.startswith('time') and data.attlen %}
{#############################################################}
{###### Need to check against separate time types - START ######}
{#############################################################}
{% if data.cltype == "timestamp without time zone" %}
timestamp({{ data.attlen }}) without time zone {% elif data.cltype == "timestamp with time zone" %}
timestamp({{ data.attlen }}) with time zone {% elif data.cltype == "time without time zone" %}
time({{ data.attlen }}) without time zone {% elif data.cltype == "time with time zone" %}
time({{ data.attlen }}) with time zone {% endif %}{% if data.hasSqrBracket %}[]{% endif %}
{#############################################################}
{# if only type changes, we need to give previous length to current type#}
{#############################################################}
{% elif data.cltype and data.cltype.startswith('time') %}
{% if data.cltype == "timestamp without time zone" %}
timestamp{% if o_data.attlen is not none %}({{ o_data.attlen }}){% endif %} without time zone {% elif data.cltype == "timestamp with time zone" %}
timestamp{% if o_data.attlen is not none %}({{ o_data.attlen }}){% endif %} with time zone {% elif data.cltype == "time without time zone" %}
time{% if o_data.attlen is not none %}({{ o_data.attlen }}){% endif %} without time zone {% elif data.cltype == "time with time zone" %}
time{% if o_data.attlen is not none %}({{ o_data.attlen }}){% endif %} with time zone {% endif %}{% if data.hasSqrBracket %}[]{% endif %}
{#############################################################}
{# if only length changes, we need to give previous length to current type#}
{#############################################################}
{% elif data.attlen and o_data.cltype.startswith('time') %}
{% if o_data.cltype == "timestamp without time zone" %}
timestamp({{ data.attlen }}) without time zone {% elif o_data.cltype == "timestamp with time zone" %}
timestamp({{ data.attlen }}) with time zone {% elif o_data.cltype == "time without time zone" %}
time({{ data.attlen }}) without time zone {% elif o_data.cltype == "time with time zone" %}
time({{ data.attlen }}) with time zone {% endif %}{% if o_data.hasSqrBracket %}[]{% endif %}
{###### Need to check against separate time types - END ######}
{% elif (data.cltype and not data.cltype.startswith('time')) or not o_data.cltype.startswith('time') %}
{#############################################################}
{########## We will create SQL for other types here ##########}
{#############################################################}
{% if data.cltype %}{{ data.cltype }}{% elif o_data.typnspname != 'pg_catalog' %}{{conn|qtTypeIdent(o_data.typnspname, o_data.cltype)}}{% else %}{{conn|qtTypeIdent(o_data.cltype)}}{% endif %}{% if (data.attlen and data.attlen is not none) or (data_precision and data_precision is not none) or (old_length and old_length is not none and old_length|int >0) or (old_precision and old_precision is not none) %}
{% if data.attlen and data.attlen is not none %}
({{ data.attlen }}{% elif old_length and old_length is not none %}({{ old_length }}{% endif %}{% if data_precision and data_precision is not none %}
, {{ data_precision }}){% elif old_precision and old_precision is not none %}, {{ old_precision }}){% else %}){% endif %}
{% endif %}{% if o_data.hasSqrBracket %}[]{% endif %}
{% endif %}
{% endmacro %}
