Skip to content

Commit 31ed96c

Browse files
committed
PHPLIB-109: Extract InsertOne and InsertMany operation classes
1 parent 4257002 commit 31ed96c

File tree

3 files changed

+203
-31
lines changed

3 files changed

+203
-31
lines changed

src/Collection.php

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use MongoDB\Operation\FindOneAndDelete;
2525
use MongoDB\Operation\FindOneAndReplace;
2626
use MongoDB\Operation\FindOneAndUpdate;
27+
use MongoDB\Operation\InsertMany;
28+
use MongoDB\Operation\InsertOne;
2729
use MongoDB\Operation\ListIndexes;
2830
use Traversable;
2931

@@ -539,56 +541,41 @@ public function getWriteOptions()
539541
}
540542

541543
/**
542-
* Inserts the provided documents
544+
* Inserts multiple documents.
543545
*
546+
* @see InsertMany::__construct() for supported options
544547
* @see http://docs.mongodb.org/manual/reference/command/insert/
545-
*
546548
* @param array[]|object[] $documents The documents to insert
549+
* @param array $options Command options
547550
* @return InsertManyResult
548551
*/
549-
public function insertMany(array $documents)
552+
public function insertMany(array $documents, array $options = array())
550553
{
551-
$options = array_merge($this->getWriteOptions());
552-
553-
$bulk = new BulkWrite($options["ordered"]);
554-
$insertedIds = array();
555-
556-
foreach ($documents as $i => $document) {
557-
$insertedId = $bulk->insert($document);
554+
$options += array('writeConcern' => $this->wc);
558555

559-
if ($insertedId !== null) {
560-
$insertedIds[$i] = $insertedId;
561-
} else {
562-
$insertedIds[$i] = is_array($document) ? $document['_id'] : $document->_id;
563-
}
564-
}
565-
566-
$writeResult = $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
556+
$operation = new InsertMany($this->dbname, $this->collname, $documents, $options);
557+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
567558

568-
return new InsertManyResult($writeResult, $insertedIds);
559+
return $operation->execute($server);
569560
}
570561

571562
/**
572-
* Inserts the provided document
563+
* Inserts one document.
573564
*
565+
* @see InsertOne::__construct() for supported options
574566
* @see http://docs.mongodb.org/manual/reference/command/insert/
575-
*
576567
* @param array|object $document The document to insert
568+
* @param array $options Command options
577569
* @return InsertOneResult
578570
*/
579-
public function insertOne($document)
571+
public function insertOne($document, array $options = array())
580572
{
581-
$options = array_merge($this->getWriteOptions());
582-
583-
$bulk = new BulkWrite($options["ordered"]);
584-
$id = $bulk->insert($document);
585-
$wr = $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
573+
$options += array('writeConcern' => $this->wc);
586574

587-
if ($id === null) {
588-
$id = is_array($document) ? $document['_id'] : $document->_id;
589-
}
575+
$operation = new InsertOne($this->dbname, $this->collname, $document, $options);
576+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
590577

591-
return new InsertOneResult($wr, $id);
578+
return $operation->execute($server);
592579
}
593580

594581
/**

src/Operation/InsertMany.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace MongoDB\Operation;
4+
5+
use MongoDB\InsertManyResult;
6+
use MongoDB\Driver\BulkWrite;
7+
use MongoDB\Driver\Server;
8+
use MongoDB\Driver\WriteConcern;
9+
use MongoDB\Exception\InvalidArgumentException;
10+
use MongoDB\Exception\InvalidArgumentTypeException;
11+
12+
/**
13+
* Operation for inserting multiple documents with the insert command.
14+
*
15+
* @api
16+
* @see MongoDB\Collection::insertMany()
17+
* @see http://docs.mongodb.org/manual/reference/command/insert/
18+
*/
19+
class InsertMany implements Executable
20+
{
21+
private $databaseName;
22+
private $collectionName;
23+
private $documents;
24+
private $options;
25+
26+
/**
27+
* Constructs an insert command.
28+
*
29+
* Supported options:
30+
*
31+
* * ordered (boolean): If true, when an insert fails, return without
32+
* performing the remaining writes. If false, when a write fails,
33+
* continue with the remaining writes, if any. The default is true.
34+
*
35+
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
36+
*
37+
* @param string $databaseName Database name
38+
* @param string $collectionName Collection name
39+
* @param array[]|object[] $documents List of documents to insert
40+
* @param array $options Command options
41+
* @throws InvalidArgumentException
42+
*/
43+
public function __construct($databaseName, $collectionName, array $documents, array $options = array())
44+
{
45+
if (empty($documents)) {
46+
throw new InvalidArgumentException('$documents is empty');
47+
}
48+
49+
$expectedIndex = 0;
50+
51+
foreach ($documents as $i => $document) {
52+
if ($i !== $expectedIndex) {
53+
throw new InvalidArgumentException(sprintf('$documents is not a list (unexpected index: "%s")', $i));
54+
}
55+
56+
if ( ! is_array($document) && ! is_object($document)) {
57+
throw new InvalidArgumentTypeException(sprintf('$documents[%d]', $i), $document, 'array or object');
58+
}
59+
60+
$expectedIndex += 1;
61+
}
62+
63+
$options += array(
64+
'ordered' => true,
65+
);
66+
67+
if ( ! is_bool($options['ordered'])) {
68+
throw new InvalidArgumentTypeException('"ordered" option', $options['ordered'], 'boolean');
69+
}
70+
71+
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
72+
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
73+
}
74+
75+
$this->databaseName = (string) $databaseName;
76+
$this->collectionName = (string) $collectionName;
77+
$this->documents = $documents;
78+
$this->options = $options;
79+
}
80+
81+
/**
82+
* Execute the operation.
83+
*
84+
* @see Executable::execute()
85+
* @param Server $server
86+
* @return InsertManyResult
87+
*/
88+
public function execute(Server $server)
89+
{
90+
$bulk = new BulkWrite($this->options['ordered']);
91+
$insertedIds = array();
92+
93+
foreach ($this->documents as $i => $document) {
94+
$insertedId = $bulk->insert($document);
95+
96+
if ($insertedId !== null) {
97+
$insertedIds[$i] = $insertedId;
98+
} else {
99+
// TODO: This may be removed if PHPC-382 is implemented
100+
$insertedIds[$i] = is_array($document) ? $document['_id'] : $document->_id;
101+
}
102+
}
103+
104+
$writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null;
105+
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern);
106+
107+
return new InsertManyResult($writeResult, $insertedIds);
108+
}
109+
}

src/Operation/InsertOne.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace MongoDB\Operation;
4+
5+
use MongoDB\InsertOneResult;
6+
use MongoDB\Driver\BulkWrite;
7+
use MongoDB\Driver\Server;
8+
use MongoDB\Driver\WriteConcern;
9+
use MongoDB\Exception\InvalidArgumentTypeException;
10+
11+
/**
12+
* Operation for inserting a single document with the insert command.
13+
*
14+
* @api
15+
* @see MongoDB\Collection::insertOne()
16+
* @see http://docs.mongodb.org/manual/reference/command/insert/
17+
*/
18+
class InsertOne implements Executable
19+
{
20+
private $databaseName;
21+
private $collectionName;
22+
private $document;
23+
private $options;
24+
25+
/**
26+
* Constructs an insert command.
27+
*
28+
* Supported options:
29+
*
30+
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
31+
*
32+
* @param string $databaseName Database name
33+
* @param string $collectionName Collection name
34+
* @param array|object $document Document to insert
35+
* @param array $options Command options
36+
* @throws InvalidArgumentException
37+
*/
38+
public function __construct($databaseName, $collectionName, $document, array $options = array())
39+
{
40+
if ( ! is_array($document) && ! is_object($document)) {
41+
throw new InvalidArgumentTypeException('$document', $document, 'array or object');
42+
}
43+
44+
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
45+
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
46+
}
47+
48+
$this->databaseName = (string) $databaseName;
49+
$this->collectionName = (string) $collectionName;
50+
$this->document = $document;
51+
$this->options = $options;
52+
}
53+
54+
/**
55+
* Execute the operation.
56+
*
57+
* @see Executable::execute()
58+
* @param Server $server
59+
* @return InsertOneResult
60+
*/
61+
public function execute(Server $server)
62+
{
63+
$bulk = new BulkWrite();
64+
$insertedId = $bulk->insert($this->document);
65+
66+
if ($insertedId === null) {
67+
// TODO: This may be removed if PHPC-382 is implemented
68+
$insertedId = is_array($this->document) ? $this->document['_id'] : $this->document->_id;
69+
}
70+
71+
$writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null;
72+
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern);
73+
74+
return new InsertOneResult($writeResult, $insertedId);
75+
}
76+
}

0 commit comments

Comments
 (0)