@@ -42,13 +42,6 @@ def __init__(self):
4242 mapper = TimeEntryCosmosDBModel ,
4343 )
4444
45- @staticmethod
46- def create_sql_ignore_id_condition (id : str ):
47- if id is None :
48- return ''
49- else :
50- return "AND c.id!=@ignore_id"
51-
5245 def find_all_entries (
5346 self ,
5447 event_context : EventContext ,
@@ -247,35 +240,38 @@ def find_interception_with_date_range(
247240 "owner_id" : owner_id ,
248241 "tenant_id" : tenant_id ,
249242 }
250- params = [
251- {"name" : "@start_date" , "value" : start_date },
252- {"name" : "@end_date" , "value" : end_date or current_datetime_str ()},
253- {"name" : "@ignore_id" , "value" : ignore_id },
243+
244+ not_equal_conditions = [
245+ {
246+ "field_name" : "start_date" ,
247+ "compare_field_name" : "end_date" ,
248+ "compare_field_value" : end_date ,
249+ },
250+ {
251+ "field_name" : "end_date" ,
252+ "compare_field_name" : "start_date" ,
253+ "compare_field_value" : start_date ,
254+ },
254255 ]
255- params .extend (self .generate_params (conditions ))
256+
257+ query_builder = (
258+ TimeEntryQueryBuilder ()
259+ .add_sql_interception_with_date_range_condition (
260+ start_date , end_date
261+ )
262+ .add_sql_where_not_equal_condition (not_equal_conditions )
263+ .add_sql_where_equal_condition (conditions )
264+ .add_sql_ignore_id_condition (ignore_id )
265+ .add_sql_visibility_condition (visible_only )
266+ .add_sql_order_by_condition ('start_date' , Order .DESC )
267+ .build ()
268+ )
269+
270+ query_str = query_builder .get_query ()
271+ params = query_builder .get_parameters ()
272+
256273 result = self .container .query_items (
257- query = """
258- SELECT * FROM c
259- WHERE (((c.start_date BETWEEN @start_date AND @end_date)
260- OR (c.end_date BETWEEN @start_date AND @end_date))
261- OR ((@start_date BETWEEN c.start_date AND c.end_date)
262- OR (@end_date BETWEEN c.start_date AND c.end_date)))
263- AND c.start_date!= @end_date
264- AND c.end_date!= @start_date
265- {conditions_clause}
266- {ignore_id_condition}
267- {visibility_condition}
268- {order_clause}
269- """ .format (
270- ignore_id_condition = self .create_sql_ignore_id_condition (
271- ignore_id
272- ),
273- visibility_condition = self .create_sql_condition_for_visibility (
274- visible_only
275- ),
276- conditions_clause = self .create_sql_where_conditions (conditions ),
277- order_clause = self .create_sql_order_clause (),
278- ),
274+ query = query_str ,
279275 parameters = params ,
280276 partition_key = tenant_id ,
281277 )
@@ -290,28 +286,31 @@ def find_running(
290286 "owner_id" : owner_id ,
291287 "tenant_id" : tenant_id ,
292288 }
289+
290+ query_builder = (
291+ TimeEntryQueryBuilder ()
292+ .add_sql_non_existent_attribute_condition ('end_date' )
293+ .add_sql_where_equal_condition (conditions )
294+ .add_sql_visibility_condition (True )
295+ .add_sql_offset_condition (0 )
296+ .add_sql_limit_condition (1 )
297+ .build ()
298+ )
299+
300+ query_str = query_builder .get_query ()
301+ params = query_builder .get_parameters ()
302+
293303 result = self .container .query_items (
294- query = """
295- SELECT * from c
296- WHERE (NOT IS_DEFINED(c.end_date) OR c.end_date = null)
297- {conditions_clause}
298- {visibility_condition}
299- OFFSET 0 LIMIT 1
300- """ .format (
301- visibility_condition = self .create_sql_condition_for_visibility (
302- True
303- ),
304- conditions_clause = self .create_sql_where_conditions (conditions ),
305- ),
306- parameters = self .generate_params (conditions ),
304+ query = query_str ,
305+ parameters = params ,
307306 partition_key = tenant_id ,
308307 max_item_count = 1 ,
309308 )
310309
311310 function_mapper = self .get_mapper_or_dict (mapper )
312311 try :
313312 return function_mapper (next (result ))
314- except StopIteration as no_result :
313+ except StopIteration :
315314 raise CustomError (HTTPStatus .NO_CONTENT )
316315
317316 def validate_data (self , data , event_context : EventContext ):
0 commit comments