|
| 1 | +# Copyright 2015 Google Inc. All rights reserved. |
| 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 | +"""Define API Datasets.""" |
| 16 | + |
| 17 | +import datetime |
| 18 | + |
| 19 | +import six |
| 20 | + |
| 21 | +from gcloud.bigquery._helpers import _datetime_from_prop |
| 22 | +from gcloud.bigquery._helpers import _prop_from_datetime |
| 23 | + |
| 24 | + |
| 25 | +class Table(object): |
| 26 | + """Tables represent a set of rows whose values correspond to a schema. |
| 27 | +
|
| 28 | + See: |
| 29 | + https://cloud.google.com/bigquery/docs/reference/v2/tables |
| 30 | +
|
| 31 | + :type name: string |
| 32 | + :param name: the name of the table |
| 33 | +
|
| 34 | + :type dataset: :class:`gcloud.bigquery.dataset.Dataset` |
| 35 | + :param dataset: The dataset which contains the table. |
| 36 | + """ |
| 37 | + |
| 38 | + def __init__(self, name, dataset): |
| 39 | + self.name = name |
| 40 | + self._dataset = dataset |
| 41 | + self._properties = {} |
| 42 | + |
| 43 | + @property |
| 44 | + def path(self): |
| 45 | + """URL path for the table's APIs. |
| 46 | +
|
| 47 | + :rtype: string |
| 48 | + :returns: the path based on project and dataste name. |
| 49 | + """ |
| 50 | + return '%s/tables/%s' % (self._dataset.path, self.name) |
| 51 | + |
| 52 | + @property |
| 53 | + def created(self): |
| 54 | + """Datetime at which the table was created. |
| 55 | +
|
| 56 | + :rtype: ``datetime.datetime``, or ``NoneType`` |
| 57 | + :returns: the creation time (None until set from the server). |
| 58 | + """ |
| 59 | + return _datetime_from_prop(self._properties.get('creationTime')) |
| 60 | + |
| 61 | + @property |
| 62 | + def etag(self): |
| 63 | + """ETag for the table resource. |
| 64 | +
|
| 65 | + :rtype: string, or ``NoneType`` |
| 66 | + :returns: the ETag (None until set from the server). |
| 67 | + """ |
| 68 | + return self._properties.get('etag') |
| 69 | + |
| 70 | + @property |
| 71 | + def modified(self): |
| 72 | + """Datetime at which the table was last modified. |
| 73 | +
|
| 74 | + :rtype: ``datetime.datetime``, or ``NoneType`` |
| 75 | + :returns: the modification time (None until set from the server). |
| 76 | + """ |
| 77 | + return _datetime_from_prop(self._properties.get('lastModifiedTime')) |
| 78 | + |
| 79 | + @property |
| 80 | + def num_bytes(self): |
| 81 | + """The size of the table in bytes. |
| 82 | +
|
| 83 | + :rtype: integer, or ``NoneType`` |
| 84 | + :returns: the byte count (None until set from the server). |
| 85 | + """ |
| 86 | + return self._properties.get('numBytes') |
| 87 | + |
| 88 | + @property |
| 89 | + def num_rows(self): |
| 90 | + """The number of rows in the table. |
| 91 | +
|
| 92 | + :rtype: integer, or ``NoneType`` |
| 93 | + :returns: the row count (None until set from the server). |
| 94 | + """ |
| 95 | + return self._properties.get('numRows') |
| 96 | + |
| 97 | + @property |
| 98 | + def self_link(self): |
| 99 | + """URL for the table resource. |
| 100 | +
|
| 101 | + :rtype: string, or ``NoneType`` |
| 102 | + :returns: the URL (None until set from the server). |
| 103 | + """ |
| 104 | + return self._properties.get('selfLink') |
| 105 | + |
| 106 | + @property |
| 107 | + def table_id(self): |
| 108 | + """ID for the table resource. |
| 109 | +
|
| 110 | + :rtype: string, or ``NoneType`` |
| 111 | + :returns: the ID (None until set from the server). |
| 112 | + """ |
| 113 | + return self._properties.get('id') |
| 114 | + |
| 115 | + @property |
| 116 | + def table_type(self): |
| 117 | + """The type of the table. |
| 118 | +
|
| 119 | + Possible values are "TABLE" or "VIEW". |
| 120 | +
|
| 121 | + :rtype: string, or ``NoneType`` |
| 122 | + :returns: the URL (None until set from the server). |
| 123 | + """ |
| 124 | + return self._properties.get('type') |
| 125 | + |
| 126 | + @property |
| 127 | + def description(self): |
| 128 | + """Description of the table. |
| 129 | +
|
| 130 | + :rtype: string, or ``NoneType`` |
| 131 | + :returns: The description as set by the user, or None (the default). |
| 132 | + """ |
| 133 | + return self._properties.get('description') |
| 134 | + |
| 135 | + @description.setter |
| 136 | + def description(self, value): |
| 137 | + """Update description of the table. |
| 138 | +
|
| 139 | + :type value: string, or ``NoneType`` |
| 140 | + :param value: new description |
| 141 | +
|
| 142 | + :raises: ValueError for invalid value types. |
| 143 | + """ |
| 144 | + if not isinstance(value, six.string_types) and value is not None: |
| 145 | + raise ValueError("Pass a string, or None") |
| 146 | + self._properties['description'] = value |
| 147 | + |
| 148 | + @property |
| 149 | + def expires(self): |
| 150 | + """Datetime at which the table will be removed. |
| 151 | +
|
| 152 | + :rtype: ``datetime.datetime``, or ``NoneType`` |
| 153 | + :returns: the expiration time, or None |
| 154 | + """ |
| 155 | + return _datetime_from_prop(self._properties.get('expirationTime')) |
| 156 | + |
| 157 | + @expires.setter |
| 158 | + def expires(self, value): |
| 159 | + """Update atetime at which the table will be removed. |
| 160 | +
|
| 161 | + :type value: ``datetime.datetime``, or ``NoneType`` |
| 162 | + :param value: the new expiration time, or None |
| 163 | + """ |
| 164 | + if not isinstance(value, datetime.datetime) and value is not None: |
| 165 | + raise ValueError("Pass a datetime, or None") |
| 166 | + self._properties['expirationTime'] = _prop_from_datetime(value) |
| 167 | + |
| 168 | + @property |
| 169 | + def friendly_name(self): |
| 170 | + """Title of the table. |
| 171 | +
|
| 172 | + :rtype: string, or ``NoneType`` |
| 173 | + :returns: The name as set by the user, or None (the default). |
| 174 | + """ |
| 175 | + return self._properties.get('friendlyName') |
| 176 | + |
| 177 | + @friendly_name.setter |
| 178 | + def friendly_name(self, value): |
| 179 | + """Update title of the table. |
| 180 | +
|
| 181 | + :type value: string, or ``NoneType`` |
| 182 | + :param value: new title |
| 183 | +
|
| 184 | + :raises: ValueError for invalid value types. |
| 185 | + """ |
| 186 | + if not isinstance(value, six.string_types) and value is not None: |
| 187 | + raise ValueError("Pass a string, or None") |
| 188 | + self._properties['friendlyName'] = value |
| 189 | + |
| 190 | + @property |
| 191 | + def location(self): |
| 192 | + """Location in which the table is hosted. |
| 193 | +
|
| 194 | + :rtype: string, or ``NoneType`` |
| 195 | + :returns: The location as set by the user, or None (the default). |
| 196 | + """ |
| 197 | + return self._properties.get('location') |
| 198 | + |
| 199 | + @location.setter |
| 200 | + def location(self, value): |
| 201 | + """Update location in which the table is hosted. |
| 202 | +
|
| 203 | + :type value: string, or ``NoneType`` |
| 204 | + :param value: new location |
| 205 | +
|
| 206 | + :raises: ValueError for invalid value types. |
| 207 | + """ |
| 208 | + if not isinstance(value, six.string_types) and value is not None: |
| 209 | + raise ValueError("Pass a string, or None") |
| 210 | + self._properties['location'] = value |
| 211 | + |
| 212 | + @property |
| 213 | + def view_query(self): |
| 214 | + """SQL query defining the table as a view. |
| 215 | +
|
| 216 | + :rtype: string, or ``NoneType`` |
| 217 | + :returns: The query as set by the user, or None (the default). |
| 218 | + """ |
| 219 | + view = self._properties.get('view') |
| 220 | + if view is not None: |
| 221 | + return view.get('query') |
| 222 | + |
| 223 | + @view_query.setter |
| 224 | + def view_query(self, value): |
| 225 | + """Update SQL query defining the table as a view. |
| 226 | +
|
| 227 | + :type value: string |
| 228 | + :param value: new location |
| 229 | +
|
| 230 | + :raises: ValueError for invalid value types. |
| 231 | + """ |
| 232 | + if not isinstance(value, six.string_types): |
| 233 | + raise ValueError("Pass a string") |
| 234 | + self._properties['view'] = {'query': value} |
| 235 | + |
| 236 | + @view_query.deleter |
| 237 | + def view_query(self): |
| 238 | + """Delete SQL query defining the table as a view.""" |
| 239 | + self._properties.pop('view', None) |
0 commit comments