Fixed Cognitive Complexity code smell reproted by SonarQube.

This commit is contained in:
Nikhil Mohite 2020-07-03 11:15:41 +05:30 committed by Akshay Joshi
parent 2ec8051303
commit 1ab257d512
6 changed files with 151 additions and 112 deletions

View File

@ -568,8 +568,7 @@ class FtsConfigurationView(PGChildNodeView, SchemaDiffObjectCompare):
status, res = self.conn.execute_dict(sql)
if not status:
return internal_server_error(errormsg=res)
if not res['rows']:
elif not res['rows']:
return make_json_response(
success=0,
errormsg=_(
@ -643,6 +642,29 @@ class FtsConfigurationView(PGChildNodeView, SchemaDiffObjectCompare):
status=200
)
def _get_sql_for_create(self, data, schema):
# Replace schema oid with schema name
new_data = data.copy()
new_data['schema'] = schema
if (
'name' in new_data and
'schema' in new_data
):
sql = render_template("/".join([self.template_path,
'create.sql']),
data=new_data,
conn=self.conn
)
else:
sql = u"-- definition incomplete"
return sql
@staticmethod
def _replace_schema_oid_with_schema_name(new_schema, new_data):
if 'schema' in new_data:
new_data['schema'] = new_schema
def get_sql(self, gid, sid, did, scid, data, cfgid=None):
"""
This function will return SQL for model data
@ -663,8 +685,7 @@ class FtsConfigurationView(PGChildNodeView, SchemaDiffObjectCompare):
status, res = self.conn.execute_dict(sql)
if not status:
return internal_server_error(errormsg=res)
if len(res['rows']) == 0:
elif len(res['rows']) == 0:
return gone(_("Could not find the FTS Configuration node."))
old_data = res['rows'][0]
@ -681,10 +702,9 @@ class FtsConfigurationView(PGChildNodeView, SchemaDiffObjectCompare):
if not status:
return internal_server_error(errormsg=new_schema)
# Replace schema oid with schema name
new_data = data.copy()
if 'schema' in new_data:
new_data['schema'] = new_schema
# Replace schema oid with schema name
self._replace_schema_oid_with_schema_name(new_schema, new_data)
# Fetch old schema name using old schema oid
sql = render_template(
@ -719,21 +739,7 @@ class FtsConfigurationView(PGChildNodeView, SchemaDiffObjectCompare):
if not status:
return internal_server_error(errormsg=schema)
# Replace schema oid with schema name
new_data = data.copy()
new_data['schema'] = schema
if (
'name' in new_data and
'schema' in new_data
):
sql = render_template("/".join([self.template_path,
'create.sql']),
data=new_data,
conn=self.conn
)
else:
sql = u"-- definition incomplete"
sql = self._get_sql_for_create(data, schema)
return sql.strip('\n'), data['name']
@check_precondition

View File

@ -564,8 +564,7 @@ class FtsDictionaryView(PGChildNodeView, SchemaDiffObjectCompare):
status, res = self.conn.execute_dict(sql)
if not status:
return internal_server_error(errormsg=res)
if not res['rows']:
elif not res['rows']:
return make_json_response(
success=0,
errormsg=_(
@ -639,6 +638,37 @@ class FtsDictionaryView(PGChildNodeView, SchemaDiffObjectCompare):
status=200
)
def _get_sql_for_create(self, data, schema):
# Replace schema oid with schema name
new_data = data.copy()
new_data['schema'] = schema
if (
'template' in new_data and
'name' in new_data and
'schema' in new_data
):
sql = render_template("/".join([self.template_path,
'create.sql']),
data=new_data,
conn=self.conn
)
else:
sql = u"-- definition incomplete"
return sql
def _check_template_name_and_schema_name(self, data, old_data):
if 'schema' not in data:
data['schema'] = old_data['schema']
# Handle templates and its schema name properly
if old_data['template_schema'] is not None and \
old_data['template_schema'] != "pg_catalog":
old_data['template'] = self.qtIdent(
self.conn, old_data['template_schema'],
old_data['template']
)
def get_sql(self, gid, sid, did, scid, data, dcid=None):
"""
This function will return SQL for model data
@ -660,21 +690,11 @@ class FtsDictionaryView(PGChildNodeView, SchemaDiffObjectCompare):
status, res = self.conn.execute_dict(sql)
if not status:
return internal_server_error(errormsg=res)
if len(res['rows']) == 0:
elif len(res['rows']) == 0:
return gone(_("Could not find the FTS Dictionary node."))
old_data = res['rows'][0]
if 'schema' not in data:
data['schema'] = old_data['schema']
# Handle templates and its schema name properly
if old_data['template_schema'] is not None and \
old_data['template_schema'] != "pg_catalog":
old_data['template'] = self.qtIdent(
self.conn, old_data['template_schema'],
old_data['template']
)
self._check_template_name_and_schema_name(data, old_data)
# If user has changed the schema then fetch new schema directly
# using its oid otherwise fetch old schema name using its oid
@ -721,22 +741,7 @@ class FtsDictionaryView(PGChildNodeView, SchemaDiffObjectCompare):
if not status:
return internal_server_error(errormsg=schema)
# Replace schema oid with schema name
new_data = data.copy()
new_data['schema'] = schema
if (
'template' in new_data and
'name' in new_data and
'schema' in new_data
):
sql = render_template("/".join([self.template_path,
'create.sql']),
data=new_data,
conn=self.conn
)
else:
sql = u"-- definition incomplete"
sql = self._get_sql_for_create(data, schema)
return sql.strip('\n'), data['name']
@check_precondition

View File

@ -511,8 +511,7 @@ class FtsParserView(PGChildNodeView, SchemaDiffObjectCompare):
status, res = self.conn.execute_dict(sql)
if not status:
return internal_server_error(errormsg=res)
if not res['rows']:
elif not res['rows']:
return make_json_response(
success=0,
errormsg=_(
@ -584,6 +583,33 @@ class FtsParserView(PGChildNodeView, SchemaDiffObjectCompare):
status=200
)
@staticmethod
def _replace_schema_oid_with_name(new_data, new_schema):
if 'schema' in new_data:
new_data['schema'] = new_schema
def _get_sql_for_create(self, data, schema):
# Replace schema oid with schema name
new_data = data.copy()
new_data['schema'] = schema
if (
'prsstart' in new_data and
'prstoken' in new_data and
'prsend' in new_data and
'prslextype' in new_data and
'name' in new_data and
'schema' in new_data
):
sql = render_template(
"/".join([self.template_path, 'create.sql']),
data=new_data,
conn=self.conn
)
else:
sql = "-- definition incomplete"
return sql
def get_sql(self, gid, sid, did, scid, data, pid=None):
"""
This function will return SQL for model data
@ -605,8 +631,7 @@ class FtsParserView(PGChildNodeView, SchemaDiffObjectCompare):
status, res = self.conn.execute_dict(sql)
if not status:
return internal_server_error(errormsg=res)
if len(res['rows']) == 0:
elif len(res['rows']) == 0:
return gone(_("Could not find the FTS Parser node."))
old_data = res['rows'][0]
@ -625,8 +650,7 @@ class FtsParserView(PGChildNodeView, SchemaDiffObjectCompare):
# Replace schema oid with schema name
new_data = data.copy()
if 'schema' in new_data:
new_data['schema'] = new_schema
FtsParserView._replace_schema_oid_with_name(new_data, new_schema)
# Fetch old schema name using old schema oid
sql = render_template(
@ -661,25 +685,7 @@ class FtsParserView(PGChildNodeView, SchemaDiffObjectCompare):
if not status:
return internal_server_error(errormsg=schema)
# Replace schema oid with schema name
new_data = data.copy()
new_data['schema'] = schema
if (
'prsstart' in new_data and
'prstoken' in new_data and
'prsend' in new_data and
'prslextype' in new_data and
'name' in new_data and
'schema' in new_data
):
sql = render_template(
"/".join([self.template_path, 'create.sql']),
data=new_data,
conn=self.conn
)
else:
sql = "-- definition incomplete"
sql = self._get_sql_for_create(data, schema)
return sql.strip('\n'), data['name']
@check_precondition

View File

@ -543,6 +543,45 @@ class FtsTemplateView(PGChildNodeView, SchemaDiffObjectCompare):
status=200
)
def _replace_schema_oid_with_name(self, new_schema, old_data,
new_data):
# Replace schema oid with schema name
if 'schema' in new_data:
new_data['schema'] = new_schema
# Fetch old schema name using old schema oid
sql = render_template(
"/".join([self.template_path, 'schema.sql']),
data=old_data)
status, old_schema = self.conn.execute_scalar(sql)
if not status:
return True, old_schema
# Replace old schema oid with old schema name
old_data['schema'] = old_schema
return False, ''
def _get_sql_for_create(self, data, schema):
# Replace schema oid with schema name
new_data = data.copy()
new_data['schema'] = schema
if (
'tmpllexize' in new_data and
'name' in new_data and
'schema' in new_data
):
sql = render_template("/".join([self.template_path,
'create.sql']),
data=new_data,
conn=self.conn
)
else:
sql = u"-- definition incomplete"
return sql
def get_sql(self, gid, sid, did, scid, data, tid=None):
"""
This function will return SQL for model data
@ -550,6 +589,7 @@ class FtsTemplateView(PGChildNodeView, SchemaDiffObjectCompare):
:param sid: server id
:param did: database id
:param scid: schema id
:param data: sql data
:param tid: fts tempate id
"""
@ -564,7 +604,7 @@ class FtsTemplateView(PGChildNodeView, SchemaDiffObjectCompare):
status, res = self.conn.execute_dict(sql)
if not status:
return internal_server_error(errormsg=res)
if len(res['rows']) == 0:
elif len(res['rows']) == 0:
return gone(
gettext("Could not find the requested FTS template.")
)
@ -586,25 +626,18 @@ class FtsTemplateView(PGChildNodeView, SchemaDiffObjectCompare):
# Replace schema oid with schema name
new_data = data.copy()
if 'schema' in new_data:
new_data['schema'] = new_schema
# Fetch old schema name using old schema oid
sql = render_template(
"/".join([self.template_path, 'schema.sql']),
data=old_data)
status, old_schema = self.conn.execute_scalar(sql)
if not status:
return internal_server_error(errormsg=old_schema)
# Replace old schema oid with old schema name
old_data['schema'] = old_schema
error, errmsg = self._replace_schema_oid_with_name(new_schema,
old_data,
new_data)
if error:
print('ERROR INSIDE UPDATE:: {0}'.format(errmsg))
return internal_server_error(errormsg=errmsg)
sql = render_template(
"/".join([self.template_path, 'update.sql']),
data=new_data, o_data=old_data
)
# Fetch sql query for modified data
if 'name' in data:
return sql.strip('\n'), data['name']
@ -618,22 +651,7 @@ class FtsTemplateView(PGChildNodeView, SchemaDiffObjectCompare):
if not status:
return internal_server_error(errormsg=schema)
# Replace schema oid with schema name
new_data = data.copy()
new_data['schema'] = schema
if (
'tmpllexize' in new_data and
'name' in new_data and
'schema' in new_data
):
sql = render_template("/".join([self.template_path,
'create.sql']),
data=new_data,
conn=self.conn
)
else:
sql = u"-- definition incomplete"
sql = self._get_sql_for_create(data, schema)
return sql.strip('\n'), data['name']
@check_precondition

View File

@ -970,6 +970,7 @@ class FunctionView(PGChildNodeView, DataTypeReader, SchemaDiffObjectCompare):
if 'argtype' in a:
args += a['argtype']
args_without_name.append(a['argtype'])
return args, args_without_name
def _get_arguments(self, args_list, args, args_without_name):
cnt = 1
@ -989,7 +990,11 @@ class FunctionView(PGChildNodeView, DataTypeReader, SchemaDiffObjectCompare):
args += self.qtIdent(
self.conn, a['argname']) + " "
FunctionView._check_argtype(args, args_without_name, a)
args, args_without_name = FunctionView._check_argtype(
args,
args_without_name,
a
)
if cnt < len(args_list):
args += ', '
@ -1194,7 +1199,6 @@ class FunctionView(PGChildNodeView, DataTypeReader, SchemaDiffObjectCompare):
@staticmethod
def _prepare_final_dict(data, old_data, chngd_variables, del_variables,
all_ids_dict):
# In case of schema diff we don't want variables from
# old data
if not all_ids_dict['is_schema_diff']:

View File

@ -1357,8 +1357,8 @@ define('pgadmin.browser.node', [
}.bind(panel),
informBeforeAttributeChange = function(ok_callback) {
var j = this.$container.find('.obj_properties').first();
view = j && j.data('obj-view');
var obj = this.$container.find('.obj_properties').first();
view = obj && obj .data('obj-view');
if (view && view.model && !_.isUndefined(view.model.inform_text) && !_.isNull(view.model.inform_text)) {
Alertify.alert(