Skip to content

Commit 80ff3b7

Browse files
committed
Add BatchQuery and ContextQuery in the docs
1 parent bc909af commit 80ff3b7

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

cassandra/cqlengine/query.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ class BatchQuery(object):
136136
Handles the batching of queries
137137
138138
http://www.datastax.com/docs/1.2/cql_cli/cql/BATCH
139+
140+
See :doc:`/cqlengine/batches` for more details.
139141
"""
140142
warn_multiple_exec = True
141143

@@ -173,6 +175,11 @@ def __init__(self, batch_type=None, timestamp=None, consistency=None, execute_on
173175
self._context_entered = False
174176

175177
def add_query(self, query):
178+
"""
179+
Adds a query to the batch.
180+
181+
:param query: The query
182+
"""
176183
if not isinstance(query, BaseCQLStatement):
177184
raise CQLEngineException('only BaseCQLStatements can be added to a batch query')
178185
self.queries.append(query)
@@ -202,6 +209,9 @@ def add_callback(self, fn, *args, **kwargs):
202209
self._callbacks.append((fn, args, kwargs))
203210

204211
def execute(self):
212+
"""
213+
Executes the batch.
214+
"""
205215
if self._executed and self.warn_multiple_exec:
206216
msg = "Batch executed multiple times."
207217
if self._context_entered:
@@ -260,8 +270,28 @@ def __exit__(self, exc_type, exc_val, exc_tb):
260270

261271

262272
class ContextQuery(object):
273+
"""
274+
A Context manager to allow a Model to switch context easily. Presently, the context only
275+
specifies a keyspace for model IO.
276+
277+
For example:
278+
279+
.. code-block:: python
280+
281+
with ContextQuery(Automobile, keyspace='test2') as A:
282+
A.objects.create(manufacturer='honda', year=2008, model='civic')
283+
print len(A.objects.all()) # 1 result
284+
285+
with ContextQuery(Automobile, keyspace='test4') as A:
286+
print len(A.objects.all()) # 0 result
287+
288+
"""
263289

264290
def __init__(self, model, keyspace=None):
291+
"""
292+
:param model: A model
293+
:param keyspace: (optional) A keyspace name
294+
"""
265295
from cassandra.cqlengine import models
266296

267297
if not issubclass(model, models.Model):

docs/api/cassandra/cqlengine/query.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ The methods here are used to filter, order, and constrain results.
5454

5555
.. automethod:: update
5656

57+
.. autoclass:: BatchQuery
58+
:members:
59+
60+
.. autoclass:: ContextQuery
61+
5762
.. autoclass:: DoesNotExist
5863

5964
.. autoclass:: MultipleObjectsReturned

0 commit comments

Comments
 (0)