mtsql 1.12.31__py3-none-any.whl → 1.12.32__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
@@ -25,12 +25,15 @@ __all__ = [
25
25
  "list_views",
26
26
  "table_exists",
27
27
  "create_temp_id_table",
28
+ "create_temp_str_id_table",
28
29
  "temp_table_name",
29
30
  "temp_table_find_new_id",
30
31
  "temp_table_drop",
31
32
  "to_temp_table",
32
33
  "find_common_ids",
34
+ "find_common_str_ids",
33
35
  "remove_records_by_id",
36
+ "remove_records_by_str_id",
34
37
  ]
35
38
 
36
39
 
@@ -420,6 +423,49 @@ def create_temp_id_table(
420
423
  return table_name
421
424
 
422
425
 
426
+ def create_temp_str_id_table(
427
+ l_ids: list,
428
+ conn: sa.engine.Connection,
429
+ chunksize: int = 1000000,
430
+ logger: tp.Optional[logg.IndentedLoggerAdapter] = None,
431
+ ) -> str:
432
+ """Creates a temporary table to containing a list of string ids.
433
+
434
+ Parameters
435
+ ----------
436
+ l_ids : list
437
+ list of string ids to be inserted into the table
438
+ conn : sqlalchemy.engine.Connection
439
+ a connection that has been opened
440
+ chunksize : int
441
+ maximum number of ids to be inserted in each INSERT statement
442
+ logger : mt.logg.IndentedLoggerAdapter, optional
443
+ logger for debugging
444
+
445
+ Returns
446
+ -------
447
+ table_name : str
448
+ name of the temporary table. The table will be deleted at the end of the connection
449
+ """
450
+
451
+ table_name = f"tab_{uuid.uuid4().hex}"
452
+
453
+ query_str = f"CREATE TEMP TABLE {table_name}(id character varying);"
454
+ exec_sql(sa.text(query_str), conn, logger=logger)
455
+
456
+ while True:
457
+ l_ids2 = l_ids[:chunksize]
458
+ if len(l_ids2) == 0:
459
+ break
460
+
461
+ values = ",".join((f"('{id}')" for id in l_ids2))
462
+ query_str = f"INSERT INTO {table_name}(id) VALUES {values};"
463
+ exec_sql(sa.text(query_str), conn, logger=logger)
464
+ l_ids = l_ids[chunksize:]
465
+
466
+ return table_name
467
+
468
+
423
469
  def temp_table_name(id: int) -> str:
424
470
  """Converts a temp table id into a temp table name."""
425
471
  return f"mttmp_{id}"
@@ -550,6 +596,55 @@ def find_common_ids(
550
596
  return l_commonIds
551
597
 
552
598
 
599
+ def find_common_str_ids(
600
+ l_ids: tp.List[str],
601
+ frame_name: str,
602
+ engine: sa.engine.Engine,
603
+ schema: tp.Optional[str] = None,
604
+ id_col: str = "uuid",
605
+ logger: tp.Optional[logg.IndentedLoggerAdapter] = None,
606
+ ) -> tp.List[int]:
607
+ """Finds common string ids between a list of string ids and the string ids in a given frame.
608
+
609
+ Parameters
610
+ ----------
611
+ l_ids : list of strings
612
+ list of string ids to be checked
613
+ frame_name : str
614
+ name of the frame to be checked against
615
+ engine : sqlalchemy.engine.Engine
616
+ connection engine to the server
617
+ schema : str, optional
618
+ schema of the frame. If None, the default schema is used.
619
+ id_col : str
620
+ name of the string id column in the frame
621
+ logger : mt.logg.IndentedLoggerAdapter, optional
622
+ logger for debugging
623
+
624
+ Returns
625
+ -------
626
+ list of strings
627
+ list of common string ids
628
+ """
629
+
630
+ with conn_ctx(engine) as conn:
631
+ temp_table = create_temp_str_id_table(l_ids, conn, logger=logger)
632
+
633
+ full_frame_name = frame_sql(frame_name, schema=schema)
634
+
635
+ sql = f"""
636
+ SELECT t.id FROM {temp_table} AS t
637
+ INNER JOIN {full_frame_name} AS f
638
+ ON t.id = f.{id_col};
639
+ """
640
+
641
+ df_common = read_sql(sql, conn, index_col=None, logger=logger)
642
+
643
+ l_commonIds = df_common["id"].tolist()
644
+
645
+ return l_commonIds
646
+
647
+
553
648
  def remove_records_by_id(
554
649
  l_ids: tp.List[int],
555
650
  frame_name: str,
@@ -591,3 +686,43 @@ def remove_records_by_id(
591
686
  """
592
687
 
593
688
  exec_sql(sql, conn, logger=logger)
689
+
690
+
691
+ def remove_records_by_str_id(
692
+ l_ids: tp.List[str],
693
+ frame_name: str,
694
+ engine: sa.engine.Engine,
695
+ schema: tp.Optional[str] = None,
696
+ id_col: str = "id",
697
+ logger: tp.Optional[logg.IndentedLoggerAdapter] = None,
698
+ ):
699
+ """Removes records from a frame by a list of string ids.
700
+
701
+ Parameters
702
+ ----------
703
+ l_ids : list of strings
704
+ list of string ids to be removed
705
+ frame_name : str
706
+ name of the frame to be modified
707
+ engine : sqlalchemy.engine.Engine
708
+ connection engine to the server
709
+ schema : str, optional
710
+ schema of the frame. If None, the default schema is used.
711
+ id_col : str
712
+ name of the id column in the frame
713
+ logger : mt.logg.IndentedLoggerAdapter, optional
714
+ logger for debugging
715
+ """
716
+
717
+ with conn_ctx(engine) as conn:
718
+ temp_table = create_temp_str_id_table(l_ids, conn, logger=logger)
719
+
720
+ full_frame_name = frame_sql(frame_name, schema=schema)
721
+
722
+ sql = f"""
723
+ DELETE FROM {full_frame_name}
724
+ USING {temp_table} AS t
725
+ WHERE {full_frame_name}.{id_col} = t.id;
726
+ """
727
+
728
+ 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 = 31
3
+ PATCH_VERSION = 32
4
4
  version = '{}.{}.{}'.format(MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION)
5
5
  __all__ = ['MAJOR_VERSION', 'MINOR_VERSION', 'PATCH_VERSION', 'version']
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mtsql
3
- Version: 1.12.31
3
+ Version: 1.12.32
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
6
  Author: Minh-Tri Pham
@@ -1,17 +1,17 @@
1
1
  mt/sql/__init__.py,sha256=b7zO50apZxt9Hg2eOkJhRLrXgACR8eS5b-Rphdn5qNQ,44
2
- mt/sql/base.py,sha256=CLFKd-rJtIUGLm37WtxeFLzhX48LgGpEoz6noR4qfXo,16510
2
+ mt/sql/base.py,sha256=kCJCLHbxO0wI7Mmx7VrCtDGcjd01_vuOfuK2XuXd5SE,20398
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=e7OodvUPDd0qLszM8zWrGxkmyhHFVodAR8B3CnSzs7g,208
6
+ mt/sql/version.py,sha256=38xiRL1MFfoE8EAiIn2HKJfyUv857BOba8101AxPUkM,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.31.dist-info/licenses/LICENSE,sha256=PojkRlQzTT5Eg6Nj03XoIVEefN3u8iiIFf1p4rqe_t4,1070
14
- mtsql-1.12.31.dist-info/METADATA,sha256=bJET1fnqtrkAZ1Qz8UzM7c2E0PL1X65I5AN1h6wlY1Q,734
15
- mtsql-1.12.31.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
16
- mtsql-1.12.31.dist-info/top_level.txt,sha256=WcqGFu9cV7iMZg09iam8eNxUvGpLSKKF2Iubf6SJVOo,3
17
- mtsql-1.12.31.dist-info/RECORD,,
13
+ mtsql-1.12.32.dist-info/licenses/LICENSE,sha256=PojkRlQzTT5Eg6Nj03XoIVEefN3u8iiIFf1p4rqe_t4,1070
14
+ mtsql-1.12.32.dist-info/METADATA,sha256=mDrQHEVcsBSQ0_Clr-aMOW2aqIOWcCAf7RzPYvzmG9I,734
15
+ mtsql-1.12.32.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
16
+ mtsql-1.12.32.dist-info/top_level.txt,sha256=WcqGFu9cV7iMZg09iam8eNxUvGpLSKKF2Iubf6SJVOo,3
17
+ mtsql-1.12.32.dist-info/RECORD,,