Development of an internal social media platform with personalised dashboards for students
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.

utils.py 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import datetime
  2. from .base import Database
  3. class InsertIdVar:
  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. def bind_parameter(self, cursor):
  10. param = cursor.cursor.var(Database.NUMBER)
  11. cursor._insert_id_var = param
  12. return param
  13. class Oracle_datetime(datetime.datetime):
  14. """
  15. A datetime object, with an additional class attribute
  16. to tell cx_Oracle to save the microseconds too.
  17. """
  18. input_size = Database.TIMESTAMP
  19. @classmethod
  20. def from_datetime(cls, dt):
  21. return Oracle_datetime(
  22. dt.year, dt.month, dt.day,
  23. dt.hour, dt.minute, dt.second, dt.microsecond,
  24. )
  25. class BulkInsertMapper:
  26. BLOB = 'TO_BLOB(%s)'
  27. DATE = 'TO_DATE(%s)'
  28. INTERVAL = 'CAST(%s as INTERVAL DAY(9) TO SECOND(6))'
  29. NUMBER = 'TO_NUMBER(%s)'
  30. TIMESTAMP = 'TO_TIMESTAMP(%s)'
  31. types = {
  32. 'BigIntegerField': NUMBER,
  33. 'BinaryField': BLOB,
  34. 'BooleanField': NUMBER,
  35. 'DateField': DATE,
  36. 'DateTimeField': TIMESTAMP,
  37. 'DecimalField': NUMBER,
  38. 'DurationField': INTERVAL,
  39. 'FloatField': NUMBER,
  40. 'IntegerField': NUMBER,
  41. 'NullBooleanField': NUMBER,
  42. 'PositiveIntegerField': NUMBER,
  43. 'PositiveSmallIntegerField': NUMBER,
  44. 'SmallIntegerField': NUMBER,
  45. 'TimeField': TIMESTAMP,
  46. }