1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
U
£ý°dcã@sÊdZddlZddlZddlZddlZddlmZddlmZm    Z    m
Z
m Z m Z m Z mZmZddlmZmZmZddlmZddlmZmZmZmZmZmZdd    lmZd
ZGd d „d ƒZ Gd d„de ƒZ!dS)z"Converting MySQL and Python types
éN)ÚDecimal)ÚAnyÚCallableÚDictÚListÚOptionalÚSetÚTupleÚUnioné)Ú CharacterSetÚ    FieldFlagÚ    FieldType)Ú
HexLiteral)ÚDescriptionTypeÚRowTypeÚ
StrOrBytesÚToMysqlInputTypesÚToMysqlOutputTypesÚToPythonOutputTypes)Ú NUMERIC_TYPESz.Could not convert '{value}' to python {pytype}c@s®eZdZdZdeeeeddœdd„Zeedd    œd
d „Zdedd œd d„Z    e
e e
e fd œdd„Z eeeedœdd„Zedeeeedœdd„ƒZeeedœdd„ƒZdS)ÚMySQLConverterBasezBase class for conversion classes
 
    All class dealing with converting to and from MySQL data types must
    be a subclass of this class.
    Úutf8TFN©ÚcharsetÚ use_unicodeÚ str_fallbackÚreturncCs8d|_d|_d|_d|_| |¡||_||_i|_dS)Nr)Z python_typesZ mysql_typesrÚ
charset_idÚ set_charsetrrÚ_cache_field_types©Úselfrrr©r#úQd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\mysql/connector/conversion.pyÚ__init__>s
ýzMySQLConverterBase.__init__)rrcCs8|dkr d}|dk    r||_nd|_t |j¡d|_dS)zSet character set)Zutf8mb4Zutf8mb3rNr)rr Úget_charset_infor)r"rr#r#r$rPs zMySQLConverterBase.set_charset©ÚvaluercCs
||_dS)zSet whether to use UnicodeN)r©r"r(r#r#r$Ú set_unicode[szMySQLConverterBase.set_unicodecCsF|jj ¡}zt|d|›dƒ|ƒ}|WStk
r@|YSXdS)ú!Convert Python data type to MySQLÚ_Ú    _to_mysqlN)Ú    __class__Ú__name__ÚlowerÚgetattrÚAttributeError©r"r(Z    type_nameZ    convertedr#r#r$Úto_mysql_s ÿzMySQLConverterBase.to_mysql©Úvtyper(rc    Cs¼|dks|dkr"|dtjkr"dS|js|i|_tj ¡D]B\}}z$t|d| ¡›dƒ|j|d<Wq8tk
rxYq8Xq8|dkrˆdSz|j|d||ƒWStk
r¶|YSXdS)ú!Convert MySQL data type to PythonóNr r,Ú
_to_pythonr)    rÚBITr ÚdescÚitemsr1r0r2ÚKeyError)r"r6r(ÚnameÚinfor#r#r$Ú    to_pythonls$ÿzMySQLConverterBase.to_python©r(Úsql_modercCs|S)z"Escape buffer for sending to MySQLr#©r(rBr#r#r$Úescape†szMySQLConverterBase.escape©ÚbufrcCst|ƒS)z!Quote buffer for sending to MySQL©Ústr©rFr#r#r$ÚquoteŽszMySQLConverterBase.quote)rTF)T)N)r/Ú
__module__Ú __qualname__Ú__doc__rrHÚboolr%rr*rr
rr4rÚbytesrr@Ú staticmethodrrDrrJr#r#r#r$r7s8üû  
þ þ þýrc@s˜eZdZdZdPeeeeddœdd„ZedQe    eee    dœd    d
„ƒZ
eee e e eeefed œd d „ƒZeedœdd„Zeeeedœdd„Zee e dœdd„ƒZee e dœdd„ƒZee ee dœdd„ƒZee eefdœdd„Zee eefdœdd„Zeeedœdd„ƒZeeedœd d!„ƒZeee dœd"d#„ƒZ edddœd$d%„ƒZ!ee"j"edœd&d'„ƒZ#ee"j$edœd(d)„ƒZ%ee"j&edœd*d+„ƒZ'ee&j(edœd,d-„ƒZ)ee"j*edœd.d/„ƒZ+eeeedœd0d1„ƒZ,e-ed2fe.ee/d3œd4d5„Z0edReeee d6œd7d8„ƒZ1e1Z2edSeeee d6œd9d:„ƒZ3e3Z4e3Z5e3Z6e3Z7e3Z8dTeeeed6œd;d<„Z9e9Z:edUeeeed6œd=d>„ƒZ;edVeeee d?œd@dA„ƒZ<edWeeeee"j$d?œdBdC„ƒZ=e=Z>edXeeee"j*d?œdDdE„ƒZ?edYeeeee"j"d?œdFdG„ƒZ@e@ZAedZeeee d?œdHdI„ƒZBd[eeeeCed?œdJdK„ZDd\eeee eEeCefd?œdLdM„ZFeFZGeFZHd]eeee eEeCefd?œdNdO„ZIeIZJeIZKeIZLdS)^ÚMySQLConverteraæDefault conversion class for MySQL Connector/Python.
 
     o escape method: for escaping values send to MySQL
     o quoting method: for quoting values send to MySQL in statements
     o conversion mapping: maps Python and MySQL data types to
       function for converting them.
 
    Whenever one needs to convert values differently, a converter_class
    argument can be given while instantiating a new connection like
    cnx.connect(converter_class=CustomMySQLConverterClass).
 
    NTFrcCst ||||¡i|_dS)N)rr%r r!r#r#r$r%¢sýzMySQLConverter.__init__rAcCsàt|ttfƒrl|dkr"| dd¡S| dd¡}| dd¡}| dd    ¡}| dd
¡}| d d ¡}| d d¡}npt|tƒrÜt|tƒsÜ|dkr”| dd¡S| dd¡}| dd¡}| dd¡}| dd¡}| dd¡}| dd¡}|S)zÝ
        Escapes special characters as they are expected to by when MySQL
        receives them.
        As found in MySQL source mysys/charset.c
 
        Returns the value if not a string, or the escaped string.
        ZNO_BACKSLASH_ESCAPESó's''ó\s\\ó
s\nó s\rs\'ó"s\"ós\ú'z''ú\z\\Ú
z\nú z\rz\'ú"z\"úz\)Ú
isinstancerOÚ    bytearrayÚreplacerHrrCr#r#r$rD®s&                 zMySQLConverter.escaperEcCs>t|tƒrt|ƒ d¡St|tdƒƒr.tdƒStd|dƒS)a 
        Quote the parameters for commands. General rules:
          o numbers are returns as bytes using ascii codec
          o None is returned as bytearray(b'NULL')
          o Everything else is single quoted '<buf>'
 
        Returns a bytearray object.
        ÚasciiNsNULLrR)r^rrHÚencodeÚtyper_rIr#r#r$rJËs
 
 
zMySQLConverter.quoter'cCsj|jj ¡}zt|d|›dƒ|ƒ}|WStk
rd|jrNt|ƒ ¡YStd|›dƒd‚YnXdS)r+r,r-zPython 'z%' cannot be converted to a MySQL typeN)    r.r/r0r1r2rrHrbÚ    TypeErrorr3r#r#r$r4Ûs ÿ
ÿþzMySQLConverter.to_mysqlr5c CsV|dkr|dtjkrdS|dkr&dS|js€i|_tj ¡D]B\}}z$t|d| ¡›dƒ|j|d<Wq<tk
r|Yq<Xq<z|j|d||ƒWStk
rØz|     d¡WYSt
k
rÒ|YYSXYnzt k
r}zt |›d|d›dƒ|‚W5d}~XYn>t k
rP}zt |›d|d›dƒ|‚W5d}~XYnXdS)    r7rr Nr,r9úutf-8ú (field ú)) rr:r r;r<r1r0r2r=ÚdecodeÚUnicodeDecodeErrorÚ
ValueErrorrd)r"r6r(r>r?Úerrr#r#r$r@ês2ÿ*zMySQLConverter.to_pythoncCst|ƒS)zConvert value to int©Úint©r(r#r#r$Ú _int_to_mysqlszMySQLConverter._int_to_mysqlcCst|ƒS)zÍConvert value to int
 
        Note: There is no type "long" in Python 3 since integers are of unlimited size.
              Since Python 2 is no longer supported, this method should be deprecated.
        rlrnr#r#r$Ú_long_to_mysqlszMySQLConverter._long_to_mysqlcCst |¡rdSt|ƒS)zConvert value to floatN)ÚmathÚisnanÚfloatrnr#r#r$Ú_float_to_mysqls
zMySQLConverter._float_to_mysqlcCs
| |¡S)zConvert value to string)Ú_unicode_to_mysqlr)r#r#r$Ú _str_to_mysql$szMySQLConverter._str_to_mysqlcCsP|j}|j}|dkr&d}t |¡d}| |¡}|tjkrLd|krLt||ƒS|S)zConvert unicodeÚbinaryrrrS)rrr r&rbZslash_charsetsr)r"r(rrÚencodedr#r#r$ru(s
 
 
z MySQLConverter._unicode_to_mysqlcCs|S©zConvert value to bytesr#rnr#r#r$Ú_bytes_to_mysql5szMySQLConverter._bytes_to_mysqlcCst|ƒSry)rOrnr#r#r$Ú_bytearray_to_mysql:sz"MySQLConverter._bytearray_to_mysqlcCs |rdSdS)zConvert value to booleanr rr#rnr#r#r$Ú_bool_to_mysql?szMySQLConverter._bool_to_mysqlcCsdS)zë
        This would return what None would be in MySQL, but instead we
        leave it None and return it right away. The actual conversion
        from None to NULL happens in the quoting functionality.
 
        Return None.
        Nr#rnr#r#r$Ú_nonetype_to_mysqlDs    z!MySQLConverter._nonetype_to_mysqlc    Cs^|jr4d}| |j|j|j|j|j|j|j¡ d¡Sd}| |j|j|j|j|j|j¡ d¡S)zð
        Converts a datetime instance to a string suitable for MySQL.
        The returned string has format: %Y-%m-%d %H:%M:%S[.%f]
 
        If the instance isn't a datetime.datetime type, it return None.
 
        Returns a bytes.
        z7{0:04d}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d}.{6:06d}raz/{0:04d}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d})    Ú microsecondÚformatÚyearÚmonthÚdayÚhourÚminuteÚsecondrb)r(Úfmtr#r#r$Ú_datetime_to_mysqlOs0
ùø
úùz!MySQLConverter._datetime_to_mysqlcCs&|jd›d|jd›d|jd› d¡S)zÚ
        Converts a date instance to a string suitable for MySQL.
        The returned string has format: %Y-%m-%d
 
        If the instance isn't a datetime.date type, it return None.
 
        Returns a bytes.
        Z04dú-Z02dra)r€rr‚rbrnr#r#r$Ú_date_to_mysqlos
zMySQLConverter._date_to_mysqlcCs&|jr| d¡ d¡S| d¡ d¡S)zß
        Converts a time instance to a string suitable for MySQL.
        The returned string has format: %H:%M:%S[.%f]
 
        If the instance isn't a datetime.time type, it return None.
 
        Returns a bytes.
        z %H:%M:%S.%fraz%H:%M:%S)r~Ústrftimerbrnr#r#r$Ú_time_to_mysql{s
zMySQLConverter._time_to_mysqlcCst d|¡ d¡S)zÉ
        Converts a time.struct_time sequence to a string suitable
        for MySQL.
        The returned string has format: %Y-%m-%d %H:%M:%S
 
        Returns a bytes or None when not valid.
        z%Y-%m-%d %H:%M:%Sra)ÚtimerŠrbrnr#r#r$Ú_struct_time_to_mysql‰s    z$MySQLConverter._struct_time_to_mysqlc    Cs¦t|jd|jƒ}|jrDd}|jdkr<d|j}|d8}qH|j}nd}|jdkrZd|}t|dƒ\}}t|d    ƒ\}}|jrŽ| ||||¡}n| |||¡}| d
¡S) zš
        Converts a timedelta instance to a string suitable for MySQL.
        The returned string has format: %H:%M:%S
 
        Returns a bytes.
        i€Qz{0:02d}:{1:02d}:{2:02d}.{3:06d}ri@Br z{0:02d}:{1:02d}:{2:02d}rˆié<ra)ÚabsÚdaysÚsecondsÚ microsecondsÚdivmodrrb)    r(r‘r†ÚmcsÚhoursÚ    remainderÚminsÚsecsÚresultr#r#r$Ú_timedelta_to_mysql”s 
 
 
 
z"MySQLConverter._timedelta_to_mysqlcCst|tƒrt|ƒ d¡SdS)zŽ
        Converts a decimal.Decimal instance to a string suitable for
        MySQL.
 
        Returns a bytes or None when not valid.
        raN)r^rrHrbrnr#r#r$Ú_decimal_to_mysqlµs
z MySQLConverter._decimal_to_mysql.)ÚrowÚfieldsrc
Csfd}dgt|ƒ}|jsli|_tj ¡D]B\}}z$t|d| ¡›dƒ|j|d<Wq(tk
rhYq(Xq(|D]ì}|d}||dkr’|tjksž||dkr¨|d7}qpz|j||||ƒ||<WnŽt    k
rz|| 
d¡||<Wn"t k
r||||<YnXYn@t t fk
rR}    z|    ›d|d›d|    _‚W5d}    ~    XYnX|d7}qpt|ƒS)    a Convert a MySQL text result row to Python types
 
        The row argument is a sequence containing text result returned
        by a MySQL server. Each value of the row is converted to the
        using the field type information in the fields argument.
 
        Returns a tuple.
        rNr,r9r rerfrg)Úlenr rr;r<r1r0r2r:r=rhrirjrdÚmessageÚtuple)
r"rœrÚir™r>r?ÚfieldZ
field_typerkr#r#r$Ú row_to_pythonÂs: ÿ"
zMySQLConverter.row_to_python)r(r;rcCst|ƒS)z.
        Returns value as float type.
        )rs©r(r;r#r#r$Ú_float_to_pythonõszMySQLConverter._float_to_pythoncCst|ƒS)z,
        Returns value as int type.
        rlr¤r#r#r$Ú_int_to_pythonþszMySQLConverter._int_to_pythoncCs| |j¡}t|ƒS)z5
        Returns value as a decimal.Decimal.
        )rhrr)r"r(r;Úvalr#r#r$Ú_decimal_to_python s z!MySQLConverter._decimal_to_pythoncCst|ƒS)z,
        Returns value as str type.
        rGr¤r#r#r$Ú_strszMySQLConverter._str)r(ÚdscrcCs8|}t|ƒdkr$ddt|ƒ|}tt d|¡dƒS)z!Returns BIT columntype as integerér8z>Qr)ržrmÚstructÚunpack)r(rªZint_valr#r#r$Ú_bit_to_pythons zMySQLConverter._bit_to_pythonc    Csºt|tjƒr|Szv| d¡}t|ƒdkr@td|›dt|ƒ›ƒ‚z*t t|dƒt|dƒt|dƒ¡WWStk
r‚YWdSXWn.ttfk
r´td    t|ƒ›d
ƒd‚YnXdS) z¾Converts TIME column MySQL to a python datetime.datetime type.
 
        Raises ValueError if the value can not be converted.
 
        Returns DATE column type as datetime.date type.
        ó-éúinvalid datetime format: ú len: rr éNzCould not convert z to python datetime.timedelta)    r^ÚdatetimeÚdateÚsplitržrjrmÚ
IndexErrorÚrepr)r(rªÚpartsr#r#r$Ú_date_to_python%s 
 
 *ÿþzMySQLConverter._date_to_pythonc
CsØd}z"| d¡\}}t| dd¡ƒ}Wn ttfk
rF|}d}YnXz\dd„| d¡Dƒ\}}}|dd    ks||dd
kr’| | | }}}tj||||d WStttfk
rÒttj    |d d ƒd‚YnXdS)aConverts TIME column value to python datetime.time value type.
 
        Converts the TIME column MySQL type passed as bytes to a python
        datetime.datetime type.
 
        Raises ValueError if the value can not be converted.
 
        Returns datetime.timedelta type.
        Nó.éó0rcSsg|] }t|ƒ‘qSr#rl)Ú.0Údr#r#r$Ú
<listcomp>Usz2MySQLConverter._time_to_python.<locals>.<listcomp>ó:é-rˆ)r•Úminutesr‘r’údatetime.timedelta©r(Zpytype)
r¶rmÚljustrdrjr´Ú    timedeltar·Ú CONVERT_ERRORr)r(rªr”Úhmsr•r—r˜r#r#r$Ú_time_to_python@s4 
ý
ÿ ÿþzMySQLConverter._time_to_pythonc    Cs t|tjƒr|Sd}d}zÀ| d¡\}}t|ƒdkrT| d¡\}}t| dd¡ƒ}n|}d}dd    „| d
¡Dƒd d    „| d ¡Dƒ|g}t|ƒdkr®td |›dt|ƒ›ƒ‚ztj|Ž}Wntk
rÔYWdSXWn.ttfk
rtt    j
|ddƒd‚YnX|S)zâConverts DATETIME column value to python datetime.time value type.
 
        Converts the DATETIME column MySQL type passed as bytes to a python
        datetime.datetime type.
 
        Returns: datetime.datetime type.
        Nó r«r»r¼r½rcSsg|] }t|ƒ‘qSr#rl©r¾r¡r#r#r$rÀ|sz6MySQLConverter._datetime_to_python.<locals>.<listcomp>r¯cSsg|] }t|ƒ‘qSr#rlrÌr#r#r$rÀ}srÁr±r²rÄrÅ) r^r´r¶ržrmrÆrjr·rdrÈr)r(rªZ datetime_valr”Zdate_Ztime_rÉZdtvalr#r#r$Ú_datetime_to_pythonds>  ÿÿþÿ  ÿþz"MySQLConverter._datetime_to_pythonc
CsJz t|ƒ}Wn8tk
rD}ztdt|ƒ›dƒ|‚W5d}~XYnX|S)z#Returns YEAR column type as integerzFailed converting YEAR to int (rgN)rmrjr¸)r(rªr€rkr#r#r$Ú_year_to_python•s
 (zMySQLConverter._year_to_pythonc
Csjd}| |j¡}|stƒSzt| d¡ƒ}Wn8tk
rd}ztdt|ƒ›dƒ|‚W5d}~XYnX|S)zêReturns SET column type as set
 
        Actually, MySQL protocol sees a SET as a string type field. So this
        code isn't called directly, but used by STRING_to_python() method.
 
        Returns SET column type as a set.
        Nú,zCould not convert set z to a sequence)rhrÚsetr¶rjr¸)r"r(rªÚset_typer§rkr#r#r$Ú_set_to_pythonŸs
 ÿþzMySQLConverter._set_to_pythoncCs |jdkr|S|dk    r`|dtjkr6|jr6| |j¡S|dtj@rP| ||¡S|ddkr`|St|t    t
fƒrœ|jrœz| |j¡WSt k
rš|YSX|S)z°
        Note that a SET is a string too, but using the FieldFlag we can see
        whether we have to split it.
 
        Returns string typed columns as string type.
        rwNr ér«é?) rrZJSONrrhr ZSETrÒr^rOr_ri©r"r(rªr#r#r$Ú_string_to_pythonµs    
 
z MySQLConverter._string_to_pythoncCsD|dk    r8|dtj@r8|dtj@r8|ddkr8t|ƒS| ||¡S)z!Convert BLOB data type to Python.NrÓr«rÔ)r ZBLOBÚBINARYrOrÖrÕr#r#r$Ú_blob_to_pythonÓs ÿ þ
üzMySQLConverter._blob_to_python)NTF)N)N)N)N)N)N)N)N)N)N)N)N)N)Mr/rKrLrMrrHrNr%rPrrDr
rsrmrrrOrJrrr4rrr@rorprtrvrurzr_r{r|r}r´r‡rµr‰rŒr‹Ú struct_timerrÇršr›r    rrr£r¥Z_double_to_pythonr¦Z_tiny_to_pythonZ_short_to_pythonZ_int24_to_pythonZ_long_to_pythonZ_longlong_to_pythonr¨Z_newdecimal_to_pythonr©r®rºZ_NEWDATE_to_pythonrÊrÍZ_timestamp_to_pythonrÎrrÒrrÖZ_var_string_to_pythonZ_json_to_pythonrØZ_long_blob_to_pythonZ_medium_blob_to_pythonZ_tiny_blob_to_pythonr#r#r#r$rQ”sðüû $ü % 
 
 
þ 3ÿþ     ÿþÿþ#ÿþ.
ÿþ ÿþ ÿþ rQ)"rMr´rqr¬rŒÚdecimalrÚtypingrrrrrrr    r
Ú    constantsr r rZ custom_typesrÚtypesrrrrrrÚutilsrrÈrrQr#r#r#r$Ú<module>s (   ]