|
1 | 1 | import dataclasses |
2 | 2 | import logging |
3 | | -from typing import Callable, List |
| 3 | +from typing import Callable |
4 | 4 |
|
5 | 5 | import azure.cosmos.cosmos_client as cosmos_client |
6 | 6 | import azure.cosmos.exceptions as exceptions |
7 | | -import flask |
8 | 7 | from azure.cosmos import ContainerProxy, PartitionKey |
9 | 8 | from flask import Flask |
10 | 9 | from werkzeug.exceptions import HTTPException |
11 | 10 |
|
12 | 11 | from commons.data_access_layer.database import CRUDDao, EventContext |
| 12 | +from utils.query_builder import CosmosDBQueryBuilder |
13 | 13 |
|
14 | 14 |
|
15 | 15 | class CosmosDBFacade: |
@@ -257,53 +257,38 @@ def find_all( |
257 | 257 | mapper: Callable = None, |
258 | 258 | ): |
259 | 259 | conditions = conditions if conditions else {} |
260 | | - partition_key_value = self.find_partition_key_value(event_context) |
261 | | - max_count = self.get_page_size_or(max_count) |
262 | | - params = [ |
263 | | - {"name": "@partition_key_value", "value": partition_key_value}, |
264 | | - {"name": "@offset", "value": offset}, |
265 | | - {"name": "@max_count", "value": max_count}, |
266 | | - ] |
267 | | - |
268 | | - status_value = None |
269 | | - if conditions.get('status') != None: |
270 | | - status_value = conditions.get('status') |
| 260 | + max_count: int = self.get_page_size_or(max_count) |
| 261 | + |
| 262 | + status_value = conditions.get('status') |
| 263 | + if status_value: |
271 | 264 | conditions.pop('status') |
272 | 265 |
|
273 | 266 | date_range = date_range if date_range else {} |
274 | | - date_range_params = ( |
275 | | - self.generate_params(date_range) if date_range else [] |
276 | | - ) |
277 | | - params.extend(self.generate_params(conditions)) |
278 | | - params.extend(date_range_params) |
279 | | - |
280 | | - query_str = """ |
281 | | - SELECT * FROM c |
282 | | - WHERE c.{partition_key_attribute}=@partition_key_value |
283 | | - {conditions_clause} |
284 | | - {active_condition} |
285 | | - {date_range_sql_condition} |
286 | | - {visibility_condition} |
287 | | - {order_clause} |
288 | | - OFFSET @offset LIMIT @max_count |
289 | | - """.format( |
290 | | - partition_key_attribute=self.partition_key_attribute, |
291 | | - visibility_condition=self.create_sql_condition_for_visibility( |
292 | | - visible_only |
293 | | - ), |
294 | | - active_condition=self.create_sql_active_condition(status_value), |
295 | | - conditions_clause=self.create_sql_where_conditions(conditions), |
296 | | - date_range_sql_condition=self.create_sql_date_range_filter( |
297 | | - date_range |
298 | | - ), |
299 | | - order_clause=self.create_sql_order_clause(), |
| 267 | + |
| 268 | + query_builder = ( |
| 269 | + CosmosDBQueryBuilder() |
| 270 | + .add_sql_where_equal_condition(conditions) |
| 271 | + .add_sql_active_condition(status_value) |
| 272 | + .add_sql_date_range_filter(date_range) |
| 273 | + .add_sql_visibility_condition(visible_only) |
| 274 | + .add_sql_limit_condition(max_count) |
| 275 | + .add_sql_offset_condition(offset) |
| 276 | + .build() |
300 | 277 | ) |
301 | 278 |
|
| 279 | + if len(self.order_fields) > 1: |
| 280 | + attribute = self.order_fields[0] |
| 281 | + order = self.order_fields[1] |
| 282 | + query_builder.add_sql_order_by_condition(attribute, order) |
| 283 | + |
| 284 | + query_str = query_builder.get_query() |
| 285 | + params = query_builder.get_parameters() |
| 286 | + partition_key_value = self.find_partition_key_value(event_context) |
| 287 | + |
302 | 288 | result = self.container.query_items( |
303 | 289 | query=query_str, |
304 | 290 | parameters=params, |
305 | 291 | partition_key=partition_key_value, |
306 | | - max_item_count=max_count, |
307 | 292 | ) |
308 | 293 |
|
309 | 294 | function_mapper = self.get_mapper_or_dict(mapper) |
|
0 commit comments