reydb 1.1.28__py3-none-any.whl → 1.1.29__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/rlog.py ADDED
@@ -0,0 +1,303 @@
1
+ # !/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+ @Time : 2025-08-20 16:57:19
6
+ @Author : Rey
7
+ @Contact : reyxbo@163.com
8
+ @Explain : Database log methods.
9
+ """
10
+
11
+
12
+ from typing import Any
13
+ from collections.abc import Callable
14
+ from traceback import StackSummary
15
+ from functools import wraps as functools_wraps
16
+ from reykit.rbase import T, throw, catch_exc
17
+
18
+ from .rbase import DatabaseBase
19
+ from .rconn import DatabaseConnection
20
+ from .rdb import Database
21
+
22
+
23
+ __all__ = (
24
+ 'DatabaseLog',
25
+ )
26
+
27
+
28
+ class DatabaseLog(DatabaseBase):
29
+ """
30
+ Database log type.
31
+ Can create database used `self.build` method.
32
+ """
33
+
34
+
35
+ def __init__(self, database: Database | DatabaseConnection) -> None:
36
+ """
37
+ Build instance attributes.
38
+
39
+ Parameters
40
+ ----------
41
+ database : Database or DatabaseConnection instance.
42
+ """
43
+
44
+ # SQLite.
45
+ if database.backend == 'sqlite':
46
+ text='not suitable for SQLite databases'
47
+ throw(AssertionError, text=text)
48
+
49
+ # Build.
50
+ self.database = database
51
+
52
+ ## Database path name.
53
+ self.db_names = {
54
+ 'log': 'log',
55
+ 'log.error': 'error',
56
+ 'log.error_stats': 'error_stats'
57
+ }
58
+
59
+
60
+ def build_db(self) -> None:
61
+ """
62
+ Check and build all standard databases and tables, by `self.db_names`.
63
+ """
64
+
65
+ # Set parameter.
66
+
67
+ ## Database.
68
+ databases = [
69
+ {
70
+ 'name': self.db_names['log']
71
+ }
72
+ ]
73
+
74
+ ## Table.
75
+ tables = [
76
+
77
+ ### 'error'.
78
+ {
79
+ 'path': (self.db_names['log'], self.db_names['log.error']),
80
+ 'fields': [
81
+ {
82
+ 'name': 'create_time',
83
+ 'type': 'datetime',
84
+ 'constraint': 'NOT NULL DEFAULT CURRENT_TIMESTAMP',
85
+ 'comment': 'Record create time.'
86
+ },
87
+ {
88
+ 'name': 'id',
89
+ 'type': 'int unsigned',
90
+ 'constraint': 'NOT NULL AUTO_INCREMENT',
91
+ 'comment': 'ID.'
92
+ },
93
+ {
94
+ 'name': 'type',
95
+ 'type': 'varchar(50)',
96
+ 'constraint': 'NOT NULL',
97
+ 'comment': 'Error type.'
98
+ },
99
+ {
100
+ 'name': 'data',
101
+ 'type': 'json',
102
+ 'comment': 'Error data.'
103
+ },
104
+ {
105
+ 'name': 'stack',
106
+ 'type': 'json',
107
+ 'comment': 'Error code traceback stack.'
108
+ },
109
+ {
110
+ 'name': 'note',
111
+ 'type': 'varchar(500)',
112
+ 'comment': 'Error note.'
113
+ }
114
+ ],
115
+ 'primary': 'id',
116
+ 'indexes': [
117
+ {
118
+ 'name': 'n_type',
119
+ 'fields': 'type',
120
+ 'type': 'noraml',
121
+ 'comment': 'Error type normal index.'
122
+ }
123
+ ],
124
+ 'comment': 'Error log table.'
125
+ },
126
+
127
+ ]
128
+
129
+ ## View stats.
130
+ views_stats = [
131
+
132
+ ### 'error_stats'.
133
+ {
134
+ 'path': (self.db_names['log'], self.db_names['log.error_stats']),
135
+ 'items': [
136
+ {
137
+ 'name': 'count',
138
+ 'select': (
139
+ 'SELECT COUNT(1)\n'
140
+ f'FROM `{self.db_names['log']}`.`{self.db_names['log.error']}`'
141
+ ),
142
+ 'comment': 'Error log count.'
143
+ },
144
+ {
145
+ 'name': 'past_day_count',
146
+ 'select': (
147
+ 'SELECT COUNT(1)\n'
148
+ f'FROM `{self.db_names['log']}`.`{self.db_names['log.error']}`\n'
149
+ 'WHERE TIMESTAMPDIFF(DAY, `create_time`, NOW()) = 0'
150
+ ),
151
+ 'comment': 'Error log count in the past day.'
152
+ },
153
+ {
154
+ 'name': 'past_week_count',
155
+ 'select': (
156
+ 'SELECT COUNT(1)\n'
157
+ f'FROM `{self.db_names['log']}`.`{self.db_names['log.error']}`\n'
158
+ 'WHERE TIMESTAMPDIFF(DAY, `create_time`, NOW()) <= 6'
159
+ ),
160
+ 'comment': 'Error log count in the past week.'
161
+ },
162
+ {
163
+ 'name': 'past_month_count',
164
+ 'select': (
165
+ 'SELECT COUNT(1)\n'
166
+ f'FROM `{self.db_names['log']}`.`{self.db_names['log.error']}`\n'
167
+ 'WHERE TIMESTAMPDIFF(DAY, `create_time`, NOW()) <= 29'
168
+ ),
169
+ 'comment': 'Error log count in the past month.'
170
+ },
171
+ {
172
+ 'name': 'last_time',
173
+ 'select': (
174
+ 'SELECT MAX(`create_time`)\n'
175
+ f'FROM `{self.db_names['log']}`.`{self.db_names['log.error']}`'
176
+ ),
177
+ 'comment': 'Error log last record create time.'
178
+ }
179
+ ]
180
+
181
+ }
182
+
183
+ ]
184
+
185
+ # Build.
186
+ self.database.build.build(databases, tables, views_stats=views_stats)
187
+
188
+
189
+ def error(
190
+ self,
191
+ exc: BaseException,
192
+ stack: StackSummary,
193
+ note: str | None = None
194
+ ) -> None:
195
+ """
196
+ Insert exception information into the table of database.
197
+
198
+ Parameters
199
+ ----------
200
+ exc : Exception instance.
201
+ stack : Exception traceback stack instance.
202
+ note : Exception note.
203
+ """
204
+
205
+ # Handle parameter.
206
+ exc_type = type(exc).__name__
207
+ exc_data = list(exc.args) or None
208
+ exc_stack = [
209
+ {
210
+ 'file': frame.filename,
211
+ 'line': frame.lineno,
212
+ 'frame': frame.name,
213
+ 'code': frame.line
214
+ }
215
+ for frame in stack
216
+ ]
217
+ data = {
218
+ 'type': exc_type,
219
+ 'data': exc_data,
220
+ 'stack': exc_stack,
221
+ 'note': note
222
+ }
223
+
224
+ # Insert.
225
+ self.database.execute_insert(
226
+ (self.db_names['log'], self.db_names['log.error']),
227
+ data=data
228
+ )
229
+
230
+
231
+ def wrap_error(
232
+ self,
233
+ func: Callable[..., T] | None = None,
234
+ *,
235
+ note: str | None = None
236
+ ) -> T | Callable[[Callable[..., T]], Callable[..., T]]:
237
+ """
238
+ Decorator, insert exception information into the table of database.
239
+
240
+ Parameters
241
+ ----------
242
+ func : Function.
243
+ note : Exception note.
244
+
245
+ Returns
246
+ -------
247
+ Decorated function or decorator with parameter.
248
+ """
249
+
250
+
251
+ def _wrap(func_: Callable[..., T]) -> Callable[..., T]:
252
+ """
253
+ Decorator, insert exception information into the table of database.
254
+
255
+ Parameters
256
+ ----------
257
+ _func : Function.
258
+
259
+ Returns
260
+ -------
261
+ Decorated function.
262
+ """
263
+
264
+
265
+ @functools_wraps(func_)
266
+ def _func(*args, **kwargs) -> Any:
267
+ """
268
+ Decorated function.
269
+
270
+ Parameters
271
+ ----------
272
+ args : Position arguments of function.
273
+ kwargs : Keyword arguments of function.
274
+
275
+ Returns
276
+ -------
277
+ Function return.
278
+ """
279
+
280
+ # Try execute.
281
+ try:
282
+ result = func_(*args, **kwargs)
283
+
284
+ # Log.
285
+ except BaseException:
286
+ _, exc, stack = catch_exc()
287
+ self.error(exc, stack, note)
288
+ raise
289
+
290
+ return result
291
+
292
+
293
+ return _func
294
+
295
+
296
+ # Decorator.
297
+ if func is None:
298
+ return _wrap
299
+
300
+ # Decorated function.
301
+ else:
302
+ _func = _wrap(func)
303
+ return _func
reydb/rparam.py CHANGED
@@ -11,19 +11,20 @@
11
11
 
12
12
  from typing import overload
13
13
 
14
- from .rbase import BaseDatabase
15
- from .rconn import DBConnection
14
+ from .rbase import DatabaseBase
15
+ from .rconn import DatabaseConnection
16
16
  from .rdb import Database
17
17
 
18
18
 
19
19
  __all__ = (
20
- 'DBParameter',
21
- 'DBPStatus',
22
- 'DBPVariable'
20
+ 'DatabaseParameter',
21
+ 'DatabaseParameterStatus',
22
+ 'DatabaseParameterVariable',
23
+ 'DatabaseParameterPragma'
23
24
  )
24
25
 
25
26
 
26
- class DBParameter(BaseDatabase):
27
+ class DatabaseParameter(DatabaseBase):
27
28
  """
28
29
  Database parameters type.
29
30
  """
@@ -31,7 +32,7 @@ class DBParameter(BaseDatabase):
31
32
 
32
33
  def __init__(
33
34
  self,
34
- rdatabase: Database | DBConnection,
35
+ rdatabase: Database | DatabaseConnection,
35
36
  global_: bool
36
37
  ) -> None:
37
38
  """
@@ -39,7 +40,7 @@ class DBParameter(BaseDatabase):
39
40
 
40
41
  Parameters
41
42
  ----------
42
- rdatabase : Database or DBConnection instance.
43
+ rdatabase : Database or DatabaseConnection instance.
43
44
  global\\_ : Whether base global.
44
45
  """
45
46
 
@@ -84,7 +85,7 @@ class DBParameter(BaseDatabase):
84
85
  self.update(params)
85
86
 
86
87
 
87
- class DBPStatus(DBParameter):
88
+ class DatabaseParameterStatus(DatabaseParameter):
88
89
  """
89
90
  Database parameter status type.
90
91
  """
@@ -154,7 +155,7 @@ class DBPStatus(DBParameter):
154
155
  raise AssertionError('database status not update')
155
156
 
156
157
 
157
- class DBPVariable(DBParameter):
158
+ class DatabaseParameterVariable(DatabaseParameter):
158
159
  """
159
160
  Database parameter variable type.
160
161
  """
@@ -246,7 +247,7 @@ class DBPVariable(DBParameter):
246
247
  self.rdatabase.execute(sql)
247
248
 
248
249
 
249
- class DBPPragma(DBParameter):
250
+ class DatabaseParameterPragma(DatabaseParameter):
250
251
  """
251
252
  Database parameter pragma type.
252
253
  """
@@ -254,14 +255,14 @@ class DBPPragma(DBParameter):
254
255
 
255
256
  def __init__(
256
257
  self,
257
- rdatabase: Database | DBConnection
258
+ rdatabase: Database | DatabaseConnection
258
259
  ) -> None:
259
260
  """
260
261
  Build instance attributes.
261
262
 
262
263
  Parameters
263
264
  ----------
264
- rdatabase : Database or DBConnection instance.
265
+ rdatabase : Database or DatabaseConnection instance.
265
266
  """
266
267
 
267
268
  # Set parameter.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: reydb
3
- Version: 1.1.28
3
+ Version: 1.1.29
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,15 @@
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=P1Y0Ci_RJz4vZrAf9f2ojWFRSV34Wf8ixoWXOc8-3dU,8862
11
+ reydb/rparam.py,sha256=3BGDBD8QshOf2J70ZJ6LJ9PiH-1ZU3ruZwoE0bN6OOw,7017
12
+ reydb-1.1.29.dist-info/METADATA,sha256=c0VyVKFNb7Xj6zoTx927oHRz48p5g-I9XeQoyzlV--4,1550
13
+ reydb-1.1.29.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
14
+ reydb-1.1.29.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
15
+ reydb-1.1.29.dist-info/RECORD,,
@@ -1,14 +0,0 @@
1
- reydb/__init__.py,sha256=UetBDQylwFTWiPMKF1ZPW45A6mwYWWSYneTesl4xKBY,474
2
- reydb/rall.py,sha256=i6-ph2cahhTTF17DjUYf6KX3DNxhEvy6iedlSeF4ibI,341
3
- reydb/rbase.py,sha256=HMhxr7_TyzAusrv5dcc1hf3PmxGWj7m63kKcr5Ikbf4,312
4
- reydb/rbuild.py,sha256=LiYGOMZFnZi-aVGfWpKT2aEHks4ygbEubweB0-fczfQ,32344
5
- reydb/rconn.py,sha256=XmT4IizNYxCNi1zl88HCgHwkLcWMRWWBcDLwuvjV6aE,6542
6
- reydb/rdb.py,sha256=fAWwiFgHinOCHnalzh3IdFTrmFhnvjThm_WPoYG45dw,60878
7
- reydb/rexec.py,sha256=ezYqSxW6PvvILugnlWtq-BoxfBHAec9f2P5mgIp_heA,8910
8
- reydb/rfile.py,sha256=zZ9fbnwi4BpJbreRpOujwvE9J_rR_JWkH_5vbWPvKF4,13816
9
- reydb/rinfo.py,sha256=p8_nlOHWqnJCD0NRgbsxqnM2VKTDNO9jj4vwgd1HvG0,14267
10
- reydb/rparam.py,sha256=d8Ijn86ieYlWsHjAwIjrVujynfgKtMF1E-q41wGtd9Y,6855
11
- reydb-1.1.28.dist-info/METADATA,sha256=6wDADfjrQBPNtwAxMIIVOQG9_bi3PVe3g4PmDdc_MJM,1550
12
- reydb-1.1.28.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
13
- reydb-1.1.28.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
14
- reydb-1.1.28.dist-info/RECORD,,
File without changes