dbhydra 2.1.1__tar.gz → 2.1.2__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.
Files changed (29) hide show
  1. {dbhydra-2.1.1 → dbhydra-2.1.2}/PKG-INFO +1 -1
  2. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/src/abstract_db.py +3 -0
  3. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/src/abstract_table.py +5 -1
  4. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/src/tables.py +19 -1
  5. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/src/xlsx_db.py +8 -6
  6. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra.egg-info/PKG-INFO +1 -1
  7. {dbhydra-2.1.1 → dbhydra-2.1.2}/setup.py +1 -1
  8. {dbhydra-2.1.1 → dbhydra-2.1.2}/LICENSE +0 -0
  9. {dbhydra-2.1.1 → dbhydra-2.1.2}/README.md +0 -0
  10. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/__init__.py +0 -0
  11. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/dbhydra_core.py +0 -0
  12. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/src/__init__.py +0 -0
  13. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/src/bigquery_db.py +0 -0
  14. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/src/errors/__init__.py +0 -0
  15. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/src/errors/exceptions.py +0 -0
  16. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/src/migrator.py +0 -0
  17. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/src/mongo_db.py +0 -0
  18. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/src/mysql_db.py +0 -0
  19. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/src/postgres_db.py +0 -0
  20. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/src/sqlserver_db.py +0 -0
  21. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/tests/__init__.py +0 -0
  22. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/tests/test_cases.py +0 -0
  23. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/tests/test_mongo.py +0 -0
  24. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra/tests/test_sql.py +0 -0
  25. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra.egg-info/SOURCES.txt +0 -0
  26. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra.egg-info/dependency_links.txt +0 -0
  27. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra.egg-info/requires.txt +0 -0
  28. {dbhydra-2.1.1 → dbhydra-2.1.2}/dbhydra.egg-info/top_level.txt +0 -0
  29. {dbhydra-2.1.1 → dbhydra-2.1.2}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dbhydra
3
- Version: 2.1.1
3
+ Version: 2.1.2
4
4
  Summary: Data science friendly ORM combining Python
5
5
  Home-page: https://github.com/DovaX/dbhydra
6
6
  Author: DovaX
@@ -1,10 +1,12 @@
1
1
  import abc
2
2
  import threading
3
3
  from contextlib import contextmanager
4
+ from typing import Optional
4
5
 
5
6
  from dbhydra.src.migrator import Migrator
6
7
  from dbhydra.src.tables import AbstractTable
7
8
 
9
+
8
10
  def read_connection_details(config_file):
9
11
  def read_file(file):
10
12
  """Reads txt file -> list"""
@@ -102,6 +104,7 @@ class AbstractDb(abc.ABC):
102
104
  # self.connect_to_db()
103
105
 
104
106
  self.active_transactions=[]
107
+ self.last_table_inserted_into: Optional[str] = None
105
108
 
106
109
  @abc.abstractmethod
107
110
  def connect_locally(self):
@@ -357,6 +357,8 @@ class AbstractTable(AbstractJoinable, abc.ABC):
357
357
 
358
358
  return df_copy
359
359
 
360
+ def extract_last_id(self) -> Any:
361
+ raise NotImplementedError("Method not implemented for this subclass")
360
362
 
361
363
  def insert_from_df(self, df, batch=1, try_mode=False, debug_mode=False, adjust_df=False, insert_id=False):
362
364
  if debug_mode:
@@ -403,7 +405,9 @@ class AbstractTable(AbstractJoinable, abc.ABC):
403
405
  # rows[i][j] = "'" + record + "'"
404
406
  #print(rows)
405
407
  rows = df.values.tolist()
406
- return self.insert(rows, batch=batch, try_mode=try_mode, debug_mode=False, insert_id=insert_id)
408
+ result = self.insert(rows, batch=batch, try_mode=try_mode, debug_mode=False, insert_id=insert_id)
409
+ self.db1.last_table_inserted_into = self.name
410
+ return result
407
411
 
408
412
  #TODO: need to solve inserting in different column_order
409
413
  #check df column names, permute if needed
@@ -675,6 +675,18 @@ class MysqlTable(AbstractTable):
675
675
 
676
676
  return python_types
677
677
 
678
+ def extract_last_id(self) -> Any:
679
+ """
680
+ Extract the last inserted ID from the DB.
681
+
682
+ LAST_INSERT_ID exists in the DB connection context, therefore is safe to use if DB session is request-scoped
683
+ In this case we only use global connection, but we use Lock to ensure thread-safety across different requests
684
+ This is a go-to mechanism for extracting the ID of the inserted record for multiple SQL DBs,
685
+ altho this specific query is applicable only to MySQL.
686
+ """
687
+ assert self.name == self.db1.last_table_inserted_into, "Last table inserted into is not the same as the table being queried"
688
+ return self.select("SELECT LAST_INSERT_ID()")[0][0]
689
+
678
690
  def get_nullable_columns(self):
679
691
  information_schema_table = MysqlTable(self.db1, 'INFORMATION_SCHEMA.COLUMNS')
680
692
  query = f"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA = '{self.db1.DB_DATABASE}' and TABLE_NAME = '{self.name}' and IS_NULLABLE = 'YES'"
@@ -1014,6 +1026,7 @@ class XlsxTable(AbstractTable):
1014
1026
  df.reset_index(drop=True, inplace=True)
1015
1027
 
1016
1028
  self._save_table(df)
1029
+ self.last_table_inserted_into = self.name
1017
1030
 
1018
1031
  def replace_from_df(self, df):
1019
1032
  assert len(df.columns) == len(self.columns) # +1 because of id column
@@ -1113,4 +1126,9 @@ class XlsxTable(AbstractTable):
1113
1126
  df.drop(df[df[where_variable] == where_value].index, inplace=True)
1114
1127
  deleted_count = number_of_records - len(df)
1115
1128
  self.replace_from_df(df)
1116
- return deleted_count
1129
+ return deleted_count
1130
+
1131
+ def extract_last_id(self) -> Any:
1132
+ assert self.name == self.db1.last_table_inserted_into, "Last table inserted into is not the same as the table being queried"
1133
+ df = self.select_to_df()
1134
+ return df.iloc[-1][self.id_column_name]
@@ -1,10 +1,11 @@
1
- from dbhydra.src.abstract_db import AbstractDb
2
- from dbhydra.src.tables import XlsxTable
3
-
4
1
  import contextlib
5
- import threading
6
- import pathlib
7
2
  import os
3
+ import pathlib
4
+ import threading
5
+ from typing import Optional
6
+
7
+ from dbhydra.src.abstract_db import AbstractDb
8
+ from dbhydra.src.tables import XlsxTable
8
9
 
9
10
 
10
11
  class XlsxDb(AbstractDb):
@@ -29,7 +30,8 @@ class XlsxDb(AbstractDb):
29
30
  if self.db_directory_path is None:
30
31
  self.db_directory_path = pathlib.Path(self.name)
31
32
 
32
-
33
+ self.last_table_inserted_into: Optional[str] = None
34
+
33
35
  self.python_database_type_mapping = {
34
36
  'int': "int",
35
37
  'float': "double",
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dbhydra
3
- Version: 2.1.1
3
+ Version: 2.1.2
4
4
  Summary: Data science friendly ORM combining Python
5
5
  Home-page: https://github.com/DovaX/dbhydra
6
6
  Author: DovaX
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
5
5
 
6
6
  setuptools.setup(
7
7
  name='dbhydra',
8
- version='2.1.1',
8
+ version='2.1.2',
9
9
  author='DovaX',
10
10
  author_email='dovax.ai@gmail.com',
11
11
  description='Data science friendly ORM combining Python',
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes