{% macro sequence_next_value(sequence_name, check_disable=false) %}
{# macro checks if a function for this sequence exists,
if it does, the function is used
if not, the standard command is used  
check can be disabled by passing true in the 2nd paramater
#}
{% if execute %}
    {% if not check_disable %}
        {% set get_func %}
            select count(*)
            from all_objects
            where object_type = 'FUNCTION'
            and status = 'VALID'
            and owner || '.' || object_name = upper('{{ sequence_name }}_fnc')
        {% endset %}

        {% set result = run_query(get_func) %}
        {% set fnc_count = result.columns[0].values()[0] %}
    {% else %}
        {% set fnc_count = 0 %}
    {% endif %}

    {% if fnc_count > 0 %}
        {% set nextvalue = sequence_name ~ '_fnc()' %}
    {% else %}
        {% set nextvalue = sequence_name ~ '.nextval'%}
    {% endif %}

    {{ return(nextvalue) }}

{% endif %}

{% endmacro %}