Skip to content

Commit

Permalink
Use cx_Oracle dml_ret_array_val
Browse files Browse the repository at this point in the history
Fixed issue for cx_Oracle 7.0 where the behavior of Oracle param.getvalue()
now returns a list, rather than a single scalar value, breaking
autoincrement logic throughout the Core and ORM. The dml_ret_array_val
compatibility flag is used for cx_Oracle 6.3 and 6.4 to establish compatible
behavior with 7.0 and forward, for cx_Oracle 6.2.1 and prior a version
number check falls back to the old logic.

Fixes: #4335
Change-Id: Ia60f5514803a505898c1ac9252355990c6203dda
  • Loading branch information
zzzeek committed Sep 17, 2018
1 parent c94d678 commit 67a2cd9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
11 changes: 11 additions & 0 deletions doc/build/changelog/unreleased_12/4335.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.. change::
:tags: bug, oracle
:tickets: 4335

Fixed issue for cx_Oracle 7.0 where the behavior of Oracle param.getvalue()
now returns a list, rather than a single scalar value, breaking
autoincrement logic throughout the Core and ORM. The dml_ret_array_val
compatibility flag is used for cx_Oracle 6.3 and 6.4 to establish compatible
behavior with 7.0 and forward, for cx_Oracle 6.2.1 and prior a version
number check falls back to the old logic.

32 changes: 27 additions & 5 deletions lib/sqlalchemy/dialects/oracle/cx_oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,9 @@ def create_cursor(self):
def get_result_proxy(self):
if self.out_parameters and self.compiled.returning:
returning_params = [
self.out_parameters["ret_%d" % i].getvalue()
self.dialect._returningval(
self.out_parameters["ret_%d" % i]
)
for i in range(len(self.out_parameters))
]
return ReturningResultProxy(self, returning_params)
Expand All @@ -633,13 +635,15 @@ def get_result_proxy(self):
if result_processor is not None:
out_parameters[name] = \
result_processor(
self.out_parameters[name].getvalue())
self.dialect._paramval(
self.out_parameters[name]
))
else:
out_parameters[name] = self.out_parameters[
name].getvalue()
out_parameters[name] = self.dialect._paramval(
self.out_parameters[name])
else:
result.out_parameters = dict(
(k, v.getvalue())
(k, self._dialect._paramval(v))
for k, v in self.out_parameters.items()
)

Expand Down Expand Up @@ -744,6 +748,24 @@ def __init__(self,
_OracleInteger, _OracleBINARY_FLOAT, _OracleBINARY_DOUBLE
}

self._paramval = lambda value: value.getvalue()

# https://github.com/oracle/python-cx_Oracle/issues/176#issuecomment-386821291
# https://github.com/oracle/python-cx_Oracle/issues/224
self._values_are_lists = self.cx_oracle_ver >= (6, 3)
if self._values_are_lists:
cx_Oracle.__future__.dml_ret_array_val = True

def _returningval(value):
try:
return value.values[0][0]
except IndexError:
return None

self._returningval = _returningval
else:
self._returningval = self._paramval

self._is_cx_oracle_6 = self.cx_oracle_ver >= (6, )

def _pop_deprecated_kwargs(self, kwargs):
Expand Down
13 changes: 7 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ deps=pytest
mysql: pymysql
mysql: mysql-connector-python
# waiting for https://github.com/oracle/python-cx_Oracle/issues/75
oracle: cx_oracle>=6.0.2,!=6.3,!=6.4
oracle: cx_oracle>=7.0
oracle6: cx_oracle>=6.0.2,!=6.3,!=6.4,<7.0
oracle5: cx_oracle==5.2.1
mssql: pyodbc
mssql: pymssql
Expand Down Expand Up @@ -51,16 +52,16 @@ setenv=
BASECOMMAND=python -m pytest --log-info=sqlalchemy.testing

WORKERS={env:TOX_WORKERS:-n4}
oracle,oracle5: WORKERS={env:TOX_WORKERS:-n2}
oracle,oracle6,oracle5: WORKERS={env:TOX_WORKERS:-n2}
nocext: DISABLE_SQLALCHEMY_CEXT=1
cov: COVERAGE={[testenv]cov_args}
sqlite: SQLITE={env:TOX_SQLITE:--db sqlite}
postgresql: POSTGRESQL={env:TOX_POSTGRESQL:--db postgresql}
mysql: MYSQL={env:TOX_MYSQL:--db mysql --db pymysql}
oracle,oracle5: ORACLE={env:TOX_ORACLE:--db oracle}
oracle,oracle6,oracle5: ORACLE={env:TOX_ORACLE:--db oracle}
mssql: MSSQL={env:TOX_MSSQL:--db mssql --db mssql_pymssql}
oracle,oracle5,mssql: IDENTS=--write-idents db_idents.txt
oracle,oracle5,mssql: NOMEMORY=--nomemory
oracle,oracle6,oracle5,mssql: IDENTS=--write-idents db_idents.txt
oracle,oracle6,oracle5,mssql: NOMEMORY=--nomemory
backendonly: BACKENDONLY=--backend-only

# tox as of 2.0 blocks all environment variables from the
Expand All @@ -72,7 +73,7 @@ passenv=ORACLE_HOME NLS_LANG TOX_POSTGRESQL TOX_MYSQL TOX_ORACLE TOX_MSSQL TOX_S
commands=
nocext: sh -c "rm -f lib/sqlalchemy/*.so"
{env:BASECOMMAND} {env:WORKERS} {env:SQLITE:} {env:POSTGRESQL:} {env:MYSQL:} {env:ORACLE:} {env:MSSQL:} {env:BACKENDONLY:} {env:IDENTS:} {env:NOMEMORY:} {env:COVERAGE:} {posargs}
oracle,oracle5,mssql: python reap_dbs.py db_idents.txt
oracle,oracle6,oracle5,mssql: python reap_dbs.py db_idents.txt

[testenv:pep8]
deps=flake8
Expand Down

0 comments on commit 67a2cd9

Please sign in to comment.