|
| 1 | +# Copyright 2021 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""User friendly container for Cloud Spanner Table.""" |
| 16 | + |
| 17 | +from google.cloud.exceptions import NotFound |
| 18 | + |
| 19 | +from google.cloud.spanner_v1.types import ( |
| 20 | + Type, |
| 21 | + TypeCode, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +_EXISTS_TEMPLATE = """ |
| 26 | +SELECT EXISTS( |
| 27 | + SELECT TABLE_NAME |
| 28 | + FROM INFORMATION_SCHEMA.TABLES |
| 29 | + WHERE TABLE_NAME = @table_id |
| 30 | +) |
| 31 | +""" |
| 32 | +_GET_SCHEMA_TEMPLATE = "SELECT * FROM {} LIMIT 0" |
| 33 | + |
| 34 | + |
| 35 | +class Table(object): |
| 36 | + """Representation of a Cloud Spanner Table. |
| 37 | +
|
| 38 | + :type table_id: str |
| 39 | + :param table_id: The ID of the table. |
| 40 | +
|
| 41 | + :type database: :class:`~google.cloud.spanner_v1.database.Database` |
| 42 | + :param database: The database that owns the table. |
| 43 | + """ |
| 44 | + |
| 45 | + def __init__(self, table_id, database): |
| 46 | + self._table_id = table_id |
| 47 | + self._database = database |
| 48 | + |
| 49 | + # Calculated properties. |
| 50 | + self._schema = None |
| 51 | + |
| 52 | + @property |
| 53 | + def table_id(self): |
| 54 | + """The ID of the table used in SQL. |
| 55 | +
|
| 56 | + :rtype: str |
| 57 | + :returns: The table ID. |
| 58 | + """ |
| 59 | + return self._table_id |
| 60 | + |
| 61 | + def exists(self): |
| 62 | + """Test whether this table exists. |
| 63 | +
|
| 64 | + :rtype: bool |
| 65 | + :returns: True if the table exists, else false. |
| 66 | + """ |
| 67 | + with self._database.snapshot() as snapshot: |
| 68 | + return self._exists(snapshot) |
| 69 | + |
| 70 | + def _exists(self, snapshot): |
| 71 | + """Query to check that the table exists. |
| 72 | +
|
| 73 | + :type snapshot: :class:`~google.cloud.spanner_v1.snapshot.Snapshot` |
| 74 | + :param snapshot: snapshot to use for database queries |
| 75 | +
|
| 76 | + :rtype: bool |
| 77 | + :returns: True if the table exists, else false. |
| 78 | + """ |
| 79 | + results = snapshot.execute_sql( |
| 80 | + _EXISTS_TEMPLATE, |
| 81 | + params={"table_id": self.table_id}, |
| 82 | + param_types={"table_id": Type(code=TypeCode.STRING)}, |
| 83 | + ) |
| 84 | + return next(iter(results))[0] |
| 85 | + |
| 86 | + @property |
| 87 | + def schema(self): |
| 88 | + """The schema of this table. |
| 89 | +
|
| 90 | + :rtype: list of :class:`~google.cloud.spanner_v1.types.StructType.Field` |
| 91 | + :returns: The table schema. |
| 92 | + """ |
| 93 | + if self._schema is None: |
| 94 | + with self._database.snapshot() as snapshot: |
| 95 | + self._schema = self._get_schema(snapshot) |
| 96 | + return self._schema |
| 97 | + |
| 98 | + def _get_schema(self, snapshot): |
| 99 | + """Get the schema of this table. |
| 100 | +
|
| 101 | + :type snapshot: :class:`~google.cloud.spanner_v1.snapshot.Snapshot` |
| 102 | + :param snapshot: snapshot to use for database queries |
| 103 | +
|
| 104 | + :rtype: list of :class:`~google.cloud.spanner_v1.types.StructType.Field` |
| 105 | + :returns: The table schema. |
| 106 | + """ |
| 107 | + query = _GET_SCHEMA_TEMPLATE.format(self.table_id) |
| 108 | + results = snapshot.execute_sql(query) |
| 109 | + # Start iterating to force the schema to download. |
| 110 | + try: |
| 111 | + next(iter(results)) |
| 112 | + except StopIteration: |
| 113 | + pass |
| 114 | + return list(results.fields) |
| 115 | + |
| 116 | + def reload(self): |
| 117 | + """Reload this table. |
| 118 | +
|
| 119 | + Refresh any configured schema into :attr:`schema`. |
| 120 | +
|
| 121 | + :raises NotFound: if the table does not exist |
| 122 | + """ |
| 123 | + with self._database.snapshot() as snapshot: |
| 124 | + if not self._exists(snapshot): |
| 125 | + raise NotFound("table '{}' does not exist".format(self.table_id)) |
| 126 | + self._schema = self._get_schema(snapshot) |
0 commit comments