mtsql 1.12.30__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 +144 -3
- mt/sql/version.py +1 -1
- {mtsql-1.12.30.dist-info → mtsql-1.12.32.dist-info}/METADATA +1 -1
- {mtsql-1.12.30.dist-info → mtsql-1.12.32.dist-info}/RECORD +7 -7
- {mtsql-1.12.30.dist-info → mtsql-1.12.32.dist-info}/WHEEL +0 -0
- {mtsql-1.12.30.dist-info → mtsql-1.12.32.dist-info}/licenses/LICENSE +0 -0
- {mtsql-1.12.30.dist-info → mtsql-1.12.32.dist-info}/top_level.txt +0 -0
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
|
|
|
@@ -377,7 +380,7 @@ def table_exists(
|
|
|
377
380
|
def create_temp_id_table(
|
|
378
381
|
l_ids: list,
|
|
379
382
|
conn: sa.engine.Connection,
|
|
380
|
-
int_type="int",
|
|
383
|
+
int_type: str = "int",
|
|
381
384
|
chunksize: int = 1000000,
|
|
382
385
|
logger: tp.Optional[logg.IndentedLoggerAdapter] = None,
|
|
383
386
|
) -> str:
|
|
@@ -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}"
|
|
@@ -504,6 +550,7 @@ def find_common_ids(
|
|
|
504
550
|
engine: sa.engine.Engine,
|
|
505
551
|
schema: tp.Optional[str] = None,
|
|
506
552
|
id_col: str = "id",
|
|
553
|
+
int_type: str = "int",
|
|
507
554
|
logger: tp.Optional[logg.IndentedLoggerAdapter] = None,
|
|
508
555
|
) -> tp.List[int]:
|
|
509
556
|
"""Finds common ids between a list of ids and the ids in a given frame.
|
|
@@ -520,6 +567,8 @@ def find_common_ids(
|
|
|
520
567
|
schema of the frame. If None, the default schema is used.
|
|
521
568
|
id_col : str
|
|
522
569
|
name of the id column in the frame
|
|
570
|
+
int_type : str
|
|
571
|
+
an SQL string representing the int type of the id column
|
|
523
572
|
logger : mt.logg.IndentedLoggerAdapter, optional
|
|
524
573
|
logger for debugging
|
|
525
574
|
|
|
@@ -530,7 +579,56 @@ def find_common_ids(
|
|
|
530
579
|
"""
|
|
531
580
|
|
|
532
581
|
with conn_ctx(engine) as conn:
|
|
533
|
-
temp_table = create_temp_id_table(l_ids, conn, logger=logger)
|
|
582
|
+
temp_table = create_temp_id_table(l_ids, conn, int_type=int_type, logger=logger)
|
|
583
|
+
|
|
584
|
+
full_frame_name = frame_sql(frame_name, schema=schema)
|
|
585
|
+
|
|
586
|
+
sql = f"""
|
|
587
|
+
SELECT t.id FROM {temp_table} AS t
|
|
588
|
+
INNER JOIN {full_frame_name} AS f
|
|
589
|
+
ON t.id = f.{id_col};
|
|
590
|
+
"""
|
|
591
|
+
|
|
592
|
+
df_common = read_sql(sql, conn, index_col=None, logger=logger)
|
|
593
|
+
|
|
594
|
+
l_commonIds = df_common["id"].tolist()
|
|
595
|
+
|
|
596
|
+
return l_commonIds
|
|
597
|
+
|
|
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)
|
|
534
632
|
|
|
535
633
|
full_frame_name = frame_sql(frame_name, schema=schema)
|
|
536
634
|
|
|
@@ -553,6 +651,7 @@ def remove_records_by_id(
|
|
|
553
651
|
engine: sa.engine.Engine,
|
|
554
652
|
schema: tp.Optional[str] = None,
|
|
555
653
|
id_col: str = "id",
|
|
654
|
+
int_type: str = "int",
|
|
556
655
|
logger: tp.Optional[logg.IndentedLoggerAdapter] = None,
|
|
557
656
|
):
|
|
558
657
|
"""Removes records from a frame by a list of ids.
|
|
@@ -569,12 +668,54 @@ def remove_records_by_id(
|
|
|
569
668
|
schema of the frame. If None, the default schema is used.
|
|
570
669
|
id_col : str
|
|
571
670
|
name of the id column in the frame
|
|
671
|
+
int_type : str
|
|
672
|
+
an SQL string representing the int type of the id column
|
|
673
|
+
logger : mt.logg.IndentedLoggerAdapter, optional
|
|
674
|
+
logger for debugging
|
|
675
|
+
"""
|
|
676
|
+
|
|
677
|
+
with conn_ctx(engine) as conn:
|
|
678
|
+
temp_table = create_temp_id_table(l_ids, conn, int_type=int_type, logger=logger)
|
|
679
|
+
|
|
680
|
+
full_frame_name = frame_sql(frame_name, schema=schema)
|
|
681
|
+
|
|
682
|
+
sql = f"""
|
|
683
|
+
DELETE FROM {full_frame_name}
|
|
684
|
+
USING {temp_table} AS t
|
|
685
|
+
WHERE {full_frame_name}.{id_col} = t.id;
|
|
686
|
+
"""
|
|
687
|
+
|
|
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
|
|
572
713
|
logger : mt.logg.IndentedLoggerAdapter, optional
|
|
573
714
|
logger for debugging
|
|
574
715
|
"""
|
|
575
716
|
|
|
576
717
|
with conn_ctx(engine) as conn:
|
|
577
|
-
temp_table =
|
|
718
|
+
temp_table = create_temp_str_id_table(l_ids, conn, logger=logger)
|
|
578
719
|
|
|
579
720
|
full_frame_name = frame_sql(frame_name, schema=schema)
|
|
580
721
|
|
mt/sql/version.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mtsql
|
|
3
|
-
Version: 1.12.
|
|
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=
|
|
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=
|
|
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.
|
|
14
|
-
mtsql-1.12.
|
|
15
|
-
mtsql-1.12.
|
|
16
|
-
mtsql-1.12.
|
|
17
|
-
mtsql-1.12.
|
|
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,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|