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

154 statements  

1import datetime 

2import re 

3import time 

4import warnings 

5from decimal import Decimal 

6 

7from .constants import FIELD_TYPE 

8from .err import ProgrammingError 

9 

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!!!" 

13 

14 

15def escape_item(val, charset=None, mapping=None): 

16 if mapping is None: 

17 mapping = encoders 

18 encoder = mapping.get(type(val)) 

19 

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") 

26 

27 if encoder == escape_sequence: 

28 val = encoder(val, charset, mapping) 

29 else: 

30 val = encoder(val, mapping) 

31 return val 

32 

33 

34def escape_not_supported(val, charset=None, mapping=None): 

35 raise TypeError(f"{type(val).__name__} can not be used as parameter") 

36 

37 

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) + ")" 

45 

46 

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]) 

50 

51 

52def escape_bool(value, mapping=None): 

53 return str(int(value)) 

54 

55 

56def escape_int(value, mapping=None): 

57 return str(value) 

58 

59 

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 

67 

68 

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("'")] = "\\'" 

77 

78 

79def escape_string(value, mapping=None): 

80 """escapes *value* without adding quote. 

81 

82 Value should be unicode 

83 """ 

84 return value.translate(_escape_table) 

85 

86 

87def escape_bytes(value, mapping=None) -> str: 

88 return "_binary X'%s'" % value.hex() 

89 

90 

91def escape_str(value, mapping=None) -> str: 

92 return "'" + escape_string(str(value)) + "'" 

93 

94 

95def escape_None(value, mapping=None): 

96 return "NULL" 

97 

98 

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) 

108 

109 micros = obj.microseconds 

110 minutes, seconds = divmod(obj.seconds, 60) 

111 hours, minutes = divmod(minutes, 60) 

112 hours += obj.days * 24 

113 

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}'" 

117 

118 

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) 

125 

126 

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) 

136 

137 

138def escape_date(obj, mapping=None): 

139 fmt = "'{0.year:04}-{0.month:02}-{0.day:02}'" 

140 return fmt.format(obj) 

141 

142 

143def escape_struct_time(obj, mapping=None): 

144 return escape_datetime(datetime.datetime(*obj[:6])) 

145 

146 

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") 

151 

152 

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]) 

159 

160 

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) 

164 

165 

166def convert_datetime(obj): 

167 """Returns a DATETIME or TIMESTAMP column value as a datetime object: 

168 

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) 

173 

174 Illegal values are returned as str: 

175 

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") 

183 

184 m = DATETIME_RE.fullmatch(obj) 

185 if not m: 

186 return convert_date(obj) 

187 

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) 

194 

195 

196TIMEDELTA_RE = re.compile(r"(-)?(\d{1,3}):(\d{1,2}):(\d{1,2})(?:\.(\d{1,6}))?") 

197 

198 

199def convert_timedelta(obj): 

200 """Returns a TIME column as a timedelta object: 

201 

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) 

206 

207 Illegal values are returned as string: 

208 

209 >>> convert_timedelta('random crap') 

210 'random crap' 

211 

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") 

218 

219 m = TIMEDELTA_RE.fullmatch(obj) 

220 if not m: 

221 return obj 

222 

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:] 

228 

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 

241 

242 

243TIME_RE = re.compile(r"(\d{1,2}):(\d{1,2}):(\d{1,2})(?:\.(\d{1,6}))?") 

244 

245 

246def convert_time(obj): 

247 """Returns a TIME column as a time object: 

248 

249 >>> convert_time('15:06:17') 

250 datetime.time(15, 6, 17) 

251 

252 Illegal values are returned as str: 

253 

254 >>> convert_time('-25:06:17') 

255 '-25:06:17' 

256 >>> convert_time('random crap') 

257 'random crap' 

258 

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. 

262 

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") 

270 

271 m = TIME_RE.fullmatch(obj) 

272 if not m: 

273 return obj 

274 

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 

287 

288 

289def convert_date(obj): 

290 """Returns a DATE column as a date object: 

291 

292 >>> convert_date('2007-02-26') 

293 datetime.date(2007, 2, 26) 

294 

295 Illegal values are returned as str: 

296 

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 

308 

309 

310def through(x): 

311 return x 

312 

313 

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 

321 

322 

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} 

342 

343 

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} 

368 

369 

370# for MySQLdb compatibility 

371conversions = encoders.copy() 

372conversions.update(decoders) 

373Thing2Literal = escape_str 

374 

375# Run doctests with `pytest --doctest-modules pymysql/converters.py`