1 # Copyright (C) 2021-2022 Wind River Systems, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 from sqlalchemy.sql.elements import ColumnElement
17 from o2common.helper import o2logging
18 logger = o2logging.get_logger(__name__)
21 def gen_filter(obj: ColumnElement, filter_str: str):
24 filter_without_space = filter_str.replace(" ", "")
25 items = filter_without_space.split(';')
30 i = i.replace("(", "")
32 i = i.replace(")", "")
33 filter_expr = i.split(',')
34 if len(filter_expr) < 3:
36 filter_op = filter_expr[0]
37 filter_key = filter_expr[1]
38 filter_vals = filter_expr[2:]
39 filter_list.extend(toFilterArgs(
40 filter_op, obj, filter_key, filter_vals))
41 logger.info('Filter list length: %d' % len(filter_list))
45 def toFilterArgs(operation: str, obj: ColumnElement, key: str, values: list):
46 if not hasattr(obj, key):
47 logger.warning('Filter attrName %s not in Object %s.' %
54 if val.lower() == 'null':
56 ll.append(getattr(obj, key) == val)
57 elif operation == 'neq':
59 if val.lower() == 'null':
61 ll.append(getattr(obj, key) != val)
62 elif operation == 'gt':
64 ll.append(getattr(obj, key) > val)
65 elif operation == 'lt':
67 ll.append(getattr(obj, key) < val)
68 elif operation == 'gte':
70 ll.append(getattr(obj, key) >= val)
71 elif operation == 'lte':
73 ll.append(getattr(obj, key) <= val)
74 elif operation == 'in':
76 elif operation == 'nin':
78 elif operation == 'count':
80 elif operation == 'ncount':
83 raise KeyError('Filter operation value not support.')