dbhydra 2.3.0__tar.gz → 2.3.3__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 (31) hide show
  1. {dbhydra-2.3.0 → dbhydra-2.3.3}/PKG-INFO +7 -1
  2. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/dbhydra_core.py +5 -0
  3. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/src/abstract_table.py +32 -8
  4. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/src/mysql_db.py +1 -0
  5. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/src/tables.py +11 -4
  6. dbhydra-2.3.3/dbhydra/tests/test_mysql_ddl.py +91 -0
  7. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra.egg-info/PKG-INFO +7 -1
  8. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra.egg-info/SOURCES.txt +1 -0
  9. {dbhydra-2.3.0 → dbhydra-2.3.3}/setup.py +1 -1
  10. {dbhydra-2.3.0 → dbhydra-2.3.3}/LICENSE +0 -0
  11. {dbhydra-2.3.0 → dbhydra-2.3.3}/README.md +0 -0
  12. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/__init__.py +0 -0
  13. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/src/__init__.py +0 -0
  14. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/src/abstract_db.py +0 -0
  15. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/src/bigquery_db.py +0 -0
  16. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/src/errors/__init__.py +0 -0
  17. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/src/errors/exceptions.py +0 -0
  18. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/src/migrator.py +0 -0
  19. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/src/mongo_db.py +0 -0
  20. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/src/postgres_db.py +0 -0
  21. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/src/sqlserver_db.py +0 -0
  22. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/src/xlsx_db.py +0 -0
  23. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/test_migrator.py +0 -0
  24. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/tests/__init__.py +0 -0
  25. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/tests/test_cases.py +0 -0
  26. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/tests/test_mongo.py +0 -0
  27. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra/tests/test_sql.py +0 -0
  28. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra.egg-info/dependency_links.txt +0 -0
  29. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra.egg-info/requires.txt +0 -0
  30. {dbhydra-2.3.0 → dbhydra-2.3.3}/dbhydra.egg-info/top_level.txt +0 -0
  31. {dbhydra-2.3.0 → dbhydra-2.3.3}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dbhydra
3
- Version: 2.3.0
3
+ Version: 2.3.3
4
4
  Summary: Data science friendly ORM combining Python
5
5
  Home-page: https://github.com/DovaX/dbhydra
6
6
  Author: DovaX
@@ -11,6 +11,12 @@ Classifier: Operating System :: OS Independent
11
11
  Requires-Python: >=3.6
12
12
  Description-Content-Type: text/markdown
13
13
  License-File: LICENSE
14
+ Requires-Dist: pyodbc
15
+ Requires-Dist: pandas
16
+ Requires-Dist: pymysql
17
+ Requires-Dist: pymongo
18
+ Requires-Dist: google-cloud-bigquery
19
+ Requires-Dist: pydantic
14
20
 
15
21
  # dbhydra
16
22
  Data science friendly ORM (Object Relational Mapping) library combining Python, Pandas, and various SQL dialects
@@ -27,6 +27,11 @@ class Blob(str):
27
27
  pass
28
28
 
29
29
 
30
+ class LongText(str):
31
+ """Marker type for columns that should be mapped to LONGTEXT in SQL backends."""
32
+ pass
33
+
34
+
30
35
  # dataframe - dictionary auxiliary functions
31
36
  def df_to_dict(df, column1, column2):
32
37
  dictionary = df.set_index(column1).to_dict()[column2]
@@ -134,18 +134,32 @@ class AbstractSelectable:
134
134
  return(rows)
135
135
 
136
136
 
137
- def select_all(self, debug_mode = False):
137
+ def select_all(self, debug_mode = False, limit: Optional[int] = None, offset: Optional[int] = None):
138
138
  quote = self.db1.identifier_quote
139
139
  all_cols_query = ""
140
140
  for col in self.columns:
141
141
  all_cols_query = all_cols_query + quote + col + quote + ","
142
142
  if all_cols_query[-1] == ",":
143
143
  all_cols_query = all_cols_query[:-1]
144
- list1 = self.select(f"SELECT {all_cols_query} FROM {quote}{self.name}{quote};", debug_mode = debug_mode)
144
+
145
+ query = f"SELECT {all_cols_query} FROM {quote}{self.name}{quote}"
146
+
147
+ # Add LIMIT and OFFSET to the query
148
+ if limit is not None and limit > 0:
149
+ if offset is not None and offset > 0:
150
+ query += f" LIMIT {offset}, {limit}"
151
+ else:
152
+ query += f" LIMIT {limit}"
153
+ elif offset is not None and offset > 0:
154
+ # MySQL requires LIMIT when using OFFSET, use a large number for all remaining rows
155
+ query += f" LIMIT {offset}, 18446744073709551615"
156
+
157
+ query += ";"
158
+ list1 = self.select(query, debug_mode = debug_mode)
145
159
  return (list1)
146
160
 
147
- def select_to_df(self, debug_mode = False):
148
- rows = self.select_all(debug_mode = debug_mode)
161
+ def select_to_df(self, debug_mode = False, limit: Optional[int] = None, offset: Optional[int] = None):
162
+ rows = self.select_all(debug_mode = debug_mode, limit = limit, offset = offset)
149
163
  if self.query_building_enabled:
150
164
  self.to_df()
151
165
  df=None
@@ -269,10 +283,20 @@ class AbstractTable(AbstractJoinable, abc.ABC):
269
283
 
270
284
  def update(self, variable_assign, where=None, debug_mode = False):
271
285
  quote = self.db1.identifier_quote
272
- if where is None:
273
- query = f"UPDATE {quote}{self.name}{quote} SET {quote}{variable_assign}{quote}"
274
- else:
275
- query = f"UPDATE {quote}{self.name}{quote} SET {quote}{variable_assign}{quote} WHERE {quote}{where}{quote}"
286
+
287
+ assert "=" in variable_assign
288
+ assigned_variable,assigned_value=variable_assign.split("=").strip()
289
+
290
+ query = f"UPDATE {quote}{self.name}{quote} SET {quote}{assigned_variable}{quote} = {assigned_value}"
291
+
292
+ if where:
293
+ query += f" WHERE {where}"
294
+
295
+ #Old broken implementation it gives `` for the SQL syntax query
296
+ # if where is None:
297
+ # query = f"UPDATE {quote}{self.name}{quote} SET {quote}{variable_assign}{quote}"
298
+ # else:
299
+ # query = f"UPDATE {quote}{self.name}{quote} SET {quote}{variable_assign}{quote} WHERE {quote}{where}{quote}"
276
300
 
277
301
  if debug_mode:
278
302
  print(query)
@@ -21,6 +21,7 @@ class MysqlDb(AbstractDb):
21
21
  'datetime': "datetime",
22
22
  'Jsonable': "json",
23
23
  'Blob': "mediumblob",
24
+ 'LongText': "longtext",
24
25
  }
25
26
 
26
27
  def __init__(self, *args, **kwargs):
@@ -38,7 +38,6 @@ PYTHON_TO_MYSQL_DATA_MAPPING = {
38
38
 
39
39
  def save_migration(function, *args, **kw): # decorator
40
40
  def new_function(instance, *args, **kw):
41
- print("TOTO TU")
42
41
  print(instance)
43
42
  print(*args)
44
43
  command = function.__name__
@@ -63,9 +62,12 @@ def save_migration(function, *args, **kw): # decorator
63
62
  print(migration_dict)
64
63
  # TODO: add other methods
65
64
 
66
- migrator = instance.db1.migrator
67
- migrator.migration_list.append(migration_dict)
68
- # migrator.migration_list_to_json()
65
+ if hasattr(instance.db1, 'migrator'):
66
+ migrator = instance.db1.migrator
67
+ migrator.migration_list.append(migration_dict)
68
+ # migrator.migration_list_to_json()
69
+ else:
70
+ print(f"[save_migration] WARNING: db1 object of type {type(instance.db1)} has no 'migrator' attribute. Migration not saved.")
69
71
  function(instance, *args, **kw)
70
72
 
71
73
  return (new_function)
@@ -806,6 +808,11 @@ class MysqlTable(AbstractTable):
806
808
  query += "'" + str(rows[k][j]) + "',"
807
809
  elif "json" in self.types[j + start_index]:
808
810
  query += f"'{rows[k][j]}', "
811
+ elif "text" in self.types[j + start_index]:
812
+ # Covers text, mediumtext, longtext
813
+ if replace_apostrophes:
814
+ rows[k][j] = str(rows[k][j]).replace("'", "")
815
+ query += "'" + str(rows[k][j]) + "',"
809
816
  elif 'blob' in self.types[j + start_index]:
810
817
  # Convert to hex to allow insertion into SQL query
811
818
  hex_data = binascii.hexlify(rows[k][j]).decode('ascii')
@@ -0,0 +1,91 @@
1
+ ##### DDL (data definition language) tests for MySQL #####
2
+
3
+ import os
4
+ import pytest
5
+ import random
6
+ import string
7
+ import dbhydra.dbhydra_core as dh
8
+
9
+ def random_table_name(prefix="test_table_"):
10
+ return prefix + ''.join(random.choices(string.ascii_lowercase + string.digits, k=8))
11
+
12
+ # Rename mysqldb fixture and all references to db1
13
+ @pytest.fixture(scope="module")
14
+ def db1():
15
+ # Get the directory of this test file, then go up one level to the project root
16
+ root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
17
+ config_path = os.path.join(root_dir, "config-mysql.ini")
18
+ return dh.MysqlDb(config_file=config_path)
19
+
20
+ @pytest.fixture(scope="function")
21
+ def temp_mysql_table(db1):
22
+ table_name = random_table_name()
23
+ columns = ["id", "name"]
24
+ types = ["int", "varchar(255)"]
25
+ table = dh.MysqlTable(db1, table_name, columns, types)
26
+ with db1.connect_to_db():
27
+ table.create()
28
+ table_dict = db1.generate_table_dict()
29
+ assert table_name in table_dict, f"Temp table {table_name} was not created!"
30
+ yield table
31
+ # Cleanup
32
+ try:
33
+ with db1.connect_to_db():
34
+ table.drop()
35
+ table_dict = db1.generate_table_dict()
36
+ assert table_name not in table_dict, f"Temp table {table_name} was not dropped!"
37
+ except Exception as e:
38
+ print(f"[CLEANUP] Could not drop table {table_name}: {e}")
39
+ raise
40
+
41
+ def test_mysql_create_and_drop_table(db1):
42
+ table_name = random_table_name()
43
+ columns = ["id", "name"]
44
+ types = ["int", "varchar(255)"]
45
+ table = dh.MysqlTable(db1, table_name, columns, types)
46
+ with db1.connect_to_db():
47
+ table.create()
48
+ table_dict = db1.generate_table_dict()
49
+ assert table_name in table_dict, f"Table {table_name} was not created!"
50
+ table.drop()
51
+ table_dict = db1.generate_table_dict()
52
+ assert table_name not in table_dict, f"Table {table_name} was not dropped!"
53
+
54
+ def test_mysql_add_column(temp_mysql_table, db1):
55
+ table = temp_mysql_table
56
+ column = "age"
57
+ type = "int"
58
+ with db1.connect_to_db():
59
+ table.add_column(column, type)
60
+ df = table.select_to_df()
61
+ columns = df.columns.tolist()
62
+ assert column in columns, f"Column '{column}' was not added! Columns: {columns}"
63
+
64
+ def test_mysql_drop_column(temp_mysql_table, db1):
65
+ table = temp_mysql_table
66
+ column = "age"
67
+ type = "int"
68
+ with db1.connect_to_db():
69
+ table.add_column(column, type)
70
+ df = table.select_to_df()
71
+ columns = df.columns.tolist()
72
+ assert column in columns, f"Column '{column}' was not added! Columns: {columns}"
73
+ table.drop_column(column)
74
+ df = table.select_to_df()
75
+ columns = df.columns.tolist()
76
+ assert column not in columns, f"Column '{column}' was not dropped! Columns: {columns}"
77
+
78
+ def test_mysql_modify_column(temp_mysql_table, db1):
79
+ table = temp_mysql_table
80
+ column = "age"
81
+ type = "int"
82
+ with db1.connect_to_db():
83
+ table.add_column(column, type)
84
+ df = table.select_to_df()
85
+ columns = df.columns.tolist()
86
+ assert column in columns, f"Column '{column}' was not added! Columns: {columns}"
87
+ # Modify column type
88
+ type2 = "varchar(100)"
89
+ table.modify_column(column, type2)
90
+ types = table.get_all_types()
91
+ assert any("varchar" in t for t in types), f"Column '{column}' was not modified to varchar! Types: {types}"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dbhydra
3
- Version: 2.3.0
3
+ Version: 2.3.3
4
4
  Summary: Data science friendly ORM combining Python
5
5
  Home-page: https://github.com/DovaX/dbhydra
6
6
  Author: DovaX
@@ -11,6 +11,12 @@ Classifier: Operating System :: OS Independent
11
11
  Requires-Python: >=3.6
12
12
  Description-Content-Type: text/markdown
13
13
  License-File: LICENSE
14
+ Requires-Dist: pyodbc
15
+ Requires-Dist: pandas
16
+ Requires-Dist: pymysql
17
+ Requires-Dist: pymongo
18
+ Requires-Dist: google-cloud-bigquery
19
+ Requires-Dist: pydantic
14
20
 
15
21
  # dbhydra
16
22
  Data science friendly ORM (Object Relational Mapping) library combining Python, Pandas, and various SQL dialects
@@ -25,4 +25,5 @@ dbhydra/src/errors/exceptions.py
25
25
  dbhydra/tests/__init__.py
26
26
  dbhydra/tests/test_cases.py
27
27
  dbhydra/tests/test_mongo.py
28
+ dbhydra/tests/test_mysql_ddl.py
28
29
  dbhydra/tests/test_sql.py
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
5
5
 
6
6
  setuptools.setup(
7
7
  name='dbhydra',
8
- version='2.3.0',
8
+ version='2.3.3',
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