FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge pull request #2 from google/update · 7dracoder/python-spanner-orm@97ea00f · GitHub

Commit 97ea00f

Browse files
authored
Merge pull request google#2 from google/update
Start cleaning up imports and naming
2 parents f9baabd + 0f58141 commit 97ea00f

16 files changed

Lines changed: 191 additions & 194 deletions

File tree

‎spanner_orm/__init__.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# python3
2+
# Copyright 2018 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
from spanner_orm import api
17+
from spanner_orm import condition
18+
from spanner_orm import model
19+
from spanner_orm import relationship
20+
21+
# pylint: disable=invalid-name
22+
Model = model.Model
23+
ModelRelationship = relationship.ModelRelationship
24+
SpannerApi = api.SpannerApi
25+
26+
equal_to = condition.equal_to
27+
greater_than = condition.greater_than
28+
greater_than_or_equal_to = condition.greater_than_or_equal_to
29+
includes = condition.includes
30+
in_list = condition.in_list
31+
less_than = condition.less_than
32+
less_than_or_equal_to = condition.less_than_or_equal_to
33+
limit = condition.limit
34+
not_equal_to = condition.not_equal_to
35+
not_greater_than = condition.not_greater_than
36+
not_in_list = condition.not_in_list
37+
not_less_than = condition.not_less_than
38+
order_by = condition.order_by

‎spanner_orm/admin/api.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
# limitations under the License.
1515
"""Interacts with the Spanner database to read and manage table schemas."""
1616

17-
from spanner_orm.api import TableReadApi
17+
from spanner_orm import api
1818

1919
from google.cloud import spanner
2020

2121

22-
class DatabaseAdminApi(TableReadApi):
22+
class SpannerAdminApi(api.TableReadApi):
2323
"""Manages table schema information on Spanner."""
2424

2525
_connection = None

‎spanner_orm/admin/metadata.py‎

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,43 @@
1515
"""Retrieves database metadata."""
1616

1717
from collections import defaultdict
18-
from spanner_orm.admin.api import DatabaseAdminApi
19-
from spanner_orm.condition import EqualityCondition
20-
from spanner_orm.condition import InequalityCondition
21-
from spanner_orm.condition import OrderByCondition
22-
from spanner_orm.condition import OrderType
23-
from spanner_orm.model import Model
24-
from spanner_orm.schemas.column import ColumnSchema
25-
from spanner_orm.schemas.index import IndexSchema
26-
from spanner_orm.schemas.index_column import IndexColumnSchema
27-
from spanner_orm.update import ColumnUpdate
28-
from spanner_orm.update import CreateTableUpdate
29-
from spanner_orm.update import IndexUpdate
18+
19+
from spanner_orm import condition
20+
from spanner_orm import model
21+
from spanner_orm import update
22+
from spanner_orm.admin import api
23+
from spanner_orm.schemas import column
24+
from spanner_orm.schemas import index
25+
from spanner_orm.schemas import index_column
3026

3127

3228
class DatabaseMetadata(object):
3329
"""Retrieve table metadata from Spanner and returns it in a usable format."""
3430

3531
@classmethod
3632
def column_update(cls, schema_change):
37-
assert isinstance(schema_change, ColumnUpdate)
38-
model = cls.models()[schema_change.table()]
39-
schema_change.validate(model)
33+
assert isinstance(schema_change, update.ColumnUpdate)
34+
klass = cls.models()[schema_change.table()]
35+
schema_change.validate(klass)
4036

41-
DatabaseAdminApi.update_schema(schema_change.ddl(model))
37+
api.SpannerAdminApi.update_schema(schema_change.ddl(klass))
4238

4339
@classmethod
4440
def create_table(cls, schema_change):
45-
assert isinstance(schema_change, CreateTableUpdate)
41+
assert isinstance(schema_change, update.CreateTableUpdate)
4642
all_models = cls.models()
4743
assert schema_change.table() not in all_models
4844
schema_change.validate()
4945

50-
DatabaseAdminApi.update_schema(schema_change.ddl())
46+
api.SpannerAdminApi.update_schema(schema_change.ddl())
5147

5248
@classmethod
5349
def index_update(cls, schema_change):
54-
assert isinstance(schema_change, IndexUpdate)
55-
model = cls.models()[schema_change.table()]
56-
schema_change.validate(model)
50+
assert isinstance(schema_change, update.IndexUpdate)
51+
klass = cls.models()[schema_change.table()]
52+
schema_change.validate(klass)
5753

58-
DatabaseAdminApi.update_schema(schema_change.ddl(model))
54+
api.SpannerAdminApi.update_schema(schema_change.ddl(klass))
5955

6056
@classmethod
6157
def models(cls, transaction=None):
@@ -73,7 +69,7 @@ def make_classmethod(retval):
7369
for table_name, schema in tables.items():
7470
primary_index = indexes[table_name]['PRIMARY_KEY']['columns']
7571
klass = type(
76-
'Model_{}'.format(table_name), (Model,), {
72+
'Model_{}'.format(table_name), (model.Model,), {
7773
'primary_index_keys': make_method(primary_index),
7874
'schema': make_classmethod(schema),
7975
'table': make_classmethod(table_name)
@@ -85,9 +81,9 @@ def make_classmethod(retval):
8581
def _tables(cls, transaction=None):
8682
"""Compiles table information from column schema."""
8783
tables = defaultdict(dict)
88-
schemas = ColumnSchema.where(transaction,
89-
EqualityCondition('table_catalog', ''),
90-
EqualityCondition('table_schema', ''))
84+
schemas = column.ColumnSchema.where(
85+
transaction, condition.EqualityCondition('table_catalog', ''),
86+
condition.EqualityCondition('table_schema', ''))
9187
for schema in schemas:
9288
tables[schema.table_name][schema.column_name] = schema.type()
9389
return tables
@@ -99,20 +95,21 @@ def _indexes(cls, transaction=None):
9995
# Results are ordered by that so the index columns are added in the correct
10096
# order. None indicates that the key isn't really a part of the index, so we
10197
# skip those
102-
index_column_schemas = IndexColumnSchema.where(
103-
transaction, EqualityCondition('table_catalog', ''),
104-
EqualityCondition('table_schema', ''),
105-
InequalityCondition('ordinal_position', None),
106-
OrderByCondition(('ordinal_position', OrderType.ASC)))
98+
index_column_schemas = index_column.IndexColumnSchema.where(
99+
transaction, condition.EqualityCondition('table_catalog', ''),
100+
condition.EqualityCondition('table_schema', ''),
101+
condition.InequalityCondition('ordinal_position', None),
102+
condition.OrderByCondition(('ordinal_position',
103+
condition.OrderType.ASC)))
107104

108105
index_columns = defaultdict(list)
109106
for schema in index_column_schemas:
110107
key = (schema.table_name, schema.index_name)
111108
index_columns[key].append(schema.column_name)
112109

113-
index_schemas = IndexSchema.where(transaction,
114-
EqualityCondition('table_catalog', ''),
115-
EqualityCondition('table_schema', ''))
110+
index_schemas = index.IndexSchema.where(
111+
transaction, condition.EqualityCondition('table_catalog', ''),
112+
condition.EqualityCondition('table_schema', ''))
116113
indexes = defaultdict(dict)
117114
for schema in index_schemas:
118115
indexes[schema.table_name][schema.index_name] = {

‎spanner_orm/api.py‎

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,22 @@
1414
# limitations under the License.
1515
"""Class that interacts with spanner database."""
1616

17-
from abc import ABC
18-
from abc import abstractmethod
17+
import abc
1918
from google.cloud import spanner
2019

2120

22-
class TableReadApi(ABC):
23-
"""Handles read from table interactions with Spanner"""
21+
class SpannerReadApi(abc.ABC):
22+
"""Handles sending read requests to Spanner"""
2423

2524
@classmethod
26-
@abstractmethod
27-
def _database_connection(cls):
25+
@abc.abstractmethod
26+
def _connection(cls):
2827
raise NotImplementedError
2928

3029
@classmethod
3130
def run_read_only(cls, method, *args, **kwargs):
32-
"""Executes the provided callback method for read-only queries."""
33-
with cls._database_connection().snapshot() as snapshot:
31+
"""Wraps read-only queries in a read transaction."""
32+
with cls._connection().snapshot() as snapshot:
3433
return method(snapshot, *args, **kwargs)
3534

3635
# Read methods
@@ -49,18 +48,18 @@ def sql_query(transaction, query, parameters, parameter_types):
4948
return list(stream_results)
5049

5150

52-
class TableWriteApi(ABC):
53-
"""Handles write to table interactions with Spanner."""
51+
class SpannerWriteApi(abc.ABC):
52+
"""Handles sending write requests to Spanner."""
5453

5554
@classmethod
56-
@abstractmethod
57-
def _database_connection(cls):
55+
@abc.abstractmethod
56+
def _connection(cls):
5857
raise NotImplementedError
5958

6059
@classmethod
6160
def run_write(cls, *args, **kwargs):
62-
"""Executes the provided callback method in a transaction."""
63-
return cls._database_connection().run_in_transaction(*args, **kwargs)
61+
"""Wraps write and read-write queries in a transaction."""
62+
return cls._connection().run_in_transaction(*args, **kwargs)
6463

6564
# Write methods
6665
@staticmethod
@@ -75,25 +74,25 @@ def update(transaction, table_name, columns, values):
7574

7675
@staticmethod
7776
def upsert(transaction, table_name, columns, values):
78-
"""Updates row if primary key already exists, otherwise, creates a row."""
77+
"""Updates existing rows of a table or adds rows if they don't exist."""
7978
transaction.insert_or_update(
8079
table=table_name, columns=columns, values=values)
8180

8281

83-
class DatabaseApi(TableReadApi, TableWriteApi):
82+
class SpannerApi(SpannerReadApi, SpannerWriteApi):
8483
"""Class that handles reading from and writing to Spanner tables."""
8584

86-
_connection = None
85+
_spanner_connection = None
8786
_connection_info = None
8887

8988
@classmethod
90-
def _database_connection(cls):
91-
assert cls._connection is not None
92-
return cls._connection
89+
def _connection(cls):
90+
assert cls._spanner_connection is not None, 'Not connected to Spanner'
91+
return cls._spanner_connection
9392

9493
# Spanner connection methods
9594
@classmethod
96-
def connect(cls, project, instance, database):
95+
def connect(cls, *, project, instance, database):
9796
"""Connects to the specified Spanner database."""
9897
connection_info = (project, instance, database)
9998
if cls._connection is not None:

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL