|
| 1 | +{% import 'macros/functions/security.macros' as SECLABEL %} |
| 2 | +{% import 'macros/functions/privilege.macros' as PRIVILEGE %} |
| 3 | +{% import 'macros/functions/variable.macros' as VARIABLE %}{% if data %} |
| 4 | +{% set name = o_data.name %} |
| 5 | +{% set exclude_quoting = ['search_path'] %} |
| 6 | +{% set set_variables = [] %} |
| 7 | +{% if 'merged_variables' in data and data.merged_variables|length > 0 %} |
| 8 | +{% set set_variables = data.merged_variables %} |
| 9 | +{% elif 'variables' in o_data and o_data.variables|length > 0 %} |
| 10 | +{% set set_variables = o_data.variables %} |
| 11 | +{% endif %} |
| 12 | +{% if data.name %} |
| 13 | +{% if data.name != o_data.name %} |
| 14 | +ALTER FUNCTION {{ conn|qtIdent(o_data.pronamespace, o_data.name) }}({{ |
| 15 | +o_data.proargtypenames }}) |
| 16 | + RENAME TO {{ conn|qtIdent(data.name) }}; |
| 17 | +{% set name = data.name %} |
| 18 | +{% endif %} |
| 19 | +{% endif -%} |
| 20 | +{% if data.change_func %} |
| 21 | + |
| 22 | +CREATE OR REPLACE FUNCTION {{ conn|qtIdent(o_data.pronamespace, name) }}({% if data.arguments %} |
| 23 | +{% for p in data.arguments %}{% if p.argmode %}{{p.argmode}} {% endif %}{% if p.argname %}{{ conn|qtIdent(p.argname) }} {% endif %}{% if p.argtype %}{{ p.argtype }}{% endif %}{% if p.argdefval %} DEFAULT {{p.argdefval}}{% endif %} |
| 24 | +{% if not loop.last %},{% endif %} |
| 25 | +{% endfor %} |
| 26 | +{% endif -%} |
| 27 | +) |
| 28 | + RETURNS {% if 'prorettypename' in data %}{{ data.prorettypename }}{% else %}{{ o_data.prorettypename }}{% endif %} |
| 29 | + |
| 30 | +{% if 'lanname' in data %} |
| 31 | + LANGUAGE {{ data.lanname|qtLiteral(conn) }} {% else %} |
| 32 | + LANGUAGE {{ o_data.lanname|qtLiteral(conn) }} |
| 33 | + {% endif %}{% if 'provolatile' in data and data.provolatile %}{{ data.provolatile }} {% elif 'provolatile' not in data and o_data.provolatile %}{{ o_data.provolatile }}{% endif %} |
| 34 | +{% if ('proleakproof' in data and data.proleakproof) or ('proleakproof' not in data and o_data.proleakproof) %} LEAKPROOF{% elif 'proleakproof' in data and not data.proleakproof %} NOT LEAKPROOF{% endif %} |
| 35 | +{% if ('proisstrict' in data and data.proisstrict) or ('proisstrict' not in data and o_data.proisstrict) %} STRICT{% endif %} |
| 36 | +{% if ('prosecdef' in data and data.prosecdef) or ('prosecdef' not in data and o_data.prosecdef) %} SECURITY DEFINER{% endif %} |
| 37 | +{% if ('proiswindow' in data and data.proiswindow) or ('proiswindow' not in data and o_data.proiswindow) %} WINDOW{% endif %} |
| 38 | + |
| 39 | + {% if 'proparallel' in data and data.proparallel %}PARALLEL {{ data.proparallel }}{% elif 'proparallel' not in data and o_data.proparallel %}PARALLEL {{ o_data.proparallel }}{% endif %} |
| 40 | + |
| 41 | + {% if data.procost %}COST {{data.procost}}{% elif o_data.procost %}COST {{o_data.procost}}{% endif %}{% if data.prorows and data.prorows != '0' %} |
| 42 | + |
| 43 | + ROWS {{data.prorows}}{% elif data.prorows is not defined and o_data.prorows and o_data.prorows != '0' %} ROWS {{o_data.prorows}} {%endif %} |
| 44 | + |
| 45 | + {% if data.prosupportfunc %}SUPPORT {{ data.prosupportfunc }}{% elif data.prosupportfunc is not defined and o_data.prosupportfunc %}SUPPORT {{ o_data.prosupportfunc }}{% endif -%}{% if set_variables and set_variables|length > 0 %}{% for v in set_variables %} |
| 46 | + |
| 47 | + SET {{ conn|qtIdent(v.name) }}={% if v.name in exclude_quoting %}{{ v.value }}{% else %}{{ v.value|qtLiteral(conn) }}{% endif %}{% endfor -%} |
| 48 | + {% endif %} |
| 49 | + |
| 50 | +{% if data.is_pure_sql %}{{ data.prosrc }} |
| 51 | +{% else %} |
| 52 | +AS {% if (data.lanname == 'c' or o_data.lanname == 'c') and ('probin' in data or 'prosrc_c' in data) %} |
| 53 | +{% if 'probin' in data %}{{ data.probin|qtLiteral(conn) }}{% else %}{{ o_data.probin|qtLiteral(conn) }}{% endif %}, {% if 'prosrc_c' in data %}{{ data.prosrc_c|qtLiteral(conn) }}{% else %}{{ o_data.prosrc_c|qtLiteral(conn) }}{% endif %}{% elif 'prosrc' in data %} |
| 54 | +$BODY${{ data.prosrc }}$BODY${% elif o_data.lanname == 'c' %} |
| 55 | +{{ o_data.probin|qtLiteral(conn) }}, {{ o_data.prosrc_c|qtLiteral(conn) }}{% else %} |
| 56 | +$BODY${{ o_data.prosrc }}$BODY${% endif -%}; |
| 57 | +{% endif -%} |
| 58 | +{% endif -%} |
| 59 | +{% if data.funcowner %} |
| 60 | + |
| 61 | +ALTER FUNCTION {{ conn|qtIdent(o_data.pronamespace, name) }}({{o_data.proargtypenames }}) |
| 62 | + OWNER TO {{ conn|qtIdent(data.funcowner) }}; |
| 63 | +{% endif -%} |
| 64 | +{# The SQL generated below will change priviledges #} |
| 65 | +{% if data.acl %} |
| 66 | +{% if 'deleted' in data.acl %} |
| 67 | +{% for priv in data.acl.deleted %} |
| 68 | + |
| 69 | +{{ PRIVILEGE.UNSETALL(conn, 'FUNCTION', priv.grantee, name, o_data.pronamespace, o_data.proargtypenames) }} |
| 70 | +{% endfor %} |
| 71 | +{% endif -%} |
| 72 | +{% if 'changed' in data.acl %} |
| 73 | +{% for priv in data.acl.changed %} |
| 74 | + |
| 75 | +{% if priv.grantee != priv.old_grantee %} |
| 76 | +{{ PRIVILEGE.UNSETALL(conn, 'FUNCTION', priv.old_grantee, name, o_data.pronamespace, o_data.proargtypenames) }} |
| 77 | +{% else %} |
| 78 | +{{ PRIVILEGE.UNSETALL(conn, 'FUNCTION', priv.grantee, name, o_data.pronamespace, o_data.proargtypenames) }} |
| 79 | +{% endif %} |
| 80 | + |
| 81 | +{{ PRIVILEGE.SET(conn, 'FUNCTION', priv.grantee, name, priv.without_grant, priv.with_grant, o_data.pronamespace, o_data.proargtypenames) }} |
| 82 | +{% endfor %} |
| 83 | +{% endif -%} |
| 84 | +{% if 'added' in data.acl %} |
| 85 | +{% for priv in data.acl.added %} |
| 86 | + |
| 87 | +{{ PRIVILEGE.SET(conn, 'FUNCTION', priv.grantee, name, priv.without_grant, priv.with_grant, o_data.pronamespace, o_data.proargtypenames) }} |
| 88 | +{% endfor %}{% endif -%} |
| 89 | +{% endif -%} |
| 90 | +{% if data.change_func == False %} |
| 91 | +{% if data.variables %} |
| 92 | +{% if 'deleted' in data.variables and data.variables.deleted|length > 0 %} |
| 93 | + |
| 94 | +{{ VARIABLE.UNSET(conn, 'FUNCTION', name, data.variables.deleted, o_data.pronamespace, o_data.proargtypenames) }} |
| 95 | +{% endif -%} |
| 96 | +{% if 'merged_variables' in data and data.merged_variables|length > 0 %} |
| 97 | + |
| 98 | +{{ VARIABLE.SET(conn, 'FUNCTION', name, data.merged_variables, o_data.pronamespace, o_data.proargtypenames) }} |
| 99 | +{% endif -%} |
| 100 | +{% endif -%} |
| 101 | +{% endif -%} |
| 102 | +{% set seclabels = data.seclabels %} |
| 103 | +{% if 'deleted' in seclabels and seclabels.deleted|length > 0 %} |
| 104 | +{% for r in seclabels.deleted %} |
| 105 | + |
| 106 | +{{ SECLABEL.UNSET(conn, 'FUNCTION', name, r.provider, o_data.pronamespace, o_data.proargtypenames) }} |
| 107 | +{% endfor %} |
| 108 | +{% endif -%} |
| 109 | +{% if 'added' in seclabels and seclabels.added|length > 0 %} |
| 110 | +{% for r in seclabels.added %} |
| 111 | + |
| 112 | +{{ SECLABEL.SET(conn, 'FUNCTION', name, r.provider, r.label, o_data.pronamespace, o_data.proargtypenames) }} |
| 113 | +{% endfor %} |
| 114 | +{% endif -%} |
| 115 | +{% if 'changed' in seclabels and seclabels.changed|length > 0 %} |
| 116 | +{% for r in seclabels.changed %} |
| 117 | + |
| 118 | +{{ SECLABEL.SET(conn, 'FUNCTION', name, r.provider, r.label, o_data.pronamespace, o_data.proargtypenames) }} |
| 119 | +{% endfor %} |
| 120 | +{% endif -%} |
| 121 | +{% if data.description is defined and data.description != o_data.description%} |
| 122 | + |
| 123 | +COMMENT ON FUNCTION {{ conn|qtIdent(o_data.pronamespace, name) }}({{o_data.proargtypenames }}) |
| 124 | + IS {{ data.description|qtLiteral(conn) }}; |
| 125 | +{% endif -%} |
| 126 | + |
| 127 | +{% if data.pronamespace %} |
| 128 | + |
| 129 | +ALTER FUNCTION {{ conn|qtIdent(o_data.pronamespace, name) }}({{o_data.proargtypenames }}) |
| 130 | + SET SCHEMA {{ conn|qtIdent(data.pronamespace) }}; |
| 131 | +{% endif -%} |
| 132 | + |
| 133 | +{% set old_exts = (o_data.dependsonextensions or []) | list %} |
| 134 | +{% set new_exts = data.dependsonextensions if 'dependsonextensions' in data else None %} |
| 135 | + |
| 136 | +{% if new_exts is not none and old_exts != new_exts %} |
| 137 | +{% for ext in (old_exts + new_exts) | unique %} |
| 138 | +{% if ext in new_exts and ext not in old_exts %} |
| 139 | +ALTER FUNCTION {{ conn|qtIdent(o_data.pronamespace, name) }}({{ o_data.proargtypenames }}) |
| 140 | + DEPENDS ON EXTENSION {{ conn|qtIdent(ext) }}; |
| 141 | +{% elif ext in old_exts and ext not in new_exts %} |
| 142 | +ALTER FUNCTION {{ conn|qtIdent(o_data.pronamespace, name) }}({{ o_data.proargtypenames }}) |
| 143 | + NO DEPENDS ON EXTENSION {{ conn|qtIdent(ext) }}; |
| 144 | +{% endif %} |
| 145 | +{% endfor %} |
| 146 | +{% endif %} |
| 147 | + |
| 148 | +{% endif %} |
0 commit comments