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
a6ac94d6
Unverified
Commit
a6ac94d6
authored
Aug 06, 2019
by
Andreas Braun
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #656
parents
e98829b6
2b1c1ec9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
190 deletions
+25
-190
CountDocuments.php
src/Operation/CountDocuments.php
+16
-103
EstimatedDocumentCount.php
src/Operation/EstimatedDocumentCount.php
+9
-87
No files found.
src/Operation/CountDocuments.php
View file @
a6ac94d6
...
@@ -17,11 +17,7 @@
...
@@ -17,11 +17,7 @@
namespace
MongoDB\Operation
;
namespace
MongoDB\Operation
;
use
MongoDB\Driver\Command
;
use
MongoDB\Driver\ReadConcern
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\Server
;
use
MongoDB\Driver\Server
;
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\UnexpectedValueException
;
use
MongoDB\Exception\UnexpectedValueException
;
...
@@ -36,13 +32,12 @@ use MongoDB\Exception\UnsupportedException;
...
@@ -36,13 +32,12 @@ use MongoDB\Exception\UnsupportedException;
*/
*/
class
CountDocuments
implements
Executable
class
CountDocuments
implements
Executable
{
{
private
static
$wireVersionForCollation
=
5
;
private
static
$wireVersionForReadConcern
=
4
;
private
$databaseName
;
private
$databaseName
;
private
$collectionName
;
private
$collectionName
;
private
$filter
;
private
$filter
;
private
$options
;
private
$aggregateOptions
;
private
$countOptions
;
private
$aggregate
;
/**
/**
* Constructs an aggregate command for counting documents
* Constructs an aggregate command for counting documents
...
@@ -89,46 +84,22 @@ class CountDocuments implements Executable
...
@@ -89,46 +84,22 @@ class CountDocuments implements Executable
throw
InvalidArgumentException
::
invalidType
(
'$filter'
,
$filter
,
'array or object'
);
throw
InvalidArgumentException
::
invalidType
(
'$filter'
,
$filter
,
'array or object'
);
}
}
if
(
isset
(
$options
[
'collation'
])
&&
!
is_array
(
$options
[
'collation'
])
&&
!
is_object
(
$options
[
'collation'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"collation" option'
,
$options
[
'collation'
],
'array or object'
);
}
if
(
isset
(
$options
[
'hint'
])
&&
!
is_string
(
$options
[
'hint'
])
&&
!
is_array
(
$options
[
'hint'
])
&&
!
is_object
(
$options
[
'hint'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"hint" option'
,
$options
[
'hint'
],
'string or array or object'
);
}
if
(
isset
(
$options
[
'limit'
])
&&
!
is_integer
(
$options
[
'limit'
]))
{
if
(
isset
(
$options
[
'limit'
])
&&
!
is_integer
(
$options
[
'limit'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"limit" option'
,
$options
[
'limit'
],
'integer'
);
throw
InvalidArgumentException
::
invalidType
(
'"limit" option'
,
$options
[
'limit'
],
'integer'
);
}
}
if
(
isset
(
$options
[
'maxTimeMS'
])
&&
!
is_integer
(
$options
[
'maxTimeMS'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"maxTimeMS" option'
,
$options
[
'maxTimeMS'
],
'integer'
);
}
if
(
isset
(
$options
[
'readConcern'
])
&&
!
$options
[
'readConcern'
]
instanceof
ReadConcern
)
{
throw
InvalidArgumentException
::
invalidType
(
'"readConcern" option'
,
$options
[
'readConcern'
],
ReadConcern
::
class
);
}
if
(
isset
(
$options
[
'readPreference'
])
&&
!
$options
[
'readPreference'
]
instanceof
ReadPreference
)
{
throw
InvalidArgumentException
::
invalidType
(
'"readPreference" option'
,
$options
[
'readPreference'
],
ReadPreference
::
class
);
}
if
(
isset
(
$options
[
'session'
])
&&
!
$options
[
'session'
]
instanceof
Session
)
{
throw
InvalidArgumentException
::
invalidType
(
'"session" option'
,
$options
[
'session'
],
Session
::
class
);
}
if
(
isset
(
$options
[
'skip'
])
&&
!
is_integer
(
$options
[
'skip'
]))
{
if
(
isset
(
$options
[
'skip'
])
&&
!
is_integer
(
$options
[
'skip'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"skip" option'
,
$options
[
'skip'
],
'integer'
);
throw
InvalidArgumentException
::
invalidType
(
'"skip" option'
,
$options
[
'skip'
],
'integer'
);
}
}
if
(
isset
(
$options
[
'readConcern'
])
&&
$options
[
'readConcern'
]
->
isDefault
())
{
unset
(
$options
[
'readConcern'
]);
}
$this
->
databaseName
=
(
string
)
$databaseName
;
$this
->
databaseName
=
(
string
)
$databaseName
;
$this
->
collectionName
=
(
string
)
$collectionName
;
$this
->
collectionName
=
(
string
)
$collectionName
;
$this
->
filter
=
$filter
;
$this
->
filter
=
$filter
;
$this
->
options
=
$options
;
$this
->
aggregateOptions
=
array_intersect_key
(
$options
,
[
'collation'
=>
1
,
'hint'
=>
1
,
'maxTimeMS'
=>
1
,
'readConcern'
=>
1
,
'readPreference'
=>
1
,
'session'
=>
1
]);
$this
->
countOptions
=
array_intersect_key
(
$options
,
[
'limit'
=>
1
,
'skip'
=>
1
]);
$this
->
aggregate
=
$this
->
createAggregate
();
}
}
/**
/**
...
@@ -143,20 +114,7 @@ class CountDocuments implements Executable
...
@@ -143,20 +114,7 @@ class CountDocuments implements Executable
*/
*/
public
function
execute
(
Server
$server
)
public
function
execute
(
Server
$server
)
{
{
if
(
isset
(
$this
->
options
[
'collation'
])
&&
!
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForCollation
))
{
$cursor
=
$this
->
aggregate
->
execute
(
$server
);
throw
UnsupportedException
::
collationNotSupported
();
}
if
(
isset
(
$this
->
options
[
'readConcern'
])
&&
!
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForReadConcern
))
{
throw
UnsupportedException
::
readConcernNotSupported
();
}
$inTransaction
=
isset
(
$this
->
options
[
'session'
])
&&
$this
->
options
[
'session'
]
->
isInTransaction
();
if
(
$inTransaction
&&
isset
(
$this
->
options
[
'readConcern'
]))
{
throw
UnsupportedException
::
readConcernNotSupportedInTransaction
();
}
$cursor
=
$server
->
executeReadCommand
(
$this
->
databaseName
,
new
Command
(
$this
->
createCommandDocument
()),
$this
->
createOptions
());
$allResults
=
$cursor
->
toArray
();
$allResults
=
$cursor
->
toArray
();
/* If there are no documents to count, the aggregation pipeline has no items to group, and
/* If there are no documents to count, the aggregation pipeline has no items to group, and
...
@@ -174,69 +132,24 @@ class CountDocuments implements Executable
...
@@ -174,69 +132,24 @@ class CountDocuments implements Executable
}
}
/**
/**
* Create the count command document.
* @return Aggregate
*
* @return array
*/
*/
private
function
create
CommandDocument
()
private
function
create
Aggregate
()
{
{
$pipeline
=
[
$pipeline
=
[
[
'$match'
=>
(
object
)
$this
->
filter
]
[
'$match'
=>
(
object
)
$this
->
filter
]
];
];
if
(
isset
(
$this
->
o
ptions
[
'skip'
]))
{
if
(
isset
(
$this
->
countO
ptions
[
'skip'
]))
{
$pipeline
[]
=
[
'$skip'
=>
$this
->
o
ptions
[
'skip'
]];
$pipeline
[]
=
[
'$skip'
=>
$this
->
countO
ptions
[
'skip'
]];
}
}
if
(
isset
(
$this
->
o
ptions
[
'limit'
]))
{
if
(
isset
(
$this
->
countO
ptions
[
'limit'
]))
{
$pipeline
[]
=
[
'$limit'
=>
$this
->
o
ptions
[
'limit'
]];
$pipeline
[]
=
[
'$limit'
=>
$this
->
countO
ptions
[
'limit'
]];
}
}
$pipeline
[]
=
[
'$group'
=>
[
'_id'
=>
1
,
'n'
=>
[
'$sum'
=>
1
]]];
$pipeline
[]
=
[
'$group'
=>
[
'_id'
=>
1
,
'n'
=>
[
'$sum'
=>
1
]]];
$cmd
=
[
return
new
Aggregate
(
$this
->
databaseName
,
$this
->
collectionName
,
$pipeline
,
$this
->
aggregateOptions
);
'aggregate'
=>
$this
->
collectionName
,
'pipeline'
=>
$pipeline
,
'cursor'
=>
(
object
)
[],
];
if
(
isset
(
$this
->
options
[
'collation'
]))
{
$cmd
[
'collation'
]
=
(
object
)
$this
->
options
[
'collation'
];
}
if
(
isset
(
$this
->
options
[
'hint'
]))
{
$cmd
[
'hint'
]
=
is_array
(
$this
->
options
[
'hint'
])
?
(
object
)
$this
->
options
[
'hint'
]
:
$this
->
options
[
'hint'
];
}
if
(
isset
(
$this
->
options
[
'maxTimeMS'
]))
{
$cmd
[
'maxTimeMS'
]
=
$this
->
options
[
'maxTimeMS'
];
}
return
$cmd
;
}
/**
* Create options for executing the command.
*
* @see http://php.net/manual/en/mongodb-driver-server.executereadcommand.php
* @return array
*/
private
function
createOptions
()
{
$options
=
[];
if
(
isset
(
$this
->
options
[
'readConcern'
]))
{
$options
[
'readConcern'
]
=
$this
->
options
[
'readConcern'
];
}
if
(
isset
(
$this
->
options
[
'readPreference'
]))
{
$options
[
'readPreference'
]
=
$this
->
options
[
'readPreference'
];
}
if
(
isset
(
$this
->
options
[
'session'
]))
{
$options
[
'session'
]
=
$this
->
options
[
'session'
];
}
return
$options
;
}
}
}
}
src/Operation/EstimatedDocumentCount.php
View file @
a6ac94d6
...
@@ -17,11 +17,7 @@
...
@@ -17,11 +17,7 @@
namespace
MongoDB\Operation
;
namespace
MongoDB\Operation
;
use
MongoDB\Driver\Command
;
use
MongoDB\Driver\ReadConcern
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\Server
;
use
MongoDB\Driver\Server
;
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\UnexpectedValueException
;
use
MongoDB\Exception\UnexpectedValueException
;
...
@@ -36,12 +32,10 @@ use MongoDB\Exception\UnsupportedException;
...
@@ -36,12 +32,10 @@ use MongoDB\Exception\UnsupportedException;
*/
*/
class
EstimatedDocumentCount
implements
Executable
,
Explainable
class
EstimatedDocumentCount
implements
Executable
,
Explainable
{
{
private
static
$wireVersionForCollation
=
5
;
private
static
$wireVersionForReadConcern
=
4
;
private
$databaseName
;
private
$databaseName
;
private
$collectionName
;
private
$collectionName
;
private
$options
;
private
$options
;
private
$count
;
/**
/**
* Constructs a count command.
* Constructs a count command.
...
@@ -69,29 +63,11 @@ class EstimatedDocumentCount implements Executable, Explainable
...
@@ -69,29 +63,11 @@ class EstimatedDocumentCount implements Executable, Explainable
*/
*/
public
function
__construct
(
$databaseName
,
$collectionName
,
array
$options
=
[])
public
function
__construct
(
$databaseName
,
$collectionName
,
array
$options
=
[])
{
{
if
(
isset
(
$options
[
'maxTimeMS'
])
&&
!
is_integer
(
$options
[
'maxTimeMS'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"maxTimeMS" option'
,
$options
[
'maxTimeMS'
],
'integer'
);
}
if
(
isset
(
$options
[
'readConcern'
])
&&
!
$options
[
'readConcern'
]
instanceof
ReadConcern
)
{
throw
InvalidArgumentException
::
invalidType
(
'"readConcern" option'
,
$options
[
'readConcern'
],
ReadConcern
::
class
);
}
if
(
isset
(
$options
[
'readPreference'
])
&&
!
$options
[
'readPreference'
]
instanceof
ReadPreference
)
{
throw
InvalidArgumentException
::
invalidType
(
'"readPreference" option'
,
$options
[
'readPreference'
],
ReadPreference
::
class
);
}
if
(
isset
(
$options
[
'session'
])
&&
!
$options
[
'session'
]
instanceof
Session
)
{
throw
InvalidArgumentException
::
invalidType
(
'"session" option'
,
$options
[
'session'
],
Session
::
class
);
}
if
(
isset
(
$options
[
'readConcern'
])
&&
$options
[
'readConcern'
]
->
isDefault
())
{
unset
(
$options
[
'readConcern'
]);
}
$this
->
databaseName
=
(
string
)
$databaseName
;
$this
->
databaseName
=
(
string
)
$databaseName
;
$this
->
collectionName
=
(
string
)
$collectionName
;
$this
->
collectionName
=
(
string
)
$collectionName
;
$this
->
options
=
$options
;
$this
->
options
=
array_intersect_key
(
$options
,
[
'maxTimeMS'
=>
1
,
'readConcern'
=>
1
,
'readPreference'
=>
1
,
'session'
=>
1
]);
$this
->
count
=
$this
->
createCount
();
}
}
/**
/**
...
@@ -106,73 +82,19 @@ class EstimatedDocumentCount implements Executable, Explainable
...
@@ -106,73 +82,19 @@ class EstimatedDocumentCount implements Executable, Explainable
*/
*/
public
function
execute
(
Server
$server
)
public
function
execute
(
Server
$server
)
{
{
if
(
isset
(
$this
->
options
[
'collation'
])
&&
!
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForCollation
))
{
return
$this
->
count
->
execute
(
$server
);
throw
UnsupportedException
::
collationNotSupported
();
}
if
(
isset
(
$this
->
options
[
'readConcern'
])
&&
!
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForReadConcern
))
{
throw
UnsupportedException
::
readConcernNotSupported
();
}
$inTransaction
=
isset
(
$this
->
options
[
'session'
])
&&
$this
->
options
[
'session'
]
->
isInTransaction
();
if
(
$inTransaction
&&
isset
(
$this
->
options
[
'readConcern'
]))
{
throw
UnsupportedException
::
readConcernNotSupportedInTransaction
();
}
$cursor
=
$server
->
executeReadCommand
(
$this
->
databaseName
,
new
Command
(
$this
->
createCommandDocument
()),
$this
->
createOptions
());
$result
=
current
(
$cursor
->
toArray
());
// Older server versions may return a float
if
(
!
isset
(
$result
->
n
)
||
!
(
is_integer
(
$result
->
n
)
||
is_float
(
$result
->
n
)))
{
throw
new
UnexpectedValueException
(
'count command did not return a numeric "n" value'
);
}
return
(
integer
)
$result
->
n
;
}
}
public
function
getCommandDocument
(
Server
$server
)
public
function
getCommandDocument
(
Server
$server
)
{
{
return
$this
->
c
reateCommandDocument
(
);
return
$this
->
c
ount
->
getCommandDocument
(
$server
);
}
}
/**
/**
* Create the count command document.
* @return Count
*
* @return array
*/
*/
private
function
createCo
mmandDocume
nt
()
private
function
createCo
u
nt
()
{
{
$cmd
=
[
'count'
=>
$this
->
collectionName
];
return
new
Count
(
$this
->
databaseName
,
$this
->
collectionName
,
[],
$this
->
options
);
if
(
isset
(
$this
->
options
[
'maxTimeMS'
]))
{
$cmd
[
'maxTimeMS'
]
=
$this
->
options
[
'maxTimeMS'
];
}
return
$cmd
;
}
/**
* Create options for executing the command.
*
* @see http://php.net/manual/en/mongodb-driver-server.executereadcommand.php
* @return array
*/
private
function
createOptions
()
{
$options
=
[];
if
(
isset
(
$this
->
options
[
'readConcern'
]))
{
$options
[
'readConcern'
]
=
$this
->
options
[
'readConcern'
];
}
if
(
isset
(
$this
->
options
[
'readPreference'
]))
{
$options
[
'readPreference'
]
=
$this
->
options
[
'readPreference'
];
}
if
(
isset
(
$this
->
options
[
'session'
]))
{
$options
[
'session'
]
=
$this
->
options
[
'session'
];
}
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