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
d070cf84
Commit
d070cf84
authored
Jun 16, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract DropIndexes operation class
parent
3c28b7d9
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
17 deletions
+77
-17
Collection.php
src/Collection.php
+10
-17
DropIndexes.php
src/Operation/DropIndexes.php
+67
-0
No files found.
src/Collection.php
View file @
d070cf84
...
@@ -19,6 +19,7 @@ use MongoDB\Operation\CreateIndexes;
...
@@ -19,6 +19,7 @@ use MongoDB\Operation\CreateIndexes;
use
MongoDB\Operation\Count
;
use
MongoDB\Operation\Count
;
use
MongoDB\Operation\Distinct
;
use
MongoDB\Operation\Distinct
;
use
MongoDB\Operation\DropCollection
;
use
MongoDB\Operation\DropCollection
;
use
MongoDB\Operation\DropIndexes
;
use
MongoDB\Operation\FindOneAndDelete
;
use
MongoDB\Operation\FindOneAndDelete
;
use
MongoDB\Operation\FindOneAndReplace
;
use
MongoDB\Operation\FindOneAndReplace
;
use
MongoDB\Operation\FindOneAndUpdate
;
use
MongoDB\Operation\FindOneAndUpdate
;
...
@@ -366,43 +367,35 @@ class Collection
...
@@ -366,43 +367,35 @@ class Collection
/**
/**
* Drop a single index in the collection.
* Drop a single index in the collection.
*
*
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
* @param string $indexName Index name
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndex/
* @return object Command result document
* @param string $indexName
* @return Cursor
* @throws InvalidArgumentException if $indexName is an empty string or "*"
* @throws InvalidArgumentException if $indexName is an empty string or "*"
*/
*/
public
function
dropIndex
(
$indexName
)
public
function
dropIndex
(
$indexName
)
{
{
$indexName
=
(
string
)
$indexName
;
$indexName
=
(
string
)
$indexName
;
if
(
$indexName
===
''
)
{
throw
new
InvalidArgumentException
(
'Index name cannot be empty'
);
}
if
(
$indexName
===
'*'
)
{
if
(
$indexName
===
'*'
)
{
throw
new
InvalidArgumentException
(
'dropIndexes() must be used to drop multiple indexes'
);
throw
new
InvalidArgumentException
(
'dropIndexes() must be used to drop multiple indexes'
);
}
}
$
command
=
new
Command
(
array
(
'dropIndexes'
=>
$this
->
collname
,
'index'
=>
$indexName
)
);
$
operation
=
new
DropIndexes
(
$this
->
dbname
,
$this
->
collname
,
$indexName
);
$
readPreference
=
new
ReadPreference
(
ReadPreference
::
RP_PRIMARY
);
$
server
=
$this
->
manager
->
selectServer
(
new
ReadPreference
(
ReadPreference
::
RP_PRIMARY
)
);
return
$
this
->
manager
->
executeCommand
(
$this
->
dbname
,
$command
,
$readPreference
);
return
$
operation
->
execute
(
$server
);
}
}
/**
/**
* Drop all indexes in the collection.
* Drop all indexes in the collection.
*
*
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
* @return object Command result document
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndexes/
* @return Cursor
*/
*/
public
function
dropIndexes
()
public
function
dropIndexes
()
{
{
$
command
=
new
Command
(
array
(
'dropIndexes'
=>
$this
->
collname
,
'index'
=>
'*'
)
);
$
operation
=
new
DropIndexes
(
$this
->
dbname
,
$this
->
collname
,
'*'
);
$
readPreference
=
new
ReadPreference
(
ReadPreference
::
RP_PRIMARY
);
$
server
=
$this
->
manager
->
selectServer
(
new
ReadPreference
(
ReadPreference
::
RP_PRIMARY
)
);
return
$
this
->
manager
->
executeCommand
(
$this
->
dbname
,
$command
,
$readPreference
);
return
$
operation
->
execute
(
$server
);
}
}
/**
/**
...
...
src/Operation/DropIndexes.php
0 → 100644
View file @
d070cf84
<?php
namespace
MongoDB\Operation
;
use
MongoDB\Driver\Command
;
use
MongoDB\Driver\Server
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\RuntimeException
;
/**
* Operation for the dropIndexes command.
*
* @api
* @see MongoDB\Collection::dropIndexes()
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
*/
class
DropIndexes
implements
Executable
{
private
$databaseName
;
private
$collectionName
;
private
$indexName
;
/**
* Constructs a dropIndexes command.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param string $indexName Index name (use "*" to drop all indexes)
* @throws InvalidArgumentException
*/
public
function
__construct
(
$databaseName
,
$collectionName
,
$indexName
)
{
$indexName
=
(
string
)
$indexName
;
if
(
$indexName
===
''
)
{
throw
new
InvalidArgumentException
(
'$indexName cannot be empty'
);
}
$this
->
databaseName
=
(
string
)
$databaseName
;
$this
->
collectionName
=
(
string
)
$collectionName
;
$this
->
indexName
=
$indexName
;
}
/**
* Execute the operation.
*
* @see Executable::execute()
* @param Server $server
* @return object Command result document
*/
public
function
execute
(
Server
$server
)
{
$cmd
=
array
(
'dropIndexes'
=>
$this
->
collectionName
,
'index'
=>
$this
->
indexName
,
);
$cursor
=
$server
->
executeCommand
(
$this
->
databaseName
,
new
Command
(
$cmd
));
$result
=
current
(
$cursor
->
toArray
());
if
(
empty
(
$result
[
'ok'
]))
{
throw
new
RuntimeException
(
isset
(
$result
[
'errmsg'
])
?
$result
[
'errmsg'
]
:
'Unknown error'
);
}
return
$result
;
}
}
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