Skip to content

Commit 689fa2e

Browse files
chore: Adding schema name property in dbapi connection (#1101)
* chore: Adding schema name property in dbapi connection * small fix * More changes * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Comments incorporated * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Comments incorporated * lint issues fixed * comment incorporated --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 1750328 commit 689fa2e

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

google/cloud/spanner_dbapi/connection.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ def spanner_client(self):
124124
"""
125125
return self._instance._client
126126

127+
@property
128+
def current_schema(self):
129+
"""schema name for this connection.
130+
131+
:rtype: str
132+
:returns: the current default schema of this connection. Currently, this
133+
is always "" for GoogleSQL and "public" for PostgreSQL databases.
134+
"""
135+
if self.database is None:
136+
raise ValueError("database property not set on the connection")
137+
return self.database.default_schema_name
138+
127139
@property
128140
def autocommit(self):
129141
"""Autocommit mode flag for this connection.
@@ -664,9 +676,10 @@ def connect(
664676
raise ValueError("project in url does not match client object project")
665677

666678
instance = client.instance(instance_id)
667-
conn = Connection(
668-
instance, instance.database(database_id, pool=pool) if database_id else None
669-
)
679+
database = None
680+
if database_id:
681+
database = instance.database(database_id, pool=pool)
682+
conn = Connection(instance, database)
670683
if pool is not None:
671684
conn._own_pool = False
672685

tests/unit/spanner_dbapi/test_connection.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import warnings
2121
import pytest
2222

23+
from google.cloud.spanner_admin_database_v1 import DatabaseDialect
2324
from google.cloud.spanner_dbapi.batch_dml_executor import BatchMode
2425
from google.cloud.spanner_dbapi.exceptions import (
2526
InterfaceError,
@@ -58,14 +59,16 @@ def _get_client_info(self):
5859

5960
return ClientInfo(user_agent=USER_AGENT)
6061

61-
def _make_connection(self, **kwargs):
62+
def _make_connection(
63+
self, database_dialect=DatabaseDialect.DATABASE_DIALECT_UNSPECIFIED, **kwargs
64+
):
6265
from google.cloud.spanner_v1.instance import Instance
6366
from google.cloud.spanner_v1.client import Client
6467

6568
# We don't need a real Client object to test the constructor
6669
client = Client()
6770
instance = Instance(INSTANCE, client=client)
68-
database = instance.database(DATABASE)
71+
database = instance.database(DATABASE, database_dialect=database_dialect)
6972
return Connection(instance, database, **kwargs)
7073

7174
@mock.patch("google.cloud.spanner_dbapi.connection.Connection.commit")
@@ -105,6 +108,22 @@ def test_property_instance(self):
105108
self.assertIsInstance(connection.instance, Instance)
106109
self.assertEqual(connection.instance, connection._instance)
107110

111+
def test_property_current_schema_google_sql_dialect(self):
112+
from google.cloud.spanner_v1.database import Database
113+
114+
connection = self._make_connection(
115+
database_dialect=DatabaseDialect.GOOGLE_STANDARD_SQL
116+
)
117+
self.assertIsInstance(connection.database, Database)
118+
self.assertEqual(connection.current_schema, "")
119+
120+
def test_property_current_schema_postgres_sql_dialect(self):
121+
from google.cloud.spanner_v1.database import Database
122+
123+
connection = self._make_connection(database_dialect=DatabaseDialect.POSTGRESQL)
124+
self.assertIsInstance(connection.database, Database)
125+
self.assertEqual(connection.current_schema, "public")
126+
108127
def test_read_only_connection(self):
109128
connection = self._make_connection(read_only=True)
110129
self.assertTrue(connection.read_only)
@@ -745,11 +764,22 @@ def __init__(self, name="instance_id", client=None):
745764
self.name = name
746765
self._client = client
747766

748-
def database(self, database_id="database_id", pool=None):
749-
return _Database(database_id, pool)
767+
def database(
768+
self,
769+
database_id="database_id",
770+
pool=None,
771+
database_dialect=DatabaseDialect.GOOGLE_STANDARD_SQL,
772+
):
773+
return _Database(database_id, pool, database_dialect)
750774

751775

752776
class _Database(object):
753-
def __init__(self, database_id="database_id", pool=None):
777+
def __init__(
778+
self,
779+
database_id="database_id",
780+
pool=None,
781+
database_dialect=DatabaseDialect.GOOGLE_STANDARD_SQL,
782+
):
754783
self.name = database_id
755784
self.pool = pool
785+
self.database_dialect = database_dialect

0 commit comments

Comments
 (0)