utils.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import datetime
  2. from .base import Database
  3. class InsertVar:
  4. """
  5. A late-binding cursor variable that can be passed to Cursor.execute
  6. as a parameter, in order to receive the id of the row created by an
  7. insert statement.
  8. """
  9. types = {
  10. 'AutoField': int,
  11. 'BigAutoField': int,
  12. 'SmallAutoField': int,
  13. 'IntegerField': int,
  14. 'BigIntegerField': int,
  15. 'SmallIntegerField': int,
  16. 'PositiveSmallIntegerField': int,
  17. 'PositiveIntegerField': int,
  18. 'FloatField': Database.NATIVE_FLOAT,
  19. 'DateTimeField': Database.TIMESTAMP,
  20. 'DateField': Database.Date,
  21. 'DecimalField': Database.NUMBER,
  22. }
  23. def __init__(self, field):
  24. internal_type = getattr(field, 'target_field', field).get_internal_type()
  25. self.db_type = self.types.get(internal_type, str)
  26. def bind_parameter(self, cursor):
  27. param = cursor.cursor.var(self.db_type)
  28. cursor._insert_id_var = param
  29. return param
  30. class Oracle_datetime(datetime.datetime):
  31. """
  32. A datetime object, with an additional class attribute
  33. to tell cx_Oracle to save the microseconds too.
  34. """
  35. input_size = Database.TIMESTAMP
  36. @classmethod
  37. def from_datetime(cls, dt):
  38. return Oracle_datetime(
  39. dt.year, dt.month, dt.day,
  40. dt.hour, dt.minute, dt.second, dt.microsecond,
  41. )
  42. class BulkInsertMapper:
  43. BLOB = 'TO_BLOB(%s)'
  44. CLOB = 'TO_CLOB(%s)'
  45. DATE = 'TO_DATE(%s)'
  46. INTERVAL = 'CAST(%s as INTERVAL DAY(9) TO SECOND(6))'
  47. NUMBER = 'TO_NUMBER(%s)'
  48. TIMESTAMP = 'TO_TIMESTAMP(%s)'
  49. types = {
  50. 'BigIntegerField': NUMBER,
  51. 'BinaryField': BLOB,
  52. 'BooleanField': NUMBER,
  53. 'DateField': DATE,
  54. 'DateTimeField': TIMESTAMP,
  55. 'DecimalField': NUMBER,
  56. 'DurationField': INTERVAL,
  57. 'FloatField': NUMBER,
  58. 'IntegerField': NUMBER,
  59. 'NullBooleanField': NUMBER,
  60. 'PositiveIntegerField': NUMBER,
  61. 'PositiveSmallIntegerField': NUMBER,
  62. 'SmallIntegerField': NUMBER,
  63. 'TextField': CLOB,
  64. 'TimeField': TIMESTAMP,
  65. }