New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add Database.list_tables
method
#219
Merged
Merged
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3f9cc92
feat: add `Database.list_tables` method
tswast 40a60e3
update docs, add tests
tswast e016b0f
remove numeric from get_schema test
tswast f407289
add unit tests
tswast 21395ec
add docs for table api and usage
tswast 2b566fa
fix link to Field class
tswast 66aa192
typo in table constructor docs
tswast bfbc105
add reload and exists methods
tswast fdd6bf9
Merge remote-tracking branch 'upstream/master' into issue156-list_tables
tswast 1250d8a
feat: add table method to database
tswast 3f6eb34
update usage docs to use factory method
tswast c7cd934
Merge remote-tracking branch 'upstream/master' into issue156-list_tables
tswast 611b27f
address warning in GitHub UI for sphinx header
tswast ef978df
Merge remote-tracking branch 'upstream/master' into issue156-list_tables
tswast d91e666
Update docs/table-usage.rst
larkee File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Table API | ||
========= | ||
|
||
.. automodule:: google.cloud.spanner_v1.table | ||
:members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
Table Admin | ||
=========== | ||
|
||
After creating an :class:`~google.cloud.spanner_v1.database.Database`, you can | ||
interact with individual tables for that instance. | ||
|
||
|
||
List Tables | ||
----------- | ||
|
||
To iterate over all existing tables for an database, use its | ||
:meth:`~google.cloud.spanner_v1.database.Database.list_tables` method: | ||
|
||
.. code:: python | ||
|
||
for table in database.list_tables(): | ||
# `table` is a `Table` object. | ||
|
||
This method yields :class:`~google.cloud.spanner_v1.table.Table` objects. | ||
|
||
|
||
Table Factory | ||
------------- | ||
|
||
A table object can be created by using the | ||
:class:`~google.cloud.spanner_v1.table.Table` constructor. Since table | ||
operations are executed via SQL, a | ||
:class:`~google.cloud.spanner_v1.database.Database` instance is required. | ||
|
||
.. code:: python | ||
|
||
table = google.cloud.spanner_v1.table.Table( | ||
"my_table_id", database | ||
) | ||
|
||
Getting the Table Schema | ||
------------------------ | ||
|
||
Use the :attr:`~google.cloud.spanner_v1.table.Table.schema` property to inspect | ||
the columns of a table as a list of | ||
:class:`~google.cloud.spanner_v1.types.StructType.Field` objects. | ||
|
||
.. code:: python | ||
|
||
for field in table.schema | ||
# `field` is a `Field` object. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""User friendly container for Cloud Spanner Table.""" | ||
|
||
from google.cloud.exceptions import NotFound | ||
|
||
from google.cloud.spanner_v1.types import ( | ||
Type, | ||
TypeCode, | ||
) | ||
|
||
|
||
_EXISTS_TEMPLATE = """ | ||
SELECT EXISTS( | ||
SELECT TABLE_NAME | ||
FROM INFORMATION_SCHEMA.TABLES | ||
WHERE TABLE_NAME = @table_id | ||
) | ||
""" | ||
_GET_SCHEMA_TEMPLATE = "SELECT * FROM {} LIMIT 0" | ||
|
||
|
||
class Table(object): | ||
"""Representation of a Cloud Spanner Table. | ||
|
||
:type table_id: str | ||
:param table_id: The ID of the table. | ||
|
||
:type database: :class:`~google.cloud.spanner_v1.database.Database` | ||
:param database: The database that owns the table. | ||
""" | ||
larkee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def __init__(self, table_id, database): | ||
self._table_id = table_id | ||
self._database = database | ||
|
||
# Calculated properties. | ||
self._schema = None | ||
|
||
@property | ||
def table_id(self): | ||
"""The ID of the table used in SQL. | ||
|
||
:rtype: str | ||
:returns: The table ID. | ||
""" | ||
return self._table_id | ||
|
||
def exists(self): | ||
"""Test whether this table exists. | ||
|
||
:rtype: bool | ||
:returns: True if the table exists, else false. | ||
""" | ||
with self._database.snapshot() as snapshot: | ||
return self._exists(snapshot) | ||
|
||
def _exists(self, snapshot): | ||
"""Query to check that the table exists. | ||
|
||
:type snapshot: :class:`~google.cloud.spanner_v1.snapshot.Snapshot` | ||
:param snapshot: snapshot to use for database queries | ||
|
||
:rtype: bool | ||
:returns: True if the table exists, else false. | ||
""" | ||
results = snapshot.execute_sql( | ||
_EXISTS_TEMPLATE, | ||
params={"table_id": self.table_id}, | ||
param_types={"table_id": Type(code=TypeCode.STRING)}, | ||
) | ||
return next(iter(results))[0] | ||
|
||
@property | ||
def schema(self): | ||
"""The schema of this table. | ||
|
||
:rtype: list of :class:`~google.cloud.spanner_v1.types.StructType.Field` | ||
:returns: The table schema. | ||
""" | ||
if self._schema is None: | ||
with self._database.snapshot() as snapshot: | ||
self._schema = self._get_schema(snapshot) | ||
return self._schema | ||
|
||
def _get_schema(self, snapshot): | ||
"""Get the schema of this table. | ||
|
||
:type snapshot: :class:`~google.cloud.spanner_v1.snapshot.Snapshot` | ||
:param snapshot: snapshot to use for database queries | ||
|
||
:rtype: list of :class:`~google.cloud.spanner_v1.types.StructType.Field` | ||
:returns: The table schema. | ||
""" | ||
query = _GET_SCHEMA_TEMPLATE.format(self.table_id) | ||
results = snapshot.execute_sql(query) | ||
# Start iterating to force the schema to download. | ||
try: | ||
next(iter(results)) | ||
except StopIteration: | ||
pass | ||
return list(results.fields) | ||
|
||
def reload(self): | ||
"""Reload this table. | ||
|
||
Refresh any configured schema into :attr:`schema`. | ||
|
||
:raises NotFound: if the table does not exist | ||
""" | ||
with self._database.snapshot() as snapshot: | ||
if not self._exists(snapshot): | ||
raise NotFound("table '{}' does not exist".format(self.table_id)) | ||
self._schema = self._get_schema(snapshot) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a
table()
method on theDatabase
object which creates theTable
object to be consistent with the other classes e.g.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! Thanks for your patience while I was OOO.
Edit: added in the following commit: 1250d8a