| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 97ea00f commit 64c5a45
12 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,16 +12,31 @@ | |||
| 12 | 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | 13 | # See the License for the specific language governing permissions and | |
| 14 | 14 | # limitations under the License. | |
| 15 | + """Sets up shorcuts for imports from the library.""" | ||
| 15 | 16 | ||
| 16 | 17 | from spanner_orm import api | |
| 17 | 18 | from spanner_orm import condition | |
| 19 | + from spanner_orm import field | ||
| 18 | 20 | from spanner_orm import model | |
| 19 | 21 | from spanner_orm import relationship | |
| 20 | 22 | ||
| 21 | 23 | # pylint: disable=invalid-name | |
| 24 | + SpannerApi = api.SpannerApi | ||
| 25 | + | ||
| 22 | 26 | Model = model.Model | |
| 27 | + | ||
| 23 | 28 | 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 | ||
| 25 | 40 | ||
| 26 | 41 | equal_to = condition.equal_to | |
| 27 | 42 | greater_than = condition.greater_than | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,7 +12,7 @@ | |||
| 12 | 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | 13 | # See the License for the specific language governing permissions and | |
| 14 | 14 | # 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.""" | ||
| 16 | 16 | ||
| 17 | 17 | from spanner_orm import api | |
| 18 | 18 | ||
@@ -26,13 +26,18 @@ class SpannerAdminApi(api.TableReadApi): | |||
| 26 | 26 | _connection_info = None | |
| 27 | 27 | ||
| 28 | 28 | @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): | ||
| 30 | 35 | """Connects to the specified database, optionally creating tables.""" | |
| 31 | - connection_info = (project, instance, database) | ||
| 36 | + connection_info = (project, instance, database, credentials) | ||
| 32 | 37 | if cls._connection is not None and connection_info == cls._connection_info: | |
| 33 | 38 | return | |
| 34 | 39 | ||
| 35 | - client = spanner.Client(project=project) | ||
| 40 | + client = spanner.Client(project=project, credentials=credentials) | ||
| 36 | 41 | instance = client.instance(instance) | |
| 37 | 42 | ||
| 38 | 43 | if create_ddl is not None: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,7 +12,7 @@ | |||
| 12 | 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | 13 | # See the License for the specific language governing permissions and | |
| 14 | 14 | # limitations under the License. | |
| 15 | - """Retrieves database metadata.""" | ||
| 15 | + """Retrieves table metadata from Spanner.""" | ||
| 16 | 16 | ||
| 17 | 17 | from collections import defaultdict | |
| 18 | 18 | ||
@@ -26,7 +26,7 @@ | |||
| 26 | 26 | ||
| 27 | 27 | ||
| 28 | 28 | class DatabaseMetadata(object): | |
| 29 | - """Retrieve table metadata from Spanner and returns it in a usable format.""" | ||
| 29 | + """Gathers information about a table from Spanner.""" | ||
| 30 | 30 | ||
| 31 | 31 | @classmethod | |
| 32 | 32 | def column_update(cls, schema_change): | |
@@ -55,7 +55,7 @@ def index_update(cls, schema_change): | |||
| 55 | 55 | ||
| 56 | 56 | @classmethod | |
| 57 | 57 | def models(cls, transaction=None): | |
| 58 | - """Constructs model classes from Spanner database schema.""" | ||
| 58 | + """Constructs model classes from Spanner table schema.""" | ||
| 59 | 59 | tables = cls._tables(transaction) | |
| 60 | 60 | indexes = cls._indexes(transaction) | |
| 61 | 61 | results = {} | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,7 +12,7 @@ | |||
| 12 | 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | 13 | # See the License for the specific language governing permissions and | |
| 14 | 14 | # limitations under the License. | |
| 15 | - """Class that interacts with spanner database.""" | ||
| 15 | + """Class that handles API calls to Spanner.""" | ||
| 16 | 16 | ||
| 17 | 17 | import abc | |
| 18 | 18 | from google.cloud import spanner | |
@@ -92,15 +92,15 @@ def _connection(cls): | |||
| 92 | 92 | ||
| 93 | 93 | # Spanner connection methods | |
| 94 | 94 | @classmethod | |
| 95 | - def connect(cls, *, project, instance, database): | ||
| 95 | + def connect(cls, project, instance, database, credentials=None): | ||
| 96 | 96 | """Connects to the specified Spanner database.""" | |
| 97 | - connection_info = (project, instance, database) | ||
| 97 | + connection_info = (project, instance, database, credentials) | ||
| 98 | 98 | if cls._connection is not None: | |
| 99 | 99 | if connection_info == cls._connection_info: | |
| 100 | 100 | return | |
| 101 | 101 | cls.hangup() | |
| 102 | 102 | ||
| 103 | - client = spanner.Client(project=project) | ||
| 103 | + client = spanner.Client(project=project, credentials=credentials) | ||
| 104 | 104 | instance = client.instance(instance) | |
| 105 | 105 | cls._connection = instance.database(database) | |
| 106 | 106 | cls._connection_info = connection_info | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,16 +12,16 @@ | |||
| 12 | 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | 13 | # See the License for the specific language governing permissions and | |
| 14 | 14 | # limitations under the License. | |
| 15 | - """Helper to deal with column types in database interactions.""" | ||
| 15 | + """Helper to deal with field types in Spanner interactions.""" | ||
| 16 | 16 | ||
| 17 | 17 | import abc | |
| 18 | 18 | import datetime | |
| 19 | 19 | ||
| 20 | 20 | from google.cloud.spanner_v1.proto import type_pb2 | |
| 21 | 21 | ||
| 22 | 22 | ||
| 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.""" | ||
| 25 | 25 | ||
| 26 | 26 | @classmethod | |
| 27 | 27 | def full_ddl(cls): | |
@@ -67,7 +67,7 @@ class NullableType(abc.ABC): | |||
| 67 | 67 | pass | |
| 68 | 68 | ||
| 69 | 69 | ||
| 70 | - class Boolean(DatabaseType): | ||
| 70 | + class Boolean(FieldType): | ||
| 71 | 71 | """Represents a boolean type.""" | |
| 72 | 72 | ||
| 73 | 73 | @staticmethod | |
@@ -91,7 +91,7 @@ class NullableBoolean(NullableType, Boolean): | |||
| 91 | 91 | pass | |
| 92 | 92 | ||
| 93 | 93 | ||
| 94 | - class Integer(DatabaseType): | ||
| 94 | + class Integer(FieldType): | ||
| 95 | 95 | """Represents an integer type.""" | |
| 96 | 96 | ||
| 97 | 97 | @staticmethod | |
@@ -115,7 +115,7 @@ class NullableInteger(NullableType, Integer): | |||
| 115 | 115 | pass | |
| 116 | 116 | ||
| 117 | 117 | ||
| 118 | - class String(DatabaseType): | ||
| 118 | + class String(FieldType): | ||
| 119 | 119 | """Represents a string type.""" | |
| 120 | 120 | ||
| 121 | 121 | @staticmethod | |
@@ -139,7 +139,7 @@ class NullableString(NullableType, String): | |||
| 139 | 139 | pass | |
| 140 | 140 | ||
| 141 | 141 | ||
| 142 | - class StringArray(DatabaseType): | ||
| 142 | + class StringArray(FieldType): | ||
| 143 | 143 | """Represents an array of strings type.""" | |
| 144 | 144 | ||
| 145 | 145 | @staticmethod | |
@@ -165,7 +165,7 @@ class NullableStringArray(NullableType, StringArray): | |||
| 165 | 165 | pass | |
| 166 | 166 | ||
| 167 | 167 | ||
| 168 | - class Timestamp(DatabaseType): | ||
| 168 | + class Timestamp(FieldType): | ||
| 169 | 169 | """Represents a timestamp type.""" | |
| 170 | 170 | ||
| 171 | 171 | @staticmethod | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -232,7 +232,7 @@ def create_or_update(cls, transaction=None, **kwargs): | |||
| 232 | 232 | ||
| 233 | 233 | @classmethod | |
| 234 | 234 | 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.""" | ||
| 236 | 236 | to_create, to_update = [], [] | |
| 237 | 237 | columns = cls.columns() | |
| 238 | 238 | for model in models: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,11 +14,8 @@ | |||
| 14 | 14 | # limitations under the License. | |
| 15 | 15 | """Model for interacting with Spanner column schema table.""" | |
| 16 | 16 | ||
| 17 | + from spanner_orm import field | ||
| 17 | 18 | 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 | ||
| 22 | 19 | ||
| 23 | 20 | ||
| 24 | 21 | class ColumnSchema(schema.Schema): | |
@@ -31,13 +28,13 @@ def primary_index_keys(): | |||
| 31 | 28 | @classmethod | |
| 32 | 29 | def schema(cls): | |
| 33 | 30 | 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 | ||
| 41 | 38 | } | |
| 42 | 39 | ||
| 43 | 40 | @classmethod | |
@@ -48,8 +45,8 @@ def nullable(self): | |||
| 48 | 45 | return self.is_nullable == 'YES' | |
| 49 | 46 | ||
| 50 | 47 | 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) | ||
| 53 | 50 | if self.spanner_type == db_type.ddl() and self.nullable() == db_nullable: | |
| 54 | 51 | return db_type | |
| 55 | 52 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,10 +14,8 @@ | |||
| 14 | 14 | # limitations under the License. | |
| 15 | 15 | """Model for interacting with Spanner index schema table.""" | |
| 16 | 16 | ||
| 17 | + from spanner_orm import field | ||
| 17 | 18 | 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 | ||
| 21 | 19 | ||
| 22 | 20 | ||
| 23 | 21 | class IndexSchema(schema.Schema): | |
@@ -30,15 +28,15 @@ def primary_index_keys(): | |||
| 30 | 28 | @classmethod | |
| 31 | 29 | def schema(cls): | |
| 32 | 30 | 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 | ||
| 42 | 40 | } | |
| 43 | 41 | ||
| 44 | 42 | @classmethod | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,10 +14,8 @@ | |||
| 14 | 14 | # limitations under the License. | |
| 15 | 15 | """Model for interacting with Spanner index column schema table.""" | |
| 16 | 16 | ||
| 17 | + from spanner_orm import field | ||
| 17 | 18 | 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 | ||
| 21 | 19 | ||
| 22 | 20 | ||
| 23 | 21 | class IndexColumnSchema(schema.Schema): | |
@@ -33,15 +31,15 @@ def primary_index_keys(): | |||
| 33 | 31 | @classmethod | |
| 34 | 32 | def schema(cls): | |
| 35 | 33 | 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 | ||
| 45 | 43 | } | |
| 46 | 44 | ||
| 47 | 45 | @classmethod | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,13 +15,13 @@ | |||
| 15 | 15 | """Models used by unit tests.""" | |
| 16 | 16 | ||
| 17 | 17 | 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 | ||
| 18 | 24 | 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 | ||
| 25 | 25 | ||
| 26 | 26 | ||
| 27 | 27 | class ChildTestModel(model.Model): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments