reydb 1.1.58__py3-none-any.whl → 1.1.60__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
@@ -49,109 +49,54 @@ class DatabaseConfig(DatabaseBase):
49
49
  """
50
50
 
51
51
 
52
- def __init__(self, db: 'rdb.Database') -> None:
52
+ def __init__(
53
+ self,
54
+ db: 'rdb.Database',
55
+ db_names: dict[str, str] | None = None
56
+ ) -> None:
53
57
  """
54
58
  Build instance attributes.
55
59
 
56
60
  Parameters
57
61
  ----------
58
62
  db: Database instance.
63
+ db_names: Update build database table names.
59
64
  """
60
65
 
61
66
  # Build.
62
67
  self.db = db
63
-
64
- ## Database path name.
65
68
  self.db_names = {
66
- 'base': 'base',
67
- 'base.config': 'config',
68
- 'base.stats_config': 'stats_config'
69
+ 'config': 'config',
70
+ 'stats_config': 'stats_config'
69
71
  }
72
+ if db_names is not None:
73
+ self.db_names.update(db_names)
70
74
 
71
75
 
72
76
  def build_db(self) -> None:
73
77
  """
74
- Check and build all standard databases and tables, by `self.db_names`.
78
+ Check and build database tables, by `self.db_names`.
75
79
  """
76
80
 
77
- # Set parameter.
78
-
79
- ## Database.
80
- databases = [
81
- {
82
- 'name': self.db_names['base']
83
- }
84
- ]
81
+ # Handle parameter.
85
82
 
86
83
  ## Table.
87
- tables = [
88
-
89
- ### 'config'.
90
- {
91
- 'path': (self.db_names['base'], self.db_names['base.config']),
92
- 'fields': [
93
- {
94
- 'name': 'create_time',
95
- 'type': 'datetime',
96
- 'constraint': 'NOT NULL DEFAULT CURRENT_TIMESTAMP',
97
- 'comment': 'Config create time.'
98
- },
99
- {
100
- 'name': 'update_time',
101
- 'type': 'datetime',
102
- 'constraint': 'DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP',
103
- 'comment': 'Config update time.'
104
- },
105
- {
106
- 'name': 'key',
107
- 'type': 'varchar(50)',
108
- 'constraint': 'NOT NULL',
109
- 'comment': 'Config key.'
110
- },
111
- {
112
- 'name': 'value',
113
- 'type': 'text',
114
- 'constraint': 'NOT NULL',
115
- 'comment': 'Config value.'
116
- },
117
- {
118
- 'name': 'type',
119
- 'type': 'varchar(50)',
120
- 'constraint': 'NOT NULL',
121
- 'comment': 'Config value type.'
122
- },
123
- {
124
- 'name': 'note',
125
- 'type': 'varchar(500)',
126
- 'comment': 'Config note.'
127
- }
128
- ],
129
- 'primary': 'key',
130
- 'indexes': [
131
- {
132
- 'name': 'n_create_time',
133
- 'fields': 'create_time',
134
- 'type': 'noraml',
135
- 'comment': 'Config create time normal index.'
136
- },
137
- {
138
- 'name': 'n_update_time',
139
- 'fields': 'update_time',
140
- 'type': 'noraml',
141
- 'comment': 'Config update time normal index.'
142
- }
143
- ],
144
- 'comment': 'Config data table.'
145
- }
146
-
147
- ]
84
+ class Config(self.db.orm.Model, table=True):
85
+ __name__ = self.db_names['config']
86
+ __commment__ = 'Config data table.'
87
+ create_time: datetime = self.db.orm.Field(not_null=True, field_default='CURRENT_TIMESTAMP', index_n=True, comment='Config create time.')
88
+ update_time: datetime = self.db.orm.Field(field_default='CURRENT_TIMESTAMP', index_n=True, comment='Config update time.')
89
+ key = self.db.orm.Field(type=self.db.orm.types.VARCHAR(50), key=True, not_null=True, comment='Config key.')
90
+ value = self.db.orm.Field(type=self.db.orm.types.TEXT, not_null=True, comment='Config value.')
91
+ type = self.db.orm.Field(type=self.db.orm.types.VARCHAR(50), not_null=True, comment='Config value type.')
92
+ note = self.db.orm.Field(type=self.db.orm.types.VARCHAR(500), comment='Config note.')
93
+
94
+ tables = [Config]
148
95
 
149
96
  ## View stats.
150
97
  views_stats = [
151
-
152
- ### 'stats_config'.
153
98
  {
154
- 'path': (self.db_names['base'], self.db_names['base.stats_config']),
99
+ 'path': self.db_names['stats_config'],
155
100
  'items': [
156
101
  {
157
102
  'name': 'count',
@@ -178,13 +123,12 @@ class DatabaseConfig(DatabaseBase):
178
123
  'comment': 'Config last record update time.'
179
124
  }
180
125
  ]
181
-
182
126
  }
183
127
 
184
128
  ]
185
129
 
186
130
  # Build.
187
- self.db.build.build(databases, tables, views_stats=views_stats)
131
+ self.db.build.build(tables=tables, views_stats=views_stats)
188
132
 
189
133
 
190
134
  @property
@@ -199,7 +143,7 @@ class DatabaseConfig(DatabaseBase):
199
143
 
200
144
  # Get.
201
145
  result = self.db.execute.select(
202
- (self.db_names['base'], self.db_names['base.config']),
146
+ self.db_names['config'],
203
147
  ['key', 'value', 'type', 'note'],
204
148
  order='IFNULL(`update_time`, `create_time`) DESC'
205
149
  )
@@ -235,7 +179,7 @@ class DatabaseConfig(DatabaseBase):
235
179
  # Get.
236
180
  where = '`key` = :key'
237
181
  result = self.db.execute.select(
238
- (self.db_names['base'], self.db_names['base.config']),
182
+ self.db_names['config'],
239
183
  '`value`',
240
184
  where,
241
185
  limit=1,
@@ -281,7 +225,7 @@ class DatabaseConfig(DatabaseBase):
281
225
  'note': default_note
282
226
  }
283
227
  result = self.db.execute.insert(
284
- (self.db_names['base'], self.db_names['base.config']),
228
+ self.db_names['config'],
285
229
  data,
286
230
  'ignore'
287
231
  )
@@ -322,7 +266,7 @@ class DatabaseConfig(DatabaseBase):
322
266
 
323
267
  # Update.
324
268
  self.db.execute.insert(
325
- (self.db_names['base'], self.db_names['base.config']),
269
+ self.db_names['config'],
326
270
  data,
327
271
  'update'
328
272
  )
@@ -345,7 +289,7 @@ class DatabaseConfig(DatabaseBase):
345
289
  where = '`key` in :key'
346
290
  limit = None
347
291
  result = self.db.execute.delete(
348
- (self.db_names['base'], self.db_names['base.config']),
292
+ self.db_names['base.config'],
349
293
  where,
350
294
  limit=limit,
351
295
  key=key
@@ -367,7 +311,7 @@ class DatabaseConfig(DatabaseBase):
367
311
 
368
312
  # Get.
369
313
  result = self.db.execute.select(
370
- (self.db_names['base'], self.db_names['base.config']),
314
+ self.db_names['config'],
371
315
  ['key', 'value']
372
316
  )
373
317
 
@@ -393,7 +337,7 @@ class DatabaseConfig(DatabaseBase):
393
337
 
394
338
  # Get.
395
339
  result = self.db.execute.select(
396
- (self.db_names['base'], self.db_names['base.config']),
340
+ self.db_names['config'],
397
341
  '`key`'
398
342
  )
399
343
 
@@ -418,7 +362,7 @@ class DatabaseConfig(DatabaseBase):
418
362
 
419
363
  # Get.
420
364
  result = self.db.execute.select(
421
- (self.db_names['base'], self.db_names['base.config']),
365
+ self.db_names['config'],
422
366
  '`value`'
423
367
  )
424
368
 
@@ -480,7 +424,7 @@ class DatabaseConfig(DatabaseBase):
480
424
  'note': note
481
425
  }
482
426
  self.db.execute.insert(
483
- (self.db_names['base'], self.db_names['base.config']),
427
+ self.db_names['config'],
484
428
  data,
485
429
  'update'
486
430
  )
reydb/rconn.py CHANGED
@@ -9,12 +9,12 @@
9
9
  """
10
10
 
11
11
 
12
- from typing import Self, Generic
12
+ from typing import Self, TypeVar, Generic
13
13
  from sqlalchemy import Connection, Transaction
14
14
  from sqlalchemy.ext.asyncio import AsyncConnection, AsyncTransaction
15
15
 
16
16
  from . import rdb, rexec
17
- from .rbase import DatabaseT, DatabaseExecuteT, ConnectionT, TransactionT, DatabaseBase
17
+ from .rbase import ConnectionT, TransactionT, DatabaseBase
18
18
 
19
19
 
20
20
  __all__ = (
@@ -24,6 +24,10 @@ __all__ = (
24
24
  )
25
25
 
26
26
 
27
+ DatabaseT = TypeVar('DatabaseT', 'rdb.Database', 'rdb.DatabaseAsync')
28
+ DatabaseExecuteT = TypeVar('DatabaseExecuteT', 'rexec.DatabaseExecute', 'rexec.DatabaseExecuteAsync')
29
+
30
+
27
31
  class DatabaseConnectionSuper(DatabaseBase, Generic[DatabaseT, DatabaseExecuteT, ConnectionT, TransactionT]):
28
32
  """
29
33
  Database connection super type.
@@ -53,8 +57,8 @@ class DatabaseConnectionSuper(DatabaseBase, Generic[DatabaseT, DatabaseExecuteT,
53
57
  case rdb.DatabaseAsync():
54
58
  exec = rexec.DatabaseExecuteAsync(self)
55
59
  self.execute: DatabaseExecuteT = exec
56
- self.conn: ConnectionT | None = None
57
- self.begin: TransactionT | None = None
60
+ self.connection: ConnectionT | None = None
61
+ self.transaction: TransactionT | None = None
58
62
 
59
63
 
60
64
  class DatabaseConnection(DatabaseConnectionSuper['rdb.Database', 'rexec.DatabaseExecute', Connection, Transaction]):
@@ -63,6 +67,39 @@ class DatabaseConnection(DatabaseConnectionSuper['rdb.Database', 'rexec.Database
63
67
  """
64
68
 
65
69
 
70
+ def __enter__(self) -> Self:
71
+ """
72
+ Enter syntax `with`.
73
+
74
+ Returns
75
+ -------
76
+ Self.
77
+ """
78
+
79
+ return self
80
+
81
+
82
+ def __exit__(
83
+ self,
84
+ exc_type: type[BaseException] | None,
85
+ *_
86
+ ) -> None:
87
+ """
88
+ Exit syntax `with`.
89
+
90
+ Parameters
91
+ ----------
92
+ exc_type : Exception type.
93
+ """
94
+
95
+ # Commit.
96
+ if exc_type is None:
97
+ self.commit()
98
+
99
+ # Close.
100
+ self.close()
101
+
102
+
66
103
  def get_conn(self) -> Connection:
67
104
  """
68
105
  Get `Connection` instance.
@@ -73,10 +110,10 @@ class DatabaseConnection(DatabaseConnectionSuper['rdb.Database', 'rexec.Database
73
110
  """
74
111
 
75
112
  # Create.
76
- if self.conn is None:
77
- self.conn = self.db.engine.connect()
113
+ if self.connection is None:
114
+ self.connection = self.db.engine.connect()
78
115
 
79
- return self.conn
116
+ return self.connection
80
117
 
81
118
 
82
119
  def get_begin(self) -> Transaction:
@@ -89,11 +126,11 @@ class DatabaseConnection(DatabaseConnectionSuper['rdb.Database', 'rexec.Database
89
126
  """
90
127
 
91
128
  # Create.
92
- if self.begin is None:
129
+ if self.transaction is None:
93
130
  conn = self.get_conn()
94
- self.begin = conn.begin()
131
+ self.transaction = conn.begin()
95
132
 
96
- return self.begin
133
+ return self.transaction
97
134
 
98
135
 
99
136
  def commit(self) -> None:
@@ -102,9 +139,9 @@ class DatabaseConnection(DatabaseConnectionSuper['rdb.Database', 'rexec.Database
102
139
  """
103
140
 
104
141
  # Commit.
105
- if self.begin is not None:
106
- self.begin.commit()
107
- self.begin = None
142
+ if self.transaction is not None:
143
+ self.transaction.commit()
144
+ self.transaction = None
108
145
 
109
146
 
110
147
  def rollback(self) -> None:
@@ -113,9 +150,9 @@ class DatabaseConnection(DatabaseConnectionSuper['rdb.Database', 'rexec.Database
113
150
  """
114
151
 
115
152
  # Rollback.
116
- if self.begin is not None:
117
- self.begin.rollback()
118
- self.begin = None
153
+ if self.transaction is not None:
154
+ self.transaction.rollback()
155
+ self.transaction = None
119
156
 
120
157
 
121
158
  def close(self) -> None:
@@ -124,17 +161,40 @@ class DatabaseConnection(DatabaseConnectionSuper['rdb.Database', 'rexec.Database
124
161
  """
125
162
 
126
163
  # Close.
127
- if self.begin is not None:
128
- self.begin.close()
129
- self.begin = None
130
- if self.conn is not None:
131
- self.conn.close()
132
- self.conn = None
164
+ if self.transaction is not None:
165
+ self.transaction.close()
166
+ self.transaction = None
167
+ if self.connection is not None:
168
+ self.connection.close()
169
+ self.connection = None
133
170
 
134
171
 
135
- def __enter__(self) -> Self:
172
+ def insert_id(self) -> int:
136
173
  """
137
- Enter syntax `with`.
174
+ Return last self increasing ID.
175
+
176
+ Returns
177
+ -------
178
+ ID.
179
+ """
180
+
181
+ # Get.
182
+ sql = 'SELECT LAST_INSERT_ID()'
183
+ result = self.execute(sql)
184
+ insert_id = result.scalar()
185
+
186
+ return insert_id
187
+
188
+
189
+ class DatabaseConnectionAsync(DatabaseConnectionSuper['rdb.DatabaseAsync', 'rexec.DatabaseExecuteAsync', AsyncConnection, AsyncTransaction]):
190
+ """
191
+ Asynchronous database connection type.
192
+ """
193
+
194
+
195
+ async def __aenter__(self):
196
+ """
197
+ Asynchronous enter syntax `async with`.
138
198
 
139
199
  Returns
140
200
  -------
@@ -144,13 +204,13 @@ class DatabaseConnection(DatabaseConnectionSuper['rdb.Database', 'rexec.Database
144
204
  return self
145
205
 
146
206
 
147
- def __exit__(
207
+ async def __aexit__(
148
208
  self,
149
209
  exc_type: type[BaseException] | None,
150
210
  *_
151
211
  ) -> None:
152
212
  """
153
- Exit syntax `with`.
213
+ Asynchronous exit syntax `async with`.
154
214
 
155
215
  Parameters
156
216
  ----------
@@ -159,38 +219,16 @@ class DatabaseConnection(DatabaseConnectionSuper['rdb.Database', 'rexec.Database
159
219
 
160
220
  # Commit.
161
221
  if exc_type is None:
162
- self.commit()
222
+ await self.commit()
163
223
 
164
224
  # 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
- """
225
+ await self.close()
226
+ await self.db.dispose()
189
227
 
190
228
 
191
229
  async def get_conn(self) -> AsyncConnection:
192
230
  """
193
- Asynchronous get `Connection` instance.
231
+ Asynchronous get `AsyncConnection` instance.
194
232
 
195
233
  Returns
196
234
  -------
@@ -198,15 +236,15 @@ class DatabaseConnectionAsync(DatabaseConnectionSuper['rdb.DatabaseAsync', 'rexe
198
236
  """
199
237
 
200
238
  # Create.
201
- if self.conn is None:
202
- self.conn = await self.db.engine.connect()
239
+ if self.connection is None:
240
+ self.connection = await self.db.engine.connect()
203
241
 
204
- return self.conn
242
+ return self.connection
205
243
 
206
244
 
207
245
  async def get_begin(self) -> AsyncTransaction:
208
246
  """
209
- Asynchronous get `Transaction` instance.
247
+ Asynchronous get `AsyncTransaction` instance.
210
248
 
211
249
  Returns
212
250
  -------
@@ -214,11 +252,11 @@ class DatabaseConnectionAsync(DatabaseConnectionSuper['rdb.DatabaseAsync', 'rexe
214
252
  """
215
253
 
216
254
  # Create.
217
- if self.begin is None:
255
+ if self.transaction is None:
218
256
  conn = await self.get_conn()
219
- self.begin = await conn.begin()
257
+ self.transaction = await conn.begin()
220
258
 
221
- return self.begin
259
+ return self.transaction
222
260
 
223
261
 
224
262
  async def commit(self) -> None:
@@ -227,9 +265,9 @@ class DatabaseConnectionAsync(DatabaseConnectionSuper['rdb.DatabaseAsync', 'rexe
227
265
  """
228
266
 
229
267
  # Commit.
230
- if self.begin is not None:
231
- await self.begin.commit()
232
- self.begin = None
268
+ if self.transaction is not None:
269
+ await self.transaction.commit()
270
+ self.transaction = None
233
271
 
234
272
 
235
273
  async def rollback(self) -> None:
@@ -238,9 +276,9 @@ class DatabaseConnectionAsync(DatabaseConnectionSuper['rdb.DatabaseAsync', 'rexe
238
276
  """
239
277
 
240
278
  # Rollback.
241
- if self.begin is not None:
242
- await self.begin.rollback()
243
- self.begin = None
279
+ if self.transaction is not None:
280
+ await self.transaction.rollback()
281
+ self.transaction = None
244
282
 
245
283
 
246
284
  async def close(self) -> None:
@@ -249,46 +287,12 @@ class DatabaseConnectionAsync(DatabaseConnectionSuper['rdb.DatabaseAsync', 'rexe
249
287
  """
250
288
 
251
289
  # Close.
252
- if self.begin is not None:
253
- await self.begin.close()
254
- self.begin = None
255
- if self.conn is not None:
256
- await self.conn.close()
257
- self.conn = None
258
-
259
-
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()
290
+ if self.transaction is not None:
291
+ await self.transaction.close()
292
+ self.transaction = None
293
+ if self.connection is not None:
294
+ await self.connection.close()
295
+ self.connection = None
292
296
 
293
297
 
294
298
  async def insert_id(self) -> int: