Skip to content

Commit 64c5a45

Browse files
committed
Rename Type to FieldType
In the future, I'm going to try to add a Field class to clean up some of these rough spots. For now, just renaming Type to FieldType and type.py to field.py so that imports will work better. Also moved over the rest of the imports in non-test to be of the approved style
1 parent 97ea00f commit 64c5a45

12 files changed

Lines changed: 87 additions & 74 deletions

File tree

spanner_orm/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,31 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15+
"""Sets up shorcuts for imports from the library."""
1516

1617
from spanner_orm import api
1718
from spanner_orm import condition
19+
from spanner_orm import field
1820
from spanner_orm import model
1921
from spanner_orm import relationship
2022

2123
# pylint: disable=invalid-name
24+
SpannerApi = api.SpannerApi
25+
2226
Model = model.Model
27+
2328
ModelRelationship = relationship.ModelRelationship
24-
SpannerApi = api.SpannerApi
29+
30+
Boolean = field.Boolean
31+
Integer = field.Integer
32+
NullableBoolean = field.NullableBoolean
33+
NullableInteger = field.NullableInteger
34+
NullableString = field.NullableString
35+
NullableStringArray = field.NullableStringArray
36+
NullableTimestamp = field.NullableTimestamp
37+
String = field.String
38+
StringArray = field.StringArray
39+
Timestamp = field.Timestamp
2540

2641
equal_to = condition.equal_to
2742
greater_than = condition.greater_than

spanner_orm/admin/api.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15-
"""Interacts with the Spanner database to read and manage table schemas."""
15+
"""Class that handles API calls to Spanner that deal with table metadata."""
1616

1717
from spanner_orm import api
1818

@@ -26,13 +26,18 @@ class SpannerAdminApi(api.TableReadApi):
2626
_connection_info = None
2727

2828
@classmethod
29-
def connect(cls, project, instance, database, create_ddl=None):
29+
def connect(cls,
30+
project,
31+
instance,
32+
database,
33+
credentials=None,
34+
create_ddl=None):
3035
"""Connects to the specified database, optionally creating tables."""
31-
connection_info = (project, instance, database)
36+
connection_info = (project, instance, database, credentials)
3237
if cls._connection is not None and connection_info == cls._connection_info:
3338
return
3439

35-
client = spanner.Client(project=project)
40+
client = spanner.Client(project=project, credentials=credentials)
3641
instance = client.instance(instance)
3742

3843
if create_ddl is not None:

spanner_orm/admin/metadata.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15-
"""Retrieves database metadata."""
15+
"""Retrieves table metadata from Spanner."""
1616

1717
from collections import defaultdict
1818

@@ -26,7 +26,7 @@
2626

2727

2828
class DatabaseMetadata(object):
29-
"""Retrieve table metadata from Spanner and returns it in a usable format."""
29+
"""Gathers information about a table from Spanner."""
3030

3131
@classmethod
3232
def column_update(cls, schema_change):
@@ -55,7 +55,7 @@ def index_update(cls, schema_change):
5555

5656
@classmethod
5757
def models(cls, transaction=None):
58-
"""Constructs model classes from Spanner database schema."""
58+
"""Constructs model classes from Spanner table schema."""
5959
tables = cls._tables(transaction)
6060
indexes = cls._indexes(transaction)
6161
results = {}

spanner_orm/api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15-
"""Class that interacts with spanner database."""
15+
"""Class that handles API calls to Spanner."""
1616

1717
import abc
1818
from google.cloud import spanner
@@ -92,15 +92,15 @@ def _connection(cls):
9292

9393
# Spanner connection methods
9494
@classmethod
95-
def connect(cls, *, project, instance, database):
95+
def connect(cls, project, instance, database, credentials=None):
9696
"""Connects to the specified Spanner database."""
97-
connection_info = (project, instance, database)
97+
connection_info = (project, instance, database, credentials)
9898
if cls._connection is not None:
9999
if connection_info == cls._connection_info:
100100
return
101101
cls.hangup()
102102

103-
client = spanner.Client(project=project)
103+
client = spanner.Client(project=project, credentials=credentials)
104104
instance = client.instance(instance)
105105
cls._connection = instance.database(database)
106106
cls._connection_info = connection_info
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15-
"""Helper to deal with column types in database interactions."""
15+
"""Helper to deal with field types in Spanner interactions."""
1616

1717
import abc
1818
import datetime
1919

2020
from google.cloud.spanner_v1.proto import type_pb2
2121

2222

23-
class DatabaseType(abc.ABC):
24-
"""Base class for column types for database interactions."""
23+
class FieldType(abc.ABC):
24+
"""Base class for column types for Spanner interactions."""
2525

2626
@classmethod
2727
def full_ddl(cls):
@@ -67,7 +67,7 @@ class NullableType(abc.ABC):
6767
pass
6868

6969

70-
class Boolean(DatabaseType):
70+
class Boolean(FieldType):
7171
"""Represents a boolean type."""
7272

7373
@staticmethod
@@ -91,7 +91,7 @@ class NullableBoolean(NullableType, Boolean):
9191
pass
9292

9393

94-
class Integer(DatabaseType):
94+
class Integer(FieldType):
9595
"""Represents an integer type."""
9696

9797
@staticmethod
@@ -115,7 +115,7 @@ class NullableInteger(NullableType, Integer):
115115
pass
116116

117117

118-
class String(DatabaseType):
118+
class String(FieldType):
119119
"""Represents a string type."""
120120

121121
@staticmethod
@@ -139,7 +139,7 @@ class NullableString(NullableType, String):
139139
pass
140140

141141

142-
class StringArray(DatabaseType):
142+
class StringArray(FieldType):
143143
"""Represents an array of strings type."""
144144

145145
@staticmethod
@@ -165,7 +165,7 @@ class NullableStringArray(NullableType, StringArray):
165165
pass
166166

167167

168-
class Timestamp(DatabaseType):
168+
class Timestamp(FieldType):
169169
"""Represents a timestamp type."""
170170

171171
@staticmethod

spanner_orm/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def create_or_update(cls, transaction=None, **kwargs):
232232

233233
@classmethod
234234
def save_batch(cls, transaction, models):
235-
"""Persist all model changes in list of models to the database."""
235+
"""Persist all model changes in list of models to Spanner."""
236236
to_create, to_update = [], []
237237
columns = cls.columns()
238238
for model in models:

spanner_orm/schemas/column.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
# limitations under the License.
1515
"""Model for interacting with Spanner column schema table."""
1616

17+
from spanner_orm import field
1718
from spanner_orm.schemas import schema
18-
from spanner_orm.type import ALL_TYPES
19-
from spanner_orm.type import Integer
20-
from spanner_orm.type import NullableType
21-
from spanner_orm.type import String
2219

2320

2421
class ColumnSchema(schema.Schema):
@@ -31,13 +28,13 @@ def primary_index_keys():
3128
@classmethod
3229
def schema(cls):
3330
return {
34-
'table_catalog': String,
35-
'table_schema': String,
36-
'table_name': String,
37-
'column_name': String,
38-
'ordinal_position': Integer,
39-
'is_nullable': String,
40-
'spanner_type': String
31+
'table_catalog': field.String,
32+
'table_schema': field.String,
33+
'table_name': field.String,
34+
'column_name': field.String,
35+
'ordinal_position': field.Integer,
36+
'is_nullable': field.String,
37+
'spanner_type': field.String
4138
}
4239

4340
@classmethod
@@ -48,8 +45,8 @@ def nullable(self):
4845
return self.is_nullable == 'YES'
4946

5047
def type(self):
51-
for db_type in ALL_TYPES:
52-
db_nullable = issubclass(db_type, NullableType)
48+
for db_type in field.ALL_TYPES:
49+
db_nullable = issubclass(db_type, field.NullableType)
5350
if self.spanner_type == db_type.ddl() and self.nullable() == db_nullable:
5451
return db_type
5552

spanner_orm/schemas/index.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
# limitations under the License.
1515
"""Model for interacting with Spanner index schema table."""
1616

17+
from spanner_orm import field
1718
from spanner_orm.schemas import schema
18-
from spanner_orm.type import Boolean
19-
from spanner_orm.type import NullableString
20-
from spanner_orm.type import String
2119

2220

2321
class IndexSchema(schema.Schema):
@@ -30,15 +28,15 @@ def primary_index_keys():
3028
@classmethod
3129
def schema(cls):
3230
return {
33-
'table_catalog': String,
34-
'table_schema': String,
35-
'table_name': String,
36-
'index_name': String,
37-
'index_type': String,
38-
'parent_table_name': NullableString,
39-
'is_unique': Boolean,
40-
'is_null_filtered': Boolean,
41-
'index_state': String
31+
'table_catalog': field.String,
32+
'table_schema': field.String,
33+
'table_name': field.String,
34+
'index_name': field.String,
35+
'index_type': field.String,
36+
'parent_table_name': field.NullableString,
37+
'is_unique': field.Boolean,
38+
'is_null_filtered': field.Boolean,
39+
'index_state': field.String
4240
}
4341

4442
@classmethod

spanner_orm/schemas/index_column.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
# limitations under the License.
1515
"""Model for interacting with Spanner index column schema table."""
1616

17+
from spanner_orm import field
1718
from spanner_orm.schemas import schema
18-
from spanner_orm.type import NullableInteger
19-
from spanner_orm.type import NullableString
20-
from spanner_orm.type import String
2119

2220

2321
class IndexColumnSchema(schema.Schema):
@@ -33,15 +31,15 @@ def primary_index_keys():
3331
@classmethod
3432
def schema(cls):
3533
return {
36-
'table_catalog': String,
37-
'table_schema': String,
38-
'table_name': String,
39-
'index_name': String,
40-
'column_name': String,
41-
'ordinal_position': NullableInteger,
42-
'column_ordering': NullableString,
43-
'is_nullable': String,
44-
'spanner_type': String
34+
'table_catalog': field.String,
35+
'table_schema': field.String,
36+
'table_name': field.String,
37+
'index_name': field.String,
38+
'column_name': field.String,
39+
'ordinal_position': field.NullableInteger,
40+
'column_ordering': field.NullableString,
41+
'is_nullable': field.String,
42+
'spanner_type': field.String
4543
}
4644

4745
@classmethod

spanner_orm/tests/models.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
"""Models used by unit tests."""
1616

1717
from spanner_orm import model
18+
from spanner_orm.field import Integer
19+
from spanner_orm.field import NullableInteger
20+
from spanner_orm.field import NullableString
21+
from spanner_orm.field import NullableStringArray
22+
from spanner_orm.field import String
23+
from spanner_orm.field import Timestamp
1824
from spanner_orm.relationship import ModelRelationship
19-
from spanner_orm.type import Integer
20-
from spanner_orm.type import NullableInteger
21-
from spanner_orm.type import NullableString
22-
from spanner_orm.type import NullableStringArray
23-
from spanner_orm.type import String
24-
from spanner_orm.type import Timestamp
2525

2626

2727
class ChildTestModel(model.Model):

0 commit comments

Comments
 (0)