reydb 1.1.61__py3-none-any.whl → 1.2.1__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.
@@ -5,7 +5,7 @@
5
5
  @Time : 2022-12-05 14:10:02
6
6
  @Author : Rey
7
7
  @Contact : reyxbo@163.com
8
- @Explain : Database parameter methods.
8
+ @Explain : Database information methods.
9
9
  """
10
10
 
11
11
 
@@ -17,29 +17,36 @@ from .rexec import Result
17
17
 
18
18
 
19
19
  __all__ = (
20
- 'DatabaseSchemaSuper',
21
- 'DatabaseSchema',
22
- 'DatabaseSchemaAsync',
23
- 'DatabaseParametersSuper',
24
- 'DatabaseParameters',
25
- 'DatabaseParametersAsync',
26
- 'DatabaseParametersVariables',
27
- 'DatabaseParametersStatus',
28
- 'DatabaseParametersVariablesGlobal',
29
- 'DatabaseParametersStatusGlobal',
30
- 'DatabaseParametersVariablesAsync',
31
- 'DatabaseParametersStatusAsync',
32
- 'DatabaseParametersVariablesGlobalAsync',
33
- 'DatabaseParametersStatusGlobalAsync'
20
+ 'DatabaseInformationBase',
21
+ 'DatabaseInformationSchemaSuper',
22
+ 'DatabaseInformationSchema',
23
+ 'DatabaseInformationSchemaAsync',
24
+ 'DatabaseInformationParameterSuper',
25
+ 'DatabaseInformationParameter',
26
+ 'DatabaseInformationParameterAsync',
27
+ 'DatabaseInformationParameterVariables',
28
+ 'DatabaseInformationParameterStatus',
29
+ 'DatabaseInformationParameterVariablesGlobal',
30
+ 'DatabaseInformationParameterStatusGlobal',
31
+ 'DatabaseInformationParameterVariablesAsync',
32
+ 'DatabaseInformationParameterStatusAsync',
33
+ 'DatabaseInformationParameterVariablesGlobalAsync',
34
+ 'DatabaseInformationParameterStatusGlobalAsync'
34
35
  )
35
36
 
36
37
 
37
38
  DatabaseT = TypeVar('DatabaseT', 'rdb.Database', 'rdb.DatabaseAsync')
38
39
 
39
40
 
40
- class DatabaseSchemaSuper(DatabaseBase, Generic[DatabaseT]):
41
+ class DatabaseInformationBase(DatabaseBase):
41
42
  """
42
- Database schema super type.
43
+ Database information base type.
44
+ """
45
+
46
+
47
+ class DatabaseInformationSchemaSuper(DatabaseInformationBase, Generic[DatabaseT]):
48
+ """
49
+ Database information schema super type.
43
50
  """
44
51
 
45
52
 
@@ -55,10 +62,9 @@ class DatabaseSchemaSuper(DatabaseBase, Generic[DatabaseT]):
55
62
  # Set parameter.
56
63
  self.db = db
57
64
 
58
-
59
- def _call__before(self, filter_default: bool = True) -> tuple[str, tuple[str, ...]]:
65
+ def handle_before__call__(self, filter_default: bool = True) -> tuple[str, tuple[str, ...]]:
60
66
  """
61
- Before handle of call method.
67
+ Before handle method of call method.
62
68
 
63
69
  Parameters
64
70
  ----------
@@ -98,9 +104,9 @@ class DatabaseSchemaSuper(DatabaseBase, Generic[DatabaseT]):
98
104
  return sql, filter_db
99
105
 
100
106
 
101
- def _call__after(self, result: Result) -> dict[str, dict[str, list[str]]]:
107
+ def handle_after__call__(self, result: Result) -> dict[str, dict[str, list[str]]]:
102
108
  """
103
- After handle of call method.
109
+ After handle method of call method.
104
110
 
105
111
  Parameters
106
112
  ----------
@@ -141,13 +147,80 @@ class DatabaseSchemaSuper(DatabaseBase, Generic[DatabaseT]):
141
147
  return schema_dict
142
148
 
143
149
 
144
- class DatabaseSchema(DatabaseSchemaSuper['rdb.Database']):
150
+ @overload
151
+ def handle_exist(
152
+ self,
153
+ schema: dict[str, dict[str, list[str]]],
154
+ database: str
155
+ ) -> bool: ...
156
+
157
+ @overload
158
+ def handle_exist(
159
+ self,
160
+ schema: dict[str, dict[str, list[str]]],
161
+ database: str,
162
+ table: str
163
+ ) -> bool: ...
164
+
165
+ @overload
166
+ def handle_exist(
167
+ self,
168
+ schema: dict[str, dict[str, list[str]]],
169
+ database: str,
170
+ table: str,
171
+ column: str
172
+ ) -> bool: ...
173
+
174
+ def handle_exist(
175
+ self,
176
+ schema: dict[str, dict[str, list[str]]],
177
+ database: str,
178
+ table: str | None = None,
179
+ column: str | None = None
180
+ ) -> bool:
181
+ """
182
+ Handle method of judge database or table or column whether it exists.
183
+
184
+ Parameters
185
+ ----------
186
+ schema : Schemata of databases and tables and columns.
187
+ database : Database name.
188
+ table : Table name.
189
+ column : Column name.
190
+
191
+ Returns
192
+ -------
193
+ Judge result.
194
+ """
195
+
196
+ # Handle parameter.
197
+
198
+ # Judge.
199
+ judge = (
200
+ database in schema
201
+ and (
202
+ table is None
203
+ or (
204
+ (database_info := schema.get(database)) is not None
205
+ and (table_info := database_info.get(table)) is not None
206
+ )
207
+ )
208
+ and (
209
+ column is None
210
+ or column in table_info
211
+ )
212
+ )
213
+
214
+ return judge
215
+
216
+
217
+ class DatabaseInformationSchema(DatabaseInformationSchemaSuper['rdb.Database']):
145
218
  """
146
- Database schema type.
219
+ Database information schema type.
147
220
  """
148
221
 
149
222
 
150
- def __call__(self, filter_default: bool = True) -> dict[str, dict[str, list[str]]]:
223
+ def schema(self, filter_default: bool = True) -> dict[str, dict[str, list[str]]]:
151
224
  """
152
225
  Get schemata of databases and tables and columns.
153
226
 
@@ -161,20 +234,92 @@ class DatabaseSchema(DatabaseSchemaSuper['rdb.Database']):
161
234
  """
162
235
 
163
236
  # Get.
164
- sql, filter_db = self._call__before(filter_default)
237
+ sql, filter_db = self.handle_before__call__(filter_default)
165
238
  result = self.db.execute(sql, filter_db=filter_db)
166
- schema_dict = self._call__after(result)
239
+ schema = self.handle_after__call__(result)
167
240
 
168
- return schema_dict
241
+ # Cache.
242
+ if self.db._schema is None:
243
+ self.db._schema = schema
244
+ else:
245
+ self.db._schema.update(schema)
246
+
247
+ return schema
248
+
249
+
250
+ __call__ = schema
251
+
252
+
253
+ @overload
254
+ def exist(
255
+ self,
256
+ database: str,
257
+ *,
258
+ refresh: bool = True
259
+ ) -> bool: ...
260
+
261
+ @overload
262
+ def exist(
263
+ self,
264
+ database: str,
265
+ *,
266
+ table: str,
267
+ refresh: bool = True
268
+ ) -> bool: ...
269
+
270
+ @overload
271
+ def exist(
272
+ self,
273
+ database: str,
274
+ table: str,
275
+ column: str,
276
+ refresh: bool = True
277
+ ) -> bool: ...
278
+
279
+ def exist(
280
+ self,
281
+ database: str,
282
+ table: str | None = None,
283
+ column: str | None = None,
284
+ refresh: bool = True
285
+ ) -> bool:
286
+ """
287
+ Judge database or table or column whether it exists.
288
+
289
+ Parameters
290
+ ----------
291
+ database : Database name.
292
+ table : Table name.
293
+ column : Column name.
294
+ refresh : Whether refresh cache data. Cache can improve efficiency.
169
295
 
296
+ Returns
297
+ -------
298
+ Judge result.
299
+ """
170
300
 
171
- class DatabaseSchemaAsync(DatabaseSchemaSuper['rdb.DatabaseAsync']):
301
+ # Handle parameter.
302
+ if (
303
+ refresh
304
+ or self.db._schema is None
305
+ ):
306
+ schema = self.schema()
307
+ else:
308
+ schema = self.db._schema
309
+
310
+ # Judge.
311
+ result = self.handle_exist(schema, database, table, column)
312
+
313
+ return result
314
+
315
+
316
+ class DatabaseInformationSchemaAsync(DatabaseInformationSchemaSuper['rdb.DatabaseAsync']):
172
317
  """
173
- Asynchronous database schema type.
318
+ Asynchronous database information schema type.
174
319
  """
175
320
 
176
321
 
177
- async def __call__(self, filter_default: bool = True) -> dict[str, dict[str, list[str]]]:
322
+ async def schema(self, filter_default: bool = True) -> dict[str, dict[str, list[str]]]:
178
323
  """
179
324
  Asynchronous get schemata of databases and tables and columns.
180
325
 
@@ -188,16 +333,86 @@ class DatabaseSchemaAsync(DatabaseSchemaSuper['rdb.DatabaseAsync']):
188
333
  """
189
334
 
190
335
  # Get.
191
- sql, filter_db = self._call__before(filter_default)
336
+ sql, filter_db = self.handle_before__call__(filter_default)
192
337
  result = await self.db.execute(sql, filter_db=filter_db)
193
- schema_dict = self._call__after(result)
338
+ schema = self.handle_after__call__(result)
194
339
 
195
- return schema_dict
340
+ # Cache.
341
+ if self.db._schema is not None:
342
+ self.db._schema.update(schema)
343
+
344
+ return schema
345
+
346
+
347
+ __call__ = schema
348
+
349
+
350
+ @overload
351
+ async def exist(
352
+ self,
353
+ database: str,
354
+ *,
355
+ refresh: bool = True
356
+ ) -> bool: ...
357
+
358
+ @overload
359
+ async def exist(
360
+ self,
361
+ database: str,
362
+ *,
363
+ table: str,
364
+ refresh: bool = True
365
+ ) -> bool: ...
366
+
367
+ @overload
368
+ async def exist(
369
+ self,
370
+ database: str,
371
+ table: str,
372
+ column: str,
373
+ refresh: bool = True
374
+ ) -> bool: ...
375
+
376
+ async def exist(
377
+ self,
378
+ database: str,
379
+ table: str | None = None,
380
+ column: str | None = None,
381
+ refresh: bool = True
382
+ ) -> bool:
383
+ """
384
+ Asynchronous judge database or table or column whether it exists.
385
+
386
+ Parameters
387
+ ----------
388
+ database : Database name.
389
+ table : Table name.
390
+ column : Column name.
391
+ refresh : Whether refresh cache data. Cache can improve efficiency.
392
+
393
+ Returns
394
+ -------
395
+ Judge result.
396
+ """
397
+
398
+ # Handle parameter.
399
+ if (
400
+ refresh
401
+ or self.db._schema is None
402
+ ):
403
+ schema = await self.schema()
404
+ else:
405
+ schema = self.db._schema
406
+
407
+ # Judge.
408
+ result = self.handle_exist(schema, database, table, column)
409
+
410
+ return result
196
411
 
197
412
 
198
- class DatabaseParametersSuper(DatabaseBase, Generic[DatabaseT]):
413
+ class DatabaseInformationParameterSuper(DatabaseInformationBase, Generic[DatabaseT]):
199
414
  """
200
- Database parameters super type.
415
+ Database information parameters super type.
201
416
  """
202
417
 
203
418
  mode: Literal['VARIABLES', 'STATUS']
@@ -220,9 +435,9 @@ class DatabaseParametersSuper(DatabaseBase, Generic[DatabaseT]):
220
435
  self.db = db
221
436
 
222
437
 
223
- class DatabaseParameters(DatabaseParametersSuper['rdb.Database']):
438
+ class DatabaseInformationParameter(DatabaseInformationParameterSuper['rdb.Database']):
224
439
  """
225
- Database parameters type.
440
+ Database information parameters type.
226
441
  """
227
442
 
228
443
 
@@ -342,9 +557,9 @@ class DatabaseParameters(DatabaseParametersSuper['rdb.Database']):
342
557
  self.db.execute(sql)
343
558
 
344
559
 
345
- class DatabaseParametersAsync(DatabaseParametersSuper['rdb.DatabaseAsync']):
560
+ class DatabaseInformationParameterAsync(DatabaseInformationParameterSuper['rdb.DatabaseAsync']):
346
561
  """
347
- Asynchrouous database parameters type.
562
+ Asynchrouous database information parameters type.
348
563
  """
349
564
 
350
565
 
@@ -468,72 +683,72 @@ class DatabaseParametersAsync(DatabaseParametersSuper['rdb.DatabaseAsync']):
468
683
  await self.db.execute(sql)
469
684
 
470
685
 
471
- class DatabaseParametersVariables(DatabaseParameters):
686
+ class DatabaseInformationParameterVariables(DatabaseInformationParameter):
472
687
  """
473
- Database variable parameters type.
688
+ Database information variable parameters type.
474
689
  """
475
690
 
476
691
  mode: Final = 'VARIABLES'
477
692
  glob: Final = False
478
693
 
479
694
 
480
- class DatabaseParametersStatus(DatabaseParameters):
695
+ class DatabaseInformationParameterStatus(DatabaseInformationParameter):
481
696
  """
482
- Database status parameters type.
697
+ Database information status parameters type.
483
698
  """
484
699
 
485
700
  mode: Final = 'STATUS'
486
701
  glob: Final = False
487
702
 
488
703
 
489
- class DatabaseParametersVariablesGlobal(DatabaseParameters):
704
+ class DatabaseInformationParameterVariablesGlobal(DatabaseInformationParameter):
490
705
  """
491
- Database global variable parameters type.
706
+ Database information global variable parameters type.
492
707
  """
493
708
 
494
709
  mode: Final = 'VARIABLES'
495
710
  glob: Final = True
496
711
 
497
712
 
498
- class DatabaseParametersStatusGlobal(DatabaseParameters):
713
+ class DatabaseInformationParameterStatusGlobal(DatabaseInformationParameter):
499
714
  """
500
- Database global status parameters type.
715
+ Database information global status parameters type.
501
716
  """
502
717
 
503
718
  mode: Final = 'STATUS'
504
719
  glob: Final = True
505
720
 
506
721
 
507
- class DatabaseParametersVariablesAsync(DatabaseParametersAsync):
722
+ class DatabaseInformationParameterVariablesAsync(DatabaseInformationParameterAsync):
508
723
  """
509
- Asynchrouous database variable parameters type.
724
+ Asynchrouous database information variable parameters type.
510
725
  """
511
726
 
512
727
  mode: Final = 'VARIABLES'
513
728
  glob: Final = False
514
729
 
515
730
 
516
- class DatabaseParametersStatusAsync(DatabaseParametersAsync):
731
+ class DatabaseInformationParameterStatusAsync(DatabaseInformationParameterAsync):
517
732
  """
518
- Asynchrouous database status parameters type.
733
+ Asynchrouous database information status parameters type.
519
734
  """
520
735
 
521
736
  mode: Final = 'STATUS'
522
737
  glob: Final = False
523
738
 
524
739
 
525
- class DatabaseParametersVariablesGlobalAsync(DatabaseParametersAsync):
740
+ class DatabaseInformationParameterVariablesGlobalAsync(DatabaseInformationParameterAsync):
526
741
  """
527
- Asynchrouous database global variable parameters type.
742
+ Asynchrouous database information global variable parameters type.
528
743
  """
529
744
 
530
745
  mode: Final = 'VARIABLES'
531
746
  glob: Final = True
532
747
 
533
748
 
534
- class DatabaseParametersStatusGlobalAsync(DatabaseParametersAsync):
749
+ class DatabaseInformationParameterStatusGlobalAsync(DatabaseInformationParameterAsync):
535
750
  """
536
- Asynchrouous database global status parameters type.
751
+ Asynchrouous database information global status parameters type.
537
752
  """
538
753
 
539
754
  mode: Final = 'STATUS'