database-wrapper 0.1.38__tar.gz → 0.1.42__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 (21) hide show
  1. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/PKG-INFO +5 -5
  2. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/database_wrapper/config.py +3 -3
  3. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/database_wrapper/db_wrapper.py +23 -69
  4. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/database_wrapper/db_wrapper_async.py +15 -68
  5. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/database_wrapper/db_wrapper_mixin.py +13 -5
  6. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/database_wrapper.egg-info/PKG-INFO +5 -5
  7. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/database_wrapper.egg-info/requires.txt +4 -4
  8. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/pyproject.toml +5 -5
  9. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/README.md +0 -0
  10. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/database_wrapper/__init__.py +0 -0
  11. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/database_wrapper/common.py +0 -0
  12. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/database_wrapper/db_backend.py +0 -0
  13. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/database_wrapper/db_data_model.py +0 -0
  14. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/database_wrapper/py.typed +0 -0
  15. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/database_wrapper/utils/__init__.py +0 -0
  16. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/database_wrapper/utils/dataclass_addons.py +0 -0
  17. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/database_wrapper/utils/timer.py +0 -0
  18. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/database_wrapper.egg-info/SOURCES.txt +0 -0
  19. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/database_wrapper.egg-info/dependency_links.txt +0 -0
  20. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/database_wrapper.egg-info/top_level.txt +0 -0
  21. {database_wrapper-0.1.38 → database_wrapper-0.1.42}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: database_wrapper
3
- Version: 0.1.38
3
+ Version: 0.1.42
4
4
  Summary: A Different Approach to Database Wrappers in Python
5
5
  Author-email: Gints Murans <gm@gm.lv>
6
6
  License: GNU General Public License v3.0 (GPL-3.0)
@@ -33,13 +33,13 @@ Classifier: Topic :: Software Development :: Libraries :: Python Modules
33
33
  Requires-Python: >=3.8
34
34
  Description-Content-Type: text/markdown
35
35
  Provides-Extra: pgsql
36
- Requires-Dist: database_wrapper_pgsql==0.1.38; extra == "pgsql"
36
+ Requires-Dist: database_wrapper_pgsql==0.1.42; extra == "pgsql"
37
37
  Provides-Extra: mysql
38
- Requires-Dist: database_wrapper_mysql==0.1.38; extra == "mysql"
38
+ Requires-Dist: database_wrapper_mysql==0.1.42; extra == "mysql"
39
39
  Provides-Extra: mssql
40
- Requires-Dist: database_wrapper_mssql==0.1.38; extra == "mssql"
40
+ Requires-Dist: database_wrapper_mssql==0.1.42; extra == "mssql"
41
41
  Provides-Extra: sqlite
42
- Requires-Dist: database_wrapper_sqlite==0.1.38; extra == "sqlite"
42
+ Requires-Dist: database_wrapper_sqlite==0.1.42; extra == "sqlite"
43
43
  Provides-Extra: all
44
44
  Requires-Dist: database_wrapper[mssql,mysql,pgsql,sqlite]; extra == "all"
45
45
  Provides-Extra: dev
@@ -3,7 +3,7 @@ from typing import Any
3
3
  CONFIG: dict[str, Any] = {
4
4
  # These are supposed to be set automatically by a git pre-compile script
5
5
  # They are one git commit hash behind, if used automatically
6
- "git_commit_hash": "5160789e2c4bdb5815a162e04dca5392b820c3f5",
7
- "git_commit_date": "11.11.2024 22:26",
8
- "app_version": "0.1.38",
6
+ "git_commit_hash": "365f062181488aa4bd2f56e694fce6b26a113b1b",
7
+ "git_commit_date": "15.11.2024 02:01",
8
+ "app_version": "0.1.42",
9
9
  }
@@ -42,7 +42,14 @@ class DBWrapper(DBWrapperMixin):
42
42
  Returns:
43
43
  The created cursor object.
44
44
  """
45
- assert self.db is not None, "Database connection is not set"
45
+ if self.db is None and self.dbConn is None:
46
+ raise ValueError(
47
+ "Database object and connection is not properly initialized"
48
+ )
49
+
50
+ if self.dbConn is not None:
51
+ return self.dbConn.cursor()
52
+
46
53
  return self.db.cursor
47
54
 
48
55
  #####################
@@ -65,12 +72,7 @@ class DBWrapper(DBWrapperMixin):
65
72
  Returns:
66
73
  DataModelType | None: The result of the query.
67
74
  """
68
- # Query and filter
69
- _query = (
70
- customQuery
71
- or emptyDataClass.queryBase()
72
- or self.filterQuery(emptyDataClass.schemaName, emptyDataClass.tableName)
73
- )
75
+ # Figure out the id key and value
74
76
  idKey = emptyDataClass.idKey
75
77
  idValue = emptyDataClass.id
76
78
  if not idKey:
@@ -78,32 +80,12 @@ class DBWrapper(DBWrapperMixin):
78
80
  if not idValue:
79
81
  raise ValueError("Id value is not set")
80
82
 
81
- _filter = f"WHERE {self.makeIdentifier(emptyDataClass.tableAlias, idKey)} = %s"
82
- _params = (idValue,)
83
-
84
- # Create a SQL object for the query and format it
85
- querySql = self._formatFilterQuery(_query, _filter, None, None)
86
-
87
- # Create a new cursor
88
- newCursor = self.createCursor(emptyDataClass)
89
-
90
- # Log
91
- self.logQuery(newCursor, querySql, _params)
92
-
93
- # Load data
94
- try:
95
- newCursor.execute(querySql, _params)
96
-
97
- # Fetch one row
98
- row = newCursor.fetchone()
99
- if row is None:
100
- return
101
-
102
- # Turn data into model
103
- return self.turnDataIntoModel(emptyDataClass, row)
104
- finally:
105
- # Close the cursor
106
- newCursor.close()
83
+ # Get the record
84
+ res = self.getAll(
85
+ emptyDataClass, idKey, idValue, limit=1, customQuery=customQuery
86
+ )
87
+ for row in res:
88
+ return row
107
89
 
108
90
  def getByKey(
109
91
  self,
@@ -124,39 +106,12 @@ class DBWrapper(DBWrapperMixin):
124
106
  Returns:
125
107
  DataModelType | None: The result of the query.
126
108
  """
127
- # Query and filter
128
- _query = (
129
- customQuery
130
- or emptyDataClass.queryBase()
131
- or self.filterQuery(emptyDataClass.schemaName, emptyDataClass.tableName)
109
+ # Get the record
110
+ res = self.getAll(
111
+ emptyDataClass, idKey, idValue, limit=1, customQuery=customQuery
132
112
  )
133
- _filter = f"WHERE {self.makeIdentifier(emptyDataClass.tableAlias, idKey)} = %s"
134
- _params = (idValue,)
135
-
136
- # Create a SQL object for the query and format it
137
- querySql = self._formatFilterQuery(_query, _filter, None, None)
138
-
139
- # Create a new cursor
140
- newCursor = self.createCursor(emptyDataClass)
141
-
142
- # Log
143
- self.logQuery(newCursor, querySql, _params)
144
-
145
- # Load data
146
- try:
147
- newCursor.execute(querySql, _params)
148
-
149
- # Fetch one row
150
- row = newCursor.fetchone()
151
- if row is None:
152
- return
153
-
154
- # Turn data into model
155
- return self.turnDataIntoModel(emptyDataClass, row)
156
-
157
- finally:
158
- # Close the cursor
159
- newCursor.close()
113
+ for row in res:
114
+ return row
160
115
 
161
116
  def getAll(
162
117
  self,
@@ -191,11 +146,10 @@ class DBWrapper(DBWrapperMixin):
191
146
  )
192
147
  _params: tuple[Any, ...] = ()
193
148
  _filter = ""
149
+
150
+ # TODO: Rewrite this so that filter method with loop is not used here
194
151
  if idKey and idValue:
195
- _filter = (
196
- f"WHERE {self.makeIdentifier(emptyDataClass.tableAlias, idKey)} = %s"
197
- )
198
- _params = (idValue,)
152
+ (_filter, _params) = self.createFilter({idKey: idValue})
199
153
 
200
154
  # Order and limit
201
155
  _order = self.orderQuery(orderBy)
@@ -66,12 +66,7 @@ class DBWrapperAsync(DBWrapperMixin):
66
66
  Returns:
67
67
  DataModelType | None: The result of the query.
68
68
  """
69
- # Query and filter
70
- _query = (
71
- customQuery
72
- or emptyDataClass.queryBase()
73
- or self.filterQuery(emptyDataClass.schemaName, emptyDataClass.tableName)
74
- )
69
+ # Figure out the id key and value
75
70
  idKey = emptyDataClass.idKey
76
71
  idValue = emptyDataClass.id
77
72
  if not idKey:
@@ -79,32 +74,12 @@ class DBWrapperAsync(DBWrapperMixin):
79
74
  if not idValue:
80
75
  raise ValueError("Id value is not set")
81
76
 
82
- _filter = f"WHERE {self.makeIdentifier(emptyDataClass.tableAlias, idKey)} = %s"
83
- _params = (idValue,)
84
-
85
- # Create a SQL object for the query and format it
86
- querySql = self._formatFilterQuery(_query, _filter, None, None)
87
-
88
- # Create a new cursor
89
- newCursor = await self.createCursor(emptyDataClass)
90
-
91
- # Log
92
- self.logQuery(newCursor, querySql, _params)
93
-
94
- # Load data
95
- try:
96
- await newCursor.execute(querySql, _params)
97
-
98
- # Fetch one row
99
- row = await newCursor.fetchone()
100
- if row is None:
101
- return
102
-
103
- # Turn data into model
104
- return self.turnDataIntoModel(emptyDataClass, row)
105
- finally:
106
- # Close the cursor
107
- await newCursor.close()
77
+ # Get the record
78
+ res = self.getAll(
79
+ emptyDataClass, idKey, idValue, limit=1, customQuery=customQuery
80
+ )
81
+ async for row in res:
82
+ return row
108
83
 
109
84
  async def getByKey(
110
85
  self,
@@ -125,39 +100,12 @@ class DBWrapperAsync(DBWrapperMixin):
125
100
  Returns:
126
101
  DataModelType | None: The result of the query.
127
102
  """
128
- # Query and filter
129
- _query = (
130
- customQuery
131
- or emptyDataClass.queryBase()
132
- or self.filterQuery(emptyDataClass.schemaName, emptyDataClass.tableName)
103
+ # Get the record
104
+ res = self.getAll(
105
+ emptyDataClass, idKey, idValue, limit=1, customQuery=customQuery
133
106
  )
134
- _filter = f"WHERE {self.makeIdentifier(emptyDataClass.tableAlias, idKey)} = %s"
135
- _params = (idValue,)
136
-
137
- # Create a SQL object for the query and format it
138
- querySql = self._formatFilterQuery(_query, _filter, None, None)
139
-
140
- # Create a new cursor
141
- newCursor = await self.createCursor(emptyDataClass)
142
-
143
- # Log
144
- self.logQuery(newCursor, querySql, _params)
145
-
146
- # Load data
147
- try:
148
- await newCursor.execute(querySql, _params)
149
-
150
- # Fetch one row
151
- row = await newCursor.fetchone()
152
- if row is None:
153
- return
154
-
155
- # Turn data into model
156
- return self.turnDataIntoModel(emptyDataClass, row)
157
-
158
- finally:
159
- # Close the cursor
160
- await newCursor.close()
107
+ async for row in res:
108
+ return row
161
109
 
162
110
  async def getAll(
163
111
  self,
@@ -192,11 +140,10 @@ class DBWrapperAsync(DBWrapperMixin):
192
140
  )
193
141
  _params: tuple[Any, ...] = ()
194
142
  _filter = None
143
+
144
+ # TODO: Rewrite this so that filter method with loop is not used here
195
145
  if idKey and idValue:
196
- _filter = (
197
- f"WHERE {self.makeIdentifier(emptyDataClass.tableAlias, idKey)} = %s"
198
- )
199
- _params = (idValue,)
146
+ (_filter, _params) = self.createFilter({idKey: idValue})
200
147
 
201
148
  # Order and limit
202
149
  _order = self.orderQuery(orderBy)
@@ -2,7 +2,6 @@ import logging
2
2
 
3
3
  from typing import cast, Any
4
4
 
5
- from .db_backend import DatabaseBackend
6
5
  from .common import OrderByItem, NoParam, DataModelType
7
6
 
8
7
 
@@ -80,22 +79,31 @@ class DBWrapperMixin:
80
79
  ### Setters ###
81
80
  ###############
82
81
 
83
- def updateDb(self, db: Any) -> None:
82
+ def setDb(self, db: Any) -> None:
84
83
  """
85
84
  Updates the database backend object.
86
85
 
87
86
  Args:
88
- db (DatabaseBackend): The new database backend object.
87
+ db (Any): The new database backend object.
89
88
  """
89
+ if db is None:
90
+ del self.db
91
+ return
92
+
90
93
  self.db = db
91
94
 
92
- def updateDbConn(self, dbConn: Any) -> None:
95
+ def setDbConn(self, dbConn: Any) -> None:
93
96
  """
94
97
  Updates the database connection object.
95
98
 
96
99
  Args:
97
100
  dbConn (Any): The new database connection object.
98
101
  """
102
+
103
+ if dbConn is None:
104
+ del self.dbConn
105
+ return
106
+
99
107
  self.dbConn = dbConn
100
108
 
101
109
  ######################
@@ -268,7 +276,7 @@ class DBWrapperMixin:
268
276
 
269
277
  def createFilter(
270
278
  self, filter: dict[str, Any] | None
271
- ) -> tuple[str, tuple[Any, ...]]:
279
+ ) -> tuple[Any, tuple[Any, ...]]:
272
280
  if filter is None or len(filter) == 0:
273
281
  return ("", tuple())
274
282
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: database_wrapper
3
- Version: 0.1.38
3
+ Version: 0.1.42
4
4
  Summary: A Different Approach to Database Wrappers in Python
5
5
  Author-email: Gints Murans <gm@gm.lv>
6
6
  License: GNU General Public License v3.0 (GPL-3.0)
@@ -33,13 +33,13 @@ Classifier: Topic :: Software Development :: Libraries :: Python Modules
33
33
  Requires-Python: >=3.8
34
34
  Description-Content-Type: text/markdown
35
35
  Provides-Extra: pgsql
36
- Requires-Dist: database_wrapper_pgsql==0.1.38; extra == "pgsql"
36
+ Requires-Dist: database_wrapper_pgsql==0.1.42; extra == "pgsql"
37
37
  Provides-Extra: mysql
38
- Requires-Dist: database_wrapper_mysql==0.1.38; extra == "mysql"
38
+ Requires-Dist: database_wrapper_mysql==0.1.42; extra == "mysql"
39
39
  Provides-Extra: mssql
40
- Requires-Dist: database_wrapper_mssql==0.1.38; extra == "mssql"
40
+ Requires-Dist: database_wrapper_mssql==0.1.42; extra == "mssql"
41
41
  Provides-Extra: sqlite
42
- Requires-Dist: database_wrapper_sqlite==0.1.38; extra == "sqlite"
42
+ Requires-Dist: database_wrapper_sqlite==0.1.42; extra == "sqlite"
43
43
  Provides-Extra: all
44
44
  Requires-Dist: database_wrapper[mssql,mysql,pgsql,sqlite]; extra == "all"
45
45
  Provides-Extra: dev
@@ -15,13 +15,13 @@ mysqlclient>=2.2.2
15
15
  pymssql>=2.2.10
16
16
 
17
17
  [mssql]
18
- database_wrapper_mssql==0.1.38
18
+ database_wrapper_mssql==0.1.42
19
19
 
20
20
  [mysql]
21
- database_wrapper_mysql==0.1.38
21
+ database_wrapper_mysql==0.1.42
22
22
 
23
23
  [pgsql]
24
- database_wrapper_pgsql==0.1.38
24
+ database_wrapper_pgsql==0.1.42
25
25
 
26
26
  [sqlite]
27
- database_wrapper_sqlite==0.1.38
27
+ database_wrapper_sqlite==0.1.42
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "database_wrapper"
7
- version = "0.1.38"
7
+ version = "0.1.42"
8
8
  description = "A Different Approach to Database Wrappers in Python"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.8"
@@ -45,10 +45,10 @@ Code = "https://github.com/gintsmurans/py_database_wrapper"
45
45
  Download = "https://pypi.org/project/database_wrapper/"
46
46
 
47
47
  [project.optional-dependencies]
48
- pgsql = ["database_wrapper_pgsql == 0.1.38"]
49
- mysql = ["database_wrapper_mysql == 0.1.38"]
50
- mssql = ["database_wrapper_mssql == 0.1.38"]
51
- sqlite = ["database_wrapper_sqlite == 0.1.38"]
48
+ pgsql = ["database_wrapper_pgsql == 0.1.42"]
49
+ mysql = ["database_wrapper_mysql == 0.1.42"]
50
+ mssql = ["database_wrapper_mssql == 0.1.42"]
51
+ sqlite = ["database_wrapper_sqlite == 0.1.42"]
52
52
  all = ["database_wrapper[pgsql,mysql,mssql,sqlite]"]
53
53
  dev = [
54
54
  # Development