tracktolib 0.59.0__tar.gz → 0.61.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tracktolib
3
- Version: 0.59.0
3
+ Version: 0.61.0
4
4
  Summary: Utility library for python
5
5
  License: MIT
6
6
  License-File: LICENSE
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "tracktolib"
3
- version = "0.59.0"
3
+ version = "0.61.0"
4
4
  description = "Utility library for python"
5
5
  authors = ["Julien Brayere <julien.brayere@tracktor.fr>"]
6
6
  license = "MIT"
@@ -83,7 +83,7 @@ pythonPlatform = "Linux"
83
83
 
84
84
  [tool.commitizen]
85
85
  name = "cz_conventional_commits"
86
- version = "0.59.0"
86
+ version = "0.61.0"
87
87
  tag_format = "$version"
88
88
  version_files = [
89
89
  "pyproject.toml:version"
@@ -12,5 +12,6 @@ from .query import (
12
12
  update_one,
13
13
  insert_pg,
14
14
  OnConflict,
15
+ update_many,
15
16
  )
16
17
  from .utils import iterate_pg, upsert_csv, safe_pg, safe_pg_context, PGError, PGException
@@ -17,7 +17,7 @@ V = TypeVar("V")
17
17
 
18
18
  def _get_insert_query(table: str, columns: Iterable[K], values: str) -> str:
19
19
  _columns = ", ".join(columns)
20
- return f"INSERT INTO {table} AS t ({_columns}) VALUES ( {values} )"
20
+ return f"INSERT INTO {table} AS t ({_columns}) VALUES ({values})"
21
21
 
22
22
 
23
23
  def _get_returning_query(query: str, returning: Iterable[K]) -> str:
@@ -217,7 +217,7 @@ def get_update_fields(
217
217
  fields.append(
218
218
  f"{_col} = ${_counter}"
219
219
  if k not in _merge_keys
220
- else f"{_col} = COALESCE(t.{_col}, jsonb_build_object()) || " f"${_counter}"
220
+ else f"{_col} = COALESCE(t.{_col}, JSONB_BUILD_OBJECT()) || " f"${_counter}"
221
221
  )
222
222
  counter += 1
223
223
  return ",\n".join(fields), values + where_values
@@ -264,7 +264,7 @@ class PGUpdateQuery(PGQuery):
264
264
  def values(self):
265
265
  if not self._values:
266
266
  raise ValueError("No values found")
267
- return self._values
267
+ return self._values if len(self.items) == 1 else super().values
268
268
 
269
269
  def _get_where_query(self) -> str:
270
270
  if self.where:
@@ -310,6 +310,10 @@ def insert_pg(
310
310
  )
311
311
 
312
312
 
313
+ Q = TypeVar("Q", bound=PGInsertQuery | PGUpdateQuery)
314
+ QueryCallback = Callable[[Q], None]
315
+
316
+
313
317
  async def insert_one(
314
318
  conn: _Connection,
315
319
  table: str,
@@ -318,8 +322,11 @@ async def insert_one(
318
322
  on_conflict: OnConflict | None = None,
319
323
  fill: bool = False,
320
324
  quote_columns: bool = False,
325
+ query_callback: QueryCallback[PGInsertQuery] | None = None,
321
326
  ):
322
327
  query = insert_pg(table=table, items=[item], on_conflict=on_conflict, fill=fill, quote_columns=quote_columns)
328
+ if query_callback is not None:
329
+ query_callback(query)
323
330
  await query.run(conn)
324
331
 
325
332
 
@@ -331,14 +338,23 @@ async def insert_many(
331
338
  on_conflict: OnConflict | None = None,
332
339
  fill: bool = False,
333
340
  quote_columns: bool = False,
341
+ query_callback: QueryCallback[PGInsertQuery] | None = None,
334
342
  ):
335
343
  query = insert_pg(table=table, items=items, on_conflict=on_conflict, fill=fill, quote_columns=quote_columns)
344
+ if query_callback is not None:
345
+ query_callback(query)
336
346
  await query.run(conn)
337
347
 
338
348
 
339
349
  @overload
340
350
  async def insert_returning(
341
- conn: _Connection, table: str, item: dict, returning: str, on_conflict: OnConflict | None = None, fill: bool = False
351
+ conn: _Connection,
352
+ table: str,
353
+ item: dict,
354
+ returning: str,
355
+ on_conflict: OnConflict | None = None,
356
+ fill: bool = False,
357
+ query_callback: QueryCallback[PGInsertQuery] | None = None,
342
358
  ) -> Any | None: ...
343
359
 
344
360
 
@@ -350,6 +366,7 @@ async def insert_returning(
350
366
  returning: list[str],
351
367
  on_conflict: OnConflict | None = None,
352
368
  fill: bool = False,
369
+ query_callback: QueryCallback[PGInsertQuery] | None = None,
353
370
  ) -> asyncpg.Record | None: ...
354
371
 
355
372
 
@@ -360,9 +377,12 @@ async def insert_returning(
360
377
  returning: list[str] | str,
361
378
  on_conflict: OnConflict | None = None,
362
379
  fill: bool = False,
380
+ query_callback: QueryCallback[PGInsertQuery] | None = None,
363
381
  ) -> asyncpg.Record | Any | None:
364
382
  returning_values = [returning] if isinstance(returning, str) else returning
365
383
  query = insert_pg(table=table, items=[item], on_conflict=on_conflict, fill=fill, returning=returning_values)
384
+ if query_callback is not None:
385
+ query_callback(query)
366
386
  fn = conn.fetchval if len(returning_values) == 1 and returning != "*" else conn.fetchrow
367
387
 
368
388
  return await fn(query.query, *query.values)
@@ -389,10 +409,13 @@ async def update_one(
389
409
  start_from: int | None = None,
390
410
  where: str | None = None,
391
411
  merge_keys: list[str] | None = None,
412
+ query_callback: QueryCallback[PGUpdateQuery] | None = None,
392
413
  ):
393
414
  query = PGUpdateQuery(
394
415
  table=table, items=[item], start_from=start_from, where_keys=keys, where=where, merge_keys=merge_keys
395
416
  )
417
+ if query_callback is not None:
418
+ query_callback(query)
396
419
  await conn.execute(query.query, *args, *query.values)
397
420
 
398
421
 
@@ -408,6 +431,7 @@ async def update_returning(
408
431
  keys: list[str] | None = None,
409
432
  start_from: int | None = None,
410
433
  merge_keys: list[str] | None = None,
434
+ query_callback: QueryCallback[PGUpdateQuery] | None = None,
411
435
  ) -> Any | None: ...
412
436
 
413
437
 
@@ -423,6 +447,7 @@ async def update_returning(
423
447
  keys: list[str] | None = None,
424
448
  start_from: int | None = None,
425
449
  merge_keys: list[str] | None = None,
450
+ query_callback: QueryCallback[PGUpdateQuery] | None = None,
426
451
  ) -> asyncpg.Record | None: ...
427
452
 
428
453
 
@@ -438,6 +463,7 @@ async def update_returning(
438
463
  keys: list[str] | None = None,
439
464
  start_from: int | None = None,
440
465
  merge_keys: list[str] | None = None,
466
+ query_callback: QueryCallback[PGUpdateQuery] | None = None,
441
467
  ) -> asyncpg.Record | None: ...
442
468
 
443
469
 
@@ -452,6 +478,7 @@ async def update_returning(
452
478
  keys: list[str] | None = None,
453
479
  start_from: int | None = None,
454
480
  merge_keys: list[str] | None = None,
481
+ query_callback: QueryCallback[PGUpdateQuery] | None = None,
455
482
  ) -> Any | asyncpg.Record | None:
456
483
  if returning is not None:
457
484
  returning_values = [returning] if isinstance(returning, str) else returning
@@ -467,5 +494,25 @@ async def update_returning(
467
494
  returning=returning_values,
468
495
  merge_keys=merge_keys,
469
496
  )
497
+ if query_callback is not None:
498
+ query_callback(query)
470
499
  fn = conn.fetchval if len(returning_values or []) == 1 else conn.fetchrow
471
500
  return await fn(query.query, *args, *query.values)
501
+
502
+
503
+ async def update_many(
504
+ conn: _Connection,
505
+ table: str,
506
+ items: list[dict],
507
+ keys: list[str] | None = None,
508
+ start_from: int | None = None,
509
+ where: str | None = None,
510
+ merge_keys: list[str] | None = None,
511
+ query_callback: QueryCallback[PGUpdateQuery] | None = None,
512
+ ):
513
+ query = PGUpdateQuery(
514
+ table=table, items=items, start_from=start_from, where_keys=keys, where=where, merge_keys=merge_keys
515
+ )
516
+ if query_callback is not None:
517
+ query_callback(query)
518
+ await conn.executemany(query.query, query.values)
@@ -70,7 +70,7 @@ def get_conflict_query(
70
70
  fields = ", ".join(f"{x} = COALESCE(EXCLUDED.{x}, t.{x})" for x in columns if x not in _ignore_columns)
71
71
  if merge_columns:
72
72
  fields = fields + ", " if fields else fields
73
- fields += ", ".join(f"{x} = COALESCE(t.{x}, jsonb_build_object()) || EXCLUDED.{x}" for x in merge_columns)
73
+ fields += ", ".join(f"{x} = COALESCE(t.{x}, JSONB_BUILD_OBJECT()) || EXCLUDED.{x}" for x in merge_columns)
74
74
  if not fields:
75
75
  raise ValueError("No fields set")
76
76
 
File without changes
File without changes