reydb 1.1.59__py3-none-any.whl → 1.1.61__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,11 +9,12 @@
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
20
  from sqlalchemy.sql import sqltypes
@@ -29,12 +30,6 @@ from . import rdb
29
30
  from .rbase import (
30
31
  SessionT,
31
32
  SessionTransactionT,
32
- DatabaseT,
33
- DatabaseORMT,
34
- DatabaseORMStatementSelectT,
35
- DatabaseORMStatementInsertT,
36
- DatabaseORMStatementUpdateT,
37
- DatabaseORMStatementDeleteT,
38
33
  DatabaseBase
39
34
  )
40
35
 
@@ -64,9 +59,15 @@ __all__ = (
64
59
  )
65
60
 
66
61
 
62
+ DatabaseT = TypeVar('DatabaseT', 'rdb.Database', 'rdb.DatabaseAsync')
67
63
  DatabaseORMModelT = TypeVar('DatabaseORMModelT', bound='DatabaseORMModel')
64
+ DatabaseORMT = TypeVar('DatabaseORMT', 'DatabaseORM', 'DatabaseORMAsync')
68
65
  DatabaseORMSessionT = TypeVar('DatabaseORMSessionT', 'DatabaseORMSession', 'DatabaseORMSessionAsync')
69
66
  DatabaseORMStatementReturn = TypeVar('DatabaseORMStatementReturn')
67
+ DatabaseORMStatementSelectT = TypeVar('DatabaseORMStatementSelectT', 'DatabaseORMStatementSelect', 'DatabaseORMStatementSelectAsync')
68
+ DatabaseORMStatementInsertT = TypeVar('DatabaseORMStatementInsertT', 'DatabaseORMStatementInsert', 'DatabaseORMStatementInsertAsync')
69
+ DatabaseORMStatementUpdateT = TypeVar('DatabaseORMStatementUpdateT', 'DatabaseORMStatementUpdate', 'DatabaseORMStatementUpdateAsync')
70
+ DatabaseORMStatementDeleteT = TypeVar('DatabaseORMStatementDeleteT', 'DatabaseORMStatementDelete', 'DatabaseORMStatementDeleteAsync')
70
71
 
71
72
 
72
73
  class DatabaseORMBase(DatabaseBase):
@@ -100,9 +101,19 @@ class DatabaseORMModelMeta(DatabaseORMBase, SQLModelMetaclass):
100
101
  """
101
102
 
102
103
  # Handle parameter.
103
- if attrs['__module__'] == '__main__':
104
+ if '__annotations__' in attrs:
104
105
  table_args = attrs.setdefault('__table_args__', {})
105
106
  table_args['quote'] = True
107
+
108
+ ## Charset.
109
+ attrs.setdefault('__charset__', 'utf8mb4')
110
+ table_args['mysql_charset'] = attrs.pop('__charset__')
111
+
112
+ ## Name.
113
+ if '__name__' in attrs:
114
+ name = attrs.pop('__name__')
115
+
116
+ ## Comment.
106
117
  if '__comment__' in attrs:
107
118
  table_args['comment'] = attrs.pop('__comment__')
108
119
 
@@ -222,7 +233,7 @@ class DatabaseORMModel(DatabaseORMBase, SQLModel, metaclass=model_metaclass):
222
233
  table.comment = comment
223
234
 
224
235
 
225
- class DatabaseORMModelField(DatabaseBase, FieldInfo):
236
+ class DatabaseORMModelField(DatabaseORMBase, FieldInfo):
226
237
  """
227
238
  Database ORM model filed type.
228
239
 
@@ -239,12 +250,12 @@ class DatabaseORMModelField(DatabaseBase, FieldInfo):
239
250
  arg_default: Any | Callable[[], Any] | Null = Null,
240
251
  *,
241
252
  arg_name: str | None = None,
242
- field_default: str | None = None,
253
+ field_default: str | Literal['CURRENT_TIMESTAMP'] | Literal['ON UPDATE CURRENT_TIMESTAMP'] | None = None,
243
254
  filed_name: str | None = None,
244
255
  field_type: TypeEngine | None = None,
245
256
  key: bool = False,
246
257
  key_auto: bool = False,
247
- non_null: bool = False,
258
+ not_null: bool = False,
248
259
  index_n: bool = False,
249
260
  index_u: bool = False,
250
261
  comment: str | None = None,
@@ -276,13 +287,15 @@ class DatabaseORMModelField(DatabaseBase, FieldInfo):
276
287
  arg_name : Call argument name.
277
288
  - `None`: Same as attribute name.
278
289
  field_default : Database field defualt value.
290
+ - `Literal['current_timestamp']`: Set SQL syntax 'current_timestamp', case insensitive.
291
+ - `Literal['on update current_timestamp']`: Set SQL syntax 'on update current_timestamp', case insensitive.
279
292
  filed_name : Database field name.
280
293
  - `None`: Same as attribute name.
281
294
  field_type : Database field type.
282
295
  - `None`: Based type annotation automatic judgment.
283
296
  key : Whether the field is primary key.
284
297
  key_auto : Whether the field is automatic increment primary key.
285
- non_null : Whether the field is non null constraint.
298
+ not_null : Whether the field is not null constraint.
286
299
  index_n : Whether the field add normal index.
287
300
  index_u : Whether the field add unique index.
288
301
  comment : Field commment.
@@ -346,7 +359,11 @@ class DatabaseORMModelField(DatabaseBase, FieldInfo):
346
359
 
347
360
  ## Field default.
348
361
  if 'field_default' in kwargs:
349
- kwargs['sa_column_kwargs']['server_default'] = kwargs.pop('field_default')
362
+ field_default: str = kwargs.pop('field_default')
363
+ field_default_upper = field_default.upper()
364
+ if field_default_upper in ('CURRENT_TIMESTAMP', 'ON UPDATE CURRENT_TIMESTAMP'):
365
+ field_default = sqlalchemy_text(field_default_upper)
366
+ kwargs['sa_column_kwargs']['server_default'] = field_default
350
367
 
351
368
  ## Field name.
352
369
  if 'filed_name' in kwargs:
@@ -363,8 +380,8 @@ class DatabaseORMModelField(DatabaseBase, FieldInfo):
363
380
  kwargs['sa_column_kwargs']['autoincrement'] = False
364
381
 
365
382
  ## Non null.
366
- if 'non_null' in kwargs:
367
- kwargs['nullable'] = not kwargs.pop('non_null')
383
+ if 'not_null' in kwargs:
384
+ kwargs['nullable'] = not kwargs.pop('not_null')
368
385
  else:
369
386
  kwargs['nullable'] = True
370
387
 
@@ -395,7 +412,7 @@ class DatabaseORMSuper(DatabaseORMBase, Generic[DatabaseT, DatabaseORMSessionT])
395
412
  Model = DatabaseORMModel
396
413
  Field = DatabaseORMModelField
397
414
  Config = ConfigDict
398
- tyeps = sqltypes
415
+ types = sqltypes
399
416
  wrap_validate_model = pydantic_model_validator
400
417
  wrap_validate_filed = pydantic_field_validator
401
418
 
@@ -792,12 +809,12 @@ class DatabaseORMSession(
792
809
  skip: bool = False
793
810
  ) -> None:
794
811
  """
795
- Create table.
812
+ Create tables.
796
813
 
797
814
  Parameters
798
815
  ----------
799
816
  models : ORM model instances.
800
- skip : Skip existing table.
817
+ skip : Whether skip existing table.
801
818
  """
802
819
 
803
820
  # Handle parameter.
@@ -821,7 +838,7 @@ class DatabaseORMSession(
821
838
  skip: bool = False
822
839
  ) -> None:
823
840
  """
824
- Delete table.
841
+ Delete tables.
825
842
 
826
843
  Parameters
827
844
  ----------
@@ -839,7 +856,7 @@ class DatabaseORMSession(
839
856
  if None in tables:
840
857
  throw(ValueError, tables)
841
858
 
842
- # Create.
859
+ # Drop.
843
860
  self.orm.metaData.drop_all(self.orm.db.engine, tables, skip)
844
861
 
845
862
 
@@ -1158,12 +1175,12 @@ class DatabaseORMSessionAsync(
1158
1175
  skip: bool = False
1159
1176
  ) -> None:
1160
1177
  """
1161
- Asynchronous create table.
1178
+ Asynchronous create tables.
1162
1179
 
1163
1180
  Parameters
1164
1181
  ----------
1165
1182
  models : ORM model instances.
1166
- skip : Skip existing table.
1183
+ skip : Whether skip existing table.
1167
1184
  """
1168
1185
 
1169
1186
  # Handle parameter.
@@ -1188,7 +1205,7 @@ class DatabaseORMSessionAsync(
1188
1205
  skip: bool = False
1189
1206
  ) -> None:
1190
1207
  """
1191
- Asynchronous delete table.
1208
+ Asynchronous delete tables.
1192
1209
 
1193
1210
  Parameters
1194
1211
  ----------
@@ -1206,7 +1223,7 @@ class DatabaseORMSessionAsync(
1206
1223
  if None in tables:
1207
1224
  throw(ValueError, tables)
1208
1225
 
1209
- # Create.
1226
+ # Drop.
1210
1227
  conn = await self.sess.connection()
1211
1228
  await conn.run_sync(self.orm.metaData.drop_all, tables, skip)
1212
1229
 
reydb/rparam.py CHANGED
@@ -9,21 +9,34 @@
9
9
  """
10
10
 
11
11
 
12
- from typing import Generic, overload
12
+ from typing import Literal, TypeVar, Generic, Final, overload
13
13
 
14
14
  from . import rdb
15
- from .rbase import DatabaseT, DatabaseBase
15
+ from .rbase import DatabaseBase
16
16
  from .rexec import Result
17
17
 
18
18
 
19
19
  __all__ = (
20
+ 'DatabaseSchemaSuper',
20
21
  'DatabaseSchema',
22
+ 'DatabaseSchemaAsync',
23
+ 'DatabaseParametersSuper',
21
24
  'DatabaseParameters',
25
+ 'DatabaseParametersAsync',
26
+ 'DatabaseParametersVariables',
22
27
  'DatabaseParametersStatus',
23
- 'DatabaseParametersVariable'
28
+ 'DatabaseParametersVariablesGlobal',
29
+ 'DatabaseParametersStatusGlobal',
30
+ 'DatabaseParametersVariablesAsync',
31
+ 'DatabaseParametersStatusAsync',
32
+ 'DatabaseParametersVariablesGlobalAsync',
33
+ 'DatabaseParametersStatusGlobalAsync'
24
34
  )
25
35
 
26
36
 
37
+ DatabaseT = TypeVar('DatabaseT', 'rdb.Database', 'rdb.DatabaseAsync')
38
+
39
+
27
40
  class DatabaseSchemaSuper(DatabaseBase, Generic[DatabaseT]):
28
41
  """
29
42
  Database schema super type.
@@ -182,16 +195,18 @@ class DatabaseSchemaAsync(DatabaseSchemaSuper['rdb.DatabaseAsync']):
182
195
  return schema_dict
183
196
 
184
197
 
185
- class DatabaseParameters(DatabaseBase):
198
+ class DatabaseParametersSuper(DatabaseBase, Generic[DatabaseT]):
186
199
  """
187
- Database parameters type.
200
+ Database parameters super type.
188
201
  """
189
202
 
203
+ mode: Literal['VARIABLES', 'STATUS']
204
+ glob: bool
205
+
190
206
 
191
207
  def __init__(
192
208
  self,
193
- db: 'rdb.Database',
194
- global_: bool
209
+ db: DatabaseT
195
210
  ) -> None:
196
211
  """
197
212
  Build instance attributes.
@@ -199,12 +214,16 @@ class DatabaseParameters(DatabaseBase):
199
214
  Parameters
200
215
  ----------
201
216
  db: Database instance.
202
- global\\_ : Whether base global.
203
217
  """
204
218
 
205
219
  # Set parameter.
206
220
  self.db = db
207
- self.global_ = global_
221
+
222
+
223
+ class DatabaseParameters(DatabaseParametersSuper['rdb.Database']):
224
+ """
225
+ Database parameters type.
226
+ """
208
227
 
209
228
 
210
229
  def __getitem__(self, key: str) -> str | None:
@@ -243,12 +262,6 @@ class DatabaseParameters(DatabaseBase):
243
262
  self.update(params)
244
263
 
245
264
 
246
- class DatabaseParametersStatus(DatabaseParameters):
247
- """
248
- Database parameters status type.
249
- """
250
-
251
-
252
265
  @overload
253
266
  def get(self) -> dict[str, str]: ...
254
267
 
@@ -271,14 +284,11 @@ class DatabaseParametersStatus(DatabaseParameters):
271
284
  """
272
285
 
273
286
  # Generate SQL.
274
-
275
- ## Global.
276
- if self.global_:
277
- sql = 'SHOW GLOBAL STATUS'
278
-
279
- ## Not global.
280
- else:
281
- sql = 'SHOW STATUS'
287
+ sql = 'SHOW ' + (
288
+ 'GLOBAL '
289
+ if self.glob
290
+ else ''
291
+ ) + self.mode
282
292
 
283
293
  # Execute SQL.
284
294
 
@@ -309,25 +319,80 @@ class DatabaseParametersStatus(DatabaseParameters):
309
319
  params : Update parameter key value pairs.
310
320
  """
311
321
 
312
- # Throw exception.
313
- raise AssertionError('database status not update')
322
+ # Generate SQL.
323
+ sql_set_list = [
324
+ '%s = %s' % (
325
+ key,
326
+ (
327
+ value
328
+ if type(value) in (int, float)
329
+ else "'%s'" % value
330
+ )
331
+ )
332
+ for key, value in params.items()
333
+ ]
334
+ sql_set = ',\n '.join(sql_set_list)
335
+ sql = 'SHOW ' + (
336
+ 'GLOBAL '
337
+ if self.glob
338
+ else ''
339
+ ) + sql_set
340
+
341
+ # Execute SQL.
342
+ self.db.execute(sql)
314
343
 
315
344
 
316
- class DatabaseParametersVariable(DatabaseParameters):
345
+ class DatabaseParametersAsync(DatabaseParametersSuper['rdb.DatabaseAsync']):
317
346
  """
318
- Database parameters variable type.
347
+ Asynchrouous database parameters type.
319
348
  """
320
349
 
321
350
 
351
+ async def __getitem__(self, key: str) -> str | None:
352
+ """
353
+ Asynchrouous get item of parameter dictionary.
354
+
355
+ Parameters
356
+ ----------
357
+ key : Parameter key.
358
+
359
+ Returns
360
+ -------
361
+ Parameter value.
362
+ """
363
+
364
+ # Get.
365
+ value = await self.get(key)
366
+
367
+ return value
368
+
369
+
370
+ async def __setitem__(self, key: str, value: str | float) -> None:
371
+ """
372
+ Asynchrouous set item of parameter dictionary.
373
+
374
+ Parameters
375
+ ----------
376
+ key : Parameter key.
377
+ value : Parameter value.
378
+ """
379
+
380
+ # Set.
381
+ params = {key: value}
382
+
383
+ # Update.
384
+ await self.update(params)
385
+
386
+
322
387
  @overload
323
- def get(self) -> dict[str, str]: ...
388
+ async def get(self) -> dict[str, str]: ...
324
389
 
325
390
  @overload
326
- def get(self, key: str) -> str | None: ...
391
+ async def get(self, key: str) -> str | None: ...
327
392
 
328
- def get(self, key: str | None = None) -> dict[str, str] | str | None:
393
+ async def get(self, key: str | None = None) -> dict[str, str] | str | None:
329
394
  """
330
- Get parameter.
395
+ Asynchrouous get parameter.
331
396
 
332
397
  Parameters
333
398
  ----------
@@ -337,48 +402,49 @@ class DatabaseParametersVariable(DatabaseParameters):
337
402
 
338
403
  Returns
339
404
  -------
340
- Variables of database.
405
+ Status of database.
341
406
  """
342
407
 
343
408
  # Generate SQL.
344
-
345
- ## Global.
346
- if self.global_:
347
- sql = 'SHOW GLOBAL VARIABLES'
348
-
349
- ## Not global.
350
- else:
351
- sql = 'SHOW VARIABLES'
409
+ sql = 'SHOW ' + (
410
+ 'GLOBAL '
411
+ if self.glob
412
+ else ''
413
+ ) + self.mode
352
414
 
353
415
  # Execute SQL.
354
416
 
355
417
  ## Dictionary.
356
418
  if key is None:
357
- result = self.db.execute(sql, key=key)
358
- variables = result.to_dict(val_field=1)
419
+ result = await self.db.execute(sql, key=key)
420
+ status = result.to_dict(val_field=1)
359
421
 
360
422
  ## Value.
361
423
  else:
362
424
  sql += ' LIKE :key'
363
- result = self.db.execute(sql, key=key)
425
+ result = await self.db.execute(sql, key=key)
364
426
  row = result.first()
365
427
  if row is None:
366
- variables = None
428
+ status = None
367
429
  else:
368
- variables = row['Value']
430
+ status = row['Value']
369
431
 
370
- return variables
432
+ return status
371
433
 
372
434
 
373
- def update(self, params: dict[str, str | float]) -> None:
435
+ async def update(self, params: dict[str, str | float]) -> None:
374
436
  """
375
- Update parameter.
437
+ Asynchrouous update parameter.
376
438
 
377
439
  Parameters
378
440
  ----------
379
441
  params : Update parameter key value pairs.
380
442
  """
381
443
 
444
+ # Check.
445
+ if self.mode == 'STATUS':
446
+ raise AssertionError('database status not update')
447
+
382
448
  # Generate SQL.
383
449
  sql_set_list = [
384
450
  '%s = %s' % (
@@ -392,14 +458,83 @@ class DatabaseParametersVariable(DatabaseParameters):
392
458
  for key, value in params.items()
393
459
  ]
394
460
  sql_set = ',\n '.join(sql_set_list)
461
+ sql = 'SHOW ' + (
462
+ 'GLOBAL '
463
+ if self.glob
464
+ else ''
465
+ ) + sql_set
395
466
 
396
- ## Global.
397
- if self.global_:
398
- sql = f'SET GLOBAL {sql_set}'
467
+ # Execute SQL.
468
+ await self.db.execute(sql)
399
469
 
400
- ## Not global.
401
- else:
402
- sql = f'SET {sql_set}'
403
470
 
404
- # Execute SQL.
405
- self.db.execute(sql)
471
+ class DatabaseParametersVariables(DatabaseParameters):
472
+ """
473
+ Database variable parameters type.
474
+ """
475
+
476
+ mode: Final = 'VARIABLES'
477
+ glob: Final = False
478
+
479
+
480
+ class DatabaseParametersStatus(DatabaseParameters):
481
+ """
482
+ Database status parameters type.
483
+ """
484
+
485
+ mode: Final = 'STATUS'
486
+ glob: Final = False
487
+
488
+
489
+ class DatabaseParametersVariablesGlobal(DatabaseParameters):
490
+ """
491
+ Database global variable parameters type.
492
+ """
493
+
494
+ mode: Final = 'VARIABLES'
495
+ glob: Final = True
496
+
497
+
498
+ class DatabaseParametersStatusGlobal(DatabaseParameters):
499
+ """
500
+ Database global status parameters type.
501
+ """
502
+
503
+ mode: Final = 'STATUS'
504
+ glob: Final = True
505
+
506
+
507
+ class DatabaseParametersVariablesAsync(DatabaseParametersAsync):
508
+ """
509
+ Asynchrouous database variable parameters type.
510
+ """
511
+
512
+ mode: Final = 'VARIABLES'
513
+ glob: Final = False
514
+
515
+
516
+ class DatabaseParametersStatusAsync(DatabaseParametersAsync):
517
+ """
518
+ Asynchrouous database status parameters type.
519
+ """
520
+
521
+ mode: Final = 'STATUS'
522
+ glob: Final = False
523
+
524
+
525
+ class DatabaseParametersVariablesGlobalAsync(DatabaseParametersAsync):
526
+ """
527
+ Asynchrouous database global variable parameters type.
528
+ """
529
+
530
+ mode: Final = 'VARIABLES'
531
+ glob: Final = True
532
+
533
+
534
+ class DatabaseParametersStatusGlobalAsync(DatabaseParametersAsync):
535
+ """
536
+ Asynchrouous database global status parameters type.
537
+ """
538
+
539
+ mode: Final = 'STATUS'
540
+ glob: Final = True
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: reydb
3
- Version: 1.1.59
3
+ Version: 1.1.61
4
4
  Summary: Database method set.
5
5
  Project-URL: homepage, https://github.com/reyxbo/reydb/
6
6
  Author-email: Rey <reyxbo@163.com>
@@ -0,0 +1,16 @@
1
+ reydb/__init__.py,sha256=zuQ14sl7rZ6lQ1P566ALjiRi-PcIXj9Ff0U_yYNQHFE,581
2
+ reydb/rall.py,sha256=8tWDeGRLKujqPHFy4898WCIgZEA4OxOfeQ8xG6Fw69c,387
3
+ reydb/rbase.py,sha256=16fG_YWnvJFu0ElXvc3BS9PtSJFZ9M631Te0b2ewWDE,9457
4
+ reydb/rbuild.py,sha256=-C8ORqJSa6tDBMm7Y-ZcN80sgWs5rX6I0aO0DioXNm4,46599
5
+ reydb/rconfig.py,sha256=mA40xrC5O9Y3lHnuJYDRUqRsLk3s2BFxgXou2tnFAXM,21196
6
+ reydb/rconn.py,sha256=guRaR8N6RuzZzujwaeq7HhKWTizF9SrUBqEAFjfjpoo,6909
7
+ reydb/rdb.py,sha256=Dh0J26fIfRrX8EK25cH1MfFzPLmWC-VnvXjLgiS_wFM,13824
8
+ reydb/rerror.py,sha256=309PJVQG7jsL2A2R5IW4zuS4IojBgoGx3qxwIotBh_k,9896
9
+ reydb/rexec.py,sha256=sOiapQnnbMhnItgYhUZ4iz8xRolsUJz6inCH1-7hb54,52074
10
+ reydb/rfile.py,sha256=usK1W39O3Fi7FoAljhDIFgqj15c1GrQv3ASYUt1IxdA,15087
11
+ reydb/rorm.py,sha256=heJf_vVJgjlNNQHbkpS_Q2rk8CBmRdOOSO7RZBfHulE,40355
12
+ reydb/rparam.py,sha256=hXpnT4YnTgZFjyuc6bf2TCc-p1HQ7ycJjamq7I1xook,12851
13
+ reydb-1.1.61.dist-info/METADATA,sha256=U2Xa2A4pdO1OKHmYgzphmIWh0cma7fCBi31grlX8iwQ,1622
14
+ reydb-1.1.61.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
15
+ reydb-1.1.61.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
16
+ reydb-1.1.61.dist-info/RECORD,,