Skip to content

Commit fb15fa7

Browse files
committed
Add bulk update/insertion, and other improvements.
--HG-- extra : convert_revision : svn%3A7a298fb0-333a-0410-83e7-658617cd9cf3/trunk%4042
1 parent fc15111 commit fb15fa7

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

ChangeLog.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ http://couchdb-python.googlecode.com/svn/tags/0.2.0
1616
encoded, anything else is left alone.
1717
* Slashes in document IDs are now URL-quoted until CouchDB supports
1818
them.
19+
* Allow the content-type to be passed for temp views via
20+
`client.Database.query()` so that view languages other than
21+
Javascript can be used.
22+
* Added `client.Database.update()` method to bulk insert/update
23+
documents in a database.
1924

2025

2126
Version 0.1

couchdb/client.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,7 @@ def __setitem__(self, id, content):
256256
documents
257257
"""
258258
result = self.resource.put(id, content=content)
259-
content['_id'] = result['id']
260-
content['_rev'] = result['rev']
259+
content.update({'_id': result['id'], '_rev': result['rev']})
261260

262261
def _get_name(self):
263262
if self._name is None:
@@ -301,7 +300,7 @@ def get(self, id, default=None, **options):
301300
return default
302301
return Document(row)
303302

304-
def query(self, code, **options):
303+
def query(self, code, content_type='text/javascript', **options):
305304
"""Execute an ad-hoc query against the database.
306305
307306
>>> server = Server('http://localhost:8888/')
@@ -330,17 +329,56 @@ def query(self, code, **options):
330329
>>> del server['python-tests']
331330
332331
:param code: the code of the view function
332+
:param content_type: the MIME type of the code, which determines the
333+
language the view function is written in
333334
:return: an iterable over the resulting `Row` objects
334335
:rtype: ``generator``
335336
"""
336337
for name, value in options.items():
337338
if name in ('key', 'startkey', 'endkey') \
338339
or not isinstance(value, basestring):
339340
options[name] = json.dumps(value)
340-
data = self.resource.post('_temp_view', content=code, **options)
341+
headers = {}
342+
if content_type:
343+
headers['Content-Type'] = content_type
344+
data = self.resource.post('_temp_view', content=code, headers=headers,
345+
**options)
341346
for row in data['rows']:
342347
yield Row(row['id'], row['key'], row['value'])
343348

349+
def update(self, documents):
350+
"""Perform a bulk update or insertion of the given documents using a
351+
single HTTP request.
352+
353+
>>> server = Server('http://localhost:8888/')
354+
>>> db = server.create('python-tests')
355+
>>> for doc in db.update([
356+
... Document(type='Person', name='John Doe'),
357+
... Document(type='Person', name='Mary Jane'),
358+
... Document(type='City', name='Gotham City')
359+
... ]):
360+
... print repr(doc) #doctest: +ELLIPSIS
361+
<Document u'...'@u'...' {'type': 'Person', 'name': 'John Doe'}>
362+
<Document u'...'@u'...' {'type': 'Person', 'name': 'Mary Jane'}>
363+
<Document u'...'@u'...' {'type': 'City', 'name': 'Gotham City'}>
364+
365+
>>> del server['python-tests']
366+
367+
:param documents: a sequence of dictionaries or `Document` objects
368+
:return: an iterable over the resulting documents
369+
:rtype: ``generator``
370+
371+
:since: version 0.2
372+
"""
373+
documents = list(documents)
374+
content = {'_bulk_docs': documents}
375+
data = self.resource.post(content=content)
376+
for idx, result in enumerate(data['results']):
377+
assert 'ok' in result # FIXME: how should error handling work here?
378+
doc = documents[idx]
379+
doc.update({'_id': result['id'], '_rev': result['rev']})
380+
yield doc
381+
344382
def view(self, name, **options):
345383
"""Execute a predefined view.
346384
@@ -369,8 +407,8 @@ class Document(dict):
369407
`id` and `rev`, which contain the document ID and revision, respectively.
370408
"""
371409

372-
def __init__(self, content):
373-
dict.__init__(self, content)
410+
def __init__(self, *args, **kwargs):
411+
dict.__init__(self, *args, **kwargs)
374412

375413
def __repr__(self):
376414
return '<%s %r@%r %r>' % (type(self).__name__, self.id, self.rev,

0 commit comments

Comments
 (0)