reydb 1.1.61__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/rconfig.py CHANGED
@@ -20,6 +20,7 @@ from reykit.rbase import Null, throw
20
20
 
21
21
  from . import rdb
22
22
  from .rbase import DatabaseBase
23
+ from .rorm import DatabaseORM as orm
23
24
 
24
25
 
25
26
  __all__ = (
@@ -40,29 +41,16 @@ class DatabaseConfigSuper(DatabaseBase, Generic[DatabaseT]):
40
41
  """
41
42
  Database config super type.
42
43
  Can create database used `self.build_db` method.
43
-
44
- Examples
45
- --------
46
- >>> config = DatabaseConfig()
47
- >>> config['key1'] = 1
48
- >>> config['key2', 'note'] = 2
49
- >>> config['key1'], config['key2']
50
- (1, 2)
51
44
  """
52
45
 
53
46
 
54
- def __init__(
55
- self,
56
- db: DatabaseT,
57
- db_names: dict[str, str] | None = None
58
- ) -> None:
47
+ def __init__(self, db: DatabaseT) -> None:
59
48
  """
60
49
  Build instance attributes.
61
50
 
62
51
  Parameters
63
52
  ----------
64
53
  db: Database instance.
65
- db_names: Update build database table names.
66
54
  """
67
55
 
68
56
  # Build.
@@ -71,55 +59,38 @@ class DatabaseConfigSuper(DatabaseBase, Generic[DatabaseT]):
71
59
  'config': 'config',
72
60
  'stats_config': 'stats_config'
73
61
  }
74
- if db_names is not None:
75
- self.db_names.update(db_names)
76
62
 
77
63
 
78
- class DatabaseConfig(DatabaseConfigSuper['rdb.Database']):
79
- """
80
- Database config type.
81
- Can create database used `self.build_db` method.
82
-
83
- Examples
84
- --------
85
- >>> config = DatabaseConfig()
86
- >>> config['key1'] = 1
87
- >>> config['key2', 'note'] = 2
88
- >>> config['key1'], config['key2']
89
- (1, 2)
90
- """
91
-
92
-
93
- def build_db(self) -> None:
64
+ def handle_build_db(self) -> None:
94
65
  """
95
- Check and build database tables, by `self.db_names`.
66
+ Handle method of check and build database tables, by `self.db_names`.
96
67
  """
97
68
 
98
69
  # Handle parameter.
99
70
 
100
71
  ## Table.
101
- class Config(self.db.orm.Model, table=True):
72
+ class Config(orm.Model, table=True):
102
73
  __name__ = self.db_names['config']
103
74
  __comment__ = 'Config data table.'
104
- create_time: Datetime = self.db.orm.Field(not_null=True, field_default='CURRENT_TIMESTAMP', index_n=True, comment='Config create time.')
105
- update_time: Datetime = self.db.orm.Field(field_default='CURRENT_TIMESTAMP', index_n=True, comment='Config update time.')
106
- key: str = self.db.orm.Field(type=self.db.orm.types.VARCHAR(50), key=True, not_null=True, comment='Config key.')
107
- value: str = self.db.orm.Field(type=self.db.orm.types.TEXT, not_null=True, comment='Config value.')
108
- type: str = self.db.orm.Field(type=self.db.orm.types.VARCHAR(50), not_null=True, comment='Config value type.')
109
- note: str = self.db.orm.Field(type=self.db.orm.types.VARCHAR(500), comment='Config note.')
75
+ create_time: Datetime = orm.Field(field_default='CURRENT_TIMESTAMP', not_null=True, index_n=True, comment='Config create time.')
76
+ update_time: Datetime = orm.Field(field_default='CURRENT_TIMESTAMP', index_n=True, comment='Config update time.')
77
+ key: str = orm.Field(field_type=orm.types.VARCHAR(50), key=True, not_null=True, comment='Config key.')
78
+ value: str = orm.Field(field_type=orm.types.TEXT, not_null=True, comment='Config value.')
79
+ type: str = orm.Field(field_type=orm.types.VARCHAR(50), not_null=True, comment='Config value type.')
80
+ note: str = orm.Field(field_type=orm.types.VARCHAR(500), comment='Config note.')
110
81
 
111
82
  tables = [Config]
112
83
 
113
84
  ## View stats.
114
85
  views_stats = [
115
86
  {
116
- 'path': (self.db.database, self.db_names['stats_config']),
87
+ 'path': self.db_names['stats_config'],
117
88
  'items': [
118
89
  {
119
90
  'name': 'count',
120
91
  'select': (
121
92
  'SELECT COUNT(1)\n'
122
- f'FROM `{self.db_names['config']}`'
93
+ f'FROM `{self.db.database}`.`{self.db_names['config']}`'
123
94
  ),
124
95
  'comment': 'Config count.'
125
96
  },
@@ -127,7 +98,7 @@ class DatabaseConfig(DatabaseConfigSuper['rdb.Database']):
127
98
  'name': 'last_create_time',
128
99
  'select': (
129
100
  'SELECT MAX(`create_time`)\n'
130
- f'FROM `{self.db_names['config']}`'
101
+ f'FROM `{self.db.database}`.`{self.db_names['config']}`'
131
102
  ),
132
103
  'comment': 'Config last record create time.'
133
104
  },
@@ -135,15 +106,40 @@ class DatabaseConfig(DatabaseConfigSuper['rdb.Database']):
135
106
  'name': 'last_update_time',
136
107
  'select': (
137
108
  'SELECT MAX(`update_time`)\n'
138
- f'FROM `{self.db_names['config']}`'
109
+ f'FROM `{self.db.database}`.`{self.db_names['config']}`'
139
110
  ),
140
111
  'comment': 'Config last record update time.'
141
112
  }
142
113
  ]
143
114
  }
144
-
145
115
  ]
146
116
 
117
+ return tables, views_stats
118
+
119
+
120
+ class DatabaseConfig(DatabaseConfigSuper['rdb.Database']):
121
+ """
122
+ Database config type.
123
+ Can create database used `self.build_db` method.
124
+
125
+ Examples
126
+ --------
127
+ >>> config = DatabaseConfig()
128
+ >>> config['key1'] = 1
129
+ >>> config['key2', 'note'] = 2
130
+ >>> config['key1'], config['key2']
131
+ (1, 2)
132
+ """
133
+
134
+
135
+ def build_db(self) -> None:
136
+ """
137
+ Check and build database tables, by `self.db_names`.
138
+ """
139
+
140
+ # Handle parameter.
141
+ tables, views_stats = self.handle_build_db()
142
+
147
143
  # Build.
148
144
  self.db.build.build(tables=tables, views_stats=views_stats, skip=True)
149
145
 
@@ -454,9 +450,9 @@ class DatabaseConfigAsync(DatabaseConfigSuper['rdb.DatabaseAsync']):
454
450
  Examples
455
451
  --------
456
452
  >>> config = DatabaseConfig()
457
- >>> config['key1'] = 1
458
- >>> config['key2', 'note'] = 2
459
- >>> config['key1'], config['key2']
453
+ >>> await config['key1'] = 1
454
+ >>> await config['key2', 'note'] = 2
455
+ >>> await config['key1'], config['key2']
460
456
  (1, 2)
461
457
  """
462
458
 
@@ -467,53 +463,7 @@ class DatabaseConfigAsync(DatabaseConfigSuper['rdb.DatabaseAsync']):
467
463
  """
468
464
 
469
465
  # Handle parameter.
470
-
471
- ## Table.
472
- class Config(self.db.orm.Model, table=True):
473
- __name__ = self.db_names['config']
474
- __comment__ = 'Config data table.'
475
- create_time: Datetime = self.db.orm.Field(not_null=True, field_default='CURRENT_TIMESTAMP', index_n=True, comment='Config create time.')
476
- update_time: Datetime = self.db.orm.Field(field_default='CURRENT_TIMESTAMP', index_n=True, comment='Config update time.')
477
- key: str = self.db.orm.Field(type=self.db.orm.types.VARCHAR(50), key=True, not_null=True, comment='Config key.')
478
- value: str = self.db.orm.Field(type=self.db.orm.types.TEXT, not_null=True, comment='Config value.')
479
- type: str = self.db.orm.Field(type=self.db.orm.types.VARCHAR(50), not_null=True, comment='Config value type.')
480
- note: str = self.db.orm.Field(type=self.db.orm.types.VARCHAR(500), comment='Config note.')
481
-
482
- tables = [Config]
483
-
484
- ## View stats.
485
- views_stats = [
486
- {
487
- 'path': (self.db.database, self.db_names['stats_config']),
488
- 'items': [
489
- {
490
- 'name': 'count',
491
- 'select': (
492
- 'SELECT COUNT(1)\n'
493
- f'FROM `{self.db_names['config']}`'
494
- ),
495
- 'comment': 'Config count.'
496
- },
497
- {
498
- 'name': 'last_create_time',
499
- 'select': (
500
- 'SELECT MAX(`create_time`)\n'
501
- f'FROM `{self.db_names['config']}`'
502
- ),
503
- 'comment': 'Config last record create time.'
504
- },
505
- {
506
- 'name': 'last_update_time',
507
- 'select': (
508
- 'SELECT MAX(`update_time`)\n'
509
- f'FROM `{self.db_names['config']}`'
510
- ),
511
- 'comment': 'Config last record update time.'
512
- }
513
- ]
514
- }
515
-
516
- ]
466
+ tables, views_stats = self.handle_build_db()
517
467
 
518
468
  # Build.
519
469
  await self.db.build.build(tables=tables, views_stats=views_stats, skip=True)
reydb/rdb.py CHANGED
@@ -16,7 +16,7 @@ from sqlalchemy import Engine, create_engine as sqlalchemy_create_engine
16
16
  from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine as sqlalchemy_create_async_engine
17
17
  from reykit.rtext import join_data_text
18
18
 
19
- from . import rbase, rbuild, rconfig, rconn, rerror, rexec, rfile, rorm, rparam
19
+ from . import rbase, rbuild, rconfig, rconn, rerror, rexec, rorm, rparam
20
20
 
21
21
 
22
22
  __all__ = (
@@ -113,6 +113,9 @@ class DatabaseSuper(
113
113
  self.report = report
114
114
  self.query = query
115
115
 
116
+ ## Schema.
117
+ self._schema: dict[str, dict[str, list[str]]] | None = None
118
+
116
119
  ## Create engine.
117
120
  self.engine = self.__create_engine()
118
121
 
@@ -336,22 +339,6 @@ class DatabaseSuper(
336
339
  return build
337
340
 
338
341
 
339
- @property
340
- def file(self):
341
- """
342
- Build database file instance.
343
-
344
- Returns
345
- -------
346
- Instance.
347
- """
348
-
349
- # Build.
350
- dbfile = rfile.DatabaseFile(self)
351
-
352
- return dbfile
353
-
354
-
355
342
  @property
356
343
  def error(self):
357
344
  """
@@ -363,9 +350,13 @@ class DatabaseSuper(
363
350
  """
364
351
 
365
352
  # Build.
366
- dbfile = rerror.DatabaseError(self)
353
+ match self:
354
+ case Database():
355
+ error = rerror.DatabaseError(self)
356
+ case DatabaseAsync():
357
+ error = rerror.DatabaseErrorAsync(self)
367
358
 
368
- return dbfile
359
+ return error
369
360
 
370
361
 
371
362
  @property