| 1 |
import datetime |
| 2 |
import decimal |
| 3 |
|
| 4 |
from .base import Database |
| 5 |
|
| 6 |
|
| 7 |
class InsertVar: |
| 8 |
""" |
| 9 |
A late-binding cursor variable that can be passed to Cursor.execute |
| 10 |
as a parameter, in order to receive the id of the row created by an |
| 11 |
insert statement. |
| 12 |
""" |
| 13 |
|
| 14 |
types = { |
| 15 |
"AutoField": int, |
| 16 |
"BigAutoField": int, |
| 17 |
"SmallAutoField": int, |
| 18 |
"IntegerField": int, |
| 19 |
"BigIntegerField": int, |
| 20 |
"SmallIntegerField": int, |
| 21 |
"PositiveBigIntegerField": int, |
| 22 |
"PositiveSmallIntegerField": int, |
| 23 |
"PositiveIntegerField": int, |
| 24 |
"BooleanField": int, |
| 25 |
"FloatField": Database.DB_TYPE_BINARY_DOUBLE, |
| 26 |
"DateTimeField": Database.DB_TYPE_TIMESTAMP, |
| 27 |
"DateField": datetime.date, |
| 28 |
"DecimalField": decimal.Decimal, |
| 29 |
} |
| 30 |
|
| 31 |
def __init__(self, field): |
| 32 |
internal_type = getattr(field, "target_field", field).get_internal_type() |
| 33 |
self.db_type = self.types.get(internal_type, str) |
| 34 |
self.bound_param = None |
| 35 |
|
| 36 |
def bind_parameter(self, cursor): |
| 37 |
self.bound_param = cursor.cursor.var(self.db_type) |
| 38 |
return self.bound_param |
| 39 |
|
| 40 |
def get_value(self): |
| 41 |
return self.bound_param.getvalue() |
| 42 |
|
| 43 |
|
| 44 |
class Oracle_datetime(datetime.datetime): |
| 45 |
""" |
| 46 |
A datetime object, with an additional class attribute |
| 47 |
to tell oracledb to save the microseconds too. |
| 48 |
""" |
| 49 |
|
| 50 |
input_size = Database.DB_TYPE_TIMESTAMP |
| 51 |
|
| 52 |
@classmethod |
| 53 |
def from_datetime(cls, dt): |
| 54 |
return Oracle_datetime( |
| 55 |
dt.year, |
| 56 |
dt.month, |
| 57 |
dt.day, |
| 58 |
dt.hour, |
| 59 |
dt.minute, |
| 60 |
dt.second, |
| 61 |
dt.microsecond, |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
class BulkInsertMapper: |
| 66 |
BLOB = "TO_BLOB(%s)" |
| 67 |
DATE = "TO_DATE(%s)" |
| 68 |
INTERVAL = "CAST(%s as INTERVAL DAY(9) TO SECOND(6))" |
| 69 |
NCLOB = "TO_NCLOB(%s)" |
| 70 |
NUMBER = "TO_NUMBER(%s)" |
| 71 |
TIMESTAMP = "TO_TIMESTAMP(%s)" |
| 72 |
|
| 73 |
types = { |
| 74 |
"AutoField": NUMBER, |
| 75 |
"BigAutoField": NUMBER, |
| 76 |
"BigIntegerField": NUMBER, |
| 77 |
"BinaryField": BLOB, |
| 78 |
"BooleanField": NUMBER, |
| 79 |
"DateField": DATE, |
| 80 |
"DateTimeField": TIMESTAMP, |
| 81 |
"DecimalField": NUMBER, |
| 82 |
"DurationField": INTERVAL, |
| 83 |
"FloatField": NUMBER, |
| 84 |
"IntegerField": NUMBER, |
| 85 |
"PositiveBigIntegerField": NUMBER, |
| 86 |
"PositiveIntegerField": NUMBER, |
| 87 |
"PositiveSmallIntegerField": NUMBER, |
| 88 |
"SmallAutoField": NUMBER, |
| 89 |
"SmallIntegerField": NUMBER, |
| 90 |
"TextField": NCLOB, |
| 91 |
"TimeField": TIMESTAMP, |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
def dsn(settings_dict): |
| 96 |
if settings_dict["PORT"]: |
| 97 |
host = settings_dict["HOST"].strip() or "localhost" |
| 98 |
return Database.makedsn(host, int(settings_dict["PORT"]), settings_dict["NAME"]) |
| 99 |
return settings_dict["NAME"] |