reydb 1.1.56__py3-none-any.whl → 1.1.57__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.
reydb/__init__.py CHANGED
@@ -22,3 +22,6 @@ rinfo : Database information methods.
22
22
  rorm : Database ORM methods.
23
23
  rparam : Database parameter methods.
24
24
  """
25
+
26
+
27
+ from .rdb import Database, DatabaseAsync
reydb/rbase.py CHANGED
@@ -9,15 +9,14 @@
9
9
  """
10
10
 
11
11
 
12
- from typing import Any, TypedDict, Literal
12
+ from typing import Any, TypedDict, Literal, TypeVar
13
13
  from enum import EnumType
14
- from sqlalchemy import text as sqlalchemy_text
15
- from sqlalchemy.engine.base import Engine, Connection
14
+ from sqlalchemy import Engine, Connection, Transaction, text as sqlalchemy_text
15
+ from sqlalchemy.ext.asyncio import AsyncEngine, AsyncConnection, AsyncTransaction
16
16
  from sqlalchemy.engine.url import URL
17
17
  from sqlalchemy.sql.elements import TextClause
18
18
  from reykit.rbase import Base, throw
19
19
  from reykit.rre import search
20
-
21
20
  from reykit.rdata import to_json
22
21
  from reykit.rre import findall
23
22
 
@@ -34,6 +33,14 @@ __all__ = (
34
33
  )
35
34
 
36
35
 
36
+ EngineT = TypeVar('EngineT', Engine, AsyncEngine)
37
+ ConnectionT = TypeVar('ConnectionT', Connection, AsyncConnection)
38
+ TransactionT = TypeVar('TransactionT', Transaction, AsyncTransaction)
39
+ DatabaseT = TypeVar('DatabaseT')
40
+ DatabaseConnectionT = TypeVar('DatabaseConnectionT')
41
+ DatabaseExecuteT = TypeVar('DatabaseExecuteT')
42
+
43
+
37
44
  URLParameters = TypedDict(
38
45
  'URLParameters',
39
46
  {
reydb/rbuild.py CHANGED
@@ -14,8 +14,8 @@ from copy import deepcopy
14
14
  from reykit.rbase import throw
15
15
  from reykit.rstdout import ask
16
16
 
17
+ from . import rdb
17
18
  from .rbase import DatabaseBase, extract_path
18
- from .rdb import Database
19
19
 
20
20
 
21
21
  __all__ = (
@@ -51,7 +51,7 @@ class DatabaseBuild(DatabaseBase):
51
51
  """
52
52
 
53
53
 
54
- def __init__(self, db: Database) -> None:
54
+ def __init__(self, db: 'rdb.Database') -> None:
55
55
  """
56
56
  Build instance attributes.
57
57
 
reydb/rconfig.py CHANGED
@@ -19,7 +19,7 @@ from datetime import (
19
19
  )
20
20
  from reykit.rbase import Null, throw
21
21
 
22
- from .rdb import Database
22
+ from . import rdb
23
23
 
24
24
 
25
25
  __all__ = (
@@ -48,7 +48,7 @@ class DatabaseConfig(object):
48
48
  """
49
49
 
50
50
 
51
- def __init__(self, db: Database) -> None:
51
+ def __init__(self, db: 'rdb.Database') -> None:
52
52
  """
53
53
  Build instance attributes.
54
54
 
reydb/rconn.py CHANGED
@@ -9,29 +9,30 @@
9
9
  """
10
10
 
11
11
 
12
- from typing import Self
12
+ from typing import Self, Generic
13
13
  from sqlalchemy import Connection, Transaction
14
14
  from sqlalchemy.ext.asyncio import AsyncConnection, AsyncTransaction
15
15
 
16
- from .rbase import DatabaseBase
17
- from .rdb import Database
16
+ from . import rdb, rexec
17
+ from .rbase import DatabaseT, DatabaseExecuteT, ConnectionT, TransactionT, DatabaseBase
18
18
 
19
19
 
20
20
  __all__ = (
21
+ 'DatabaseConnectionSuper',
21
22
  'DatabaseConnection',
22
23
  'DatabaseConnectionAsync'
23
24
  )
24
25
 
25
26
 
26
- class DatabaseConnection(DatabaseBase):
27
+ class DatabaseConnectionSuper(DatabaseBase, Generic[DatabaseT, DatabaseExecuteT, ConnectionT, TransactionT]):
27
28
  """
28
- Database connection type.
29
+ Database connection super type.
29
30
  """
30
31
 
31
32
 
32
33
  def __init__(
33
34
  self,
34
- db: Database,
35
+ db: DatabaseT,
35
36
  autocommit: bool
36
37
  ) -> None:
37
38
  """
@@ -39,19 +40,27 @@ class DatabaseConnection(DatabaseBase):
39
40
 
40
41
  Parameters
41
42
  ----------
42
- db : `Database` instance.
43
+ db : `Database` or `DatabaseAsync` instance.
43
44
  autocommit: Whether automatic commit execute.
44
45
  """
45
46
 
46
- # Import.
47
- from .rexec import DatabaseExecute
48
-
49
47
  # Build.
50
48
  self.db = db
51
49
  self.autocommit = autocommit
52
- self.execute = DatabaseExecute(self)
53
- self.conn: Connection | None = None
54
- self.begin: Transaction | None = None
50
+ match db:
51
+ case rdb.Database():
52
+ exec = rexec.DatabaseExecute(self)
53
+ case rdb.DatabaseAsync():
54
+ exec = rexec.DatabaseExecuteAsync(self)
55
+ self.execute: DatabaseExecuteT = exec
56
+ self.conn: ConnectionT | None = None
57
+ self.begin: TransactionT | None = None
58
+
59
+
60
+ class DatabaseConnection(DatabaseConnectionSuper['rdb.Database', 'rexec.DatabaseExecute', Connection, Transaction]):
61
+ """
62
+ Database connection type.
63
+ """
55
64
 
56
65
 
57
66
  def get_conn(self) -> Connection:
@@ -168,42 +177,17 @@ class DatabaseConnection(DatabaseBase):
168
177
  # Get.
169
178
  sql = 'SELECT LAST_INSERT_ID()'
170
179
  result = self.execute(sql)
171
- id_ = result.scalar()
180
+ insert_id = result.scalar()
172
181
 
173
- return id_
182
+ return insert_id
174
183
 
175
184
 
176
- class DatabaseConnectionAsync(DatabaseBase):
185
+ class DatabaseConnectionAsync(DatabaseConnectionSuper['rdb.DatabaseAsync', 'rexec.DatabaseExecuteAsync', AsyncConnection, AsyncTransaction]):
177
186
  """
178
187
  Asynchronous database connection type.
179
188
  """
180
189
 
181
190
 
182
- def __init__(
183
- self,
184
- db: Database,
185
- autocommit: bool
186
- ) -> None:
187
- """
188
- Build instance attributes.
189
-
190
- Parameters
191
- ----------
192
- db : `DatabaseAsync` instance.
193
- autocommit: Whether automatic commit execute.
194
- """
195
-
196
- # Import.
197
- from .rexec import DatabaseExecuteAsync
198
-
199
- # Build.
200
- self.db = db
201
- self.autocommit = autocommit
202
- self.aexecute = DatabaseExecuteAsync(self)
203
- self.aconn: AsyncConnection | None = None
204
- self.abegin: AsyncTransaction | None = None
205
-
206
-
207
191
  async def get_conn(self) -> AsyncConnection:
208
192
  """
209
193
  Asynchronous get `Connection` instance.
@@ -214,10 +198,10 @@ class DatabaseConnectionAsync(DatabaseBase):
214
198
  """
215
199
 
216
200
  # Create.
217
- if self.aconn is None:
218
- self.aconn = await self.db.aengine.connect()
201
+ if self.conn is None:
202
+ self.conn = await self.db.engine.connect()
219
203
 
220
- return self.aconn
204
+ return self.conn
221
205
 
222
206
 
223
207
  async def get_begin(self) -> AsyncTransaction:
@@ -230,11 +214,11 @@ class DatabaseConnectionAsync(DatabaseBase):
230
214
  """
231
215
 
232
216
  # Create.
233
- if self.abegin is None:
217
+ if self.begin is None:
234
218
  conn = await self.get_conn()
235
- self.abegin = await conn.begin()
219
+ self.begin = await conn.begin()
236
220
 
237
- return self.abegin
221
+ return self.begin
238
222
 
239
223
 
240
224
  async def commit(self) -> None:
@@ -243,9 +227,9 @@ class DatabaseConnectionAsync(DatabaseBase):
243
227
  """
244
228
 
245
229
  # Commit.
246
- if self.abegin is not None:
247
- await self.abegin.commit()
248
- self.abegin = None
230
+ if self.begin is not None:
231
+ await self.begin.commit()
232
+ self.begin = None
249
233
 
250
234
 
251
235
  async def rollback(self) -> None:
@@ -254,9 +238,9 @@ class DatabaseConnectionAsync(DatabaseBase):
254
238
  """
255
239
 
256
240
  # Rollback.
257
- if self.abegin is not None:
258
- await self.abegin.rollback()
259
- self.abegin = None
241
+ if self.begin is not None:
242
+ await self.begin.rollback()
243
+ self.begin = None
260
244
 
261
245
 
262
246
  async def close(self) -> None:
@@ -265,12 +249,12 @@ class DatabaseConnectionAsync(DatabaseBase):
265
249
  """
266
250
 
267
251
  # Close.
268
- if self.abegin is not None:
269
- await self.abegin.close()
270
- self.abegin = None
271
- if self.aconn is not None:
272
- await self.aconn.close()
273
- self.aconn = None
252
+ if self.begin is not None:
253
+ await self.begin.close()
254
+ self.begin = None
255
+ if self.conn is not None:
256
+ await self.conn.close()
257
+ self.conn = None
274
258
 
275
259
 
276
260
  async def __aenter__(self):
@@ -318,7 +302,7 @@ class DatabaseConnectionAsync(DatabaseBase):
318
302
 
319
303
  # Get.
320
304
  sql = 'SELECT LAST_INSERT_ID()'
321
- result = await self.aexecute(sql)
305
+ result = await self.execute(sql)
322
306
  id_ = result.scalar()
323
307
 
324
308
  return id_