reydb 1.1.22__py3-none-any.whl → 1.1.24__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/rconn.py CHANGED
@@ -149,12 +149,11 @@ class DBConnection(Database):
149
149
  Result object.
150
150
  """
151
151
 
152
- # Handle parameter.by priority.
152
+ # Handle parameter by priority.
153
153
  report = get_first_notnone(report, self.default_report)
154
154
 
155
155
  # Handle parameter.
156
- if type(sql) == str:
157
- sql = sqlalchemy_text(sql)
156
+ sql = self.handle_sql(sql)
158
157
  if data is None:
159
158
  if kwdata == {}:
160
159
  data = []
@@ -165,8 +164,6 @@ class DBConnection(Database):
165
164
  data = data_table.to_table()
166
165
  for row in data:
167
166
  row.update(kwdata)
168
-
169
- # Handle data.
170
167
  data = self.handle_data(data, sql)
171
168
 
172
169
  # Execute.
reydb/rdb.py CHANGED
@@ -646,6 +646,32 @@ class Database(BaseDatabase):
646
646
  return keep_n, overflow_n
647
647
 
648
648
 
649
+ def handle_sql(self, sql: str | TextClause) -> TextClause:
650
+ """
651
+ Handle SQL.
652
+
653
+ Parameters
654
+ ----------
655
+ sql : SQL in method `sqlalchemy.text` format, or TextClause object.
656
+
657
+ Returns
658
+ -------
659
+ TextClause instance.
660
+ """
661
+
662
+ # Handle parameter.
663
+ if type(sql) == TextClause:
664
+ sql = sql.text
665
+
666
+ # Handle.
667
+ sql = sql.strip()
668
+ if sql[-1] != ';':
669
+ sql += ';'
670
+ sql = sqlalchemy_text(sql)
671
+
672
+ return sql
673
+
674
+
649
675
  def handle_data(
650
676
  self,
651
677
  data: list[dict],
@@ -657,7 +683,7 @@ class Database(BaseDatabase):
657
683
  Parameters
658
684
  ----------
659
685
  data : Data set for filling.
660
- sql : SQL in method sqlalchemy.text format, or TextClause object.
686
+ sql : SQL in method `sqlalchemy.text` format, or TextClause object.
661
687
 
662
688
  Returns
663
689
  -------
@@ -830,12 +856,11 @@ class Database(BaseDatabase):
830
856
  Result object.
831
857
  """
832
858
 
833
- # Handle parameter.by priority.
859
+ # Handle parameter by priority.
834
860
  report = get_first_notnone(report, self.default_report)
835
861
 
836
862
  # Handle parameter.
837
- if type(sql) == str:
838
- sql = sqlalchemy_text(sql)
863
+ sql = self.handle_sql(sql)
839
864
  if data is None:
840
865
  if kwdata == {}:
841
866
  data = []
@@ -846,8 +871,6 @@ class Database(BaseDatabase):
846
871
  data = data_table.to_table()
847
872
  for row in data:
848
873
  row.update(kwdata)
849
-
850
- # Handle data.
851
874
  data = self.handle_data(data, sql)
852
875
 
853
876
  # Execute.
@@ -1783,7 +1806,7 @@ class Database(BaseDatabase):
1783
1806
  )
1784
1807
  if filter_default:
1785
1808
  where_database = 'WHERE `SCHEMA_NAME` NOT IN :filter_db\n'
1786
- where_column = 'WHERE `TABLE_SCHEMA` NOT IN :filter_db\n'
1809
+ where_column = ' WHERE `TABLE_SCHEMA` NOT IN :filter_db\n'
1787
1810
  else:
1788
1811
  where_database = where_column = ''
1789
1812
 
@@ -1793,10 +1816,11 @@ class Database(BaseDatabase):
1793
1816
  'FROM `information_schema`.`SCHEMATA`\n'
1794
1817
  f'{where_database}'
1795
1818
  'UNION ALL (\n'
1796
- 'SELECT `TABLE_SCHEMA`, `TABLE_NAME`, `COLUMN_NAME`\n'
1797
- 'FROM `information_schema`.`COLUMNS`\n'
1819
+ ' SELECT `TABLE_SCHEMA`, `TABLE_NAME`, `COLUMN_NAME`\n'
1820
+ ' FROM `information_schema`.`COLUMNS`\n'
1798
1821
  f'{where_column}'
1799
- 'ORDER BY `TABLE_SCHEMA`, `TABLE_NAME`, `ORDINAL_POSITION`)'
1822
+ ' ORDER BY `TABLE_SCHEMA`, `TABLE_NAME`, `ORDINAL_POSITION`\n'
1823
+ ')'
1800
1824
  )
1801
1825
  result = self.execute(sql, filter_db=filter_db)
1802
1826
 
reydb/rfile.py CHANGED
@@ -29,41 +29,43 @@ FileInfo = TypedDict('FileInfo', {'create_time': datetime, 'md5': str, 'name': s
29
29
  class DBFile(BaseDatabase):
30
30
  """
31
31
  Database file type.
32
+ Can create database used `self.build` method.
32
33
  """
33
34
 
34
35
 
35
36
  def __init__(
36
37
  self,
37
- rdatabase: Database | DBConnection
38
+ database: Database | DBConnection
38
39
  ) -> None:
39
40
  """
40
41
  Build instance attributes.
41
42
 
42
43
  Parameters
43
44
  ----------
44
- rdatabase : Database or DBConnection instance.
45
+ database : Database or DBConnection instance.
45
46
  """
46
47
 
47
48
  # SQLite.
48
- if rdatabase.backend == 'sqlite':
49
+ if database.backend == 'sqlite':
49
50
  text='not suitable for SQLite databases'
50
51
  throw(AssertionError, text=text)
51
52
 
52
53
  # Set attribute.
53
- self.rdatabase = rdatabase
54
+ self.database = database
54
55
 
55
56
  ## Database path name.
56
- self.path_names = {
57
+ self.db_names = {
57
58
  'file': 'file',
58
59
  'file.information': 'information',
59
60
  'file.data': 'data',
60
- 'file.stats': 'stats'
61
+ 'file.stats': 'stats',
62
+ 'file.data_information': 'data_information'
61
63
  }
62
64
 
63
65
 
64
66
  def build(self) -> None:
65
67
  """
66
- Check and build all standard databases and tables, by `self.path_names`.
68
+ Check and build all standard databases and tables, by `self.db_names`.
67
69
  """
68
70
 
69
71
  # Set parameter.
@@ -71,7 +73,7 @@ class DBFile(BaseDatabase):
71
73
  ## Database.
72
74
  databases = [
73
75
  {
74
- 'name': self.path_names['file']
76
+ 'name': self.db_names['file']
75
77
  }
76
78
  ]
77
79
 
@@ -80,7 +82,7 @@ class DBFile(BaseDatabase):
80
82
 
81
83
  ### 'information'.
82
84
  {
83
- 'path': (self.path_names['file'], self.path_names['file.information']),
85
+ 'path': (self.db_names['file'], self.db_names['file.information']),
84
86
  'fields': [
85
87
  {
86
88
  'name': 'create_time',
@@ -92,7 +94,7 @@ class DBFile(BaseDatabase):
92
94
  'name': 'file_id',
93
95
  'type': 'mediumint unsigned',
94
96
  'constraint': 'NOT NULL AUTO_INCREMENT',
95
- 'comment': 'File self increase ID.'
97
+ 'comment': 'File ID.'
96
98
  },
97
99
  {
98
100
  'name': 'md5',
@@ -133,7 +135,7 @@ class DBFile(BaseDatabase):
133
135
 
134
136
  ### 'data'.
135
137
  {
136
- 'path': (self.path_names['file'], self.path_names['file.data']),
138
+ 'path': (self.db_names['file'], self.db_names['file.data']),
137
139
  'fields': [
138
140
  {
139
141
  'name': 'md5',
@@ -157,6 +159,33 @@ class DBFile(BaseDatabase):
157
159
  'primary': 'md5',
158
160
  'comment': 'File data table.'
159
161
  }
162
+
163
+ ]
164
+
165
+ ## View.
166
+ views = [
167
+
168
+ ### Data information.
169
+ {
170
+ 'path': (self.db_names['file'], self.db_names['file.data_information']),
171
+ 'select': (
172
+ 'SELECT `b`.`last_time`, `a`.`md5`, `a`.`size`, `b`.`names`, `b`.`notes`\n'
173
+ f'FROM `{self.db_names['file']}`.`{self.db_names['file.data']}` AS `a`\n'
174
+ 'LEFT JOIN (\n'
175
+ ' SELECT\n'
176
+ ' `md5`,\n'
177
+ " GROUP_CONCAT(DISTINCT(`name`) ORDER BY `create_time` DESC SEPARATOR ' | ') AS `names`,\n"
178
+ " GROUP_CONCAT(DISTINCT(`note`) ORDER BY `create_time` DESC SEPARATOR ' | ') AS `notes`,\n"
179
+ ' MAX(`create_time`) as `last_time`\n'
180
+ f' FROM `{self.db_names['file']}`.`{self.db_names['file.information']}`\n'
181
+ ' GROUP BY `md5`\n'
182
+ ' ORDER BY `last_time` DESC\n'
183
+ ') AS `b`\n'
184
+ 'ON `a`.`md5` = `b`.`md5`\n'
185
+ 'ORDER BY `last_time` DESC'
186
+ )
187
+ }
188
+
160
189
  ]
161
190
 
162
191
  ## View stats.
@@ -164,13 +193,13 @@ class DBFile(BaseDatabase):
164
193
 
165
194
  ### 'stats'.
166
195
  {
167
- 'path': (self.path_names['file'], self.path_names['file.stats']),
196
+ 'path': (self.db_names['file'], self.db_names['file.stats']),
168
197
  'items': [
169
198
  {
170
199
  'name': 'count',
171
200
  'select': (
172
201
  'SELECT COUNT(1)\n'
173
- 'FROM `file`.`information`'
202
+ f'FROM `{self.db_names['file']}`.`{self.db_names['file.information']}`'
174
203
  ),
175
204
  'comment': 'File information count.'
176
205
  },
@@ -178,10 +207,21 @@ class DBFile(BaseDatabase):
178
207
  'name': 'count_data',
179
208
  'select': (
180
209
  'SELECT COUNT(1)\n'
181
- 'FROM `file`.`data`'
210
+ f'FROM `{self.db_names['file']}`.`{self.db_names['file.data']}`'
182
211
  ),
183
212
  'comment': 'File data unique count.'
184
213
  },
214
+ {
215
+ 'name': 'size_sum',
216
+ 'select': (
217
+ 'SELECT CONCAT(\n'
218
+ ' ROUND(SUM(`size`) / 1024),\n'
219
+ " ' KB'\n"
220
+ ')\n'
221
+ f'FROM `{self.db_names['file']}`.`{self.db_names['file.data']}`'
222
+ ),
223
+ 'comment': 'File total size.'
224
+ },
185
225
  {
186
226
  'name': 'size_avg',
187
227
  'select': (
@@ -189,7 +229,7 @@ class DBFile(BaseDatabase):
189
229
  ' ROUND(AVG(`size`) / 1024),\n'
190
230
  " ' KB'\n"
191
231
  ')\n'
192
- 'FROM `file`.`data`\n'
232
+ f'FROM `{self.db_names['file']}`.`{self.db_names['file.data']}`'
193
233
  ),
194
234
  'comment': 'File average size.'
195
235
  },
@@ -200,7 +240,7 @@ class DBFile(BaseDatabase):
200
240
  ' ROUND(MAX(`size`) / 1024),\n'
201
241
  " ' KB'\n"
202
242
  ')\n'
203
- 'FROM `file`.`data`\n'
243
+ f'FROM `{self.db_names['file']}`.`{self.db_names['file.data']}`'
204
244
  ),
205
245
  'comment': 'File maximum size.'
206
246
  },
@@ -208,16 +248,17 @@ class DBFile(BaseDatabase):
208
248
  'name': 'last_time',
209
249
  'select': (
210
250
  'SELECT MAX(`create_time`)\n'
211
- 'FROM `file`.`information`'
251
+ f'FROM `{self.db_names['file']}`.`{self.db_names['file.information']}`'
212
252
  ),
213
253
  'comment': 'File last record create time.'
214
254
  }
215
255
  ]
216
256
  }
257
+
217
258
  ]
218
259
 
219
260
  # Build.
220
- self.rdatabase.build.build(databases, tables, views_stats=views_stats)
261
+ self.database.build.build(databases, tables, views, views_stats)
221
262
 
222
263
 
223
264
  def upload(
@@ -245,7 +286,7 @@ class DBFile(BaseDatabase):
245
286
  """
246
287
 
247
288
  # Handle parameter.
248
- conn = self.rdatabase.connect()
289
+ conn = self.database.connect()
249
290
  match source:
250
291
 
251
292
  ## File path.
@@ -272,7 +313,7 @@ class DBFile(BaseDatabase):
272
313
 
273
314
  # Exist.
274
315
  exist = conn.execute_exist(
275
- (self.path_names['file'], self.path_names['file.data']),
316
+ (self.db_names['file'], self.db_names['file.data']),
276
317
  '`md5` = :file_md5',
277
318
  file_md5=file_md5
278
319
  )
@@ -287,7 +328,7 @@ class DBFile(BaseDatabase):
287
328
  'bytes': file_bytes
288
329
  }
289
330
  conn.execute_insert(
290
- (self.path_names['file'], self.path_names['file.data']),
331
+ (self.db_names['file'], self.db_names['file.data']),
291
332
  data,
292
333
  'ignore'
293
334
  )
@@ -299,7 +340,7 @@ class DBFile(BaseDatabase):
299
340
  'note': note
300
341
  }
301
342
  conn.execute_insert(
302
- (self.path_names['file'], self.path_names['file.information']),
343
+ (self.db_names['file'], self.db_names['file.information']),
303
344
  data
304
345
  )
305
346
 
@@ -352,17 +393,17 @@ class DBFile(BaseDatabase):
352
393
  sql = (
353
394
  'SELECT `name`, (\n'
354
395
  ' SELECT `bytes`\n'
355
- ' FROM `file`.`data`\n'
356
- ' WHERE `md5` = `information`.`md5`\n'
396
+ f' FROM `{self.db_names['file']}`.`{self.db_names['file.data']}`\n'
397
+ f' WHERE `md5` = `{self.db_names['file.information']}`.`md5`\n'
357
398
  ' LIMIT 1\n'
358
399
  ') AS `bytes`\n'
359
- 'FROM `file`.`information`\n'
400
+ f'FROM `{self.db_names['file']}`.`{self.db_names['file.information']}`\n'
360
401
  'WHERE `file_id` = :file_id\n'
361
402
  'LIMIT 1'
362
403
  )
363
404
 
364
405
  # Execute SQL.
365
- result = self.rdatabase.execute(sql, file_id=file_id)
406
+ result = self.database.execute(sql, file_id=file_id)
366
407
 
367
408
  # Check.
368
409
  if result.empty:
@@ -404,17 +445,17 @@ class DBFile(BaseDatabase):
404
445
  sql = (
405
446
  'SELECT `create_time`, `md5`, `name`, `note`, (\n'
406
447
  ' SELECT `size`\n'
407
- ' FROM `file`.`data`\n'
448
+ f' FROM `{self.db_names['file']}`.`{self.db_names['file.data']}`\n'
408
449
  ' WHERE `md5` = `a`.`md5`\n'
409
450
  ' LIMIT 1\n'
410
451
  ') AS `size`\n'
411
- 'FROM `file`.`information` AS `a`\n'
452
+ f'FROM `{self.db_names['file']}`.`{self.db_names['file.information']}` AS `a`\n'
412
453
  'WHERE `file_id` = :file_id\n'
413
454
  'LIMIT 1'
414
455
  )
415
456
 
416
457
  # Execute SQL.
417
- result = self.rdatabase.execute(sql, file_id=file_id)
458
+ result = self.database.execute(sql, file_id=file_id)
418
459
 
419
460
  # Check.
420
461
  if result.empty:
@@ -425,6 +466,3 @@ class DBFile(BaseDatabase):
425
466
  info = table[0]
426
467
 
427
468
  return info
428
-
429
-
430
- __call__ = build
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: reydb
3
- Version: 1.1.22
3
+ Version: 1.1.24
4
4
  Summary: Database method set.
5
5
  Project-URL: homepage, https://github.com/reyxbo/reydb/
6
6
  Author-email: Rey <reyxbo@163.com>
@@ -2,13 +2,13 @@ reydb/__init__.py,sha256=UetBDQylwFTWiPMKF1ZPW45A6mwYWWSYneTesl4xKBY,474
2
2
  reydb/rall.py,sha256=i6-ph2cahhTTF17DjUYf6KX3DNxhEvy6iedlSeF4ibI,341
3
3
  reydb/rbase.py,sha256=HMhxr7_TyzAusrv5dcc1hf3PmxGWj7m63kKcr5Ikbf4,312
4
4
  reydb/rbuild.py,sha256=ppsaGWTC3S-zwQDj3keL3oF5TddORTuEpfqNElj6thU,32327
5
- reydb/rconn.py,sha256=52k8MceNF6PjAUXIkC6NoDqjhAD_RM2_mueWbMMfoPE,6567
6
- reydb/rdb.py,sha256=YWsHdxYqw8JXYrsJ-a9zS4eimwhcqM8hxpMmjchrpbw,60365
5
+ reydb/rconn.py,sha256=ZZDssqBCb1GQT61xG1N96-wYwISTIlKO1nJkgBuwBlI,6507
6
+ reydb/rdb.py,sha256=VVytmnw07MFC-gc3JzjJC50SJTXA0H-oy_pFtYf_ras,60895
7
7
  reydb/rexec.py,sha256=kc5xSfuEDFW9rcAeJj_x6JWPDSYnHZQXcfSBXFGIhL8,8893
8
- reydb/rfile.py,sha256=YD9EAglrWflWCjTiu9ze-ZKlm9x-WzKkXzfLie1kcAQ,12133
8
+ reydb/rfile.py,sha256=QQBsWB-ulumdmCcOnTOLiOKYFUpBmGWb0IZCI3aCo58,14275
9
9
  reydb/rinfo.py,sha256=NhcFhzp9gQ49EpAqcFYXcG-FMUXu3qii8WGoHxN6UcQ,14250
10
10
  reydb/rparam.py,sha256=EVSz_D4wyiaj5ZKxvdDcECmpbriOUNBBFCzz4bnV6dY,6838
11
- reydb-1.1.22.dist-info/METADATA,sha256=3IDok7XwNpaCmBK1ng9uyk-iF9F5wwTroG8DGg5O4EE,1550
12
- reydb-1.1.22.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
13
- reydb-1.1.22.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
14
- reydb-1.1.22.dist-info/RECORD,,
11
+ reydb-1.1.24.dist-info/METADATA,sha256=9vreMU2KglvIkMrUXQl_ZIRdfNEpTiBZmP0is-Pkg_8,1550
12
+ reydb-1.1.24.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
13
+ reydb-1.1.24.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
14
+ reydb-1.1.24.dist-info/RECORD,,
File without changes