Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 1250d8a

Browse files
committed
feat: add table method to database
1 parent fdd6bf9 commit 1250d8a

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

google/cloud/spanner_v1/database.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,28 @@ def list_database_operations(self, filter_="", page_size=None):
602602
filter_=database_filter, page_size=page_size
603603
)
604604

605+
def table(self, table_id):
606+
"""Factory to create a table object within this database.
607+
608+
Note: This method does not create a table in Cloud Spanner, but it can
609+
be used to check if a table exists.
610+
611+
.. code-block:: python
612+
613+
my_table = database.table("my_table")
614+
if my_table.exists():
615+
print("Table with ID 'my_table' exists.")
616+
else:
617+
print("Table with ID 'my_table' does not exist.")
618+
619+
:type table_id: str
620+
:param table_id: The ID of the table.
621+
622+
:rtype: :class:`~google.cloud.spanner_v1.table.Table`
623+
:returns: a table owned by this database.
624+
"""
625+
return Table(table_id, self)
626+
605627
def list_tables(self):
606628
"""List tables within the database.
607629
@@ -613,7 +635,7 @@ def list_tables(self):
613635
with self.snapshot() as snapshot:
614636
results = snapshot.execute_sql(_LIST_TABLES_QUERY)
615637
for row in results:
616-
yield Table(row[0], self)
638+
yield self.table(row[0])
617639

618640

619641
class BatchCheckout(object):

google/cloud/spanner_v1/instance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def database(self, database_id, ddl_statements=(), pool=None):
361361
"""Factory to create a database within this instance.
362362
363363
:type database_id: str
364-
:param database_id: The ID of the instance.
364+
:param database_id: The ID of the database.
365365
366366
:type ddl_statements: list of string
367367
:param ddl_statements: (Optional) DDL statements, excluding the

tests/unit/test_database.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,18 @@ def test_list_database_operations_explicit_filter(self):
12431243
filter_=expected_filter_, page_size=page_size
12441244
)
12451245

1246+
def test_table_factory_defaults(self):
1247+
from google.cloud.spanner_v1.table import Table
1248+
1249+
client = _Client()
1250+
instance = _Instance(self.INSTANCE_NAME, client=client)
1251+
pool = _Pool()
1252+
database = self._make_one(self.DATABASE_ID, instance, pool=pool)
1253+
my_table = database.table("my_table")
1254+
self.assertIsInstance(my_table, Table)
1255+
self.assertIs(my_table._database, database)
1256+
self.assertEqual(my_table.table_id, "my_table")
1257+
12461258
def test_list_tables(self):
12471259
client = _Client()
12481260
instance = _Instance(self.INSTANCE_NAME, client=client)

0 commit comments

Comments
 (0)