Skip to content

Commit 0792659

Browse files
committed
PHPLIB-109: Extract InsertOne and InsertMany operation classes
1 parent aa124d0 commit 0792659

File tree

3 files changed

+206
-28
lines changed

3 files changed

+206
-28
lines changed

src/Collection.php

Lines changed: 21 additions & 28 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

@@ -536,56 +538,47 @@ public function getWriteOptions()
536538
}
537539

538540
/**
539-
* Inserts the provided documents
541+
* Inserts multiple documents.
540542
*
541543
* @see http://docs.mongodb.org/manual/reference/command/insert/
542544
*
545+
* @see InsertMany::__construct() for supported options
543546
* @param array[]|object[] $documents The documents to insert
547+
* @param array $options Command options
544548
* @return InsertManyResult
545549
*/
546-
public function insertMany(array $documents)
550+
public function insertMany(array $documents, array $options = array())
547551
{
548-
$options = array_merge($this->getWriteOptions());
549-
550-
$bulk = new BulkWrite($options["ordered"]);
551-
$insertedIds = array();
552-
553-
foreach ($documents as $i => $document) {
554-
$insertedId = $bulk->insert($document);
555-
556-
if ($insertedId !== null) {
557-
$insertedIds[$i] = $insertedId;
558-
} else {
559-
$insertedIds[$i] = is_array($document) ? $document['_id'] : $document->_id;
560-
}
552+
if (!isset($options['writeConcern'])) {
553+
$options['writeConcern'] = $this->wc;
561554
}
562555

563-
$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));
564558

565-
return new InsertManyResult($writeResult, $insertedIds);
559+
return $operation->execute($server);
566560
}
567561

568562
/**
569-
* Inserts the provided document
563+
* Inserts one document.
570564
*
571565
* @see http://docs.mongodb.org/manual/reference/command/insert/
572566
*
567+
* @see InsertOne::__construct() for supported options
573568
* @param array|object $document The document to insert
569+
* @param array $options Command options
574570
* @return InsertOneResult
575571
*/
576-
public function insertOne($document)
572+
public function insertOne($document, array $options = array())
577573
{
578-
$options = array_merge($this->getWriteOptions());
579-
580-
$bulk = new BulkWrite($options["ordered"]);
581-
$id = $bulk->insert($document);
582-
$wr = $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
583-
584-
if ($id === null) {
585-
$id = is_array($document) ? $document['_id'] : $document->_id;
574+
if (!isset($options['writeConcern'])) {
575+
$options['writeConcern'] = $this->wc;
586576
}
587577

588-
return new InsertOneResult($wr, $id);
578+
$operation = new InsertOne($this->dbname, $this->collname, $document, $options);
579+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
580+
581+
return $operation->execute($server);
589582
}
590583

591584
/**

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)