2020from google .cloud .spanner_v1 .proto import type_pb2
2121
2222
23- class FieldType ( abc . ABC ):
24- """Base class for column types for Spanner interactions ."""
23+ class Field ( object ):
24+ """Represents a column in a table as a field in a model ."""
2525
26- @classmethod
27- def full_ddl (cls ):
28- if issubclass (cls , NullableType ):
29- return cls .ddl ()
26+ def __init__ (self , field_type , nullable = False ):
27+ self ._type = field_type
28+ self ._nullable = nullable
29+
30+ def ddl (self ):
31+ if self ._nullable :
32+ return self ._type .ddl ()
33+ return '{field_type} NOT NULL' .format (field_type = self ._type .ddl ())
34+
35+ def field_type (self ):
36+ return self ._type
37+
38+ def grpc_type (self ):
39+ return self ._type .grpc_type ()
40+
41+ def grpc_list_type (self ):
42+ return self ._type .grpc_list_type ()
43+
44+ def nullable (self ):
45+ return self ._nullable
46+
47+ def validate (self , value ):
48+ if value is None :
49+ assert self ._nullable
3050 else :
31- return '{} NOT NULL' . format ( cls . ddl () )
51+ self . _type . validate_type ( value )
3252
33- @staticmethod
34- @abc .abstractmethod
35- def db_type ():
36- raise NotImplementedError
53+
54+ class FieldType (abc .ABC ):
55+ """Base class for column types for Spanner interactions."""
3756
3857 @staticmethod
3958 @abc .abstractmethod
@@ -50,30 +69,15 @@ def grpc_list_type(cls):
5069 return type_pb2 .Type (
5170 code = type_pb2 .ARRAY , array_element_type = cls .grpc_type ())
5271
53- @classmethod
54- def validate (cls , value ):
55- if value is None :
56- assert issubclass (cls , NullableType ), 'Null value for non-nullable column'
57- else :
58- cls .validate_type (value )
59-
6072 @staticmethod
6173 @abc .abstractmethod
6274 def validate_type (value ):
6375 raise NotImplementedError
6476
6577
66- class NullableType (abc .ABC ):
67- pass
68-
69-
7078class Boolean (FieldType ):
7179 """Represents a boolean type."""
7280
73- @staticmethod
74- def db_type ():
75- return Boolean
76-
7781 @staticmethod
7882 def ddl ():
7983 return 'BOOL'
@@ -87,17 +91,9 @@ def validate_type(value):
8791 assert isinstance (value , bool ), '{} is not of type bool' .format (value )
8892
8993
90- class NullableBoolean (NullableType , Boolean ):
91- pass
92-
93-
9494class Integer (FieldType ):
9595 """Represents an integer type."""
9696
97- @staticmethod
98- def db_type ():
99- return Integer
100-
10197 @staticmethod
10298 def ddl ():
10399 return 'INT64'
@@ -111,17 +107,9 @@ def validate_type(value):
111107 assert isinstance (value , int ), '{} is not of type int' .format (value )
112108
113109
114- class NullableInteger (NullableType , Integer ):
115- pass
116-
117-
118110class String (FieldType ):
119111 """Represents a string type."""
120112
121- @staticmethod
122- def db_type ():
123- return String
124-
125113 @staticmethod
126114 def ddl ():
127115 return 'STRING(MAX)'
@@ -135,17 +123,9 @@ def validate_type(value):
135123 assert isinstance (value , str ), '{} is not of type str' .format (value )
136124
137125
138- class NullableString (NullableType , String ):
139- pass
140-
141-
142126class StringArray (FieldType ):
143127 """Represents an array of strings type."""
144128
145- @staticmethod
146- def db_type ():
147- return StringArray
148-
149129 @staticmethod
150130 def ddl ():
151131 return 'ARRAY<STRING(MAX)>'
@@ -161,17 +141,9 @@ def validate_type(value):
161141 assert isinstance (item , str ), '{} is not of type str' .format (item )
162142
163143
164- class NullableStringArray (NullableType , StringArray ):
165- pass
166-
167-
168144class Timestamp (FieldType ):
169145 """Represents a timestamp type."""
170146
171- @staticmethod
172- def db_type ():
173- return Timestamp
174-
175147 @staticmethod
176148 def ddl ():
177149 return 'TIMESTAMP'
@@ -185,11 +157,4 @@ def validate_type(value):
185157 assert isinstance (value , datetime .datetime )
186158
187159
188- class NullableTimestamp (NullableType , Timestamp ):
189- pass
190-
191-
192- ALL_TYPES = [
193- Boolean , NullableBoolean , Integer , NullableInteger , String , NullableString ,
194- StringArray , NullableStringArray , Timestamp , NullableTimestamp
195- ]
160+ ALL_TYPES = [Boolean , Integer , String , StringArray , Timestamp ]
0 commit comments