Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
ad4f3e6c
Commit
ad4f3e6c
authored
Feb 20, 2018
by
Katherine Walker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add getCommandDocument for Delete and Update
parent
fefdb978
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
108 additions
and
29 deletions
+108
-29
Delete.php
src/Operation/Delete.php
+28
-8
Update.php
src/Operation/Update.php
+35
-17
ExplainFunctionalTest.php
tests/Operation/ExplainFunctionalTest.php
+45
-4
No files found.
src/Operation/Delete.php
View file @
ad4f3e6c
...
@@ -35,7 +35,7 @@ use MongoDB\Exception\UnsupportedException;
...
@@ -35,7 +35,7 @@ use MongoDB\Exception\UnsupportedException;
* @internal
* @internal
* @see http://docs.mongodb.org/manual/reference/command/delete/
* @see http://docs.mongodb.org/manual/reference/command/delete/
*/
*/
class
Delete
implements
Executable
class
Delete
implements
Executable
,
Explainable
{
{
private
static
$wireVersionForCollation
=
5
;
private
static
$wireVersionForCollation
=
5
;
...
@@ -117,18 +117,38 @@ class Delete implements Executable
...
@@ -117,18 +117,38 @@ class Delete implements Executable
throw
UnsupportedException
::
collationNotSupported
();
throw
UnsupportedException
::
collationNotSupported
();
}
}
$deleteOptions
=
$this
->
createDeleteOptions
();
$bulk
=
new
Bulk
();
$bulk
->
delete
(
$this
->
filter
,
$this
->
createDeleteOptions
());
$writeResult
=
$server
->
executeBulkWrite
(
$this
->
databaseName
.
'.'
.
$this
->
collectionName
,
$bulk
,
$this
->
createExecuteOptions
());
return
new
DeleteResult
(
$writeResult
);
}
public
function
getCommandDocument
()
{
return
[
'delete'
=>
$this
->
collectionName
,
'deletes'
=>
[[
'q'
=>
$this
->
filter
]
+
$this
->
createDeleteOptions
()]];
}
/**
* Create options for the delete command.
*
* Note that these options are different from the bulk write options, which
* are created in createOptions().
*
* @return array
*/
private
function
createDeleteOptions
()
{
$deleteOptions
=
[
'limit'
=>
$this
->
limit
];
$deleteOptions
=
[
'limit'
=>
$this
->
limit
];
if
(
isset
(
$this
->
options
[
'collation'
]))
{
if
(
isset
(
$this
->
options
[
'collation'
]))
{
$deleteOptions
[
'collation'
]
=
(
object
)
$this
->
options
[
'collation'
];
$deleteOptions
[
'collation'
]
=
(
object
)
$this
->
options
[
'collation'
];
}
}
$bulk
=
new
Bulk
();
return
$deleteOptions
;
$bulk
->
delete
(
$this
->
filter
,
$deleteOptions
);
$writeResult
=
$server
->
executeBulkWrite
(
$this
->
databaseName
.
'.'
.
$this
->
collectionName
,
$bulk
,
$this
->
createOptions
());
return
new
DeleteResult
(
$writeResult
);
}
}
/**
/**
...
@@ -137,7 +157,7 @@ class Delete implements Executable
...
@@ -137,7 +157,7 @@ class Delete implements Executable
* @see http://php.net/manual/en/mongodb-driver-server.executebulkwrite.php
* @see http://php.net/manual/en/mongodb-driver-server.executebulkwrite.php
* @return array
* @return array
*/
*/
private
function
createOptions
()
private
function
create
Execute
Options
()
{
{
$options
=
[];
$options
=
[];
...
...
src/Operation/Update.php
View file @
ad4f3e6c
...
@@ -35,7 +35,7 @@ use MongoDB\Exception\UnsupportedException;
...
@@ -35,7 +35,7 @@ use MongoDB\Exception\UnsupportedException;
* @internal
* @internal
* @see http://docs.mongodb.org/manual/reference/command/update/
* @see http://docs.mongodb.org/manual/reference/command/update/
*/
*/
class
Update
implements
Executable
class
Update
implements
Executable
,
Explainable
{
{
private
static
$wireVersionForArrayFilters
=
6
;
private
static
$wireVersionForArrayFilters
=
6
;
private
static
$wireVersionForCollation
=
5
;
private
static
$wireVersionForCollation
=
5
;
...
@@ -167,19 +167,6 @@ class Update implements Executable
...
@@ -167,19 +167,6 @@ class Update implements Executable
throw
UnsupportedException
::
collationNotSupported
();
throw
UnsupportedException
::
collationNotSupported
();
}
}
$updateOptions
=
[
'multi'
=>
$this
->
options
[
'multi'
],
'upsert'
=>
$this
->
options
[
'upsert'
],
];
if
(
isset
(
$this
->
options
[
'arrayFilters'
]))
{
$updateOptions
[
'arrayFilters'
]
=
$this
->
options
[
'arrayFilters'
];
}
if
(
isset
(
$this
->
options
[
'collation'
]))
{
$updateOptions
[
'collation'
]
=
(
object
)
$this
->
options
[
'collation'
];
}
$bulkOptions
=
[];
$bulkOptions
=
[];
if
(
isset
(
$this
->
options
[
'bypassDocumentValidation'
])
&&
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForDocumentLevelValidation
))
{
if
(
isset
(
$this
->
options
[
'bypassDocumentValidation'
])
&&
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForDocumentLevelValidation
))
{
...
@@ -187,20 +174,25 @@ class Update implements Executable
...
@@ -187,20 +174,25 @@ class Update implements Executable
}
}
$bulk
=
new
Bulk
(
$bulkOptions
);
$bulk
=
new
Bulk
(
$bulkOptions
);
$bulk
->
update
(
$this
->
filter
,
$this
->
update
,
$
updateOptions
);
$bulk
->
update
(
$this
->
filter
,
$this
->
update
,
$
this
->
createUpdateOptions
()
);
$writeResult
=
$server
->
executeBulkWrite
(
$this
->
databaseName
.
'.'
.
$this
->
collectionName
,
$bulk
,
$this
->
createOptions
());
$writeResult
=
$server
->
executeBulkWrite
(
$this
->
databaseName
.
'.'
.
$this
->
collectionName
,
$bulk
,
$this
->
create
Execute
Options
());
return
new
UpdateResult
(
$writeResult
);
return
new
UpdateResult
(
$writeResult
);
}
}
public
function
getCommandDocument
()
{
return
[
'update'
=>
$this
->
collectionName
,
'updates'
=>
[[
'q'
=>
$this
->
filter
]
+
[
'u'
=>
$this
->
update
]
+
$this
->
createUpdateOptions
()]];
}
/**
/**
* Create options for executing the bulk write.
* Create options for executing the bulk write.
*
*
* @see http://php.net/manual/en/mongodb-driver-server.executebulkwrite.php
* @see http://php.net/manual/en/mongodb-driver-server.executebulkwrite.php
* @return array
* @return array
*/
*/
private
function
createOptions
()
private
function
create
Execute
Options
()
{
{
$options
=
[];
$options
=
[];
...
@@ -214,4 +206,30 @@ class Update implements Executable
...
@@ -214,4 +206,30 @@ class Update implements Executable
return
$options
;
return
$options
;
}
}
/**
* Create options for the update command.
*
* Note that these options are different from the bulk write options, which
* are created in createOptions().
*
* @return array
*/
private
function
createUpdateOptions
()
{
$updateOptions
=
[
'multi'
=>
$this
->
options
[
'multi'
],
'upsert'
=>
$this
->
options
[
'upsert'
],
];
if
(
isset
(
$this
->
options
[
'arrayFilters'
]))
{
$updateOptions
[
'arrayFilters'
]
=
$this
->
options
[
'arrayFilters'
];
}
if
(
isset
(
$this
->
options
[
'collation'
]))
{
$updateOptions
[
'collation'
]
=
(
object
)
$this
->
options
[
'collation'
];
}
return
$updateOptions
;
}
}
}
tests/Operation/ExplainFunctionalTest.php
View file @
ad4f3e6c
...
@@ -3,14 +3,17 @@
...
@@ -3,14 +3,17 @@
namespace
MongoDB\Tests\Operation
;
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Driver\BulkWrite
;
use
MongoDB\Driver\BulkWrite
;
use
MongoDB\Collection
;
use
MongoDB\Operation\Count
;
use
MongoDB\Operation\Count
;
use
MongoDB\Operation\CreateCollection
;
use
MongoDB\Operation\CreateCollection
;
use
MongoDB\Operation\Distinct
;
use
MongoDB\Operation\Distinct
;
use
MongoDB\Operation\Delete
;
use
MongoDB\Operation\Explain
;
use
MongoDB\Operation\Explain
;
use
MongoDB\Operation\Find
;
use
MongoDB\Operation\Find
;
use
MongoDB\Operation\FindAndModify
;
use
MongoDB\Operation\FindAndModify
;
use
MongoDB\Operation\FindOne
;
use
MongoDB\Operation\FindOne
;
use
MongoDB\Operation\InsertMany
;
use
MongoDB\Operation\InsertMany
;
use
MongoDB\Operation\Update
;
class
ExplainFunctionalTest
extends
FunctionalTestCase
class
ExplainFunctionalTest
extends
FunctionalTestCase
{
{
...
@@ -18,7 +21,7 @@ class ExplainFunctionalTest extends FunctionalTestCase
...
@@ -18,7 +21,7 @@ class ExplainFunctionalTest extends FunctionalTestCase
{
{
parent
::
setUp
();
parent
::
setUp
();
if
(
version_compare
(
$this
->
getServerVersion
(),
'3.0.0'
,
'<'
))
{
if
(
version_compare
(
$this
->
getServerVersion
(),
'3.0.0'
,
'<'
))
{
$this
->
markTestSkipped
(
'Explain is not supported'
);
$this
->
markTestSkipped
(
'Explain
command
is not supported'
);
}
}
}
}
...
@@ -36,6 +39,26 @@ class ExplainFunctionalTest extends FunctionalTestCase
...
@@ -36,6 +39,26 @@ class ExplainFunctionalTest extends FunctionalTestCase
$insertMany
->
execute
(
$this
->
getPrimaryServer
());
$insertMany
->
execute
(
$this
->
getPrimaryServer
());
$operation
=
new
Count
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'x'
=>
[
'$gte'
=>
1
]],
[]);
$operation
=
new
Count
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'x'
=>
[
'$gte'
=>
1
]],
[]);
$explainOperation
=
new
Explain
(
$this
->
getDatabaseName
(),
$operation
,
[
'verbosity'
=>
$verbosity
,
'typeMap'
=>
[
'root'
=>
'array'
,
'document'
=>
'array'
]]);
$result
=
$explainOperation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertExplainResult
(
$result
,
$executionStatsExpected
,
$allPlansExecutionExpected
);
}
/**
* @dataProvider provideVerbosityInformation
*/
public
function
testDelete
(
$verbosity
,
$executionStatsExpected
,
$allPlansExecutionExpected
)
{
$this
->
collection
=
new
Collection
(
$this
->
manager
,
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
$this
->
createFixtures
(
3
);
$filter
=
[
'_id'
=>
1
];
$operation
=
new
Delete
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$filter
,
1
);
$explainOperation
=
new
Explain
(
$this
->
getDatabaseName
(),
$operation
,
[
'verbosity'
=>
$verbosity
,
'typeMap'
=>
[
'root'
=>
'array'
,
'document'
=>
'array'
]]);
$explainOperation
=
new
Explain
(
$this
->
getDatabaseName
(),
$operation
,
[
'verbosity'
=>
$verbosity
,
'typeMap'
=>
[
'root'
=>
'array'
,
'document'
=>
'array'
]]);
$result
=
$explainOperation
->
execute
(
$this
->
getPrimaryServer
());
$result
=
$explainOperation
->
execute
(
$this
->
getPrimaryServer
());
...
@@ -48,7 +71,7 @@ class ExplainFunctionalTest extends FunctionalTestCase
...
@@ -48,7 +71,7 @@ class ExplainFunctionalTest extends FunctionalTestCase
public
function
testDistinct
(
$verbosity
,
$executionStatsExpected
,
$allPlansExecutionExpected
)
public
function
testDistinct
(
$verbosity
,
$executionStatsExpected
,
$allPlansExecutionExpected
)
{
{
if
(
version_compare
(
$this
->
getServerVersion
(),
'3.2.0'
,
'<'
))
{
if
(
version_compare
(
$this
->
getServerVersion
(),
'3.2.0'
,
'<'
))
{
$this
->
markTestSkipped
(
'
Distinct is not supported on servers with version <
3.2'
);
$this
->
markTestSkipped
(
'
Explaining distinct command requires server version >=
3.2'
);
}
}
$operation
=
new
Distinct
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
'x'
,
[]);
$operation
=
new
Distinct
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
'x'
,
[]);
...
@@ -65,7 +88,7 @@ class ExplainFunctionalTest extends FunctionalTestCase
...
@@ -65,7 +88,7 @@ class ExplainFunctionalTest extends FunctionalTestCase
public
function
testFindAndModify
(
$verbosity
,
$executionStatsExpected
,
$allPlansExecutionExpected
)
public
function
testFindAndModify
(
$verbosity
,
$executionStatsExpected
,
$allPlansExecutionExpected
)
{
{
if
(
version_compare
(
$this
->
getServerVersion
(),
'3.2.0'
,
'<'
))
{
if
(
version_compare
(
$this
->
getServerVersion
(),
'3.2.0'
,
'<'
))
{
$this
->
markTestSkipped
(
'
FindAndModify is not supported on servers with version <
3.2'
);
$this
->
markTestSkipped
(
'
Explaining findAndModify command requires server version >=
3.2'
);
}
}
$operation
=
new
FindAndModify
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'remove'
=>
true
]);
$operation
=
new
FindAndModify
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'remove'
=>
true
]);
...
@@ -148,6 +171,24 @@ class ExplainFunctionalTest extends FunctionalTestCase
...
@@ -148,6 +171,24 @@ class ExplainFunctionalTest extends FunctionalTestCase
$this
->
assertExplainResult
(
$result
,
$executionStatsExpected
,
$allPlansExecutionExpected
);
$this
->
assertExplainResult
(
$result
,
$executionStatsExpected
,
$allPlansExecutionExpected
);
}
}
/**
* @dataProvider provideVerbosityInformation
*/
public
function
testUpdate
(
$verbosity
,
$executionStatsExpected
,
$allPlansExecutionExpected
)
{
$this
->
createFixtures
(
3
);
$filter
=
[
'_id'
=>
[
'$gt'
=>
1
]];
$update
=
[
'$inc'
=>
[
'x'
=>
1
]];
$operation
=
new
Update
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$filter
,
$update
);
$explainOperation
=
new
Explain
(
$this
->
getDatabaseName
(),
$operation
,
[
'verbosity'
=>
$verbosity
,
'typeMap'
=>
[
'root'
=>
'array'
,
'document'
=>
'array'
]]);
$result
=
$explainOperation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertExplainResult
(
$result
,
$executionStatsExpected
,
$allPlansExecutionExpected
);
}
public
function
provideVerbosityInformation
()
public
function
provideVerbosityInformation
()
{
{
return
[
return
[
...
@@ -184,7 +225,7 @@ class ExplainFunctionalTest extends FunctionalTestCase
...
@@ -184,7 +225,7 @@ class ExplainFunctionalTest extends FunctionalTestCase
for
(
$i
=
1
;
$i
<=
$n
;
$i
++
)
{
for
(
$i
=
1
;
$i
<=
$n
;
$i
++
)
{
$bulkWrite
->
insert
([
$bulkWrite
->
insert
([
'_id'
=>
$i
,
'_id'
=>
$i
,
'x'
=>
(
object
)
[
'foo'
=>
'bar'
]
,
'x'
=>
(
integer
)
(
$i
.
$i
)
,
]);
]);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment