reydb 1.1.45__py3-none-any.whl → 1.1.47__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
@@ -9,15 +9,9 @@
9
9
  """
10
10
 
11
11
 
12
- from typing import Any, Self
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 Transaction object.
88
- if self.begin_count == 0:
89
- self.rollback()
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
- execute = wrap_runtime(connection.execute, to_return=True)
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:
@@ -251,3 +157,21 @@ class DatabaseConnection(Database):
251
157
 
252
158
 
253
159
  __del__ = close
160
+
161
+
162
+ @property
163
+ def insert_id(self) -> int:
164
+ """
165
+ Return last self increasing ID.
166
+
167
+ Returns
168
+ -------
169
+ ID.
170
+ """
171
+
172
+ # Get.
173
+ sql = 'SELECT LAST_INSERT_ID()'
174
+ result = self.execute(sql)
175
+ id_ = result.scalar()
176
+
177
+ return id_
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 sqlalchemy.exc import OperationalError
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, wrap_retry
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.
@@ -545,10 +538,51 @@ class Database(DatabaseBase):
545
538
  return False
546
539
 
547
540
 
548
- def executor(
541
+ def executor_report(
549
542
  self,
550
543
  connection: Connection,
551
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,
552
586
  data: list[dict],
553
587
  report: bool
554
588
  ) -> Result:
@@ -557,7 +591,6 @@ class Database(DatabaseBase):
557
591
 
558
592
  Parameters
559
593
  ----------
560
- connection : Connection object.
561
594
  sql : TextClause object.
562
595
  data : Data set for filling.
563
596
  report : Whether report SQL execute information.
@@ -567,32 +600,21 @@ class Database(DatabaseBase):
567
600
  Result object.
568
601
  """
569
602
 
570
- # Create Transaction object.
571
- with connection.begin():
603
+ # Create connection.
604
+ with self.engine.connect() as connection:
572
605
 
573
- # Execute.
606
+ # Create transaction.
607
+ with connection.begin():
574
608
 
575
- ## Report.
576
- if report:
577
- execute = wrap_runtime(connection.execute, to_return=True)
578
- result, report_runtime, *_ = execute(sql, data)
579
- report_info = (
580
- f'{report_runtime}\n'
581
- f'Row Count: {result.rowcount}'
582
- )
583
- sqls = [
584
- sql_part.strip()
585
- for sql_part in sql.text.split(';')
586
- if sql_part != ''
587
- ]
588
- if data == []:
589
- echo(report_info, *sqls, title='SQL')
590
- else:
591
- echo(report_info, *sqls, data, title='SQL')
609
+ # Execute.
592
610
 
593
- ## Not report.
594
- else:
595
- result = connection.execute(sql, data)
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)
596
618
 
597
619
  return result
598
620
 
@@ -639,24 +661,7 @@ class Database(DatabaseBase):
639
661
  data = self.handle_data(data, sql)
640
662
 
641
663
  # Execute.
642
-
643
- ## Create Connection object.
644
- with self.engine.connect() as connection:
645
-
646
- ## Can retry.
647
- if (
648
- self.retry
649
- and not self.is_multi_sql(sql)
650
- ):
651
- text = 'Retrying...'
652
- title = 'Database Execute Operational Error'
653
- handler = lambda exc_text, *_: echo(exc_text, text, title=title, frame='top')
654
- executor = wrap_retry(self.executor, handler=handler, exception=OperationalError)
655
- result = executor(self.connection, sql, data, report)
656
-
657
- ## Cannot retry.
658
- else:
659
- result = self.executor(connection, sql, data, report)
664
+ result = self.executor(sql, data, report)
660
665
 
661
666
  return result
662
667
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: reydb
3
- Version: 1.1.45
3
+ Version: 1.1.47
4
4
  Summary: Database method set.
5
5
  Project-URL: homepage, https://github.com/reyxbo/reydb/
6
6
  Author-email: Rey <reyxbo@163.com>
@@ -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=YWscFMjBph9ttPXjzTTpaziTd7v7vnE0b7E4mVpJ6oc,6163
7
- reydb/rdb.py,sha256=bHrHWW7i5t4NMGBTRtvEcQAuBOKXchOf6yiOhjEF45U,54709
6
+ reydb/rconn.py,sha256=UyYx-134b8KwlhBl3D3y5PdXrGceL3n9Oc7bk8xWek0,3475
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.45.dist-info/METADATA,sha256=qRlbuMQhG2OxBMYe6q4j46D1Oz6Lp5ImWwHtb62kqCk,1550
14
- reydb-1.1.45.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
15
- reydb-1.1.45.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
16
- reydb-1.1.45.dist-info/RECORD,,
13
+ reydb-1.1.47.dist-info/METADATA,sha256=5jXtKibf68M6FUjL8NKHnQXLWpW5AkWIeLUbkAWcNeY,1550
14
+ reydb-1.1.47.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
15
+ reydb-1.1.47.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
16
+ reydb-1.1.47.dist-info/RECORD,,
File without changes