reydb 1.1.50__py3-none-any.whl → 1.1.52__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/rbuild.py +1 -1
- reydb/rconfig.py +1 -1
- reydb/rconn.py +25 -26
- reydb/rdb.py +1 -1
- reydb/rerror.py +1 -1
- reydb/rexec.py +58 -2
- reydb/rfile.py +1 -1
- reydb/rinfo.py +14 -15
- reydb/rorm.py +749 -3
- reydb/rparam.py +2 -2
- {reydb-1.1.50.dist-info → reydb-1.1.52.dist-info}/METADATA +1 -1
- reydb-1.1.52.dist-info/RECORD +17 -0
- reydb-1.1.50.dist-info/RECORD +0 -17
- {reydb-1.1.50.dist-info → reydb-1.1.52.dist-info}/WHEEL +0 -0
- {reydb-1.1.50.dist-info → reydb-1.1.52.dist-info}/licenses/LICENSE +0 -0
reydb/rbuild.py
CHANGED
reydb/rconfig.py
CHANGED
reydb/rconn.py
CHANGED
@@ -10,6 +10,7 @@
|
|
10
10
|
|
11
11
|
|
12
12
|
from typing import Self
|
13
|
+
from sqlalchemy import Transaction
|
13
14
|
|
14
15
|
from .rbase import DatabaseBase
|
15
16
|
from .rdb import Database
|
@@ -36,38 +37,19 @@ class DatabaseConnection(DatabaseBase):
|
|
36
37
|
|
37
38
|
Parameters
|
38
39
|
----------
|
39
|
-
db : Database instance.
|
40
|
-
autocommit: Whether automatic commit
|
41
|
-
"""
|
42
|
-
|
43
|
-
# Build.
|
44
|
-
self.db = db
|
45
|
-
self.autocommit = autocommit
|
46
|
-
self.conn = db.engine.connect()
|
47
|
-
self.begin = None
|
48
|
-
|
49
|
-
|
50
|
-
@property
|
51
|
-
def execute(self):
|
52
|
-
"""
|
53
|
-
Build `database execute` instance.
|
54
|
-
|
55
|
-
Returns
|
56
|
-
-------
|
57
|
-
Instance.
|
40
|
+
db : `Database` instance.
|
41
|
+
autocommit: Whether automatic commit execute.
|
58
42
|
"""
|
59
43
|
|
60
44
|
# Import.
|
61
45
|
from .rexec import DatabaseExecute
|
62
46
|
|
63
|
-
# Create transaction.
|
64
|
-
if self.begin is None:
|
65
|
-
self.begin = self.conn.begin()
|
66
|
-
|
67
47
|
# Build.
|
68
|
-
|
69
|
-
|
70
|
-
|
48
|
+
self.db = db
|
49
|
+
self.autocommit = autocommit
|
50
|
+
self.conn = db.engine.connect()
|
51
|
+
self.exec = DatabaseExecute(self)
|
52
|
+
self.begin: Transaction | None = None
|
71
53
|
|
72
54
|
|
73
55
|
def commit(self) -> None:
|
@@ -138,6 +120,23 @@ class DatabaseConnection(DatabaseBase):
|
|
138
120
|
__del__ = close
|
139
121
|
|
140
122
|
|
123
|
+
@property
|
124
|
+
def execute(self):
|
125
|
+
"""
|
126
|
+
Build `database execute` instance.
|
127
|
+
|
128
|
+
Returns
|
129
|
+
-------
|
130
|
+
Instance.
|
131
|
+
"""
|
132
|
+
|
133
|
+
# Create transaction.
|
134
|
+
if self.begin is None:
|
135
|
+
self.begin = self.conn.begin()
|
136
|
+
|
137
|
+
return self.exec
|
138
|
+
|
139
|
+
|
141
140
|
@property
|
142
141
|
def insert_id(self) -> int:
|
143
142
|
"""
|
reydb/rdb.py
CHANGED
reydb/rerror.py
CHANGED
reydb/rexec.py
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
"""
|
10
10
|
|
11
11
|
|
12
|
-
from typing import Any, Literal
|
12
|
+
from typing import Any, Literal, overload
|
13
13
|
from collections.abc import Iterable, Generator, Container
|
14
14
|
from enum import EnumType
|
15
15
|
from sqlalchemy import text as sqlalchemy_text
|
@@ -17,6 +17,7 @@ from sqlalchemy.sql.elements import TextClause
|
|
17
17
|
from reykit.rbase import throw, get_first_notnone
|
18
18
|
from reykit.rdata import FunctionGenerator, to_json
|
19
19
|
from reykit.rmonkey import monkey_sqlalchemy_result_more_fetch, monkey_sqlalchemy_row_index_field
|
20
|
+
from reykit.rrand import randn
|
20
21
|
from reykit.rre import findall
|
21
22
|
from reykit.rstdout import echo
|
22
23
|
from reykit.rtable import TableData, Table
|
@@ -50,7 +51,7 @@ class DatabaseExecute(DatabaseBase):
|
|
50
51
|
|
51
52
|
Parameters
|
52
53
|
----------
|
53
|
-
dbconn : DatabaseConnection instance.
|
54
|
+
dbconn : `DatabaseConnection` instance.
|
54
55
|
"""
|
55
56
|
|
56
57
|
# Build.
|
@@ -891,6 +892,61 @@ class DatabaseExecute(DatabaseBase):
|
|
891
892
|
return func_generator.generator
|
892
893
|
|
893
894
|
|
895
|
+
@overload
|
896
|
+
def sleep(self, report: bool | None = None) -> int: ...
|
897
|
+
|
898
|
+
@overload
|
899
|
+
def sleep(self, second: int, report: bool | None = None) -> int: ...
|
900
|
+
|
901
|
+
@overload
|
902
|
+
def sleep(self, low: int = 0, high: int = 10, report: bool | None = None) -> int: ...
|
903
|
+
|
904
|
+
@overload
|
905
|
+
def sleep(self, *thresholds: float, report: bool | None = None) -> float: ...
|
906
|
+
|
907
|
+
@overload
|
908
|
+
def sleep(self, *thresholds: float, precision: Literal[0], report: bool | None = None) -> int: ...
|
909
|
+
|
910
|
+
@overload
|
911
|
+
def sleep(self, *thresholds: float, precision: int, report: bool | None = None) -> float: ...
|
912
|
+
|
913
|
+
def sleep(self, *thresholds: float, precision: int | None = None, report: bool | None = None) -> float:
|
914
|
+
"""
|
915
|
+
Let the database wait random seconds.
|
916
|
+
|
917
|
+
Parameters
|
918
|
+
----------
|
919
|
+
thresholds : Low and high thresholds of random range, range contains thresholds.
|
920
|
+
- When `length is 0`, then low and high thresholds is `0` and `10`.
|
921
|
+
- When `length is 1`, then low and high thresholds is `0` and `thresholds[0]`.
|
922
|
+
- When `length is 2`, then low and high thresholds is `thresholds[0]` and `thresholds[1]`.
|
923
|
+
precision : Precision of random range, that is maximum decimal digits of return value.
|
924
|
+
- `None`: Set to Maximum decimal digits of element of parameter `thresholds`.
|
925
|
+
- `int`: Set to this value.
|
926
|
+
report : Whether report SQL execute information.
|
927
|
+
- `None`: Use attribute `default_report`.
|
928
|
+
- `bool`: Use this value.
|
929
|
+
|
930
|
+
Returns
|
931
|
+
-------
|
932
|
+
Random seconds.
|
933
|
+
- When parameters `precision` is `0`, then return int.
|
934
|
+
- When parameters `precision` is `greater than 0`, then return float.
|
935
|
+
"""
|
936
|
+
|
937
|
+
# Handle parameter.
|
938
|
+
if len(thresholds) == 1:
|
939
|
+
second = thresholds[0]
|
940
|
+
else:
|
941
|
+
second = randn(*thresholds, precision=precision)
|
942
|
+
|
943
|
+
# Sleep.
|
944
|
+
sql = f'SELECT SLEEP({second})'
|
945
|
+
self.execute(sql, report=report)
|
946
|
+
|
947
|
+
return second
|
948
|
+
|
949
|
+
|
894
950
|
def handle_sql(self, sql: str | TextClause) -> TextClause:
|
895
951
|
"""
|
896
952
|
Handle SQL.
|
reydb/rfile.py
CHANGED
reydb/rinfo.py
CHANGED
@@ -9,7 +9,6 @@
|
|
9
9
|
"""
|
10
10
|
|
11
11
|
|
12
|
-
from __future__ import annotations
|
13
12
|
from typing import Any, Literal, overload
|
14
13
|
|
15
14
|
from .rbase import DatabaseBase
|
@@ -32,21 +31,21 @@ class DatabaseInformation(DatabaseBase):
|
|
32
31
|
|
33
32
|
|
34
33
|
@overload
|
35
|
-
def __call__(self: DatabaseInformationSchema | DatabaseInformationSchema | DatabaseInformationDatabase | DatabaseInformationTable) -> list[dict]: ...
|
34
|
+
def __call__(self: 'DatabaseInformationSchema | DatabaseInformationSchema | DatabaseInformationDatabase | DatabaseInformationTable') -> list[dict]: ...
|
36
35
|
|
37
36
|
@overload
|
38
|
-
def __call__(self: DatabaseInformationSchema, name: str) -> DatabaseInformationDatabase: ...
|
37
|
+
def __call__(self: 'DatabaseInformationSchema', name: str) -> 'DatabaseInformationDatabase': ...
|
39
38
|
|
40
39
|
@overload
|
41
|
-
def __call__(self: DatabaseInformationDatabase, name: str) -> DatabaseInformationTable: ...
|
40
|
+
def __call__(self: 'DatabaseInformationDatabase', name: str) -> 'DatabaseInformationTable': ...
|
42
41
|
|
43
42
|
@overload
|
44
|
-
def __call__(self: DatabaseInformationTable, name: str) -> DatabaseInformationColumn: ...
|
43
|
+
def __call__(self: 'DatabaseInformationTable', name: str) -> 'DatabaseInformationColumn': ...
|
45
44
|
|
46
45
|
@overload
|
47
|
-
def __call__(self: DatabaseInformationColumn) -> dict: ...
|
46
|
+
def __call__(self: 'DatabaseInformationColumn') -> dict: ...
|
48
47
|
|
49
|
-
def __call__(self, name: str | None = None) -> DatabaseInformationDatabase | DatabaseInformationTable | DatabaseInformationColumn | list[dict] | dict:
|
48
|
+
def __call__(self, name: str | None = None) -> 'DatabaseInformationDatabase | DatabaseInformationTable | DatabaseInformationColumn | list[dict] | dict':
|
50
49
|
"""
|
51
50
|
Get information table or subclass instance.
|
52
51
|
|
@@ -125,15 +124,15 @@ class DatabaseInformation(DatabaseBase):
|
|
125
124
|
|
126
125
|
|
127
126
|
@overload
|
128
|
-
def __getattr__(self: DatabaseInformationSchema, name: str) -> DatabaseInformationDatabase: ...
|
127
|
+
def __getattr__(self: 'DatabaseInformationSchema', name: str) -> 'DatabaseInformationDatabase': ...
|
129
128
|
|
130
129
|
@overload
|
131
|
-
def __getattr__(self: DatabaseInformationDatabase, name: str) -> DatabaseInformationTable: ...
|
130
|
+
def __getattr__(self: 'DatabaseInformationDatabase', name: str) -> 'DatabaseInformationTable': ...
|
132
131
|
|
133
132
|
@overload
|
134
|
-
def __getattr__(self: DatabaseInformationTable, name: str) -> DatabaseInformationColumn: ...
|
133
|
+
def __getattr__(self: 'DatabaseInformationTable', name: str) -> 'DatabaseInformationColumn': ...
|
135
134
|
|
136
|
-
def __getattr__(self, name: str) -> DatabaseInformationDatabase | DatabaseInformationTable | DatabaseInformationColumn:
|
135
|
+
def __getattr__(self, name: str) -> 'DatabaseInformationDatabase | DatabaseInformationTable | DatabaseInformationColumn':
|
137
136
|
"""
|
138
137
|
Build subclass instance.
|
139
138
|
|
@@ -198,7 +197,7 @@ class DatabaseInformationSchema(DatabaseInformation):
|
|
198
197
|
|
199
198
|
Parameters
|
200
199
|
----------
|
201
|
-
db: Database instance.
|
200
|
+
db: `Database` instance.
|
202
201
|
"""
|
203
202
|
|
204
203
|
# Set parameter.
|
@@ -262,7 +261,7 @@ class DatabaseInformationDatabase(DatabaseInformation):
|
|
262
261
|
|
263
262
|
Parameters
|
264
263
|
----------
|
265
|
-
db: Database instance.
|
264
|
+
db: `Database` instance.
|
266
265
|
database : Database name.
|
267
266
|
"""
|
268
267
|
|
@@ -358,7 +357,7 @@ class DatabaseInformationTable(DatabaseInformation):
|
|
358
357
|
|
359
358
|
Parameters
|
360
359
|
----------
|
361
|
-
db: Database instance.
|
360
|
+
db: `Database` instance.
|
362
361
|
database : Database name.
|
363
362
|
table : Table name.
|
364
363
|
"""
|
@@ -453,7 +452,7 @@ class DatabaseInformationColumn(DatabaseInformation):
|
|
453
452
|
|
454
453
|
Parameters
|
455
454
|
----------
|
456
|
-
db: Database instance.
|
455
|
+
db: `Database` instance.
|
457
456
|
database : Database name.
|
458
457
|
table : Table name.
|
459
458
|
column : Column name.
|
reydb/rorm.py
CHANGED
@@ -9,22 +9,173 @@
|
|
9
9
|
"""
|
10
10
|
|
11
11
|
|
12
|
-
from
|
12
|
+
from typing import Self, Any, Type, TypeVar, Generic, Final
|
13
|
+
from functools import wraps as functools_wraps
|
14
|
+
from pydantic import ConfigDict, field_validator as pydantic_field_validator, model_validator as pydantic_model_validator
|
15
|
+
from sqlalchemy.orm import SessionTransaction
|
16
|
+
from sqlalchemy.sql.dml import Insert, Update, Delete
|
17
|
+
from sqlmodel import SQLModel, Session, Table, Field as sqlmodel_Field
|
18
|
+
from sqlmodel.sql._expression_select_cls import SelectOfScalar as Select
|
19
|
+
from reykit.rbase import CallableT, is_instance
|
13
20
|
|
14
21
|
from .rbase import DatabaseBase
|
15
22
|
from .rdb import Database
|
16
23
|
|
17
24
|
|
18
25
|
__all__ = (
|
26
|
+
'DatabaseORMBase',
|
27
|
+
'DatabaseORMModel',
|
19
28
|
'DatabaseORM',
|
29
|
+
'DatabaseORMSession',
|
30
|
+
'DatabaseORMStatement',
|
31
|
+
'DatabaseORMStatementSelect',
|
32
|
+
'DatabaseORMStatementInsert',
|
33
|
+
'DatabaseORMStatementUpdate',
|
34
|
+
'DatabaseORMStatementDelete'
|
20
35
|
)
|
21
36
|
|
22
37
|
|
23
|
-
class
|
38
|
+
class DatabaseORMBase(DatabaseBase):
|
39
|
+
"""
|
40
|
+
Database ORM base type.
|
41
|
+
"""
|
42
|
+
|
43
|
+
|
44
|
+
class DatabaseORMModelField(DatabaseBase):
|
45
|
+
"""
|
46
|
+
Database ORM model filed type.
|
47
|
+
"""
|
48
|
+
|
49
|
+
|
50
|
+
class DatabaseORMModel(DatabaseORMBase, SQLModel):
|
51
|
+
"""
|
52
|
+
Database ORM model type.
|
53
|
+
"""
|
54
|
+
|
55
|
+
|
56
|
+
def update(self, data: 'DatabaseORMModel | dict[dict, Any]') -> None:
|
57
|
+
"""
|
58
|
+
Update attributes.
|
59
|
+
|
60
|
+
Parameters
|
61
|
+
----------
|
62
|
+
data : `DatabaseORMModel` or `dict`.
|
63
|
+
"""
|
64
|
+
|
65
|
+
# Update.
|
66
|
+
self.sqlmodel_update(data)
|
67
|
+
|
68
|
+
|
69
|
+
def validate(self) -> Self:
|
70
|
+
"""
|
71
|
+
Validate all attributes, and copy self instance to new instance.
|
72
|
+
"""
|
73
|
+
|
74
|
+
# Validate.
|
75
|
+
model = self.model_validate(self)
|
76
|
+
|
77
|
+
return model
|
78
|
+
|
79
|
+
|
80
|
+
def copy(self) -> Self:
|
81
|
+
"""
|
82
|
+
Copy self instance to new instance.
|
83
|
+
|
84
|
+
Returns
|
85
|
+
-------
|
86
|
+
New instance.
|
87
|
+
"""
|
88
|
+
|
89
|
+
# Copy.
|
90
|
+
data = self.data
|
91
|
+
instance = self.__class__(**data)
|
92
|
+
|
93
|
+
return instance
|
94
|
+
|
95
|
+
|
96
|
+
@property
|
97
|
+
def data(self) -> dict[str, Any]:
|
98
|
+
"""
|
99
|
+
All attributes data.
|
100
|
+
|
101
|
+
Returns
|
102
|
+
-------
|
103
|
+
data.
|
104
|
+
"""
|
105
|
+
|
106
|
+
# Get.
|
107
|
+
data = self.model_dump()
|
108
|
+
|
109
|
+
return data
|
110
|
+
|
111
|
+
|
112
|
+
@classmethod
|
113
|
+
def table(cls_or_self) -> Table:
|
114
|
+
"""
|
115
|
+
Mapping `Table` instance.
|
116
|
+
|
117
|
+
Returns
|
118
|
+
-------
|
119
|
+
Instance.
|
120
|
+
"""
|
121
|
+
|
122
|
+
# Get.
|
123
|
+
table: Table = cls_or_self.__table__
|
124
|
+
|
125
|
+
return table
|
126
|
+
|
127
|
+
|
128
|
+
@classmethod
|
129
|
+
def comment(cls_or_self) -> str | None:
|
130
|
+
"""
|
131
|
+
Table comment.
|
132
|
+
|
133
|
+
Returns
|
134
|
+
-------
|
135
|
+
Comment.
|
136
|
+
"""
|
137
|
+
|
138
|
+
# Get.
|
139
|
+
table = cls_or_self.table()
|
140
|
+
comment = table.comment
|
141
|
+
|
142
|
+
return comment
|
143
|
+
|
144
|
+
|
145
|
+
@classmethod
|
146
|
+
def set_comment(cls_or_self, comment: str) -> None:
|
147
|
+
"""
|
148
|
+
Set table comment.
|
149
|
+
|
150
|
+
Parameters
|
151
|
+
----------
|
152
|
+
comment : Comment.
|
153
|
+
"""
|
154
|
+
|
155
|
+
# Set.
|
156
|
+
table = cls_or_self.table()
|
157
|
+
table.comment = comment
|
158
|
+
|
159
|
+
|
160
|
+
ModelT = TypeVar('ModelT', bound=DatabaseORMModel)
|
161
|
+
|
162
|
+
|
163
|
+
class DatabaseORM(DatabaseORMBase):
|
24
164
|
"""
|
25
165
|
Database ORM type.
|
166
|
+
|
167
|
+
Attributes
|
168
|
+
----------
|
169
|
+
DatabaseModel : Database ORM model type.
|
170
|
+
Field : Factory function of database ORM model field.
|
26
171
|
"""
|
27
172
|
|
173
|
+
Model = DatabaseORMModel
|
174
|
+
Field = sqlmodel_Field
|
175
|
+
Config = ConfigDict
|
176
|
+
wrap_validate_filed = pydantic_field_validator
|
177
|
+
wrap_validate_model = pydantic_model_validator
|
178
|
+
|
28
179
|
|
29
180
|
def __init__(self, db: Database) -> None:
|
30
181
|
"""
|
@@ -32,8 +183,603 @@ class DatabaseORM(DatabaseBase):
|
|
32
183
|
|
33
184
|
Parameters
|
34
185
|
----------
|
35
|
-
db: Database instance.
|
186
|
+
db: `Database` instance.
|
36
187
|
"""
|
37
188
|
|
38
189
|
# Build.
|
39
190
|
self.db = db
|
191
|
+
self._session = self.session(True)
|
192
|
+
|
193
|
+
## Method.
|
194
|
+
self.get = self._session.get
|
195
|
+
self.gets = self._session.gets
|
196
|
+
self.all = self._session.all
|
197
|
+
self.add = self._session.add
|
198
|
+
|
199
|
+
## Avoid descriptor error.
|
200
|
+
self.Field = sqlmodel_Field
|
201
|
+
|
202
|
+
|
203
|
+
def session(self, autocommit: bool = False):
|
204
|
+
"""
|
205
|
+
Build `DataBaseORMSession` instance.
|
206
|
+
|
207
|
+
Parameters
|
208
|
+
----------
|
209
|
+
autocommit: Whether automatic commit execute.
|
210
|
+
|
211
|
+
Returns
|
212
|
+
-------
|
213
|
+
Instance.
|
214
|
+
"""
|
215
|
+
|
216
|
+
# Build.
|
217
|
+
sess = DataBaseORMSession(self, autocommit)
|
218
|
+
|
219
|
+
return sess
|
220
|
+
|
221
|
+
|
222
|
+
def create(
|
223
|
+
self,
|
224
|
+
*models: Type[DatabaseORMModel] | DatabaseORMModel,
|
225
|
+
skip: bool = False
|
226
|
+
) -> None:
|
227
|
+
"""
|
228
|
+
Create table.
|
229
|
+
|
230
|
+
Parameters
|
231
|
+
----------
|
232
|
+
models : ORM model instances.
|
233
|
+
check : Skip existing table.
|
234
|
+
"""
|
235
|
+
|
236
|
+
# Create.
|
237
|
+
for model in models:
|
238
|
+
table = model.table()
|
239
|
+
table.create(self.db.engine, checkfirst=skip)
|
240
|
+
|
241
|
+
|
242
|
+
def drop(
|
243
|
+
self,
|
244
|
+
*models: Type[DatabaseORMModel] | DatabaseORMModel,
|
245
|
+
skip: bool = False
|
246
|
+
) -> None:
|
247
|
+
"""
|
248
|
+
Delete table.
|
249
|
+
|
250
|
+
Parameters
|
251
|
+
----------
|
252
|
+
models : ORM model instances.
|
253
|
+
check : Skip not exist table.
|
254
|
+
"""
|
255
|
+
|
256
|
+
# Create.
|
257
|
+
for model in models:
|
258
|
+
table = model.table()
|
259
|
+
table.drop(self.db.engine, checkfirst=skip)
|
260
|
+
|
261
|
+
|
262
|
+
class DataBaseORMSession(DatabaseORMBase):
|
263
|
+
"""
|
264
|
+
Database ORM session type, based ORM model.
|
265
|
+
"""
|
266
|
+
|
267
|
+
|
268
|
+
def __init__(
|
269
|
+
self,
|
270
|
+
orm: 'DatabaseORM',
|
271
|
+
autocommit: bool = False
|
272
|
+
) -> None:
|
273
|
+
"""
|
274
|
+
Build instance attributes.
|
275
|
+
|
276
|
+
Parameters
|
277
|
+
----------
|
278
|
+
orm : `DatabaseORM` instance.
|
279
|
+
autocommit: Whether automatic commit execute.
|
280
|
+
"""
|
281
|
+
|
282
|
+
# Build.
|
283
|
+
self.orm = orm
|
284
|
+
self.autocommit = autocommit
|
285
|
+
self.session: Session | None = None
|
286
|
+
self.begin: SessionTransaction | None = None
|
287
|
+
|
288
|
+
|
289
|
+
def commit(self) -> None:
|
290
|
+
"""
|
291
|
+
Commit cumulative executions.
|
292
|
+
"""
|
293
|
+
|
294
|
+
# Commit.
|
295
|
+
if self.begin is not None:
|
296
|
+
self.begin.commit()
|
297
|
+
self.begin = None
|
298
|
+
|
299
|
+
|
300
|
+
def rollback(self) -> None:
|
301
|
+
"""
|
302
|
+
Rollback cumulative executions.
|
303
|
+
"""
|
304
|
+
|
305
|
+
# Rollback.
|
306
|
+
if self.begin is not None:
|
307
|
+
self.begin.rollback()
|
308
|
+
self.begin = None
|
309
|
+
|
310
|
+
|
311
|
+
def close(self) -> None:
|
312
|
+
"""
|
313
|
+
Close database connection.
|
314
|
+
"""
|
315
|
+
|
316
|
+
# Close.
|
317
|
+
if self.session is not None:
|
318
|
+
self.session.close()
|
319
|
+
self.session = None
|
320
|
+
|
321
|
+
|
322
|
+
def __enter__(self) -> Self:
|
323
|
+
"""
|
324
|
+
Enter syntax `with`.
|
325
|
+
|
326
|
+
Returns
|
327
|
+
-------
|
328
|
+
Self.
|
329
|
+
"""
|
330
|
+
|
331
|
+
return self
|
332
|
+
|
333
|
+
|
334
|
+
def __exit__(
|
335
|
+
self,
|
336
|
+
exc_type: type[BaseException] | None,
|
337
|
+
*_
|
338
|
+
) -> None:
|
339
|
+
"""
|
340
|
+
Exit syntax `with`.
|
341
|
+
|
342
|
+
Parameters
|
343
|
+
----------
|
344
|
+
exc_type : Exception type.
|
345
|
+
"""
|
346
|
+
|
347
|
+
# Commit.
|
348
|
+
if exc_type is None:
|
349
|
+
self.commit()
|
350
|
+
|
351
|
+
# Close.
|
352
|
+
else:
|
353
|
+
self.close()
|
354
|
+
|
355
|
+
|
356
|
+
__del__ = close
|
357
|
+
|
358
|
+
|
359
|
+
def wrap_transact(method: CallableT) -> CallableT:
|
360
|
+
"""
|
361
|
+
Decorator, automated transaction.
|
362
|
+
|
363
|
+
Parameters
|
364
|
+
----------
|
365
|
+
method : Method.
|
366
|
+
|
367
|
+
Returns
|
368
|
+
-------
|
369
|
+
Decorated method.
|
370
|
+
"""
|
371
|
+
|
372
|
+
|
373
|
+
# Define.
|
374
|
+
@functools_wraps(method)
|
375
|
+
def wrap(self: 'DataBaseORMSession', *args, **kwargs):
|
376
|
+
|
377
|
+
# Session.
|
378
|
+
if self.session is None:
|
379
|
+
self.session = Session(self.orm.db.engine)
|
380
|
+
|
381
|
+
# Begin.
|
382
|
+
if self.begin is None:
|
383
|
+
self.begin = self.session.begin()
|
384
|
+
|
385
|
+
# Execute.
|
386
|
+
result = method(self, *args, **kwargs)
|
387
|
+
|
388
|
+
# Autucommit.
|
389
|
+
if self.autocommit:
|
390
|
+
self.commit()
|
391
|
+
self.close()
|
392
|
+
|
393
|
+
return result
|
394
|
+
|
395
|
+
|
396
|
+
return wrap
|
397
|
+
|
398
|
+
|
399
|
+
@wrap_transact
|
400
|
+
def create(
|
401
|
+
self,
|
402
|
+
*models: Type[DatabaseORMModel] | DatabaseORMModel,
|
403
|
+
skip: bool = False
|
404
|
+
) -> None:
|
405
|
+
"""
|
406
|
+
Create table.
|
407
|
+
|
408
|
+
Parameters
|
409
|
+
----------
|
410
|
+
models : ORM model instances.
|
411
|
+
check : Skip existing table.
|
412
|
+
"""
|
413
|
+
|
414
|
+
# Create.
|
415
|
+
for model in models:
|
416
|
+
table = model.table()
|
417
|
+
table.create(self.session.connection(), checkfirst=skip)
|
418
|
+
|
419
|
+
|
420
|
+
@wrap_transact
|
421
|
+
def drop(
|
422
|
+
self,
|
423
|
+
*models: Type[DatabaseORMModel] | DatabaseORMModel,
|
424
|
+
skip: bool = False
|
425
|
+
) -> None:
|
426
|
+
"""
|
427
|
+
Delete table.
|
428
|
+
|
429
|
+
Parameters
|
430
|
+
----------
|
431
|
+
models : ORM model instances.
|
432
|
+
check : Skip not exist table.
|
433
|
+
"""
|
434
|
+
|
435
|
+
# Create.
|
436
|
+
for model in models:
|
437
|
+
table = model.table()
|
438
|
+
table.drop(self.session.connection(), checkfirst=skip)
|
439
|
+
|
440
|
+
|
441
|
+
@wrap_transact
|
442
|
+
def get(self, model: Type[ModelT] | ModelT, key: Any | tuple[Any]) -> ModelT | None:
|
443
|
+
"""
|
444
|
+
Select records by primary key.
|
445
|
+
|
446
|
+
Parameters
|
447
|
+
----------
|
448
|
+
model : ORM model type or instance.
|
449
|
+
key : Primary key.
|
450
|
+
- `Any`: Single primary key.
|
451
|
+
- `tuple[Any]`: Composite primary key.
|
452
|
+
|
453
|
+
Returns
|
454
|
+
-------
|
455
|
+
With records ORM model instance or null.
|
456
|
+
"""
|
457
|
+
|
458
|
+
# Handle parameter.
|
459
|
+
if is_instance(model):
|
460
|
+
model = type(model)
|
461
|
+
|
462
|
+
# Get.
|
463
|
+
result = self.session.get(model, key)
|
464
|
+
|
465
|
+
# Autucommit.
|
466
|
+
if (
|
467
|
+
self.autocommit
|
468
|
+
and result is not None
|
469
|
+
):
|
470
|
+
self.session.expunge(result)
|
471
|
+
|
472
|
+
return result
|
473
|
+
|
474
|
+
|
475
|
+
@wrap_transact
|
476
|
+
def gets(self, model: Type[ModelT] | ModelT, *keys: Any | tuple[Any]) -> list[ModelT]:
|
477
|
+
"""
|
478
|
+
Select records by primary key sequence.
|
479
|
+
|
480
|
+
Parameters
|
481
|
+
----------
|
482
|
+
model : ORM model type or instance.
|
483
|
+
keys : Primary key sequence.
|
484
|
+
- `Any`: Single primary key.
|
485
|
+
- `tuple[Any]`: Composite primary key.
|
486
|
+
|
487
|
+
Returns
|
488
|
+
-------
|
489
|
+
With records ORM model instance list.
|
490
|
+
"""
|
491
|
+
|
492
|
+
# Handle parameter.
|
493
|
+
if is_instance(model):
|
494
|
+
model = type(model)
|
495
|
+
|
496
|
+
# Get.
|
497
|
+
results = [
|
498
|
+
result
|
499
|
+
for key in keys
|
500
|
+
if (result := self.session.get(model, key)) is not None
|
501
|
+
]
|
502
|
+
|
503
|
+
return results
|
504
|
+
|
505
|
+
|
506
|
+
@wrap_transact
|
507
|
+
def all(self, model: Type[ModelT] | ModelT) -> list[ModelT]:
|
508
|
+
"""
|
509
|
+
Select all records.
|
510
|
+
|
511
|
+
Parameters
|
512
|
+
----------
|
513
|
+
model : ORM model type or instance.
|
514
|
+
|
515
|
+
Returns
|
516
|
+
-------
|
517
|
+
With records ORM model instance list.
|
518
|
+
"""
|
519
|
+
|
520
|
+
# Handle parameter.
|
521
|
+
if is_instance(model):
|
522
|
+
model = type(model)
|
523
|
+
|
524
|
+
# Get.
|
525
|
+
select = Select(model)
|
526
|
+
models = self.session.exec(select)
|
527
|
+
models = list(models)
|
528
|
+
|
529
|
+
return models
|
530
|
+
|
531
|
+
|
532
|
+
@wrap_transact
|
533
|
+
def add(self, *models: DatabaseORMModel) -> None:
|
534
|
+
"""
|
535
|
+
Insert records.
|
536
|
+
|
537
|
+
Parameters
|
538
|
+
----------
|
539
|
+
models : ORM model instances.
|
540
|
+
"""
|
541
|
+
|
542
|
+
# Add.
|
543
|
+
self.session.add_all(models)
|
544
|
+
|
545
|
+
|
546
|
+
@wrap_transact
|
547
|
+
def rm(self, *models: DatabaseORMModel) -> None:
|
548
|
+
"""
|
549
|
+
Delete records.
|
550
|
+
|
551
|
+
Parameters
|
552
|
+
----------
|
553
|
+
models : ORM model instances.
|
554
|
+
"""
|
555
|
+
|
556
|
+
# Delete.
|
557
|
+
for model in models:
|
558
|
+
self.session.delete(model)
|
559
|
+
|
560
|
+
|
561
|
+
@wrap_transact
|
562
|
+
def refresh(self, *models: DatabaseORMModel) -> None:
|
563
|
+
"""
|
564
|
+
Refresh records.
|
565
|
+
|
566
|
+
Parameters
|
567
|
+
----------
|
568
|
+
models : ORM model instances.
|
569
|
+
"""
|
570
|
+
|
571
|
+
# Refresh.
|
572
|
+
for model in models:
|
573
|
+
self.session.refresh(model)
|
574
|
+
|
575
|
+
|
576
|
+
@wrap_transact
|
577
|
+
def expire(self, *models: DatabaseORMModel) -> None:
|
578
|
+
"""
|
579
|
+
Mark records to expire, refresh on next call.
|
580
|
+
|
581
|
+
Parameters
|
582
|
+
----------
|
583
|
+
models : ORM model instances.
|
584
|
+
"""
|
585
|
+
|
586
|
+
# Refresh.
|
587
|
+
for model in models:
|
588
|
+
self.session.expire(model)
|
589
|
+
|
590
|
+
|
591
|
+
@wrap_transact
|
592
|
+
def select(self, model: Type[ModelT] | ModelT):
|
593
|
+
"""
|
594
|
+
Build `DatabaseORMSelect` instance.
|
595
|
+
|
596
|
+
Parameters
|
597
|
+
----------
|
598
|
+
model : ORM model instance.
|
599
|
+
|
600
|
+
Returns
|
601
|
+
-------
|
602
|
+
Instance.
|
603
|
+
"""
|
604
|
+
|
605
|
+
# Handle parameter.
|
606
|
+
if is_instance(model):
|
607
|
+
model = type(model)
|
608
|
+
|
609
|
+
# Build.
|
610
|
+
select = DatabaseORMStatementSelect[ModelT](self, model)
|
611
|
+
|
612
|
+
return select
|
613
|
+
|
614
|
+
|
615
|
+
@wrap_transact
|
616
|
+
def insert(self, model: Type[ModelT] | ModelT):
|
617
|
+
"""
|
618
|
+
Build `DatabaseORMInsert` instance.
|
619
|
+
|
620
|
+
Parameters
|
621
|
+
----------
|
622
|
+
model : ORM model instance.
|
623
|
+
|
624
|
+
Returns
|
625
|
+
-------
|
626
|
+
Instance.
|
627
|
+
"""
|
628
|
+
|
629
|
+
# Handle parameter.
|
630
|
+
if is_instance(model):
|
631
|
+
model = type(model)
|
632
|
+
|
633
|
+
# Build.
|
634
|
+
select = DatabaseORMStatementInsert[ModelT](self, model)
|
635
|
+
|
636
|
+
return select
|
637
|
+
|
638
|
+
|
639
|
+
@wrap_transact
|
640
|
+
def update(self, model: Type[ModelT] | ModelT):
|
641
|
+
"""
|
642
|
+
Build `DatabaseORMUpdate` instance.
|
643
|
+
|
644
|
+
Parameters
|
645
|
+
----------
|
646
|
+
model : ORM model instance.
|
647
|
+
|
648
|
+
Returns
|
649
|
+
-------
|
650
|
+
Instance.
|
651
|
+
"""
|
652
|
+
|
653
|
+
# Handle parameter.
|
654
|
+
if is_instance(model):
|
655
|
+
model = type(model)
|
656
|
+
|
657
|
+
# Build.
|
658
|
+
select = DatabaseORMStatementUpdate[ModelT](self, model)
|
659
|
+
|
660
|
+
return select
|
661
|
+
|
662
|
+
|
663
|
+
@wrap_transact
|
664
|
+
def delete(self, model: Type[ModelT] | ModelT):
|
665
|
+
"""
|
666
|
+
Build `DatabaseORMDelete` instance.
|
667
|
+
|
668
|
+
Parameters
|
669
|
+
----------
|
670
|
+
model : ORM model instance.
|
671
|
+
|
672
|
+
Returns
|
673
|
+
-------
|
674
|
+
Instance.
|
675
|
+
"""
|
676
|
+
|
677
|
+
# Handle parameter.
|
678
|
+
if is_instance(model):
|
679
|
+
model = type(model)
|
680
|
+
|
681
|
+
# Build.
|
682
|
+
select = DatabaseORMStatementDelete[ModelT](self, model)
|
683
|
+
|
684
|
+
return select
|
685
|
+
|
686
|
+
|
687
|
+
class DatabaseORMStatement(DatabaseORMBase):
|
688
|
+
"""
|
689
|
+
Database ORM statement type.
|
690
|
+
"""
|
691
|
+
|
692
|
+
|
693
|
+
def __init__(
|
694
|
+
self,
|
695
|
+
sess: DataBaseORMSession,
|
696
|
+
model: Type[ModelT]
|
697
|
+
) -> None:
|
698
|
+
"""
|
699
|
+
Build instance attributes.
|
700
|
+
|
701
|
+
Parameters
|
702
|
+
----------
|
703
|
+
sess : `DataBaseORMSession` instance.
|
704
|
+
model : ORM model instance.
|
705
|
+
"""
|
706
|
+
|
707
|
+
# Build.
|
708
|
+
self.sess = sess
|
709
|
+
self.model = model
|
710
|
+
|
711
|
+
# Init.
|
712
|
+
super().__init__(self.model)
|
713
|
+
|
714
|
+
|
715
|
+
def execute(self) -> None:
|
716
|
+
"""
|
717
|
+
Execute statement.
|
718
|
+
"""
|
719
|
+
|
720
|
+
# Execute.
|
721
|
+
self.sess.session.exec(self)
|
722
|
+
|
723
|
+
|
724
|
+
class DatabaseORMStatementSelect(DatabaseORMStatement, Select, Generic[ModelT]):
|
725
|
+
"""
|
726
|
+
Database ORM `select` statement type.
|
727
|
+
|
728
|
+
Attributes
|
729
|
+
----------
|
730
|
+
inherit_cache : Compatible `Select` type.
|
731
|
+
"""
|
732
|
+
|
733
|
+
inherit_cache: Final = True
|
734
|
+
|
735
|
+
|
736
|
+
def execute(self) -> list[ModelT]:
|
737
|
+
"""
|
738
|
+
Execute self statement.
|
739
|
+
|
740
|
+
Returns
|
741
|
+
-------
|
742
|
+
With records ORM model instance list.
|
743
|
+
"""
|
744
|
+
|
745
|
+
# Execute.
|
746
|
+
result = self.sess.session.exec(self)
|
747
|
+
models = list(result)
|
748
|
+
|
749
|
+
return models
|
750
|
+
|
751
|
+
|
752
|
+
class DatabaseORMStatementInsert(Generic[ModelT], DatabaseORMStatement, Insert):
|
753
|
+
"""
|
754
|
+
Database ORM `insert` statement type.
|
755
|
+
|
756
|
+
Attributes
|
757
|
+
----------
|
758
|
+
inherit_cache : Compatible `Select` type.
|
759
|
+
"""
|
760
|
+
|
761
|
+
inherit_cache: Final = True
|
762
|
+
|
763
|
+
|
764
|
+
class DatabaseORMStatementUpdate(Generic[ModelT], DatabaseORMStatement, Update):
|
765
|
+
"""
|
766
|
+
Database ORM `update` statement type.
|
767
|
+
|
768
|
+
Attributes
|
769
|
+
----------
|
770
|
+
inherit_cache : Compatible `Update` type.
|
771
|
+
"""
|
772
|
+
|
773
|
+
inherit_cache: Final = True
|
774
|
+
|
775
|
+
|
776
|
+
class DatabaseORMStatementDelete(Generic[ModelT], DatabaseORMStatement, Delete):
|
777
|
+
"""
|
778
|
+
Database ORM `delete` statement type.
|
779
|
+
|
780
|
+
Attributes
|
781
|
+
----------
|
782
|
+
inherit_cache : Compatible `Delete` type.
|
783
|
+
"""
|
784
|
+
|
785
|
+
inherit_cache: Final = True
|
reydb/rparam.py
CHANGED
@@ -39,7 +39,7 @@ class DatabaseParameters(DatabaseBase):
|
|
39
39
|
|
40
40
|
Parameters
|
41
41
|
----------
|
42
|
-
db: Database instance.
|
42
|
+
db: `Database` instance.
|
43
43
|
global\\_ : Whether base global.
|
44
44
|
"""
|
45
45
|
|
@@ -261,7 +261,7 @@ class DatabaseParametersPragma(DatabaseParameters):
|
|
261
261
|
|
262
262
|
Parameters
|
263
263
|
----------
|
264
|
-
db: Database instance.
|
264
|
+
db: `Database` instance.
|
265
265
|
"""
|
266
266
|
|
267
267
|
# Set parameter.
|
@@ -0,0 +1,17 @@
|
|
1
|
+
reydb/__init__.py,sha256=vwShFPkCDpLTrVGCq1AZgKCGQ8tBBodCxrvrib9ptrs,574
|
2
|
+
reydb/rall.py,sha256=GsXHqvT1k--U53HpDY4SALjIHN8rwgSxeXpJjH5gq2E,409
|
3
|
+
reydb/rbase.py,sha256=A7or663TcrQyq56P813WsV4BlApZIzXOPdZLsT7KwWw,7040
|
4
|
+
reydb/rbuild.py,sha256=6N8aLqCeX8JnOwQstVA2AuM0Rl5kUHx5Enrm2GGcGvo,31852
|
5
|
+
reydb/rconfig.py,sha256=kkqJg68bGZTE3JqH9dF1n-c1shuPJ4O8Bqg7J9Pd2CM,12659
|
6
|
+
reydb/rconn.py,sha256=AYGi_A4qMMHEAiH0lGtHpH2Q3PZylNFoBjGtXeDSNlA,2810
|
7
|
+
reydb/rdb.py,sha256=ZtEb6LSL4yFw5pnolqL39fr7BgeGMMUk-OYotpx_Mrw,12916
|
8
|
+
reydb/rerror.py,sha256=Lsl7UECYdIFYjd9t7RhvNcHdyGStI3gffm8zmkK1DEc,9943
|
9
|
+
reydb/rexec.py,sha256=z82pYvtFQmYaKWeo26G7jT2f3f9IyxCPfF4n5Cf0LcM,31222
|
10
|
+
reydb/rfile.py,sha256=8HSrlpuslSCcWzjeh2y4Fs7R_qnm3jS_c13CDrKxpaw,15182
|
11
|
+
reydb/rinfo.py,sha256=4btKBBZzVXGuPsmswqXDxvjZQuAc9raQ0tpXvmft71s,12741
|
12
|
+
reydb/rorm.py,sha256=DvRBRPdvfq019CZV21OJoTdcqicDZUBdSMzlikTFLXg,16563
|
13
|
+
reydb/rparam.py,sha256=six7wwQRKycoscv-AGyQqsPjA4_TZgcGQ_jk7FZytQs,6803
|
14
|
+
reydb-1.1.52.dist-info/METADATA,sha256=bNFHLGFuX0lblm5mSXi_Es8l0nHOLdt12-fSh54BaNU,1550
|
15
|
+
reydb-1.1.52.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
16
|
+
reydb-1.1.52.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
|
17
|
+
reydb-1.1.52.dist-info/RECORD,,
|
reydb-1.1.50.dist-info/RECORD
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
reydb/__init__.py,sha256=vwShFPkCDpLTrVGCq1AZgKCGQ8tBBodCxrvrib9ptrs,574
|
2
|
-
reydb/rall.py,sha256=GsXHqvT1k--U53HpDY4SALjIHN8rwgSxeXpJjH5gq2E,409
|
3
|
-
reydb/rbase.py,sha256=A7or663TcrQyq56P813WsV4BlApZIzXOPdZLsT7KwWw,7040
|
4
|
-
reydb/rbuild.py,sha256=8sYuqYR3jKlPfIrCwXk9o-q8cx-VNU0XsIsA6XKIKPo,31850
|
5
|
-
reydb/rconfig.py,sha256=4vT0kKVdb9hJjHPQGtmvenPPRbtR1EP2k7jCgfPR0s8,12657
|
6
|
-
reydb/rconn.py,sha256=IGRiOkk0TzWj-NQ2o6A1FnMqYnDvg2G1Dd-7cA0dpP0,2765
|
7
|
-
reydb/rdb.py,sha256=Ijxja3lb73YmJfzpJBT3bQrqz9rV3TC5w2shQfwj0gQ,12919
|
8
|
-
reydb/rerror.py,sha256=TBAzMsf7BrmelhXr5hbqOYVmmRFQtFkyC2SZQx81oYA,9941
|
9
|
-
reydb/rexec.py,sha256=xIUbTULfmMkLBYZTPX6lyN5yob-eg6M7Xir1j3jf6PM,29071
|
10
|
-
reydb/rfile.py,sha256=k5y3U179NED3gWdyZQ_JRuHmzFTKsZ8aMhxg04ENuHg,15180
|
11
|
-
reydb/rinfo.py,sha256=aM3mMFwgT9pemrusS_YjXAVUCJOJ3V709bEWuB-fkuc,12737
|
12
|
-
reydb/rorm.py,sha256=ZdXPCeflshlg-ABFpEAI7UFJWdqdBYbbi5yrCAE8Fo8,625
|
13
|
-
reydb/rparam.py,sha256=M_tT8nesAResxoDVhk8cmG2N5woYMxXinSGIC2i_8ak,6799
|
14
|
-
reydb-1.1.50.dist-info/METADATA,sha256=JtY5fp1zwawCi2KPhLqytLCK_ze6ZxgJ91r5fjIKmmc,1550
|
15
|
-
reydb-1.1.50.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
16
|
-
reydb-1.1.50.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
|
17
|
-
reydb-1.1.50.dist-info/RECORD,,
|
File without changes
|
File without changes
|