converters.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. """MySQLdb type conversion module
  2. This module handles all the type conversions for MySQL. If the default
  3. type conversions aren't what you need, you can make your own. The
  4. dictionary conversions maps some kind of type to a conversion function
  5. which returns the corresponding value:
  6. Key: FIELD_TYPE.* (from MySQLdb.constants)
  7. Conversion function:
  8. Arguments: string
  9. Returns: Python object
  10. Key: Python type object (from types) or class
  11. Conversion function:
  12. Arguments: Python object of indicated type or class AND
  13. conversion dictionary
  14. Returns: SQL literal value
  15. Notes: Most conversion functions can ignore the dictionary, but
  16. it is a required parameter. It is necessary for converting
  17. things like sequences and instances.
  18. Don't modify conversions if you can avoid it. Instead, make copies
  19. (with the copy() method), modify the copies, and then pass them to
  20. MySQL.connect().
  21. """
  22. from decimal import Decimal
  23. from MySQLdb._mysql import string_literal
  24. from MySQLdb.constants import FIELD_TYPE, FLAG
  25. from MySQLdb.times import (
  26. Date,
  27. DateTimeType,
  28. DateTime2literal,
  29. DateTimeDeltaType,
  30. DateTimeDelta2literal,
  31. DateTime_or_None,
  32. TimeDelta_or_None,
  33. Date_or_None,
  34. )
  35. from MySQLdb._exceptions import ProgrammingError
  36. import array
  37. NoneType = type(None)
  38. try:
  39. ArrayType = array.ArrayType
  40. except AttributeError:
  41. ArrayType = array.array
  42. def Bool2Str(s, d):
  43. return b"1" if s else b"0"
  44. def Set2Str(s, d):
  45. # Only support ascii string. Not tested.
  46. return string_literal(",".join(s))
  47. def Thing2Str(s, d):
  48. """Convert something into a string via str()."""
  49. return str(s)
  50. def Float2Str(o, d):
  51. s = repr(o)
  52. if s in ("inf", "-inf", "nan"):
  53. raise ProgrammingError("%s can not be used with MySQL" % s)
  54. if "e" not in s:
  55. s += "e0"
  56. return s
  57. def None2NULL(o, d):
  58. """Convert None to NULL."""
  59. return b"NULL"
  60. def Thing2Literal(o, d):
  61. """Convert something into a SQL string literal. If using
  62. MySQL-3.23 or newer, string_literal() is a method of the
  63. _mysql.MYSQL object, and this function will be overridden with
  64. that method when the connection is created."""
  65. return string_literal(o)
  66. def Decimal2Literal(o, d):
  67. return format(o, "f")
  68. def array2Str(o, d):
  69. return Thing2Literal(o.tostring(), d)
  70. # bytes or str regarding to BINARY_FLAG.
  71. _bytes_or_str = ((FLAG.BINARY, bytes), (None, str))
  72. conversions = {
  73. int: Thing2Str,
  74. float: Float2Str,
  75. NoneType: None2NULL,
  76. ArrayType: array2Str,
  77. bool: Bool2Str,
  78. Date: Thing2Literal,
  79. DateTimeType: DateTime2literal,
  80. DateTimeDeltaType: DateTimeDelta2literal,
  81. set: Set2Str,
  82. Decimal: Decimal2Literal,
  83. FIELD_TYPE.TINY: int,
  84. FIELD_TYPE.SHORT: int,
  85. FIELD_TYPE.LONG: int,
  86. FIELD_TYPE.FLOAT: float,
  87. FIELD_TYPE.DOUBLE: float,
  88. FIELD_TYPE.DECIMAL: Decimal,
  89. FIELD_TYPE.NEWDECIMAL: Decimal,
  90. FIELD_TYPE.LONGLONG: int,
  91. FIELD_TYPE.INT24: int,
  92. FIELD_TYPE.YEAR: int,
  93. FIELD_TYPE.TIMESTAMP: DateTime_or_None,
  94. FIELD_TYPE.DATETIME: DateTime_or_None,
  95. FIELD_TYPE.TIME: TimeDelta_or_None,
  96. FIELD_TYPE.DATE: Date_or_None,
  97. FIELD_TYPE.TINY_BLOB: bytes,
  98. FIELD_TYPE.MEDIUM_BLOB: bytes,
  99. FIELD_TYPE.LONG_BLOB: bytes,
  100. FIELD_TYPE.BLOB: bytes,
  101. FIELD_TYPE.STRING: bytes,
  102. FIELD_TYPE.VAR_STRING: bytes,
  103. FIELD_TYPE.VARCHAR: bytes,
  104. FIELD_TYPE.JSON: bytes,
  105. }