Skip to content

Commit 6ec64d8

Browse files
authored
docs: update batch-usage reflect the correct usage (#93)
The documentation incorrectly stated that `database.batch()` would return a `Batch` object. The recommended way to get a `Batch` object is to use the `BatchCheckout` as a context manager in a `with` statement. The documentation has been update to reflect this. Documentation links and minor grammar fixes are also included.
1 parent da0bc88 commit 6ec64d8

File tree

1 file changed

+47
-56
lines changed

1 file changed

+47
-56
lines changed

docs/batch-usage.rst

Lines changed: 47 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,64 @@
11
Batching Modifications
22
######################
33

4-
A :class:`~google.cloud.spanner.batch.Batch` represents a set of data
4+
A :class:`~google.cloud.spanner_v1.batch.Batch` represents a set of data
55
modification operations to be performed on tables in a database. Use of a
66
``Batch`` does not require creating an explicit
7-
:class:`~google.cloud.spanner.snapshot.Snapshot` or
8-
:class:`~google.cloud.spanner.transaction.Transaction`. Until
9-
:meth:`~google.cloud.spanner.batch.Batch.commit` is called on a ``Batch``,
7+
:class:`~google.cloud.spanner_v1.snapshot.Snapshot` or
8+
:class:`~google.cloud.spanner_v1.transaction.Transaction`. Until
9+
:meth:`~google.cloud.spanner_v1.batch.Batch.commit` is called on a ``Batch``,
1010
no changes are propagated to the back-end.
1111

1212

13-
Starting a Batch
14-
----------------
13+
Use Batch via BatchCheckout
14+
--------------------------------
1515

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

1823
.. code:: python
1924
20-
from google.cloud import spanner
25+
from google.cloud.spanner import KeySet
2126
2227
client = spanner.Client()
2328
instance = client.instance(INSTANCE_NAME)
2429
database = instance.database(DATABASE_NAME)
2530
26-
batch = database.batch()
31+
to_delete = KeySet(keys=[
32+
33+
34+
])
35+
36+
with database.batch() as batch:
37+
38+
batch.insert(
39+
'citizens', columns=['email', 'first_name', 'last_name', 'age'],
40+
values=[
41+
['[email protected]', 'Phred', 'Phlyntstone', 32],
42+
['[email protected]', 'Bharney', 'Rhubble', 31],
43+
])
44+
45+
batch.update(
46+
'citizens', columns=['email', 'age'],
47+
values=[
48+
49+
50+
])
51+
52+
...
53+
54+
batch.delete('citizens', to_delete)
2755
2856
2957
Inserting records using a Batch
3058
-------------------------------
3159

32-
:meth:`Batch.insert` adds one or more new records to a table. Fails if
33-
any of the records already exists.
60+
:meth:`Batch.insert` adds one or more new records to a table. This fails if
61+
any of the records already exist.
3462

3563
.. code:: python
3664
@@ -53,8 +81,8 @@ any of the records already exists.
5381
Update records using a Batch
5482
-------------------------------
5583

56-
:meth:`Batch.update` updates one or more existing records in a table. Fails
57-
if any of the records does not already exist.
84+
:meth:`Batch.update` updates one or more existing records in a table. This fails
85+
if any of the records do not already exist.
5886

5987
.. code:: python
6088
@@ -127,8 +155,8 @@ column values are set to null.
127155
Delete records using a Batch
128156
----------------------------
129157

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

133161
.. code:: python
134162
@@ -151,50 +179,13 @@ After describing the modifications to be made to table data via the
151179
the back-end by calling :meth:`Batch.commit`, which makes the ``Commit``
152180
API call.
153181

154-
.. code:: python
155-
156-
batch.commit()
157-
158-
159-
Use a Batch as a Context Manager
160-
--------------------------------
161-
162-
Rather than calling :meth:`Batch.commit` manually, you can use the
163-
:class:`Batch` instance as a context manager, and have it called automatically
164-
if the ``with`` block exits without raising an exception.
182+
You do not need to call this yourself as
183+
:class:`~google.cloud.spanner_v1.database.BatchCheckout` will call
184+
this method automatically upon exiting the ``with`` block.
165185

166186
.. code:: python
167187
168-
from google.cloud.spanner import KeySet
169-
170-
client = spanner.Client()
171-
instance = client.instance(INSTANCE_NAME)
172-
database = instance.database(DATABASE_NAME)
173-
174-
to_delete = KeySet(keys=[
175-
176-
177-
])
178-
179-
with database.batch() as batch:
180-
181-
batch.insert(
182-
'citizens', columns=['email', 'first_name', 'last_name', 'age'],
183-
values=[
184-
['[email protected]', 'Phred', 'Phlyntstone', 32],
185-
['[email protected]', 'Bharney', 'Rhubble', 31],
186-
])
187-
188-
batch.update(
189-
'citizens', columns=['email', 'age'],
190-
values=[
191-
192-
193-
])
194-
195-
...
196-
197-
batch.delete('citizens', to_delete)
188+
batch.commit()
198189
199190
200191
Next Step

0 commit comments

Comments
 (0)