Refactor datatables static methods

This commit is contained in:
JonnyWong16 2021-04-02 11:42:48 -07:00
parent b32b1bae7c
commit 9b4f4b8ae4
No known key found for this signature in database
GPG key ID: B1F1F9807184697A

View file

@ -63,22 +63,22 @@ class DataTables(object):
'named json_data.') 'named json_data.')
return None return None
extracted_columns = self.extract_columns(columns=columns) extracted_columns = extract_columns(columns=columns)
join = self.build_join(join_types, join_tables, join_evals) join = build_join(join_types, join_tables, join_evals)
group = self.build_grouping(group_by) group = build_grouping(group_by)
c_where, cw_args = self.build_custom_where(custom_where) c_where, cw_args = build_custom_where(custom_where)
order = self.build_order(parameters['order'], order = build_order(parameters['order'],
extracted_columns['column_named'], extracted_columns['column_named'],
parameters['columns']) parameters['columns'])
where, w_args = self.build_where(parameters['search']['value'], where, w_args = build_where(parameters['search']['value'],
extracted_columns['column_named'], extracted_columns['column_named'],
parameters['columns']) parameters['columns'])
# Build union parameters # Build union parameters
if table_name_union: if table_name_union:
extracted_columns_union = self.extract_columns(columns=columns_union) extracted_columns_union = extract_columns(columns=columns_union)
group_u = self.build_grouping(group_by_union) group_u = build_grouping(group_by_union)
c_where_u, cwu_args = self.build_custom_where(custom_where_union) c_where_u, cwu_args = build_custom_where(custom_where_union)
union = 'UNION SELECT %s FROM %s %s %s' % (extracted_columns_union['column_string'], union = 'UNION SELECT %s FROM %s %s %s' % (extracted_columns_union['column_string'],
table_name_union, table_name_union,
c_where_u, c_where_u,
@ -117,7 +117,8 @@ class DataTables(object):
return output return output
def build_grouping(self, group_by=[]):
def build_grouping(group_by=[]):
# Build grouping # Build grouping
group = '' group = ''
@ -128,7 +129,8 @@ class DataTables(object):
return group return group
def build_join(self, join_types=[], join_tables=[], join_evals=[]):
def build_join(join_types=[], join_tables=[], join_evals=[]):
# Build join parameters # Build join parameters
join = '' join = ''
@ -140,40 +142,48 @@ class DataTables(object):
return join return join
def build_custom_where(self, custom_where=[]):
def build_custom_where(custom_where=[]):
# Build custom where parameters # Build custom where parameters
c_where = '' c_where = ''
args = [] args = []
for w in custom_where: for w in custom_where:
and_or = ' OR ' if w[0].endswith('OR') else ' AND '
w[0] = w[0].rstrip(' OR')
if isinstance(w[1], (list, tuple)) and len(w[1]): if isinstance(w[1], (list, tuple)) and len(w[1]):
c_where += '(' c_where += '('
for w_ in w[1]: for w_ in w[1]:
if w_ == None: if w_ is None:
c_where += w[0] + ' IS NULL OR ' c_where += w[0] + ' IS NULL'
elif str(w_).startswith('LIKE '): elif str(w_).startswith('LIKE '):
c_where += w[0] + ' LIKE ? OR ' c_where += w[0] + ' LIKE ?'
args.append(w_[5:]) args.append(w_[5:])
else: else:
c_where += w[0] + ' = ? OR ' c_where += w[0] + ' = ?'
args.append(w_) args.append(w_)
c_where = c_where.rstrip(' OR ') + ') AND ' c_where += ' OR '
c_where = c_where.rstrip(' OR ') + ')' + and_or
else: else:
if w[1] == None: if w[1] is None:
c_where += w[0] + ' IS NULL AND ' c_where += w[0] + ' IS NULL'
elif str(w[1]).startswith('LIKE '): elif str(w[1]).startswith('LIKE '):
c_where += w[0] + ' LIKE ? AND ' c_where += w[0] + ' LIKE ?'
args.append(w[1][5:]) args.append(w[1][5:])
else: else:
c_where += w[0] + ' = ? AND ' c_where += w[0] + ' = ?'
args.append(w[1]) args.append(w[1])
c_where += and_or
if c_where: if c_where:
c_where = 'WHERE ' + c_where.rstrip(' AND ') c_where = 'WHERE ' + c_where.rstrip(' AND ').rstrip(' OR ')
return c_where, args return c_where, args
def build_order(self, order_param=[], columns=[], dt_columns=[]):
def build_order(order_param=[], columns=[], dt_columns=[]):
# Build ordering # Build ordering
order = '' order = ''
@ -200,7 +210,8 @@ class DataTables(object):
return order return order
def build_where(self, search_param='', columns=[], dt_columns=[]):
def build_where(search_param='', columns=[], dt_columns=[]):
# Build where parameters # Build where parameters
where = '' where = ''
args = [] args = []
@ -227,11 +238,11 @@ class DataTables(object):
return where, args return where, args
# This method extracts column data from our column list
# The first parameter is required, the match_columns parameter is optional and will cause the function to # This method extracts column data from our column list
# only return results if the value also exists in the match_columns 'data' field # The first parameter is required, the match_columns parameter is optional and will cause the function to
@staticmethod # only return results if the value also exists in the match_columns 'data' field
def extract_columns(columns=None, match_columns=None): def extract_columns(columns=None, match_columns=None):
columns_string = '' columns_string = ''
columns_literal = [] columns_literal = []
columns_named = [] columns_named = []