reykit 1.1.1__py3-none-any.whl → 1.1.2__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/rinformation.py DELETED
@@ -1,503 +0,0 @@
1
- # !/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
-
4
- """
5
- @Time : 2022-12-05 14:10:02
6
- @Author : Rey
7
- @Contact : reyxbo@163.com
8
- @Explain : Database information methods.
9
- """
10
-
11
-
12
- from __future__ import annotations
13
- from typing import Any, Literal, overload
14
-
15
- from .rconnection import RDatabase, RDBConnection
16
-
17
-
18
- __all__ = (
19
- 'RDBInformation',
20
- 'RDBISchema',
21
- 'RDBIDatabase',
22
- 'RDBITable',
23
- 'RDBIColumn'
24
- )
25
-
26
-
27
- class RDBInformation(object):
28
- """
29
- Rey's `database base information` type.
30
- """
31
-
32
-
33
- @overload
34
- def __call__(self: RDBISchema, name: str | None = None) -> RDBIDatabase | list[dict]: ...
35
-
36
- @overload
37
- def __call__(self: RDBIDatabase, name: str | None = None) -> RDBITable | list[dict]: ...
38
-
39
- @overload
40
- def __call__(self: RDBITable, name: str | None = None) -> RDBIColumn | list[dict]: ...
41
-
42
- @overload
43
- def __call__(self: RDBIColumn, name: str | None = None) -> dict: ...
44
-
45
- def __call__(self, name: str | None = None) -> RDBIDatabase | RDBITable | RDBIColumn | list[dict] | dict:
46
- """
47
- Get information table or subclass instance.
48
-
49
- Parameters
50
- ----------
51
- name : Subclass index name.
52
-
53
- Returns
54
- -------
55
- Information table or subclass instance.
56
- """
57
-
58
- # Information table.
59
- if name is None:
60
-
61
- ## Break.
62
- if not hasattr(self, '_get_info_table'):
63
- raise AssertionError("class '%s' does not have this method" % self.__class__.__name__)
64
-
65
- ## Get.
66
- result: list[dict] = self._get_info_table()
67
-
68
- # Subobject.
69
- else:
70
-
71
- ## Break.
72
- if not hasattr(self, '__getattr__'):
73
- raise AssertionError("class '%s' does not have this method" % self.__class__.__name__)
74
-
75
- ## Get.
76
- result = self.__getattr__(name)
77
-
78
- return result
79
-
80
-
81
- @overload
82
- def __getitem__(self, key: Literal['*', 'all', 'ALL']) -> dict: ...
83
-
84
- @overload
85
- def __getitem__(self, key: str) -> Any: ...
86
-
87
- def __getitem__(self, key: str) -> Any:
88
- """
89
- Get information attribute value or dictionary.
90
-
91
- Parameters
92
- ----------
93
- key : Attribute key. When key not exist, then try all caps key.
94
- - `Literal['*', 'all', 'ALL']`: Get attribute dictionary.
95
- - `str`: Get attribute value.
96
-
97
- Returns
98
- -------
99
- Information attribute value or dictionary.
100
- """
101
-
102
- # Break.
103
- if not hasattr(self, '_get_info_attrs'):
104
- raise AssertionError("class '%s' does not have this method" % self.__class__.__name__)
105
-
106
- # Get.
107
- info_attrs: dict = self._get_info_attrs()
108
-
109
- # Return.
110
-
111
- ## Dictionary.
112
- if key in ('*', 'all', 'ALL'):
113
- return info_attrs
114
-
115
- ## Value.
116
- info_attr = info_attrs.get(key)
117
- if info_attr is None:
118
- key_upper = key.upper()
119
- info_attr = info_attrs[key_upper]
120
- return info_attr
121
-
122
-
123
- @overload
124
- def __getattr__(self, key: Literal['_rdatabase']) -> RDatabase | RDBConnection: ...
125
-
126
- @overload
127
- def __getattr__(self, key: Literal['_database_name', '_table_name']) -> str: ...
128
-
129
- @overload
130
- def __getattr__(self: RDBISchema, key: str) -> RDBIDatabase: ...
131
-
132
- @overload
133
- def __getattr__(self: RDBIDatabase, key: str) -> RDBITable: ...
134
-
135
- @overload
136
- def __getattr__(self: RDBITable, key: str) -> RDBIColumn: ...
137
-
138
- def __getattr__(self, key: str) -> RDatabase | RDBConnection | str | RDBIDatabase | RDBITable | RDBIColumn:
139
- """
140
- Get attribute or build subclass instance.
141
-
142
- Parameters
143
- ----------
144
- key : Attribute key or table name.
145
-
146
- Returns
147
- -------
148
- Attribute or subclass instance.
149
- """
150
-
151
- # Filter private
152
- if key in ('_rdatabase', '_database_name', '_table_name'):
153
- return self.__dict__[key]
154
-
155
- # Build.
156
- match self:
157
- case RDBISchema():
158
- rtable = RDBIDatabase(self._rdatabase, key)
159
- case RDBIDatabase():
160
- rtable = RDBITable(self._rdatabase, self._database_name, key)
161
- case RDBITable():
162
- rtable = RDBIColumn(self._rdatabase, self._database_name, self._table_name, key)
163
- case _:
164
- raise AssertionError("class '%s' does not have this method" % self.__class__.__name__)
165
-
166
- return rtable
167
-
168
-
169
- class RDBISchema(RDBInformation):
170
- """
171
- Rey's `database information schema` type.
172
-
173
- Examples
174
- --------
175
- Get databases information of server.
176
- >>> databases_info = RDBISchema()
177
-
178
- Get tables information of database.
179
- >>> tables_info = RDBISchema.database()
180
-
181
- Get columns information of table.
182
- >>> columns_info = RDBISchema.database.table()
183
-
184
- Get column information.
185
- >>> column_info = RDBISchema.database.table.column()
186
-
187
- Get database attribute.
188
- >>> database_attr = RDBISchema.database['attribute']
189
-
190
- Get table attribute.
191
- >>> database_attr = RDBISchema.database.table['attribute']
192
-
193
- Get column attribute.
194
- >>> database_attr = RDBISchema.database.table.column['attribute']
195
- """
196
-
197
-
198
- def __init__(
199
- self,
200
- rdatabase: RDatabase | RDBConnection
201
- ) -> None:
202
- """
203
- Build `database information schema` attributes.
204
-
205
- Parameters
206
- ----------
207
- rdatabase : RDatabase or RDBConnection instance.
208
- """
209
-
210
- # Set parameter.
211
- self._rdatabase = rdatabase
212
-
213
-
214
- def _get_info_table(self) -> list[dict]:
215
- """
216
- Get information table.
217
-
218
- Returns
219
- -------
220
- Information table.
221
- """
222
-
223
- # Select.
224
- result = self._rdatabase.execute_select(
225
- 'information_schema.SCHEMATA',
226
- order='`schema_name`'
227
- )
228
-
229
- # Convert.
230
- info_table = result.fetch_table()
231
-
232
- return info_table
233
-
234
-
235
- class RDBIDatabase(RDBInformation):
236
- """
237
- Rey's `database information database` type.
238
-
239
- Examples
240
- --------
241
- Get tables information of database.
242
- >>> tables_info = RDBIDatabase()
243
-
244
- Get columns information of table.
245
- >>> columns_info = RDBIDatabase.table()
246
-
247
- Get column information.
248
- >>> column_info = RDBIDatabase.table.column()
249
-
250
- Get database attribute.
251
- >>> database_attr = RDBIDatabase['attribute']
252
-
253
- Get table attribute.
254
- >>> database_attr = RDBIDatabase.table['attribute']
255
-
256
- Get column attribute.
257
- >>> database_attr = RDBIDatabase.table.column['attribute']
258
- """
259
-
260
-
261
- def __init__(
262
- self,
263
- rdatabase: RDatabase | RDBConnection,
264
- database_name: str
265
- ) -> None:
266
- """
267
- Build `database information database` attributes.
268
-
269
- Parameters
270
- ----------
271
- rdatabase : RDatabase or RDBConnection instance.
272
- database_name : Database name.
273
- """
274
-
275
- # Set parameter.
276
- self._rdatabase = rdatabase
277
- self._database_name = database_name
278
-
279
-
280
- def _get_info_attrs(self) -> dict:
281
- """
282
- Get information attribute dictionary.
283
-
284
- Returns
285
- -------
286
- Information attribute dictionary.
287
- """
288
-
289
- # Select.
290
- where = '`SCHEMA_NAME` = :database_name'
291
- result = self._rdatabase.execute_select(
292
- 'information_schema.SCHEMATA',
293
- where=where,
294
- limit=1,
295
- database_name=self._database_name
296
- )
297
-
298
- # Check.
299
- assert result.exist, "database '%s' not exist" % self._database_name
300
-
301
- # Convert.
302
- info_table = result.fetch_table()
303
- info_attrs = info_table[0]
304
-
305
- return info_attrs
306
-
307
-
308
- def _get_info_table(self) -> list[dict]:
309
- """
310
- Get information table.
311
-
312
- Returns
313
- -------
314
- Information table.
315
- """
316
-
317
- # Select.
318
- where = '`TABLE_SCHEMA` = :database_name'
319
- result = self._rdatabase.execute_select(
320
- 'information_schema.TABLES',
321
- where=where,
322
- order='`TABLE_NAME`',
323
- database_name=self._database_name
324
- )
325
-
326
- # Check.
327
- assert result.exist, "database '%s' not exist" % self._database_name
328
-
329
- # Convert.
330
- info_table = result.fetch_table()
331
-
332
- return info_table
333
-
334
-
335
- class RDBITable(RDBInformation):
336
- """
337
- Rey's `database information table` type.
338
-
339
- Examples
340
- --------
341
- Get columns information of table.
342
- >>> columns_info = RDBITable()
343
-
344
- Get column information.
345
- >>> column_info = RDBITable.column()
346
-
347
- Get table attribute.
348
- >>> database_attr = RDBITable['attribute']
349
-
350
- Get column attribute.
351
- >>> database_attr = RDBITable.column['attribute']
352
- """
353
-
354
-
355
- def __init__(
356
- self,
357
- rdatabase: RDatabase | RDBConnection,
358
- database_name: str,
359
- table_name: str
360
- ) -> None:
361
- """
362
- Build `database information table` attributes.
363
-
364
- Parameters
365
- ----------
366
- rdatabase : RDatabase or RDBConnection instance.
367
- database_name : Database name.
368
- table_name : Table name.
369
- """
370
-
371
- # Set parameter.
372
- self._rdatabase = rdatabase
373
- self._database_name = database_name
374
- self._table_name = table_name
375
-
376
-
377
- def _get_info_attrs(self) -> dict:
378
- """
379
- Get information attribute dictionary.
380
-
381
- Returns
382
- -------
383
- Information attribute dictionary.
384
- """
385
-
386
- # Select.
387
- where = '`TABLE_SCHEMA` = :database_name AND `TABLE_NAME` = :table_name'
388
- result = self._rdatabase.execute_select(
389
- 'information_schema.TABLES',
390
- where=where,
391
- limit=1,
392
- database_name=self._database_name,
393
- table_name=self._table_name
394
- )
395
-
396
- # Check.
397
- assert result.exist, "database '%s' or table '%s' not exist" % (self._database_name, self._table_name)
398
-
399
- # Convert.
400
- info_table = result.fetch_table()
401
- info_attrs = info_table[0]
402
-
403
- return info_attrs
404
-
405
-
406
- def _get_info_table(self) -> list[dict]:
407
- """
408
- Get information table.
409
-
410
- Returns
411
- -------
412
- Information table.
413
- """
414
-
415
- # Select.
416
- where = '`TABLE_SCHEMA` = :database_name AND `TABLE_NAME` = :table_name'
417
- result = self._rdatabase.execute_select(
418
- 'information_schema.COLUMNS',
419
- where=where,
420
- order='`ORDINAL_POSITION`',
421
- database_name=self._database_name,
422
- table_name=self._table_name
423
- )
424
-
425
- # Check.
426
- assert result.exist, "database '%s' or table '%s' not exist" % (self._database_name, self._table_name)
427
-
428
- # Convert.
429
- info_table = result.fetch_table()
430
-
431
- return info_table
432
-
433
-
434
- class RDBIColumn(RDBInformation):
435
- """
436
- Rey's `database information column` type.
437
-
438
- Examples
439
- --------
440
- Get column information.
441
- >>> column_info = RDBIColumn()
442
-
443
- Get column attribute.
444
- >>> database_attr = RDBIColumn['attribute']
445
- """
446
-
447
-
448
- def __init__(
449
- self,
450
- rdatabase: RDatabase | RDBConnection,
451
- database_name: str,
452
- table_name: str,
453
- column_name: str
454
- ) -> None:
455
- """
456
- Build `database information column` attributes.
457
-
458
- Parameters
459
- ----------
460
- rdatabase : RDatabase or RDBConnection instance.
461
- database_name : Database name.
462
- table_name : Table name.
463
- column_name : Column name.
464
- """
465
-
466
- # Set parameter.
467
- self._rdatabase = rdatabase
468
- self._database_name = database_name
469
- self._table_name = table_name
470
- self._column_name = column_name
471
-
472
-
473
- def _get_info_attrs(self) -> dict:
474
- """
475
- Get information attribute dictionary.
476
-
477
- Returns
478
- -------
479
- Information attribute dictionary.
480
- """
481
-
482
- # Select.
483
- where = '`TABLE_SCHEMA` = :database_name AND `TABLE_NAME` = :table_name AND `COLUMN_NAME` = :column_name'
484
- result = self._rdatabase.execute_select(
485
- 'information_schema.COLUMNS',
486
- where=where,
487
- limit=1,
488
- database_name=self._database_name,
489
- table_name=self._table_name,
490
- column_name=self._column_name
491
- )
492
-
493
- # Check.
494
- assert result.exist, "database '%s' or table '%s' or column '%s' not exist" % (self._database_name, self._table_name, self._column_name)
495
-
496
- # Convert.
497
- info_table = result.fetch_table()
498
- info_attrs = info_table[0]
499
-
500
- return info_attrs
501
-
502
-
503
- _get_info_table = _get_info_attrs