reydb 1.1.56__py3-none-any.whl → 1.1.57__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/rdb.py CHANGED
@@ -9,29 +9,28 @@
9
9
  """
10
10
 
11
11
 
12
- from typing import Literal, overload
12
+ from typing import Generic
13
13
  from urllib.parse import quote as urllib_quote
14
14
  from pymysql.constants.CLIENT import MULTI_STATEMENTS
15
- from sqlalchemy import create_engine as sqlalchemy_create_engine
16
- from sqlalchemy.engine.base import Engine
15
+ from sqlalchemy import Engine, create_engine as sqlalchemy_create_engine
17
16
  from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine as sqlalchemy_create_async_engine
18
17
  from reykit.rtext import join_data_text
19
18
 
20
- from .rbase import DatabaseBase, extract_url
19
+ from . import rbase, rbuild, rconfig, rconn, rerror, rexec, rfile, rinfo, rorm, rparam
21
20
 
22
21
 
23
22
  __all__ = (
24
- 'Database',
23
+ 'DatabaseSuper',
24
+ 'rdb.Database',
25
+ 'rdb.DatabaseAsync'
25
26
  )
26
27
 
27
28
 
28
- class Database(DatabaseBase):
29
+ class DatabaseSuper(rbase.DatabaseBase, Generic[rbase.EngineT, rbase.DatabaseConnectionT, rbase.DatabaseExecuteT]):
29
30
  """
30
- Database type, based `MySQL`.
31
+ Database super type, based `MySQL`.
31
32
  """
32
33
 
33
- default_report: bool = False
34
-
35
34
 
36
35
  def __init__(
37
36
  self,
@@ -44,6 +43,7 @@ class Database(DatabaseBase):
44
43
  max_overflow: int = 10,
45
44
  pool_timeout: float = 30.0,
46
45
  pool_recycle: int | None = 3600,
46
+ report: bool = False,
47
47
  **query: str
48
48
  ) -> None:
49
49
  """
@@ -62,6 +62,7 @@ class Database(DatabaseBase):
62
62
  pool_recycle : Number of seconds `recycle` connection.
63
63
  - `None | Literal[-1]`: No recycle.
64
64
  - `int`: Use this value.
65
+ report : Whether report SQL execute information.
65
66
  query : Remote server database parameters.
66
67
  """
67
68
 
@@ -82,53 +83,41 @@ class Database(DatabaseBase):
82
83
  self.pool_recycle = -1
83
84
  else:
84
85
  self.pool_recycle = pool_recycle
86
+ self.report = report
85
87
  self.query = query
86
88
 
87
- # Create engine.
88
- self.engine = self.__create_engine(False)
89
- self.aengine = self.__create_engine(True)
90
-
91
-
92
- @property
93
- def backend(self) -> str:
94
- """
95
- Database backend name.
96
-
97
- Returns
98
- -------
99
- Name.
100
- """
101
-
102
- # Get.
103
- url = self.url(False)
104
- url_params = extract_url(url)
105
- backend = url_params['backend']
89
+ ## Create engine.
90
+ self.engine = self.__create_engine()
106
91
 
107
- return backend
108
92
 
109
-
110
- @property
111
- def driver(self) -> str:
93
+ def __str__(self) -> str:
112
94
  """
113
- Database driver name.
114
-
115
- Returns
116
- -------
117
- Name.
95
+ Return connection information text.
118
96
  """
119
97
 
120
- # Get.
121
- url = self.url(False)
122
- url_params = extract_url(url)
123
- driver = url_params['driver']
98
+ # Generate.
99
+ filter_key = (
100
+ 'engine',
101
+ 'connection',
102
+ 'rdatabase',
103
+ 'begin'
104
+ )
105
+ info = {
106
+ key: value
107
+ for key, value in self.__dict__.items()
108
+ if key not in filter_key
109
+ }
110
+ info['conn_count'] = self.conn_count
111
+ info['aconn_count'] = self.aconn_count
112
+ text = join_data_text(info)
124
113
 
125
- return driver
114
+ return text
126
115
 
127
116
 
128
117
  @property
129
- def abackend(self) -> str:
118
+ def backend(self) -> str:
130
119
  """
131
- Asynchronous database backend name.
120
+ Database backend name.
132
121
 
133
122
  Returns
134
123
  -------
@@ -136,17 +125,16 @@ class Database(DatabaseBase):
136
125
  """
137
126
 
138
127
  # Get.
139
- url = self.url(True)
140
- url_params = extract_url(url)
128
+ url_params = rbase.extract_url(self.url)
141
129
  backend = url_params['backend']
142
130
 
143
131
  return backend
144
132
 
145
133
 
146
134
  @property
147
- def adriver(self) -> str:
135
+ def driver(self) -> str:
148
136
  """
149
- Asynchronous database driver name.
137
+ Database driver name.
150
138
 
151
139
  Returns
152
140
  -------
@@ -154,21 +142,17 @@ class Database(DatabaseBase):
154
142
  """
155
143
 
156
144
  # Get.
157
- url = self.url(True)
158
- url_params = extract_url(url)
145
+ url_params = rbase.extract_url(self.url)
159
146
  driver = url_params['driver']
160
147
 
161
148
  return driver
162
149
 
163
150
 
164
- def url(self, is_async: bool) -> str:
151
+ @property
152
+ def url(self) -> str:
165
153
  """
166
154
  Generate server URL.
167
155
 
168
- Parameters
169
- ----------
170
- is_async : Whether to use asynchronous engine.
171
-
172
156
  Returns
173
157
  -------
174
158
  Server URL.
@@ -176,10 +160,11 @@ class Database(DatabaseBase):
176
160
 
177
161
  # Generate URL.
178
162
  password = urllib_quote(self.password)
179
- if is_async:
180
- url_ = f'mysql+aiomysql://{self.username}:{password}@{self.host}:{self.port}/{self.database}'
181
- else:
182
- url_ = f'mysql+pymysql://{self.username}:{password}@{self.host}:{self.port}/{self.database}'
163
+ match self:
164
+ case Database():
165
+ url_ = f'mysql+pymysql://{self.username}:{password}@{self.host}:{self.port}/{self.database}'
166
+ case DatabaseAsync():
167
+ url_ = f'mysql+aiomysql://{self.username}:{password}@{self.host}:{self.port}/{self.database}'
183
168
 
184
169
  # Add Server parameter.
185
170
  if self.query != {}:
@@ -194,29 +179,18 @@ class Database(DatabaseBase):
194
179
  return url_
195
180
 
196
181
 
197
- @overload
198
- def __create_engine(self, is_async: Literal[False]) -> Engine: ...
199
-
200
- @overload
201
- def __create_engine(self, is_async: Literal[True]) -> AsyncEngine: ...
202
-
203
- def __create_engine(self, is_async: bool) -> Engine | AsyncEngine:
182
+ def __create_engine(self) -> rbase.EngineT:
204
183
  """
205
184
  Create database `Engine` object.
206
185
 
207
- Parameters
208
- ----------
209
- is_async : Whether to use asynchronous engine.
210
-
211
186
  Returns
212
187
  -------
213
188
  Engine object.
214
189
  """
215
190
 
216
191
  # Handle parameter.
217
- url = self.url(is_async)
218
192
  engine_params = {
219
- 'url': url,
193
+ 'url': self.url,
220
194
  'pool_size': self.pool_size,
221
195
  'max_overflow': self.max_overflow,
222
196
  'pool_timeout': self.pool_timeout,
@@ -225,44 +199,27 @@ class Database(DatabaseBase):
225
199
  }
226
200
 
227
201
  # Create Engine.
228
- if is_async:
229
- engine = sqlalchemy_create_async_engine(**engine_params)
230
- else:
231
- engine = sqlalchemy_create_engine(**engine_params)
202
+ match self:
203
+ case Database():
204
+ engine = sqlalchemy_create_engine(**engine_params)
205
+ case DatabaseAsync():
206
+ engine = sqlalchemy_create_async_engine(**engine_params)
232
207
 
233
208
  return engine
234
209
 
235
210
 
236
- async def dispose(self) -> None:
237
- """
238
- Dispose asynchronous connections.
239
- """
240
-
241
- # Dispose.
242
- await self.aengine.dispose()
243
-
244
-
245
- def __conn_count(self, is_async: bool) -> tuple[int, int]:
211
+ @property
212
+ def conn_count(self) -> tuple[int, int]:
246
213
  """
247
214
  Count number of keep open and allowed overflow connection.
248
215
 
249
- Parameters
250
- ----------
251
- is_async : Whether to use asynchronous engine.
252
-
253
216
  Returns
254
217
  -------
255
218
  Number of keep open and allowed overflow connection.
256
219
  """
257
220
 
258
- # Handle parameter.
259
- if is_async:
260
- engine = self.aengine
261
- else:
262
- engine = self.engine
263
-
264
221
  # Count.
265
- _overflow: int = engine.pool._overflow
222
+ _overflow: int = self.engine.pool._overflow
266
223
  if _overflow < 0:
267
224
  keep_n = self.pool_size + _overflow
268
225
  overflow_n = 0
@@ -273,38 +230,6 @@ class Database(DatabaseBase):
273
230
  return keep_n, overflow_n
274
231
 
275
232
 
276
- @property
277
- def conn_count(self) -> tuple[int, int]:
278
- """
279
- Count number of keep open and allowed overflow connection.
280
-
281
- Returns
282
- -------
283
- Number of keep open and allowed overflow connection.
284
- """
285
-
286
- # Count.
287
- keep_n, overflow_n = self.__conn_count(False)
288
-
289
- return keep_n, overflow_n
290
-
291
-
292
- @property
293
- def aconn_count(self) -> tuple[int, int]:
294
- """
295
- Count number of keep open and allowed overflow asynchronous connection.
296
-
297
- Returns
298
- -------
299
- Number of keep open and allowed overflow asynchronous connection.
300
- """
301
-
302
- # Count.
303
- keep_n, overflow_n = self.__conn_count(True)
304
-
305
- return keep_n, overflow_n
306
-
307
-
308
233
  def schema(self, filter_default: bool = True) -> dict[str, dict[str, list[str]]]:
309
234
  """
310
235
  Get schemata of databases and tables and columns.
@@ -375,48 +300,9 @@ class Database(DatabaseBase):
375
300
  return schema_dict
376
301
 
377
302
 
378
- def connect(self, autocommit: bool = False):
379
- """
380
- Build `DatabaseConnection` instance.
381
-
382
- Parameters
383
- ----------
384
- autocommit: Whether automatic commit execute.
385
-
386
- Returns
387
- -------
388
- Database connection instance.
389
- """
390
-
391
- # Import.
392
- from .rconn import DatabaseConnection
393
-
394
- # Build.
395
- conn = DatabaseConnection(self, autocommit)
396
-
397
- return conn
398
-
399
-
400
- @property
401
- def execute(self):
402
- """
403
- Build `DatabaseExecute` instance.
404
-
405
- Returns
406
- -------
407
- Instance.
408
- """
409
-
410
- # Build.
411
- dbconn = self.connect(True)
412
- exec = dbconn.execute
413
-
414
- return exec
415
-
416
-
417
- def aconnect(self, autocommit: bool = False):
303
+ def connect(self, autocommit: bool = False) -> rbase.DatabaseConnectionT:
418
304
  """
419
- Build `DatabaseConnectionAsync` instance.
305
+ Build database connection instance.
420
306
 
421
307
  Parameters
422
308
  ----------
@@ -427,19 +313,20 @@ class Database(DatabaseBase):
427
313
  Database connection instance.
428
314
  """
429
315
 
430
- # Import.
431
- from .rconn import DatabaseConnectionAsync
432
-
433
316
  # Build.
434
- conn = DatabaseConnectionAsync(self, autocommit)
317
+ match self:
318
+ case Database():
319
+ conn = rconn.DatabaseConnection(self, autocommit)
320
+ case DatabaseAsync():
321
+ conn = rconn.DatabaseConnectionAsync(self, autocommit)
435
322
 
436
323
  return conn
437
324
 
438
325
 
439
326
  @property
440
- def aexecute(self):
327
+ def execute(self) -> rbase.DatabaseExecuteT:
441
328
  """
442
- Build `DatabaseConnectionAsync` instance.
329
+ Build database execute instance.
443
330
 
444
331
  Returns
445
332
  -------
@@ -447,8 +334,8 @@ class Database(DatabaseBase):
447
334
  """
448
335
 
449
336
  # Build.
450
- dbconn = self.aconnect(True)
451
- exec = dbconn.aexecute
337
+ conn = self.connect(True)
338
+ exec = conn.execute
452
339
 
453
340
  return exec
454
341
 
@@ -456,18 +343,15 @@ class Database(DatabaseBase):
456
343
  @property
457
344
  def orm(self):
458
345
  """
459
- Build `DatabaseORM` instance.
346
+ Build database ORM instance.
460
347
 
461
348
  Returns
462
349
  -------
463
350
  Instance.
464
351
  """
465
352
 
466
- # Import.
467
- from .rorm import DatabaseORM
468
-
469
353
  # Build.
470
- orm = DatabaseORM(self)
354
+ orm = rorm.DatabaseORM(self)
471
355
 
472
356
  return orm
473
357
 
@@ -475,7 +359,7 @@ class Database(DatabaseBase):
475
359
  @property
476
360
  def info(self):
477
361
  """
478
- Build `DatabaseInformationSchema` instance.
362
+ Build database information schema instance.
479
363
 
480
364
  Returns
481
365
  -------
@@ -502,11 +386,8 @@ class Database(DatabaseBase):
502
386
  >>> database_attr = DatabaseInformationSchema.database.table.column['attribute']
503
387
  """
504
388
 
505
- # Import.
506
- from .rinfo import DatabaseInformationSchema
507
-
508
389
  # Build.
509
- dbischema = DatabaseInformationSchema(self)
390
+ dbischema = rinfo.DatabaseInformationSchema(self)
510
391
 
511
392
  return dbischema
512
393
 
@@ -514,18 +395,15 @@ class Database(DatabaseBase):
514
395
  @property
515
396
  def build(self):
516
397
  """
517
- Build `DatabaseBuild` instance.
398
+ Build database build instance.
518
399
 
519
400
  Returns
520
401
  -------
521
402
  Instance.
522
403
  """
523
404
 
524
- # Import.
525
- from .rbuild import DatabaseBuild
526
-
527
405
  # Build.
528
- dbbuild = DatabaseBuild(self)
406
+ dbbuild = rbuild.DatabaseBuild(self)
529
407
 
530
408
  return dbbuild
531
409
 
@@ -533,18 +411,15 @@ class Database(DatabaseBase):
533
411
  @property
534
412
  def file(self):
535
413
  """
536
- Build `DatabaseFile` instance.
414
+ Build database file instance.
537
415
 
538
416
  Returns
539
417
  -------
540
418
  Instance.
541
419
  """
542
420
 
543
- # Import.
544
- from .rfile import DatabaseFile
545
-
546
421
  # Build.
547
- dbfile = DatabaseFile(self)
422
+ dbfile = rfile.DatabaseFile(self)
548
423
 
549
424
  return dbfile
550
425
 
@@ -552,18 +427,15 @@ class Database(DatabaseBase):
552
427
  @property
553
428
  def error(self):
554
429
  """
555
- Build `DatabaseError` instance.
430
+ Build database error instance.
556
431
 
557
432
  Returns
558
433
  -------
559
434
  Instance.
560
435
  """
561
436
 
562
- # Import.
563
- from .rerror import DatabaseError
564
-
565
437
  # Build.
566
- dbfile = DatabaseError(self)
438
+ dbfile = rerror.DatabaseError(self)
567
439
 
568
440
  return dbfile
569
441
 
@@ -571,18 +443,15 @@ class Database(DatabaseBase):
571
443
  @property
572
444
  def config(self):
573
445
  """
574
- Build `DatabaseConfig` instance.
446
+ Build database config instance.
575
447
 
576
448
  Returns
577
449
  -------
578
450
  Instance.
579
451
  """
580
452
 
581
- # Import.
582
- from .rconfig import DatabaseConfig
583
-
584
453
  # Build.
585
- dbconfig = DatabaseConfig(self)
454
+ dbconfig = rconfig.DatabaseConfig(self)
586
455
 
587
456
  return dbconfig
588
457
 
@@ -590,18 +459,15 @@ class Database(DatabaseBase):
590
459
  @property
591
460
  def status(self):
592
461
  """
593
- Build `DatabaseParametersStatus` instance.
462
+ Build database parameters status instance.
594
463
 
595
464
  Returns
596
465
  -------
597
466
  Instance.
598
467
  """
599
468
 
600
- # Import.
601
- from .rparam import DatabaseParametersStatus
602
-
603
469
  # Build.
604
- dbps = DatabaseParametersStatus(self, False)
470
+ dbps = rparam.DatabaseParametersStatus(self, False)
605
471
 
606
472
  return dbps
607
473
 
@@ -609,18 +475,15 @@ class Database(DatabaseBase):
609
475
  @property
610
476
  def global_status(self):
611
477
  """
612
- Build global `DatabaseParametersStatus` instance.
478
+ Build global database parameters status instance.
613
479
 
614
480
  Returns
615
481
  -------
616
482
  Instance.
617
483
  """
618
484
 
619
- # Import.
620
- from .rparam import DatabaseParametersStatus
621
-
622
485
  # Build.
623
- dbps = DatabaseParametersStatus(self, True)
486
+ dbps = rparam.DatabaseParametersStatus(self, True)
624
487
 
625
488
  return dbps
626
489
 
@@ -628,18 +491,15 @@ class Database(DatabaseBase):
628
491
  @property
629
492
  def variables(self):
630
493
  """
631
- Build `DatabaseParametersVariable` instance.
494
+ Build database parameters variable instance.
632
495
 
633
496
  Returns
634
497
  -------
635
498
  Instance.
636
499
  """
637
500
 
638
- # Import.
639
- from .rparam import DatabaseParametersVariable
640
-
641
501
  # Build.
642
- dbpv = DatabaseParametersVariable(self, False)
502
+ dbpv = rparam.DatabaseParametersVariable(self, False)
643
503
 
644
504
  return dbpv
645
505
 
@@ -647,43 +507,37 @@ class Database(DatabaseBase):
647
507
  @property
648
508
  def global_variables(self):
649
509
  """
650
- Build global `DatabaseParametersVariable` instance.
510
+ Build global database parameters variable instance.
651
511
 
652
512
  Returns
653
513
  -------
654
514
  Instance.
655
515
  """
656
516
 
657
- # Import.
658
- from .rparam import DatabaseParametersVariable
659
-
660
517
  # Build.
661
518
 
662
519
  ## SQLite.
663
- dbpv = DatabaseParametersVariable(self, True)
520
+ dbpv = rparam.DatabaseParametersVariable(self, True)
664
521
 
665
522
  return dbpv
666
523
 
667
524
 
668
- def __str__(self) -> str:
525
+ class Database(DatabaseSuper[Engine, 'rconn.DatabaseConnection', 'rexec.DatabaseExecute']):
526
+ """
527
+ Database type, based `MySQL`.
528
+ """
529
+
530
+
531
+ class DatabaseAsync(DatabaseSuper[AsyncEngine, 'rconn.DatabaseConnectionAsync', 'rexec.DatabaseExecuteAsync']):
532
+ """
533
+ Asynchronous database type, based `MySQL`.
534
+ """
535
+
536
+
537
+ async def dispose(self) -> None:
669
538
  """
670
- Return connection information text.
539
+ Dispose asynchronous connections.
671
540
  """
672
541
 
673
- # Generate.
674
- filter_key = (
675
- 'engine',
676
- 'connection',
677
- 'rdatabase',
678
- 'begin'
679
- )
680
- info = {
681
- key: value
682
- for key, value in self.__dict__.items()
683
- if key not in filter_key
684
- }
685
- info['conn_count'] = self.conn_count
686
- info['aconn_count'] = self.aconn_count
687
- text = join_data_text(info)
688
-
689
- return text
542
+ # Dispose.
543
+ await self.engine.dispose()
reydb/rerror.py CHANGED
@@ -15,8 +15,8 @@ from traceback import StackSummary
15
15
  from functools import wraps as functools_wraps
16
16
  from reykit.rbase import T, Exit, catch_exc
17
17
 
18
+ from . import rdb
18
19
  from .rbase import DatabaseBase
19
- from .rdb import Database
20
20
 
21
21
 
22
22
  __all__ = (
@@ -31,7 +31,7 @@ class DatabaseError(DatabaseBase):
31
31
  """
32
32
 
33
33
 
34
- def __init__(self, db: Database) -> None:
34
+ def __init__(self, db: 'rdb.Database') -> None:
35
35
  """
36
36
  Build instance attributes.
37
37