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
44f36ce3
Commit
44f36ce3
authored
Feb 16, 2018
by
Katherine Walker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-66: Wrapper method for collMod command
parent
4dc895c8
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
255 additions
and
0 deletions
+255
-0
Database.php
src/Database.php
+28
-0
ModifyCollection.php
src/Operation/ModifyCollection.php
+135
-0
DatabaseFunctionalTest.php
tests/Database/DatabaseFunctionalTest.php
+17
-0
ModifyCollectionFunctionalTest.php
tests/Operation/ModifyCollectionFunctionalTest.php
+31
-0
ModifyCollectionTest.php
tests/Operation/ModifyCollectionTest.php
+44
-0
No files found.
src/Database.php
View file @
44f36ce3
...
@@ -33,6 +33,7 @@ use MongoDB\Operation\DatabaseCommand;
...
@@ -33,6 +33,7 @@ use MongoDB\Operation\DatabaseCommand;
use
MongoDB\Operation\DropCollection
;
use
MongoDB\Operation\DropCollection
;
use
MongoDB\Operation\DropDatabase
;
use
MongoDB\Operation\DropDatabase
;
use
MongoDB\Operation\ListCollections
;
use
MongoDB\Operation\ListCollections
;
use
MongoDB\Operation\ModifyCollection
;
class
Database
class
Database
{
{
...
@@ -340,6 +341,33 @@ class Database
...
@@ -340,6 +341,33 @@ class Database
return
$operation
->
execute
(
$server
);
return
$operation
->
execute
(
$server
);
}
}
/**
* Modifies a collection or view.
*
* @see ModifyCollection::__construct() for supported options
* @param string $collectionName Collection or view to modify
* @param array $collectionOptions Collection or view options to assign
* @param array $options Command options
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public
function
modifyCollection
(
$collectionName
,
array
$collectionOptions
,
array
$options
=
[])
{
if
(
!
isset
(
$options
[
'typeMap'
]))
{
$options
[
'typeMap'
]
=
$this
->
typeMap
;
}
$server
=
$this
->
manager
->
selectServer
(
new
ReadPreference
(
ReadPreference
::
RP_PRIMARY
));
if
(
!
isset
(
$options
[
'writeConcern'
])
&&
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForWritableCommandWriteConcern
))
{
$options
[
'writeConcern'
]
=
$this
->
writeConcern
;
}
$operation
=
new
ModifyCollection
(
$this
->
databaseName
,
$collectionName
,
$collectionOptions
,
$options
);
return
$operation
->
execute
(
$server
);
}
/**
/**
* Select a collection within this database.
* Select a collection within this database.
*
*
...
...
src/Operation/ModifyCollection.php
0 → 100644
View file @
44f36ce3
<?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\Driver\Session
;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Driver\Exception\RuntimeException
as
DriverRuntimeException
;
use
MongoDB\Exception\InvalidArgumentException
;
/**
* Operation for the collMod command.
*
* @api
* @see \MongoDB\Database::modifyCollection()
* @see http://docs.mongodb.org/manual/reference/command/collMod/
*/
class
ModifyCollection
implements
Executable
{
private
$databaseName
;
private
$collectionName
;
private
$collectionOptions
;
private
$options
;
/**
* Constructs a collMod command.
*
* Supported options:
*
* * session (MongoDB\Driver\Session): Client session.
*
* Sessions are not supported for server versions < 3.6.
*
* * typeMap (array): Type map for BSON deserialization. This will only be
* used for the returned command result document.
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* This is not supported for server versions < 3.2 and will result in an
* exception at execution time if used.
*
* @param string $databaseName Database name
* @param string $collectionName Collection or view to modify
* @param string $collectionOptions Collection or view options to assign
* @param array $options Command options
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public
function
__construct
(
$databaseName
,
$collectionName
,
array
$collectionOptions
,
array
$options
=
[])
{
if
(
empty
(
$collectionOptions
))
{
throw
new
InvalidArgumentException
(
'$collectionOptions is empty'
);
}
if
(
isset
(
$options
[
'session'
])
&&
!
$options
[
'session'
]
instanceof
Session
)
{
throw
InvalidArgumentException
::
invalidType
(
'"session" option'
,
$options
[
'session'
],
'MongoDB\Driver\Session'
);
}
if
(
isset
(
$options
[
'typeMap'
])
&&
!
is_array
(
$options
[
'typeMap'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"typeMap" option'
,
$options
[
'typeMap'
],
'array'
);
}
if
(
isset
(
$options
[
'writeConcern'
])
&&
!
$options
[
'writeConcern'
]
instanceof
WriteConcern
)
{
throw
InvalidArgumentException
::
invalidType
(
'"writeConcern" option'
,
$options
[
'writeConcern'
],
'MongoDB\Driver\WriteConcern'
);
}
if
(
isset
(
$options
[
'writeConcern'
])
&&
$options
[
'writeConcern'
]
->
isDefault
())
{
unset
(
$options
[
'writeConcern'
]);
}
$this
->
databaseName
=
(
string
)
$databaseName
;
$this
->
collectionName
=
(
string
)
$collectionName
;
$this
->
collectionOptions
=
$collectionOptions
;
$this
->
options
=
$options
;
}
/**
* Execute the operation.
*
* @see Executable::execute()
* @param Server $server
* @return array|object Command result document
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public
function
execute
(
Server
$server
)
{
if
(
isset
(
$this
->
options
[
'writeConcern'
])
&&
!
\MongoDB\server_supports_feature
(
$server
,
self
::
$wireVersionForWriteConcern
))
{
throw
UnsupportedException
::
writeConcernNotSupported
();
}
$cursor
=
$server
->
executeWriteCommand
(
$this
->
databaseName
,
new
Command
([
'collMod'
=>
$this
->
collectionName
]
+
$this
->
collectionOptions
),
$this
->
createOptions
());
if
(
isset
(
$this
->
options
[
'typeMap'
]))
{
$cursor
->
setTypeMap
(
$this
->
options
[
'typeMap'
]);
}
return
current
(
$cursor
->
toArray
());
}
/**
* Create options for executing the command.
*
* @see http://php.net/manual/en/mongodb-driver-server.executereadwritecommand.php
* @return array
*/
private
function
createOptions
()
{
$options
=
[];
if
(
isset
(
$this
->
options
[
'session'
]))
{
$options
[
'session'
]
=
$this
->
options
[
'session'
];
}
if
(
isset
(
$this
->
options
[
'writeConcern'
]))
{
$options
[
'writeConcern'
]
=
$this
->
options
[
'writeConcern'
];
}
return
$options
;
}
}
tests/Database/DatabaseFunctionalTest.php
View file @
44f36ce3
...
@@ -8,6 +8,7 @@ use MongoDB\Driver\ReadConcern;
...
@@ -8,6 +8,7 @@ use MongoDB\Driver\ReadConcern;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Operation\CreateIndexes
;
/**
/**
* Functional tests for the Database class.
* Functional tests for the Database class.
...
@@ -152,6 +153,22 @@ class DatabaseFunctionalTest extends FunctionalTestCase
...
@@ -152,6 +153,22 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$this
->
assertSame
(
WriteConcern
::
MAJORITY
,
$debug
[
'writeConcern'
]
->
getW
());
$this
->
assertSame
(
WriteConcern
::
MAJORITY
,
$debug
[
'writeConcern'
]
->
getW
());
}
}
public
function
testModifyCollection
()
{
$this
->
database
->
createCollection
(
$this
->
getCollectionName
());
$indexes
=
[[
'key'
=>
[
'lastAccess'
=>
1
],
'expireAfterSeconds'
=>
3
]];
$createIndexes
=
new
CreateIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$indexes
);
$createIndexes
->
execute
(
$this
->
getPrimaryServer
());
$commandResult
=
$this
->
database
->
modifyCollection
(
$this
->
getCollectionName
(),
[
'index'
=>
[
'keyPattern'
=>
[
'lastAccess'
=>
1
],
'expireAfterSeconds'
=>
1000
]]);
$this
->
assertCommandSucceeded
(
$commandResult
);
$this
->
assertSame
(
3
,
$commandResult
[
'expireAfterSeconds_old'
]);
$this
->
assertSame
(
1000
,
$commandResult
[
'expireAfterSeconds_new'
]);
}
public
function
testSelectCollectionInheritsOptions
()
public
function
testSelectCollectionInheritsOptions
()
{
{
$databaseOptions
=
[
$databaseOptions
=
[
...
...
tests/Operation/ModifyCollectionFunctionalTest.php
0 → 100644
View file @
44f36ce3
<?php
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Operation\ModifyCollection
;
use
MongoDB\Operation\CreateCollection
;
use
MongoDB\Operation\CreateIndexes
;
class
ModifyCollectionFunctionalTest
extends
FunctionalTestCase
{
public
function
testCollMod
()
{
$operation
=
new
CreateCollection
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
$operation
->
execute
(
$this
->
getPrimaryServer
());
$indexes
=
[[
'key'
=>
[
'lastAccess'
=>
1
],
'expireAfterSeconds'
=>
3
]];
$createIndexes
=
new
CreateIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$indexes
);
$createIndexes
->
execute
(
$this
->
getPrimaryServer
());
$modifyCollection
=
new
ModifyCollection
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'index'
=>
[
'keyPattern'
=>
[
'lastAccess'
=>
1
],
'expireAfterSeconds'
=>
1000
]],
[
'typeMap'
=>
[
'root'
=>
'array'
]]
);
$result
=
$modifyCollection
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertSame
(
3
,
$result
[
'expireAfterSeconds_old'
]);
$this
->
assertSame
(
1000
,
$result
[
'expireAfterSeconds_new'
]);
}
}
tests/Operation/ModifyCollectionTest.php
0 → 100644
View file @
44f36ce3
<?php
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Operation\ModifyCollection
;
class
ModifyCollectionTest
extends
TestCase
{
public
function
testConstructorEmptyCollectionOptions
()
{
$this
->
expectException
(
InvalidArgumentException
::
class
);
$this
->
expectExceptionMessage
(
'$collectionOptions is empty'
);
new
ModifyCollection
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[]);
}
/**
* @dataProvider provideInvalidConstructorOptions
*/
public
function
testConstructorOptionTypeChecks
(
array
$options
)
{
$this
->
expectException
(
InvalidArgumentException
::
class
);
new
ModifyCollection
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[],
$options
);
}
public
function
provideInvalidConstructorOptions
()
{
$options
=
[];
foreach
(
$this
->
getInvalidSessionValues
()
as
$value
)
{
$options
[][]
=
[
'session'
=>
$value
];
}
foreach
(
$this
->
getInvalidArrayValues
()
as
$value
)
{
$options
[][]
=
[
'typeMap'
=>
$value
];
}
foreach
(
$this
->
getInvalidWriteConcernValues
()
as
$value
)
{
$options
[][]
=
[
'writeConcern'
=>
$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