Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Docstrings for django_spanner #564

Merged
merged 36 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
e264a94
docs for base
tina80lvl Nov 25, 2020
5069132
docs for client
tina80lvl Nov 25, 2020
ca9c7ce
docs for compiler
tina80lvl Nov 26, 2020
1a6257a
docs for expressions
tina80lvl Nov 26, 2020
cfdba5f
docs for features
tina80lvl Nov 26, 2020
2b742f8
docs for functions
tina80lvl Nov 26, 2020
a82bf1b
docs for introspection
tina80lvl Nov 26, 2020
b819547
resolving conflict
tina80lvl Nov 26, 2020
321858f
docs for lookups
tina80lvl Nov 26, 2020
e77b4e9
docs for operations
tina80lvl Dec 3, 2020
4db2d4a
docs for schema
tina80lvl Dec 3, 2020
115881f
docs for utils
tina80lvl Dec 3, 2020
0647468
docs: updates to `base.py`
mf2199 Dec 3, 2020
6970b97
docs: updates to `client.py`
mf2199 Dec 3, 2020
8dab421
docs for validation
tina80lvl Dec 3, 2020
04a0cac
docs: updates to `complier.py`
mf2199 Dec 3, 2020
c2a09f0
Merge branch 'docstr-2' of https://github.com/q-logic/python-spanner-…
mf2199 Dec 3, 2020
0d2ec73
docs: updates to `creation.py`
mf2199 Dec 3, 2020
3a6e84a
docs: updates to `expressions.py`
mf2199 Dec 3, 2020
1479a2f
docs: updates to `features.py`
mf2199 Dec 3, 2020
c902cd2
docs: updates to `functions.py`
mf2199 Dec 3, 2020
6cfb4a5
docs: updates to `introspection.py`
mf2199 Dec 3, 2020
5580621
docs: updates to `lookups.py`
mf2199 Dec 3, 2020
7deb3d3
docs: updates to `operations.py`
mf2199 Dec 3, 2020
ed86c06
Merge branch 'master' into docstr-2
c24t Dec 7, 2020
67106fa
fix: docstrings
mf2199 Dec 7, 2020
c9364d0
fix: typo
mf2199 Dec 8, 2020
678ecb3
fix: docstrings
mf2199 Dec 9, 2020
0b84fb6
fix: single quotes
tina80lvl Dec 9, 2020
583398d
fix: docstrings typing
tina80lvl Dec 9, 2020
2afc4eb
Remove runshell docstring
c24t Dec 10, 2020
d36b9ab
Remove some TODOs from docstrings
c24t Dec 10, 2020
117484b
Merge branch 'master' into docstr-2
c24t Dec 15, 2020
5240d21
Fix operations docstrings
c24t Dec 15, 2020
499c61a
Merge branch 'master' into docstr-2
mf2199 Dec 16, 2020
d749356
docs: docstrings
mf2199 Dec 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion django_spanner/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ class DatabaseWrapper(BaseDatabaseWrapper):

@property
def instance(self):
"""Reference to a Cloud Spanner Instance containing the Database.

:rtype: :class:`~google.cloud.spanner_v1.instance.Instance`
:returns: A new instance owned by the existing Spanner Client.
"""
return spanner.Client().instance(self.settings_dict["INSTANCE"])

@property
Expand All @@ -112,6 +117,12 @@ def _nodb_connection(self):
)

def get_connection_params(self):
"""Retrieve the connection parameters.

:rtype: dict
:returns: A dictionary containing the Spanner connection parameters
in Django Spanner format.
"""
return {
"project": self.settings_dict["PROJECT"],
"instance_id": self.settings_dict["INSTANCE"],
Expand All @@ -120,20 +131,52 @@ def get_connection_params(self):
**self.settings_dict["OPTIONS"],
}

def get_new_connection(self, conn_params):
def get_new_connection(self, **conn_params):
"""Creates a new connection with corresponding connection parameters.

:type conn_params: list
:param conn_params: A List of the connection parameters for
:class:`~google.cloud.spanner_dbapi.connection.Connection`

:rtype: :class:`google.cloud.spanner_dbapi.connection.Connection`
:returns: A new Spanner DB API Connection object associated with the
given Google Cloud Spanner resource.

:raises: :class:`ValueError` in case the given instance/database
doesn't exist.
"""
return Database.connect(**conn_params)

def init_connection_state(self):
"""Initialize the state of the existing connection."""
pass

def create_cursor(self, name=None):
"""Create a new Database cursor.

:type name: str
:param name: Currently not used.

:rtype: :class:`~google.cloud.spanner_dbapi.cursor.Cursor`
:returns: The Cursor for this connection.
"""
return self.connection.cursor()

def _set_autocommit(self, autocommit):
"""Set the Spanner transaction autocommit flag.

:type autocommit: bool
:param autocommit: The new value of the autocommit flag.
"""
with self.wrap_database_errors:
self.connection.autocommit = autocommit

def is_usable(self):
"""Check whether the connection is valid.

:rtype: bool
:returns: True if the connection is open, otherwise False.
"""
if self.connection is None:
return False
try:
Expand Down
11 changes: 11 additions & 0 deletions django_spanner/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,16 @@


class DatabaseClient(BaseDatabaseClient):
"""Wraps the Django base class."""

def runshell(self, parameters):
"""Overrides the base class method.

TODO: Consider actual implementation of this method.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear that we could implement this since cloud spanner doesn't have a "shell" executable. We might want to consider https://github.com/cloudspannerecosystem/spanner-cli or a similar project for this in the future.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the docstring here, just raising NotSupportedError is fine since spanner doesn't really give us a way to support this.


:type parameters: list
:param parameters: Currently not used.

:raises: :class:`~google.cloud.spanner_dbapi.exceptions.NotSupportedError`
"""
raise NotSupportedError("This method is not supported.")
21 changes: 20 additions & 1 deletion django_spanner/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,26 @@


class SQLCompiler(BaseSQLCompiler):
"""A variation of the Django SQL compiler, adjusted for Spanner-specific
functionality.
"""

def get_combinator_sql(self, combinator, all):
"""
"""Overrides the native Django method.

Copied from the base class except for:
combinator_sql += ' ALL' if all else ' DISTINCT'
Cloud Spanner requires ALL or DISTINCT.

:type combinator: str
:param combinator: A type of the combinator for the operation.

:type all: bool
:param all: Bool option for the SQL statement.

:rtype: tuple
:returns: A tuple containing SQL statement(s) with some additional
parameters.
"""
features = self.connection.features
compilers = [
Expand Down Expand Up @@ -97,16 +112,20 @@ def get_combinator_sql(self, combinator, all):


class SQLInsertCompiler(BaseSQLInsertCompiler, SQLCompiler):
"""A wrapper class for compatibility with Django specifications."""
pass


class SQLDeleteCompiler(BaseSQLDeleteCompiler, SQLCompiler):
"""A wrapper class for compatibility with Django specifications."""
pass


class SQLUpdateCompiler(BaseSQLUpdateCompiler, SQLCompiler):
"""A wrapper class for compatibility with Django specifications."""
pass


class SQLAggregateCompiler(BaseSQLAggregateCompiler, SQLCompiler):
"""A wrapper class for compatibility with Django specifications."""
pass
13 changes: 13 additions & 0 deletions django_spanner/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@


class DatabaseCreation(BaseDatabaseCreation):
"""Spanner-specific wrapper for Django class encapsulating methods for
creation and destruction of the underlying test database.
"""

def mark_skips(self):
"""Skip tests that don't work on Spanner."""
for test_name in self.connection.features.skip_tests:
Expand All @@ -30,13 +34,22 @@ def mark_skips(self):
)

def create_test_db(self, *args, **kwargs):
"""Creates a test database.

:rtype: str
:returns: The name of the newly created test Database.
"""
# This environment variable is set by the Travis build script or
# by a developer running the tests locally.
if os.environ.get("RUNNING_SPANNER_BACKEND_TESTS") == "1":
self.mark_skips()
super().create_test_db(*args, **kwargs)

def _create_test_db(self, verbosity, autoclobber, keepdb=False):
"""Creates dummy test tables. This method is mostly copied from the
base class but removes usage of _nodb_connection since Spanner doesn't
have or need one.
"""
# Mostly copied from the base class but removes usage of
# _nodb_connection since Spanner doesn't have or need one.
test_database_name = self._get_test_db_name()
Expand Down
12 changes: 10 additions & 2 deletions django_spanner/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@


def order_by(self, compiler, connection, **extra_context):
# In Django 3.1, this can be replaced with
# DatabaseFeatures.supports_order_by_nulls_modifier = False.
"""Order expressions in the SQL query and generate a new query using
Spanner-specific templates.

:rtype: str
:returns: A SQL query.
"""
# TODO: In Django 3.1, this can be replaced with
# DatabaseFeatures.supports_order_by_nulls_modifier = False.
# Also, consider making this a class method.
template = None
if self.nulls_last:
template = "%(expression)s IS NULL, %(expression)s %(ordering)s"
Expand All @@ -21,4 +28,5 @@ def order_by(self, compiler, connection, **extra_context):


def register_expressions():
"""Add Spanner-specific attribute to the Django OrderBy class."""
OrderBy.as_spanner = order_by
1 change: 1 addition & 0 deletions django_spanner/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@


class DatabaseFeatures(BaseDatabaseFeatures):
"""An extension to Django database feature descriptor."""
can_introspect_big_integer_field = False
can_introspect_duration_field = False
can_introspect_foreign_keys = False
Expand Down
Loading