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
9"""defines generic type conversion functions, as used in bind and result
10processors.
11
12They all share one common characteristic: None is passed through unchanged.
13
14"""
15from __future__ import annotations
16
17import typing
18
19from ._py_processors import str_to_datetime_processor_factory # noqa
20from ..util._has_cy import HAS_CYEXTENSION
21
22if typing.TYPE_CHECKING or not HAS_CYEXTENSION:
23 from ._py_processors import int_to_boolean as int_to_boolean
24 from ._py_processors import str_to_date as str_to_date
25 from ._py_processors import str_to_datetime as str_to_datetime
26 from ._py_processors import str_to_time as str_to_time
27 from ._py_processors import (
28 to_decimal_processor_factory as to_decimal_processor_factory,
29 )
30 from ._py_processors import to_float as to_float
31 from ._py_processors import to_str as to_str
32else:
33 from sqlalchemy.cyextension.processors import (
34 DecimalResultProcessor,
35 )
36 from sqlalchemy.cyextension.processors import ( # noqa: F401
37 int_to_boolean as int_to_boolean,
38 )
39 from sqlalchemy.cyextension.processors import ( # noqa: F401,E501
40 str_to_date as str_to_date,
41 )
42 from sqlalchemy.cyextension.processors import ( # noqa: F401
43 str_to_datetime as str_to_datetime,
44 )
45 from sqlalchemy.cyextension.processors import ( # noqa: F401,E501
46 str_to_time as str_to_time,
47 )
48 from sqlalchemy.cyextension.processors import ( # noqa: F401,E501
49 to_float as to_float,
50 )
51 from sqlalchemy.cyextension.processors import ( # noqa: F401,E501
52 to_str as to_str,
53 )
54
55 def to_decimal_processor_factory(target_class, scale):
56 # Note that the scale argument is not taken into account for integer
57 # values in the C implementation while it is in the Python one.
58 # For example, the Python implementation might return
59 # Decimal('5.00000') whereas the C implementation will
60 # return Decimal('5'). These are equivalent of course.
61 return DecimalResultProcessor(target_class, "%%.%df" % scale).process