1 | """ |
---|
2 | PyMySQL: A pure-Python MySQL client library. |
---|
3 | |
---|
4 | Copyright (c) 2010-2016 PyMySQL contributors |
---|
5 | |
---|
6 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
7 | of this software and associated documentation files (the "Software"), to deal |
---|
8 | in the Software without restriction, including without limitation the rights |
---|
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
10 | copies of the Software, and to permit persons to whom the Software is |
---|
11 | furnished to do so, subject to the following conditions: |
---|
12 | |
---|
13 | The above copyright notice and this permission notice shall be included in |
---|
14 | all copies or substantial portions of the Software. |
---|
15 | |
---|
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
22 | THE SOFTWARE. |
---|
23 | """ |
---|
24 | import sys |
---|
25 | |
---|
26 | from ._compat import PY2 |
---|
27 | from .constants import FIELD_TYPE |
---|
28 | from .converters import escape_dict, escape_sequence, escape_string |
---|
29 | from .err import ( |
---|
30 | Warning, Error, InterfaceError, DataError, |
---|
31 | DatabaseError, OperationalError, IntegrityError, InternalError, |
---|
32 | NotSupportedError, ProgrammingError, MySQLError) |
---|
33 | from .times import ( |
---|
34 | Date, Time, Timestamp, |
---|
35 | DateFromTicks, TimeFromTicks, TimestampFromTicks) |
---|
36 | |
---|
37 | |
---|
38 | VERSION = (0, 7, 9, None) |
---|
39 | threadsafety = 1 |
---|
40 | apilevel = "2.0" |
---|
41 | paramstyle = "pyformat" |
---|
42 | |
---|
43 | |
---|
44 | class DBAPISet(frozenset): |
---|
45 | |
---|
46 | def __ne__(self, other): |
---|
47 | if isinstance(other, set): |
---|
48 | return frozenset.__ne__(self, other) |
---|
49 | else: |
---|
50 | return other not in self |
---|
51 | |
---|
52 | def __eq__(self, other): |
---|
53 | if isinstance(other, frozenset): |
---|
54 | return frozenset.__eq__(self, other) |
---|
55 | else: |
---|
56 | return other in self |
---|
57 | |
---|
58 | def __hash__(self): |
---|
59 | return frozenset.__hash__(self) |
---|
60 | |
---|
61 | |
---|
62 | STRING = DBAPISet([FIELD_TYPE.ENUM, FIELD_TYPE.STRING, |
---|
63 | FIELD_TYPE.VAR_STRING]) |
---|
64 | BINARY = DBAPISet([FIELD_TYPE.BLOB, FIELD_TYPE.LONG_BLOB, |
---|
65 | FIELD_TYPE.MEDIUM_BLOB, FIELD_TYPE.TINY_BLOB]) |
---|
66 | NUMBER = DBAPISet([FIELD_TYPE.DECIMAL, FIELD_TYPE.DOUBLE, FIELD_TYPE.FLOAT, |
---|
67 | FIELD_TYPE.INT24, FIELD_TYPE.LONG, FIELD_TYPE.LONGLONG, |
---|
68 | FIELD_TYPE.TINY, FIELD_TYPE.YEAR]) |
---|
69 | DATE = DBAPISet([FIELD_TYPE.DATE, FIELD_TYPE.NEWDATE]) |
---|
70 | TIME = DBAPISet([FIELD_TYPE.TIME]) |
---|
71 | TIMESTAMP = DBAPISet([FIELD_TYPE.TIMESTAMP, FIELD_TYPE.DATETIME]) |
---|
72 | DATETIME = TIMESTAMP |
---|
73 | ROWID = DBAPISet() |
---|
74 | |
---|
75 | |
---|
76 | def Binary(x): |
---|
77 | """Return x as a binary type.""" |
---|
78 | if PY2: |
---|
79 | return bytearray(x) |
---|
80 | else: |
---|
81 | return bytes(x) |
---|
82 | |
---|
83 | |
---|
84 | def Connect(*args, **kwargs): |
---|
85 | """ |
---|
86 | Connect to the database; see connections.Connection.__init__() for |
---|
87 | more information. |
---|
88 | """ |
---|
89 | from .connections import Connection |
---|
90 | return Connection(*args, **kwargs) |
---|
91 | |
---|
92 | from . import connections as _orig_conn |
---|
93 | if _orig_conn.Connection.__init__.__doc__ is not None: |
---|
94 | Connect.__doc__ = _orig_conn.Connection.__init__.__doc__ |
---|
95 | del _orig_conn |
---|
96 | |
---|
97 | |
---|
98 | def get_client_info(): # for MySQLdb compatibility |
---|
99 | return '.'.join(map(str, VERSION)) |
---|
100 | |
---|
101 | connect = Connection = Connect |
---|
102 | |
---|
103 | # we include a doctored version_info here for MySQLdb compatibility |
---|
104 | version_info = (1,2,6,"final",0) |
---|
105 | |
---|
106 | NULL = "NULL" |
---|
107 | |
---|
108 | __version__ = get_client_info() |
---|
109 | |
---|
110 | def thread_safe(): |
---|
111 | return True # match MySQLdb.thread_safe() |
---|
112 | |
---|
113 | def install_as_MySQLdb(): |
---|
114 | """ |
---|
115 | After this function is called, any application that imports MySQLdb or |
---|
116 | _mysql will unwittingly actually use |
---|
117 | """ |
---|
118 | sys.modules["MySQLdb"] = sys.modules["_mysql"] = sys.modules["pymysql"] |
---|
119 | |
---|
120 | |
---|
121 | __all__ = [ |
---|
122 | 'BINARY', 'Binary', 'Connect', 'Connection', 'DATE', 'Date', |
---|
123 | 'Time', 'Timestamp', 'DateFromTicks', 'TimeFromTicks', 'TimestampFromTicks', |
---|
124 | 'DataError', 'DatabaseError', 'Error', 'FIELD_TYPE', 'IntegrityError', |
---|
125 | 'InterfaceError', 'InternalError', 'MySQLError', 'NULL', 'NUMBER', |
---|
126 | 'NotSupportedError', 'DBAPISet', 'OperationalError', 'ProgrammingError', |
---|
127 | 'ROWID', 'STRING', 'TIME', 'TIMESTAMP', 'Warning', 'apilevel', 'connect', |
---|
128 | 'connections', 'constants', 'converters', 'cursors', |
---|
129 | 'escape_dict', 'escape_sequence', 'escape_string', 'get_client_info', |
---|
130 | 'paramstyle', 'threadsafety', 'version_info', |
---|
131 | |
---|
132 | "install_as_MySQLdb", |
---|
133 | "NULL", "__version__", |
---|
134 | ] |
---|