Skip to content

Commit be2f16d

Browse files
loryrutadan-obx
authored andcommitted
query condition: make _OP_MAP constant #39
1 parent 442b892 commit be2f16d

1 file changed

Lines changed: 24 additions & 20 deletions

File tree

objectbox/condition.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,33 @@ class PropertyQueryConditionOp(Enum):
7474
class PropertyQueryCondition(QueryCondition):
7575
""" A QueryCondition describing an operation to be applied on a property (e.g. name == "John", age == 24) """
7676

77+
_OP_MAP: Dict[PropertyQueryConditionOp, str] = {
78+
PropertyQueryConditionOp.EQ: "_apply_eq",
79+
PropertyQueryConditionOp.NOT_EQ: "_apply_not_eq",
80+
PropertyQueryConditionOp.CONTAINS: "_apply_contains",
81+
PropertyQueryConditionOp.STARTS_WITH: "_apply_starts_with",
82+
PropertyQueryConditionOp.ENDS_WITH: "_apply_ends_with",
83+
PropertyQueryConditionOp.GT: "_apply_gt",
84+
PropertyQueryConditionOp.GTE: "_apply_gte",
85+
PropertyQueryConditionOp.LT: "_apply_lt",
86+
PropertyQueryConditionOp.LTE: "_apply_lte",
87+
PropertyQueryConditionOp.BETWEEN: "_apply_between",
88+
PropertyQueryConditionOp.NEAREST_NEIGHBOR: "_apply_nearest_neighbor",
89+
PropertyQueryConditionOp.CONTAINS_KEY_VALUE: "_contains_key_value"
90+
# ... new property query conditions here ... :)
91+
}
92+
7793
def __init__(self, property_id: int, op: PropertyQueryConditionOp, args: Dict[str, Any]):
78-
if op not in self._get_op_map():
79-
raise Exception(f"Invalid query condition op with ID: {op}")
94+
if op not in self._OP_MAP:
95+
raise Exception(f"Invalid PropertyQueryConditionOp: {op}")
96+
op_func_name = self._OP_MAP[op]
97+
if not hasattr(self, op_func_name):
98+
raise Exception(f"Missing PropertyQueryCondition op function: {op_func_name} (op: {op})")
99+
op_func = getattr(self, op_func_name)
80100

81101
self._property_id = property_id
82102
self._op = op
103+
self._op_func = op_func
83104
self._args = args
84105
self._alias = None
85106

@@ -88,23 +109,6 @@ def alias(self, value: str):
88109
self._alias = value
89110
return self
90111

91-
def _get_op_map(self):
92-
return {
93-
PropertyQueryConditionOp.EQ: self._apply_eq,
94-
PropertyQueryConditionOp.NOT_EQ: self._apply_not_eq,
95-
PropertyQueryConditionOp.CONTAINS: self._apply_contains,
96-
PropertyQueryConditionOp.STARTS_WITH: self._apply_starts_with,
97-
PropertyQueryConditionOp.ENDS_WITH: self._apply_ends_with,
98-
PropertyQueryConditionOp.GT: self._apply_gt,
99-
PropertyQueryConditionOp.GTE: self._apply_gte,
100-
PropertyQueryConditionOp.LT: self._apply_lt,
101-
PropertyQueryConditionOp.LTE: self._apply_lte,
102-
PropertyQueryConditionOp.BETWEEN: self._apply_between,
103-
PropertyQueryConditionOp.NEAREST_NEIGHBOR: self._apply_nearest_neighbor,
104-
PropertyQueryConditionOp.CONTAINS_KEY_VALUE: self._contains_key_value
105-
# ... new property query condition here ... :)
106-
}
107-
108112
def _apply_eq(self, qb: QueryBuilder) -> obx_qb_cond:
109113
value = self._args['value']
110114
case_sensitive = self._args['case_sensitive']
@@ -219,7 +223,7 @@ def _contains_key_value(self, qb: QueryBuilder) -> obx_qb_cond:
219223
return qb.contains_key_value(self._property_id, key, value, case_sensitive)
220224

221225
def apply(self, qb: QueryBuilder) -> obx_qb_cond:
222-
c_cond = self._get_op_map()[self._op](qb)
226+
c_cond = self._op_func(qb)
223227
if self._alias is not None:
224228
qb.alias(self._alias)
225229
return c_cond

0 commit comments

Comments
 (0)