From 55cf25d0852376f227cd3bf483f2350d2b7a0dd2 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Mon, 16 Mar 2015 20:34:35 -0400 Subject: [PATCH 1/2] PHPLIB-60: Create result classes for CRUD methods The bulkWrite() method still returns the driver's original WriteResult class. We may change this to use a library-specific wrapper class later. --- src/Collection.php | 5 ++- src/DeleteResult.php | 37 +++++++++++++++++++--- src/InsertOneResult.php | 66 ++++++++++++++++++++++++++++++++++++++++ src/InsertResult.php | 22 -------------- src/UpdateResult.php | 59 +++++++++++++++++++++++++++++++---- tests/CollectionTest.php | 2 +- 6 files changed, 155 insertions(+), 36 deletions(-) create mode 100644 src/InsertOneResult.php delete mode 100644 src/InsertResult.php diff --git a/src/Collection.php b/src/Collection.php index b7c3d52f2..327a252f9 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -406,11 +406,10 @@ public function bulkWrite(array $bulk, array $options = array()) * Inserts the provided document * * @see http://docs.mongodb.org/manual/reference/command/insert/ - * @see Collection::getWriteOptions() for supported $options * * @param array $document The document to insert * @param array $options Additional options - * @return InsertResult + * @return InsertOneResult */ public function insertOne(array $document) { @@ -420,7 +419,7 @@ public function insertOne(array $document) $id = $bulk->insert($document); $wr = $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc); - return new InsertResult($wr, $id); + return new InsertOneResult($wr, $id); } /** diff --git a/src/DeleteResult.php b/src/DeleteResult.php index 2b61dc248..b4f88020b 100644 --- a/src/DeleteResult.php +++ b/src/DeleteResult.php @@ -4,17 +4,46 @@ use MongoDB\Driver\WriteResult; +/** + * Result class for a delete operation. + */ class DeleteResult { - protected $wr; + private $writeResult; - public function __construct(WriteResult $wr) + /** + * Constructor. + * + * @param WriteResult $writeResult + */ + public function __construct(WriteResult $writeResult) { - $this->wr = $wr; + $this->writeResult = $writeResult; } + /** + * Return the number of documents that were deleted. + * + * This value is undefined if the write was not acknowledged. + * + * @see UpdateResult::isAcknowledged() + * @return integer + */ public function getDeletedCount() { - return $this->wr->getDeletedCount(); + return $this->writeResult->getDeletedCount(); + } + + /** + * Return whether this delete was acknowledged by the server. + * + * If the delete was not acknowledged, other fields from the WriteResult + * (e.g. deletedCount) will be undefined. + * + * @return boolean + */ + public function isAcknowledged() + { + return $this->writeResult->isAcknowledged(); } } diff --git a/src/InsertOneResult.php b/src/InsertOneResult.php new file mode 100644 index 000000000..efdd92066 --- /dev/null +++ b/src/InsertOneResult.php @@ -0,0 +1,66 @@ +writeResult = $writeResult; + $this->insertedId = $insertedId; + } + + /** + * Return the number of documents that were inserted. + * + * This value is undefined if the write was not acknowledged. + * + * @see InsertOneResult::isAcknowledged() + * @return integer + */ + public function getInsertedCount() + { + return $this->writeResult->getInsertedCount(); + } + + /** + * Return the inserted ID that was generated by the driver. + * + * If the inserted document already had an ID (e.g. it was generated by the + * application), this will be null. + * + * @return ObjectId|null + */ + public function getInsertedId() + { + return $this->insertedId; + } + + /** + * Return whether this insert was acknowledged by the server. + * + * If the insert was not acknowledged, other fields from the WriteResult + * (e.g. insertedCount) will be undefined. + * + * @return boolean + */ + public function isAcknowledged() + { + return $this->writeResult->isAcknowledged(); + } +} diff --git a/src/InsertResult.php b/src/InsertResult.php deleted file mode 100644 index 11ec45b62..000000000 --- a/src/InsertResult.php +++ /dev/null @@ -1,22 +0,0 @@ -wr = $wr; - $this->id = $id; - } - - public function getInsertedId() - { - return $this->id; - } -} diff --git a/src/UpdateResult.php b/src/UpdateResult.php index 1fff96384..8df698478 100644 --- a/src/UpdateResult.php +++ b/src/UpdateResult.php @@ -2,29 +2,76 @@ namespace MongoDB; +use BSON\ObjectId; use MongoDB\Driver\WriteResult; +/** + * Result class for an update operation. + */ class UpdateResult { - protected $wr; + private $writeResult; - public function __construct(WriteResult $wr) + /** + * Constructor. + * + * @param WriteResult $writeResult + */ + public function __construct(WriteResult $writeResult) { - $this->wr = $wr; + $this->writeResult = $writeResult; } + /** + * Return the number of documents that were matched by the filter. + * + * This value is undefined if the write was not acknowledged. + * + * @see UpdateResult::isAcknowledged() + * @return integer + */ public function getMatchedCount() { - return $this->wr->getMatchedCount(); + return $this->writeResult->getMatchedCount(); } + /** + * Return the number of documents that were modified. + * + * This value is undefined if the write was not acknowledged. + * + * @see UpdateResult::isAcknowledged() + * @return integer + */ public function getModifiedCount() { - return $this->wr->getModifiedCount(); + return $this->writeResult->getModifiedCount(); } + /** + * Return the ID of the document inserted by an upsert operation. + * + * This value is undefined if an upsert did not take place. + * + * @return ObjectId|null + */ public function getUpsertedId() { - return $this->wr->getUpsertedIds()[0]; + foreach ($this->writeResult->getUpsertedIds() as $id) { + return $id; + } + } + + /** + * Return whether this update was acknowledged by the server. + * + * If the update was not acknowledged, other fields from the WriteResult + * (e.g. matchedCount) will be undefined. + * + * @return boolean + */ + public function isAcknowledged() + { + return $this->writeResult->isAcknowledged(); } } diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index 816e423d0..059caa547 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -21,7 +21,7 @@ function testInsertAndRetrieve() { for($i=0; $i<10;$i++) { $user = createUser($this->faker); $result = $collection->insertOne($user); - $this->assertInstanceOf('MongoDB\InsertResult', $result); + $this->assertInstanceOf('MongoDB\InsertOneResult', $result); $this->assertInstanceOf('BSON\ObjectId', $result->getInsertedId()); $this->assertEquals(24, strlen($result->getInsertedId())); From af72fa5f666b74c0f77fb4e7712b87759f1a11ec Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Mon, 16 Mar 2015 20:37:50 -0400 Subject: [PATCH 2/2] PHPLIB-59: Implement insertMany() and InsertManyResult --- src/Collection.php | 28 +++++++++++++++++ src/InsertManyResult.php | 66 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 src/InsertManyResult.php diff --git a/src/Collection.php b/src/Collection.php index 327a252f9..511439e3f 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -422,6 +422,34 @@ public function insertOne(array $document) return new InsertOneResult($wr, $id); } + /** + * Inserts the provided documents + * + * @see http://docs.mongodb.org/manual/reference/command/insert/ + * + * @param array $documents The documents to insert + * @return InsertManyResult + */ + public function insertMany(array $documents) + { + $options = array_merge($this->getWriteOptions()); + + $bulk = new BulkWrite($options["ordered"]); + $insertedIds = array(); + + foreach ($documents as $i => $document) { + $insertedId = $bulk->insert($document); + + if ($insertedId !== null) { + $insertedIds[$i] = $insertedId; + } + } + + $writeResult = $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc); + + return new InsertManyResult($writeResult, $insertedIds); + } + /** * Internal helper for delete one/many documents * @internal diff --git a/src/InsertManyResult.php b/src/InsertManyResult.php new file mode 100644 index 000000000..4480bc8f8 --- /dev/null +++ b/src/InsertManyResult.php @@ -0,0 +1,66 @@ +writeResult = $writeResult; + $this->insertedIds = $insertedIds; + } + + /** + * Return the number of documents that were inserted. + * + * This value is undefined if the write was not acknowledged. + * + * @see InsertManyResult::isAcknowledged() + * @return integer + */ + public function getInsertedCount() + { + return $this->writeResult->getInsertedCount(); + } + + /** + * Return the map of inserted IDs that were generated by the driver. + * + * The index of each ID in the map corresponds to the document's position + * in bulk operation. If an inserted document already had an ID (e.g. it was + * generated by the application), it will not be present in this map. + * + * @return array + */ + public function getInsertedIds() + { + return $this->insertedIds; + } + + /** + * Return whether this insert result was acknowledged by the server. + * + * If the insert was not acknowledged, other fields from the WriteResult + * (e.g. insertedCount) will be undefined. + * + * @return boolean + */ + public function isAcknowledged() + { + return $this->writeResult->isAcknowledged(); + } +}