From 3db84b174ad9a6c7ba64a8dfd1746905651ca04f Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C5=82awek=20Ehlert?= Date: Wed, 2 Apr 2014 13:45:53 -0400 Subject: [PATCH] - cx_Oracle.makedsn can now be passed service_name; squash commit of pr152 --- doc/build/changelog/changelog_10.rst | 8 ++++++++ lib/sqlalchemy/dialects/oracle/cx_oracle.py | 22 +++++++++++++++++++-- test/dialect/test_oracle.py | 20 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/doc/build/changelog/changelog_10.rst b/doc/build/changelog/changelog_10.rst index ca0ae4d526..10d6dc786d 100644 --- a/doc/build/changelog/changelog_10.rst +++ b/doc/build/changelog/changelog_10.rst @@ -23,6 +23,14 @@ series as well. For changes that are specific to 1.0 with an emphasis on compatibility concerns, see :doc:`/changelog/migration_10`. + .. change:: + :tags: feature, oracle + :pullreq: github:152 + + Added support for cx_oracle connections to a specific service + name, as opposed to a tns name, by passing ``?service_name=`` + to the URL. Pull request courtesy Sławomir Ehlert. + .. change:: :tags: feature, mysql :tickets: 3155 diff --git a/lib/sqlalchemy/dialects/oracle/cx_oracle.py b/lib/sqlalchemy/dialects/oracle/cx_oracle.py index 4a1ceecb15..4cd6fe6396 100644 --- a/lib/sqlalchemy/dialects/oracle/cx_oracle.py +++ b/lib/sqlalchemy/dialects/oracle/cx_oracle.py @@ -61,6 +61,12 @@ on the URL, or as keyword arguments to :func:`.create_engine()` are: Defaults to ``True``. Note that this is the opposite default of the cx_Oracle DBAPI itself. +* ``service_name`` - An option to use connection string (DSN) with + ``SERVICE_NAME`` instead of ``SID``. It can't be passed when a ``database`` + part is given. + E.g. ``oracle+cx_oracle://scott:tiger@host:1521/?service_name=hr`` + is a valid url. This value is only available as a URL query string argument. + .. _cx_oracle_unicode: Unicode @@ -862,14 +868,26 @@ class OracleDialect_cx_oracle(OracleDialect): util.coerce_kw_type(dialect_opts, opt, bool) setattr(self, opt, dialect_opts[opt]) - if url.database: + database = url.database + service_name = dialect_opts.get('service_name', None) + if database or service_name: # if we have a database, then we have a remote host port = url.port if port: port = int(port) else: port = 1521 - dsn = self.dbapi.makedsn(url.host, port, url.database) + + if database and service_name: + raise exc.InvalidRequestError( + '"service_name" option shouldn\'t ' + 'be used with a "database" part of the url') + if database: + makedsn_kwargs = {'sid': database} + if service_name: + makedsn_kwargs = {'service_name': service_name} + + dsn = self.dbapi.makedsn(url.host, port, **makedsn_kwargs) else: # we have a local tnsname dsn = url.host diff --git a/test/dialect/test_oracle.py b/test/dialect/test_oracle.py index 1e50b90701..3c67f15903 100644 --- a/test/dialect/test_oracle.py +++ b/test/dialect/test_oracle.py @@ -2074,3 +2074,23 @@ class DBLinkReflectionTest(fixtures.TestBase): autoload_with=testing.db, oracle_resolve_synonyms=True) eq_(list(t.c.keys()), ['id', 'data']) eq_(list(t.primary_key), [t.c.id]) + + +class ServiceNameTest(fixtures.TestBase): + __only_on__ = 'oracle+cx_oracle' + + def test_cx_oracle_service_name(self): + url_string = 'oracle+cx_oracle://scott:tiger@host/?service_name=hr' + eng = create_engine(url_string, _initialize=False) + cargs, cparams = eng.dialect.create_connect_args(eng.url) + + assert 'SERVICE_NAME=hr' in cparams['dsn'] + assert 'SID=hr' not in cparams['dsn'] + + def test_cx_oracle_service_name_bad(self): + url_string = 'oracle+cx_oracle://scott:tiger@host/hr1?service_name=hr2' + assert_raises( + exc.InvalidRequestError, + create_engine, url_string, + _initialize=False + ) -- 2.47.2