forked from oracle/python-cx_Oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransforms.c
More file actions
498 lines (442 loc) · 18.1 KB
/
Transforms.c
File metadata and controls
498 lines (442 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//-----------------------------------------------------------------------------
// Copyright 2016, 2017, Oracle and/or its affiliates. All rights reserved.
//
// Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved.
//
// Portions Copyright 2001-2007, Computronix (Canada) Ltd., Edmonton, Alberta,
// Canada. All rights reserved.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Transforms.c
// Provides methods for transforming Oracle data to Python objects or for
// setting Oracle data from Python objects.
//-----------------------------------------------------------------------------
static udt_VariableType vt_Date;
#if ORACLE_VERSION_HEX >= ORACLE_VERSION(12, 1)
//-----------------------------------------------------------------------------
// OracleBooleanToPythonBoolean()
// Return a Python boolean object given an Oracle boolean.
//-----------------------------------------------------------------------------
static PyObject *OracleBooleanToPythonBoolean(
boolean *value) // value to convert
{
PyObject *pythonValue;
pythonValue = (*value) ? Py_True : Py_False;
Py_INCREF(pythonValue);
return pythonValue;
}
#endif
//-----------------------------------------------------------------------------
// OracleDateToPythonDate()
// Return a Python date object given an Oracle date.
//-----------------------------------------------------------------------------
static PyObject *OracleDateToPythonDate(
udt_VariableType *varType, // variable type
OCIDate* value) // value to convert
{
ub1 hour, minute, second, month, day;
sb2 year;
OCIDateGetDate(value, &year, &month, &day);
OCIDateGetTime(value, &hour, &minute, &second);
if (varType == &vt_Date)
return PyDate_FromDate(year, month, day);
return PyDateTime_FromDateAndTime(year, month, day, hour, minute, second,
0);
}
//-----------------------------------------------------------------------------
// OracleIntervalToPythonDelta()
// Return a Python delta object given an Oracle interval.
//-----------------------------------------------------------------------------
static PyObject *OracleIntervalToPythonDelta(
udt_Environment *environment, // environment
OCIInterval *value) // value to convert
{
sb4 days, hours, minutes, seconds, fseconds;
sword status;
status = OCIIntervalGetDaySecond(environment->handle,
environment->errorHandle, &days, &hours, &minutes, &seconds,
&fseconds, value);
if (Environment_CheckForError(environment, status,
"OracleIntervalToPythonDelta()") < 0)
return NULL;
seconds = hours * 60 * 60 + minutes * 60 + seconds;
return PyDelta_FromDSU(days, seconds, fseconds / 1000);
}
//-----------------------------------------------------------------------------
// OracleTimestampToPythonDate()
// Return a Python date object given an Oracle timestamp.
//-----------------------------------------------------------------------------
static PyObject *OracleTimestampToPythonDate(
udt_Environment *environment, // environment
OCIDateTime* value) // value to convert
{
ub1 hour, minute, second, month, day;
sword status;
ub4 fsecond;
sb2 year;
status = OCIDateTimeGetDate(environment->handle, environment->errorHandle,
value, &year, &month, &day);
if (Environment_CheckForError(environment, status,
"OracleTimestampToPythonDate(): date portion") < 0)
return NULL;
status = OCIDateTimeGetTime(environment->handle, environment->errorHandle,
value, &hour, &minute, &second, &fsecond);
if (Environment_CheckForError(environment, status,
"OracleTimestampToPythonDate(): time portion") < 0)
return NULL;
return PyDateTime_FromDateAndTime(year, month, day, hour, minute, second,
fsecond / 1000);
}
//-----------------------------------------------------------------------------
// OracleNumberToPythonFloat()
// Return a Python float given an Oracle number.
//-----------------------------------------------------------------------------
static PyObject *OracleNumberToPythonFloat(
udt_Environment *environment, // environment
OCINumber* value) // value to convert
{
double doubleValue;
sword status;
status = OCINumberToReal(environment->errorHandle,
value, sizeof(double), (dvoid*) &doubleValue);
if (Environment_CheckForError(environment, status,
"OracleNumberToPythonFloat()") < 0)
return NULL;
return PyFloat_FromDouble(doubleValue);
}
//-----------------------------------------------------------------------------
// OracleNumberToPythonInteger()
// Return a Python integer given an Oracle number.
//-----------------------------------------------------------------------------
static PyObject *OracleNumberToPythonInteger(
udt_Environment *environment, // environment
OCINumber* value) // value to convert
{
long integerValue;
sword status;
status = OCINumberToInt(environment->errorHandle, value,
sizeof(long), OCI_NUMBER_SIGNED, (dvoid*) &integerValue);
if (Environment_CheckForError(environment, status,
"OracleNumberToPythonInteger()") < 0)
return NULL;
return PyInt_FromLong(integerValue);
}
#if ORACLE_VERSION_HEX >= ORACLE_VERSION(12, 1)
//-----------------------------------------------------------------------------
// PythonBooleanToOracleBoolean()
// Transform a Python boolean into an Oracle boolean.
//-----------------------------------------------------------------------------
static int PythonBooleanToOracleBoolean(
PyObject *pythonValue, // Python value to convert
boolean *oracleValue) // value to convert
{
*oracleValue = (pythonValue == Py_True);
return 0;
}
#endif
//-----------------------------------------------------------------------------
// PythonBooleanToOracleNumber()
// Transform a Python boolean into an Oracle number.
//-----------------------------------------------------------------------------
static int PythonBooleanToOracleNumber(
udt_Environment *environment, // environment
PyObject *pythonValue, // Python value to convert
OCINumber *oracleValue) // Oracle value to set
{
long integerValue;
sword status;
integerValue = (pythonValue == Py_True);
status = OCINumberFromInt(environment->errorHandle, &integerValue,
sizeof(long), OCI_NUMBER_SIGNED, oracleValue);
return Environment_CheckForError(environment, status,
"PythonBooleanToOracleNumber()");
}
//-----------------------------------------------------------------------------
// PythonDateToOracleDate()
// Transform a Python date into an Oracle date.
//-----------------------------------------------------------------------------
static int PythonDateToOracleDate(
PyObject *pythonValue, // Python value to convert
OCIDate *oracleValue) // Oracle value to set
{
ub1 month, day, hour, minute, second;
sb2 year;
if (PyDateTime_Check(pythonValue)) {
year = (short) PyDateTime_GET_YEAR(pythonValue);
month = PyDateTime_GET_MONTH(pythonValue);
day = PyDateTime_GET_DAY(pythonValue);
hour = PyDateTime_DATE_GET_HOUR(pythonValue);
minute = PyDateTime_DATE_GET_MINUTE(pythonValue);
second = PyDateTime_DATE_GET_SECOND(pythonValue);
} else if (PyDate_Check(pythonValue)) {
year = (short) PyDateTime_GET_YEAR(pythonValue);
month = PyDateTime_GET_MONTH(pythonValue);
day = PyDateTime_GET_DAY(pythonValue);
hour = minute = second = 0;
} else {
PyErr_SetString(PyExc_TypeError, "expecting date data");
return -1;
}
OCIDateSetDate(oracleValue, year, month, day);
OCIDateSetTime(oracleValue, hour, minute, second);
return 0;
}
#if PY_MAJOR_VERSION < 3
//-----------------------------------------------------------------------------
// PythonIntegerToOracleNumber()
// Transform a Python integer into an Oracle number.
//-----------------------------------------------------------------------------
static int PythonIntegerToOracleNumber(
udt_Environment *environment, // environment
PyObject *pythonValue, // Python value to convert
OCINumber *oracleValue) // Oracle value to set
{
long integerValue;
sword status;
integerValue = PyInt_AS_LONG(pythonValue);
status = OCINumberFromInt(environment->errorHandle, &integerValue,
sizeof(long), OCI_NUMBER_SIGNED, oracleValue);
return Environment_CheckForError(environment, status,
"PythonIntegerToOracleNumber()");
}
#endif
//-----------------------------------------------------------------------------
// PythonFloatToOracleNumber()
// Transform a Python float into an Oracle number.
//-----------------------------------------------------------------------------
static int PythonFloatToOracleNumber(
udt_Environment *environment, // environment
PyObject *pythonValue, // Python value to convert
OCINumber *oracleValue) // Oracle value to set
{
double doubleValue;
sword status;
doubleValue = PyFloat_AS_DOUBLE(pythonValue);
if (isnan(doubleValue)) {
PyErr_SetString(g_DatabaseErrorException,
"value is not a number (NaN) and cannot be used in Oracle "
"numbers");
return -1;
}
status = OCINumberFromReal(environment->errorHandle, &doubleValue,
sizeof(double), oracleValue);
return Environment_CheckForError(environment, status,
"PythonFloatToOracleNumber()");
}
//-----------------------------------------------------------------------------
// PythonLongToOracleNumber()
// Set the value of the variable from a Python long.
//-----------------------------------------------------------------------------
static int PythonLongToOracleNumber(
udt_Environment *environment, // environment
PyObject *pythonValue, // Python value to convert
OCINumber *oracleValue) // Oracle value to set
{
udt_Buffer textBuffer;
PyObject *textValue;
sword status;
textValue = PyObject_Str(pythonValue);
if (!textValue)
return -1;
if (cxBuffer_FromObject(&textBuffer, textValue, environment->encoding) < 0)
return -1;
status = OCINumberFromText(environment->errorHandle,
(text*) textBuffer.ptr, (ub4) textBuffer.size,
(text*) environment->numberFromStringFormatBuffer.ptr,
(ub4) environment->numberFromStringFormatBuffer.size, NULL, 0,
oracleValue);
cxBuffer_Clear(&textBuffer);
Py_DECREF(textValue);
return Environment_CheckForError(environment, status,
"PythonLongToOracleNumber()");
}
//-----------------------------------------------------------------------------
// GetFormatAndTextFromPythonDecimal()
// Return the number format and text to use for the Decimal object.
//-----------------------------------------------------------------------------
static int GetFormatAndTextFromPythonDecimal(
PyObject *tupleValue, // decimal as_tuple() value
PyObject **textObj, // text string for conversion
PyObject **formatObj) // format for conversion
{
Py_ssize_t numDigits, scale, i, sign, length, digit;
char *textValue, *format, *textPtr, *formatPtr;
PyObject *digits;
// acquire basic information from the value tuple
sign = PyInt_AsLong(PyTuple_GET_ITEM(tupleValue, 0));
if (PyErr_Occurred())
return -1;
digits = PyTuple_GET_ITEM(tupleValue, 1);
scale = PyInt_AsLong(PyTuple_GET_ITEM(tupleValue, 2));
if (PyErr_Occurred())
return -1;
numDigits = PyTuple_GET_SIZE(digits);
// allocate memory for the string and format to use in conversion
length = numDigits + abs( (long) scale) + 3;
textValue = textPtr = PyMem_Malloc(length);
if (!textValue) {
PyErr_NoMemory();
return -1;
}
format = formatPtr = PyMem_Malloc(length);
if (!format) {
PyMem_Free(textValue);
PyErr_NoMemory();
return -1;
}
// populate the string and format
if (sign)
*textPtr++ = '-';
for (i = 0; i < numDigits + scale; i++) {
*formatPtr++ = '9';
if (i < numDigits) {
digit = PyInt_AsLong(PyTuple_GetItem(digits, i));
if (PyErr_Occurred()) {
PyMem_Free(textValue);
return -1;
}
}
else digit = 0;
*textPtr++ = '0' + (char) digit;
}
if (scale < 0) {
*formatPtr++ = 'D';
*textPtr++ = '.';
for (i = scale; i < 0; i++) {
*formatPtr++ = '9';
if (numDigits + i < 0)
digit = 0;
else {
digit = PyInt_AsLong(PyTuple_GetItem(digits, numDigits + i));
if (PyErr_Occurred()) {
PyMem_Free(textValue);
return -1;
}
}
*textPtr++ = '0' + (char) digit;
}
}
*formatPtr = '\0';
*textPtr = '\0';
*textObj = cxString_FromAscii(textValue);
PyMem_Free(textValue);
if (!*textObj) {
PyMem_Free(format);
return -1;
}
*formatObj = cxString_FromAscii(format);
PyMem_Free(format);
if (!*formatObj) {
Py_DECREF(*textObj);
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
// PythonDecimalToOracleNumber()
// Transform a Python decimal object into an Oracle number.
//-----------------------------------------------------------------------------
static int PythonDecimalToOracleNumber(
udt_Environment *environment, // environment
PyObject *pythonValue, // Python value to convert
OCINumber *oracleValue) // Oracle value to set
{
PyObject *textValue, *format, *tupleValue;
udt_Buffer textBuffer, formatBuffer;
sword status;
tupleValue = PyObject_CallMethod(pythonValue, "as_tuple", NULL);
if (!tupleValue)
return -1;
if (GetFormatAndTextFromPythonDecimal(tupleValue, &textValue,
&format) < 0) {
Py_DECREF(tupleValue);
return -1;
}
Py_DECREF(tupleValue);
if (cxBuffer_FromObject(&textBuffer, textValue, environment->encoding) < 0)
return -1;
if (cxBuffer_FromObject(&formatBuffer, format,
environment->encoding) < 0) {
cxBuffer_Clear(&textBuffer);
return -1;
}
status = OCINumberFromText(environment->errorHandle,
(text*) textBuffer.ptr, (ub4) textBuffer.size,
(text*) formatBuffer.ptr, (ub4) formatBuffer.size,
environment->nlsNumericCharactersBuffer.ptr,
(ub4) environment->nlsNumericCharactersBuffer.size, oracleValue);
cxBuffer_Clear(&textBuffer);
cxBuffer_Clear(&formatBuffer);
Py_DECREF(textValue);
Py_DECREF(format);
return Environment_CheckForError(environment, status,
"PythonDecimalToOracleNumber()");
}
//-----------------------------------------------------------------------------
// PythonNumberToOracleNumber()
// Convert a Python number to an Oracle number.
//-----------------------------------------------------------------------------
static int PythonNumberToOracleNumber(
udt_Environment *environment, // environment
PyObject *pythonValue, // Python value
OCINumber* oracleValue) // Oracle value
{
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(pythonValue))
return PythonIntegerToOracleNumber(environment, pythonValue,
oracleValue);
#endif
if (PyBool_Check(pythonValue))
return PythonBooleanToOracleNumber(environment, pythonValue,
oracleValue);
if (PyLong_Check(pythonValue))
return PythonLongToOracleNumber(environment, pythonValue, oracleValue);
if (PyFloat_Check(pythonValue))
return PythonFloatToOracleNumber(environment, pythonValue,
oracleValue);
if (Py_TYPE(pythonValue) == g_DecimalType)
return PythonDecimalToOracleNumber(environment, pythonValue,
oracleValue);
PyErr_SetString(PyExc_TypeError, "expecting numeric data");
return -1;
}
//-----------------------------------------------------------------------------
// PythonDateToOracleTimestamp()
// Convert a Python date to an Oracle timestamp.
//-----------------------------------------------------------------------------
static int PythonDateToOracleTimestamp(
udt_Environment *environment, // environment
PyObject *pythonValue, // Python value
OCIDateTime* oracleValue) // Oracle value
{
sword status;
uword valid;
// make sure a timestamp is being bound
if (!PyDateTime_Check(pythonValue)) {
PyErr_SetString(PyExc_TypeError, "expecting timestamp data");
return -1;
}
// store a copy of the value
status = OCIDateTimeConstruct(environment->handle,
environment->errorHandle, oracleValue,
(sb2) PyDateTime_GET_YEAR(pythonValue),
PyDateTime_GET_MONTH(pythonValue),
PyDateTime_GET_DAY(pythonValue),
PyDateTime_DATE_GET_HOUR(pythonValue),
PyDateTime_DATE_GET_MINUTE(pythonValue),
PyDateTime_DATE_GET_SECOND(pythonValue),
PyDateTime_DATE_GET_MICROSECOND(pythonValue) * 1000, NULL, 0);
if (Environment_CheckForError(environment, status,
"PythonDateToOracleTimestamp(): create structure") < 0)
return -1;
status = OCIDateTimeCheck(environment->handle, environment->errorHandle,
oracleValue, &valid);
if (Environment_CheckForError(environment, status,
"PythonDateToOracleTimestamp(): check validity") < 0)
return -1;
if (valid != 0) {
PyErr_SetString(g_DataErrorException, "invalid date");
return -1;
}
return 0;
}