Subject: [pgAdmin4][Patch]: RM#1293 - SQL pane is not displaying GRANT queries in Function, Procedure and Trigger node

From: Surinder Kumar <[email-masked]>
Date: Thu, 2 Jun 2016 19:25:39 +0530

Hi,

Please find attached patch for RM#1293.
Add support to display GRANT privilege query in SQL Pane.

Please review.

Thanks,
Surinder Kumar

-- 
Sent via pgadmin-hackers mailing list ([email-masked])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgadmin-hackers

---

From: Surinder Kumar <[email-masked]>
Date: Thu, 2 Jun 2016 19:32:31 +0530

Please ignore this patch. I will send another patch.

---

From: Surinder Kumar <[email-masked]>
Date: Fri, 3 Jun 2016 15:31:03 +0530

Hi,

Please find attached patch for:
1. RM#1293 - SQL pane is not displaying GRANT queries in functions node
2. RM#1294 - Comments are not visible in sql pane for trigger functions and
functions

Explanation:
Previously we we using 'get_defintion.sql' template to generate SQL for SQL
pane for functions, procedure & trigger functions node.
but GRANT statements and COMMENTS changes were missing. In order to fix
that, now we have used both 'create.sql' and 'get_definition.sql' templates
to generate full SQL query
for SQL pane.

Please review.

---

From: Surinder Kumar <[email-masked]>
Date: Mon, 6 Jun 2016 19:17:26 +0530

Hi,

Please find updated patch:
*Issue*: Code changes are not consistent with the present code in
functions/__init__.py, like don't rename variable func_def to
func_defintion until it is required.
Review comment given by Ashesh personally.

Please review.

On Fri, Jun 3, 2016 at 3:31 PM, Surinder Kumar <
[email-masked]> wrote:

> 2. RM#1294 - Comments are not visible in sql pane for trigger functions
> and functions
>
> Explanation:
> Previously we we using 'get_defintion.sql' template to generate SQL for
> SQL pane for functions, procedure & trigger functions node.

---

From: Harshal Dhumal <[email-masked]>
Date: Fri, 22 Jul 2016 13:26:06 +0530

Hi,

PFA rebased patch for RM1293 and RM1294.

-- 
*Harshal Dhumal*
*Software Engineer*

EnterpriseDB India: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

>>>> Add support to display GRANT privilege query in SQL Pane.
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/__init__.py
index 7d5d129..75ba022 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/__init__.py
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/__init__.py
@@ -889,10 +889,31 @@ class FunctionView(PGChildNodeView, DataTypeReader):
             scid: Schema Id
             fnid: Function Id
         """
+        resp_data = self._fetch_properties(gid, sid, did, scid, fnid)
+        # Fetch the function definition.
+        args = ''
+        cnt = 1
+        if 'arguments' in resp_data:
+            for a in resp_data['arguments']:
+                if (('argmode' in a and a['argmode'] != 'OUT' and
+                    a['argmode'] is not None
+                      ) or 'argnode' not in a):
+                    if 'argmode' in a:
+                        args += a['argmode'] + " "
+                    if 'argname' in a and a['argname'] != ''\
+                            and a['argname'] is not None:
+                        args += self.qtIdent(
+                            self.conn, a['argname']) + " "
+                    if 'argtype' in a:
+                        args += a['argtype']
+                    if cnt < len(resp_data['arguments']):
+                        args += ', '
+                cnt += 1
+
+        resp_data['func_args'] = args.strip(' ')
 
         if self.node_type == 'procedure':
             object_type = 'procedure'
-            resp_data = self._fetch_properties(gid, sid, did, scid, fnid)
 
             # Get SQL to create Function
             status, func_def = self._get_sql(gid, sid, did, scid, resp_data,
@@ -901,16 +922,38 @@ class FunctionView(PGChildNodeView, DataTypeReader):
                 return internal_server_error(errormsg=func_def)
 
             name = resp_data['pronamespace'] + "." + resp_data['name_with_args']
+
+            # Create mode
+            func_def = render_template("/".join([self.sql_template_path,
+                                                'create.sql']),
+                                       data=resp_data, query_type="create")
         else:
             object_type = 'function'
-            # Fetch the function definition.
+
+            # Get Schema Name from its OID.
+            if 'pronamespace' in resp_data:
+                resp_data['pronamespace'] = self._get_schema(resp_data[
+                    'pronamespace'])
+
+            # Parse privilege data
+            if 'acl' in resp_data:
+                resp_data['acl'] = parse_priv_to_db(resp_data['acl'], ['X'])
+
+            # Create mode
             SQL = render_template("/".join([self.sql_template_path,
-                                            'get_definition.sql']), fnid=fnid, scid=scid)
+                                            'get_definition.sql']
+                                           ), data=resp_data,
+                                  fnid=fnid, scid=scid)
+
             status, res = self.conn.execute_2darray(SQL)
             if not status:
                 return internal_server_error(errormsg=res)
 
             func_def, name = res['rows'][0]
+            # Create mode
+            func_def = render_template("/".join([self.sql_template_path,
+                                                 'create.sql']),
+                                       data=resp_data, query_type="create")
 
         sql_header = """-- {0}: {1}
 
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/pg/sql/9.1_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/pg/sql/9.1_plus/create.sql
index 62e61d3..efd7775 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/pg/sql/9.1_plus/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/pg/sql/9.1_plus/create.sql
@@ -3,22 +3,33 @@
 {% import 'macros/functions/variable.macros' as VARIABLE %}
 {% set is_columns = [] %}
 {% if data %}
-CREATE FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}({% if data.args %}
-{% for p in data.args %}{% if p.argmode %}{{p.argmode}} {% endif %}{% if p.argname %}{{ conn|qtIdent(p.argname)}} {% endif %}{% if p.argtype %}{{ conn|qtTypeIdent(p.argtype) }}{% endif %}{% if p.argdefval %} DEFAULT {{p.argdefval}}{% endif %}
+CREATE{% if query_type is defined %}{{' OR REPLACE'}}{% endif %} FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}({% if data.args %}
+{% for p in data.args %}{% if p.argmode %}{{p.argmode}} {% endif %}{% if p.argname %}{{ conn|qtIdent(p.argname)}}{% endif %}{% if p.argtype %}{{ conn|qtIdent(p.argtype) }}{% endif %}{% if p.argdefval %} DEFAULT {{p.argdefval}}{% endif %}
 {% if not loop.last %},{% endif %}
 {% endfor %}
 {% endif -%}
-    )
-    RETURNS{% if data.proretset %} SETOF{% endif %} {{ conn|qtTypeIdent(data.prorettypename) }}
-    LANGUAGE {{ data.lanname|qtLiteral }}
-    {% if data.provolatile %}{{ data.provolatile }} {% endif %}{% if data.proisstrict %}STRICT {% endif %}{% if data.prosecdef %}SECURITY DEFINER {% endif %}{% if data.proiswindow %}WINDOW{% endif -%}{% if data.procost %}
+{% if data.func_args %}
+{% set func_args = data.func_args.split(',') %}
 
-    COST {{data.procost}}{% endif %}{% if data.prorows %}
+{% for f in func_args %}
+    {{ f|trim }}{% if not loop.last %},
+{% endif %}
+{% endfor %}
 
+{% endif %}
+)
+    RETURNS{% if data.proretset %} SETOF{% endif %} {{ conn|qtTypeIdent(data.prorettypename) }}
+    LANGUAGE {{ data.lanname|qtLiteral }}
+{% if data.procost %}
+    COST {{data.procost}}
+{% endif %}
+{% if query_type != 'create' %}
+    {% if data.provolatile %}{{ data.provolatile }} {% endif %}{% if data.proisstrict %}STRICT {% endif %}{% if data.prosecdef %}SECURITY DEFINER {% endif %}{% if data.proiswindow %}WINDOW{% endif -%}
+{% if data.prorows %}
     ROWS {{data.prorows}}{% endif -%}{% if data.variables %}{% for v in data.variables %}
 
     SET {{ conn|qtIdent(v.name) }}={{ v.value|qtLiteral }}{% endfor %}
-{% endif %}
+{% endif %}{% endif %}
 
 AS {% if data.lanname == 'c' %}
 {{ data.probin|qtLiteral }}, {{ data.prosrc_c|qtLiteral }}
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/pg/sql/9.2_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/pg/sql/9.2_plus/create.sql
index 6976a75..63585ad 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/pg/sql/9.2_plus/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/pg/sql/9.2_plus/create.sql
@@ -3,25 +3,37 @@
 {% import 'macros/functions/variable.macros' as VARIABLE %}
 {% set is_columns = [] %}
 {% if data %}
-CREATE FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}({% if data.args %}
-{% for p in data.args %}{% if p.argmode %}{{p.argmode}} {% endif %}{% if p.argname %}{{ conn|qtIdent(p.argname)}} {% endif %}{% if p.argtype %}{{ conn|qtTypeIdent(p.argtype) }}{% endif %}{% if p.argdefval %} DEFAULT {{p.argdefval}}{% endif %}
+CREATE{% if query_type is defined %}{{' OR REPLACE'}}{% endif %} FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}({% if data.args %}
+{% for p in data.args %}{% if p.argmode %}{{p.argmode}} {% endif %}{% if p.argname %}{{ conn|qtIdent(p.argname)}}{% endif %}{% if p.argtype %}{{ conn|qtIdent(p.argtype) }}{% endif %}{% if p.argdefval %} DEFAULT {{p.argdefval}}{% endif %}
 {% if not loop.last %},{% endif %}
 {% endfor %}
 {% endif -%}
-    )
+{% if data.func_args %}
+{% set func_args = data.func_args.split(',') %}
+
+{% for f in func_args %}
+    {{ f|trim }}{% if not loop.last %},
+{% endif %}
+{% endfor %}
+
+{% endif %}
+)
     RETURNS{% if data.proretset %} SETOF{% endif %} {{ conn|qtTypeIdent(data.prorettypename) }}
     LANGUAGE {{ data.lanname|qtLiteral }}
+{% if data.procost %}
+    COST {{data.procost}}
+{% endif %}
+{% if query_type != 'create' %}
     {% if data.provolatile %}{{ data.provolatile }} {% endif %}{% if data.proleakproof %}LEAKPROOF {% else %}NOT LEAKPROOF {% endif %}
 {% if data.proisstrict %}STRICT {% endif %}
 {% if data.prosecdef %}SECURITY DEFINER {% endif %}
-{% if data.proiswindow %}WINDOW{% endif %}{% if data.procost %}
-
-    COST {{data.procost}}{% endif %}{% if data.prorows %}
+{% if data.proiswindow %}WINDOW{% endif %}
+{% if data.prorows %}
 
     ROWS {{data.prorows}}{% endif -%}{% if data.variables %}{% for v in data.variables %}
 
     SET {{ conn|qtIdent(v.name) }}={{ v.value|qtLiteral }}{% endfor %}
-{% endif %}
+{% endif %}{% endif %}
 
 AS {% if data.lanname == 'c' %}
 {{ data.probin|qtLiteral }}, {{ data.prosrc_c|qtLiteral }}
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/pg/sql/9.5_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/pg/sql/9.5_plus/create.sql
index 102dc7a..45a9c61 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/pg/sql/9.5_plus/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/pg/sql/9.5_plus/create.sql
@@ -3,26 +3,37 @@
 {% import 'macros/functions/variable.macros' as VARIABLE %}
 {% set is_columns = [] %}
 {% if data %}
-CREATE FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}({% if data
-.args %}
+CREATE{% if query_type is defined %}{{' OR REPLACE'}}{% endif %} FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}({% if data.args %}
 {% for p in data.args %}{% if p.argmode %}{{p.argmode}} {% endif %}{% if p.argname %}{{ conn|qtIdent(p.argname) }} {% endif %}{% if p.argtype %}{{ conn|qtTypeIdent(p.argtype) }}{% endif %}{% if p.argdefval %} DEFAULT {{p.argdefval}}{% endif %}
 {% if not loop.last %},{% endif %}
 {% endfor %}
 {% endif -%}
-    )
+{% if data.func_args %}
+{% set func_args = data.func_args.split(',') %}
+
+{% for f in func_args %}
+    {{ f|trim }}{% if not loop.last %},
+{% endif %}
+{% endfor %}
+
+{% endif %}
+)
     RETURNS{% if data.proretset %} SETOF{% endif %} {{ conn|qtTypeIdent(data.prorettypename) }}
     LANGUAGE {{ data.lanname|qtLiteral }}
+{% if data.procost %}
+    COST {{data.procost}}
+{% endif %}
+{% if query_type != 'create' %}
     {% if data.provolatile %}{{ data.provolatile }} {% endif %}{% if data.proleakproof %}LEAKPROOF {% else %}NOT LEAKPROOF {% endif %}
 {% if data.proisstrict %}STRICT {% endif %}
 {% if data.prosecdef %}SECURITY DEFINER {% endif %}
-{% if data.proiswindow %}WINDOW{% endif %}{% if data.procost %}
-
-    COST {{data.procost}}{% endif %}{% if data.prorows %}
+{% if data.proiswindow %}WINDOW{% endif %}
+{% if data.prorows %}
 
     ROWS {{data.prorows}}{% endif -%}{% if data.variables %}{% for v in data.variables %}
 
     SET {{ conn|qtIdent(v.name) }}={{ v.value|qtLiteral }}{% endfor %}
-{% endif %}
+{% endif %}{% endif %}
 
 AS {% if data.lanname == 'c' %}
 {{ data.probin|qtLiteral }}, {{ data.prosrc_c|qtLiteral }}
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/ppas/sql/9.1_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/ppas/sql/9.1_plus/create.sql
index 62e61d3..efd7775 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/ppas/sql/9.1_plus/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/ppas/sql/9.1_plus/create.sql
@@ -3,22 +3,33 @@
 {% import 'macros/functions/variable.macros' as VARIABLE %}
 {% set is_columns = [] %}
 {% if data %}
-CREATE FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}({% if data.args %}
-{% for p in data.args %}{% if p.argmode %}{{p.argmode}} {% endif %}{% if p.argname %}{{ conn|qtIdent(p.argname)}} {% endif %}{% if p.argtype %}{{ conn|qtTypeIdent(p.argtype) }}{% endif %}{% if p.argdefval %} DEFAULT {{p.argdefval}}{% endif %}
+CREATE{% if query_type is defined %}{{' OR REPLACE'}}{% endif %} FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}({% if data.args %}
+{% for p in data.args %}{% if p.argmode %}{{p.argmode}} {% endif %}{% if p.argname %}{{ conn|qtIdent(p.argname)}}{% endif %}{% if p.argtype %}{{ conn|qtIdent(p.argtype) }}{% endif %}{% if p.argdefval %} DEFAULT {{p.argdefval}}{% endif %}
 {% if not loop.last %},{% endif %}
 {% endfor %}
 {% endif -%}
-    )
-    RETURNS{% if data.proretset %} SETOF{% endif %} {{ conn|qtTypeIdent(data.prorettypename) }}
-    LANGUAGE {{ data.lanname|qtLiteral }}
-    {% if data.provolatile %}{{ data.provolatile }} {% endif %}{% if data.proisstrict %}STRICT {% endif %}{% if data.prosecdef %}SECURITY DEFINER {% endif %}{% if data.proiswindow %}WINDOW{% endif -%}{% if data.procost %}
+{% if data.func_args %}
+{% set func_args = data.func_args.split(',') %}
 
-    COST {{data.procost}}{% endif %}{% if data.prorows %}
+{% for f in func_args %}
+    {{ f|trim }}{% if not loop.last %},
+{% endif %}
+{% endfor %}
 
+{% endif %}
+)
+    RETURNS{% if data.proretset %} SETOF{% endif %} {{ conn|qtTypeIdent(data.prorettypename) }}
+    LANGUAGE {{ data.lanname|qtLiteral }}
+{% if data.procost %}
+    COST {{data.procost}}
+{% endif %}
+{% if query_type != 'create' %}
+    {% if data.provolatile %}{{ data.provolatile }} {% endif %}{% if data.proisstrict %}STRICT {% endif %}{% if data.prosecdef %}SECURITY DEFINER {% endif %}{% if data.proiswindow %}WINDOW{% endif -%}
+{% if data.prorows %}
     ROWS {{data.prorows}}{% endif -%}{% if data.variables %}{% for v in data.variables %}
 
     SET {{ conn|qtIdent(v.name) }}={{ v.value|qtLiteral }}{% endfor %}
-{% endif %}
+{% endif %}{% endif %}
 
 AS {% if data.lanname == 'c' %}
 {{ data.probin|qtLiteral }}, {{ data.prosrc_c|qtLiteral }}
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/ppas/sql/9.2_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/ppas/sql/9.2_plus/create.sql
index 9514344..516e6d4 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/ppas/sql/9.2_plus/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/ppas/sql/9.2_plus/create.sql
@@ -3,25 +3,37 @@
 {% import 'macros/functions/variable.macros' as VARIABLE %}
 {% set is_columns = [] %}
 {% if data %}
-CREATE FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}({% if data.args %}
-{% for p in data.args %}{% if p.argmode %}{{p.argmode}} {% endif %}{% if p.argname %}{{ conn|qtIdent(p.argname)}} {% endif %}{% if p.argtype %}{{ conn|qtTypeIdent(p.argtype) }}{% endif %}{% if p.argdefval %} DEFAULT {{p.argdefval}}{% endif %}
+CREATE{% if query_type is defined %}{{' OR REPLACE'}}{% endif %} FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}({% if data.args %}
+{% for p in data.args %}{% if p.argmode %}{{p.argmode}} {% endif %}{% if p.argname %}{{ conn|qtIdent(p.argname)}}{% endif %}{% if p.argtype %}{{ conn|qtIdent(p.argtype) }}{% endif %}{% if p.argdefval %} DEFAULT {{p.argdefval}}{% endif %}
 {% if not loop.last %},{% endif %}
 {% endfor %}
 {% endif -%}
-    )
+{% if data.func_args %}
+{% set func_args = data.func_args.split(',') %}
+
+{% for f in func_args %}
+    {{ f|trim }}{% if not loop.last %},
+{% endif %}
+{% endfor %}
+
+{% endif %}
+)
     RETURNS{% if data.proretset %} SETOF{% endif %} {{ conn|qtTypeIdent(data.prorettypename) }}
     LANGUAGE {{ data.lanname|qtLiteral }}
+{% if data.procost %}
+    COST {{data.procost}}
+{% endif %}
+{% if query_type != 'create' %}
     {% if data.provolatile %}{{ data.provolatile }} {% endif %}{% if data.proleakproof %} LEAKPROOF {% else %} NOT LEAKPROOF {% endif %}
 {% if data.proisstrict %}STRICT {% endif %}
 {% if data.prosecdef %}SECURITY DEFINER {% endif %}
-{% if data.proiswindow %}WINDOW{% endif %}{% if data.procost %}
-
-    COST {{data.procost}}{% endif %}{% if data.prorows %}
+{% if data.proiswindow %}WINDOW{% endif %}
+{% if data.prorows %}
 
     ROWS {{data.prorows}}{% endif -%}{% if data.variables %}{% for v in data.variables %}
 
     SET {{ conn|qtIdent(v.name) }}={{ v.value|qtLiteral }}{% endfor %}
-{% endif %}
+{% endif %}{% endif %}
 
 AS {% if data.lanname == 'c' %}
 {{ data.probin|qtLiteral }}, {{ data.prosrc_c|qtLiteral }}
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/ppas/sql/9.5_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/ppas/sql/9.5_plus/create.sql
index 102dc7a..b5b7176 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/ppas/sql/9.5_plus/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/function/ppas/sql/9.5_plus/create.sql
@@ -3,8 +3,7 @@
 {% import 'macros/functions/variable.macros' as VARIABLE %}
 {% set is_columns = [] %}
 {% if data %}
-CREATE FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}({% if data
-.args %}
+CREATE{% if query_type is defined %}{{' OR REPLACE'}}{% endif %} FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}({% if data.args %}
 {% for p in data.args %}{% if p.argmode %}{{p.argmode}} {% endif %}{% if p.argname %}{{ conn|qtIdent(p.argname) }} {% endif %}{% if p.argtype %}{{ conn|qtTypeIdent(p.argtype) }}{% endif %}{% if p.argdefval %} DEFAULT {{p.argdefval}}{% endif %}
 {% if not loop.last %},{% endif %}
 {% endfor %}
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/procedure/ppas/sql/9.1_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/procedure/ppas/sql/9.1_plus/create.sql
index a971b55..0a8cb20 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/procedure/ppas/sql/9.1_plus/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/procedure/ppas/sql/9.1_plus/create.sql
@@ -3,11 +3,12 @@
 {% import 'macros/functions/variable.macros' as VARIABLE %}
 {% set is_columns = [] %}
 {% if data %}
-CREATE OR REPLACE PROCEDURE {{ conn|qtIdent(data.pronamespace, data.name) }}{% if data.args %}(
-{% for p in data.args %}{% if p.argmode %}{{p.argmode}} {% endif %}{% if p.argname %}{{ conn|qtIdent(p.argname)}} {% endif %}{% if p.argtype %}{{ conn|qtTypeIdent(p.argtype) }}{% endif %}{% if p.argdefval %} DEFAULT {{p.argdefval}}{% endif %}
-{% if not loop.last %},{% endif %}
-{% endfor %})
-{% endif %}
+CREATE OR REPLACE PROCEDURE {{ conn|qtIdent(data.pronamespace, data.name) }}{% if data.args is defined or data.func_args is defined %}
+({% for p in data.args %}{% if p.argmode %}{{p.argmode}} {% endif %}{% if p.argname %}{{ conn|qtIdent(p.argname)}} {% endif %}{% if p.argtype %}{{ conn|qtIdent(p.argtype) }}{% endif %}{% if p.argdefval %} DEFAULT {{p.argdefval}}{% endif %}
+{% if not loop.last %}, {% endif %}
+{% endfor -%}
+{% if data.func_args %}{{ data.func_args }}{% endif %}
+){% endif %}
 
 AS
 {{ data.prosrc }};
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/procedure/ppas/sql/9.2_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/procedure/ppas/sql/9.2_plus/create.sql
index 2396517..073ce1d 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/procedure/ppas/sql/9.2_plus/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/procedure/ppas/sql/9.2_plus/create.sql
@@ -3,10 +3,12 @@
 {% import 'macros/functions/variable.macros' as VARIABLE %}
 {% set is_columns = [] %}
 {% if data %}
-CREATE OR REPLACE PROCEDURE {{ conn|qtIdent(data.pronamespace, data.name) }}{% if data.args %}
-({% for p in data.args %}{% if p.argmode %}{{p.argmode}} {% endif %}{% if p.argname %} {{ conn|qtIdent(p.argname)}} {% endif %}{% if p.argtype %}{{ conn|qtTypeIdent(p.argtype) }}{% endif %}{% if p.argdefval %} DEFAULT {{p.argdefval}}{% endif %}
-{% if not loop.last %}, {% endif %}
-{% endfor -%}){% endif %}
+CREATE OR REPLACE PROCEDURE {{ conn|qtIdent(data.pronamespace, data.name) }}{% if data.args is defined or data.func_args is defined %}
+({% for p in data.args %}{% if p.argmode %}{{p.argmode}} {% endif %}{% if p.argname %}{{ conn|qtIdent(p.argname)}} {% endif %}{% if p.argtype %}{{ conn|qtIdent(p.argtype) }}{% endif %}{% if p.argdefval %} DEFAULT {{p.argdefval}}{% endif %}
+ {% if not loop.last %}, {% endif %}
+{% endfor -%}
+{% if data.func_args %}{{ data.func_args }}{% endif %}
+){% endif %}
 
 AS
 {{ data.prosrc }};
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/procedure/ppas/sql/9.5_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/procedure/ppas/sql/9.5_plus/create.sql
index c5f0084..f1a40af 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/procedure/ppas/sql/9.5_plus/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/procedure/ppas/sql/9.5_plus/create.sql
@@ -3,10 +3,13 @@
 {% import 'macros/functions/variable.macros' as VARIABLE %}
 {% set is_columns = [] %}
 {% if data %}
-CREATE OR REPLACE PROCEDURE {{ conn|qtIdent(data.pronamespace, data.name) }}{% if data.args %}
+CREATE OR REPLACE PROCEDURE {{ conn|qtIdent(data.pronamespace, data.name) }}{% if data.args is defined or data.func_args is defined %}
 ({% for p in data.args %}{% if p.argmode %}{{p.argmode}} {% endif %}{% if p.argname %}{{ conn|qtIdent(p.argname)}} {% endif %}{% if p.argtype %}{{ conn|qtTypeIdent(p.argtype) }}{% endif %}{% if p.argdefval %} DEFAULT {{p.argdefval}}{% endif %}
 {% if not loop.last %}, {% endif %}
-{% endfor -%}){% endif %}
+{% endfor -%}
+{% if data.func_args %}{{ data.func_args }}{% endif %}
+){% endif %}
+{% if query_type != 'create' %}
 
     {{ data.provolatile }}{% if data.proleakproof %} LEAKPROOF {% elif not data.proleakproof %} NOT LEAKPROOF {% endif %}
 {% if data.proisstrict %}STRICT {% endif %}
@@ -17,7 +20,7 @@ CREATE OR REPLACE PROCEDURE {{ conn|qtIdent(data.pronamespace, data.name) }}{% i
     ROWS {{data.prorows}}{% endif -%}{% if data.variables %}{% for v in data.variables %}
 
     SET {{ conn|qtIdent(v.name) }}={{ v.value|qtLiteral }}{% endfor -%}
-{% endif %}
+{% endif %}{% endif %}
 
 AS
 {{ data.prosrc }};
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/pg/sql/9.1_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/pg/sql/9.1_plus/create.sql
index 8e51a49..6e9fd78 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/pg/sql/9.1_plus/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/pg/sql/9.1_plus/create.sql
@@ -6,16 +6,19 @@
 CREATE FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}()
     RETURNS{% if data.proretset %} SETOF{% endif %} {{ conn|qtTypeIdent(data.prorettypename) }}
     LANGUAGE {{ data.lanname|qtLiteral }}
-    {% if data.provolatile %}{{ data.provolatile }} {% endif %}{% if data.proisstrict %}STRICT {% endif %}{% if data.prosecdef %}SECURITY DEFINER {% endif %}{% if data.proiswindow %}WINDOW{% endif -%}{% if data.procost %}
-
-    COST {{data.procost}}{% endif %}{% if data.prorows %}
+{% if data.procost %}
+    COST {{data.procost}}
+{% endif %}
+{% if query_type != 'create' %}
+    {% if data.provolatile %}{{ data.provolatile }} {% endif %}{% if data.proisstrict %}STRICT {% endif %}{% if data.prosecdef %}SECURITY DEFINER {% endif %}{% if data.proiswindow %}WINDOW{% endif -%}
+{% if data.prorows %}
 
     ROWS {{data.prorows}}{% endif -%}{% if data.variables %}{% for v in data.variables %}
 
     SET {{ conn|qtIdent(v.name) }}={{ v.value|qtLiteral }}{% endfor %}
 {% endif %}
 
-AS {% if data.lanname == 'c' %}
+AS {% endif %}{% if data.lanname == 'c' %}
 {{ data.probin|qtLiteral }}, {{ data.prosrc_c|qtLiteral }}
 {% else %}
 $BODY$
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/pg/sql/9.2_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/pg/sql/9.2_plus/create.sql
index 149c204..7ca4bf1 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/pg/sql/9.2_plus/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/pg/sql/9.2_plus/create.sql
@@ -6,6 +6,10 @@
 CREATE FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}()
     RETURNS{% if data.proretset %} SETOF{% endif %} {{ conn|qtTypeIdent(data.prorettypename) }}
     LANGUAGE {{ data.lanname|qtLiteral }}
+{% if data.procost %}
+    COST {{data.procost}}
+{% endif %}
+{% if query_type != 'create' %}
     {% if data.provolatile %}{{ data.provolatile }} {% endif %}{% if data.proleakproof %}LEAKPROOF {% else %}NOT LEAKPROOF {% endif %}
 {% if data.proisstrict %}STRICT {% endif %}
 {% if data.prosecdef %}SECURITY DEFINER {% endif %}
@@ -18,7 +22,7 @@ CREATE FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}()
     SET {{ conn|qtIdent(v.name) }}={{ v.value|qtLiteral }}{% endfor %}
 {% endif %}
 
-AS {% if data.lanname == 'c' %}
+AS {% endif %}{% if data.lanname == 'c' %}
 {{ data.probin|qtLiteral }}, {{ data.prosrc_c|qtLiteral }}
 {% else %}
 $BODY$
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/pg/sql/9.5_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/pg/sql/9.5_plus/create.sql
index fa36ad8..5445855 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/pg/sql/9.5_plus/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/pg/sql/9.5_plus/create.sql
@@ -3,22 +3,25 @@
 {% import 'macros/functions/variable.macros' as VARIABLE %}
 {% set is_columns = [] %}
 {% if data %}
-CREATE FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}()
+CREATE FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}({% if data.proargnames %}{{data.proargnames}}{% endif %})
     RETURNS{% if data.proretset %} SETOF{% endif %} {{ conn|qtTypeIdent(data.prorettypename) }}
     LANGUAGE {{ data.lanname|qtLiteral }}
+{% if data.procost %}
+    COST {{data.procost}}
+{% endif %}
+{% if query_type != 'create' %}
     {% if data.provolatile %}{{ data.provolatile }} {% endif %}{% if data.proleakproof %}LEAKPROOF {% else %}NOT LEAKPROOF {% endif %}
 {% if data.proisstrict %}STRICT {% endif %}
 {% if data.prosecdef %}SECURITY DEFINER {% endif %}
-{% if data.proiswindow %}WINDOW{% endif %}{% if data.procost %}
-
-    COST {{data.procost}}{% endif %}{% if data.prorows %}
+{% if data.proiswindow %}WINDOW{% endif %}
+{% if data.prorows %}
 
     ROWS {{data.prorows}}{% endif -%}{% if data.variables %}{% for v in data.variables %}
 
     SET {{ conn|qtIdent(v.name) }}={{ v.value|qtLiteral }}{% endfor %}
 {% endif %}
 
-AS {% if data.lanname == 'c' %}
+AS {% endif %}{% if data.lanname == 'c' %}
 {{ data.probin|qtLiteral }}, {{ data.prosrc_c|qtLiteral }}
 {% else %}
 $BODY$
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/ppas/sql/9.1_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/ppas/sql/9.1_plus/create.sql
index 8e51a49..6e9fd78 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/ppas/sql/9.1_plus/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/ppas/sql/9.1_plus/create.sql
@@ -6,16 +6,19 @@
 CREATE FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}()
     RETURNS{% if data.proretset %} SETOF{% endif %} {{ conn|qtTypeIdent(data.prorettypename) }}
     LANGUAGE {{ data.lanname|qtLiteral }}
-    {% if data.provolatile %}{{ data.provolatile }} {% endif %}{% if data.proisstrict %}STRICT {% endif %}{% if data.prosecdef %}SECURITY DEFINER {% endif %}{% if data.proiswindow %}WINDOW{% endif -%}{% if data.procost %}
-
-    COST {{data.procost}}{% endif %}{% if data.prorows %}
+{% if data.procost %}
+    COST {{data.procost}}
+{% endif %}
+{% if query_type != 'create' %}
+    {% if data.provolatile %}{{ data.provolatile }} {% endif %}{% if data.proisstrict %}STRICT {% endif %}{% if data.prosecdef %}SECURITY DEFINER {% endif %}{% if data.proiswindow %}WINDOW{% endif -%}
+{% if data.prorows %}
 
     ROWS {{data.prorows}}{% endif -%}{% if data.variables %}{% for v in data.variables %}
 
     SET {{ conn|qtIdent(v.name) }}={{ v.value|qtLiteral }}{% endfor %}
 {% endif %}
 
-AS {% if data.lanname == 'c' %}
+AS {% endif %}{% if data.lanname == 'c' %}
 {{ data.probin|qtLiteral }}, {{ data.prosrc_c|qtLiteral }}
 {% else %}
 $BODY$
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/ppas/sql/9.2_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/ppas/sql/9.2_plus/create.sql
index f7d497e..210cf3a 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/ppas/sql/9.2_plus/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/ppas/sql/9.2_plus/create.sql
@@ -6,19 +6,22 @@
 CREATE FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}()
     RETURNS{% if data.proretset %} SETOF{% endif %} {{ conn|qtTypeIdent(data.prorettypename) }}
     LANGUAGE {{ data.lanname|qtLiteral }}
+{% if data.procost %}
+    COST {{data.procost}}
+{% endif %}
+{% if query_type != 'create' %}
     {% if data.provolatile %}{{ data.provolatile }} {% endif %}{% if data.proleakproof %} LEAKPROOF {% else %} NOT LEAKPROOF {% endif %}
 {% if data.proisstrict %}STRICT {% endif %}
 {% if data.prosecdef %}SECURITY DEFINER {% endif %}
-{% if data.proiswindow %}WINDOW{% endif %}{% if data.procost %}
-
-    COST {{data.procost}}{% endif %}{% if data.prorows %}
+{% if data.proiswindow %}WINDOW{% endif %}
+{% if data.prorows %}
 
     ROWS {{data.prorows}}{% endif -%}{% if data.variables %}{% for v in data.variables %}
 
     SET {{ conn|qtIdent(v.name) }}={{ v.value|qtLiteral }}{% endfor %}
 {% endif %}
 
-AS {% if data.lanname == 'c' %}
+AS {% endif %}{% if data.lanname == 'c' %}
 {{ data.probin|qtLiteral }}, {{ data.prosrc_c|qtLiteral }}
 {% else %}
 $BODY$
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/ppas/sql/9.5_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/ppas/sql/9.5_plus/create.sql
index fa36ad8..5c7c568 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/ppas/sql/9.5_plus/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/functions/templates/trigger_function/ppas/sql/9.5_plus/create.sql
@@ -6,23 +6,26 @@
 CREATE FUNCTION {{ conn|qtIdent(data.pronamespace, data.name) }}()
     RETURNS{% if data.proretset %} SETOF{% endif %} {{ conn|qtTypeIdent(data.prorettypename) }}
     LANGUAGE {{ data.lanname|qtLiteral }}
+{% if data.procost %}
+    COST {{data.procost}}
+{% endif %}
+{% if query_type != 'create' %}
     {% if data.provolatile %}{{ data.provolatile }} {% endif %}{% if data.proleakproof %}LEAKPROOF {% else %}NOT LEAKPROOF {% endif %}
 {% if data.proisstrict %}STRICT {% endif %}
 {% if data.prosecdef %}SECURITY DEFINER {% endif %}
-{% if data.proiswindow %}WINDOW{% endif %}{% if data.procost %}
-
-    COST {{data.procost}}{% endif %}{% if data.prorows %}
+{% if data.proiswindow %}WINDOW{% endif %}
+{% if data.prorows %}
 
     ROWS {{data.prorows}}{% endif -%}{% if data.variables %}{% for v in data.variables %}
 
     SET {{ conn|qtIdent(v.name) }}={{ v.value|qtLiteral }}{% endfor %}
 {% endif %}
 
-AS {% if data.lanname == 'c' %}
++AS {% endif %}{% if data.lanname == 'c' %}
 {{ data.probin|qtLiteral }}, {{ data.prosrc_c|qtLiteral }}
 {% else %}
 $BODY$
-{{ data.prosrc }}
+{{ data.prosrc }};
 $BODY${% endif -%};
 {% if data.funcowner %}

---

From: Dave Page <[email-masked]>
Date: Fri, 22 Jul 2016 16:38:36 +0100

Thanks, applied.

On Fri, Jul 22, 2016 at 8:56 AM, Harshal Dhumal
<[email-masked]> wrote:
> Harshal Dhumal
> Software Engineer
> On Mon, Jun 6, 2016 at 7:17 PM, Surinder Kumar
> <[email-masked]> wrote:
>>
>> Hi,
>>
>> Issue: Code changes are not consistent with the present code in
>> functions/__init__.py, like don't rename variable func_def to func_defintion
>> until it is required.
>> On Fri, Jun 3, 2016 at 3:31 PM, Surinder Kumar
>> <[email-masked]> wrote:
>>>
>>> Hi,
>>>
>>> On Thu, Jun 2, 2016 at 7:32 PM, Surinder Kumar
>>> <[email-masked]> wrote:
>>>>
>>>> On Thu, Jun 2, 2016 at 7:25 PM, Surinder Kumar
>>>> <[email-masked]> wrote:
>>>>>
>>>>> Hi,
>>>>>

-- 
Dave Page
Blog: http://pgsnake.blogspot.com
Twitter: @pgsnake

EnterpriseDB UK: http://www.enterprisedb.com
The Enterprise PostgreSQL Company