|
2 | 2 |
|
3 | 3 | import datetime |
4 | 4 | import decimal |
5 | | -import re |
6 | 5 | from abc import ABC, abstractmethod |
7 | 6 | from collections import OrderedDict, namedtuple |
8 | 7 | from collections.abc import Iterable |
@@ -388,34 +387,26 @@ def inject_parameters(operation: str, parameters: Dict[str, str]): |
388 | 387 | return operation % parameters |
389 | 388 |
|
390 | 389 |
|
391 | | -def transform_paramstyle(operation: str) -> str: |
| 390 | +def transform_paramstyle(operation: str, parameters: Dict[str, Any]) -> str: |
392 | 391 | """ |
393 | | - Performs a regex substitution such that any occurence of `%(param)s` will be replaced with `:param` |
| 392 | + Performs a Python string interpolation such that any occurence of `%(param)s` will be replaced with `:param` |
394 | 393 |
|
395 | 394 | This utility function is built to assist users in the transition between the default paramstyle in |
396 | 395 | this connector prior to version 3.0.0 (`pyformat`) and the new default paramstyle (`named`). |
397 | 396 |
|
| 397 | + This method will fail if parameters is passed as a list. |
| 398 | +
|
398 | 399 | Args: |
399 | 400 | operation (str): The operation or SQL text to transform. |
| 401 | + parameters (Dict[str, Any]): The parameters to use for the transformation. |
400 | 402 |
|
401 | 403 | Returns: |
402 | 404 | str |
403 | 405 | """ |
404 | 406 |
|
405 | | - _output_operation = operation |
406 | | - |
407 | | - PYFORMAT_PARAMSTYLE_REGEX = r"%\((\w+)\)s" |
408 | | - pat = re.compile(PYFORMAT_PARAMSTYLE_REGEX) |
409 | | - NAMED_PARAMSTYLE_FMT = ":{}" |
410 | | - PYFORMAT_PARAMSTYLE_FMT = "%({})s" |
411 | | - |
412 | | - pyformat_markers = pat.findall(operation) |
413 | | - for marker in pyformat_markers: |
414 | | - pyformat_marker = PYFORMAT_PARAMSTYLE_FMT.format(marker) |
415 | | - named_marker = NAMED_PARAMSTYLE_FMT.format(marker) |
416 | | - _output_operation = _output_operation.replace(pyformat_marker, named_marker) |
| 407 | + interpolation_values = {key: f":{key}" for key in parameters.keys()} |
417 | 408 |
|
418 | | - return _output_operation |
| 409 | + return operation % interpolation_values |
419 | 410 |
|
420 | 411 |
|
421 | 412 | def create_arrow_table_from_arrow_file(file_bytes: bytes, description) -> pyarrow.Table: |
|
0 commit comments