Fixed cognitive complexity issues reported by SonarQube.

This commit is contained in:
Aditya Toshniwal
2020-09-03 18:10:57 +05:30
committed by Akshay Joshi
parent a67f6e4f9c
commit 974d395a48
3 changed files with 198 additions and 188 deletions

View File

@@ -174,6 +174,79 @@ def acl_list(sid, did):
mimetype="application/json")
def _get_rows_for_type(conn, ntype, server_prop, node_id):
"""
Used internally by properties to get rows for an object type
:param conn: connection object
:param ntype: object type
:param server_prop: server properties
:param node_id: oid
:return: status, execute response
"""
function_sql_url = '/sql/function.sql'
status, res = True, []
if ntype in ['function']:
sql = render_template("/".join(
[server_prop['template_path'], function_sql_url]),
node_id=node_id, type='function')
status, res = conn.execute_dict(sql)
# Fetch procedures only if server type is EPAS or PG >= 11
elif len(server_prop) > 0 and (
server_prop['server_type'] == 'ppas' or (
server_prop['server_type'] == 'pg' and
server_prop['version'] >= 11000
)
) and ntype in ['procedure']:
sql = render_template("/".join(
[server_prop['template_path'], function_sql_url]),
node_id=node_id, type='procedure')
status, res = conn.execute_dict(sql)
# Fetch trigger functions
elif ntype in ['trigger_function']:
sql = render_template("/".join(
[server_prop['template_path'], function_sql_url]),
node_id=node_id, type='trigger_function')
status, res = conn.execute_dict(sql)
# Fetch Sequences against schema
elif ntype in ['sequence']:
sql = render_template("/".join(
[server_prop['template_path'], '/sql/sequence.sql']),
node_id=node_id)
status, res = conn.execute_dict(sql)
# Fetch Tables against schema
elif ntype in ['table']:
sql = render_template("/".join(
[server_prop['template_path'], '/sql/table.sql']),
node_id=node_id)
status, res = conn.execute_dict(sql)
# Fetch Views against schema
elif ntype in ['view']:
sql = render_template("/".join(
[server_prop['template_path'], '/sql/view.sql']),
node_id=node_id, node_type='v')
status, res = conn.execute_dict(sql)
# Fetch Materialzed Views against schema
elif ntype in ['mview']:
sql = render_template("/".join(
[server_prop['template_path'], '/sql/view.sql']),
node_id=node_id, node_type='m')
status, res = conn.execute_dict(sql)
return status, res
@blueprint.route(
'/<int:sid>/<int:did>/<int:node_id>/<node_type>/',
methods=['GET'], endpoint='objects'
@@ -185,7 +258,6 @@ def properties(sid, did, node_id, node_type):
and render into selection page of wizard
"""
function_sql_url = '/sql/function.sql'
get_schema_sql_url = '/sql/get_schemas.sql'
# unquote encoded url parameter
@@ -198,130 +270,66 @@ def properties(sid, did, node_id, node_type):
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid)
conn = manager.connection(did=did)
node_types = []
show_sysobj = blueprint.show_system_objects().get()
if node_type == 'database':
# Fetch list of schemas
# Get sys_obj_values and get list of schemas
ntype = 'schema'
sql = render_template("/".join(
[server_prop['template_path'], get_schema_sql_url]),
show_sysobj=show_sysobj)
status, res = conn.execute_dict(sql)
if not status:
return internal_server_error(errormsg=res)
node_types = res['rows']
ntype = 'schema'
else:
sql = render_template("/".join(
[server_prop['template_path'], get_schema_sql_url]),
nspid=node_id, show_sysobj=False)
status, res = conn.execute_dict(sql)
if not status:
return internal_server_error(errormsg=res)
node_types = res['rows']
show_sysobj=show_sysobj, nspid=node_id)
ntype = node_type
status, res = conn.execute_dict(sql)
if not status:
return internal_server_error(errormsg=res)
node_types = res['rows']
def _append_rows(status, res, disp_type):
if not status:
current_app.logger.error(res)
failed_objects.append(disp_type)
else:
res_data.extend(res['rows'])
for row in node_types:
if 'oid' in row:
node_id = row['oid']
# Fetch functions against schema
if ntype in ['schema', 'function']:
sql = render_template("/".join(
[server_prop['template_path'], function_sql_url]),
node_id=node_id, type='function')
if ntype == 'schema':
status, res = _get_rows_for_type(
conn, 'function', server_prop, node_id)
_append_rows(status, res, 'function')
status, res = conn.execute_dict(sql)
if not status:
current_app.logger.error(res)
failed_objects.append('function')
else:
res_data.extend(res['rows'])
status, res = _get_rows_for_type(
conn, 'procedure', server_prop, node_id)
_append_rows(status, res, 'procedure')
# Fetch procedures only if server type is EPAS or PG >= 11
if (len(server_prop) > 0 and
(server_prop['server_type'] == 'ppas' or
(server_prop['server_type'] == 'pg' and
server_prop['version'] >= 11000)) and
ntype in ['schema', 'procedure']):
sql = render_template("/".join(
[server_prop['template_path'], function_sql_url]),
node_id=node_id, type='procedure')
status, res = _get_rows_for_type(
conn, 'trigger_function', server_prop, node_id)
_append_rows(status, res, 'trigger function')
status, res = conn.execute_dict(sql)
status, res = _get_rows_for_type(
conn, 'sequence', server_prop, node_id)
_append_rows(status, res, 'sequence')
if not status:
current_app.logger.error(res)
failed_objects.append('procedure')
else:
res_data.extend(res['rows'])
status, res = _get_rows_for_type(
conn, 'table', server_prop, node_id)
_append_rows(status, res, 'table')
# Fetch trigger functions
if ntype in ['schema', 'trigger_function']:
sql = render_template("/".join(
[server_prop['template_path'], function_sql_url]),
node_id=node_id, type='trigger_function')
status, res = conn.execute_dict(sql)
status, res = _get_rows_for_type(
conn, 'view', server_prop, node_id)
_append_rows(status, res, 'view')
if not status:
current_app.logger.error(res)
failed_objects.append('trigger function')
else:
res_data.extend(res['rows'])
# Fetch Sequences against schema
if ntype in ['schema', 'sequence']:
sql = render_template("/".join(
[server_prop['template_path'], '/sql/sequence.sql']),
node_id=node_id)
status, res = conn.execute_dict(sql)
if not status:
current_app.logger.error(res)
failed_objects.append('sequence')
else:
res_data.extend(res['rows'])
# Fetch Tables against schema
if ntype in ['schema', 'table']:
sql = render_template("/".join(
[server_prop['template_path'], '/sql/table.sql']),
node_id=node_id)
status, res = conn.execute_dict(sql)
if not status:
current_app.logger.error(res)
failed_objects.append('table')
else:
res_data.extend(res['rows'])
# Fetch Views against schema
if ntype in ['schema', 'view']:
sql = render_template("/".join(
[server_prop['template_path'], '/sql/view.sql']),
node_id=node_id, node_type='v')
status, res = conn.execute_dict(sql)
if not status:
current_app.logger.error(res)
failed_objects.append('view')
else:
res_data.extend(res['rows'])
# Fetch Materialzed Views against schema
if ntype in ['schema', 'mview']:
sql = render_template("/".join(
[server_prop['template_path'], '/sql/view.sql']),
node_id=node_id, node_type='m')
status, res = conn.execute_dict(sql)
if not status:
current_app.logger.error(res)
failed_objects.append('materialized view')
else:
res_data.extend(res['rows'])
status, res = _get_rows_for_type(
conn, 'mview', server_prop, node_id)
_append_rows(status, res, 'materialized view')
else:
status, res = _get_rows_for_type(conn, ntype, server_prop, node_id)
_append_rows(status, res, 'function')
msg = None
if len(failed_objects) > 0: