processors.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # engine/processors.py
  2. # Copyright (C) 2010-2025 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. # Copyright (C) 2010 Gaetan de Menten gdementen@gmail.com
  5. #
  6. # This module is part of SQLAlchemy and is released under
  7. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  8. """defines generic type conversion functions, as used in bind and result
  9. processors.
  10. They all share one common characteristic: None is passed through unchanged.
  11. """
  12. from __future__ import annotations
  13. import typing
  14. from ._py_processors import str_to_datetime_processor_factory # noqa
  15. from ..util._has_cy import HAS_CYEXTENSION
  16. if typing.TYPE_CHECKING or not HAS_CYEXTENSION:
  17. from ._py_processors import int_to_boolean as int_to_boolean
  18. from ._py_processors import str_to_date as str_to_date
  19. from ._py_processors import str_to_datetime as str_to_datetime
  20. from ._py_processors import str_to_time as str_to_time
  21. from ._py_processors import (
  22. to_decimal_processor_factory as to_decimal_processor_factory,
  23. )
  24. from ._py_processors import to_float as to_float
  25. from ._py_processors import to_str as to_str
  26. else:
  27. from sqlalchemy.cyextension.processors import (
  28. DecimalResultProcessor,
  29. )
  30. from sqlalchemy.cyextension.processors import ( # noqa: F401
  31. int_to_boolean as int_to_boolean,
  32. )
  33. from sqlalchemy.cyextension.processors import ( # noqa: F401,E501
  34. str_to_date as str_to_date,
  35. )
  36. from sqlalchemy.cyextension.processors import ( # noqa: F401
  37. str_to_datetime as str_to_datetime,
  38. )
  39. from sqlalchemy.cyextension.processors import ( # noqa: F401,E501
  40. str_to_time as str_to_time,
  41. )
  42. from sqlalchemy.cyextension.processors import ( # noqa: F401,E501
  43. to_float as to_float,
  44. )
  45. from sqlalchemy.cyextension.processors import ( # noqa: F401,E501
  46. to_str as to_str,
  47. )
  48. def to_decimal_processor_factory(target_class, scale):
  49. # Note that the scale argument is not taken into account for integer
  50. # values in the C implementation while it is in the Python one.
  51. # For example, the Python implementation might return
  52. # Decimal('5.00000') whereas the C implementation will
  53. # return Decimal('5'). These are equivalent of course.
  54. return DecimalResultProcessor(target_class, "%%.%df" % scale).process