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
f6b1b49a
Commit
f6b1b49a
authored
Feb 23, 2018
by
Katherine Walker
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #497
parents
966511c8
1eebfe0d
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
19 changed files
with
784 additions
and
60 deletions
+784
-60
UnsupportedException.php
src/Exception/UnsupportedException.php
+10
-0
Count.php
src/Operation/Count.php
+11
-6
Delete.php
src/Operation/Delete.php
+32
-8
DeleteMany.php
src/Operation/DeleteMany.php
+6
-1
DeleteOne.php
src/Operation/DeleteOne.php
+6
-1
Distinct.php
src/Operation/Distinct.php
+11
-6
Explain.php
src/Operation/Explain.php
+113
-0
Explainable.php
src/Operation/Explainable.php
+31
-0
Find.php
src/Operation/Find.php
+56
-8
FindAndModify.php
src/Operation/FindAndModify.php
+11
-7
FindOne.php
src/Operation/FindOne.php
+6
-1
FindOneAndDelete.php
src/Operation/FindOneAndDelete.php
+6
-1
FindOneAndReplace.php
src/Operation/FindOneAndReplace.php
+6
-1
FindOneAndUpdate.php
src/Operation/FindOneAndUpdate.php
+6
-1
Update.php
src/Operation/Update.php
+45
-17
UpdateMany.php
src/Operation/UpdateMany.php
+6
-1
UpdateOne.php
src/Operation/UpdateOne.php
+6
-1
ExplainFunctionalTest.php
tests/Operation/ExplainFunctionalTest.php
+381
-0
ExplainTest.php
tests/Operation/ExplainTest.php
+35
-0
No files found.
src/Exception/UnsupportedException.php
View file @
f6b1b49a
...
@@ -39,6 +39,16 @@ class UnsupportedException extends RuntimeException
...
@@ -39,6 +39,16 @@ class UnsupportedException extends RuntimeException
return
new
static
(
'Collations are not supported by the server executing this operation'
);
return
new
static
(
'Collations are not supported by the server executing this operation'
);
}
}
/**
* Thrown when explain is not supported by a server.
*
* @return self
*/
public
static
function
explainNotSupported
()
{
return
new
static
(
'Explain is not supported by the server executing this operation'
);
}
/**
/**
* Thrown when a command's readConcern option is not supported by a server.
* Thrown when a command's readConcern option is not supported by a server.
*
*
...
...
src/Operation/Count.php
View file @
f6b1b49a
...
@@ -34,7 +34,7 @@ use MongoDB\Exception\UnsupportedException;
...
@@ -34,7 +34,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::count()
* @see \MongoDB\Collection::count()
* @see http://docs.mongodb.org/manual/reference/command/count/
* @see http://docs.mongodb.org/manual/reference/command/count/
*/
*/
class
Count
implements
Executable
class
Count
implements
Executable
,
Explainable
{
{
private
static
$wireVersionForCollation
=
5
;
private
static
$wireVersionForCollation
=
5
;
private
static
$wireVersionForReadConcern
=
4
;
private
static
$wireVersionForReadConcern
=
4
;
...
@@ -151,7 +151,7 @@ class Count implements Executable
...
@@ -151,7 +151,7 @@ class Count implements Executable
throw
UnsupportedException
::
readConcernNotSupported
();
throw
UnsupportedException
::
readConcernNotSupported
();
}
}
$cursor
=
$server
->
executeReadCommand
(
$this
->
databaseName
,
$this
->
createCommand
(
),
$this
->
createOptions
());
$cursor
=
$server
->
executeReadCommand
(
$this
->
databaseName
,
new
Command
(
$this
->
createCommandDocument
()
),
$this
->
createOptions
());
$result
=
current
(
$cursor
->
toArray
());
$result
=
current
(
$cursor
->
toArray
());
// Older server versions may return a float
// Older server versions may return a float
...
@@ -162,12 +162,17 @@ class Count implements Executable
...
@@ -162,12 +162,17 @@ class Count implements Executable
return
(
integer
)
$result
->
n
;
return
(
integer
)
$result
->
n
;
}
}
public
function
getCommandDocument
(
Server
$server
)
{
return
$this
->
createCommandDocument
();
}
/**
/**
* Create the count command.
* Create the count command
document
.
*
*
* @return
Command
* @return
array
*/
*/
private
function
createCommand
()
private
function
createCommand
Document
()
{
{
$cmd
=
[
'count'
=>
$this
->
collectionName
];
$cmd
=
[
'count'
=>
$this
->
collectionName
];
...
@@ -189,7 +194,7 @@ class Count implements Executable
...
@@ -189,7 +194,7 @@ class Count implements Executable
}
}
}
}
return
new
Command
(
$cmd
)
;
return
$cmd
;
}
}
/**
/**
...
...
src/Operation/Delete.php
View file @
f6b1b49a
...
@@ -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,42 @@ class Delete implements Executable
...
@@ -117,18 +117,42 @@ class Delete implements Executable
throw
UnsupportedException
::
collationNotSupported
();
throw
UnsupportedException
::
collationNotSupported
();
}
}
$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
(
Server
$server
)
{
$cmd
=
[
'delete'
=>
$this
->
collectionName
,
'deletes'
=>
[[
'q'
=>
$this
->
filter
]
+
$this
->
createDeleteOptions
()]];
if
(
isset
(
$this
->
options
[
'writeConcern'
]))
{
$cmd
[
'writeConcern'
]
=
$this
->
options
[
'writeConcern'
];
}
return
$cmd
;
}
/**
* Create options for the delete command.
*
* Note that these options are different from the bulk write options, which
* are created in createExecuteOptions().
*
* @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 +161,7 @@ class Delete implements Executable
...
@@ -137,7 +161,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/DeleteMany.php
View file @
f6b1b49a
...
@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
...
@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::deleteOne()
* @see \MongoDB\Collection::deleteOne()
* @see http://docs.mongodb.org/manual/reference/command/delete/
* @see http://docs.mongodb.org/manual/reference/command/delete/
*/
*/
class
DeleteMany
implements
Executable
class
DeleteMany
implements
Executable
,
Explainable
{
{
private
$delete
;
private
$delete
;
...
@@ -74,4 +74,9 @@ class DeleteMany implements Executable
...
@@ -74,4 +74,9 @@ class DeleteMany implements Executable
{
{
return
$this
->
delete
->
execute
(
$server
);
return
$this
->
delete
->
execute
(
$server
);
}
}
public
function
getCommandDocument
(
Server
$server
)
{
return
$this
->
delete
->
getCommandDocument
(
$server
);
}
}
}
src/Operation/DeleteOne.php
View file @
f6b1b49a
...
@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
...
@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::deleteOne()
* @see \MongoDB\Collection::deleteOne()
* @see http://docs.mongodb.org/manual/reference/command/delete/
* @see http://docs.mongodb.org/manual/reference/command/delete/
*/
*/
class
DeleteOne
implements
Executable
class
DeleteOne
implements
Executable
,
Explainable
{
{
private
$delete
;
private
$delete
;
...
@@ -74,4 +74,9 @@ class DeleteOne implements Executable
...
@@ -74,4 +74,9 @@ class DeleteOne implements Executable
{
{
return
$this
->
delete
->
execute
(
$server
);
return
$this
->
delete
->
execute
(
$server
);
}
}
public
function
getCommandDocument
(
Server
$server
)
{
return
$this
->
delete
->
getCommandDocument
(
$server
);
}
}
}
src/Operation/Distinct.php
View file @
f6b1b49a
...
@@ -34,7 +34,7 @@ use MongoDB\Exception\UnsupportedException;
...
@@ -34,7 +34,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::distinct()
* @see \MongoDB\Collection::distinct()
* @see http://docs.mongodb.org/manual/reference/command/distinct/
* @see http://docs.mongodb.org/manual/reference/command/distinct/
*/
*/
class
Distinct
implements
Executable
class
Distinct
implements
Executable
,
Explainable
{
{
private
static
$wireVersionForCollation
=
5
;
private
static
$wireVersionForCollation
=
5
;
private
static
$wireVersionForReadConcern
=
4
;
private
static
$wireVersionForReadConcern
=
4
;
...
@@ -133,7 +133,7 @@ class Distinct implements Executable
...
@@ -133,7 +133,7 @@ class Distinct implements Executable
throw
UnsupportedException
::
readConcernNotSupported
();
throw
UnsupportedException
::
readConcernNotSupported
();
}
}
$cursor
=
$server
->
executeReadCommand
(
$this
->
databaseName
,
$this
->
createCommand
(
),
$this
->
createOptions
());
$cursor
=
$server
->
executeReadCommand
(
$this
->
databaseName
,
new
Command
(
$this
->
createCommandDocument
()
),
$this
->
createOptions
());
$result
=
current
(
$cursor
->
toArray
());
$result
=
current
(
$cursor
->
toArray
());
if
(
!
isset
(
$result
->
values
)
||
!
is_array
(
$result
->
values
))
{
if
(
!
isset
(
$result
->
values
)
||
!
is_array
(
$result
->
values
))
{
...
@@ -143,12 +143,17 @@ class Distinct implements Executable
...
@@ -143,12 +143,17 @@ class Distinct implements Executable
return
$result
->
values
;
return
$result
->
values
;
}
}
public
function
getCommandDocument
(
Server
$server
)
{
return
$this
->
createCommandDocument
();
}
/**
/**
* Create the distinct command.
* Create the distinct command
document
.
*
*
* @return
Command
* @return
array
*/
*/
private
function
createCommand
()
private
function
createCommand
Document
()
{
{
$cmd
=
[
$cmd
=
[
'distinct'
=>
$this
->
collectionName
,
'distinct'
=>
$this
->
collectionName
,
...
@@ -167,7 +172,7 @@ class Distinct implements Executable
...
@@ -167,7 +172,7 @@ class Distinct implements Executable
$cmd
[
'maxTimeMS'
]
=
$this
->
options
[
'maxTimeMS'
];
$cmd
[
'maxTimeMS'
]
=
$this
->
options
[
'maxTimeMS'
];
}
}
return
new
Command
(
$cmd
)
;
return
$cmd
;
}
}
/**
/**
...
...
src/Operation/Explain.php
0 → 100644
View file @
f6b1b49a
<?php
/*
* Copyright 2018 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace
MongoDB\Operation
;
use
MongoDB\Driver\Command
;
use
MongoDB\Driver\Server
;
use
MongoDB\Exception\UnsupportedException
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Model\BSONDocument
;
/**
* Operation for the explain command.
*
* @api
* @see \MongoDB\Collection::explain()
* @see http://docs.mongodb.org/manual/reference/command/explain/
*/
class
Explain
implements
Executable
{
const
VERBOSITY_ALL_PLANS
=
'allPlansExecution'
;
const
VERBOSITY_EXEC_STATS
=
'executionStats'
;
const
VERBOSITY_QUERY
=
'queryPlanner'
;
private
static
$wireVersionForExplain
=
2
;
private
static
$wireVersionForDistinct
=
4
;
private
static
$wireVersionForFindAndModify
=
4
;
private
$databaseName
;
private
$explainable
;
private
$options
;
/**
* Constructs an explain command for explainable operations.
*
* Supported options:
*
* * verbosity (string): The mode in which the explain command will be run.
*
* * typeMap (array): Type map for BSON deserialization. This will be used
* used for the returned command result document.
*
* @param string $databaseName Database name
* @param Explainable $explainable Operation to explain
* @param array $options Command options
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public
function
__construct
(
$databaseName
,
Explainable
$explainable
,
array
$options
=
[])
{
if
(
isset
(
$options
[
'verbosity'
])
&&
!
is_string
(
$options
[
'verbosity'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"verbosity" option'
,
$options
[
'verbosity'
],
'string'
);
}
if
(
isset
(
$options
[
'typeMap'
])
&&
!
is_array
(
$options
[
'typeMap'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"typeMap" option'
,
$options
[
'typeMap'
],
'array'
);
}
$this
->
databaseName
=
$databaseName
;
$this
->
explainable
=
$explainable
;
$this
->
options
=
$options
;
}
public
function
execute
(
Server
$server
)
{
if
(
!
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForExplain
))
{
throw
UnsupportedException
::
explainNotSupported
();
}
if
(
$this
->
explainable
instanceof
Distinct
&&
!
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForDistinct
))
{
throw
UnsupportedException
::
explainNotSupported
();
}
if
(
$this
->
isFindAndModify
(
$this
->
explainable
)
&&
!
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForFindAndModify
))
{
throw
UnsupportedException
::
explainNotSupported
();
}
$cmd
=
[
'explain'
=>
$this
->
explainable
->
getCommandDocument
(
$server
)];
if
(
isset
(
$this
->
options
[
'verbosity'
]))
{
$cmd
[
'verbosity'
]
=
$this
->
options
[
'verbosity'
];
}
$cursor
=
$server
->
executeCommand
(
$this
->
databaseName
,
new
Command
(
$cmd
));
if
(
isset
(
$this
->
options
[
'typeMap'
]))
{
$cursor
->
setTypeMap
(
$this
->
options
[
'typeMap'
]);
}
return
current
(
$cursor
->
toArray
());
}
private
function
isFindAndModify
(
$explainable
)
{
if
(
$explainable
instanceof
FindAndModify
||
$explainable
instanceof
FindOneAndDelete
||
$explainable
instanceof
FindOneAndReplace
||
$explainable
instanceof
FindOneAndUpdate
)
{
return
true
;
}
return
false
;
}
}
src/Operation/Explainable.php
0 → 100644
View file @
f6b1b49a
<?php
/*
* Copyright 2018 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace
MongoDB\Operation
;
use
MongoDB\Driver\Server
;
/**
* Explainable interface for explainable operations (count, distinct, find,
* findAndModify, delete, and update).
*
* @internal
*/
interface
Explainable
extends
Executable
{
function
getCommandDocument
(
Server
$server
);
}
src/Operation/Find.php
View file @
f6b1b49a
...
@@ -26,7 +26,7 @@ use MongoDB\Driver\Session;
...
@@ -26,7 +26,7 @@ use MongoDB\Driver\Session;
use
MongoDB\Driver\Exception\RuntimeException
as
DriverRuntimeException
;
use
MongoDB\Driver\Exception\RuntimeException
as
DriverRuntimeException
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\UnsupportedException
;
use
MongoDB\Exception\UnsupportedException
;
use
MongoDB\Model\BSONDocument
;
/**
/**
* Operation for the find command.
* Operation for the find command.
*
*
...
@@ -35,7 +35,7 @@ use MongoDB\Exception\UnsupportedException;
...
@@ -35,7 +35,7 @@ use MongoDB\Exception\UnsupportedException;
* @see http://docs.mongodb.org/manual/tutorial/query-documents/
* @see http://docs.mongodb.org/manual/tutorial/query-documents/
* @see http://docs.mongodb.org/manual/reference/operator/query-modifier/
* @see http://docs.mongodb.org/manual/reference/operator/query-modifier/
*/
*/
class
Find
implements
Executable
class
Find
implements
Executable
,
Explainable
{
{
const
NON_TAILABLE
=
1
;
const
NON_TAILABLE
=
1
;
const
TAILABLE
=
2
;
const
TAILABLE
=
2
;
...
@@ -284,7 +284,7 @@ class Find implements Executable
...
@@ -284,7 +284,7 @@ class Find implements Executable
throw
UnsupportedException
::
readConcernNotSupported
();
throw
UnsupportedException
::
readConcernNotSupported
();
}
}
$cursor
=
$server
->
executeQuery
(
$this
->
databaseName
.
'.'
.
$this
->
collectionName
,
$this
->
createQuery
(),
$this
->
crea
teOptions
());
$cursor
=
$server
->
executeQuery
(
$this
->
databaseName
.
'.'
.
$this
->
collectionName
,
new
Query
(
$this
->
filter
,
$this
->
createQueryOptions
()),
$this
->
createExecu
teOptions
());
if
(
isset
(
$this
->
options
[
'typeMap'
]))
{
if
(
isset
(
$this
->
options
[
'typeMap'
]))
{
$cursor
->
setTypeMap
(
$this
->
options
[
'typeMap'
]);
$cursor
->
setTypeMap
(
$this
->
options
[
'typeMap'
]);
...
@@ -293,13 +293,58 @@ class Find implements Executable
...
@@ -293,13 +293,58 @@ class Find implements Executable
return
$cursor
;
return
$cursor
;
}
}
public
function
getCommandDocument
(
Server
$server
)
{
return
$this
->
createCommandDocument
();
}
/**
* Construct a command document for Find
*/
private
function
createCommandDocument
()
{
$cmd
=
[
'find'
=>
$this
->
collectionName
,
'filter'
=>
(
object
)
$this
->
filter
];
$options
=
$this
->
createQueryOptions
();
if
(
empty
(
$options
))
{
return
$cmd
;
}
// maxAwaitTimeMS is a Query level option so should not be considered here
unset
(
$options
[
'maxAwaitTimeMS'
]);
$modifierFallback
=
[
[
'allowPartialResults'
,
'partial'
],
[
'comment'
,
'$comment'
],
[
'hint'
,
'$hint'
],
[
'maxScan'
,
'$maxScan'
],
[
'max'
,
'$max'
],
[
'maxTimeMS'
,
'$maxTimeMS'
],
[
'min'
,
'$min'
],
[
'returnKey'
,
'$returnKey'
],
[
'showRecordId'
,
'$showDiskLoc'
],
[
'sort'
,
'$orderby'
],
[
'snapshot'
,
'$snapshot'
],
];
foreach
(
$modifierFallback
as
$modifier
)
{
if
(
!
isset
(
$options
[
$modifier
[
0
]])
&&
isset
(
$options
[
'modifiers'
][
$modifier
[
1
]]))
{
$options
[
$modifier
[
0
]]
=
$options
[
'modifiers'
][
$modifier
[
1
]];
}
}
unset
(
$options
[
'modifiers'
]);
return
$cmd
+
$options
;
}
/**
/**
* Create options for executing the command.
* Create options for executing the command.
*
*
* @see http://php.net/manual/en/mongodb-driver-server.executequery.php
* @see http://php.net/manual/en/mongodb-driver-server.executequery.php
* @return array
* @return array
*/
*/
private
function
createOptions
()
private
function
create
Execute
Options
()
{
{
$options
=
[];
$options
=
[];
...
@@ -315,11 +360,14 @@ class Find implements Executable
...
@@ -315,11 +360,14 @@ class Find implements Executable
}
}
/**
/**
* Create the find query.
* Create
options for
the find query.
*
*
* @return Query
* Note that these are separate from the options for executing the command,
* which are created in createExecuteOptions().
*
* @return array
*/
*/
private
function
createQuery
()
private
function
createQuery
Options
()
{
{
$options
=
[];
$options
=
[];
...
@@ -351,6 +399,6 @@ class Find implements Executable
...
@@ -351,6 +399,6 @@ class Find implements Executable
$options
[
'modifiers'
]
=
$modifiers
;
$options
[
'modifiers'
]
=
$modifiers
;
}
}
return
new
Query
(
$this
->
filter
,
$options
)
;
return
$options
;
}
}
}
}
src/Operation/FindAndModify.php
View file @
f6b1b49a
...
@@ -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/findAndModify/
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
*/
*/
class
FindAndModify
implements
Executable
class
FindAndModify
implements
Executable
,
Explainable
{
{
private
static
$wireVersionForArrayFilters
=
6
;
private
static
$wireVersionForArrayFilters
=
6
;
private
static
$wireVersionForCollation
=
5
;
private
static
$wireVersionForCollation
=
5
;
...
@@ -210,7 +210,7 @@ class FindAndModify implements Executable
...
@@ -210,7 +210,7 @@ class FindAndModify implements Executable
throw
UnsupportedException
::
writeConcernNotSupported
();
throw
UnsupportedException
::
writeConcernNotSupported
();
}
}
$cursor
=
$server
->
executeReadWriteCommand
(
$this
->
databaseName
,
$this
->
createCommand
(
$server
),
$this
->
createOptions
());
$cursor
=
$server
->
executeReadWriteCommand
(
$this
->
databaseName
,
new
Command
(
$this
->
createCommandDocument
(
$server
)
),
$this
->
createOptions
());
$result
=
current
(
$cursor
->
toArray
());
$result
=
current
(
$cursor
->
toArray
());
if
(
!
isset
(
$result
->
value
))
{
if
(
!
isset
(
$result
->
value
))
{
...
@@ -239,13 +239,17 @@ class FindAndModify implements Executable
...
@@ -239,13 +239,17 @@ class FindAndModify implements Executable
return
$result
->
value
;
return
$result
->
value
;
}
}
public
function
getCommandDocument
(
Server
$server
)
{
return
$this
->
createCommandDocument
(
$server
);
}
/**
/**
* Create the findAndModify command.
* Create the findAndModify command
document
.
*
*
* @param Server $server
* @return array
* @return Command
*/
*/
private
function
createCommand
(
Server
$server
)
private
function
createCommand
Document
(
Server
$server
)
{
{
$cmd
=
[
'findAndModify'
=>
$this
->
collectionName
];
$cmd
=
[
'findAndModify'
=>
$this
->
collectionName
];
...
@@ -274,7 +278,7 @@ class FindAndModify implements Executable
...
@@ -274,7 +278,7 @@ class FindAndModify implements Executable
$cmd
[
'bypassDocumentValidation'
]
=
$this
->
options
[
'bypassDocumentValidation'
];
$cmd
[
'bypassDocumentValidation'
]
=
$this
->
options
[
'bypassDocumentValidation'
];
}
}
return
new
Command
(
$cmd
)
;
return
$cmd
;
}
}
/**
/**
...
...
src/Operation/FindOne.php
View file @
f6b1b49a
...
@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
...
@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
* @see http://docs.mongodb.org/manual/tutorial/query-documents/
* @see http://docs.mongodb.org/manual/tutorial/query-documents/
* @see http://docs.mongodb.org/manual/reference/operator/query-modifier/
* @see http://docs.mongodb.org/manual/reference/operator/query-modifier/
*/
*/
class
FindOne
implements
Executable
class
FindOne
implements
Executable
,
Explainable
{
{
private
$find
;
private
$find
;
...
@@ -126,4 +126,9 @@ class FindOne implements Executable
...
@@ -126,4 +126,9 @@ class FindOne implements Executable
return
(
$document
===
false
)
?
null
:
$document
;
return
(
$document
===
false
)
?
null
:
$document
;
}
}
public
function
getCommandDocument
(
Server
$server
)
{
return
$this
->
find
->
getCommandDocument
(
$server
);
}
}
}
src/Operation/FindOneAndDelete.php
View file @
f6b1b49a
...
@@ -29,7 +29,7 @@ use MongoDB\Exception\UnsupportedException;
...
@@ -29,7 +29,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::findOneAndDelete()
* @see \MongoDB\Collection::findOneAndDelete()
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
*/
*/
class
FindOneAndDelete
implements
Executable
class
FindOneAndDelete
implements
Executable
,
Explainable
{
{
private
$findAndModify
;
private
$findAndModify
;
...
@@ -105,4 +105,9 @@ class FindOneAndDelete implements Executable
...
@@ -105,4 +105,9 @@ class FindOneAndDelete implements Executable
{
{
return
$this
->
findAndModify
->
execute
(
$server
);
return
$this
->
findAndModify
->
execute
(
$server
);
}
}
public
function
getCommandDocument
(
Server
$server
)
{
return
$this
->
findAndModify
->
getCommandDocument
(
$server
);
}
}
}
src/Operation/FindOneAndReplace.php
View file @
f6b1b49a
...
@@ -29,7 +29,7 @@ use MongoDB\Exception\UnsupportedException;
...
@@ -29,7 +29,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::findOneAndReplace()
* @see \MongoDB\Collection::findOneAndReplace()
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
*/
*/
class
FindOneAndReplace
implements
Executable
class
FindOneAndReplace
implements
Executable
,
Explainable
{
{
const
RETURN_DOCUMENT_BEFORE
=
1
;
const
RETURN_DOCUMENT_BEFORE
=
1
;
const
RETURN_DOCUMENT_AFTER
=
2
;
const
RETURN_DOCUMENT_AFTER
=
2
;
...
@@ -148,4 +148,9 @@ class FindOneAndReplace implements Executable
...
@@ -148,4 +148,9 @@ class FindOneAndReplace implements Executable
{
{
return
$this
->
findAndModify
->
execute
(
$server
);
return
$this
->
findAndModify
->
execute
(
$server
);
}
}
public
function
getCommandDocument
(
Server
$server
)
{
return
$this
->
findAndModify
->
getCommandDocument
(
$server
);
}
}
}
src/Operation/FindOneAndUpdate.php
View file @
f6b1b49a
...
@@ -29,7 +29,7 @@ use MongoDB\Exception\UnsupportedException;
...
@@ -29,7 +29,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::findOneAndUpdate()
* @see \MongoDB\Collection::findOneAndUpdate()
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
*/
*/
class
FindOneAndUpdate
implements
Executable
class
FindOneAndUpdate
implements
Executable
,
Explainable
{
{
const
RETURN_DOCUMENT_BEFORE
=
1
;
const
RETURN_DOCUMENT_BEFORE
=
1
;
const
RETURN_DOCUMENT_AFTER
=
2
;
const
RETURN_DOCUMENT_AFTER
=
2
;
...
@@ -151,4 +151,9 @@ class FindOneAndUpdate implements Executable
...
@@ -151,4 +151,9 @@ class FindOneAndUpdate implements Executable
{
{
return
$this
->
findAndModify
->
execute
(
$server
);
return
$this
->
findAndModify
->
execute
(
$server
);
}
}
public
function
getCommandDocument
(
Server
$server
)
{
return
$this
->
findAndModify
->
getCommandDocument
(
$server
);
}
}
}
src/Operation/Update.php
View file @
f6b1b49a
...
@@ -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,35 @@ class Update implements Executable
...
@@ -187,20 +174,35 @@ 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
(
Server
$server
)
{
$cmd
=
[
'update'
=>
$this
->
collectionName
,
'updates'
=>
[[
'q'
=>
$this
->
filter
,
'u'
=>
$this
->
update
]
+
$this
->
createUpdateOptions
()]];
if
(
isset
(
$this
->
options
[
'writeConcern'
]))
{
$cmd
[
'writeConcern'
]
=
$this
->
options
[
'writeConcern'
];
}
if
(
isset
(
$this
->
options
[
'bypassDocumentValidation'
])
&&
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForDocumentLevelValidation
))
{
$cmd
[
'bypassDocumentValidation'
]
=
$this
->
options
[
'bypassDocumentValidation'
];
}
return
$cmd
;
}
/**
/**
* 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 +216,30 @@ class Update implements Executable
...
@@ -214,4 +216,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 createExecuteOptions().
*
* @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
;
}
}
}
src/Operation/UpdateMany.php
View file @
f6b1b49a
...
@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
...
@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::updateMany()
* @see \MongoDB\Collection::updateMany()
* @see http://docs.mongodb.org/manual/reference/command/update/
* @see http://docs.mongodb.org/manual/reference/command/update/
*/
*/
class
UpdateMany
implements
Executable
class
UpdateMany
implements
Executable
,
Explainable
{
{
private
$update
;
private
$update
;
...
@@ -104,4 +104,9 @@ class UpdateMany implements Executable
...
@@ -104,4 +104,9 @@ class UpdateMany implements Executable
{
{
return
$this
->
update
->
execute
(
$server
);
return
$this
->
update
->
execute
(
$server
);
}
}
public
function
getCommandDocument
(
Server
$server
)
{
return
$this
->
update
->
getCommandDocument
(
$server
);
}
}
}
src/Operation/UpdateOne.php
View file @
f6b1b49a
...
@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
...
@@ -30,7 +30,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::updateOne()
* @see \MongoDB\Collection::updateOne()
* @see http://docs.mongodb.org/manual/reference/command/update/
* @see http://docs.mongodb.org/manual/reference/command/update/
*/
*/
class
UpdateOne
implements
Executable
class
UpdateOne
implements
Executable
,
Explainable
{
{
private
$update
;
private
$update
;
...
@@ -104,4 +104,9 @@ class UpdateOne implements Executable
...
@@ -104,4 +104,9 @@ class UpdateOne implements Executable
{
{
return
$this
->
update
->
execute
(
$server
);
return
$this
->
update
->
execute
(
$server
);
}
}
public
function
getCommandDocument
(
Server
$server
)
{
return
$this
->
update
->
getCommandDocument
(
$server
);
}
}
}
tests/Operation/ExplainFunctionalTest.php
0 → 100644
View file @
f6b1b49a
This diff is collapsed.
Click to expand it.
tests/Operation/ExplainTest.php
0 → 100644
View file @
f6b1b49a
<?php
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Operation\Count
;
use
MongoDB\Operation\Distinct
;
use
MongoDB\Operation\Explain
;
class
ExplainTest
extends
TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidConstructorOptions
*/
public
function
testConstructorOptionTypeChecks
(
array
$options
)
{
$explainable
=
$this
->
getMockBuilder
(
'MongoDB\Operation\Explainable'
)
->
getMock
();
new
Explain
(
$this
->
getDatabaseName
(),
$explainable
,
$options
);
}
public
function
provideInvalidConstructorOptions
()
{
$options
=
[];
foreach
(
$this
->
getInvalidStringValues
()
as
$value
)
{
$options
[][]
=
[
'verbosity'
=>
$value
];
}
foreach
(
$this
->
getInvalidArrayValues
()
as
$value
)
{
$options
[][]
=
[
'typeMap'
=>
$value
];
}
return
$options
;
}
}
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