mtsql 1.12.28__py3-none-any.whl → 1.12.30__py3-none-any.whl

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.
mt/sql/base.py CHANGED
@@ -29,6 +29,8 @@ __all__ = [
29
29
  "temp_table_find_new_id",
30
30
  "temp_table_drop",
31
31
  "to_temp_table",
32
+ "find_common_ids",
33
+ "remove_records_by_id",
32
34
  ]
33
35
 
34
36
 
@@ -97,7 +99,7 @@ def run_func(
97
99
  raise
98
100
 
99
101
 
100
- def conn_ctx(engine):
102
+ def conn_ctx(engine: sa.engine.Engine):
101
103
  if isinstance(engine, sa.engine.Engine):
102
104
  return engine.begin()
103
105
  return ctx.nullcontext(engine)
@@ -456,10 +458,10 @@ def temp_table_drop(
456
458
 
457
459
  Parameters
458
460
  ----------
459
- id : int or str
460
- table id or table name. An id can be generated by invoking :func:`temp_table_find_new_id`.
461
461
  engine : sqlalchemy.engine.Engine
462
462
  connection engine to the server
463
+ id : int or str
464
+ table id or table name. An id can be generated by invoking :func:`temp_table_find_new_id`.
463
465
  logger : mt.logg.IndentedLoggerAdapter, optional
464
466
  logger for debugging
465
467
  """
@@ -494,3 +496,92 @@ def to_temp_table(df: pd.DataFrame, engine: sa.engine.Engine, **kwds):
494
496
  yield name
495
497
  finally:
496
498
  temp_table_drop(engine, tid)
499
+
500
+
501
+ def find_common_ids(
502
+ l_ids: tp.List[int],
503
+ frame_name: str,
504
+ engine: sa.engine.Engine,
505
+ schema: tp.Optional[str] = None,
506
+ id_col: str = "id",
507
+ logger: tp.Optional[logg.IndentedLoggerAdapter] = None,
508
+ ) -> tp.List[int]:
509
+ """Finds common ids between a list of ids and the ids in a given frame.
510
+
511
+ Parameters
512
+ ----------
513
+ l_ids : list of int
514
+ list of ids to be checked
515
+ frame_name : str
516
+ name of the frame to be checked against
517
+ engine : sqlalchemy.engine.Engine
518
+ connection engine to the server
519
+ schema : str, optional
520
+ schema of the frame. If None, the default schema is used.
521
+ id_col : str
522
+ name of the id column in the frame
523
+ logger : mt.logg.IndentedLoggerAdapter, optional
524
+ logger for debugging
525
+
526
+ Returns
527
+ -------
528
+ list of int
529
+ list of common ids
530
+ """
531
+
532
+ with conn_ctx(engine) as conn:
533
+ temp_table = create_temp_id_table(l_ids, conn, logger=logger)
534
+
535
+ full_frame_name = frame_sql(frame_name, schema=schema)
536
+
537
+ sql = f"""
538
+ SELECT t.id FROM {temp_table} AS t
539
+ INNER JOIN {full_frame_name} AS f
540
+ ON t.id = f.{id_col};
541
+ """
542
+
543
+ df_common = read_sql(sql, conn, index_col=None, logger=logger)
544
+
545
+ l_commonIds = df_common["id"].tolist()
546
+
547
+ return l_commonIds
548
+
549
+
550
+ def remove_records_by_id(
551
+ l_ids: tp.List[int],
552
+ frame_name: str,
553
+ engine: sa.engine.Engine,
554
+ schema: tp.Optional[str] = None,
555
+ id_col: str = "id",
556
+ logger: tp.Optional[logg.IndentedLoggerAdapter] = None,
557
+ ):
558
+ """Removes records from a frame by a list of ids.
559
+
560
+ Parameters
561
+ ----------
562
+ l_ids : list of int
563
+ list of ids to be removed
564
+ frame_name : str
565
+ name of the frame to be modified
566
+ engine : sqlalchemy.engine.Engine
567
+ connection engine to the server
568
+ schema : str, optional
569
+ schema of the frame. If None, the default schema is used.
570
+ id_col : str
571
+ name of the id column in the frame
572
+ logger : mt.logg.IndentedLoggerAdapter, optional
573
+ logger for debugging
574
+ """
575
+
576
+ with conn_ctx(engine) as conn:
577
+ temp_table = create_temp_id_table(l_ids, conn, logger=logger)
578
+
579
+ full_frame_name = frame_sql(frame_name, schema=schema)
580
+
581
+ sql = f"""
582
+ DELETE FROM {full_frame_name}
583
+ USING {temp_table} AS t
584
+ WHERE {full_frame_name}.{id_col} = t.id;
585
+ """
586
+
587
+ exec_sql(sql, conn, logger=logger)
mt/sql/version.py CHANGED
@@ -1,5 +1,5 @@
1
1
  MAJOR_VERSION = 1
2
2
  MINOR_VERSION = 12
3
- PATCH_VERSION = 28
3
+ PATCH_VERSION = 30
4
4
  version = '{}.{}.{}'.format(MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION)
5
5
  __all__ = ['MAJOR_VERSION', 'MINOR_VERSION', 'PATCH_VERSION', 'version']
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mtsql
3
- Version: 1.12.28
3
+ Version: 1.12.30
4
4
  Summary: Extra Python modules to deal with the interaction between pandas dataframes and remote SQL servers, for Minh-Tri Pham
5
5
  Home-page: https://github.com/inteplus/mtsql
6
- Author: ['Minh-Tri Pham']
6
+ Author: Minh-Tri Pham
7
7
  Project-URL: Documentation, https://mtdoc.readthedocs.io/en/latest/mt.sql/mt.sql.html
8
8
  Project-URL: Source Code, https://github.com/inteplus/mtsql
9
9
  License-File: LICENSE
@@ -12,7 +12,7 @@ Requires-Dist: tzlocal
12
12
  Requires-Dist: tqdm
13
13
  Requires-Dist: psycopg[binary]
14
14
  Requires-Dist: redshift_connector>=2.1.5
15
- Requires-Dist: mtbase>=4.32.34
15
+ Requires-Dist: mtbase>=4.3.2
16
16
  Requires-Dist: mtpandas>=1.17.16
17
17
  Dynamic: author
18
18
  Dynamic: home-page
@@ -1,17 +1,17 @@
1
1
  mt/sql/__init__.py,sha256=b7zO50apZxt9Hg2eOkJhRLrXgACR8eS5b-Rphdn5qNQ,44
2
- mt/sql/base.py,sha256=S44nryyeguGNnzdf5ETmKXb9ewbAveiGYhEXVoS7lC8,13732
2
+ mt/sql/base.py,sha256=AAoiWIpN8jyHdnfbmUvee6iU2pxo7pDcgO5dLrsybrY,16243
3
3
  mt/sql/mysql.py,sha256=n2ENDctdUqZuSaDAcrqZYtPtawq3Wx4dOPCRsCB5Q4w,4894
4
4
  mt/sql/psql.py,sha256=U8XEyg4rQYr5gm8KohRWrpCNJKl5WC1yxJMBkkm1k_A,68125
5
5
  mt/sql/sqlite.py,sha256=T2ak_hhNi_zRfpg_gp8JhNHn7D2kl4i-Ey6-9ANMtz0,8678
6
- mt/sql/version.py,sha256=s9O6vZP85_qckYPCAa8UMcuMzYn1fIdaCEIpTTQIoHw,208
6
+ mt/sql/version.py,sha256=ATznhpd6AvkQUj3yf0sDQ4RYHO5ZWLiQdaKgfHdbxlE,208
7
7
  mt/sql/redshift/__init__.py,sha256=S-eRxJWcrvncF7LZXuulCdPV-UERu9eiw6uyDb5aGsM,443
8
8
  mt/sql/redshift/commands.py,sha256=xhGUBf3bL66EYdZI5HCUtOx-XqPCnXT_P-LnhPgtzrY,40193
9
9
  mt/sql/redshift/ddl.py,sha256=eUcZj9oIajiE1wKKBAP-V64gYJ7cVnSAt8dLFfluOJA,9777
10
10
  mt/sql/redshift/dialect.py,sha256=-0JjJubZZHRw0abhl6H6rKWaUE9pKtGwAuX-62T0e_c,53399
11
11
  mt/sql/redshift/main.py,sha256=H8_5sjtJ7dzWoCMXzPNjYhrMQ18eLUQ9xg-aYl5QeTc,17104
12
12
  mt/sql/redshift/redshift-ca-bundle.crt,sha256=532qYkOpQOstFE0mdXE1GVtL3v00XDKgZNTr6gK5-KE,8621
13
- mtsql-1.12.28.dist-info/licenses/LICENSE,sha256=PojkRlQzTT5Eg6Nj03XoIVEefN3u8iiIFf1p4rqe_t4,1070
14
- mtsql-1.12.28.dist-info/METADATA,sha256=9Kx_mlxJwgCMqwBM8CO3YYNzzz3fkATwYPW8Fcm7xWg,740
15
- mtsql-1.12.28.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
16
- mtsql-1.12.28.dist-info/top_level.txt,sha256=WcqGFu9cV7iMZg09iam8eNxUvGpLSKKF2Iubf6SJVOo,3
17
- mtsql-1.12.28.dist-info/RECORD,,
13
+ mtsql-1.12.30.dist-info/licenses/LICENSE,sha256=PojkRlQzTT5Eg6Nj03XoIVEefN3u8iiIFf1p4rqe_t4,1070
14
+ mtsql-1.12.30.dist-info/METADATA,sha256=mxs0fj1WO1myL6Zlf3iTyPSDDe6Do4TnZtif48dPbms,734
15
+ mtsql-1.12.30.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
16
+ mtsql-1.12.30.dist-info/top_level.txt,sha256=WcqGFu9cV7iMZg09iam8eNxUvGpLSKKF2Iubf6SJVOo,3
17
+ mtsql-1.12.30.dist-info/RECORD,,