Skip to content

Commit 007c4c4

Browse files
Attribute and element values of DbObject instances that are numbers are
now returned as integers if the precision and scale allow for it -- in the same way that numbers are fetched from the database (#99).
1 parent dc2365c commit 007c4c4

File tree

9 files changed

+176
-99
lines changed

9 files changed

+176
-99
lines changed

doc/src/release_notes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ Common Changes
4949
:data:`FetchInfo.type_code` for data of this type was
5050
:data:`~oracledb.DB_TYPE_LONG` in Thick mode and
5151
:data:`~oracledb.DB_TYPE_OBJECT` in Thin mode.
52+
#) Attribute and element values of DbObject instances that are numbers are now
53+
returned as integers if the precision and scale allow for it -- in the same
54+
way that numbers are fetched from the database
55+
(`issue 99 <https://github.com/oracle/python-oracledb/issues/99>`__).
5256
#) Added support for parsing the ``FAILOVER`` clause in full connect
5357
descriptors.
5458
#) Fixed bug with getting unknown attributes from DbObject instances.

src/oracledb/base_impl.pxd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,15 @@ cdef class BaseDbObjectTypeImpl:
379379
readonly DbType element_dbtype
380380
readonly BaseDbObjectTypeImpl element_objtype
381381
readonly BaseConnImpl _conn_impl
382+
int _element_preferred_num_type
382383

383384

384385
cdef class BaseDbObjectAttrImpl:
385386
cdef:
386387
readonly str name
387388
readonly DbType dbtype
388389
readonly BaseDbObjectTypeImpl objtype
390+
int _preferred_num_type
389391

390392

391393
cdef class BaseDbObjectImpl:
@@ -474,3 +476,4 @@ cdef class BindVar:
474476
bint defer_type_assignment) except -1
475477

476478
cdef object get_exception_class(int32_t code)
479+
cdef int get_preferred_num_type(int16_t precision, int8_t scale)

src/oracledb/base_impl.pyx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ cdef object get_exception_class(int32_t code):
140140
return exceptions.DatabaseError
141141

142142

143+
cdef int get_preferred_num_type(int16_t precision, int8_t scale):
144+
if scale == 0 or (scale == -127 and precision == 0):
145+
return NUM_TYPE_INT
146+
return NUM_TYPE_FLOAT
147+
148+
143149
include "impl/base/utils.pyx"
144150
include "impl/base/connect_params.pyx"
145151
include "impl/base/pool_params.pyx"

src/oracledb/impl/thick/dbobject.pyx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ cdef class ThickDbObjectImpl(BaseDbObjectImpl):
116116
type_impl = self.type
117117
try:
118118
return _convert_to_python(type_impl._conn_impl, attr.dbtype,
119-
attr.objtype, &data.value)
119+
attr.objtype, &data.value,
120+
attr._preferred_num_type)
120121
finally:
121122
if attr.objtype is not None:
122123
dpiObject_release(data.value.asObject)
@@ -145,7 +146,8 @@ cdef class ThickDbObjectImpl(BaseDbObjectImpl):
145146
try:
146147
return _convert_to_python(objtype._conn_impl,
147148
objtype.element_dbtype,
148-
objtype.element_objtype, &data.value)
149+
objtype.element_objtype, &data.value,
150+
objtype._element_preferred_num_type)
149151
finally:
150152
if objtype.element_objtype is not None:
151153
dpiObject_release(data.value.asObject)
@@ -285,6 +287,9 @@ cdef class ThickDbObjectAttrImpl(BaseDbObjectAttrImpl):
285287
_raise_from_odpi()
286288
impl.name = info.name[:info.nameLength].decode()
287289
impl.dbtype = DbType._from_num(info.typeInfo.oracleTypeNum)
290+
impl._preferred_num_type = \
291+
get_preferred_num_type(info.typeInfo.precision,
292+
info.typeInfo.scale)
288293
if info.typeInfo.objectType:
289294
typ_handle = info.typeInfo.objectType
290295
impl.objtype = ThickDbObjectTypeImpl._from_handle(conn_impl,
@@ -333,6 +338,9 @@ cdef class ThickDbObjectTypeImpl(BaseDbObjectTypeImpl):
333338
if impl.is_collection:
334339
dbtype = DbType._from_num(info.elementTypeInfo.oracleTypeNum)
335340
impl.element_dbtype = dbtype
341+
impl._element_preferred_num_type = \
342+
get_preferred_num_type(info.elementTypeInfo.precision,
343+
info.elementTypeInfo.scale)
336344
if info.elementTypeInfo.objectType != NULL:
337345
handle = info.elementTypeInfo.objectType
338346
temp = ThickDbObjectTypeImpl._from_handle(conn_impl, handle)

src/oracledb/impl/thin/constants.pxi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,10 @@ cdef enum:
473473
TNS_OBJ_TDS_TYPE_BLOB = 30
474474
TNS_OBJ_TDS_TYPE_TIMESTAMP_LTZ = 33
475475
TNS_OBJ_TDS_TYPE_BINARY_FLOAT = 37
476+
TNS_OBJ_TDS_TYPE_START_EMBED_ADT = 39
477+
TNS_OBJ_TDS_TYPE_END_EMBED_ADT = 40
478+
TNS_OBJ_TDS_TYPE_SUBTYPE_MARKER = 43
479+
TNS_OBJ_TDS_TYPE_EMBED_ADT_INFO = 44
476480
TNS_OBJ_TDS_TYPE_BINARY_DOUBLE = 45
477481

478482
# xml type constants

0 commit comments

Comments
 (0)