14
14
logger = logging .getLogger (__name__ )
15
15
16
16
17
+ def _convert_decimal (
18
+ value : str , precision : Optional [int ] = None , scale : Optional [int ] = None
19
+ ) -> decimal .Decimal :
20
+ """
21
+ Convert a string value to a decimal with optional precision and scale.
22
+
23
+ Args:
24
+ value: The string value to convert
25
+ precision: Optional precision (total number of significant digits) for the decimal
26
+ scale: Optional scale (number of decimal places) for the decimal
27
+
28
+ Returns:
29
+ A decimal.Decimal object with appropriate precision and scale
30
+ """
31
+
32
+ # First create the decimal from the string value
33
+ result = decimal .Decimal (value )
34
+
35
+ # Apply scale (quantize to specific number of decimal places) if specified
36
+ quantizer = None
37
+ if scale is not None :
38
+ quantizer = decimal .Decimal (f'0.{ "0" * scale } ' )
39
+
40
+ # Apply precision (total number of significant digits) if specified
41
+ context = None
42
+ if precision is not None :
43
+ context = decimal .Context (prec = precision )
44
+
45
+ if quantizer is not None :
46
+ result = result .quantize (quantizer , context = context )
47
+
48
+ return result
49
+
50
+
17
51
class SqlType :
18
52
"""
19
53
SQL type constants
@@ -72,13 +106,7 @@ class SqlTypeConverter:
72
106
SqlType .LONG : lambda v : int (v ),
73
107
SqlType .FLOAT : lambda v : float (v ),
74
108
SqlType .DOUBLE : lambda v : float (v ),
75
- SqlType .DECIMAL : lambda v , p = None , s = None : (
76
- decimal .Decimal (v ).quantize (
77
- decimal .Decimal (f'0.{ "0" * s } ' ), context = decimal .Context (prec = p )
78
- )
79
- if p is not None and s is not None
80
- else decimal .Decimal (v )
81
- ),
109
+ SqlType .DECIMAL : _convert_decimal ,
82
110
# Boolean type
83
111
SqlType .BOOLEAN : lambda v : v .lower () in ("true" , "t" , "1" , "yes" , "y" ),
84
112
# Date/Time types
@@ -98,19 +126,17 @@ class SqlTypeConverter:
98
126
99
127
@staticmethod
100
128
def convert_value (
101
- value : Any ,
129
+ value : str ,
102
130
sql_type : str ,
103
- precision : Optional [int ] = None ,
104
- scale : Optional [int ] = None ,
131
+ ** kwargs ,
105
132
) -> Any :
106
133
"""
107
134
Convert a string value to the appropriate Python type based on SQL type.
108
135
109
136
Args:
110
137
value: The string value to convert
111
138
sql_type: The SQL type (e.g., 'int', 'decimal')
112
- precision: Optional precision for decimal types
113
- scale: Optional scale for decimal types
139
+ **kwargs: Additional keyword arguments for the conversion function
114
140
115
141
Returns:
116
142
The converted value in the appropriate Python type
@@ -127,6 +153,8 @@ def convert_value(
127
153
converter_func = SqlTypeConverter .TYPE_MAPPING [sql_type ]
128
154
try :
129
155
if sql_type == SqlType .DECIMAL :
156
+ precision = kwargs .get ("precision" , None )
157
+ scale = kwargs .get ("scale" , None )
130
158
return converter_func (value , precision , scale )
131
159
else :
132
160
return converter_func (value )
0 commit comments