Skip to content

Commit 61a7b9e

Browse files
committed
Fix: 处理exclude过滤问题
1 parent 68135f0 commit 61a7b9e

File tree

1 file changed

+38
-41
lines changed

1 file changed

+38
-41
lines changed

flask_sql_pro/db.py

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -63,44 +63,39 @@ def handle_ops(cls, key, val, opt_type="where"):
6363
val: value for either "where" or "exclude"
6464
opt_type: "where" or "exclude"
6565
"""
66-
phrase = f"{key} = :_where_{key} AND"
67-
68-
filter_str = '_where_'
69-
if opt_type == 'exclude':
70-
filter_str = '_exclude_'
71-
72-
if key.endswith('__gt'):
73-
phrase = f"{key} >= :{filter_str}{key} AND"
74-
if opt_type == 'exclude':
75-
phrase = f"{key} < :{filter_str}{key} AND"
76-
if key.endswith('__gte'):
77-
phrase = f"{key} >= :{filter_str}{key} AND"
78-
if opt_type == 'exclude':
79-
phrase = f"{key} < :{filter_str}{key} AND"
80-
if key.endswith('__lt'):
81-
phrase = f"{key} < :{filter_str}{key} AND"
82-
if opt_type == 'exclude':
83-
phrase = f"{key} >= :{filter_str}{key} AND"
84-
if key.endswith('__lte'):
85-
phrase = f"{key} <= :{filter_str}{key} AND"
86-
if opt_type == 'exclude':
87-
phrase = f"{key} > :{filter_str}{key} AND"
88-
if key.endswith('__like'):
89-
phrase = f"{key} LIKE :{filter_str}{key} AND"
90-
if opt_type == 'exclude':
91-
phrase = f"{key} NOT LIKE :{filter_str}{key} AND"
92-
if key.endswith('__in'):
93-
phrase = f"{key} IN :{filter_str}{key} AND"
94-
if opt_type == 'exclude':
95-
phrase = f"{key} NOT IN :{filter_str}{key} AND"
96-
if key.endswith('__isnull'):
97-
phrase = f"{key} IS NULL AND " if val else f"{key} IS NOT NULL AND "
98-
if opt_type == 'exclude':
99-
phrase = f"{key} IS NOT NULL AND " if val else f"{key} IS NULL AND "
100-
if key.endswith('__between'):
101-
phrase = f"{key} BETWEEN :{filter_str}_between_1_{key} AND :{filter_str}_between_2_{key} AND"
102-
103-
return phrase
66+
filter_str = '_where_' if opt_type == 'where' else '_exclude_'
67+
operator_mapping = {
68+
'__gt': '>=',
69+
'__gte': '>=',
70+
'__lt': '<',
71+
'__lte': '<=',
72+
'__like': 'LIKE',
73+
'__in': 'IN',
74+
'__isnull': 'IS NULL' if val else 'IS NOT NULL',
75+
'__between': 'BETWEEN'
76+
}
77+
exclude_mapping = {
78+
'__gt': '<',
79+
'__gte': '<',
80+
'__lt': '>=',
81+
'__lte': '>',
82+
'__like': 'NOT LIKE',
83+
'__in': 'NOT IN',
84+
'__isnull': 'IS NOT NULL' if val else 'IS NULL',
85+
'__between': 'NOT BETWEEN'
86+
}
87+
88+
for op, sql_op in operator_mapping.items():
89+
if key.endswith(op):
90+
if opt_type == 'exclude':
91+
sql_op = exclude_mapping[op]
92+
if op == '__between':
93+
phrase = f"{key} {sql_op} :{filter_str}_between_1_{key} AND :{filter_str}_between_2_{key} AND"
94+
else:
95+
phrase = f"{key} {sql_op} :{filter_str}{key} AND"
96+
return phrase
97+
98+
return f"{key} = :_where_{key} AND"
10499

105100
@classmethod
106101
def set_where_phrase(cls, sql, where):
@@ -130,8 +125,9 @@ def set_exclude_phrase(cls, sql, exclude):
130125
sql += " WHERE "
131126
else:
132127
sql += " AND "
133-
for key in exclude.keys():
134-
sql += key + " != :" + "_exclude_%s" % key + " and "
128+
129+
for key, val in exclude.items():
130+
sql += cls.handle_ops(key, val, opt_type="exclude")
135131
sql = sql[0:-5]
136132

137133
return sql
@@ -290,7 +286,7 @@ def execute_create(cls, tb_name, data, app=None, bind=None, commit=False):
290286
return None
291287

292288
@classmethod
293-
def execute_delete(cls, tb_name, where, logic=False, app=None, bind=None, commit=False):
289+
def execute_delete(cls, tb_name, where, logic=False, app=None, bind=None, commit=False, exclude=None):
294290
"""
295291
Delete data
296292
:param bind:
@@ -307,6 +303,7 @@ def execute_delete(cls, tb_name, where, logic=False, app=None, bind=None, commit
307303
sql = "UPDATE %s SET %s=1" % (tb_name, cls.logic_delete_flag)
308304
sql = cls.set_where_phrase(sql, where)
309305
where = cls.fullfilled_data({}, where)
306+
sql = cls.set_exclude_phrase(sql, exclude=exclude)
310307

311308
try:
312309
if app and bind:

0 commit comments

Comments
 (0)