From fb0bc65cedcf00d5e539ae7e4eaad0786438fbb1 Mon Sep 17 00:00:00 2001 From: Akshay Joshi Date: Mon, 27 Jul 2020 16:31:03 +0530 Subject: [PATCH] =?UTF-8?q?Fixed=20following=20SonarQube=20issues:=20?= =?UTF-8?q?=C2=A0=201)=C2=A0Define=20a=20constant=20instead=20of=20duplica?= =?UTF-8?q?ting=20this=20literal.=20=C2=A0=202)=C2=A0Reduce=20cognitive=20?= =?UTF-8?q?complexity=20(Patch=20by=20Aditya)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../servers/databases/casts/__init__.py | 30 +- .../external_tables/actions/get_all_nodes.py | 4 - .../servers/databases/languages/__init__.py | 3 +- .../servers/databases/schemas/__init__.py | 41 +- .../databases/schemas/collations/__init__.py | 14 +- .../databases/schemas/domains/__init__.py | 6 +- .../schemas/foreign_tables/__init__.py | 10 +- .../schemas/fts_configurations/__init__.py | 11 +- .../databases/schemas/fts_parsers/__init__.py | 15 +- .../schemas/fts_templates/__init__.py | 4 +- .../databases/schemas/functions/__init__.py | 6 +- .../databases/schemas/tables/__init__.py | 2 +- .../schemas/tables/indexes/__init__.py | 2 +- .../schemas/tables/partitions/__init__.py | 2 +- .../servers/databases/schemas/tables/utils.py | 8 +- .../databases/schemas/views/__init__.py | 407 ++++++++++-------- .../servers/resource_groups/__init__.py | 33 +- .../servers/tablespaces/__init__.py | 8 +- web/pgadmin/browser/utils.py | 7 + web/pgadmin/tools/debugger/__init__.py | 47 +- 20 files changed, 351 insertions(+), 309 deletions(-) delete mode 100644 web/pgadmin/browser/server_groups/servers/databases/external_tables/actions/get_all_nodes.py diff --git a/web/pgadmin/browser/server_groups/servers/databases/casts/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/casts/__init__.py index 7068a0805..30640662a 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/casts/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/casts/__init__.py @@ -206,6 +206,11 @@ class CastView(PGChildNodeView): # Set template path for the SQL scripts self.template_path = 'casts/sql/#{0}#'.format(self.manager.version) + self.datlastsysoid = \ + self.manager.db_info[kwargs['did']]['datlastsysoid'] \ + if self.manager.db_info is not None and \ + kwargs['did'] in self.manager.db_info else 0 + return f(*args, **kwargs) return wrap @@ -220,9 +225,7 @@ class CastView(PGChildNodeView): :return: """ last_system_oid = 0 if self.blueprint.show_system_objects else \ - (self.manager.db_info[did])['datlastsysoid'] \ - if self.manager.db_info is not None and \ - did in self.manager.db_info else 0 + self.datlastsysoid sql = render_template( "/".join([self.template_path, self._PROPERTIES_SQL]), datlastsysoid=last_system_oid, @@ -254,9 +257,8 @@ class CastView(PGChildNodeView): """ res = [] last_system_oid = 0 if self.blueprint.show_system_objects else \ - (self.manager.db_info[did])['datlastsysoid'] \ - if self.manager.db_info is not None and \ - did in self.manager.db_info else 0 + self.datlastsysoid + sql = render_template( "/".join([self.template_path, self._NODES_SQL]), datlastsysoid=last_system_oid, @@ -316,13 +318,10 @@ class CastView(PGChildNodeView): :param cid: cast id :return: """ - last_system_oid = (self.manager.db_info[did])['datlastsysoid'] if \ - self.manager.db_info is not None and \ - did in self.manager.db_info else 0 sql = render_template( "/".join([self.template_path, self._PROPERTIES_SQL]), cid=cid, - datlastsysoid=last_system_oid, + datlastsysoid=self.datlastsysoid, showsysobj=self.blueprint.show_system_objects ) status, res = self.conn.execute_dict(sql) @@ -380,9 +379,7 @@ class CastView(PGChildNodeView): # we need oid to to add object in tree at browser, below sql will # gives the same last_system_oid = 0 if self.blueprint.show_system_objects else \ - (self.manager.db_info[did])['datlastsysoid'] \ - if self.manager.db_info is not None and \ - did in self.manager.db_info else 0 + self.datlastsysoid sql = render_template( "/".join([self.template_path, self._PROPERTIES_SQL]), srctyp=data['srctyp'], @@ -541,9 +538,7 @@ class CastView(PGChildNodeView): """ if cid is not None: last_system_oid = 0 if self.blueprint.show_system_objects else \ - (self.manager.db_info[did])['datlastsysoid'] \ - if self.manager.db_info is not None and \ - did in self.manager.db_info else 0 + self.datlastsysoid sql = render_template( "/".join([self.template_path, self._PROPERTIES_SQL]), cid=cid, @@ -591,7 +586,8 @@ class CastView(PGChildNodeView): request.data, encoding='utf-8' ) - sql = render_template("/".join([self.template_path, 'functions.sql']), + sql = render_template("/".join([self.template_path, + self._FUNCTIONS_SQL]), srctyp=data['srctyp'], trgtyp=data['trgtyp']) status, rset = self.conn.execute_dict(sql) diff --git a/web/pgadmin/browser/server_groups/servers/databases/external_tables/actions/get_all_nodes.py b/web/pgadmin/browser/server_groups/servers/databases/external_tables/actions/get_all_nodes.py deleted file mode 100644 index a06bc0b3f..000000000 --- a/web/pgadmin/browser/server_groups/servers/databases/external_tables/actions/get_all_nodes.py +++ /dev/null @@ -1,4 +0,0 @@ - -class GetAllNodes: - def execute(self): - pass diff --git a/web/pgadmin/browser/server_groups/servers/databases/languages/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/languages/__init__.py index 40108a0f8..06966ca2b 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/languages/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/languages/__init__.py @@ -660,7 +660,8 @@ class LanguageView(PGChildNodeView): sid: Server ID did: Database ID """ - sql = render_template("/".join([self.template_path, 'functions.sql'])) + sql = render_template("/".join([self.template_path, + self._FUNCTIONS_SQL])) status, result = self.conn.execute_dict(sql) if not status: return internal_server_error(errormsg=result) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/__init__.py index 148bd0282..e0ef0ae18 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/__init__.py @@ -207,6 +207,7 @@ class SchemaView(PGChildNodeView): pane for the selected schema node. """ node_type = schema_blueprint.node_type + _SQL_PREFIX = 'sql/' parent_ids = [ {'type': 'int', 'id': 'gid'}, @@ -396,7 +397,8 @@ class SchemaView(PGChildNodeView): param = schema_res % (tuple(schema_restrictions.split(','))) SQL = render_template( - "/".join([self.template_path, 'sql/properties.sql']), + "/".join([self.template_path, + self._SQL_PREFIX + self._PROPERTIES_SQL]), _=gettext, show_sysobj=self.blueprint.show_system_objects, schema_restrictions=param @@ -436,7 +438,7 @@ class SchemaView(PGChildNodeView): param = schema_res % (tuple(schema_restrictions.split(','))) SQL = render_template( - "/".join([self.template_path, 'sql/nodes.sql']), + "/".join([self.template_path, self._SQL_PREFIX + self._NODES_SQL]), show_sysobj=self.blueprint.show_system_objects, _=gettext, scid=scid, @@ -499,7 +501,7 @@ class SchemaView(PGChildNodeView): JSON of given schema child node """ SQL = render_template( - "/".join([self.template_path, 'sql/nodes.sql']), + "/".join([self.template_path, self._SQL_PREFIX + self._NODES_SQL]), show_sysobj=self.blueprint.show_system_objects, _=gettext, scid=scid @@ -545,7 +547,8 @@ It may have been removed by another user. JSON of selected schema node """ SQL = render_template( - "/".join([self.template_path, 'sql/properties.sql']), + "/".join([self.template_path, + self._SQL_PREFIX + self._PROPERTIES_SQL]), scid=scid, _=gettext, show_sysobj=self.blueprint.show_system_objects @@ -603,7 +606,8 @@ It may have been removed by another user. try: self.format_request_acls(data) SQL = render_template( - "/".join([self.template_path, 'sql/create.sql']), + "/".join([self.template_path, + self._SQL_PREFIX + self._CREATE_SQL]), data=data, conn=self.conn, _=gettext ) status, res = self.conn.execute_scalar(SQL) @@ -718,7 +722,8 @@ It may have been removed by another user. # drop schema SQL = render_template( - "/".join([self.template_path, 'sql/delete.sql']), + "/".join([self.template_path, + self._SQL_PREFIX + self._DELETE_SQL]), _=gettext, name=name, conn=self.conn, cascade=True if self.cmd == 'delete' else False ) @@ -776,7 +781,8 @@ It may have been removed by another user. """ if scid is not None: SQL = render_template( - "/".join([self.template_path, 'sql/properties.sql']), + "/".join([self.template_path, + self._SQL_PREFIX + self._PROPERTIES_SQL]), _=gettext, scid=scid, show_sysobj=self.blueprint.show_system_objects ) @@ -796,7 +802,8 @@ It may have been removed by another user. self.format_request_acls(data, True) SQL = render_template( - "/".join([self.template_path, 'sql/update.sql']), + "/".join([self.template_path, + self._SQL_PREFIX + self._UPDATE_SQL]), _=gettext, data=data, o_data=old_data, conn=self.conn ) return SQL, data['name'] if 'name' in data else old_data['nam'] @@ -811,7 +818,8 @@ It may have been removed by another user. self.format_request_acls(data) SQL = render_template( - "/".join([self.template_path, 'sql/create.sql']), + "/".join([self.template_path, + self._SQL_PREFIX + self._CREATE_SQL]), data=data, conn=self.conn, _=gettext ) @@ -830,7 +838,8 @@ It may have been removed by another user. scid: Schema ID """ SQL = render_template( - "/".join([self.template_path, 'sql/properties.sql']), + "/".join([self.template_path, + self._SQL_PREFIX + self._PROPERTIES_SQL]), scid=scid, _=gettext ) @@ -852,7 +861,8 @@ It may have been removed by another user. # Render sql from create & alter sql using properties & acl data SQL = '' SQL = render_template( - "/".join([self.template_path, 'sql/create.sql']), + "/".join([self.template_path, + self._SQL_PREFIX + self._CREATE_SQL]), _=gettext, data=data, conn=self.conn ) @@ -860,7 +870,8 @@ It may have been removed by another user. # drop schema sql_header += render_template( - "/".join([self.template_path, 'sql/delete.sql']), + "/".join([self.template_path, + self._SQL_PREFIX + self._DELETE_SQL]), _=gettext, name=data['name'], conn=self.conn, cascade=False) SQL = sql_header + '\n\n' + SQL @@ -1010,7 +1021,8 @@ class CatalogView(SchemaView): scid: Schema ID """ SQL = render_template( - "/".join([self.template_path, 'sql/properties.sql']), + "/".join([self.template_path, + self._SQL_PREFIX + self._PROPERTIES_SQL]), scid=scid, _=gettext ) @@ -1033,7 +1045,8 @@ It may have been removed by another user. # Render sql from create & alter sql using properties & acl data SQL = '' SQL = render_template( - "/".join([self.template_path, 'sql/create.sql']), + "/".join([self.template_path, + self._SQL_PREFIX + self._CREATE_SQL]), _=gettext, data=old_data, conn=self.conn ) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/collations/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/collations/__init__.py index f4506c032..7961e25d2 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/collations/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/collations/__init__.py @@ -520,19 +520,11 @@ class CollationView(PGChildNodeView, SchemaDiffObjectCompare): coid: Collation ID only_sql: Return only sql if True """ - if coid is None: - data = request.form if request.form else json.loads( - request.data, encoding='utf-8' - ) - else: - data = {'ids': [coid]} + data = json.loads(request.data, encoding='utf-8') if coid is None \ + else {'ids': [coid]} # Below will decide if it's simple drop or drop with cascade call - if self.cmd == 'delete': - # This is a cascade operation - cascade = True - else: - cascade = False + cascade = True if self.cmd == 'delete' else False try: for coid in data['ids']: diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/domains/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/domains/__init__.py index 4616629a4..653c6b5d4 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/domains/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/domains/__init__.py @@ -413,7 +413,7 @@ It may have been removed by another user or moved to another schema. # Get Domain Constraints SQL = render_template("/".join([self.template_path, - 'get_constraints.sql']), + self._GET_CONSTRAINTS_SQL]), doid=doid) status, res = self.conn.execute_dict(SQL) if not status: @@ -728,7 +728,7 @@ AND relkind != 'c'))""" # Get Domain Constraints SQL = render_template("/".join([self.template_path, - 'get_constraints.sql']), + self._GET_CONSTRAINTS_SQL]), doid=doid) status, res = self.conn.execute_dict(SQL) if not status: @@ -823,7 +823,7 @@ AND relkind != 'c'))""" # Get Domain Constraints SQL = render_template("/".join([self.template_path, - 'get_constraints.sql']), + self._GET_CONSTRAINTS_SQL]), doid=doid) status, res = self.conn.execute_dict(SQL) if not status: diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/__init__.py index d0933e783..e14b3f2c5 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/__init__.py @@ -292,7 +292,7 @@ class ForeignTableView(PGChildNodeView, DataTypeReader, # as we need Table names to generate the SQL. SQL = render_template( "/".join([self.template_path, - 'get_tables.sql']), + self._GET_TABLES_SQL]), attrelid=inherits) status, res = self.conn.execute_dict(SQL) @@ -578,7 +578,7 @@ class ForeignTableView(PGChildNodeView, DataTypeReader, res = [] try: SQL = render_template("/".join( - [self.template_path, 'get_tables.sql']), + [self.template_path, self._GET_TABLES_SQL]), foid=foid, server_type=self.manager.server_type, show_sys_objects=self.blueprint.show_system_objects) status, rset = self.conn.execute_dict(SQL) @@ -1135,7 +1135,7 @@ class ForeignTableView(PGChildNodeView, DataTypeReader, data.update(self._parse_variables_from_db(data['ftoptions'])) SQL = render_template("/".join([self.template_path, - 'get_constraints.sql']), foid=foid) + self._GET_CONSTRAINTS_SQL]), foid=foid) status, cons = self.conn.execute_dict(SQL) if not status: return False, internal_server_error(errormsg=cons) @@ -1144,7 +1144,7 @@ class ForeignTableView(PGChildNodeView, DataTypeReader, data['constraints'] = cons['rows'] SQL = render_template("/".join([self.template_path, - 'get_columns.sql']), foid=foid) + self._GET_COLUMNS_SQL]), foid=foid) status, cols = self.conn.execute_dict(SQL) if not status: return False, internal_server_error(errormsg=cols) @@ -1178,7 +1178,7 @@ class ForeignTableView(PGChildNodeView, DataTypeReader, inherits = "(" + str(inherits[0]) + ")" SQL = render_template("/".join([self.template_path, - 'get_tables.sql']), + self._GET_TABLES_SQL]), attrelid=inherits) status, res = self.conn.execute_dict(SQL) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/fts_configurations/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/fts_configurations/__init__.py index cec25474a..1e989e544 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/fts_configurations/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/fts_configurations/__init__.py @@ -696,9 +696,10 @@ class FtsConfigurationView(PGChildNodeView, SchemaDiffObjectCompare): status, res = self.conn.execute_dict(sql) if not status: - return internal_server_error(errormsg=res) + return internal_server_error(errormsg=res), '' elif len(res['rows']) == 0: - return gone(_("Could not find the FTS Configuration node.")) + return \ + gone(_("Could not find the FTS Configuration node.")), '' old_data = res['rows'][0] if 'schema' not in data: @@ -712,7 +713,7 @@ class FtsConfigurationView(PGChildNodeView, SchemaDiffObjectCompare): status, new_schema = self.conn.execute_scalar(sql) if not status: - return internal_server_error(errormsg=new_schema) + return internal_server_error(errormsg=new_schema), '' new_data = data.copy() # Replace schema oid with schema name @@ -726,7 +727,7 @@ class FtsConfigurationView(PGChildNodeView, SchemaDiffObjectCompare): status, old_schema = self.conn.execute_scalar(sql) if not status: - return internal_server_error(errormsg=old_schema) + return internal_server_error(errormsg=old_schema), '' # Replace old schema oid with old schema name old_data['schema'] = old_schema @@ -749,7 +750,7 @@ class FtsConfigurationView(PGChildNodeView, SchemaDiffObjectCompare): status, schema = self.conn.execute_scalar(sql) if not status: - return internal_server_error(errormsg=schema) + return internal_server_error(errormsg=schema), '' sql = self._get_sql_for_create(data, schema) return sql.strip('\n'), data['name'] diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/fts_parsers/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/fts_parsers/__init__.py index ea1ce883c..3699d8217 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/fts_parsers/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/fts_parsers/__init__.py @@ -708,7 +708,8 @@ class FtsParserView(PGChildNodeView, SchemaDiffObjectCompare): :param did: database id :param scid: schema id """ - sql = render_template("/".join([self.template_path, 'functions.sql']), + sql = render_template("/".join([self.template_path, + self._FUNCTIONS_SQL]), start=True) status, rset = self.conn.execute_dict(sql) @@ -735,7 +736,8 @@ class FtsParserView(PGChildNodeView, SchemaDiffObjectCompare): :param did: database id :param scid: schema id """ - sql = render_template("/".join([self.template_path, 'functions.sql']), + sql = render_template("/".join([self.template_path, + self._FUNCTIONS_SQL]), token=True) status, rset = self.conn.execute_dict(sql) @@ -762,7 +764,8 @@ class FtsParserView(PGChildNodeView, SchemaDiffObjectCompare): :param did: database id :param scid: schema id """ - sql = render_template("/".join([self.template_path, 'functions.sql']), + sql = render_template("/".join([self.template_path, + self._FUNCTIONS_SQL]), end=True) status, rset = self.conn.execute_dict(sql) @@ -789,7 +792,8 @@ class FtsParserView(PGChildNodeView, SchemaDiffObjectCompare): :param did: database id :param scid: schema id """ - sql = render_template("/".join([self.template_path, 'functions.sql']), + sql = render_template("/".join([self.template_path, + self._FUNCTIONS_SQL]), lextype=True) status, rset = self.conn.execute_dict(sql) @@ -816,7 +820,8 @@ class FtsParserView(PGChildNodeView, SchemaDiffObjectCompare): :param did: database id :param scid: schema id """ - sql = render_template("/".join([self.template_path, 'functions.sql']), + sql = render_template("/".join([self.template_path, + self._FUNCTIONS_SQL]), headline=True) status, rset = self.conn.execute_dict(sql) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/fts_templates/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/fts_templates/__init__.py index 011e36728..a7fd405cc 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/fts_templates/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/fts_templates/__init__.py @@ -680,7 +680,7 @@ class FtsTemplateView(PGChildNodeView, SchemaDiffObjectCompare): :param tid: fts tempate id """ sql = render_template( - "/".join([self.template_path, 'functions.sql']), lexize=True + "/".join([self.template_path, self._FUNCTIONS_SQL]), lexize=True ) status, rset = self.conn.execute_dict(sql) @@ -709,7 +709,7 @@ class FtsTemplateView(PGChildNodeView, SchemaDiffObjectCompare): :param tid: fts tempate id """ sql = render_template( - "/".join([self.template_path, 'functions.sql']), init=True + "/".join([self.template_path, self._FUNCTIONS_SQL]), init=True ) status, rset = self.conn.execute_dict(sql) 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 e1ab8b625..5f31bab19 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 @@ -1124,7 +1124,7 @@ class FunctionView(PGChildNodeView, DataTypeReader, SchemaDiffObjectCompare): self._get_schema_name_from_oid(resp_data) sql = render_template("/".join([self.sql_template_path, - 'get_definition.sql'] + self._GET_DEFINITION_SQL] ), data=resp_data, fnid=fnid, scid=scid) @@ -1163,7 +1163,7 @@ class FunctionView(PGChildNodeView, DataTypeReader, SchemaDiffObjectCompare): self._parse_privilege_data(resp_data) sql = render_template("/".join([self.sql_template_path, - 'get_definition.sql'] + self._GET_DEFINITION_SQL] ), data=resp_data, fnid=fnid, scid=scid) @@ -1687,7 +1687,7 @@ class FunctionView(PGChildNodeView, DataTypeReader, SchemaDiffObjectCompare): """ # Fetch the function definition. sql = render_template("/".join([self.sql_template_path, - 'get_definition.sql']), fnid=fnid, + self._GET_DEFINITION_SQL]), fnid=fnid, scid=scid) status, res = self.conn.execute_2darray(sql) if not status: diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/__init__.py index 3f616c946..716560976 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/__init__.py @@ -1014,7 +1014,7 @@ class TableView(BaseTableView, DataTypeReader, VacuumSettings, # Get updated schema oid sql = render_template( - "/".join([self.table_template_path, 'get_schema_oid.sql']), + "/".join([self.table_template_path, self._GET_SCHEMA_OID_SQL]), tname=data['name'] ) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/indexes/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/indexes/__init__.py index 010e31a4c..c218e9661 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/indexes/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/indexes/__init__.py @@ -620,7 +620,7 @@ class IndexesView(PGChildNodeView, SchemaDiffObjectCompare): # If user chooses concurrent index then we cannot run it along # with other alter statements so we will separate alter index part SQL = render_template( - "/".join([self.template_path, 'alter.sql']), + "/".join([self.template_path, self._ALTER_SQL]), data=data, conn=self.conn ) SQL = SQL.strip('\n').strip(' ') diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/partitions/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/partitions/__init__.py index 7020b1a80..eeed70eb5 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/partitions/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/partitions/__init__.py @@ -570,7 +570,7 @@ class PartitionsView(BaseTableView, DataTypeReader, VacuumSettings, # Get schema oid of partition status, pscid = self.conn.execute_scalar( render_template("/".join([self.table_template_path, - 'get_schema_oid.sql']), tid=ptid)) + self._GET_SCHEMA_OID_SQL]), tid=ptid)) if not status: return internal_server_error(errormsg=scid) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/utils.py index 576726457..44a0cf56a 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/utils.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/utils.py @@ -1296,7 +1296,7 @@ class BaseTableView(PGChildNodeView, BasePartitionTable): return internal_server_error(errormsg=rest) sql = render_template("/".join([self.table_template_path, - 'get_schema_oid.sql']), tid=tid) + self._GET_SCHEMA_OID_SQL]), tid=tid) status, rest = self.conn.execute_2darray(sql) if not status: return internal_server_error(errormsg=rest) @@ -1343,7 +1343,7 @@ class BaseTableView(PGChildNodeView, BasePartitionTable): render_template( "/".join([ self.table_template_path, - 'get_schema_oid.sql' + self._GET_SCHEMA_OID_SQL ]), tid=row['oid'] ) @@ -1376,7 +1376,7 @@ class BaseTableView(PGChildNodeView, BasePartitionTable): render_template( "/".join([ self.table_template_path, - 'get_schema_oid.sql' + self._GET_SCHEMA_OID_SQL ]), tid=row['partition_name'] ) @@ -1721,7 +1721,7 @@ class BaseTableView(PGChildNodeView, BasePartitionTable): # Get schema oid status, scid = self.conn.execute_scalar( render_template("/".join([self.table_template_path, - 'get_schema_oid.sql']), tid=tid)) + self._GET_SCHEMA_OID_SQL]), tid=tid)) if not status: return internal_server_error(errormsg=scid) if scid is None: diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/views/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/views/__init__.py index 5b003ebf4..c716aa00c 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/views/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/views/__init__.py @@ -9,7 +9,6 @@ """Implements View and Materialized View Node""" -import copy import re from functools import wraps @@ -311,6 +310,8 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): different schemas. """ node_type = view_blueprint.node_type + _SQL_PREFIX = 'sql/' + _ALLOWED_PRIVS_JSON = 'sql/allowed_privs.json' parent_ids = [ {'type': 'int', 'id': 'gid'}, @@ -390,7 +391,8 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): Fetches all views properties and render into properties tab """ SQL = render_template("/".join( - [self.template_path, 'sql/properties.sql']), did=did, scid=scid) + [self.template_path, self._SQL_PREFIX + self._PROPERTIES_SQL]), + did=did, scid=scid) status, res = self.conn.execute_dict(SQL) if not status: @@ -406,7 +408,7 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): Lists all views under the Views Collection node """ SQL = render_template("/".join( - [self.template_path, 'sql/nodes.sql']), + [self.template_path, self._SQL_PREFIX + self._NODES_SQL]), vid=vid, datlastsysoid=self.datlastsysoid) status, rset = self.conn.execute_2darray(SQL) if not status: @@ -435,7 +437,8 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): """ res = [] SQL = render_template("/".join( - [self.template_path, 'sql/nodes.sql']), scid=scid) + [self.template_path, self._SQL_PREFIX + self._NODES_SQL]), + scid=scid) status, rset = self.conn.execute_2darray(SQL) if not status: return internal_server_error(errormsg=rset) @@ -478,7 +481,7 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): :return: """ SQL = render_template("/".join( - [self.template_path, 'sql/properties.sql'] + [self.template_path, self._SQL_PREFIX + self._PROPERTIES_SQL] ), vid=vid, datlastsysoid=self.datlastsysoid) status, res = self.conn.execute_dict(SQL) if not status: @@ -488,7 +491,7 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): return False, gone(gettext("""Could not find the view.""")) SQL = render_template("/".join( - [self.template_path, 'sql/acl.sql']), vid=vid) + [self.template_path, self._SQL_PREFIX + self._ACL_SQL]), vid=vid) status, dataclres = self.conn.execute_dict(SQL) if not status: return False, internal_server_error(errormsg=res) @@ -564,16 +567,16 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): # Get updated schema oid SQL = render_template("/".join( - [self.template_path, 'sql/get_oid.sql']), vid=view_id) - status, scid = self.conn.execute_scalar(SQL) - + [self.template_path, self._SQL_PREFIX + self._OID_SQL]), + vid=view_id) + status, new_scid = self.conn.execute_scalar(SQL) if not status: - return internal_server_error(errormsg=res) + return internal_server_error(errormsg=new_scid) return jsonify( node=self.blueprint.generate_browser_node( view_id, - scid, + new_scid, data['name'], icon="icon-view" if self.node_type == 'view' else "icon-mview" @@ -611,15 +614,16 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): # Get updated schema oid SQL = render_template("/".join( - [self.template_path, 'sql/get_oid.sql']), vid=view_id) - status, scid = self.conn.execute_scalar(SQL) + [self.template_path, self._SQL_PREFIX + self._OID_SQL]), + vid=view_id) + status, new_scid = self.conn.execute_scalar(SQL) if not status: - return internal_server_error(errormsg=res) + return internal_server_error(errormsg=new_scid) return jsonify( node=self.blueprint.generate_browser_node( view_id, - scid, + new_scid, new_view_name, icon="icon-view" if self.node_type == 'view' else "icon-mview" @@ -648,9 +652,8 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): for vid in data['ids']: # Get name for view from vid SQL = render_template( - "/".join([ - self.template_path, 'sql/properties.sql' - ]), + "/".join([self.template_path, + self._SQL_PREFIX + self._PROPERTIES_SQL]), did=did, vid=vid, datlastsysoid=self.datlastsysoid @@ -672,9 +675,8 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): # drop view SQL = render_template( - "/".join([ - self.template_path, 'sql/delete.sql' - ]), + "/".join([self.template_path, + self._SQL_PREFIX + self._DELETE_SQL]), nspname=res_data['rows'][0]['schema'], name=res_data['rows'][0]['name'], cascade=cascade ) @@ -778,7 +780,8 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): """ if vid is not None: sql = render_template("/".join( - [self.template_path, 'sql/properties.sql']), + [self.template_path, + self._SQL_PREFIX + self._PROPERTIES_SQL]), vid=vid, datlastsysoid=self.datlastsysoid ) @@ -795,7 +798,7 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): try: acls = render_template( - "/".join([self.template_path, 'sql/allowed_privs.json']) + "/".join([self.template_path, self._ALLOWED_PRIVS_JSON]) ) acls = json.loads(acls, encoding='utf-8') except Exception as e: @@ -814,7 +817,8 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): try: sql = render_template("/".join( - [self.template_path, 'sql/update.sql']), data=data, + [self.template_path, + self._SQL_PREFIX + self._UPDATE_SQL]), data=data, o_data=old_data, conn=self.conn) except Exception as e: @@ -852,7 +856,7 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): acls = [] try: acls = render_template( - "/".join([self.template_path, 'sql/allowed_privs.json']) + "/".join([self.template_path, self._ALLOWED_PRIVS_JSON]) ) acls = json.loads(acls, encoding='utf-8') except Exception as e: @@ -862,11 +866,13 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): ViewNode._parse_priv_data(acls, data) sql = render_template("/".join( - [self.template_path, 'sql/create.sql']), data=data) + [self.template_path, self._SQL_PREFIX + self._CREATE_SQL]), + data=data) if data['definition']: sql += "\n" sql += render_template("/".join( - [self.template_path, 'sql/grant.sql']), data=data) + [self.template_path, self._SQL_PREFIX + self._GRANT_SQL]), + data=data) return False, '', sql @@ -908,7 +914,8 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): ViewNode._parse_priv_data(acls, old_data) old_data['acl_sql'] = render_template("/".join( - [self.template_path, 'sql/grant.sql']), data=old_data) + [self.template_path, self._SQL_PREFIX + self._GRANT_SQL]), + data=old_data) return False, '' def _fetch_all_view_priv(self, vid, res): @@ -918,7 +925,7 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): res: response data from property sql """ sql_acl = render_template("/".join( - [self.template_path, 'sql/acl.sql']), vid=vid) + [self.template_path, self._SQL_PREFIX + self._ACL_SQL]), vid=vid) status, dataclres = self.conn.execute_dict(sql_acl) if not status: return True, internal_server_error(errormsg=res) @@ -1010,7 +1017,7 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): self.trigger_temp_path = 'schemas/triggers' SQL = render_template("/".join([self.trigger_temp_path, - 'get_columns.sql']), + self._GET_COLUMNS_SQL]), tid=tid, clist=clist) status, rset = self.conn.execute_2darray(SQL) if not status: @@ -1033,7 +1040,8 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): self.rule_temp_path = 'rules' sql_data = '' SQL = render_template("/".join( - [self.rule_temp_path, 'sql/properties.sql']), tid=vid) + [self.rule_temp_path, self._SQL_PREFIX + self._PROPERTIES_SQL]), + tid=vid) status, data = self.conn.execute_dict(SQL) if not status: @@ -1045,13 +1053,15 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): if rule['name'] != '_RETURN': res = [] SQL = render_template("/".join( - [self.rule_temp_path, 'sql/properties.sql']), + [self.rule_temp_path, + self._SQL_PREFIX + self._PROPERTIES_SQL]), rid=rule['oid'] ) status, res = self.conn.execute_dict(SQL) res = parse_rule_definition(res) SQL = render_template("/".join( - [self.rule_temp_path, 'sql/create.sql']), + [self.rule_temp_path, + self._SQL_PREFIX + self._CREATE_SQL]), data=res, display_comments=display_comments) sql_data += '\n' sql_data += SQL @@ -1305,7 +1315,7 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): sql_data = '' SQL = render_template("/".join( - [self.template_path, 'sql/properties.sql']), + [self.template_path, self._SQL_PREFIX + self._PROPERTIES_SQL]), vid=vid, datlastsysoid=self.datlastsysoid ) @@ -1334,7 +1344,7 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): # Fetch all privileges for view SQL = render_template("/".join( - [self.template_path, 'sql/acl.sql']), vid=vid) + [self.template_path, self._SQL_PREFIX + self._ACL_SQL]), vid=vid) status, dataclres = self.conn.execute_dict(SQL) if not status: return internal_server_error(errormsg=res) @@ -1348,7 +1358,7 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): acls = [] try: acls = render_template( - "/".join([self.template_path, 'sql/allowed_privs.json']) + "/".join([self.template_path, self._ALLOWED_PRIVS_JSON]) ) acls = json.loads(acls, encoding='utf-8') except Exception as e: @@ -1363,14 +1373,15 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): ) SQL = render_template("/".join( - [self.template_path, 'sql/create.sql']), + [self.template_path, self._SQL_PREFIX + self._CREATE_SQL]), data=result, conn=self.conn, display_comments=display_comments ) SQL += "\n" SQL += render_template("/".join( - [self.template_path, 'sql/grant.sql']), data=result) + [self.template_path, self._SQL_PREFIX + self._GRANT_SQL]), + data=result) sql_data += SQL sql_data += self.get_rule_sql(vid, display_comments) @@ -1463,7 +1474,7 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): """ SQL = render_template( "/".join([ - self.template_path, 'sql/properties.sql' + self.template_path, self._SQL_PREFIX + self._PROPERTIES_SQL ]), scid=scid, vid=vid, did=did, datlastsysoid=self.datlastsysoid @@ -1524,7 +1535,7 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): """ SQL = render_template( "/".join([ - self.template_path, 'sql/properties.sql' + self.template_path, self._SQL_PREFIX + self._PROPERTIES_SQL ]), scid=scid, vid=vid, did=did, datlastsysoid=self.datlastsysoid @@ -1587,9 +1598,9 @@ class ViewNode(PGChildNodeView, VacuumSettings, SchemaDiffObjectCompare): res = dict() if not oid: - SQL = render_template("/".join([self.template_path, - 'sql/nodes.sql']), did=did, - scid=scid, datlastsysoid=self.datlastsysoid) + SQL = render_template("/".join( + [self.template_path, self._SQL_PREFIX + self._NODES_SQL]), + did=did, scid=scid, datlastsysoid=self.datlastsysoid) status, views = self.conn.execute_2darray(SQL) if not status: current_app.logger.error(views) @@ -1709,146 +1720,165 @@ class MViewNode(ViewNode, VacuumSettings): '9.3_plus' ) + @staticmethod + def merge_to_vacuum_data(old_data, data, vacuum_key): + """ + Used by getSQL method to merge vacuum data + """ + if vacuum_key not in data: + return + + if 'changed' not in data[vacuum_key]: + return + + for item in data[vacuum_key]['changed']: + old_data_item_key = item['name'] + if vacuum_key == 'vacuum_toast': + old_data_item_key = 'toast_' + item['name'] + item['name'] = 'toast.' + item['name'] + + if 'value' not in item.keys(): + continue + if item['value'] is None: + if old_data[old_data_item_key] != item['value']: + data['vacuum_data']['reset'].append(item) + elif old_data[old_data_item_key] is None or \ + float(old_data[old_data_item_key]) != \ + float(item['value']): + data['vacuum_data']['changed'].append(item) + + def _getSQL_existing(self, did, data, vid): + """ + Used by getSQL to get SQL for existing mview. + """ + status, res = self._fetch_mview_properties(did, None, vid) + + if not status: + return res + + old_data = res + + if 'name' not in data: + data['name'] = res['name'] + if 'schema' not in data: + data['schema'] = res['schema'] + + # merge vacuum lists into one + data['vacuum_data'] = {} + data['vacuum_data']['changed'] = [] + data['vacuum_data']['reset'] = [] + + # table vacuum: separate list of changed and reset data for + self.merge_to_vacuum_data(old_data, data, 'vacuum_table') + # table vacuum toast: separate list of changed and reset data for + self.merge_to_vacuum_data(old_data, data, 'vacuum_toast') + + acls = [] + try: + acls = render_template( + "/".join([self.template_path, self._ALLOWED_PRIVS_JSON]) + ) + acls = json.loads(acls, encoding='utf-8') + except Exception as e: + current_app.logger.exception(e) + + # Privileges + for aclcol in acls: + if aclcol in data: + allowedacl = acls[aclcol] + + for key in ['added', 'changed', 'deleted']: + if key in data[aclcol]: + data[aclcol][key] = parse_priv_to_db( + data[aclcol][key], allowedacl['acl'] + ) + + try: + SQL = render_template("/".join( + [self.template_path, + self._SQL_PREFIX + self._UPDATE_SQL]), data=data, + o_data=old_data, conn=self.conn) + except Exception as e: + current_app.logger.exception(e) + return None, internal_server_error(errormsg=str(e)) + + return SQL, old_data['name'] + + def _getSQL_new(self, data): + """ + Used by getSQL to get SQL for new mview. + """ + required_args = [ + 'name', + 'schema', + 'definition' + ] + for arg in required_args: + if arg not in data: + return None, make_json_response( + data=gettext(" -- definition incomplete"), + status=200 + ) + + # Get Schema Name from its OID. + if 'schema' in data and isinstance(data['schema'], int): + data['schema'] = self._get_schema(data['schema']) + + # merge vacuum lists into one + vacuum_table = [item for item in data.get('vacuum_table', []) + if 'value' in item.keys() and + item['value'] is not None] + vacuum_toast = [ + {'name': 'toast.' + item['name'], 'value': item['value']} + for item in data.get('vacuum_toast', []) + if 'value' in item.keys() and item['value'] is not None] + + # add vacuum_toast dict to vacuum_data + data['vacuum_data'] = [] + if data.get('autovacuum_custom', False): + data['vacuum_data'] = vacuum_table + + if data.get('toast_autovacuum', False): + data['vacuum_data'] += vacuum_toast + + acls = [] + try: + acls = render_template( + "/".join([self.template_path, self._ALLOWED_PRIVS_JSON]) + ) + acls = json.loads(acls, encoding='utf-8') + except Exception as e: + current_app.logger.exception(e) + + # Privileges + for aclcol in acls: + if aclcol in data: + allowedacl = acls[aclcol] + data[aclcol] = parse_priv_to_db( + data[aclcol], allowedacl['acl'] + ) + + SQL = render_template("/".join( + [self.template_path, self._SQL_PREFIX + self._CREATE_SQL]), + data=data) + if data['definition']: + SQL += "\n" + SQL += render_template("/".join( + [self.template_path, self._SQL_PREFIX + self._GRANT_SQL]), + data=data) + + return SQL, data.get('name', None) + def getSQL(self, gid, sid, did, data, vid=None): """ This function will generate sql from model data """ if vid is not None: - status, res = self._fetch_properties(did, None, vid) - - if not status: - return res - - old_data = res - - if 'name' not in data: - data['name'] = res['name'] - if 'schema' not in data: - data['schema'] = res['schema'] - - # merge vacuum lists into one - data['vacuum_data'] = {} - data['vacuum_data']['changed'] = [] - data['vacuum_data']['reset'] = [] - - # table vacuum: separate list of changed and reset data for - if 'vacuum_table' in data and 'changed' in data['vacuum_table']: - for item in data['vacuum_table']['changed']: - if 'value' in item.keys(): - if item['value'] is None: - if old_data[item['name']] != item['value']: - data['vacuum_data']['reset'].append(item) - else: - if (old_data[item['name']] is None or - (float(old_data[item['name']]) != float( - item['value']))): - data['vacuum_data']['changed'].append(item) - - # toast autovacuum: separate list of changed and reset data - if 'vacuum_toast' in data and 'changed' in data['vacuum_toast']: - for item in data['vacuum_toast']['changed']: - if 'value' in item.keys(): - toast_key = 'toast_' + item['name'] - item['name'] = 'toast.' + item['name'] - if item['value'] is None: - if old_data[toast_key] != item['value']: - data['vacuum_data']['reset'].append(item) - else: - if (old_data[toast_key] is None or - (float(old_data[toast_key]) != float( - item['value']))): - data['vacuum_data']['changed'].append(item) - - acls = [] - try: - acls = render_template( - "/".join([self.template_path, 'sql/allowed_privs.json']) - ) - acls = json.loads(acls, encoding='utf-8') - except Exception as e: - current_app.logger.exception(e) - - # Privileges - for aclcol in acls: - if aclcol in data: - allowedacl = acls[aclcol] - - for key in ['added', 'changed', 'deleted']: - if key in data[aclcol]: - data[aclcol][key] = parse_priv_to_db( - data[aclcol][key], allowedacl['acl'] - ) - - try: - SQL = render_template("/".join( - [self.template_path, 'sql/update.sql']), data=data, - o_data=old_data, conn=self.conn) - except Exception as e: - current_app.logger.exception(e) - return None, internal_server_error(errormsg=str(e)) + SQL, data_name = self._getSQL_existing(did, data, vid) else: - required_args = [ - 'name', - 'schema', - 'definition' - ] - for arg in required_args: - if arg not in data: - return None, make_json_response( - data=gettext(" -- definition incomplete"), - status=200 - ) + SQL, data_name = self._getSQL_new(data) - # Get Schema Name from its OID. - if 'schema' in data and isinstance(data['schema'], int): - data['schema'] = self._get_schema(data['schema']) - - # merge vacuum lists into one - vacuum_table = [item for item in data.get('vacuum_table', []) - if 'value' in item.keys() and - item['value'] is not None] - vacuum_toast = [ - {'name': 'toast.' + item['name'], 'value': item['value']} - for item in data.get('vacuum_toast', []) - if 'value' in item.keys() and item['value'] is not None] - - # add vacuum_toast dict to vacuum_data - data['vacuum_data'] = [] - if ( - 'autovacuum_custom' in data and - data['autovacuum_custom'] is True - ): - data['vacuum_data'] = vacuum_table - if ( - 'toast_autovacuum' in data and - data['toast_autovacuum'] is True - ): - data['vacuum_data'] += vacuum_toast - - acls = [] - try: - acls = render_template( - "/".join([self.template_path, 'sql/allowed_privs.json']) - ) - acls = json.loads(acls, encoding='utf-8') - except Exception as e: - current_app.logger.exception(e) - - # Privileges - for aclcol in acls: - if aclcol in data: - allowedacl = acls[aclcol] - data[aclcol] = parse_priv_to_db( - data[aclcol], allowedacl['acl'] - ) - - SQL = render_template("/".join( - [self.template_path, 'sql/create.sql']), data=data) - if data['definition']: - SQL += "\n" - SQL += render_template("/".join( - [self.template_path, 'sql/grant.sql']), data=data) - return SQL, data['name'] if 'name' in data else old_data['name'] + return SQL, data_name @check_precondition def sql(self, gid, sid, did, scid, vid, **kwargs): @@ -1864,7 +1894,7 @@ class MViewNode(ViewNode, VacuumSettings): display_comments = False sql_data = '' - status, result = self._fetch_properties(did, scid, vid) + status, result = self._fetch_mview_properties(did, scid, vid) if not status: return result @@ -1889,7 +1919,7 @@ class MViewNode(ViewNode, VacuumSettings): acls = [] try: acls = render_template( - "/".join([self.template_path, 'sql/allowed_privs.json']) + "/".join([self.template_path, self._ALLOWED_PRIVS_JSON]) ) acls = json.loads(acls, encoding='utf-8') except Exception as e: @@ -1904,14 +1934,15 @@ class MViewNode(ViewNode, VacuumSettings): ) SQL = render_template("/".join( - [self.template_path, 'sql/create.sql']), + [self.template_path, self._SQL_PREFIX + self._CREATE_SQL]), data=result, conn=self.conn, display_comments=display_comments ) SQL += "\n" SQL += render_template("/".join( - [self.template_path, 'sql/grant.sql']), data=result) + [self.template_path, self._SQL_PREFIX + self._GRANT_SQL]), + data=result) sql_data += SQL sql_data += self.get_rule_sql(vid, display_comments) @@ -1963,7 +1994,7 @@ class MViewNode(ViewNode, VacuumSettings): Fetches the properties of an individual view and render in the properties tab """ - status, res = self._fetch_properties(did, scid, vid) + status, res = self._fetch_mview_properties(did, scid, vid) if not status: return res @@ -1973,7 +2004,7 @@ class MViewNode(ViewNode, VacuumSettings): status=200 ) - def _fetch_properties(self, did, scid, vid): + def _fetch_mview_properties(self, did, scid, vid): """ This function is used to fetch the properties of the specified object :param did: @@ -1982,7 +2013,7 @@ class MViewNode(ViewNode, VacuumSettings): :return: """ SQL = render_template("/".join( - [self.template_path, 'sql/properties.sql'] + [self.template_path, self._SQL_PREFIX + self._PROPERTIES_SQL] ), did=did, vid=vid, datlastsysoid=self.datlastsysoid) status, res = self.conn.execute_dict(SQL) if not status: @@ -2046,7 +2077,7 @@ class MViewNode(ViewNode, VacuumSettings): ].replace('=', ' = ') SQL = render_template("/".join( - [self.template_path, 'sql/acl.sql']), vid=vid) + [self.template_path, self._SQL_PREFIX + self._ACL_SQL]), vid=vid) status, dataclres = self.conn.execute_dict(SQL) if not status: return False, internal_server_error(errormsg=res) @@ -2208,15 +2239,15 @@ class MViewNode(ViewNode, VacuumSettings): :return: """ res = dict() - SQL = render_template("/".join([self.template_path, - 'sql/nodes.sql']), did=did, - scid=scid, datlastsysoid=self.datlastsysoid) + SQL = render_template("/".join( + [self.template_path, self._SQL_PREFIX + self._NODES_SQL]), + did=did, scid=scid, datlastsysoid=self.datlastsysoid) status, rset = self.conn.execute_2darray(SQL) if not status: return internal_server_error(errormsg=res) for row in rset['rows']: - status, data = self._fetch_properties(did, scid, row['oid']) + status, data = self._fetch_mview_properties(did, scid, row['oid']) if status: res[row['name']] = data diff --git a/web/pgadmin/browser/server_groups/servers/resource_groups/__init__.py b/web/pgadmin/browser/server_groups/servers/resource_groups/__init__.py index bd860ae4a..b78bfba02 100644 --- a/web/pgadmin/browser/server_groups/servers/resource_groups/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/resource_groups/__init__.py @@ -164,6 +164,9 @@ class ResourceGroupView(NodeView): """ node_type = blueprint.node_type + _PROPERTIES_SQL = 'properties.sql' + _CREATE_SQL = 'create.sql' + _UPDATE_SQL = 'update.sql' parent_ids = [ {'type': 'int', 'id': 'gid'}, @@ -243,7 +246,7 @@ class ResourceGroupView(NodeView): gid: Server Group ID sid: Server ID """ - sql = render_template("/".join([self.sql_path, 'properties.sql'])) + sql = render_template("/".join([self.sql_path, ])) status, res = self.conn.execute_dict(sql) if not status: @@ -327,7 +330,7 @@ class ResourceGroupView(NodeView): rg_id: Resource Group ID """ sql = render_template( - "/".join([self.sql_path, 'properties.sql']), rgid=rg_id) + "/".join([self.sql_path, self._PROPERTIES_SQL]), rgid=rg_id) status, res = self.conn.execute_dict(sql) if not status: @@ -389,7 +392,7 @@ class ResourceGroupView(NodeView): try: # Below logic will create new resource group sql = render_template( - "/".join([self.sql_path, 'create.sql']), + "/".join([self.sql_path, self._CREATE_SQL]), rgname=data['name'], conn=self.conn ) if sql and sql.strip('\n') and sql.strip(' '): @@ -401,7 +404,7 @@ class ResourceGroupView(NodeView): # resource group you can't run multiple commands in one # transaction. sql = render_template( - "/".join([self.sql_path, 'update.sql']), + "/".join([self.sql_path, self._UPDATE_SQL]), data=data, conn=self.conn ) # Checking if we are not executing empty query @@ -451,7 +454,7 @@ class ResourceGroupView(NodeView): if data['cpu_rate_limit'] != old_data['cpu_rate_limit'] or \ data['dirty_rate_limit'] != old_data['dirty_rate_limit']: sql = render_template( - "/".join([self.sql_path, 'update.sql']), + "/".join([self.sql_path, self._UPDATE_SQL]), data=data, conn=self.conn ) if sql and sql.strip('\n') and sql.strip(' '): @@ -478,7 +481,7 @@ class ResourceGroupView(NodeView): try: sql = render_template( - "/".join([self.sql_path, 'properties.sql']), rgid=rg_id) + "/".join([self.sql_path, self._PROPERTIES_SQL]), rgid=rg_id) status, res = self.conn.execute_dict(sql) if not status: return internal_server_error(errormsg=res) @@ -489,7 +492,7 @@ class ResourceGroupView(NodeView): if data['name'] != old_data['name']: sql = render_template( - "/".join([self.sql_path, 'update.sql']), + "/".join([self.sql_path, self._UPDATE_SQL]), oldname=old_data['name'], newname=data['name'], conn=self.conn ) @@ -614,7 +617,7 @@ class ResourceGroupView(NodeView): :return: """ sql = render_template( - "/".join([self.sql_path, 'properties.sql']), rgid=rg_id) + "/".join([self.sql_path, self._PROPERTIES_SQL]), rgid=rg_id) status, res = self.conn.execute_dict(sql) if not status: return internal_server_error(errormsg=res) @@ -633,7 +636,7 @@ class ResourceGroupView(NodeView): if data['name'] != old_data['name']: name_changed = True sql = render_template( - "/".join([self.sql_path, 'update.sql']), + "/".join([self.sql_path, self._UPDATE_SQL]), oldname=old_data['name'], newname=data['name'], conn=self.conn ) @@ -643,7 +646,7 @@ class ResourceGroupView(NodeView): sql += "\n-- Following query will be executed in a " \ "separate transaction\n" sql += render_template( - "/".join([self.sql_path, 'update.sql']), + "/".join([self.sql_path, self._UPDATE_SQL]), data=data, conn=self.conn ) @@ -667,7 +670,7 @@ class ResourceGroupView(NodeView): sql, old_name = self._get_update_sql(rg_id, data, required_args) else: sql = render_template( - "/".join([self.sql_path, 'create.sql']), + "/".join([self.sql_path, self._CREATE_SQL]), rgname=data['name'], conn=self.conn ) @@ -683,7 +686,7 @@ class ResourceGroupView(NodeView): sql += "\n-- Following query will be executed in a " \ "separate transaction\n" sql += render_template( - "/".join([self.sql_path, 'update.sql']), + "/".join([self.sql_path, self._UPDATE_SQL]), data=data, conn=self.conn ) @@ -700,7 +703,7 @@ class ResourceGroupView(NodeView): rg_id: Resource Group ID """ sql = render_template( - "/".join([self.sql_path, 'properties.sql']), rgid=rg_id + "/".join([self.sql_path, self._PROPERTIES_SQL]), rgid=rg_id ) status, res = self.conn.execute_dict(sql) if not status: @@ -714,13 +717,13 @@ class ResourceGroupView(NodeView): old_data = dict(res['rows'][0]) sql = render_template( - "/".join([self.sql_path, 'create.sql']), + "/".join([self.sql_path, self._CREATE_SQL]), display_comments=True, rgname=old_data['name'], conn=self.conn ) sql += "\n" sql += render_template( - "/".join([self.sql_path, 'update.sql']), + "/".join([self.sql_path, self._UPDATE_SQL]), data=old_data, conn=self.conn ) diff --git a/web/pgadmin/browser/server_groups/servers/tablespaces/__init__.py b/web/pgadmin/browser/server_groups/servers/tablespaces/__init__.py index c823cbf86..cb384c350 100644 --- a/web/pgadmin/browser/server_groups/servers/tablespaces/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/tablespaces/__init__.py @@ -316,7 +316,7 @@ class TablespaceView(PGChildNodeView): if not status: return internal_server_error(errormsg=res) SQL = render_template( - "/".join([self.template_path, 'alter.sql']), + "/".join([self.template_path, self._ALTER_SQL]), data=data, conn=self.conn ) @@ -328,7 +328,7 @@ class TablespaceView(PGChildNodeView): # To fetch the oid of newly created tablespace SQL = render_template( - "/".join([self.template_path, 'alter.sql']), + "/".join([self.template_path, self._ALTER_SQL]), tablespace=data['name'], conn=self.conn ) @@ -533,7 +533,7 @@ class TablespaceView(PGChildNodeView): ) SQL += "\n" SQL += render_template( - "/".join([self.template_path, 'alter.sql']), + "/".join([self.template_path, self._ALTER_SQL]), data=data, conn=self.conn ) SQL = re.sub('\n{2,}', '\n\n', SQL) @@ -574,7 +574,7 @@ class TablespaceView(PGChildNodeView): ) SQL += "\n" SQL += render_template( - "/".join([self.template_path, 'alter.sql']), + "/".join([self.template_path, self._ALTER_SQL]), data=old_data, conn=self.conn ) diff --git a/web/pgadmin/browser/utils.py b/web/pgadmin/browser/utils.py index ab0e86a02..d14c8cf5c 100644 --- a/web/pgadmin/browser/utils.py +++ b/web/pgadmin/browser/utils.py @@ -375,12 +375,19 @@ class PGChildNodeView(NodeView): _NODES_SQL = 'nodes.sql' _CREATE_SQL = 'create.sql' _UPDATE_SQL = 'update.sql' + _ALTER_SQL = 'alter.sql' _PROPERTIES_SQL = 'properties.sql' _DELETE_SQL = 'delete.sql' _GRANT_SQL = 'grant.sql' _SCHEMA_SQL = 'schema.sql' _ACL_SQL = 'acl.sql' _OID_SQL = 'get_oid.sql' + _FUNCTIONS_SQL = 'functions.sql' + _GET_CONSTRAINTS_SQL = 'get_constraints.sql' + _GET_TABLES_SQL = 'get_tables.sql' + _GET_DEFINITION_SQL = 'get_definition.sql' + _GET_SCHEMA_OID_SQL = 'get_schema_oid.sql' + _GET_COLUMNS_SQL = 'get_columns.sql' def get_children_nodes(self, manager, **kwargs): """ diff --git a/web/pgadmin/tools/debugger/__init__.py b/web/pgadmin/tools/debugger/__init__.py index 27583bf2a..9b5bf5ddd 100644 --- a/web/pgadmin/tools/debugger/__init__.py +++ b/web/pgadmin/tools/debugger/__init__.py @@ -36,6 +36,9 @@ MODULE_NAME = 'debugger' # Constants ASYNC_OK = 1 +DEBUGGER_SQL_PATH = 'debugger/sql' +DEBUGGER_SQL_V1_PATH = 'debugger/sql/v1' +DEBUGGER_SQL_V2_PATH = 'debugger/sql/v2' class DebuggerModule(PgAdminModule): @@ -353,13 +356,10 @@ def init_function(node_type, sid, did, scid, fid, trid=None): else: is_proc_supported = True if manager.version >= 110000 else False - # Set the template path required to read the sql files - template_path = 'debugger/sql' - if node_type == 'trigger': # Find trigger function id from trigger id sql = render_template( - "/".join([template_path, 'get_trigger_function_info.sql']), + "/".join([DEBUGGER_SQL_PATH, 'get_trigger_function_info.sql']), table_id=fid, trigger_id=trid ) @@ -378,7 +378,7 @@ def init_function(node_type, sid, did, scid, fid, trid=None): sql = '' sql = render_template( - "/".join([template_path, 'get_function_debug_info.sql']), + "/".join([DEBUGGER_SQL_PATH, 'get_function_debug_info.sql']), is_ppas_database=ppas_server, hasFeatureFunctionDefaults=True, fid=fid, @@ -738,13 +738,10 @@ def initialize_target(debug_type, trans_id, sid, did, if not status: return error - # Set the template path required to read the sql files - template_path = 'debugger/sql' - if tri_id is not None: # Find trigger function id from trigger id sql = render_template( - "/".join([template_path, 'get_trigger_function_info.sql']), + "/".join([DEBUGGER_SQL_PATH, 'get_trigger_function_info.sql']), table_id=func_id, trigger_id=tri_id ) @@ -928,9 +925,9 @@ def start_debugger_listener(trans_id): # find the debugger version and execute the query accordingly dbg_version = de_inst.debugger_data['debugger_version'] if dbg_version <= 2: - template_path = 'debugger/sql/v1' + template_path = DEBUGGER_SQL_V1_PATH else: - template_path = 'debugger/sql/v2' + template_path = DEBUGGER_SQL_V2_PATH # If user again start the same debug function with different arguments # then we need to save that values to session variable and database. @@ -1016,7 +1013,7 @@ def start_debugger_listener(trans_id): # Below are two different template to execute and start executer if manager.server_type != 'pg' and manager.version < 90300: str_query = render_template( - "/".join(['debugger/sql', 'execute_edbspl.sql']), + "/".join([DEBUGGER_SQL_PATH, 'execute_edbspl.sql']), func_name=func_name, is_func=de_inst.function_data['is_func'], lan_name=de_inst.function_data['language'], @@ -1027,7 +1024,7 @@ def start_debugger_listener(trans_id): ) else: str_query = render_template( - "/".join(['debugger/sql', 'execute_plpgsql.sql']), + "/".join([DEBUGGER_SQL_PATH, 'execute_plpgsql.sql']), func_name=func_name, is_func=de_inst.function_data['is_func'], ret_type=de_inst.function_data['return_type'], @@ -1160,9 +1157,9 @@ def execute_debugger_query(trans_id, query_type): conn_id=de_inst.debugger_data['exe_conn_id']) # find the debugger version and execute the query accordingly - template_path = 'debugger/sql/v1' \ + template_path = DEBUGGER_SQL_V1_PATH \ if de_inst.debugger_data['debugger_version'] <= 2 \ - else 'debugger/sql/v2' + else DEBUGGER_SQL_V2_PATH if not conn.connected(): result = gettext('Not connected to server or connection ' @@ -1319,9 +1316,9 @@ def start_execution(trans_id, port_num): # find the debugger version and execute the query accordingly dbg_version = de_inst.debugger_data['debugger_version'] if dbg_version <= 2: - template_path = 'debugger/sql/v1' + template_path = DEBUGGER_SQL_V1_PATH else: - template_path = 'debugger/sql/v2' + template_path = DEBUGGER_SQL_V2_PATH # connect to port and store the session ID in the session variables sql = render_template( @@ -1388,9 +1385,9 @@ def set_clear_breakpoint(trans_id, line_no, set_type): # find the debugger version and execute the query accordingly dbg_version = de_inst.debugger_data['debugger_version'] if dbg_version <= 2: - template_path = 'debugger/sql/v1' + template_path = DEBUGGER_SQL_V1_PATH else: - template_path = 'debugger/sql/v2' + template_path = DEBUGGER_SQL_V2_PATH query_type = '' @@ -1475,9 +1472,9 @@ def clear_all_breakpoint(trans_id): # find the debugger version and execute the query accordingly dbg_version = de_inst.debugger_data['debugger_version'] if dbg_version <= 2: - template_path = 'debugger/sql/v1' + template_path = DEBUGGER_SQL_V1_PATH else: - template_path = 'debugger/sql/v2' + template_path = DEBUGGER_SQL_V2_PATH if conn.connected(): # get the data sent through post from client @@ -1541,9 +1538,9 @@ def deposit_parameter_value(trans_id): # find the debugger version and execute the query accordingly dbg_version = de_inst.debugger_data['debugger_version'] if dbg_version <= 2: - template_path = 'debugger/sql/v1' + template_path = DEBUGGER_SQL_V1_PATH else: - template_path = 'debugger/sql/v2' + template_path = DEBUGGER_SQL_V2_PATH if conn.connected(): # get the data sent through post from client @@ -1622,9 +1619,9 @@ def select_frame(trans_id, frame_id): # find the debugger version and execute the query accordingly dbg_version = de_inst.debugger_data['debugger_version'] if dbg_version <= 2: - template_path = 'debugger/sql/v1' + template_path = DEBUGGER_SQL_V1_PATH else: - template_path = 'debugger/sql/v2' + template_path = DEBUGGER_SQL_V2_PATH de_inst.debugger_data['frame_id'] = frame_id de_inst.update_session()