Commit a363637f authored by Hannes Magnusson's avatar Hannes Magnusson

PHP-1300: Create WriteResult classes

parent 124f8ad4
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
require __DIR__ . "/../src/MongoDB/QueryFlags.php"; require __DIR__ . "/../src/MongoDB/QueryFlags.php";
require __DIR__ . "/../src/MongoDB/CursorType.php"; require __DIR__ . "/../src/MongoDB/CursorType.php";
require __DIR__ . "/../src/MongoDB/InsertResult.php";
require __DIR__ . "/../src/MongoDB/DeleteResult.php";
require __DIR__ . "/../src/MongoDB/UpdateResult.php";
require __DIR__ . "/../src/MongoDB/Collection.php"; require __DIR__ . "/../src/MongoDB/Collection.php";
...@@ -37,11 +40,11 @@ $spassky = array( ...@@ -37,11 +40,11 @@ $spassky = array(
try { try {
$result = $collection->insertOne($hannes); $result = $collection->insertOne($hannes);
printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted()); printf("Inserted _id: %s\n", $result->getInsertedId());
$result = $collection->insertOne($hayley); $result = $collection->insertOne($hayley);
printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted()); printf("Inserted _id: %s\n", $result->getInsertedId());
$result = $collection->insertOne($bobby); $result = $collection->insertOne($bobby);
printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted()); printf("Inserted _id: %s\n", $result->getInsertedId());
$count = $collection->count(array("nick" => "bjori")); $count = $collection->count(array("nick" => "bjori"));
printf("Searching for nick => bjori, should have only one result: %d\n", $count); printf("Searching for nick => bjori, should have only one result: %d\n", $count);
...@@ -50,7 +53,7 @@ try { ...@@ -50,7 +53,7 @@ try {
array("citizen" => "USA"), array("citizen" => "USA"),
array('$set' => array("citizen" => "Iceland")) array('$set' => array("citizen" => "Iceland"))
); );
printf("Updated: %s (out of expected 1)\n", $result->getNumModified()); printf("Updated: %s (out of expected 1)\n", $result->getModifiedCount());
$result = $collection->find(array("citizen" => "Iceland"), array("comment" => "Excellent query")); $result = $collection->find(array("citizen" => "Iceland"), array("comment" => "Excellent query"));
echo "Searching for citizen => Iceland, verify Hayley is now Icelandic\n"; echo "Searching for citizen => Iceland, verify Hayley is now Icelandic\n";
...@@ -91,7 +94,7 @@ try { ...@@ -91,7 +94,7 @@ try {
array('$set' => array("viking" => true)) array('$set' => array("viking" => true))
); );
printf("Updated: %d (out of expected 2), verify Icelandic people are vikings\n", $result->getNumModified()); printf("Updated: %d (out of expected 2), verify Icelandic people are vikings\n", $result->getModifiedCount());
$result = $collection->find(); $result = $collection->find();
foreach($result as $document) { foreach($result as $document) {
var_dump($document); var_dump($document);
...@@ -108,7 +111,7 @@ try { ...@@ -108,7 +111,7 @@ try {
array("nick" => "Bobby Fischer"), array("nick" => "Bobby Fischer"),
array("name" => "Magnus Carlsen", "nick" => "unknown", "citizen" => "Norway") array("name" => "Magnus Carlsen", "nick" => "unknown", "citizen" => "Norway")
); );
printf("Replaced: %d (out of expected 1), verify Bobby has been replaced with Magnus\n", $result->getNumModified()); printf("Replaced: %d (out of expected 1), verify Bobby has been replaced with Magnus\n", $result->getModifiedCount());
$result = $collection->find(); $result = $collection->find();
foreach($result as $document) { foreach($result as $document) {
var_dump($document); var_dump($document);
...@@ -121,10 +124,10 @@ try { ...@@ -121,10 +124,10 @@ try {
try { try {
$result = $collection->deleteOne($document); $result = $collection->deleteOne($document);
printf("Deleted: %d (out of expected 1)\n", $result->getNumRemoved()); printf("Deleted: %d (out of expected 1)\n", $result->getDeletedCount());
$result = $collection->deleteMany(array("citizen" => "Iceland")); $result = $collection->deleteMany(array("citizen" => "Iceland"));
printf("Deleted: %d (out of expected 2)\n", $result->getNumRemoved()); printf("Deleted: %d (out of expected 2)\n", $result->getDeletedCount());
} catch(Exception $e) { } catch(Exception $e) {
printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
exit; exit;
......
...@@ -172,26 +172,6 @@ class Collection { ...@@ -172,26 +172,6 @@ class Collection {
} /* }}} */ } /* }}} */
/* }}} */ /* }}} */
/* {{{ writes */ /* {{{ writes */
protected function _writeSingle($filter, $type, array $options = array(), $newobj = array()) { /* {{{ */
$options = array_merge($this->getWriteOptions(), $options);
$batch = new WriteBatch($options["ordered"]);
switch($type) {
case self::INSERT:
$batch->insert($filter);
break;
case self::DELETE:
$batch->delete($filter, $options);
break;
case self::UPDATE:
$batch->update($filter, $newobj, $options);
break;
}
return $this->manager->executeWriteBatch($this->ns, $batch, $this->wc);
} /* }}} */
function getWriteOptions() { /* {{{ */ function getWriteOptions() { /* {{{ */
return array( return array(
"ordered" => false, "ordered" => false,
...@@ -201,28 +181,60 @@ class Collection { ...@@ -201,28 +181,60 @@ class Collection {
} /* }}} */ } /* }}} */
function insertOne(array $filter) { /* {{{ */ function insertOne(array $filter) { /* {{{ */
return $this->_writeSingle($filter, self::INSERT); $options = array_merge($this->getWriteOptions());
$batch = new WriteBatch($options["ordered"]);
$id = $batch->insert($filter);
$wr = $this->manager->executeWriteBatch($this->ns, $batch, $this->wc);
return new InsertResult($wr, $id);
} /* }}} */
protected function _delete($filter, $limit = 1) { /* {{{ */
$options = array_merge($this->getWriteOptions(), array("limit" => $limit));
$batch = new WriteBatch($options["ordered"]);
$batch->delete($filter, $options);
return $this->manager->executeWriteBatch($this->ns, $batch, $this->wc);
} /* }}} */ } /* }}} */
function deleteOne(array $filter) { /* {{{ */ function deleteOne(array $filter) { /* {{{ */
return $this->_writeSingle($filter, self::DELETE); $wr = $this->_delete($filter);
return new DeleteResult($wr);
} /* }}} */ } /* }}} */
function deleteMany(array $filter) { /* {{{ */ function deleteMany(array $filter) { /* {{{ */
return $this->_writeSingle($filter, self::DELETE, array("limit" => 0)); $wr = $this->_delete($filter, 0);
return new DeleteResult($wr);
} /* }}} */
protected function _update($filter, $update, $options) { /* {{{ */
$options = array_merge($this->getWriteOptions(), $options);
$batch = new WriteBatch($options["ordered"]);
$batch->update($filter, $update, $options);
return $this->manager->executeWriteBatch($this->ns, $batch, $this->wc);
} /* }}} */ } /* }}} */
function updateOne(array $filter, array $update, array $options = array()) { /* {{{ */ function updateOne(array $filter, array $update, array $options = array()) { /* {{{ */
if (key($update)[0] != '$') { if (key($update)[0] != '$') {
throw new \RuntimeException("First key in \$update must be a \$operator"); throw new \RuntimeException("First key in \$update must be a \$operator");
} }
return $this->_writeSingle($filter, self::UPDATE, $options, $update); $wr = $this->_update($filter, $update, $options);
return new UpdateResult($wr);
} /* }}} */ } /* }}} */
function replaceOne(array $filter, array $update, array $options = array()) { /* {{{ */ function replaceOne(array $filter, array $update, array $options = array()) { /* {{{ */
if (key($update)[0] == '$') { if (key($update)[0] == '$') {
throw new \RuntimeException("First key in \$update must NOT be a \$operator"); throw new \RuntimeException("First key in \$update must NOT be a \$operator");
} }
return $this->_writeSingle($filter, self::UPDATE, $options, $update); $wr = $this->_update($filter, $update, $options);
return new UpdateResult($wr);
} /* }}} */ } /* }}} */
function updateMany(array $filter, $update, array $options = array()) { /* {{{ */ function updateMany(array $filter, $update, array $options = array()) { /* {{{ */
return $this->_writeSingle($filter, self::UPDATE, $options + array("limit" => 0), $update); $wr = $this->_update($filter, $update, $options + array("limit" => 0));
return new UpdateResult($wr);
} /* }}} */ } /* }}} */
/* }}} */ /* }}} */
......
<?php
namespace MongoDB;
class DeleteResult {
protected $wr;
function __construct(\MongoDB\WriteResult $wr) {
$this->wr = $wr;
}
function getDeletedCount() {
return $this->wr->getDeletedCount();
}
}
<?php
namespace MongoDB;
class InsertResult {
protected $wr;
function __construct(\MongoDB\WriteResult $wr, \BSON\ObjectId $id = null) {
$this->wr = $wr;
$this->id = $id;
}
function getInsertedId() {
return $this->id;
}
}
<?php
namespace MongoDB;
class UpdateResult {
protected $wr;
function __construct(\MongoDB\WriteResult $wr) {
$this->wr = $wr;
}
function getMatchedCount() {
return $this->wr->getMatchedCount();
}
function getModifiedCount() {
return $this->wr->getModifiedCount();
}
function getUpsertedId() {
return $this->wr->getUpsertedIds()[0];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment