Hide keyboard shortcuts

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

2PyMySQL: A pure-Python MySQL client library. 

3 

4Copyright (c) 2010-2016 PyMySQL contributors 

5 

6Permission is hereby granted, free of charge, to any person obtaining a copy 

7of this software and associated documentation files (the "Software"), to deal 

8in the Software without restriction, including without limitation the rights 

9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 

10copies of the Software, and to permit persons to whom the Software is 

11furnished to do so, subject to the following conditions: 

12 

13The above copyright notice and this permission notice shall be included in 

14all copies or substantial portions of the Software. 

15 

16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 

22THE SOFTWARE. 

23""" 

24import sys 

25 

26from .constants import FIELD_TYPE 

27from .err import ( 

28 Warning, 

29 Error, 

30 InterfaceError, 

31 DataError, 

32 DatabaseError, 

33 OperationalError, 

34 IntegrityError, 

35 InternalError, 

36 NotSupportedError, 

37 ProgrammingError, 

38 MySQLError, 

39) 

40from .times import ( 

41 Date, 

42 Time, 

43 Timestamp, 

44 DateFromTicks, 

45 TimeFromTicks, 

46 TimestampFromTicks, 

47) 

48 

49 

50VERSION = (1, 0, 2, None) 

51if VERSION[3] is not None: 

52 VERSION_STRING = "%d.%d.%d_%s" % VERSION 

53else: 

54 VERSION_STRING = "%d.%d.%d" % VERSION[:3] 

55threadsafety = 1 

56apilevel = "2.0" 

57paramstyle = "pyformat" 

58 

59from . import connections # noqa: E402 

60 

61 

62class DBAPISet(frozenset): 

63 def __ne__(self, other): 

64 if isinstance(other, set): 

65 return frozenset.__ne__(self, other) 

66 else: 

67 return other not in self 

68 

69 def __eq__(self, other): 

70 if isinstance(other, frozenset): 

71 return frozenset.__eq__(self, other) 

72 else: 

73 return other in self 

74 

75 def __hash__(self): 

76 return frozenset.__hash__(self) 

77 

78 

79STRING = DBAPISet([FIELD_TYPE.ENUM, FIELD_TYPE.STRING, FIELD_TYPE.VAR_STRING]) 

80BINARY = DBAPISet( 

81 [ 

82 FIELD_TYPE.BLOB, 

83 FIELD_TYPE.LONG_BLOB, 

84 FIELD_TYPE.MEDIUM_BLOB, 

85 FIELD_TYPE.TINY_BLOB, 

86 ] 

87) 

88NUMBER = DBAPISet( 

89 [ 

90 FIELD_TYPE.DECIMAL, 

91 FIELD_TYPE.DOUBLE, 

92 FIELD_TYPE.FLOAT, 

93 FIELD_TYPE.INT24, 

94 FIELD_TYPE.LONG, 

95 FIELD_TYPE.LONGLONG, 

96 FIELD_TYPE.TINY, 

97 FIELD_TYPE.YEAR, 

98 ] 

99) 

100DATE = DBAPISet([FIELD_TYPE.DATE, FIELD_TYPE.NEWDATE]) 

101TIME = DBAPISet([FIELD_TYPE.TIME]) 

102TIMESTAMP = DBAPISet([FIELD_TYPE.TIMESTAMP, FIELD_TYPE.DATETIME]) 

103DATETIME = TIMESTAMP 

104ROWID = DBAPISet() 

105 

106 

107def Binary(x): 

108 """Return x as a binary type.""" 

109 return bytes(x) 

110 

111 

112Connect = connect = Connection = connections.Connection 

113 

114 

115def get_client_info(): # for MySQLdb compatibility 

116 version = VERSION 

117 if VERSION[3] is None: 

118 version = VERSION[:3] 

119 return ".".join(map(str, version)) 

120 

121 

122# we include a doctored version_info here for MySQLdb compatibility 

123version_info = (1, 4, 0, "final", 0) 

124 

125NULL = "NULL" 

126 

127__version__ = get_client_info() 

128 

129 

130def thread_safe(): 

131 return True # match MySQLdb.thread_safe() 

132 

133 

134def install_as_MySQLdb(): 

135 """ 

136 After this function is called, any application that imports MySQLdb or 

137 _mysql will unwittingly actually use pymysql. 

138 """ 

139 sys.modules["MySQLdb"] = sys.modules["_mysql"] = sys.modules["pymysql"] 

140 

141 

142__all__ = [ 

143 "BINARY", 

144 "Binary", 

145 "Connect", 

146 "Connection", 

147 "DATE", 

148 "Date", 

149 "Time", 

150 "Timestamp", 

151 "DateFromTicks", 

152 "TimeFromTicks", 

153 "TimestampFromTicks", 

154 "DataError", 

155 "DatabaseError", 

156 "Error", 

157 "FIELD_TYPE", 

158 "IntegrityError", 

159 "InterfaceError", 

160 "InternalError", 

161 "MySQLError", 

162 "NULL", 

163 "NUMBER", 

164 "NotSupportedError", 

165 "DBAPISet", 

166 "OperationalError", 

167 "ProgrammingError", 

168 "ROWID", 

169 "STRING", 

170 "TIME", 

171 "TIMESTAMP", 

172 "Warning", 

173 "apilevel", 

174 "connect", 

175 "connections", 

176 "constants", 

177 "converters", 

178 "cursors", 

179 "get_client_info", 

180 "paramstyle", 

181 "threadsafety", 

182 "version_info", 

183 "install_as_MySQLdb", 

184 "__version__", 

185]