From: Federico Caselli Date: Sat, 26 Oct 2024 19:50:36 +0000 (+0200) Subject: Improve Oracle identifier length detection X-Git-Tag: rel_2_0_37~42^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a3d6a26a81ebe7629d3931857f5b0bf5c406cd38;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Improve Oracle identifier length detection Use the connection attribute ``max_identifier_length`` available in oracledb since version 2.5 when determining the identifier length in the Oracle dialect. Fixes: #12032 Change-Id: If16db93e0df25776295bc521706dbad1cc541f4a (cherry picked from commit 90bf575b81c5396b364908547551b6592a333bf7) --- diff --git a/doc/build/changelog/unreleased_20/12032.rst b/doc/build/changelog/unreleased_20/12032.rst new file mode 100644 index 0000000000..5a40732980 --- /dev/null +++ b/doc/build/changelog/unreleased_20/12032.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: oracle, usecase + :tickets: 12032 + + Use the connection attribute ``max_identifier_length`` available + in oracledb since version 2.5 when determining the identifier length + in the Oracle dialect. diff --git a/lib/sqlalchemy/dialects/oracle/base.py b/lib/sqlalchemy/dialects/oracle/base.py index abf3645c76..30b575140f 100644 --- a/lib/sqlalchemy/dialects/oracle/base.py +++ b/lib/sqlalchemy/dialects/oracle/base.py @@ -176,12 +176,15 @@ To assist with this change and others, Oracle includes the concept of a actual server version in order to assist with migration of Oracle databases, and may be configured within the Oracle server itself. This compatibility version is retrieved using the query ``SELECT value FROM v$parameter WHERE -name = 'compatible';``. The SQLAlchemy Oracle dialect, when tasked with -determining the default max identifier length, will attempt to use this query -upon first connect in order to determine the effective compatibility version of -the server, which determines what the maximum allowed identifier length is for -the server. If the table is not available, the server version information is -used instead. +name = 'compatible';``. +The SQLAlchemy Oracle dialect, when tasked with determining the default max +identifier length, will use the ``max_identifier_length`` attribute available +in the connection of the oracledb driver since version 2.5. When using an older +version or cx_oracle SQLAlchemy will instead attempted to use the query +mentioned above upon first connect in order to determine the effective +compatibility version of the server, which determines what the maximum allowed +identifier length is for the server. If the table is not available, the server +version information is used instead. As of SQLAlchemy 1.4, the default max identifier length for the Oracle dialect is 128 characters. Upon first connect, the compatibility version is detected diff --git a/lib/sqlalchemy/dialects/oracle/oracledb.py b/lib/sqlalchemy/dialects/oracle/oracledb.py index 0667ed768e..bdeb535e68 100644 --- a/lib/sqlalchemy/dialects/oracle/oracledb.py +++ b/lib/sqlalchemy/dialects/oracle/oracledb.py @@ -221,6 +221,12 @@ class OracleDialect_oracledb(_cx_oracle.OracleDialect_cx_oracle): for fi, gti, bq in connection.connection.tpc_recover() ] + def _check_max_identifier_length(self, connection): + if self.oracledb_ver >= (2, 5): + return connection.connection.max_identifier_length + else: + super()._check_max_identifier_length(connection) + class AsyncAdapt_oracledb_cursor(AsyncAdapt_dbapi_cursor): _cursor: AsyncCursor @@ -251,7 +257,7 @@ class AsyncAdapt_oracledb_cursor(AsyncAdapt_dbapi_cursor): self._adapt_connection._handle_exception(error) async def _execute_async(self, operation, parameters): - # override to not use mutex, oracledb already has mutex + # override to not use mutex, oracledb already has a mutex if parameters is None: result = await self._cursor.execute(operation) @@ -267,7 +273,7 @@ class AsyncAdapt_oracledb_cursor(AsyncAdapt_dbapi_cursor): operation, seq_of_parameters, ): - # override to not use mutex, oracledb already has mutex + # override to not use mutex, oracledb already has a mutex return await self._cursor.executemany(operation, seq_of_parameters) def __enter__(self): @@ -325,6 +331,10 @@ class AsyncAdapt_oracledb_connection(AsyncAdapt_dbapi_connection): def stmtcachesize(self, value): self._connection.stmtcachesize = value + @property + def max_identifier_length(self): + return self._connection.max_identifier_length + def cursor(self): return AsyncAdapt_oracledb_cursor(self)