reydb 1.1.58__py3-none-any.whl → 1.1.59__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/rbase.py +10 -1
- reydb/rconn.py +65 -65
- reydb/rdb.py +13 -6
- reydb/rorm.py +728 -162
- {reydb-1.1.58.dist-info → reydb-1.1.59.dist-info}/METADATA +1 -1
- {reydb-1.1.58.dist-info → reydb-1.1.59.dist-info}/RECORD +8 -8
- {reydb-1.1.58.dist-info → reydb-1.1.59.dist-info}/WHEEL +0 -0
- {reydb-1.1.58.dist-info → reydb-1.1.59.dist-info}/licenses/LICENSE +0 -0
reydb/rbase.py
CHANGED
@@ -12,7 +12,8 @@
|
|
12
12
|
from typing import Any, TypedDict, Literal, TypeVar
|
13
13
|
from enum import EnumType
|
14
14
|
from sqlalchemy import Engine, Connection, Transaction, text as sqlalchemy_text
|
15
|
-
from sqlalchemy.
|
15
|
+
from sqlalchemy.orm import Session, SessionTransaction
|
16
|
+
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncConnection, AsyncTransaction, AsyncSession, AsyncSessionTransaction
|
16
17
|
from sqlalchemy.engine.url import URL
|
17
18
|
from sqlalchemy.sql.elements import TextClause
|
18
19
|
from reykit.rbase import Base, throw
|
@@ -36,10 +37,18 @@ __all__ = (
|
|
36
37
|
EngineT = TypeVar('EngineT', Engine, AsyncEngine)
|
37
38
|
ConnectionT = TypeVar('ConnectionT', Connection, AsyncConnection)
|
38
39
|
TransactionT = TypeVar('TransactionT', Transaction, AsyncTransaction)
|
40
|
+
SessionT = TypeVar('SessionT', Session, AsyncSession)
|
41
|
+
SessionTransactionT = TypeVar('SessionTransactionT', SessionTransaction, AsyncSessionTransaction)
|
39
42
|
DatabaseT = TypeVar('DatabaseT')
|
40
43
|
DatabaseConnectionT = TypeVar('DatabaseConnectionT')
|
41
44
|
DatabaseExecuteT = TypeVar('DatabaseExecuteT')
|
42
45
|
DatabaseSchemaT = TypeVar('DatabaseSchemaT')
|
46
|
+
DatabaseORMT = TypeVar('DatabaseORMT')
|
47
|
+
DatabaseORMSessionT = TypeVar('DatabaseSessionT')
|
48
|
+
DatabaseORMStatementSelectT = TypeVar('DatabaseORMStatementSelectT')
|
49
|
+
DatabaseORMStatementInsertT = TypeVar('DatabaseORMStatementInsertT')
|
50
|
+
DatabaseORMStatementUpdateT = TypeVar('DatabaseORMStatementUpdateT')
|
51
|
+
DatabaseORMStatementDeleteT = TypeVar('DatabaseORMStatementDeleteT')
|
43
52
|
|
44
53
|
|
45
54
|
URLParameters = TypedDict(
|
reydb/rconn.py
CHANGED
@@ -63,6 +63,39 @@ class DatabaseConnection(DatabaseConnectionSuper['rdb.Database', 'rexec.Database
|
|
63
63
|
"""
|
64
64
|
|
65
65
|
|
66
|
+
def __enter__(self) -> Self:
|
67
|
+
"""
|
68
|
+
Enter syntax `with`.
|
69
|
+
|
70
|
+
Returns
|
71
|
+
-------
|
72
|
+
Self.
|
73
|
+
"""
|
74
|
+
|
75
|
+
return self
|
76
|
+
|
77
|
+
|
78
|
+
def __exit__(
|
79
|
+
self,
|
80
|
+
exc_type: type[BaseException] | None,
|
81
|
+
*_
|
82
|
+
) -> None:
|
83
|
+
"""
|
84
|
+
Exit syntax `with`.
|
85
|
+
|
86
|
+
Parameters
|
87
|
+
----------
|
88
|
+
exc_type : Exception type.
|
89
|
+
"""
|
90
|
+
|
91
|
+
# Commit.
|
92
|
+
if exc_type is None:
|
93
|
+
self.commit()
|
94
|
+
|
95
|
+
# Close.
|
96
|
+
self.close()
|
97
|
+
|
98
|
+
|
66
99
|
def get_conn(self) -> Connection:
|
67
100
|
"""
|
68
101
|
Get `Connection` instance.
|
@@ -132,9 +165,32 @@ class DatabaseConnection(DatabaseConnectionSuper['rdb.Database', 'rexec.Database
|
|
132
165
|
self.conn = None
|
133
166
|
|
134
167
|
|
135
|
-
def
|
168
|
+
def insert_id(self) -> int:
|
136
169
|
"""
|
137
|
-
|
170
|
+
Return last self increasing ID.
|
171
|
+
|
172
|
+
Returns
|
173
|
+
-------
|
174
|
+
ID.
|
175
|
+
"""
|
176
|
+
|
177
|
+
# Get.
|
178
|
+
sql = 'SELECT LAST_INSERT_ID()'
|
179
|
+
result = self.execute(sql)
|
180
|
+
insert_id = result.scalar()
|
181
|
+
|
182
|
+
return insert_id
|
183
|
+
|
184
|
+
|
185
|
+
class DatabaseConnectionAsync(DatabaseConnectionSuper['rdb.DatabaseAsync', 'rexec.DatabaseExecuteAsync', AsyncConnection, AsyncTransaction]):
|
186
|
+
"""
|
187
|
+
Asynchronous database connection type.
|
188
|
+
"""
|
189
|
+
|
190
|
+
|
191
|
+
async def __aenter__(self):
|
192
|
+
"""
|
193
|
+
Asynchronous enter syntax `async with`.
|
138
194
|
|
139
195
|
Returns
|
140
196
|
-------
|
@@ -144,13 +200,13 @@ class DatabaseConnection(DatabaseConnectionSuper['rdb.Database', 'rexec.Database
|
|
144
200
|
return self
|
145
201
|
|
146
202
|
|
147
|
-
def
|
203
|
+
async def __aexit__(
|
148
204
|
self,
|
149
205
|
exc_type: type[BaseException] | None,
|
150
206
|
*_
|
151
207
|
) -> None:
|
152
208
|
"""
|
153
|
-
|
209
|
+
Asynchronous exit syntax `async with`.
|
154
210
|
|
155
211
|
Parameters
|
156
212
|
----------
|
@@ -159,38 +215,16 @@ class DatabaseConnection(DatabaseConnectionSuper['rdb.Database', 'rexec.Database
|
|
159
215
|
|
160
216
|
# Commit.
|
161
217
|
if exc_type is None:
|
162
|
-
self.commit()
|
218
|
+
await self.commit()
|
163
219
|
|
164
220
|
# Close.
|
165
|
-
self.close()
|
166
|
-
|
167
|
-
|
168
|
-
def insert_id(self) -> int:
|
169
|
-
"""
|
170
|
-
Return last self increasing ID.
|
171
|
-
|
172
|
-
Returns
|
173
|
-
-------
|
174
|
-
ID.
|
175
|
-
"""
|
176
|
-
|
177
|
-
# Get.
|
178
|
-
sql = 'SELECT LAST_INSERT_ID()'
|
179
|
-
result = self.execute(sql)
|
180
|
-
insert_id = result.scalar()
|
181
|
-
|
182
|
-
return insert_id
|
183
|
-
|
184
|
-
|
185
|
-
class DatabaseConnectionAsync(DatabaseConnectionSuper['rdb.DatabaseAsync', 'rexec.DatabaseExecuteAsync', AsyncConnection, AsyncTransaction]):
|
186
|
-
"""
|
187
|
-
Asynchronous database connection type.
|
188
|
-
"""
|
221
|
+
await self.close()
|
222
|
+
await self.db.dispose()
|
189
223
|
|
190
224
|
|
191
225
|
async def get_conn(self) -> AsyncConnection:
|
192
226
|
"""
|
193
|
-
Asynchronous get `
|
227
|
+
Asynchronous get `AsyncConnection` instance.
|
194
228
|
|
195
229
|
Returns
|
196
230
|
-------
|
@@ -206,7 +240,7 @@ class DatabaseConnectionAsync(DatabaseConnectionSuper['rdb.DatabaseAsync', 'rexe
|
|
206
240
|
|
207
241
|
async def get_begin(self) -> AsyncTransaction:
|
208
242
|
"""
|
209
|
-
Asynchronous get `
|
243
|
+
Asynchronous get `AsyncTransaction` instance.
|
210
244
|
|
211
245
|
Returns
|
212
246
|
-------
|
@@ -257,40 +291,6 @@ class DatabaseConnectionAsync(DatabaseConnectionSuper['rdb.DatabaseAsync', 'rexe
|
|
257
291
|
self.conn = None
|
258
292
|
|
259
293
|
|
260
|
-
async def __aenter__(self):
|
261
|
-
"""
|
262
|
-
Asynchronous enter syntax `async with`.
|
263
|
-
|
264
|
-
Returns
|
265
|
-
-------
|
266
|
-
Self.
|
267
|
-
"""
|
268
|
-
|
269
|
-
return self
|
270
|
-
|
271
|
-
|
272
|
-
async def __aexit__(
|
273
|
-
self,
|
274
|
-
exc_type: type[BaseException] | None,
|
275
|
-
*_
|
276
|
-
) -> None:
|
277
|
-
"""
|
278
|
-
Asynchronous exit syntax `async with`.
|
279
|
-
|
280
|
-
Parameters
|
281
|
-
----------
|
282
|
-
exc_type : Exception type.
|
283
|
-
"""
|
284
|
-
|
285
|
-
# Commit.
|
286
|
-
if exc_type is None:
|
287
|
-
await self.commit()
|
288
|
-
|
289
|
-
# Close.
|
290
|
-
await self.close()
|
291
|
-
await self.db.dispose()
|
292
|
-
|
293
|
-
|
294
294
|
async def insert_id(self) -> int:
|
295
295
|
"""
|
296
296
|
Asynchronous return last self increasing ID.
|
reydb/rdb.py
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
"""
|
10
10
|
|
11
11
|
|
12
|
-
from typing import Generic
|
12
|
+
from typing import TypeVar, Generic
|
13
13
|
from urllib.parse import quote as urllib_quote
|
14
14
|
from pymysql.constants.CLIENT import MULTI_STATEMENTS
|
15
15
|
from sqlalchemy import Engine, create_engine as sqlalchemy_create_engine
|
@@ -32,7 +32,8 @@ class DatabaseSuper(
|
|
32
32
|
rbase.EngineT,
|
33
33
|
rbase.DatabaseConnectionT,
|
34
34
|
rbase.DatabaseExecuteT,
|
35
|
-
rbase.DatabaseSchemaT
|
35
|
+
rbase.DatabaseSchemaT,
|
36
|
+
rbase.DatabaseORMT
|
36
37
|
]
|
37
38
|
):
|
38
39
|
"""
|
@@ -278,7 +279,7 @@ class DatabaseSuper(
|
|
278
279
|
|
279
280
|
|
280
281
|
@property
|
281
|
-
def orm(self):
|
282
|
+
def orm(self) -> 'rbase.DatabaseORMT':
|
282
283
|
"""
|
283
284
|
Build database ORM instance.
|
284
285
|
|
@@ -288,7 +289,11 @@ class DatabaseSuper(
|
|
288
289
|
"""
|
289
290
|
|
290
291
|
# Build.
|
291
|
-
|
292
|
+
match self:
|
293
|
+
case Database():
|
294
|
+
orm = rorm.DatabaseORM(self)
|
295
|
+
case DatabaseAsync():
|
296
|
+
orm = rorm.DatabaseORMAsync(self)
|
292
297
|
|
293
298
|
return orm
|
294
299
|
|
@@ -484,7 +489,8 @@ class Database(
|
|
484
489
|
Engine,
|
485
490
|
'rconn.DatabaseConnection',
|
486
491
|
'rexec.DatabaseExecute',
|
487
|
-
'rparam.DatabaseSchema'
|
492
|
+
'rparam.DatabaseSchema',
|
493
|
+
'rorm.DatabaseORM'
|
488
494
|
]
|
489
495
|
):
|
490
496
|
"""
|
@@ -497,7 +503,8 @@ class DatabaseAsync(
|
|
497
503
|
AsyncEngine,
|
498
504
|
'rconn.DatabaseConnectionAsync',
|
499
505
|
'rexec.DatabaseExecuteAsync',
|
500
|
-
'rparam.DatabaseSchemaAsync'
|
506
|
+
'rparam.DatabaseSchemaAsync',
|
507
|
+
'rorm.DatabaseORMAsync'
|
501
508
|
]
|
502
509
|
):
|
503
510
|
"""
|