reydb 1.1.60__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/rparam.py CHANGED
@@ -9,7 +9,7 @@
9
9
  """
10
10
 
11
11
 
12
- from typing import TypeVar, Generic, overload
12
+ from typing import Literal, TypeVar, Generic, Final, overload
13
13
 
14
14
  from . import rdb
15
15
  from .rbase import DatabaseBase
@@ -17,10 +17,20 @@ from .rexec import Result
17
17
 
18
18
 
19
19
  __all__ = (
20
+ 'DatabaseSchemaSuper',
20
21
  'DatabaseSchema',
22
+ 'DatabaseSchemaAsync',
23
+ 'DatabaseParametersSuper',
21
24
  'DatabaseParameters',
25
+ 'DatabaseParametersAsync',
26
+ 'DatabaseParametersVariables',
22
27
  'DatabaseParametersStatus',
23
- 'DatabaseParametersVariable'
28
+ 'DatabaseParametersVariablesGlobal',
29
+ 'DatabaseParametersStatusGlobal',
30
+ 'DatabaseParametersVariablesAsync',
31
+ 'DatabaseParametersStatusAsync',
32
+ 'DatabaseParametersVariablesGlobalAsync',
33
+ 'DatabaseParametersStatusGlobalAsync'
24
34
  )
25
35
 
26
36
 
@@ -45,10 +55,9 @@ class DatabaseSchemaSuper(DatabaseBase, Generic[DatabaseT]):
45
55
  # Set parameter.
46
56
  self.db = db
47
57
 
48
-
49
- def _call__before(self, filter_default: bool = True) -> tuple[str, tuple[str, ...]]:
58
+ def handle_before__call__(self, filter_default: bool = True) -> tuple[str, tuple[str, ...]]:
50
59
  """
51
- Before handle of call method.
60
+ Before handle method of call method.
52
61
 
53
62
  Parameters
54
63
  ----------
@@ -88,9 +97,9 @@ class DatabaseSchemaSuper(DatabaseBase, Generic[DatabaseT]):
88
97
  return sql, filter_db
89
98
 
90
99
 
91
- def _call__after(self, result: Result) -> dict[str, dict[str, list[str]]]:
100
+ def handle_after__call__(self, result: Result) -> dict[str, dict[str, list[str]]]:
92
101
  """
93
- After handle of call method.
102
+ After handle method of call method.
94
103
 
95
104
  Parameters
96
105
  ----------
@@ -131,13 +140,80 @@ class DatabaseSchemaSuper(DatabaseBase, Generic[DatabaseT]):
131
140
  return schema_dict
132
141
 
133
142
 
143
+ @overload
144
+ def handle_exist(
145
+ self,
146
+ schema: dict[str, dict[str, list[str]]],
147
+ database: str
148
+ ) -> bool: ...
149
+
150
+ @overload
151
+ def handle_exist(
152
+ self,
153
+ schema: dict[str, dict[str, list[str]]],
154
+ database: str,
155
+ table: str
156
+ ) -> bool: ...
157
+
158
+ @overload
159
+ def handle_exist(
160
+ self,
161
+ schema: dict[str, dict[str, list[str]]],
162
+ database: str,
163
+ table: str,
164
+ column: str
165
+ ) -> bool: ...
166
+
167
+ def handle_exist(
168
+ self,
169
+ schema: dict[str, dict[str, list[str]]],
170
+ database: str,
171
+ table: str | None = None,
172
+ column: str | None = None
173
+ ) -> bool:
174
+ """
175
+ Handle method of judge database or table or column whether it exists.
176
+
177
+ Parameters
178
+ ----------
179
+ schema : Schemata of databases and tables and columns.
180
+ database : Database name.
181
+ table : Table name.
182
+ column : Column name.
183
+
184
+ Returns
185
+ -------
186
+ Judge result.
187
+ """
188
+
189
+ # Handle parameter.
190
+
191
+ # Judge.
192
+ judge = (
193
+ database in schema
194
+ and (
195
+ table is None
196
+ or (
197
+ (database_info := schema.get(database)) is not None
198
+ and (table_info := database_info.get(table)) is not None
199
+ )
200
+ )
201
+ and (
202
+ column is None
203
+ or column in table_info
204
+ )
205
+ )
206
+
207
+ return judge
208
+
209
+
134
210
  class DatabaseSchema(DatabaseSchemaSuper['rdb.Database']):
135
211
  """
136
212
  Database schema type.
137
213
  """
138
214
 
139
215
 
140
- def __call__(self, filter_default: bool = True) -> dict[str, dict[str, list[str]]]:
216
+ def schema(self, filter_default: bool = True) -> dict[str, dict[str, list[str]]]:
141
217
  """
142
218
  Get schemata of databases and tables and columns.
143
219
 
@@ -151,11 +227,83 @@ class DatabaseSchema(DatabaseSchemaSuper['rdb.Database']):
151
227
  """
152
228
 
153
229
  # Get.
154
- sql, filter_db = self._call__before(filter_default)
230
+ sql, filter_db = self.handle_before__call__(filter_default)
155
231
  result = self.db.execute(sql, filter_db=filter_db)
156
- schema_dict = self._call__after(result)
232
+ schema = self.handle_after__call__(result)
157
233
 
158
- return schema_dict
234
+ # Cache.
235
+ if self.db._schema is None:
236
+ self.db._schema = schema
237
+ else:
238
+ self.db._schema.update(schema)
239
+
240
+ return schema
241
+
242
+
243
+ __call__ = schema
244
+
245
+
246
+ @overload
247
+ def exist(
248
+ self,
249
+ database: str,
250
+ *,
251
+ refresh: bool = True
252
+ ) -> bool: ...
253
+
254
+ @overload
255
+ def exist(
256
+ self,
257
+ database: str,
258
+ *,
259
+ table: str,
260
+ refresh: bool = True
261
+ ) -> bool: ...
262
+
263
+ @overload
264
+ def exist(
265
+ self,
266
+ database: str,
267
+ table: str,
268
+ column: str,
269
+ refresh: bool = True
270
+ ) -> bool: ...
271
+
272
+ def exist(
273
+ self,
274
+ database: str,
275
+ table: str | None = None,
276
+ column: str | None = None,
277
+ refresh: bool = True
278
+ ) -> bool:
279
+ """
280
+ Judge database or table or column whether it exists.
281
+
282
+ Parameters
283
+ ----------
284
+ database : Database name.
285
+ table : Table name.
286
+ column : Column name.
287
+ refresh : Whether refresh cache data. Cache can improve efficiency.
288
+
289
+ Returns
290
+ -------
291
+ Judge result.
292
+ """
293
+
294
+ # Handle parameter.
295
+ if (
296
+ refresh
297
+ or self.db._schema is None
298
+ ):
299
+ schema = self.schema()
300
+ else:
301
+ schema = self.db._schema
302
+
303
+ # Judge.
304
+ result = self.handle_exist(schema, database, table, column)
305
+
306
+ return result
159
307
 
160
308
 
161
309
  class DatabaseSchemaAsync(DatabaseSchemaSuper['rdb.DatabaseAsync']):
@@ -164,7 +312,7 @@ class DatabaseSchemaAsync(DatabaseSchemaSuper['rdb.DatabaseAsync']):
164
312
  """
165
313
 
166
314
 
167
- async def __call__(self, filter_default: bool = True) -> dict[str, dict[str, list[str]]]:
315
+ async def schema(self, filter_default: bool = True) -> dict[str, dict[str, list[str]]]:
168
316
  """
169
317
  Asynchronous get schemata of databases and tables and columns.
170
318
 
@@ -178,23 +326,95 @@ class DatabaseSchemaAsync(DatabaseSchemaSuper['rdb.DatabaseAsync']):
178
326
  """
179
327
 
180
328
  # Get.
181
- sql, filter_db = self._call__before(filter_default)
329
+ sql, filter_db = self.handle_before__call__(filter_default)
182
330
  result = await self.db.execute(sql, filter_db=filter_db)
183
- schema_dict = self._call__after(result)
331
+ schema = self.handle_after__call__(result)
184
332
 
185
- return schema_dict
333
+ # Cache.
334
+ if self.db._schema is not None:
335
+ self.db._schema.update(schema)
336
+
337
+ return schema
338
+
339
+
340
+ __call__ = schema
341
+
342
+
343
+ @overload
344
+ async def exist(
345
+ self,
346
+ database: str,
347
+ *,
348
+ refresh: bool = True
349
+ ) -> bool: ...
350
+
351
+ @overload
352
+ async def exist(
353
+ self,
354
+ database: str,
355
+ *,
356
+ table: str,
357
+ refresh: bool = True
358
+ ) -> bool: ...
359
+
360
+ @overload
361
+ async def exist(
362
+ self,
363
+ database: str,
364
+ table: str,
365
+ column: str,
366
+ refresh: bool = True
367
+ ) -> bool: ...
368
+
369
+ async def exist(
370
+ self,
371
+ database: str,
372
+ table: str | None = None,
373
+ column: str | None = None,
374
+ refresh: bool = True
375
+ ) -> bool:
376
+ """
377
+ Asynchronous judge database or table or column whether it exists.
378
+
379
+ Parameters
380
+ ----------
381
+ database : Database name.
382
+ table : Table name.
383
+ column : Column name.
384
+ refresh : Whether refresh cache data. Cache can improve efficiency.
186
385
 
386
+ Returns
387
+ -------
388
+ Judge result.
389
+ """
390
+
391
+ # Handle parameter.
392
+ if (
393
+ refresh
394
+ or self.db._schema is None
395
+ ):
396
+ schema = await self.schema()
397
+ else:
398
+ schema = self.db._schema
399
+
400
+ # Judge.
401
+ result = self.handle_exist(schema, database, table, column)
187
402
 
188
- class DatabaseParameters(DatabaseBase):
403
+ return result
404
+
405
+
406
+ class DatabaseParametersSuper(DatabaseBase, Generic[DatabaseT]):
189
407
  """
190
- Database parameters type.
408
+ Database parameters super type.
191
409
  """
192
410
 
411
+ mode: Literal['VARIABLES', 'STATUS']
412
+ glob: bool
413
+
193
414
 
194
415
  def __init__(
195
416
  self,
196
- db: 'rdb.Database',
197
- global_: bool
417
+ db: DatabaseT
198
418
  ) -> None:
199
419
  """
200
420
  Build instance attributes.
@@ -202,12 +422,16 @@ class DatabaseParameters(DatabaseBase):
202
422
  Parameters
203
423
  ----------
204
424
  db: Database instance.
205
- global\\_ : Whether base global.
206
425
  """
207
426
 
208
427
  # Set parameter.
209
428
  self.db = db
210
- self.global_ = global_
429
+
430
+
431
+ class DatabaseParameters(DatabaseParametersSuper['rdb.Database']):
432
+ """
433
+ Database parameters type.
434
+ """
211
435
 
212
436
 
213
437
  def __getitem__(self, key: str) -> str | None:
@@ -246,12 +470,6 @@ class DatabaseParameters(DatabaseBase):
246
470
  self.update(params)
247
471
 
248
472
 
249
- class DatabaseParametersStatus(DatabaseParameters):
250
- """
251
- Database parameters status type.
252
- """
253
-
254
-
255
473
  @overload
256
474
  def get(self) -> dict[str, str]: ...
257
475
 
@@ -274,14 +492,11 @@ class DatabaseParametersStatus(DatabaseParameters):
274
492
  """
275
493
 
276
494
  # Generate SQL.
277
-
278
- ## Global.
279
- if self.global_:
280
- sql = 'SHOW GLOBAL STATUS'
281
-
282
- ## Not global.
283
- else:
284
- sql = 'SHOW STATUS'
495
+ sql = 'SHOW ' + (
496
+ 'GLOBAL '
497
+ if self.glob
498
+ else ''
499
+ ) + self.mode
285
500
 
286
501
  # Execute SQL.
287
502
 
@@ -312,25 +527,80 @@ class DatabaseParametersStatus(DatabaseParameters):
312
527
  params : Update parameter key value pairs.
313
528
  """
314
529
 
315
- # Throw exception.
316
- raise AssertionError('database status not update')
530
+ # Generate SQL.
531
+ sql_set_list = [
532
+ '%s = %s' % (
533
+ key,
534
+ (
535
+ value
536
+ if type(value) in (int, float)
537
+ else "'%s'" % value
538
+ )
539
+ )
540
+ for key, value in params.items()
541
+ ]
542
+ sql_set = ',\n '.join(sql_set_list)
543
+ sql = 'SHOW ' + (
544
+ 'GLOBAL '
545
+ if self.glob
546
+ else ''
547
+ ) + sql_set
548
+
549
+ # Execute SQL.
550
+ self.db.execute(sql)
317
551
 
318
552
 
319
- class DatabaseParametersVariable(DatabaseParameters):
553
+ class DatabaseParametersAsync(DatabaseParametersSuper['rdb.DatabaseAsync']):
320
554
  """
321
- Database parameters variable type.
555
+ Asynchrouous database parameters type.
322
556
  """
323
557
 
324
558
 
559
+ async def __getitem__(self, key: str) -> str | None:
560
+ """
561
+ Asynchrouous get item of parameter dictionary.
562
+
563
+ Parameters
564
+ ----------
565
+ key : Parameter key.
566
+
567
+ Returns
568
+ -------
569
+ Parameter value.
570
+ """
571
+
572
+ # Get.
573
+ value = await self.get(key)
574
+
575
+ return value
576
+
577
+
578
+ async def __setitem__(self, key: str, value: str | float) -> None:
579
+ """
580
+ Asynchrouous set item of parameter dictionary.
581
+
582
+ Parameters
583
+ ----------
584
+ key : Parameter key.
585
+ value : Parameter value.
586
+ """
587
+
588
+ # Set.
589
+ params = {key: value}
590
+
591
+ # Update.
592
+ await self.update(params)
593
+
594
+
325
595
  @overload
326
- def get(self) -> dict[str, str]: ...
596
+ async def get(self) -> dict[str, str]: ...
327
597
 
328
598
  @overload
329
- def get(self, key: str) -> str | None: ...
599
+ async def get(self, key: str) -> str | None: ...
330
600
 
331
- def get(self, key: str | None = None) -> dict[str, str] | str | None:
601
+ async def get(self, key: str | None = None) -> dict[str, str] | str | None:
332
602
  """
333
- Get parameter.
603
+ Asynchrouous get parameter.
334
604
 
335
605
  Parameters
336
606
  ----------
@@ -340,48 +610,49 @@ class DatabaseParametersVariable(DatabaseParameters):
340
610
 
341
611
  Returns
342
612
  -------
343
- Variables of database.
613
+ Status of database.
344
614
  """
345
615
 
346
616
  # Generate SQL.
347
-
348
- ## Global.
349
- if self.global_:
350
- sql = 'SHOW GLOBAL VARIABLES'
351
-
352
- ## Not global.
353
- else:
354
- sql = 'SHOW VARIABLES'
617
+ sql = 'SHOW ' + (
618
+ 'GLOBAL '
619
+ if self.glob
620
+ else ''
621
+ ) + self.mode
355
622
 
356
623
  # Execute SQL.
357
624
 
358
625
  ## Dictionary.
359
626
  if key is None:
360
- result = self.db.execute(sql, key=key)
361
- variables = result.to_dict(val_field=1)
627
+ result = await self.db.execute(sql, key=key)
628
+ status = result.to_dict(val_field=1)
362
629
 
363
630
  ## Value.
364
631
  else:
365
632
  sql += ' LIKE :key'
366
- result = self.db.execute(sql, key=key)
633
+ result = await self.db.execute(sql, key=key)
367
634
  row = result.first()
368
635
  if row is None:
369
- variables = None
636
+ status = None
370
637
  else:
371
- variables = row['Value']
638
+ status = row['Value']
372
639
 
373
- return variables
640
+ return status
374
641
 
375
642
 
376
- def update(self, params: dict[str, str | float]) -> None:
643
+ async def update(self, params: dict[str, str | float]) -> None:
377
644
  """
378
- Update parameter.
645
+ Asynchrouous update parameter.
379
646
 
380
647
  Parameters
381
648
  ----------
382
649
  params : Update parameter key value pairs.
383
650
  """
384
651
 
652
+ # Check.
653
+ if self.mode == 'STATUS':
654
+ raise AssertionError('database status not update')
655
+
385
656
  # Generate SQL.
386
657
  sql_set_list = [
387
658
  '%s = %s' % (
@@ -395,14 +666,83 @@ class DatabaseParametersVariable(DatabaseParameters):
395
666
  for key, value in params.items()
396
667
  ]
397
668
  sql_set = ',\n '.join(sql_set_list)
669
+ sql = 'SHOW ' + (
670
+ 'GLOBAL '
671
+ if self.glob
672
+ else ''
673
+ ) + sql_set
398
674
 
399
- ## Global.
400
- if self.global_:
401
- sql = f'SET GLOBAL {sql_set}'
675
+ # Execute SQL.
676
+ await self.db.execute(sql)
402
677
 
403
- ## Not global.
404
- else:
405
- sql = f'SET {sql_set}'
406
678
 
407
- # Execute SQL.
408
- self.db.execute(sql)
679
+ class DatabaseParametersVariables(DatabaseParameters):
680
+ """
681
+ Database variable parameters type.
682
+ """
683
+
684
+ mode: Final = 'VARIABLES'
685
+ glob: Final = False
686
+
687
+
688
+ class DatabaseParametersStatus(DatabaseParameters):
689
+ """
690
+ Database status parameters type.
691
+ """
692
+
693
+ mode: Final = 'STATUS'
694
+ glob: Final = False
695
+
696
+
697
+ class DatabaseParametersVariablesGlobal(DatabaseParameters):
698
+ """
699
+ Database global variable parameters type.
700
+ """
701
+
702
+ mode: Final = 'VARIABLES'
703
+ glob: Final = True
704
+
705
+
706
+ class DatabaseParametersStatusGlobal(DatabaseParameters):
707
+ """
708
+ Database global status parameters type.
709
+ """
710
+
711
+ mode: Final = 'STATUS'
712
+ glob: Final = True
713
+
714
+
715
+ class DatabaseParametersVariablesAsync(DatabaseParametersAsync):
716
+ """
717
+ Asynchrouous database variable parameters type.
718
+ """
719
+
720
+ mode: Final = 'VARIABLES'
721
+ glob: Final = False
722
+
723
+
724
+ class DatabaseParametersStatusAsync(DatabaseParametersAsync):
725
+ """
726
+ Asynchrouous database status parameters type.
727
+ """
728
+
729
+ mode: Final = 'STATUS'
730
+ glob: Final = False
731
+
732
+
733
+ class DatabaseParametersVariablesGlobalAsync(DatabaseParametersAsync):
734
+ """
735
+ Asynchrouous database global variable parameters type.
736
+ """
737
+
738
+ mode: Final = 'VARIABLES'
739
+ glob: Final = True
740
+
741
+
742
+ class DatabaseParametersStatusGlobalAsync(DatabaseParametersAsync):
743
+ """
744
+ Asynchrouous database global status parameters type.
745
+ """
746
+
747
+ mode: Final = 'STATUS'
748
+ glob: Final = True
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: reydb
3
- Version: 1.1.60
3
+ Version: 1.2.0
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=G2U0uiNjk2koYxIZjf9zvHoiQFO485XMnfQBMYSUdRc,549
2
+ reydb/rall.py,sha256=sOeiVXSIo2voFiiZaRx8h7_wWrx8fFjXkRQRSYHQccA,365
3
+ reydb/rbase.py,sha256=0QGHxbdrcyDTY6BK7zMCTM9cRDkZyVNlL7S1j7A4Zp0,8260
4
+ reydb/rbuild.py,sha256=R_WQNpp67oHZFZlRuui_vaZAlJL4s_y5NX0gJXZY1yA,40320
5
+ reydb/rconfig.py,sha256=mVRrkWa35stM5_sB0rD7VtgqLkVDv0BDnV_u2AGh0kI,18962
6
+ reydb/rconn.py,sha256=guRaR8N6RuzZzujwaeq7HhKWTizF9SrUBqEAFjfjpoo,6909
7
+ reydb/rdb.py,sha256=QB9het4tAPnJo4urWJ4oePI1g1avNI8O1P0-VPJLDEo,13809
8
+ reydb/rerror.py,sha256=LlEmKREe88QKLU3xf4mF88nXnGSQodrUruQAm9ifUeQ,14781
9
+ reydb/rexec.py,sha256=NLSvwwFhf60OndW3pjffzyMbWcLykhTbBbfI1YSueQ8,53223
10
+ reydb/rorm.py,sha256=IipUvaFnS0gfHxzYkNSuM60skYsIGNBA9DvD8DnTpCc,40885
11
+ reydb/rparam.py,sha256=S32dAKrCM0c4Qm-fhZkIRRhshsK1MmI12NgavNBfvJg,17443
12
+ reydb-1.2.0.dist-info/METADATA,sha256=0wocK72W-r7ZRvnaiZ4JFfJ9RLVCkXs2u6yrx1foDFw,1621
13
+ reydb-1.2.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
14
+ reydb-1.2.0.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
15
+ reydb-1.2.0.dist-info/RECORD,,