__init__.py 4.3 KB

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