Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pymysql/converters.py: 31%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import datetime
2import re
3import time
4import warnings
5from decimal import Decimal
7from .constants import FIELD_TYPE
8from .err import ProgrammingError
10# This silly alias is needed for aiomysql compatibility for now. It is scheduled for removal in 1.3.0.
11# https://github.com/PyMySQL/PyMySQL/issues/1274
12escape_dict = escape_bytes_prefixed = "DO NOT IMPORT THIS!!!"
15def escape_item(val, charset=None, mapping=None):
16 if mapping is None:
17 mapping = encoders
18 encoder = mapping.get(type(val))
20 # Fallback to default when no encoder found
21 if not encoder:
22 try:
23 encoder = mapping[str]
24 except KeyError:
25 raise TypeError("no default type converter defined")
27 if encoder == escape_sequence:
28 val = encoder(val, charset, mapping)
29 else:
30 val = encoder(val, mapping)
31 return val
34def escape_not_supported(val, charset=None, mapping=None):
35 raise TypeError(f"{type(val).__name__} can not be used as parameter")
38def escape_sequence(val, charset=None, mapping=None):
39 warnings.warn("escape_sequence is deprecated", DeprecationWarning, stacklevel=2)
40 n = []
41 for item in val:
42 quoted = escape_item(item, charset, mapping)
43 n.append(quoted)
44 return "(" + ",".join(n) + ")"
47def escape_set(val, charset=None, mapping=None):
48 warnings.warn("escape_set is deprecated", DeprecationWarning, stacklevel=2)
49 return ",".join([escape_item(x, charset, mapping) for x in val])
52def escape_bool(value, mapping=None):
53 return str(int(value))
56def escape_int(value, mapping=None):
57 return str(value)
60def escape_float(value, mapping=None):
61 s = repr(value)
62 if s in ("inf", "-inf", "nan"):
63 raise ProgrammingError(f"{s} can not be used with MySQL")
64 if "e" not in s:
65 s += "e0"
66 return s
69_escape_table = [chr(x) for x in range(128)]
70_escape_table[0] = "\\0"
71_escape_table[ord("\\")] = "\\\\"
72_escape_table[ord("\n")] = "\\n"
73_escape_table[ord("\r")] = "\\r"
74_escape_table[ord("\032")] = "\\Z"
75_escape_table[ord('"')] = '\\"'
76_escape_table[ord("'")] = "\\'"
79def escape_string(value, mapping=None):
80 """escapes *value* without adding quote.
82 Value should be unicode
83 """
84 return value.translate(_escape_table)
87def escape_bytes(value, mapping=None) -> str:
88 return "_binary X'%s'" % value.hex()
91def escape_str(value, mapping=None) -> str:
92 return "'" + escape_string(str(value)) + "'"
95def escape_None(value, mapping=None):
96 return "NULL"
99def escape_timedelta(obj, mapping=None):
100 # timedelta normalizes a negative value so that seconds/microseconds are
101 # non-negative and the sign lives entirely in days, so the signed magnitude
102 # must be reconstructed before it is split into hours/minutes/seconds --
103 # otherwise a negative TIME comes out with complemented sub-hour fields.
104 sign = ""
105 if obj.days < 0:
106 sign = "-"
107 obj = abs(obj)
109 micros = obj.microseconds
110 minutes, seconds = divmod(obj.seconds, 60)
111 hours, minutes = divmod(minutes, 60)
112 hours += obj.days * 24
114 if micros:
115 return f"'{sign}{hours:02d}:{minutes:02d}:{seconds:02d}.{micros:06d}'"
116 return f"'{sign}{hours:02d}:{minutes:02d}:{seconds:02d}'"
119def escape_time(obj, mapping=None):
120 if obj.microsecond:
121 fmt = "'{0.hour:02}:{0.minute:02}:{0.second:02}.{0.microsecond:06}'"
122 else:
123 fmt = "'{0.hour:02}:{0.minute:02}:{0.second:02}'"
124 return fmt.format(obj)
127def escape_datetime(obj, mapping=None):
128 if obj.microsecond:
129 fmt = (
130 "'{0.year:04}-{0.month:02}-{0.day:02}"
131 + " {0.hour:02}:{0.minute:02}:{0.second:02}.{0.microsecond:06}'"
132 )
133 else:
134 fmt = "'{0.year:04}-{0.month:02}-{0.day:02} {0.hour:02}:{0.minute:02}:{0.second:02}'"
135 return fmt.format(obj)
138def escape_date(obj, mapping=None):
139 fmt = "'{0.year:04}-{0.month:02}-{0.day:02}'"
140 return fmt.format(obj)
143def escape_struct_time(obj, mapping=None):
144 return escape_datetime(datetime.datetime(*obj[:6]))
147def Decimal2Literal(o, d):
148 if not o.is_finite():
149 raise ProgrammingError("%s can not be used with MySQL" % str(o).lower())
150 return format(o, "f")
153def _convert_second_fraction(s):
154 if not s:
155 return 0
156 # Pad zeros to ensure the fraction length in microseconds
157 s = s.ljust(6, "0")
158 return int(s[:6])
161DATETIME_RE = re.compile(
162 r"(\d{1,4})-(\d{1,2})-(\d{1,2})[T ](\d{1,2}):(\d{1,2}):(\d{1,2})(?:\.(\d{1,6}))?"
163)
166def convert_datetime(obj):
167 """Returns a DATETIME or TIMESTAMP column value as a datetime object:
169 >>> convert_datetime('2007-02-25 23:06:20')
170 datetime.datetime(2007, 2, 25, 23, 6, 20)
171 >>> convert_datetime('2007-02-25T23:06:20')
172 datetime.datetime(2007, 2, 25, 23, 6, 20)
174 Illegal values are returned as str:
176 >>> convert_datetime('2007-02-31T23:06:20')
177 '2007-02-31T23:06:20'
178 >>> convert_datetime('0000-00-00 00:00:00')
179 '0000-00-00 00:00:00'
180 """
181 if isinstance(obj, (bytes, bytearray)):
182 obj = obj.decode("ascii")
184 m = DATETIME_RE.fullmatch(obj)
185 if not m:
186 return convert_date(obj)
188 try:
189 groups = list(m.groups())
190 groups[-1] = _convert_second_fraction(groups[-1])
191 return datetime.datetime(*[int(x) for x in groups])
192 except ValueError:
193 return convert_date(obj)
196TIMEDELTA_RE = re.compile(r"(-)?(\d{1,3}):(\d{1,2}):(\d{1,2})(?:\.(\d{1,6}))?")
199def convert_timedelta(obj):
200 """Returns a TIME column as a timedelta object:
202 >>> convert_timedelta('25:06:17')
203 datetime.timedelta(days=1, seconds=3977)
204 >>> convert_timedelta('-25:06:17')
205 datetime.timedelta(days=-2, seconds=82423)
207 Illegal values are returned as string:
209 >>> convert_timedelta('random crap')
210 'random crap'
212 Note that MySQL always returns TIME columns as (+|-)HH:MM:SS, but
213 can accept values as (+|-)DD HH:MM:SS. The latter format will not
214 be parsed correctly by this function.
215 """
216 if isinstance(obj, (bytes, bytearray)):
217 obj = obj.decode("ascii")
219 m = TIMEDELTA_RE.fullmatch(obj)
220 if not m:
221 return obj
223 try:
224 groups = list(m.groups())
225 groups[-1] = _convert_second_fraction(groups[-1])
226 negate = -1 if groups[0] else 1
227 hours, minutes, seconds, microseconds = groups[1:]
229 tdelta = (
230 datetime.timedelta(
231 hours=int(hours),
232 minutes=int(minutes),
233 seconds=int(seconds),
234 microseconds=int(microseconds),
235 )
236 * negate
237 )
238 return tdelta
239 except ValueError:
240 return obj
243TIME_RE = re.compile(r"(\d{1,2}):(\d{1,2}):(\d{1,2})(?:\.(\d{1,6}))?")
246def convert_time(obj):
247 """Returns a TIME column as a time object:
249 >>> convert_time('15:06:17')
250 datetime.time(15, 6, 17)
252 Illegal values are returned as str:
254 >>> convert_time('-25:06:17')
255 '-25:06:17'
256 >>> convert_time('random crap')
257 'random crap'
259 Note that MySQL always returns TIME columns as (+|-)HH:MM:SS, but
260 can accept values as (+|-)DD HH:MM:SS. The latter format will not
261 be parsed correctly by this function.
263 Also note that MySQL's TIME column corresponds more closely to
264 Python's timedelta and not time. However if you want TIME columns
265 to be treated as time-of-day and not a time offset, then you can
266 use set this function as the converter for FIELD_TYPE.TIME.
267 """
268 if isinstance(obj, (bytes, bytearray)):
269 obj = obj.decode("ascii")
271 m = TIME_RE.fullmatch(obj)
272 if not m:
273 return obj
275 try:
276 groups = list(m.groups())
277 groups[-1] = _convert_second_fraction(groups[-1])
278 hours, minutes, seconds, microseconds = groups
279 return datetime.time(
280 hour=int(hours),
281 minute=int(minutes),
282 second=int(seconds),
283 microsecond=int(microseconds),
284 )
285 except ValueError:
286 return obj
289def convert_date(obj):
290 """Returns a DATE column as a date object:
292 >>> convert_date('2007-02-26')
293 datetime.date(2007, 2, 26)
295 Illegal values are returned as str:
297 >>> convert_date('2007-02-31')
298 '2007-02-31'
299 >>> convert_date('0000-00-00')
300 '0000-00-00'
301 """
302 if isinstance(obj, (bytes, bytearray)):
303 obj = obj.decode("ascii")
304 try:
305 return datetime.date(*[int(x) for x in obj.split("-", 2)])
306 except ValueError:
307 return obj
310def through(x):
311 return x
314# def convert_bit(b):
315# b = "\x00" * (8 - len(b)) + b # pad w/ zeroes
316# return struct.unpack(">Q", b)[0]
317#
318# the snippet above is right, but MySQLdb doesn't process bits,
319# so we shouldn't either
320convert_bit = through
323encoders = {
324 bool: escape_bool,
325 int: escape_int,
326 float: escape_float,
327 str: escape_str,
328 bytes: escape_bytes,
329 tuple: escape_not_supported,
330 list: escape_not_supported,
331 set: escape_not_supported,
332 frozenset: escape_not_supported,
333 dict: escape_not_supported,
334 type(None): escape_None,
335 datetime.date: escape_date,
336 datetime.datetime: escape_datetime,
337 datetime.timedelta: escape_timedelta,
338 datetime.time: escape_time,
339 time.struct_time: escape_struct_time,
340 Decimal: Decimal2Literal,
341}
344decoders = {
345 FIELD_TYPE.BIT: convert_bit,
346 FIELD_TYPE.TINY: int,
347 FIELD_TYPE.SHORT: int,
348 FIELD_TYPE.LONG: int,
349 FIELD_TYPE.FLOAT: float,
350 FIELD_TYPE.DOUBLE: float,
351 FIELD_TYPE.LONGLONG: int,
352 FIELD_TYPE.INT24: int,
353 FIELD_TYPE.YEAR: int,
354 FIELD_TYPE.TIMESTAMP: convert_datetime,
355 FIELD_TYPE.DATETIME: convert_datetime,
356 FIELD_TYPE.TIME: convert_timedelta,
357 FIELD_TYPE.DATE: convert_date,
358 FIELD_TYPE.BLOB: through,
359 FIELD_TYPE.TINY_BLOB: through,
360 FIELD_TYPE.MEDIUM_BLOB: through,
361 FIELD_TYPE.LONG_BLOB: through,
362 FIELD_TYPE.STRING: through,
363 FIELD_TYPE.VAR_STRING: through,
364 FIELD_TYPE.VARCHAR: through,
365 FIELD_TYPE.DECIMAL: Decimal,
366 FIELD_TYPE.NEWDECIMAL: Decimal,
367}
370# for MySQLdb compatibility
371conversions = encoders.copy()
372conversions.update(decoders)
373Thing2Literal = escape_str
375# Run doctests with `pytest --doctest-modules pymysql/converters.py`