reydb 1.1.44__py3-none-any.whl → 1.1.46__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 +6 -100
- reydb/rdb.py +59 -57
- {reydb-1.1.44.dist-info → reydb-1.1.46.dist-info}/METADATA +1 -1
- {reydb-1.1.44.dist-info → reydb-1.1.46.dist-info}/RECORD +6 -6
- {reydb-1.1.44.dist-info → reydb-1.1.46.dist-info}/WHEEL +0 -0
- {reydb-1.1.44.dist-info → reydb-1.1.46.dist-info}/licenses/LICENSE +0 -0
reydb/rconn.py
CHANGED
@@ -9,15 +9,9 @@
|
|
9
9
|
"""
|
10
10
|
|
11
11
|
|
12
|
-
from typing import
|
12
|
+
from typing import Self
|
13
13
|
from sqlalchemy.engine.base import Connection
|
14
14
|
from sqlalchemy.sql.elements import TextClause
|
15
|
-
from sqlalchemy.exc import OperationalError
|
16
|
-
from reykit.rbase import get_first_notnone
|
17
|
-
from reykit.rdata import objs_in
|
18
|
-
from reykit.rstdout import echo
|
19
|
-
from reykit.rtable import TableData, Table
|
20
|
-
from reykit.rwrap import wrap_runtime, wrap_retry
|
21
15
|
|
22
16
|
from .rdb import Result, Database
|
23
17
|
|
@@ -51,7 +45,6 @@ class DatabaseConnection(Database):
|
|
51
45
|
self.connection = connection
|
52
46
|
self.rdatabase = rdatabase
|
53
47
|
self.begin = None
|
54
|
-
self.begin_count = 0
|
55
48
|
self.username = rdatabase.username
|
56
49
|
self.password = rdatabase.password
|
57
50
|
self.host = rdatabase.host
|
@@ -59,12 +52,10 @@ class DatabaseConnection(Database):
|
|
59
52
|
self.database = rdatabase.database
|
60
53
|
self.query = rdatabase.query
|
61
54
|
self.pool_recycle = rdatabase.pool_recycle
|
62
|
-
self.retry = rdatabase.retry
|
63
55
|
|
64
56
|
|
65
57
|
def executor(
|
66
58
|
self,
|
67
|
-
connection: Connection,
|
68
59
|
sql: TextClause,
|
69
60
|
data: list[dict],
|
70
61
|
report: bool
|
@@ -74,7 +65,6 @@ class DatabaseConnection(Database):
|
|
74
65
|
|
75
66
|
Parameters
|
76
67
|
----------
|
77
|
-
connection : Connection object.
|
78
68
|
sql : TextClause object.
|
79
69
|
data : Data set for filling.
|
80
70
|
report : Whether report SQL execute information.
|
@@ -84,101 +74,19 @@ class DatabaseConnection(Database):
|
|
84
74
|
Result object.
|
85
75
|
"""
|
86
76
|
|
87
|
-
# Create
|
88
|
-
if self.
|
89
|
-
self.
|
90
|
-
self.begin = connection.begin()
|
77
|
+
# Create transaction.
|
78
|
+
if self.begin is None:
|
79
|
+
self.begin = self.connection.begin()
|
91
80
|
|
92
81
|
# Execute.
|
93
82
|
|
94
83
|
## Report.
|
95
84
|
if report:
|
96
|
-
|
97
|
-
result, report_runtime, *_ = execute(sql, data)
|
98
|
-
report_info = (
|
99
|
-
f'{report_runtime}\n'
|
100
|
-
f'Row Count: {result.rowcount}'
|
101
|
-
)
|
102
|
-
sqls = [
|
103
|
-
sql_part.strip()
|
104
|
-
for sql_part in sql.text.split(';')
|
105
|
-
if sql_part != ''
|
106
|
-
]
|
107
|
-
if data == []:
|
108
|
-
echo(report_info, *sqls, title='SQL')
|
109
|
-
else:
|
110
|
-
echo(report_info, *sqls, data, title='SQL')
|
85
|
+
result = self.executor_report(self.connection, sql, data)
|
111
86
|
|
112
87
|
## Not report.
|
113
88
|
else:
|
114
|
-
result = connection.execute(sql, data)
|
115
|
-
|
116
|
-
# Count.
|
117
|
-
syntaxes = self.get_syntax(sql)
|
118
|
-
if objs_in(syntaxes, 'INSERT', 'UPDATE', 'DELETE'):
|
119
|
-
self.begin_count += 1
|
120
|
-
|
121
|
-
return result
|
122
|
-
|
123
|
-
|
124
|
-
def execute(
|
125
|
-
self,
|
126
|
-
sql: str | TextClause,
|
127
|
-
data: TableData | None = None,
|
128
|
-
report: bool | None = None,
|
129
|
-
**kwdata: Any
|
130
|
-
) -> Result:
|
131
|
-
"""
|
132
|
-
Execute SQL.
|
133
|
-
|
134
|
-
Parameters
|
135
|
-
----------
|
136
|
-
sql : SQL in method `sqlalchemy.text` format, or `TextClause` object.
|
137
|
-
data : Data set for filling.
|
138
|
-
report : Whether report SQL execute information.
|
139
|
-
- `None`: Use attribute `default_report`.
|
140
|
-
- `bool`: Use this value.
|
141
|
-
kwdata : Keyword parameters for filling.
|
142
|
-
|
143
|
-
Returns
|
144
|
-
-------
|
145
|
-
Result object.
|
146
|
-
"""
|
147
|
-
|
148
|
-
# Handle parameter by priority.
|
149
|
-
report = get_first_notnone(report, self.default_report)
|
150
|
-
|
151
|
-
# Handle parameter.
|
152
|
-
sql = self.handle_sql(sql)
|
153
|
-
if data is None:
|
154
|
-
if kwdata == {}:
|
155
|
-
data = []
|
156
|
-
else:
|
157
|
-
data = [kwdata]
|
158
|
-
else:
|
159
|
-
data_table = Table(data)
|
160
|
-
data = data_table.to_table()
|
161
|
-
for row in data:
|
162
|
-
row.update(kwdata)
|
163
|
-
data = self.handle_data(data, sql)
|
164
|
-
|
165
|
-
# Execute.
|
166
|
-
|
167
|
-
## Can retry.
|
168
|
-
if (
|
169
|
-
self.retry
|
170
|
-
and self.begin_count == 0
|
171
|
-
and not self.is_multi_sql(sql)
|
172
|
-
):
|
173
|
-
text = 'Retrying...'
|
174
|
-
title = 'Database Execute Operational Error'
|
175
|
-
handler = lambda exc_text, *_: echo(exc_text, text, title=title, frame='top')
|
176
|
-
executor = wrap_retry(self.executor, handler=handler, exception=OperationalError)
|
177
|
-
result = executor(self.connection, sql, data, report)
|
178
|
-
|
179
|
-
## Cannot retry.
|
180
|
-
else:
|
181
|
-
result = self.executor(self.connection, sql, data, report)
|
89
|
+
result = self.connection.execute(sql, data)
|
182
90
|
|
183
91
|
return result
|
184
92
|
|
@@ -192,7 +100,6 @@ class DatabaseConnection(Database):
|
|
192
100
|
if self.begin is not None:
|
193
101
|
self.begin.commit()
|
194
102
|
self.begin = None
|
195
|
-
self.begin_count = 0
|
196
103
|
|
197
104
|
|
198
105
|
def rollback(self) -> None:
|
@@ -204,7 +111,6 @@ class DatabaseConnection(Database):
|
|
204
111
|
if self.begin is not None:
|
205
112
|
self.begin.rollback()
|
206
113
|
self.begin = None
|
207
|
-
self.begin_count = 0
|
208
114
|
|
209
115
|
|
210
116
|
def close(self) -> None:
|
reydb/rdb.py
CHANGED
@@ -17,15 +17,14 @@ from pymysql.constants.CLIENT import MULTI_STATEMENTS
|
|
17
17
|
from sqlalchemy import create_engine as sqlalchemy_create_engine, text as sqlalchemy_text
|
18
18
|
from sqlalchemy.engine.base import Engine, Connection
|
19
19
|
from sqlalchemy.sql.elements import TextClause
|
20
|
-
from
|
21
|
-
from reykit.rbase import throw, is_iterable, get_first_notnone
|
20
|
+
from reykit.rbase import throw, get_first_notnone
|
22
21
|
from reykit.rdata import Generator, to_json
|
23
22
|
from reykit.rmonkey import monkey_sqlalchemy_result_more_fetch, monkey_sqlalchemy_row_index_field
|
24
23
|
from reykit.rre import search, findall
|
25
24
|
from reykit.rstdout import echo
|
26
25
|
from reykit.rtable import TableData, Table
|
27
26
|
from reykit.rtext import join_data_text
|
28
|
-
from reykit.rwrap import wrap_runtime
|
27
|
+
from reykit.rwrap import wrap_runtime
|
29
28
|
|
30
29
|
from .rbase import DatabaseBase, extract_url
|
31
30
|
|
@@ -72,7 +71,6 @@ class Database(DatabaseBase):
|
|
72
71
|
max_overflow: int = 10,
|
73
72
|
pool_timeout: float = 30.0,
|
74
73
|
pool_recycle: int | None = None,
|
75
|
-
retry: bool = False,
|
76
74
|
**query: str
|
77
75
|
) -> None: ...
|
78
76
|
|
@@ -85,7 +83,6 @@ class Database(DatabaseBase):
|
|
85
83
|
max_overflow: int = 10,
|
86
84
|
pool_timeout: float = 30.0,
|
87
85
|
pool_recycle: int | None = None,
|
88
|
-
retry: bool = False,
|
89
86
|
**query: str
|
90
87
|
) -> None: ...
|
91
88
|
|
@@ -97,7 +94,6 @@ class Database(DatabaseBase):
|
|
97
94
|
max_overflow: int = 10,
|
98
95
|
pool_timeout: float = 30.0,
|
99
96
|
pool_recycle: int | None = None,
|
100
|
-
retry: bool = False,
|
101
97
|
**query: str
|
102
98
|
) -> None: ...
|
103
99
|
|
@@ -113,7 +109,6 @@ class Database(DatabaseBase):
|
|
113
109
|
max_overflow: int = 10,
|
114
110
|
pool_timeout: float = 30.0,
|
115
111
|
pool_recycle: int | None = None,
|
116
|
-
retry: bool = False,
|
117
112
|
**query: str
|
118
113
|
) -> None:
|
119
114
|
"""
|
@@ -136,7 +131,6 @@ class Database(DatabaseBase):
|
|
136
131
|
When is local database file, then is `-1`.
|
137
132
|
- `Literal[-1]`: No recycle.
|
138
133
|
- `int`: Use this value.
|
139
|
-
retry : Whether retry execute.
|
140
134
|
query : Remote server database parameters.
|
141
135
|
"""
|
142
136
|
|
@@ -157,7 +151,6 @@ class Database(DatabaseBase):
|
|
157
151
|
self.pool_recycle = -1
|
158
152
|
else:
|
159
153
|
self.pool_recycle = pool_recycle
|
160
|
-
self.retry = retry
|
161
154
|
self.query = query
|
162
155
|
|
163
156
|
# Create engine.
|
@@ -475,11 +468,8 @@ class Database(DatabaseBase):
|
|
475
468
|
for key in sql_keys:
|
476
469
|
value = row.get(key)
|
477
470
|
|
478
|
-
#
|
479
|
-
if
|
480
|
-
not bool(value)
|
481
|
-
and is_iterable(value)
|
482
|
-
):
|
471
|
+
# Empty string.
|
472
|
+
if value == '':
|
483
473
|
value = None
|
484
474
|
|
485
475
|
# Convert.
|
@@ -548,10 +538,51 @@ class Database(DatabaseBase):
|
|
548
538
|
return False
|
549
539
|
|
550
540
|
|
551
|
-
def
|
541
|
+
def executor_report(
|
552
542
|
self,
|
553
543
|
connection: Connection,
|
554
544
|
sql: TextClause,
|
545
|
+
data: list[dict]
|
546
|
+
) -> Result:
|
547
|
+
"""
|
548
|
+
SQL executor and report SQL execute information
|
549
|
+
|
550
|
+
Parameters
|
551
|
+
----------
|
552
|
+
connection : Connection object.
|
553
|
+
sql : TextClause object.
|
554
|
+
data : Data set for filling.
|
555
|
+
|
556
|
+
Returns
|
557
|
+
-------
|
558
|
+
Result object.
|
559
|
+
"""
|
560
|
+
|
561
|
+
# Execute.
|
562
|
+
execute = wrap_runtime(connection.execute, to_return=True)
|
563
|
+
result, report_runtime, *_ = execute(sql, data)
|
564
|
+
|
565
|
+
# Report.
|
566
|
+
report_info = (
|
567
|
+
f'{report_runtime}\n'
|
568
|
+
f'Row Count: {result.rowcount}'
|
569
|
+
)
|
570
|
+
sqls = [
|
571
|
+
sql_part.strip()
|
572
|
+
for sql_part in sql.text.split(';')
|
573
|
+
if sql_part != ''
|
574
|
+
]
|
575
|
+
if data == []:
|
576
|
+
echo(report_info, *sqls, title='SQL')
|
577
|
+
else:
|
578
|
+
echo(report_info, *sqls, data, title='SQL')
|
579
|
+
|
580
|
+
return result
|
581
|
+
|
582
|
+
|
583
|
+
def executor(
|
584
|
+
self,
|
585
|
+
sql: TextClause,
|
555
586
|
data: list[dict],
|
556
587
|
report: bool
|
557
588
|
) -> Result:
|
@@ -560,7 +591,6 @@ class Database(DatabaseBase):
|
|
560
591
|
|
561
592
|
Parameters
|
562
593
|
----------
|
563
|
-
connection : Connection object.
|
564
594
|
sql : TextClause object.
|
565
595
|
data : Data set for filling.
|
566
596
|
report : Whether report SQL execute information.
|
@@ -570,32 +600,21 @@ class Database(DatabaseBase):
|
|
570
600
|
Result object.
|
571
601
|
"""
|
572
602
|
|
573
|
-
# Create
|
574
|
-
with
|
603
|
+
# Create connection.
|
604
|
+
with self.engine.connect() as connection:
|
575
605
|
|
576
|
-
#
|
606
|
+
# Create transaction.
|
607
|
+
with connection.begin():
|
577
608
|
|
578
|
-
|
579
|
-
if report:
|
580
|
-
execute = wrap_runtime(connection.execute, to_return=True)
|
581
|
-
result, report_runtime, *_ = execute(sql, data)
|
582
|
-
report_info = (
|
583
|
-
f'{report_runtime}\n'
|
584
|
-
f'Row Count: {result.rowcount}'
|
585
|
-
)
|
586
|
-
sqls = [
|
587
|
-
sql_part.strip()
|
588
|
-
for sql_part in sql.text.split(';')
|
589
|
-
if sql_part != ''
|
590
|
-
]
|
591
|
-
if data == []:
|
592
|
-
echo(report_info, *sqls, title='SQL')
|
593
|
-
else:
|
594
|
-
echo(report_info, *sqls, data, title='SQL')
|
609
|
+
# Execute.
|
595
610
|
|
596
|
-
|
597
|
-
|
598
|
-
|
611
|
+
## Report.
|
612
|
+
if report:
|
613
|
+
result = self.executor_report(connection, sql, data)
|
614
|
+
|
615
|
+
## Not report.
|
616
|
+
else:
|
617
|
+
result = connection.execute(sql, data)
|
599
618
|
|
600
619
|
return result
|
601
620
|
|
@@ -642,24 +661,7 @@ class Database(DatabaseBase):
|
|
642
661
|
data = self.handle_data(data, sql)
|
643
662
|
|
644
663
|
# Execute.
|
645
|
-
|
646
|
-
## Create Connection object.
|
647
|
-
with self.engine.connect() as connection:
|
648
|
-
|
649
|
-
## Can retry.
|
650
|
-
if (
|
651
|
-
self.retry
|
652
|
-
and not self.is_multi_sql(sql)
|
653
|
-
):
|
654
|
-
text = 'Retrying...'
|
655
|
-
title = 'Database Execute Operational Error'
|
656
|
-
handler = lambda exc_text, *_: echo(exc_text, text, title=title, frame='top')
|
657
|
-
executor = wrap_retry(self.executor, handler=handler, exception=OperationalError)
|
658
|
-
result = executor(self.connection, sql, data, report)
|
659
|
-
|
660
|
-
## Cannot retry.
|
661
|
-
else:
|
662
|
-
result = self.executor(connection, sql, data, report)
|
664
|
+
result = self.executor(sql, data, report)
|
663
665
|
|
664
666
|
return result
|
665
667
|
|
@@ -3,14 +3,14 @@ reydb/rall.py,sha256=UWnbtl4oG4YqXyqTMN_5uqE-QqD5nb_-dvarotlTUeU,388
|
|
3
3
|
reydb/rbase.py,sha256=HYoFasGBAhXUUHNFP1doGnHBW3-xkgRc32O9KppGSak,4824
|
4
4
|
reydb/rbuild.py,sha256=iXJ76BNlUflJfMAAEbPuQLyVePyye8aDq7MaXD16Ktw,32419
|
5
5
|
reydb/rconfig.py,sha256=akY9zDQ7chmA5Hkf7Q7F_Oqqjn6gDk9B88dyLQEYMak,12886
|
6
|
-
reydb/rconn.py,sha256=
|
7
|
-
reydb/rdb.py,sha256=
|
6
|
+
reydb/rconn.py,sha256=jjcwxiBkE2h1nNvvLHZRXdIS-gZGUjFgkIMSP1CAmOc,3159
|
7
|
+
reydb/rdb.py,sha256=iThWa2afN0NUkrcsp4_JXGJwPsyp6Xu-l3Qsl5xxaeY,54288
|
8
8
|
reydb/rerror.py,sha256=UFz9RnuQX83Ki-fAv9ihrRorVMKlUbawaxAwhnS-dVw,10232
|
9
9
|
reydb/rexec.py,sha256=dGdRkG1XR0Z66T0r4nPCSdQzSRWc_Q3t6TPSSrDTIxY,9042
|
10
10
|
reydb/rfile.py,sha256=kFKcExyU8PjWRTr6j7ZPB4qJqFMdYNDB77Nkpw4vHcE,15519
|
11
11
|
reydb/rinfo.py,sha256=KXTkcpTGAD3p9RVKKcnmc_FjJtiKRPk-K5ZepPOnphQ,15253
|
12
12
|
reydb/rparam.py,sha256=6cnSjNlX54iPS1uxMQdpazPM5XL4J87vVgfL6CIYG3U,7031
|
13
|
-
reydb-1.1.
|
14
|
-
reydb-1.1.
|
15
|
-
reydb-1.1.
|
16
|
-
reydb-1.1.
|
13
|
+
reydb-1.1.46.dist-info/METADATA,sha256=HnqlMGyE-v5bC4uW7n60v4p_Y17hpg-4UakaTpWhOpo,1550
|
14
|
+
reydb-1.1.46.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
15
|
+
reydb-1.1.46.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
|
16
|
+
reydb-1.1.46.dist-info/RECORD,,
|
File without changes
|
File without changes
|