Fixed cognitive complexity code smell reported by SonarQube.

pull/33/head
Nikhil Mohite 2020-07-07 15:19:47 +05:30 committed by Akshay Joshi
parent 58b4c45d0c
commit aee6bac5ef
2 changed files with 96 additions and 70 deletions

View File

@ -536,6 +536,22 @@ class IndexesView(PGChildNodeView, SchemaDiffObjectCompare):
return True, data return True, data
@staticmethod
def _check_for_error(required_args, data):
for arg in required_args:
err_msg = None
if arg == 'columns' and len(data['columns']) < 1:
err_msg = gettext("You must provide one or more column to "
"create index.")
if arg not in data:
err_msg = gettext("Could not find the required parameter ({})"
".").format(required_args[arg])
# Check if we have at least one column
if err_msg is not None:
return True, err_msg
return False, ''
@check_precondition @check_precondition
def create(self, gid, sid, did, scid, tid): def create(self, gid, sid, did, scid, tid):
""" """
@ -568,17 +584,8 @@ class IndexesView(PGChildNodeView, SchemaDiffObjectCompare):
'columns': 'Columns' 'columns': 'Columns'
} }
for arg in required_args: is_error, err_msg = IndexesView._check_for_error(required_args, data)
err_msg = None if is_error:
if arg == 'columns' and len(data['columns']) < 1:
err_msg = gettext("You must provide one or more column to "
"create index.")
if arg not in data:
err_msg = gettext("Could not find the required parameter ({})"
".").format(required_args[arg])
# Check if we have at least one column
if err_msg is not None:
return make_json_response( return make_json_response(
status=410, status=410,
success=0, success=0,
@ -686,8 +693,7 @@ class IndexesView(PGChildNodeView, SchemaDiffObjectCompare):
status, res = self.conn.execute_dict(SQL) status, res = self.conn.execute_dict(SQL)
if not status: if not status:
return internal_server_error(errormsg=res) return internal_server_error(errormsg=res)
elif not res['rows']:
if not res['rows']:
return make_json_response( return make_json_response(
success=0, success=0,
errormsg=gettext( errormsg=gettext(
@ -1018,6 +1024,22 @@ class IndexesView(PGChildNodeView, SchemaDiffObjectCompare):
return res return res
@staticmethod
def _check_for_create_req(required_create_keys, diff_dict):
create_req = False
for key in required_create_keys:
if key in diff_dict and \
((key == 'columns' and
(('added' in diff_dict[key] and
len(diff_dict[key]['added']) > 0) or
('changed' in diff_dict[key] and
len(diff_dict[key]['changed']) > 0) or
('deleted' in diff_dict[key] and
len(diff_dict[key]['deleted']) > 0))) or
key != 'columns'):
create_req = True
return create_req
def ddl_compare(self, **kwargs): def ddl_compare(self, **kwargs):
""" """
This function returns the DDL/DML statements based on the This function returns the DDL/DML statements based on the
@ -1055,19 +1077,11 @@ class IndexesView(PGChildNodeView, SchemaDiffObjectCompare):
) )
required_create_keys = ['columns'] required_create_keys = ['columns']
create_req = False
for key in required_create_keys: create_req = IndexesView._check_for_create_req(
if key in diff_dict and \ required_create_keys,
((key == 'columns' and diff_dict
(('added' in diff_dict[key] and )
len(diff_dict[key]['added']) > 0) or
('changed' in diff_dict[key] and
len(diff_dict[key]['changed']) > 0) or
('deleted' in diff_dict[key] and
len(diff_dict[key]['deleted']) > 0))) or
key != 'columns'):
create_req = True
if create_req: if create_req:
diff = self.get_sql_from_index_diff(sid=src_params['sid'], diff = self.get_sql_from_index_diff(sid=src_params['sid'],

View File

@ -59,6 +59,22 @@ def get_parent(conn, tid, template_path=None):
return schema, table return schema, table
def _get_column_property_display_data(row, col_str, data):
if row['collnspname']:
col_str += ' COLLATE ' + row['collnspname']
if row['opcname']:
col_str += ' ' + row['opcname']
# ASC/DESC and NULLS works only with btree indexes
if 'amname' in data and data['amname'] == 'btree':
# Append sort order
col_str += ' ' + row['options'][0]
# Append nulls value
col_str += ' ' + row['options'][1]
return col_str
@get_template_path @get_template_path
def get_column_details(conn, idx, data, mode='properties', template_path=None): def get_column_details(conn, idx, data, mode='properties', template_path=None):
""" """
@ -108,17 +124,7 @@ def get_column_details(conn, idx, data, mode='properties', template_path=None):
# We need same data as string to display in properties window # We need same data as string to display in properties window
# If multiple column then separate it by colon # If multiple column then separate it by colon
cols_str = row['attdef'] cols_str = row['attdef']
if row['collnspname']: cols_str += _get_column_property_display_data(row, cols_str, data)
cols_str += ' COLLATE ' + row['collnspname']
if row['opcname']:
cols_str += ' ' + row['opcname']
# ASC/DESC and NULLS works only with btree indexes
if 'amname' in data and data['amname'] == 'btree':
# Append sort order
cols_str += ' ' + row['options'][0]
# Append nulls value
cols_str += ' ' + row['options'][1]
cols.append(cols_str) cols.append(cols_str)
@ -156,6 +162,36 @@ def get_include_details(conn, idx, data, template_path=None):
return data return data
def _get_sql_with_index_none(data, template_path, conn, mode, name):
required_args = {
'name': 'Name',
'columns': 'Columns'
}
for arg in required_args:
err = False
if arg == 'columns' and len(data['columns']) < 1:
err = True
if arg not in data:
err = True
# Check if we have at least one column
if err:
return _('-- definition incomplete'), name
# If the request for new object which do not have did
sql = render_template(
"/".join([template_path, 'create.sql']),
data=data, conn=conn, mode=mode
)
sql += "\n"
sql += render_template(
"/".join([template_path, 'alter.sql']),
data=data, conn=conn
)
return sql
@get_template_path @get_template_path
def get_sql(conn, data, did, tid, idx, datlastsysoid, def get_sql(conn, data, did, tid, idx, datlastsysoid,
mode=None, template_path=None): mode=None, template_path=None):
@ -174,11 +210,11 @@ def get_sql(conn, data, did, tid, idx, datlastsysoid,
""" """
name = data['name'] if 'name' in data else None name = data['name'] if 'name' in data else None
if idx is not None: if idx is not None:
SQL = render_template("/".join([template_path, 'properties.sql']), sql = render_template("/".join([template_path, 'properties.sql']),
did=did, tid=tid, idx=idx, did=did, tid=tid, idx=idx,
datlastsysoid=datlastsysoid) datlastsysoid=datlastsysoid)
status, res = conn.execute_dict(SQL) status, res = conn.execute_dict(sql)
if not status: if not status:
return internal_server_error(errormsg=res) return internal_server_error(errormsg=res)
@ -202,38 +238,14 @@ def get_sql(conn, data, did, tid, idx, datlastsysoid,
if 'name' not in data: if 'name' not in data:
name = data['name'] = old_data['name'] name = data['name'] = old_data['name']
SQL = render_template( sql = render_template(
"/".join([template_path, 'update.sql']), "/".join([template_path, 'update.sql']),
data=data, o_data=old_data, conn=conn data=data, o_data=old_data, conn=conn
) )
else: else:
required_args = { sql = _get_sql_with_index_none(data, template_path, conn, mode, name)
'name': 'Name',
'columns': 'Columns'
}
for arg in required_args:
err = False
if arg == 'columns' and len(data['columns']) < 1:
err = True
if arg not in data: return sql, name
err = True
# Check if we have at least one column
if err:
return _('-- definition incomplete'), name
# If the request for new object which do not have did
SQL = render_template(
"/".join([template_path, 'create.sql']),
data=data, conn=conn, mode=mode
)
SQL += "\n"
SQL += render_template(
"/".join([template_path, 'alter.sql']),
data=data, conn=conn
)
return SQL, name
@get_template_path @get_template_path