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: update batch-usage reflect the correct usage #93

Merged
merged 3 commits into from Jun 4, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 39 additions & 52 deletions docs/batch-usage.rst
Expand Up @@ -10,27 +10,52 @@ modification operations to be performed on tables in a database. Use of a
no changes are propagated to the back-end.


Starting a Batch
----------------
Use Batch via BatchCheckout
--------------------------------

Construct a :class:`~google.cloud.spanner.batch.Batch` object from a :class:`~google.cloud.spanner.database.Database` object:
:meth:`Database.batch` creates a :class:`BatchCheckout` instance to use as a context manager to handle creating and
committing a :class:`Batch`. The :class:`BatchCheckout` will automatically call :meth:`~google.cloud.spanner.batch.Batch.commit`
if the ``with`` block exits without raising an exception.

.. code:: python

from google.cloud import spanner
from google.cloud.spanner import KeySet

client = spanner.Client()
instance = client.instance(INSTANCE_NAME)
database = instance.database(DATABASE_NAME)

batch = database.batch()
to_delete = KeySet(keys=[
('bharney@example.com',)
('nonesuch@example.com',)
])

with database.batch() as batch:

batch.insert(
'citizens', columns=['email', 'first_name', 'last_name', 'age'],
values=[
['phred@exammple.com', 'Phred', 'Phlyntstone', 32],
['bharney@example.com', 'Bharney', 'Rhubble', 31],
])

batch.update(
'citizens', columns=['email', 'age'],
values=[
['phred@exammple.com', 33],
['bharney@example.com', 32],
])

...

batch.delete('citizens', to_delete)


Inserting records using a Batch
-------------------------------

:meth:`Batch.insert` adds one or more new records to a table. Fails if
any of the records already exists.
:meth:`Batch.insert` adds one or more new records to a table. This fails if
any of the records already exist.

.. code:: python

Expand All @@ -53,8 +78,8 @@ any of the records already exists.
Update records using a Batch
-------------------------------

:meth:`Batch.update` updates one or more existing records in a table. Fails
if any of the records does not already exist.
:meth:`Batch.update` updates one or more existing records in a table. This fails
if any of the records do not already exist.

.. code:: python

Expand Down Expand Up @@ -127,8 +152,8 @@ column values are set to null.
Delete records using a Batch
----------------------------

:meth:`Batch.delete` removes one or more records from a table. Non-existent
rows do not cause errors.
:meth:`Batch.delete` removes one or more records from a table. Attempting to delete
rows that do not exist will not cause errors.

.. code:: python

Expand All @@ -151,50 +176,12 @@ After describing the modifications to be made to table data via the
the back-end by calling :meth:`Batch.commit`, which makes the ``Commit``
API call.

.. code:: python

batch.commit()


Use a Batch as a Context Manager
--------------------------------

Rather than calling :meth:`Batch.commit` manually, you can use the
:class:`Batch` instance as a context manager, and have it called automatically
if the ``with`` block exits without raising an exception.
Yu do not need to call this yourself as :class:`BatchCheckout` will call
larkee marked this conversation as resolved.
Show resolved Hide resolved
this method automatically upon exiting the ``with`` block.

.. code:: python

from google.cloud.spanner import KeySet

client = spanner.Client()
instance = client.instance(INSTANCE_NAME)
database = instance.database(DATABASE_NAME)

to_delete = KeySet(keys=[
('bharney@example.com',)
('nonesuch@example.com',)
])

with database.batch() as batch:

batch.insert(
'citizens', columns=['email', 'first_name', 'last_name', 'age'],
values=[
['phred@exammple.com', 'Phred', 'Phlyntstone', 32],
['bharney@example.com', 'Bharney', 'Rhubble', 31],
])

batch.update(
'citizens', columns=['email', 'age'],
values=[
['phred@exammple.com', 33],
['bharney@example.com', 32],
])

...

batch.delete('citizens', to_delete)
batch.commit()


Next Step
Expand Down