Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/MySQLdb/converters.py : 56%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""MySQLdb type conversion module
3This module handles all the type conversions for MySQL. If the default
4type conversions aren't what you need, you can make your own. The
5dictionary conversions maps some kind of type to a conversion function
6which returns the corresponding value:
8Key: FIELD_TYPE.* (from MySQLdb.constants)
10Conversion function:
12 Arguments: string
14 Returns: Python object
16Key: Python type object (from types) or class
18Conversion function:
20 Arguments: Python object of indicated type or class AND
21 conversion dictionary
23 Returns: SQL literal value
25 Notes: Most conversion functions can ignore the dictionary, but
26 it is a required parameter. It is necessary for converting
27 things like sequences and instances.
29Don't modify conversions if you can avoid it. Instead, make copies
30(with the copy() method), modify the copies, and then pass them to
31MySQL.connect().
32"""
33from decimal import Decimal
35from MySQLdb._mysql import string_literal
36from MySQLdb.constants import FIELD_TYPE, FLAG
37from MySQLdb.times import (
38 Date,
39 DateTimeType,
40 DateTime2literal,
41 DateTimeDeltaType,
42 DateTimeDelta2literal,
43 DateTime_or_None,
44 TimeDelta_or_None,
45 Date_or_None,
46)
47from MySQLdb._exceptions import ProgrammingError
49import array
51NoneType = type(None)
53try:
54 ArrayType = array.ArrayType
55except AttributeError:
56 ArrayType = array.array
59def Bool2Str(s, d):
60 return b"1" if s else b"0"
63def Set2Str(s, d):
64 # Only support ascii string. Not tested.
65 return string_literal(",".join(s))
68def Thing2Str(s, d):
69 """Convert something into a string via str()."""
70 return str(s)
73def Float2Str(o, d):
74 s = repr(o)
75 if s in ("inf", "nan"):
76 raise ProgrammingError("%s can not be used with MySQL" % s)
77 if "e" not in s:
78 s += "e0"
79 return s
82def None2NULL(o, d):
83 """Convert None to NULL."""
84 return b"NULL"
87def Thing2Literal(o, d):
88 """Convert something into a SQL string literal. If using
89 MySQL-3.23 or newer, string_literal() is a method of the
90 _mysql.MYSQL object, and this function will be overridden with
91 that method when the connection is created."""
92 return string_literal(o)
95def Decimal2Literal(o, d):
96 return format(o, "f")
99def array2Str(o, d):
100 return Thing2Literal(o.tostring(), d)
103# bytes or str regarding to BINARY_FLAG.
104_bytes_or_str = ((FLAG.BINARY, bytes), (None, str))
106conversions = {
107 int: Thing2Str,
108 float: Float2Str,
109 NoneType: None2NULL,
110 ArrayType: array2Str,
111 bool: Bool2Str,
112 Date: Thing2Literal,
113 DateTimeType: DateTime2literal,
114 DateTimeDeltaType: DateTimeDelta2literal,
115 set: Set2Str,
116 Decimal: Decimal2Literal,
117 FIELD_TYPE.TINY: int,
118 FIELD_TYPE.SHORT: int,
119 FIELD_TYPE.LONG: int,
120 FIELD_TYPE.FLOAT: float,
121 FIELD_TYPE.DOUBLE: float,
122 FIELD_TYPE.DECIMAL: Decimal,
123 FIELD_TYPE.NEWDECIMAL: Decimal,
124 FIELD_TYPE.LONGLONG: int,
125 FIELD_TYPE.INT24: int,
126 FIELD_TYPE.YEAR: int,
127 FIELD_TYPE.TIMESTAMP: DateTime_or_None,
128 FIELD_TYPE.DATETIME: DateTime_or_None,
129 FIELD_TYPE.TIME: TimeDelta_or_None,
130 FIELD_TYPE.DATE: Date_or_None,
131 FIELD_TYPE.TINY_BLOB: bytes,
132 FIELD_TYPE.MEDIUM_BLOB: bytes,
133 FIELD_TYPE.LONG_BLOB: bytes,
134 FIELD_TYPE.BLOB: bytes,
135 FIELD_TYPE.STRING: bytes,
136 FIELD_TYPE.VAR_STRING: bytes,
137 FIELD_TYPE.VARCHAR: bytes,
138 FIELD_TYPE.JSON: bytes,
139}