reydb 1.1.60__py3-none-any.whl → 1.2.0__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/rorm.py CHANGED
@@ -9,14 +9,16 @@
9
9
  """
10
10
 
11
11
 
12
- from typing import Self, Any, Type, TypeVar, Generic, Final, overload
12
+ from typing import Self, Any, Type, Literal, TypeVar, Generic, Final, overload
13
13
  from collections.abc import Callable
14
14
  from functools import wraps as functools_wraps
15
15
  from inspect import iscoroutinefunction as inspect_iscoroutinefunction
16
16
  from pydantic import ConfigDict, field_validator as pydantic_field_validator, model_validator as pydantic_model_validator
17
+ from sqlalchemy import text as sqlalchemy_text
17
18
  from sqlalchemy.orm import SessionTransaction
18
19
  from sqlalchemy.ext.asyncio import AsyncSessionTransaction
19
- from sqlalchemy.sql import sqltypes
20
+ from sqlalchemy import types
21
+ from sqlalchemy.dialects.mysql import types as types_mysql
20
22
  from sqlalchemy.sql.sqltypes import TypeEngine
21
23
  from sqlalchemy.sql.dml import Insert, Update, Delete
22
24
  from sqlmodel import SQLModel, Session, Table
@@ -36,8 +38,9 @@ from .rbase import (
36
38
  __all__ = (
37
39
  'DatabaseORMBase',
38
40
  'DatabaseORMModelMeta',
39
- 'DatabaseORMModel',
40
41
  'DatabaseORMModelField',
42
+ 'DatabaseORMModel',
43
+ 'DatabaseORMModelMethod',
41
44
  'DatabaseORMSuper',
42
45
  'DatabaseORM',
43
46
  'DatabaseORMAsync',
@@ -100,7 +103,7 @@ class DatabaseORMModelMeta(DatabaseORMBase, SQLModelMetaclass):
100
103
  """
101
104
 
102
105
  # Handle parameter.
103
- if attrs['__module__'] == '__main__':
106
+ if '__annotations__' in attrs:
104
107
  table_args = attrs.setdefault('__table_args__', {})
105
108
  table_args['quote'] = True
106
109
 
@@ -110,7 +113,7 @@ class DatabaseORMModelMeta(DatabaseORMBase, SQLModelMetaclass):
110
113
 
111
114
  ## Name.
112
115
  if '__name__' in attrs:
113
- table_args['name'] = attrs.pop('__name__')
116
+ attrs['__tablename__'] = attrs.pop('__name__')
114
117
 
115
118
  ## Comment.
116
119
  if '__comment__' in attrs:
@@ -130,109 +133,7 @@ class DatabaseORMModelMeta(DatabaseORMBase, SQLModelMetaclass):
130
133
  return new_cls
131
134
 
132
135
 
133
- model_metaclass: SQLModelMetaclass = DatabaseORMModelMeta
134
-
135
-
136
- class DatabaseORMModel(DatabaseORMBase, SQLModel, metaclass=model_metaclass):
137
- """
138
- Database ORM model type.
139
-
140
- Examples
141
- --------
142
- >>> class Foo(DatabaseORMModel, table=True):
143
- ... __comment__ = 'Table comment.'
144
- ... ...
145
- """
146
-
147
-
148
- def update(self, data: 'DatabaseORMModel | dict[dict, Any]') -> None:
149
- """
150
- Update attributes.
151
-
152
- Parameters
153
- ----------
154
- data : `DatabaseORMModel` or `dict`.
155
- """
156
-
157
- # Update.
158
- self.sqlmodel_update(data)
159
-
160
-
161
- def validate(self) -> Self:
162
- """
163
- Validate all attributes, and copy self instance to new instance.
164
- """
165
-
166
- # Validate.
167
- model = self.model_validate(self)
168
-
169
- return model
170
-
171
-
172
- def copy(self) -> Self:
173
- """
174
- Copy self instance to new instance.
175
-
176
- Returns
177
- -------
178
- New instance.
179
- """
180
-
181
- # Copy.
182
- data = self.data
183
- instance = self.__class__(**data)
184
-
185
- return instance
186
-
187
-
188
- @property
189
- def data(self) -> dict[str, Any]:
190
- """
191
- All attributes data.
192
-
193
- Returns
194
- -------
195
- data.
196
- """
197
-
198
- # Get.
199
- data = self.model_dump()
200
-
201
- return data
202
-
203
-
204
- @classmethod
205
- def table(cls_or_self) -> Table | None:
206
- """
207
- Mapping `Table` instance.
208
-
209
- Returns
210
- -------
211
- Instance or null.
212
- """
213
-
214
- # Get.
215
- table: Table | None = getattr(cls_or_self, '__table__', None)
216
-
217
- return table
218
-
219
-
220
- @classmethod
221
- def comment(cls_or_self, comment: str) -> None:
222
- """
223
- Set table comment.
224
-
225
- Parameters
226
- ----------
227
- comment : Comment.
228
- """
229
-
230
- # Set.
231
- table = cls_or_self.table()
232
- table.comment = comment
233
-
234
-
235
- class DatabaseORMModelField(DatabaseBase, FieldInfo):
136
+ class DatabaseORMModelField(DatabaseORMBase, FieldInfo):
236
137
  """
237
138
  Database ORM model filed type.
238
139
 
@@ -249,7 +150,7 @@ class DatabaseORMModelField(DatabaseBase, FieldInfo):
249
150
  arg_default: Any | Callable[[], Any] | Null = Null,
250
151
  *,
251
152
  arg_name: str | None = None,
252
- field_default: str | None = None,
153
+ field_default: str | Literal['CURRENT_TIMESTAMP'] | Literal['ON UPDATE CURRENT_TIMESTAMP'] | None = None,
253
154
  filed_name: str | None = None,
254
155
  field_type: TypeEngine | None = None,
255
156
  key: bool = False,
@@ -286,7 +187,9 @@ class DatabaseORMModelField(DatabaseBase, FieldInfo):
286
187
  arg_name : Call argument name.
287
188
  - `None`: Same as attribute name.
288
189
  field_default : Database field defualt value.
289
- filed_name : Database field name.
190
+ - `Literal['current_timestamp']`: Set SQL syntax 'current_timestamp', case insensitive.
191
+ - `Literal['on update current_timestamp']`: Set SQL syntax 'on update current_timestamp', case insensitive.
192
+ field_name : Database field name.
290
193
  - `None`: Same as attribute name.
291
194
  field_type : Database field type.
292
195
  - `None`: Based type annotation automatic judgment.
@@ -356,15 +259,19 @@ class DatabaseORMModelField(DatabaseBase, FieldInfo):
356
259
 
357
260
  ## Field default.
358
261
  if 'field_default' in kwargs:
359
- kwargs['sa_column_kwargs']['server_default'] = kwargs.pop('field_default')
262
+ field_default: str = kwargs.pop('field_default')
263
+ field_default_upper = field_default.upper()
264
+ if field_default_upper in ('CURRENT_TIMESTAMP', 'ON UPDATE CURRENT_TIMESTAMP'):
265
+ field_default = sqlalchemy_text(field_default_upper)
266
+ kwargs['sa_column_kwargs']['server_default'] = field_default
360
267
 
361
268
  ## Field name.
362
- if 'filed_name' in kwargs:
363
- kwargs['sa_column_kwargs']['name'] = kwargs.pop('filed_name')
269
+ if 'field_name' in kwargs:
270
+ kwargs['sa_column_kwargs']['name'] = kwargs.pop('field_name')
364
271
 
365
272
  ## Field type.
366
- if 'filed_name' in kwargs:
367
- kwargs['sa_column_kwargs']['type_'] = kwargs.pop('filed_type')
273
+ if 'field_type' in kwargs:
274
+ kwargs['sa_type'] = kwargs.pop('field_type')
368
275
 
369
276
  ## Key auto.
370
277
  if 'key_auto' in kwargs:
@@ -386,6 +293,129 @@ class DatabaseORMModelField(DatabaseBase, FieldInfo):
386
293
  super().__init__(**kwargs)
387
294
 
388
295
 
296
+ model_metaclass: SQLModelMetaclass = DatabaseORMModelMeta
297
+
298
+
299
+ class DatabaseORMModel(DatabaseORMBase, SQLModel, metaclass=model_metaclass):
300
+ """
301
+ Database ORM model type.
302
+
303
+ Examples
304
+ --------
305
+ >>> class Foo(DatabaseORMModel, table=True):
306
+ ... __name__ = 'Table name, default is class name.'
307
+ ... __comment__ = 'Table comment.'
308
+ ... ...
309
+ """
310
+
311
+
312
+ @classmethod
313
+ def _table(cls_or_self) -> Table:
314
+ """
315
+ Return mapping database table instance.
316
+
317
+ Returns
318
+ -------
319
+ Table instance.
320
+ """
321
+
322
+ # Get.
323
+ table: Table = cls_or_self.__table__
324
+
325
+ return table
326
+
327
+
328
+ @property
329
+ def _m(self):
330
+ """
331
+ Build database ORM model method instance.
332
+
333
+ Returns
334
+ -------
335
+ Instance.
336
+ """
337
+
338
+ # Build.
339
+ method = DatabaseORMModelMethod(self)
340
+
341
+ return method
342
+
343
+
344
+ class DatabaseORMModelMethod(DatabaseORMBase):
345
+ """
346
+ Database ORM model method type.
347
+ """
348
+
349
+
350
+ def __init__(self, model: DatabaseORMModel) -> None:
351
+ """
352
+ Build instance attributes.
353
+
354
+ Parameters
355
+ ----------
356
+ model : Database ORM model instance.
357
+ """
358
+
359
+ # Build.
360
+ self.model = model
361
+
362
+
363
+ @property
364
+ def data(self) -> dict[str, Any]:
365
+ """
366
+ All attributes data.
367
+
368
+ Returns
369
+ -------
370
+ data.
371
+ """
372
+
373
+ # Get.
374
+ data = self.model.model_dump()
375
+
376
+ return data
377
+
378
+
379
+ def update(self, data: 'DatabaseORMModel | dict[dict, Any]') -> None:
380
+ """
381
+ Update attributes.
382
+
383
+ Parameters
384
+ ----------
385
+ data : `DatabaseORMModel` or `dict`.
386
+ """
387
+
388
+ # Update.
389
+ self.model.sqlmodel_update(data)
390
+
391
+
392
+ def validate(self) -> Self:
393
+ """
394
+ Validate all attributes, and copy self instance to new instance.
395
+ """
396
+
397
+ # Validate.
398
+ model = self.model.model_validate(self.model)
399
+
400
+ return model
401
+
402
+
403
+ def copy(self) -> Self:
404
+ """
405
+ Copy self instance to new instance.
406
+
407
+ Returns
408
+ -------
409
+ New instance.
410
+ """
411
+
412
+ # Copy.
413
+ data = self.data
414
+ instance = self.model.__class__(**data)
415
+
416
+ return instance
417
+
418
+
389
419
  class DatabaseORMSuper(DatabaseORMBase, Generic[DatabaseT, DatabaseORMSessionT]):
390
420
  """
391
421
  Database ORM super type.
@@ -405,7 +435,8 @@ class DatabaseORMSuper(DatabaseORMBase, Generic[DatabaseT, DatabaseORMSessionT])
405
435
  Model = DatabaseORMModel
406
436
  Field = DatabaseORMModelField
407
437
  Config = ConfigDict
408
- types = sqltypes
438
+ types = types
439
+ types_mysql = types_mysql
409
440
  wrap_validate_model = pydantic_model_validator
410
441
  wrap_validate_filed = pydantic_field_validator
411
442
 
@@ -812,7 +843,7 @@ class DatabaseORMSession(
812
843
 
813
844
  # Handle parameter.
814
845
  tables = [
815
- model.table()
846
+ model._table()
816
847
  for model in models
817
848
  ]
818
849
 
@@ -841,7 +872,7 @@ class DatabaseORMSession(
841
872
 
842
873
  # Handle parameter.
843
874
  tables = [
844
- model.table()
875
+ model._table()
845
876
  for model in models
846
877
  ]
847
878
 
@@ -1178,7 +1209,7 @@ class DatabaseORMSessionAsync(
1178
1209
 
1179
1210
  # Handle parameter.
1180
1211
  tables = [
1181
- model.table()
1212
+ model._table()
1182
1213
  for model in models
1183
1214
  ]
1184
1215
 
@@ -1208,7 +1239,7 @@ class DatabaseORMSessionAsync(
1208
1239
 
1209
1240
  # Handle parameter.
1210
1241
  tables = [
1211
- model.table()
1242
+ model._table()
1212
1243
  for model in models
1213
1244
  ]
1214
1245