1
1
import datetime
2
2
from decimal import Decimal
3
3
from sqlalchemy .sql import sqltypes
4
+ from uuid import UUID as _python_UUID
4
5
5
6
HOROLOG_ORDINAL = datetime .date (1840 , 12 , 31 ).toordinal ()
6
7
@@ -16,13 +17,15 @@ def process(value):
16
17
elif isinstance (value , bool ):
17
18
return 1 if value is True else 0
18
19
return None
20
+
19
21
return process
20
22
21
23
def result_processor (self , dialect , coltype ):
22
24
def process (value ):
23
25
if isinstance (value , int ):
24
26
return value > 0
25
27
return value
28
+
26
29
return process
27
30
28
31
@@ -40,8 +43,8 @@ def result_processor(self, dialect, coltype):
40
43
def process (value ):
41
44
if value is None :
42
45
return None
43
- if isinstance (value , str ) and '-' in value [1 :]:
44
- return datetime .datetime .strptime (value , ' %Y-%m-%d' ).date ()
46
+ if isinstance (value , str ) and "-" in value [1 :]:
47
+ return datetime .datetime .strptime (value , " %Y-%m-%d" ).date ()
45
48
horolog = int (value ) + HOROLOG_ORDINAL
46
49
return datetime .date .fromordinal (horolog )
47
50
@@ -54,19 +57,19 @@ def process(value: datetime.datetime):
54
57
if value is not None :
55
58
# value = int(value.timestamp() * 1000000)
56
59
# value += (2 ** 60) if value > 0 else -(2 ** 61 * 3)
57
- return value .strftime (' %Y-%m-%d %H:%M:%S.%f' )
60
+ return value .strftime (" %Y-%m-%d %H:%M:%S.%f" )
58
61
return value
59
62
60
63
return process
61
64
62
65
def result_processor (self , dialect , coltype ):
63
66
def process (value ):
64
67
if isinstance (value , str ):
65
- if '.' not in value :
66
- value += '.0'
67
- return datetime .datetime .strptime (value , ' %Y-%m-%d %H:%M:%S.%f' )
68
+ if "." not in value :
69
+ value += ".0"
70
+ return datetime .datetime .strptime (value , " %Y-%m-%d %H:%M:%S.%f" )
68
71
if isinstance (value , int ):
69
- value -= (2 ** 60 ) if value > 0 else - (2 ** 61 * 3 )
72
+ value -= (2 ** 60 ) if value > 0 else - (2 ** 61 * 3 )
70
73
value = value / 1000000
71
74
value = datetime .datetime .utcfromtimestamp (value )
72
75
return value
@@ -78,17 +81,17 @@ class IRISDateTime(sqltypes.DateTime):
78
81
def bind_processor (self , dialect ):
79
82
def process (value ):
80
83
if value is not None :
81
- return value .strftime (' %Y-%m-%d %H:%M:%S.%f' )
84
+ return value .strftime (" %Y-%m-%d %H:%M:%S.%f" )
82
85
return value
83
86
84
87
return process
85
88
86
89
def result_processor (self , dialect , coltype ):
87
90
def process (value ):
88
91
if isinstance (value , str ):
89
- if '.' not in value :
90
- value += '.0'
91
- return datetime .datetime .strptime (value , ' %Y-%m-%d %H:%M:%S.%f' )
92
+ if "." not in value :
93
+ value += ".0"
94
+ return datetime .datetime .strptime (value , " %Y-%m-%d %H:%M:%S.%f" )
92
95
return value
93
96
94
97
return process
@@ -98,17 +101,17 @@ class IRISTime(sqltypes.DateTime):
98
101
def bind_processor (self , dialect ):
99
102
def process (value ):
100
103
if value is not None :
101
- return value .strftime (' %H:%M:%S.%f' )
104
+ return value .strftime (" %H:%M:%S.%f" )
102
105
return value
103
106
104
107
return process
105
108
106
109
def result_processor (self , dialect , coltype ):
107
110
def process (value ):
108
111
if isinstance (value , str ):
109
- if '.' not in value :
110
- value += '.0'
111
- return datetime .datetime .strptime (value , ' %H:%M:%S.%f' ).time ()
112
+ if "." not in value :
113
+ value += ".0"
114
+ return datetime .datetime .strptime (value , " %H:%M:%S.%f" ).time ()
112
115
if isinstance (value , int ) or isinstance (value , Decimal ):
113
116
horolog = value
114
117
hour = int (horolog // 3600 )
@@ -122,6 +125,75 @@ def process(value):
122
125
return process
123
126
124
127
128
+ class IRISUniqueIdentifier (sqltypes .Uuid ):
129
+ def literal_processor (self , dialect ):
130
+ if not self .as_uuid :
131
+
132
+ def process (value ):
133
+ return f"""'{ value .replace ("'" , "''" )} '"""
134
+
135
+ return process
136
+ else :
137
+
138
+ def process (value ):
139
+ return f"""'{ str (value ).replace ("'" , "''" )} '"""
140
+
141
+ return process
142
+
143
+ def bind_processor (self , dialect ):
144
+ character_based_uuid = not dialect .supports_native_uuid or not self .native_uuid
145
+
146
+ if character_based_uuid :
147
+ if self .as_uuid :
148
+
149
+ def process (value ):
150
+ if value is not None :
151
+ value = str (value )
152
+ return value
153
+
154
+ return process
155
+ else :
156
+
157
+ def process (value ):
158
+ return value
159
+
160
+ return process
161
+ else :
162
+ return None
163
+
164
+ def result_processor (self , dialect , coltype ):
165
+ character_based_uuid = not dialect .supports_native_uuid or not self .native_uuid
166
+
167
+ if character_based_uuid :
168
+ if self .as_uuid :
169
+
170
+ def process (value ):
171
+ if value and not isinstance (value , _python_UUID ):
172
+ value = _python_UUID (value )
173
+ return value
174
+
175
+ return process
176
+ else :
177
+
178
+ def process (value ):
179
+ if value and isinstance (value , _python_UUID ):
180
+ value = str (value )
181
+ return value
182
+
183
+ return process
184
+ else :
185
+ if not self .as_uuid :
186
+
187
+ def process (value ):
188
+ if value and isinstance (value , _python_UUID ):
189
+ value = str (value )
190
+ return value
191
+
192
+ return process
193
+ else :
194
+ return None
195
+
196
+
125
197
class BIT (sqltypes .TypeEngine ):
126
198
__visit_name__ = "BIT"
127
199
0 commit comments