mysqlengine 1.0.0__cp310-cp310-macosx_10_9_universal2.whl → 1.0.1__cp310-cp310-macosx_10_9_universal2.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.

Potentially problematic release.


This version of mysqlengine might be problematic. Click here for more details.

Binary file
mysqlengine/element.py CHANGED
@@ -136,19 +136,16 @@ class Element(ObjStr):
136
136
  # Acquire / Transaction / Fill / Release -----------------------------------------------
137
137
  @cython.ccall
138
138
  def acquire(self) -> PoolConnectionManager:
139
- """Acquire a free connection from the connection pool
140
- through context manager `<'PoolConnectionManager'>`.
139
+ """Acquire a free connection from the pool through context manager `<'PoolConnectionManager'>`.
141
140
 
142
141
  ## Notice
143
- - On acquisition, the following session settings are reset to the pool's defaults:
144
- 'autocommit', 'used_decimal', 'decode_bit', 'decode_json'.
145
- - If you use any `set_*()` methods to change charset or timeouts
146
- (read_timeout, write_timeout, wait_timeout, interactive_timeout,
147
- lock_wait_timeout, execution_timeout), those will be reverted to
148
- pool defaults on release.
149
- - Any other session-level changes (e.g. via SQL statements) will break
150
- the pool connections consistency. Please call `conn.schedule_close()`
151
- before releasing the connection back to the pool.
142
+ - **On Acquisition**: The following session settings are reset to the pool's defaults:
143
+ `autocommit`, `used_decimal`, `decode_bit`, `decode_json`.
144
+ - **At Release**: Any changes made vis `set_*()` methods (e.g. `set_charset()`,
145
+ `set_read_timeout()`, etc.) will be reverted back to the pool defaults.
146
+ - **Consistency**: Any other session-level changes (e.g. via SQL statements) will
147
+ break the pool connection consistency. Please call `Connection.schedule_close()`
148
+ before exiting the context.
152
149
 
153
150
  ## Example (sync):
154
151
  >>> with element.acquire() as conn:
@@ -164,38 +161,30 @@ class Element(ObjStr):
164
161
 
165
162
  @cython.ccall
166
163
  def transaction(self) -> PoolTransactionManager:
167
- """Acquire a free connection from the connection pool in
168
- `TRANSACTION` mode through context manager `<'PoolTransactionManager'>`.
164
+ """Acquire a free connection from the pool in TRANSACTION mode
165
+ through context manager `<'PoolTransactionManager'>`.
169
166
 
170
- ## Explanation
171
- By acquiring connection through this method, the following happens:
167
+ ## On enter
172
168
  - 1. Acquire a free connection from the pool.
173
- - 2. Use the connection to `START` a transaction.
174
- - 3. Returns the connection to the user.
175
- - 4a. If the transaction executed successfully, execute `COMMIT`
176
- and release the connection back to the pool.
177
- - 4b. If catches ANY exceptions during the transaction, close
178
- the connection when release back to the pool.
169
+ - 2. Calls `BEGIN` on the connection
170
+
171
+ ## On exit
172
+ - If no exception occurs: calls `COMMIT` and releases the connection back to the pool for reuse.
173
+ - If an exception occurs: Schedules the connection for closure and releases it back to the pool.
179
174
 
180
175
  ## Notice
181
- - On acquisition, the following session settings are reset to the pool's defaults:
182
- 'autocommit', 'used_decimal', 'decode_bit', 'decode_json'.
183
- - If you use any `set_*()` methods to change charset or timeouts
184
- (read_timeout, write_timeout, wait_timeout, interactive_timeout,
185
- lock_wait_timeout, execution_timeout), those will be reverted to
186
- pool defaults on release.
187
- - Any other session-level changes (e.g. via SQL statements) will break
188
- the pool connections consistency. Please call `conn.schedule_close()`
189
- before releasing the connection back to the pool.
176
+ - **On Acquisition**: The following session settings are reset to the pool's defaults:
177
+ `autocommit`, `used_decimal`, `decode_bit`, `decode_json`.
178
+ - **At Release**: Any changes made vis `set_*()` methods (e.g. `set_charset()`,
179
+ `set_read_timeout()`, etc.) will be reverted back to the pool defaults.
180
+ - **Consistency**: Any other session-level changes (e.g. via SQL statements) will
181
+ break the pool connection consistency. Please call `Connection.schedule_close()`
182
+ before exiting the context.
190
183
 
191
184
  ## Example (sync):
192
185
  >>> with element.transaction() as conn:
193
186
  with conn.cursor() as cur:
194
187
  cur.execute("INSERT INTO tb (id, name) VALUES (1, 'test')")
195
- # Equivalent to:
196
- BEGIN;
197
- INSERT INTO tb (id, name) VALUES (1, 'test');
198
- COMMIT;
199
188
 
200
189
  ## Example (async):
201
190
  >>> async with element.transaction() as conn:
@@ -209,36 +198,39 @@ class Element(ObjStr):
209
198
  return self._pool.transaction()
210
199
 
211
200
  async def fill(self, num: int = 1) -> None:
212
- """Fill the pool with new asynchronous connections.
201
+ """Fill the pool with new [async] connections.
213
202
 
214
203
  :param num `<'int'>`: Number of new [async] connections to create. Defaults to `1`.
215
- - If 'num' plus the existing [async] connections in the pool exceeds the
216
- maximum pool size, pool is only filled up to the 'max_size' limit.
217
- - If 'num=-1', the pool is filled up to the 'min_size' limit.
204
+
205
+ - If 'num' plus the total [async] connections in the pool exceeds the
206
+ maximum pool size, only fills up to the `Pool.max_size` limit.
207
+ - If 'num=-1', fills up to the `Pool.min_size` limit.
218
208
  """
219
209
  return await self._pool.fill(num)
220
210
 
221
211
  @cython.ccall
222
212
  def release(self, conn: PoolConnection | PoolSyncConnection) -> object:
223
- """Release a connection back to the pool `<'Future'>`.
213
+ """Release a connection back to the pool `<'Task[None]'>`.
224
214
 
225
- Use this method only when you acquire a connection manually. Connections
226
- obtained via 'acquire()' or 'transaction()' are released automatically
227
- by their context managers.
215
+ - Use this method `ONLY` when you directly acquired a connection without the context manager.
216
+ - Connections obtained via context manager are released automatically on exits.
228
217
 
229
218
  :param conn `<'PoolConnection/PoolSyncConnection'>`: The pool [sync/async] connection to release.
230
- :raises `<'PoolReleaseError'>`: If the connection does not belong to the connection pool.
231
219
 
232
- ## Notice
233
- - On acquisition, the following session settings are reset to the pool's defaults:
234
- 'autocommit', 'used_decimal', 'decode_bit', 'decode_json'.
235
- - If you use any `set_*()` methods to change charset or timeouts
236
- (read_timeout, write_timeout, wait_timeout, interactive_timeout,
237
- lock_wait_timeout, execution_timeout), those will be reverted to
238
- pool defaults on release.
239
- - Any other session-level changes (e.g. via SQL statements) will break
240
- the pool connections consistency. Please call `conn.schedule_close()`
241
- before calling the 'release()' method.
220
+ :returns `<'Task[None]'>`: An `asyncio.Task` that resolves once the connection is released.
221
+
222
+ - For a [sync] connection, the returned `Task` can be ignored,
223
+ as the connection is released immediately.
224
+ - For an [async] connection, the returned `Task` must be awaited
225
+ to ensure the connection is properly handled.
226
+
227
+ :raises `<'PoolReleaseError'>`: If the connection does not belong to the pool.
228
+
229
+ ## Example (sync):
230
+ >>> element.release(sync_conn) # immediate release
231
+
232
+ ## Example (async):
233
+ >>> await element.release(async_conn) # 'await' for release
242
234
  """
243
235
  return self._pool.release(conn)
244
236
 
@@ -974,18 +966,38 @@ class Element(ObjStr):
974
966
  # Utils --------------------------------------------------------------------------------
975
967
  @cython.cfunc
976
968
  @cython.inline(True)
977
- def _escape_args(self, args: object | None, itemize: cython.bint = True) -> object:
978
- """(internal) Escape the arguments `<'str/tuple/list'>`.
979
-
980
- :param args `<'list/tuple/DataFrame/Any'>`: The arguments to escape.
981
- :param itemize `<'bool'>`: Whether to escape each items of the 'args' individually. Defaults to True.
982
- - **'itemize=True'**: the 'args' type determines how to escape.
983
- * 1. Sequence or Mapping (e.g. list, tuple, dict, etc) escapes to <'tuple[str]'>.
984
- * 2. pd.Series and 1-dimensional np.ndarray escapes to <'tuple[str]'>.
985
- * 3. pd.DataFrame and 2-dimensional np.ndarray escapes to <'list[tuple[str]]'>.
986
- Each tuple represents one row of the 'args' .
987
- * 4. Single object (such as int, float, str, etc) escapes to one literal string <'str'>.
988
- - **'itemize=False'**: regardless of the 'args' type, all escapes to one single literal string <'str'>.
969
+ def _escape_args(self, args: object, itemize: cython.bint = True) -> object:
970
+ """(internal) Prepare and escape arguments for SQL binding `<'str/tuple/list[str/tuple]'>`.
971
+
972
+ :param args `<'Any'>`: Arguments to escape, supports:
973
+
974
+ - **Python built-ins**:
975
+ int, float, bool, str, None, datetime, date, time,
976
+ timedelta, struct_time, bytes, bytearray, memoryview,
977
+ Decimal, dict, list, tuple, set, frozenset, range
978
+ - **Library [numpy](https://github.com/numpy/numpy)**:
979
+ np.int, np.uint, np.float, np.bool, np.bytes,
980
+ np.str, np.datetime64, np.timedelta64, np.ndarray
981
+ - **Library [pandas](https://github.com/pandas-dev/pandas)**:
982
+ pd.Timestamp, pd.Timedelta, pd.DatetimeIndex,
983
+ pd.TimedeltaIndex, pd.Series, pd.DataFrame
984
+ - **Library [cytimes](https://github.com/AresJef/cyTimes)**:
985
+ cytimes.Pydt, cytimes.Pddt
986
+
987
+ :param itemize `<'bool'>`: Whether to escape items of the 'args' individually. Defaults to `True`.
988
+ - `itemize=False`: Always escapes to one single literal string `<'str'>`, regardless of the 'args' type.
989
+ - `itemize=True`: The 'args' data type determines how escape is done.
990
+ - 1. Sequence or Mapping (e.g. `list`, `tuple`, `dict`, etc) escapes to `<'tuple[str]'>`.
991
+ - 2. `pd.Series` and 1-dimensional `np.ndarray` escapes to `<'tuple[str]'>`.
992
+ - 3. `pd.DataFrame` and 2-dimensional `np.ndarray` escapes to `<'list[tuple[str]]'>`.
993
+ - 4. Single object (such as `int`, `float`, `str`, etc) escapes to one literal string `<'str'>`.
994
+
995
+ :returns `<'str/tuple/list'>`:
996
+ - If returns `<'str'>`, it represents a single literal string.
997
+ - If returns `<'tuple'>`, it represents a single row of literal strings.
998
+ - If returns `<'list'>`, it represents multiple rows of literal strings.
999
+
1000
+ :raises `<'EscapeTypeError'>`: If escape fails due to unsupported type.
989
1001
  """
990
1002
  return _escape(args, False, itemize)
991
1003
 
@@ -994,21 +1006,37 @@ class Element(ObjStr):
994
1006
  def _format_sql(
995
1007
  self,
996
1008
  sql: str,
997
- args: object | None,
1009
+ args: object,
998
1010
  itemize: cython.bint = True,
999
1011
  ) -> str:
1000
1012
  """(internal) Format the SQL with the given arguments `<'str'>`.
1001
1013
 
1002
1014
  :param sql `<'str'>`: The SQL statement to format.
1003
- :param args `<'list/tuple/DataFrame/Any'>`: The arguments to escape and incorporate into the SQL statement.
1004
- :param itemize `<'bool'>`: Whether to escape each items of the 'args' individually. Defaults to True.
1005
- - **'itemize=True'**: the 'args' type determines how to escape.
1006
- * 1. Sequence or Mapping (e.g. list, tuple, dict, etc) escapes to <'tuple[str]'>.
1007
- * 2. pd.Series and 1-dimensional np.ndarray escapes to <'tuple[str]'>.
1008
- * 3. pd.DataFrame and 2-dimensional np.ndarray escapes to <'list[tuple[str]]'>.
1009
- Each tuple represents one row of the 'args' .
1010
- * 4. Single object (such as int, float, str, etc) escapes to one literal string <'str'>.
1011
- - **'itemize=False'**: regardless of the 'args' type, all escapes to one single literal string <'str'>.
1015
+
1016
+ :param args `<'Any'>`: Arguments to escape, supports:
1017
+
1018
+ - **Python built-ins**:
1019
+ int, float, bool, str, None, datetime, date, time,
1020
+ timedelta, struct_time, bytes, bytearray, memoryview,
1021
+ Decimal, dict, list, tuple, set, frozenset, range
1022
+ - **Library [numpy](https://github.com/numpy/numpy)**:
1023
+ np.int, np.uint, np.float, np.bool, np.bytes,
1024
+ np.str, np.datetime64, np.timedelta64, np.ndarray
1025
+ - **Library [pandas](https://github.com/pandas-dev/pandas)**:
1026
+ pd.Timestamp, pd.Timedelta, pd.DatetimeIndex,
1027
+ pd.TimedeltaIndex, pd.Series, pd.DataFrame
1028
+ - **Library [cytimes](https://github.com/AresJef/cyTimes)**:
1029
+ cytimes.Pydt, cytimes.Pddt
1030
+
1031
+ :param itemize `<'bool'>`: Whether to escape items of the 'args' individually. Defaults to `True`.
1032
+ - `itemize=False`: Always escapes to one single literal string `<'str'>`, regardless of the 'args' type.
1033
+ - `itemize=True`: The 'args' data type determines how escape is done.
1034
+ - 1. Sequence or Mapping (e.g. `list`, `tuple`, `dict`, etc) escapes to `<'tuple[str]'>`.
1035
+ - 2. `pd.Series` and 1-dimensional `np.ndarray` escapes to `<'tuple[str]'>`.
1036
+ - 3. `pd.DataFrame` and 2-dimensional `np.ndarray` escapes to `<'list[tuple[str]]'>`.
1037
+ - 4. Single object (such as `int`, `float`, `str`, etc) escapes to one literal string `<'str'>`.
1038
+
1039
+ :returns `<'str'>`: The SQL statement formatted with escaped arguments.
1012
1040
  """
1013
1041
  if args is None:
1014
1042
  return sql # exit