Skip to content

Commit 79d88ad

Browse files
committed
Implemented revision check on replacing a document.
Signed-off-by: Frank Mayer <frank@frankmayer.net>
1 parent e6c111c commit 79d88ad

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/triagens/ArangoDb/ConnectionOptions.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ class ConnectionOptions implements \ArrayAccess {
6161
*/
6262
const OPTION_CREATE = 'createCollection';
6363

64+
/**
65+
* Update revision constant
66+
*/
67+
const OPTION_REVISION = 'rev';
68+
6469
/**
6570
* Update policy index constant
6671
*/
@@ -195,6 +200,7 @@ private static function getDefaults() {
195200
self::OPTION_TIMEOUT => DefaultValues::DEFAULT_TIMEOUT,
196201
self::OPTION_CREATE => DefaultValues::DEFAULT_CREATE,
197202
self::OPTION_UPDATE_POLICY => DefaultValues::DEFAULT_UPDATE_POLICY,
203+
self::OPTION_REVISION => NULL,
198204
self::OPTION_WAIT_SYNC => DefaultValues::DEFAULT_WAIT_SYNC,
199205
self::OPTION_CONNECTION => DefaultValues::DEFAULT_CONNECTION,
200206
self::OPTION_TRACE => NULL,

lib/triagens/ArangoDb/DocumentHandler.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ public function save($collectionId, Document $document, $create = NULL) {
160160
* Update an existing document in a collection, identified by the document itself
161161
* This will update the document on the server
162162
* This will throw if the document cannot be updated
163+
* If policy is set to error (locally or globally through the connectionoptions)
164+
* and the passed document has a _rev value set, the database will check
165+
* that the revision of the to-be-replaced document is the same as the one given.
163166
*
164167
* @throws Exception
165168
* @param Document $document - document to be updated
@@ -176,6 +179,9 @@ public function update(Document $document, $policy = NULL) {
176179
* Replace an existing document in a collection, identified by the document itself
177180
* This will update the document on the server
178181
* This will throw if the document cannot be replaced
182+
* If policy is set to error (locally or globally through the connectionoptions)
183+
* and the passed document has a _rev value set, the database will check
184+
* that the revision of the to-be-replaced document is the same as the one given.
179185
*
180186
* @throws Exception
181187
* @param Document $document - document to be updated
@@ -193,6 +199,9 @@ public function replace(Document $document, $policy = NULL) {
193199
* Update an existing document in a collection, identified by collection id and document id
194200
* This will update the document on the server
195201
* This will throw if the document cannot be updated
202+
* If policy is set to error (locally or globally through the connectionoptions)
203+
* and the passed document has a _rev value set, the database will check
204+
* that the revision of the to-be-replaced document is the same as the one given.
196205
*
197206
* @throws Exception
198207
* @param mixed $collectionId - collection id as string or number
@@ -212,6 +221,9 @@ public function updateById($collectionId, $documentId, Document $document, $poli
212221
* Replace an existing document in a collection, identified by collection id and document id
213222
* This will update the document on the server
214223
* This will throw if the document cannot be Replaced
224+
* If policy is set to error (locally or globally through the connectionoptions)
225+
* and the passed document has a _rev value set, the database will check
226+
* that the revision of the to-be-replaced document is the same as the one given.
215227
*
216228
* @throws Exception
217229
* @param mixed $collectionId - collection id as string or number
@@ -221,15 +233,24 @@ public function updateById($collectionId, $documentId, Document $document, $poli
221233
* @return bool - always true, will throw if there is an error
222234
*/
223235
public function replaceById($collectionId, $documentId, Document $document, $policy = NULL) {
236+
$rev = $document->getRevision();
237+
if (!is_null($rev)) {
238+
$params[ConnectionOptions::OPTION_REVISION]=$rev;
239+
}
240+
241+
242+
224243
if ($policy === NULL) {
225244
$policy = $this->getConnection()->getOption(ConnectionOptions::OPTION_UPDATE_POLICY);
226245
}
227-
246+
$params[ConnectionOptions::OPTION_UPDATE_POLICY]=$policy;
247+
248+
228249
UpdatePolicy::validate($policy);
229250

230251
$data = $document->getAll();
231252
$url = UrlHelper::buildUrl(Urls::URL_DOCUMENT, $collectionId, $documentId);
232-
$url = UrlHelper::appendParamsUrl($url, array(ConnectionOptions::OPTION_UPDATE_POLICY => $policy));
253+
$url = UrlHelper::appendParamsUrl($url, $params);
233254
$result = $this->getConnection()->put($url, json_encode($data));
234255

235256
return true;

0 commit comments

Comments
 (0)