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.')
return None
extracted_columns = self.extract_columns(columns=columns)
join = self.build_join(join_types, join_tables, join_evals)
group = self.build_grouping(group_by)
c_where, cw_args = self.build_custom_where(custom_where)
order = self.build_order(parameters['order'],
extracted_columns = extract_columns(columns=columns)
join = build_join(join_types, join_tables, join_evals)
group = build_grouping(group_by)
c_where, cw_args = build_custom_where(custom_where)
order = build_order(parameters['order'],
extracted_columns['column_named'],
parameters['columns'])
where, w_args = self.build_where(parameters['search']['value'],
where, w_args = build_where(parameters['search']['value'],
extracted_columns['column_named'],
parameters['columns'])
# Build union parameters
if table_name_union:
extracted_columns_union = self.extract_columns(columns=columns_union)
group_u = self.build_grouping(group_by_union)
c_where_u, cwu_args = self.build_custom_where(custom_where_union)
extracted_columns_union = extract_columns(columns=columns_union)
group_u = build_grouping(group_by_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'],
table_name_union,
c_where_u,
@ -117,7 +117,8 @@ class DataTables(object):
return output
def build_grouping(self, group_by=[]):
def build_grouping(group_by=[]):
# Build grouping
group = ''
@ -128,7 +129,8 @@ class DataTables(object):
return group
def build_join(self, join_types=[], join_tables=[], join_evals=[]):
def build_join(join_types=[], join_tables=[], join_evals=[]):
# Build join parameters
join = ''
@ -140,40 +142,48 @@ class DataTables(object):
return join
def build_custom_where(self, custom_where=[]):
def build_custom_where(custom_where=[]):
# Build custom where parameters
c_where = ''
args = []
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]):
c_where += '('
for w_ in w[1]:
if w_ == None:
c_where += w[0] + ' IS NULL OR '
if w_ is None:
c_where += w[0] + ' IS NULL'
elif str(w_).startswith('LIKE '):
c_where += w[0] + ' LIKE ? OR '
c_where += w[0] + ' LIKE ?'
args.append(w_[5:])
else:
c_where += w[0] + ' = ? OR '
c_where += w[0] + ' = ?'
args.append(w_)
c_where = c_where.rstrip(' OR ') + ') AND '
c_where += ' OR '
c_where = c_where.rstrip(' OR ') + ')' + and_or
else:
if w[1] == None:
c_where += w[0] + ' IS NULL AND '
if w[1] is None:
c_where += w[0] + ' IS NULL'
elif str(w[1]).startswith('LIKE '):
c_where += w[0] + ' LIKE ? AND '
c_where += w[0] + ' LIKE ?'
args.append(w[1][5:])
else:
c_where += w[0] + ' = ? AND '
c_where += w[0] + ' = ?'
args.append(w[1])
c_where += and_or
if c_where:
c_where = 'WHERE ' + c_where.rstrip(' AND ')
c_where = 'WHERE ' + c_where.rstrip(' AND ').rstrip(' OR ')
return c_where, args
def build_order(self, order_param=[], columns=[], dt_columns=[]):
def build_order(order_param=[], columns=[], dt_columns=[]):
# Build ordering
order = ''
@ -200,7 +210,8 @@ class DataTables(object):
return order
def build_where(self, search_param='', columns=[], dt_columns=[]):
def build_where(search_param='', columns=[], dt_columns=[]):
# Build where parameters
where = ''
args = []
@ -227,11 +238,11 @@ class DataTables(object):
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
# only return results if the value also exists in the match_columns 'data' field
@staticmethod
def extract_columns(columns=None, match_columns=None):
# 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
# only return results if the value also exists in the match_columns 'data' field
def extract_columns(columns=None, match_columns=None):
columns_string = ''
columns_literal = []
columns_named = []