Skip to content

Commit 66c26a0

Browse files
Restore support for setting numeric bind variables with boolean values.
1 parent 02d36b4 commit 66c26a0

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

Diff for: src/cxoTransform.c

+19-13
Original file line numberDiff line numberDiff line change
@@ -274,22 +274,28 @@ int cxoTransform_fromPython(cxoTransformNum transformNum,
274274
case CXO_TRANSFORM_INT:
275275
case CXO_TRANSFORM_DECIMAL:
276276
case CXO_TRANSFORM_FLOAT:
277-
if (!PyFloat_Check(pyValue) &&
277+
if (PyBool_Check(pyValue)) {
278+
buffer->ptr = (pyValue == Py_True) ? "1" : "0";
279+
buffer->size = 1;
280+
buffer->numCharacters = 1;
281+
} else {
282+
if (!PyFloat_Check(pyValue) &&
278283
#if PY_MAJOR_VERSION < 3
279-
!PyInt_Check(pyValue) &&
284+
!PyInt_Check(pyValue) &&
280285
#endif
281-
!PyLong_Check(pyValue) &&
282-
!PyObject_TypeCheck(pyValue, cxoPyTypeDecimal)) {
283-
PyErr_SetString(PyExc_TypeError, "expecting number");
284-
return -1;
286+
!PyLong_Check(pyValue) &&
287+
!PyObject_TypeCheck(pyValue, cxoPyTypeDecimal)) {
288+
PyErr_SetString(PyExc_TypeError, "expecting number");
289+
return -1;
290+
}
291+
textValue = PyObject_Str(pyValue);
292+
if (!textValue)
293+
return -1;
294+
status = cxoBuffer_fromObject(buffer, textValue, encoding);
295+
Py_DECREF(textValue);
296+
if (status < 0)
297+
return -1;
285298
}
286-
textValue = PyObject_Str(pyValue);
287-
if (!textValue)
288-
return -1;
289-
status = cxoBuffer_fromObject(buffer, textValue, encoding);
290-
Py_DECREF(textValue);
291-
if (status < 0)
292-
return -1;
293299
dbValue->asBytes.ptr = (char*) buffer->ptr;
294300
dbValue->asBytes.length = buffer->size;
295301
return 0;

Diff for: test/NumberVar.py

+12
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ def testBindBoolean(self):
5050
(True,))
5151
self.assertEqual(result, "TRUE")
5252

53+
def testBindBooleanAsNumber(self):
54+
"test binding in a boolean as a number"
55+
var = self.cursor.var(cx_Oracle.NUMBER)
56+
var.setvalue(0, True)
57+
self.cursor.execute("select :1 from dual", [var])
58+
result, = self.cursor.fetchone()
59+
self.assertEqual(result, 1)
60+
var.setvalue(0, False)
61+
self.cursor.execute("select :1 from dual", [var])
62+
result, = self.cursor.fetchone()
63+
self.assertEqual(result, 0)
64+
5365
def testBindDecimal(self):
5466
"test binding in a decimal.Decimal"
5567
self.cursor.execute("""

0 commit comments

Comments
 (0)