This commit is contained in:
yumoqing 2024-05-15 11:40:54 +08:00
parent 8b0db960a4
commit fae798e183

View File

@ -34,6 +34,25 @@ except:
import json import json
class DBFilter(object): class DBFilter(object):
operators = [
"=",
"!=",
"IN",
"NOT IN",
">",
">=",
"<",
"<=",
"IS NULL",
"IS NOT NULL",
"LIKE",
"NOT LIKE"
]
logic = [
"AND",
"OR",
"NOT"
]
def __init__(self,filterjson): def __init__(self,filterjson):
if isinstance(filterjson, str): if isinstance(filterjson, str):
filterjson = json.loads(filterjson) filterjson = json.loads(filterjson)
@ -103,9 +122,12 @@ class DBFilter(object):
keys = fj.keys() keys = fj.keys()
assert 'field' in keys assert 'field' in keys
assert 'op' in keys assert 'op' in keys
assert 'const' in keys or 'var' in keys assert 'const' in keys or 'var' in keys or fj['op'].upper() in ['IS NULL', 'IS NOT NULL']
op = fj.get('op') op = fj.get('op').upper()
assert op in ['=','<>','>','<','>=','<=','in','not in'] assert op in self.operators
if op in ['IS NULL', 'IS NOT NULL']:
return f'{fj['field']} {op}'
var = fj.get('var') var = fj.get('var')
if var and not var in ns.keys(): if var and not var in ns.keys():
@ -128,6 +150,11 @@ def default_filterjson(fields: list, ns: dict):
'op':'=', 'op':'=',
'var':k 'var':k
} for k in ns.keys() if k in fields ] } for k in ns.keys() if k in fields ]
print(f'default_filterjson():{c=}')
if len(c) < 1:
return None
if len(c) < 2:
return c[0]
return { return {
'AND':c 'AND':c
} }