(conn, update_dict: Dict, update_time: bool, table_name: str, equal_filters: Dict)
| 9 | |
| 10 | |
| 11 | async def update_object(conn, update_dict: Dict, update_time: bool, table_name: str, equal_filters: Dict) -> None: |
| 12 | # 2. build update dict |
| 13 | pg_update_dict = update_dict.copy() |
| 14 | for key, value in update_dict.items(): |
| 15 | if isinstance(value, (dict, list)): |
| 16 | pg_update_dict[key] = json.dumps(value) |
| 17 | |
| 18 | timestamp_int = current_timestamp_int_milliseconds() |
| 19 | |
| 20 | # 3. Prepare the SET clause for the update query |
| 21 | updates = ", ".join( |
| 22 | f"{key} = ${idx}" for idx, key in enumerate(pg_update_dict.keys(), start=len(equal_filters) + 2) |
| 23 | ) |
| 24 | if update_time: |
| 25 | updates += ", updated_timestamp = $1" |
| 26 | |
| 27 | # 4. Prepare the WHERE clause for the update query |
| 28 | conditions = " AND ".join(f"{key} = ${idx}" for idx, key in enumerate(equal_filters.keys(), start=2)) |
| 29 | |
| 30 | # 5. Prepare the final query |
| 31 | query = f"UPDATE {table_name} SET {updates} WHERE {conditions}" |
| 32 | |
| 33 | # 6. Prepare the arguments for the query |
| 34 | args = [timestamp_int, *equal_filters.values(), *pg_update_dict.values()] |
| 35 | |
| 36 | # 7. Execute the query |
| 37 | await conn.execute(query, *args) |
| 38 | |
| 39 | |
| 40 | async def get_object_total( |
no test coverage detected