forked from googleapis/python-spanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.py
More file actions
126 lines (100 loc) · 3.7 KB
/
Copy pathtable.py
File metadata and controls
126 lines (100 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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.
"""
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)