You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.7 KiB

6 years ago
  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 _mysql import string_literal, escape, NULL
  23. from MySQLdb.constants import FIELD_TYPE, FLAG
  24. from MySQLdb.times import *
  25. from MySQLdb.compat import PY2, long
  26. NoneType = type(None)
  27. import array
  28. try:
  29. ArrayType = array.ArrayType
  30. except AttributeError:
  31. ArrayType = array.array
  32. def Bool2Str(s, d): return str(int(s))
  33. def Str2Set(s):
  34. return set([ i for i in s.split(',') if i ])
  35. def Set2Str(s, d):
  36. # Only support ascii string. Not tested.
  37. return string_literal(','.join(s), d)
  38. def Thing2Str(s, d):
  39. """Convert something into a string via str()."""
  40. return str(s)
  41. def Unicode2Str(s, d):
  42. """Convert a unicode object to a string using the default encoding.
  43. This is only used as a placeholder for the real function, which
  44. is connection-dependent."""
  45. return s.encode()
  46. def Float2Str(o, d):
  47. return '%.15g' % o
  48. def None2NULL(o, d):
  49. """Convert None to NULL."""
  50. return NULL # duh
  51. def Thing2Literal(o, d):
  52. """Convert something into a SQL string literal. If using
  53. MySQL-3.23 or newer, string_literal() is a method of the
  54. _mysql.MYSQL object, and this function will be overridden with
  55. that method when the connection is created."""
  56. return string_literal(o, d)
  57. def char_array(s):
  58. return array.array('c', s)
  59. def array2Str(o, d):
  60. return Thing2Literal(o.tostring(), d)
  61. def quote_tuple(t, d):
  62. return "(%s)" % (','.join(escape_sequence(t, d)))
  63. # bytes or str regarding to BINARY_FLAG.
  64. _bytes_or_str = [(FLAG.BINARY, bytes)]
  65. conversions = {
  66. int: Thing2Str,
  67. long: Thing2Str,
  68. float: Float2Str,
  69. NoneType: None2NULL,
  70. ArrayType: array2Str,
  71. bool: Bool2Str,
  72. Date: Thing2Literal,
  73. DateTimeType: DateTime2literal,
  74. DateTimeDeltaType: DateTimeDelta2literal,
  75. str: Thing2Literal, # default
  76. set: Set2Str,
  77. FIELD_TYPE.TINY: int,
  78. FIELD_TYPE.SHORT: int,
  79. FIELD_TYPE.LONG: long,
  80. FIELD_TYPE.FLOAT: float,
  81. FIELD_TYPE.DOUBLE: float,
  82. FIELD_TYPE.DECIMAL: float,
  83. FIELD_TYPE.NEWDECIMAL: float,
  84. FIELD_TYPE.LONGLONG: long,
  85. FIELD_TYPE.INT24: int,
  86. FIELD_TYPE.YEAR: int,
  87. FIELD_TYPE.SET: Str2Set,
  88. FIELD_TYPE.TIMESTAMP: mysql_timestamp_converter,
  89. FIELD_TYPE.DATETIME: DateTime_or_None,
  90. FIELD_TYPE.TIME: TimeDelta_or_None,
  91. FIELD_TYPE.DATE: Date_or_None,
  92. FIELD_TYPE.TINY_BLOB: _bytes_or_str,
  93. FIELD_TYPE.MEDIUM_BLOB: _bytes_or_str,
  94. FIELD_TYPE.LONG_BLOB: _bytes_or_str,
  95. FIELD_TYPE.BLOB: _bytes_or_str,
  96. FIELD_TYPE.STRING: _bytes_or_str,
  97. FIELD_TYPE.VAR_STRING: _bytes_or_str,
  98. FIELD_TYPE.VARCHAR: _bytes_or_str,
  99. }
  100. if PY2:
  101. conversions[unicode] = Unicode2Str
  102. else:
  103. conversions[bytes] = Thing2Literal
  104. try:
  105. from decimal import Decimal
  106. conversions[FIELD_TYPE.DECIMAL] = Decimal
  107. conversions[FIELD_TYPE.NEWDECIMAL] = Decimal
  108. except ImportError:
  109. pass

Powered by TurnKey Linux.