reydb 1.1.33__py3-none-any.whl → 1.1.35__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/__init__.py CHANGED
@@ -12,11 +12,12 @@ Modules
12
12
  rall : All methods.
13
13
  rbase : Base methods.
14
14
  rbuild : Database build methods.
15
+ rconfig : Database config methods.
15
16
  rconn : Database connection methods.
16
17
  rdb : Database methods.
18
+ rerror : Database error methods.
17
19
  rexec : Database execute methods.
18
20
  rfile : Database file methods.
19
21
  rinfo : Database information methods.
20
- rlog : Database log methods.
21
22
  rparam : Database parameter methods.
22
23
  """
reydb/rall.py CHANGED
@@ -11,10 +11,11 @@
11
11
 
12
12
  from .rbase import *
13
13
  from .rbuild import *
14
+ from .rconfig import *
14
15
  from .rconn import *
15
16
  from .rdb import *
17
+ from .rerror import *
16
18
  from .rexec import *
17
19
  from .rfile import *
18
20
  from .rinfo import *
19
- from .rlog import *
20
21
  from .rparam import *
reydb/rconfig.py ADDED
@@ -0,0 +1,360 @@
1
+ # !/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+ @Time : 2025-08-22 13:45:58
6
+ @Author : Rey
7
+ @Contact : reyxbo@163.com
8
+ @Explain : Database config methods.
9
+ """
10
+
11
+
12
+ from typing import Any
13
+ from reykit.rbase import T, throw
14
+
15
+ from .rconn import DatabaseConnection
16
+ from .rdb import Database
17
+
18
+
19
+ __all__ = (
20
+ 'DatabaseConfig',
21
+ )
22
+
23
+
24
+ class DatabaseConfig(object):
25
+ """
26
+ Database config type.
27
+ Can create database used `self.build` method.
28
+ """
29
+
30
+
31
+ def __init__(self, database: Database | DatabaseConnection) -> None:
32
+ """
33
+ Build instance attributes.
34
+ """
35
+
36
+ # SQLite.
37
+ if database.backend == 'sqlite':
38
+ text='not suitable for SQLite databases'
39
+ throw(AssertionError, text=text)
40
+
41
+ # Build.
42
+ self.database = database
43
+
44
+ ## Database path name.
45
+ self.db_names = {
46
+ 'base': 'base',
47
+ 'base.config': 'config',
48
+ 'base.stats_config': 'stats_config'
49
+ }
50
+
51
+
52
+ def build_db(self) -> None:
53
+ """
54
+ Check and build all standard databases and tables, by `self.db_names`.
55
+ """
56
+
57
+ # Set parameter.
58
+
59
+ ## Database.
60
+ databases = [
61
+ {
62
+ 'name': self.db_names['base']
63
+ }
64
+ ]
65
+
66
+ ## Table.
67
+ tables = [
68
+
69
+ ### 'config'.
70
+ {
71
+ 'path': (self.db_names['base'], self.db_names['base.config']),
72
+ 'fields': [
73
+ {
74
+ 'name': 'create_time',
75
+ 'type': 'datetime',
76
+ 'constraint': 'NOT NULL DEFAULT CURRENT_TIMESTAMP',
77
+ 'comment': 'Config create time.'
78
+ },
79
+ {
80
+ 'name': 'update_time',
81
+ 'type': 'datetime',
82
+ 'constraint': 'DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP',
83
+ 'comment': 'Config update time.'
84
+ },
85
+ {
86
+ 'name': 'key',
87
+ 'type': 'varchar(50)',
88
+ 'constraint': 'NOT NULL',
89
+ 'comment': 'Config key.'
90
+ },
91
+ {
92
+ 'name': 'value',
93
+ 'type': 'json',
94
+ 'constraint': 'NOT NULL',
95
+ 'comment': 'Config value.'
96
+ },
97
+ {
98
+ 'name': 'note',
99
+ 'type': 'varchar(500)',
100
+ 'comment': 'Config note.'
101
+ }
102
+ ]
103
+ }
104
+
105
+ ]
106
+
107
+ ## View stats.
108
+ views_stats = [
109
+
110
+ ### 'stats_config'.
111
+ {
112
+ 'path': (self.db_names['base'], self.db_names['base.stats_config']),
113
+ 'items': [
114
+ {
115
+ 'name': 'count',
116
+ 'select': (
117
+ 'SELECT COUNT(1)\n'
118
+ f'FROM `{self.db_names['base']}`.`{self.db_names['base.config']}`'
119
+ ),
120
+ 'comment': 'Config count.'
121
+ },
122
+ {
123
+ 'name': 'last_create_time',
124
+ 'select': (
125
+ 'SELECT MAX(`create_time`)\n'
126
+ f'FROM `{self.db_names['base']}`.`{self.db_names['base.config']}`'
127
+ ),
128
+ 'comment': 'Config last record create time.'
129
+ },
130
+ {
131
+ 'name': 'last_update_time',
132
+ 'select': (
133
+ 'SELECT MAX(`update_time`)\n'
134
+ f'FROM `{self.db_names['base']}`.`{self.db_names['base.config']}`'
135
+ ),
136
+ 'comment': 'Config last record update time.'
137
+ }
138
+ ]
139
+
140
+ }
141
+
142
+ ]
143
+
144
+ # Build.
145
+ self.database.build.build(databases, tables, views_stats=views_stats)
146
+
147
+
148
+ def get(self, key: str, default: T | None = None) -> Any | T:
149
+ """
150
+ Get config value, when not exist, then return default value.
151
+
152
+ Parameters
153
+ ----------
154
+ key : Config key.
155
+ default : Config default value.
156
+
157
+ Returns
158
+ -------
159
+ Config value.
160
+ """
161
+
162
+ # Get.
163
+ where = '`key` = :key'
164
+ result = self.database.execute_select(
165
+ (self.db_names['base'], self.db_names['base.config']),
166
+ 'value',
167
+ where,
168
+ limit=1,
169
+ key=key
170
+ )
171
+ value = result.scalar()
172
+
173
+ # Default.
174
+ if value is None:
175
+ value = default
176
+
177
+ return value
178
+
179
+
180
+ def setdefault(self, key: str, default: T | None = None) -> Any | T:
181
+ """
182
+ Set config value.
183
+
184
+ Parameters
185
+ ----------
186
+ key : Config key.
187
+ default : Config default value.
188
+
189
+ Returns
190
+ -------
191
+ Config value.
192
+ """
193
+
194
+ # Set.
195
+ data = {'key': key, 'value': default}
196
+ result = self.database.execute_insert(
197
+ (self.db_names['base'], self.db_names['base.config']),
198
+ data,
199
+ 'ignore'
200
+ )
201
+
202
+ # Get.
203
+ if result.rowcount == 0:
204
+ result = self[key]
205
+ else:
206
+ result = default
207
+
208
+ return result
209
+
210
+
211
+ def update(self, data: dict[str, Any]) -> None:
212
+ """
213
+ Update config values.
214
+
215
+ Parameters
216
+ ----------
217
+ data : Config update data.
218
+ """
219
+
220
+ # Update.
221
+ data = [
222
+ {
223
+ 'key': key,
224
+ 'value': value
225
+ }
226
+ for key, value in data.items
227
+ ]
228
+ self.database.execute_insert(
229
+ (self.db_names['base'], self.db_names['base.config']),
230
+ data,
231
+ 'update'
232
+ )
233
+
234
+
235
+ def remove(self, key: str) -> None:
236
+ """
237
+ Remove config.
238
+
239
+ Parameters
240
+ ----------
241
+ key : Config key.
242
+ """
243
+
244
+ # Remove.
245
+ where = '`key` = :key'
246
+ result = self.database.execute_delete(
247
+ (self.db_names['base'], self.db_names['base.config']),
248
+ where,
249
+ limit=1,
250
+ key=key
251
+ )
252
+
253
+ # Check.
254
+ if result.rowcount == 0:
255
+ throw(KeyError, key)
256
+
257
+
258
+ def items(self) -> dict:
259
+ """
260
+ Get all config keys and values.
261
+
262
+ Returns
263
+ -------
264
+ All config keys and values.
265
+ """
266
+
267
+ # Get.
268
+ result = self.database.execute_select(
269
+ (self.db_names['base'], self.db_names['base.config']),
270
+ ['key', 'value']
271
+ )
272
+
273
+ # Convert.
274
+ result = result.to_dict('key', 'value')
275
+
276
+ return result
277
+
278
+
279
+ def keys(self) -> list[str]:
280
+ """
281
+ Get all config keys.
282
+
283
+ Returns
284
+ -------
285
+ All config keys.
286
+ """
287
+
288
+ # Get.
289
+ result = self.database.execute_select(
290
+ (self.db_names['base'], self.db_names['base.config']),
291
+ 'key'
292
+ )
293
+
294
+ # Convert.
295
+ result = result.to_list()
296
+
297
+ return result
298
+
299
+
300
+ def values(self) -> list[Any]:
301
+ """
302
+ Get all config value.
303
+
304
+ Returns
305
+ -------
306
+ All config values.
307
+ """
308
+
309
+ # Get.
310
+ result = self.database.execute_select(
311
+ (self.db_names['base'], self.db_names['base.config']),
312
+ 'value'
313
+ )
314
+
315
+ # Convert.
316
+ result = result.to_list()
317
+
318
+ return result
319
+
320
+
321
+ def __getitem__(self, key: str) -> Any:
322
+ """
323
+ Get config value.
324
+
325
+ Parameters
326
+ ----------
327
+ key : Config key.
328
+
329
+ Returns
330
+ -------
331
+ Config value.
332
+ """
333
+
334
+ # Get.
335
+ value = self.get(key)
336
+
337
+ # Check.
338
+ if value is None:
339
+ throw(KeyError, key)
340
+
341
+ return value
342
+
343
+
344
+ def __setitem__(self, key: str, value: Any) -> None:
345
+ """
346
+ Set config value.
347
+
348
+ Parameters
349
+ ----------
350
+ key : Config key.
351
+ value : Config value.
352
+ """
353
+
354
+ # Set.
355
+ data = {'key': key, 'value': value}
356
+ self.database.execute_insert(
357
+ (self.db_names['base'], self.db_names['base.config']),
358
+ data,
359
+ 'update'
360
+ )
reydb/rdb.py CHANGED
@@ -1932,9 +1932,9 @@ class Database(DatabaseBase):
1932
1932
 
1933
1933
 
1934
1934
  @property
1935
- def log(self):
1935
+ def error(self):
1936
1936
  """
1937
- Build `DatabaseLog` instance.
1937
+ Build `DatabaseError` instance.
1938
1938
 
1939
1939
  Returns
1940
1940
  -------
@@ -1942,14 +1942,33 @@ class Database(DatabaseBase):
1942
1942
  """
1943
1943
 
1944
1944
  # Import.
1945
- from .rlog import DatabaseLog
1945
+ from .rerror import DatabaseError
1946
1946
 
1947
1947
  # Build.
1948
- dbfile = DatabaseLog(self)
1948
+ dbfile = DatabaseError(self)
1949
1949
 
1950
1950
  return dbfile
1951
1951
 
1952
1952
 
1953
+ @property
1954
+ def config(self):
1955
+ """
1956
+ Build `DatabaseConfig` instance.
1957
+
1958
+ Returns
1959
+ -------
1960
+ Database file instance.
1961
+ """
1962
+
1963
+ # Import.
1964
+ from .rconfig import DatabaseConfig
1965
+
1966
+ # Build.
1967
+ dbconfig = DatabaseConfig(self)
1968
+
1969
+ return dbconfig
1970
+
1971
+
1953
1972
  @property
1954
1973
  def status(self):
1955
1974
  """
@@ -5,7 +5,7 @@
5
5
  @Time : 2025-08-20 16:57:19
6
6
  @Author : Rey
7
7
  @Contact : reyxbo@163.com
8
- @Explain : Database log methods.
8
+ @Explain : Database error methods.
9
9
  """
10
10
 
11
11
 
@@ -21,13 +21,13 @@ from .rdb import Database
21
21
 
22
22
 
23
23
  __all__ = (
24
- 'DatabaseLog',
24
+ 'DatabaseError',
25
25
  )
26
26
 
27
27
 
28
- class DatabaseLog(DatabaseBase):
28
+ class DatabaseError(DatabaseBase):
29
29
  """
30
- Database log type.
30
+ Database error type.
31
31
  Can create database used `self.build` method.
32
32
  """
33
33
 
@@ -51,9 +51,9 @@ class DatabaseLog(DatabaseBase):
51
51
 
52
52
  ## Database path name.
53
53
  self.db_names = {
54
- 'log': 'log',
55
- 'log.error': 'error',
56
- 'log.error_stats': 'error_stats'
54
+ 'base': 'base',
55
+ 'base.error': 'error',
56
+ 'base.error_stats': 'error_stats'
57
57
  }
58
58
 
59
59
 
@@ -67,7 +67,7 @@ class DatabaseLog(DatabaseBase):
67
67
  ## Database.
68
68
  databases = [
69
69
  {
70
- 'name': self.db_names['log']
70
+ 'name': self.db_names['base']
71
71
  }
72
72
  ]
73
73
 
@@ -76,7 +76,7 @@ class DatabaseLog(DatabaseBase):
76
76
 
77
77
  ### 'error'.
78
78
  {
79
- 'path': (self.db_names['log'], self.db_names['log.error']),
79
+ 'path': (self.db_names['base'], self.db_names['base.error']),
80
80
  'fields': [
81
81
  {
82
82
  'name': 'create_time',
@@ -131,13 +131,13 @@ class DatabaseLog(DatabaseBase):
131
131
 
132
132
  ### 'error_stats'.
133
133
  {
134
- 'path': (self.db_names['log'], self.db_names['log.error_stats']),
134
+ 'path': (self.db_names['base'], self.db_names['base.error_stats']),
135
135
  'items': [
136
136
  {
137
137
  'name': 'count',
138
138
  'select': (
139
139
  'SELECT COUNT(1)\n'
140
- f'FROM `{self.db_names['log']}`.`{self.db_names['log.error']}`'
140
+ f'FROM `{self.db_names['base']}`.`{self.db_names['base.error']}`'
141
141
  ),
142
142
  'comment': 'Error log count.'
143
143
  },
@@ -145,7 +145,7 @@ class DatabaseLog(DatabaseBase):
145
145
  'name': 'past_day_count',
146
146
  'select': (
147
147
  'SELECT COUNT(1)\n'
148
- f'FROM `{self.db_names['log']}`.`{self.db_names['log.error']}`\n'
148
+ f'FROM `{self.db_names['base']}`.`{self.db_names['base.error']}`\n'
149
149
  'WHERE TIMESTAMPDIFF(DAY, `create_time`, NOW()) = 0'
150
150
  ),
151
151
  'comment': 'Error log count in the past day.'
@@ -154,7 +154,7 @@ class DatabaseLog(DatabaseBase):
154
154
  'name': 'past_week_count',
155
155
  'select': (
156
156
  'SELECT COUNT(1)\n'
157
- f'FROM `{self.db_names['log']}`.`{self.db_names['log.error']}`\n'
157
+ f'FROM `{self.db_names['base']}`.`{self.db_names['base.error']}`\n'
158
158
  'WHERE TIMESTAMPDIFF(DAY, `create_time`, NOW()) <= 6'
159
159
  ),
160
160
  'comment': 'Error log count in the past week.'
@@ -163,7 +163,7 @@ class DatabaseLog(DatabaseBase):
163
163
  'name': 'past_month_count',
164
164
  'select': (
165
165
  'SELECT COUNT(1)\n'
166
- f'FROM `{self.db_names['log']}`.`{self.db_names['log.error']}`\n'
166
+ f'FROM `{self.db_names['base']}`.`{self.db_names['base.error']}`\n'
167
167
  'WHERE TIMESTAMPDIFF(DAY, `create_time`, NOW()) <= 29'
168
168
  ),
169
169
  'comment': 'Error log count in the past month.'
@@ -172,7 +172,7 @@ class DatabaseLog(DatabaseBase):
172
172
  'name': 'last_time',
173
173
  'select': (
174
174
  'SELECT MAX(`create_time`)\n'
175
- f'FROM `{self.db_names['log']}`.`{self.db_names['log.error']}`'
175
+ f'FROM `{self.db_names['base']}`.`{self.db_names['base.error']}`'
176
176
  ),
177
177
  'comment': 'Error log last record create time.'
178
178
  }
@@ -186,7 +186,7 @@ class DatabaseLog(DatabaseBase):
186
186
  self.database.build.build(databases, tables, views_stats=views_stats)
187
187
 
188
188
 
189
- def error(
189
+ def record(
190
190
  self,
191
191
  exc: BaseException,
192
192
  stack: StackSummary,
@@ -223,12 +223,12 @@ class DatabaseLog(DatabaseBase):
223
223
 
224
224
  # Insert.
225
225
  self.database.execute_insert(
226
- (self.db_names['log'], self.db_names['log.error']),
226
+ (self.db_names['base'], self.db_names['base.error']),
227
227
  data=data
228
228
  )
229
229
 
230
230
 
231
- def wrap_error(
231
+ def wrap(
232
232
  self,
233
233
  func: Callable[..., T] | None = None,
234
234
  *,
@@ -317,7 +317,7 @@ class DatabaseLog(DatabaseBase):
317
317
  if isinstance(exc, type_):
318
318
  break
319
319
  else:
320
- self.error(exc, stack, note)
320
+ self.record(exc, stack, note)
321
321
 
322
322
  raise
323
323
 
@@ -335,3 +335,5 @@ class DatabaseLog(DatabaseBase):
335
335
  else:
336
336
  _func = _wrap(func)
337
337
  return _func
338
+
339
+ __call__ = record
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: reydb
3
- Version: 1.1.33
3
+ Version: 1.1.35
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=SqjJEBMiUMnKkNfmOvw_jprZcj9Edi0jyBKt67xeYUE,544
2
+ reydb/rall.py,sha256=UWnbtl4oG4YqXyqTMN_5uqE-QqD5nb_-dvarotlTUeU,388
3
+ reydb/rbase.py,sha256=X8bDBmKyIiLXGvOVnTh5nD0ltkhOPWU6g4BebifOZYY,312
4
+ reydb/rbuild.py,sha256=s4lLVQONfL5a9B6mw5D7bfbPyJoYNIqBxlas9rCM1Y8,32458
5
+ reydb/rconfig.py,sha256=d8xvpkoLbyVDVUNGPb0_TlrqxMdJsbjEXluXj7xWbN8,8801
6
+ reydb/rconn.py,sha256=kdw2xQb0JqSUXM-BD7XfJ-ZqaWHxYkeT8psRhOK43mA,6211
7
+ reydb/rdb.py,sha256=OQ3ztC4G53usAbocqGwTqo1ry1AIGjoWTN7pn8Pn3Uk,62184
8
+ reydb/rerror.py,sha256=xHLvRxu8KBvUdfVS7mD906jx9XTQPGwEl7psBLGEZ5w,9966
9
+ reydb/rexec.py,sha256=dGdRkG1XR0Z66T0r4nPCSdQzSRWc_Q3t6TPSSrDTIxY,9042
10
+ reydb/rfile.py,sha256=7g6hPBz33p-mkGFc6LEmL2hpFes-LM-AWQ0SxgJe2BI,15254
11
+ reydb/rinfo.py,sha256=KXTkcpTGAD3p9RVKKcnmc_FjJtiKRPk-K5ZepPOnphQ,15253
12
+ reydb/rparam.py,sha256=3BGDBD8QshOf2J70ZJ6LJ9PiH-1ZU3ruZwoE0bN6OOw,7017
13
+ reydb-1.1.35.dist-info/METADATA,sha256=gXTzXVZRTCIRkoKawXAeaEpsF9f9QUaUgQ6Q3mUpQUs,1550
14
+ reydb-1.1.35.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
15
+ reydb-1.1.35.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
16
+ reydb-1.1.35.dist-info/RECORD,,
@@ -1,15 +0,0 @@
1
- reydb/__init__.py,sha256=U7zk2fbM2FbAWHdLDgXPX2wVc-NJKNeBGht09vVS5L4,504
2
- reydb/rall.py,sha256=iSjkt2aqJtKVio8jAghc7-V-7rWdjYu-2rR2vPUpaDE,362
3
- reydb/rbase.py,sha256=X8bDBmKyIiLXGvOVnTh5nD0ltkhOPWU6g4BebifOZYY,312
4
- reydb/rbuild.py,sha256=s4lLVQONfL5a9B6mw5D7bfbPyJoYNIqBxlas9rCM1Y8,32458
5
- reydb/rconn.py,sha256=kdw2xQb0JqSUXM-BD7XfJ-ZqaWHxYkeT8psRhOK43mA,6211
6
- reydb/rdb.py,sha256=hjWwB778jxBoTgx2lgs6n1NZMJA0Hbg4HHdXE57hZiY,61841
7
- reydb/rexec.py,sha256=dGdRkG1XR0Z66T0r4nPCSdQzSRWc_Q3t6TPSSrDTIxY,9042
8
- reydb/rfile.py,sha256=7g6hPBz33p-mkGFc6LEmL2hpFes-LM-AWQ0SxgJe2BI,15254
9
- reydb/rinfo.py,sha256=KXTkcpTGAD3p9RVKKcnmc_FjJtiKRPk-K5ZepPOnphQ,15253
10
- reydb/rlog.py,sha256=EpKPIjI_cAmXzFUs3D8ZC-JUylAPodm9LfXqRiLGEG0,9916
11
- reydb/rparam.py,sha256=3BGDBD8QshOf2J70ZJ6LJ9PiH-1ZU3ruZwoE0bN6OOw,7017
12
- reydb-1.1.33.dist-info/METADATA,sha256=AW2-MON8LlkmeGeY5zCbAOLtGtMhHYDjcca_Ak5msls,1550
13
- reydb-1.1.33.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
14
- reydb-1.1.33.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
15
- reydb-1.1.33.dist-info/RECORD,,
File without changes