forked from google/python-spanner-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield.py
More file actions
187 lines (138 loc) · 4.61 KB
/
field.py
File metadata and controls
187 lines (138 loc) · 4.61 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# python3
# Copyright 2019 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
#
# https://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.
"""Helper to deal with field types in Spanner interactions."""
import abc
import datetime
from typing import Any, Type
from spanner_orm import error
from google.cloud.spanner_v1.proto import type_pb2
class FieldType(abc.ABC):
"""Base class for column types for Spanner interactions."""
@staticmethod
@abc.abstractmethod
def ddl() -> str:
raise NotImplementedError
@staticmethod
@abc.abstractmethod
def grpc_type() -> type_pb2.Type:
raise NotImplementedError
@staticmethod
@abc.abstractmethod
def validate_type(value: Any) -> None:
raise NotImplementedError
class Field(object):
"""Represents a column in a table as a field in a model."""
def __init__(self,
field_type: Type[FieldType],
nullable: bool = False,
primary_key: bool = False):
self.name = None
self._type = field_type
self._nullable = nullable
self._primary_key = primary_key
def ddl(self) -> str:
if self._nullable:
return self._type.ddl()
return '{field_type} NOT NULL'.format(field_type=self._type.ddl())
def field_type(self) -> Type[FieldType]:
return self._type
def grpc_type(self) -> str:
return self._type.grpc_type()
def nullable(self) -> bool:
return self._nullable
def primary_key(self) -> bool:
return self._primary_key
def validate(self, value) -> None:
if value is None:
if not self._nullable:
raise error.ValidationError('None set for non-nullable field')
else:
self._type.validate_type(value)
class Boolean(FieldType):
"""Represents a boolean type."""
@staticmethod
def ddl() -> str:
return 'BOOL'
@staticmethod
def grpc_type() -> type_pb2.Type:
return type_pb2.Type(code=type_pb2.BOOL)
@staticmethod
def validate_type(value: Any) -> None:
if not isinstance(value, bool):
raise error.ValidationError('{} is not of type bool'.format(value))
class Integer(FieldType):
"""Represents an integer type."""
@staticmethod
def ddl() -> str:
return 'INT64'
@staticmethod
def grpc_type() -> type_pb2.Type:
return type_pb2.Type(code=type_pb2.INT64)
@staticmethod
def validate_type(value: Any) -> None:
if not isinstance(value, int):
raise error.ValidationError('{} is not of type int'.format(value))
class Float(FieldType):
"""Represents a float type."""
@staticmethod
def ddl() -> str:
return "FLOAT64"
@staticmethod
def grpc_type() -> type_pb2.Type:
return type_pb2.Type(code=type_pb2.FLOAT64)
@staticmethod
def validate_type(value: Any) -> None:
if not isinstance(value, (int, float)):
raise error.ValidationError("{} is not of type float".format(value))
class String(FieldType):
"""Represents a string type."""
@staticmethod
def ddl() -> str:
return 'STRING(MAX)'
@staticmethod
def grpc_type() -> type_pb2.Type:
return type_pb2.Type(code=type_pb2.STRING)
@staticmethod
def validate_type(value) -> None:
if not isinstance(value, str):
raise error.ValidationError('{} is not of type str'.format(value))
class StringArray(FieldType):
"""Represents an array of strings type."""
@staticmethod
def ddl() -> str:
return 'ARRAY<STRING(MAX)>'
@staticmethod
def grpc_type() -> type_pb2.Type:
return type_pb2.Type(code=type_pb2.ARRAY)
@staticmethod
def validate_type(value: Any) -> None:
if not isinstance(value, list):
raise error.ValidationError('{} is not of type list'.format(value))
for item in value:
if not isinstance(item, str):
raise error.ValidationError('{} is not of type str'.format(item))
class Timestamp(FieldType):
"""Represents a timestamp type."""
@staticmethod
def ddl() -> str:
return 'TIMESTAMP'
@staticmethod
def grpc_type() -> type_pb2.Type:
return type_pb2.Type(code=type_pb2.TIMESTAMP)
@staticmethod
def validate_type(value: Any) -> None:
if not isinstance(value, datetime.datetime):
raise error.ValidationError('{} is not of type datetime'.format(value))
ALL_TYPES = [Boolean, Integer, Float, String, StringArray, Timestamp]