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
3c28b7d9
Commit
3c28b7d9
authored
Jun 16, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract Database::createCollection() to an operation class
parent
d2fe8b39
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
145 additions
and
7 deletions
+145
-7
Database.php
src/Database.php
+6
-7
CreateCollection.php
src/Operation/CreateCollection.php
+139
-0
No files found.
src/Database.php
View file @
3c28b7d9
...
...
@@ -12,6 +12,7 @@ use MongoDB\Driver\Server;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Model\CollectionInfoIterator
;
use
MongoDB\Operation\CreateCollection
;
use
MongoDB\Operation\DropCollection
;
use
MongoDB\Operation\DropDatabase
;
use
MongoDB\Operation\ListCollections
;
...
...
@@ -55,19 +56,17 @@ class Database
/**
* Create a new collection explicitly.
*
* @see http://docs.mongodb.org/manual/reference/command/create/
* @see http://docs.mongodb.org/manual/reference/method/db.createCollection/
* @see CreateCollection::__construct() for supported options
* @param string $collectionName
* @param array $options
* @return
Cursor
* @return
object Command result document
*/
public
function
createCollection
(
$collectionName
,
array
$options
=
array
())
{
$collectionName
=
(
string
)
$collectionName
;
$command
=
new
Command
(
array
(
'create'
=>
$collectionName
)
+
$options
);
$readPreference
=
new
ReadPreference
(
ReadPreference
::
RP_PRIMARY
);
$operation
=
new
CreateCollection
(
$this
->
databaseName
,
$collectionName
,
$options
);
$server
=
$this
->
manager
->
selectServer
(
new
ReadPreference
(
ReadPreference
::
RP_PRIMARY
));
return
$
this
->
manager
->
executeCommand
(
$this
->
databaseName
,
$command
,
$readPreference
);
return
$
operation
->
execute
(
$server
);
}
/**
...
...
src/Operation/CreateCollection.php
0 → 100644
View file @
3c28b7d9
<?php
namespace
MongoDB\Operation
;
use
MongoDB\Driver\Command
;
use
MongoDB\Driver\Server
;
use
MongoDB\Driver\BulkWrite
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\RuntimeException
;
use
MongoDB\Exception\UnexpectedTypeException
;
use
MongoDB\Model\IndexInput
;
/**
* Operation for the create command.
*
* @api
* @see MongoDB\Database::createCollection()
* @see http://docs.mongodb.org/manual/reference/command/create/
*/
class
CreateCollection
implements
Executable
{
const
USE_POWER_OF_2_SIZES
=
1
;
const
NO_PADDING
=
2
;
private
$databaseName
;
private
$collectionName
;
private
$options
=
array
();
/**
* Constructs a create command.
*
* Supported options:
*
* * autoIndexId (boolean): Specify false to disable the automatic creation
* of an index on the _id field. For replica sets, this option cannot be
* false. The default is true.
*
* * capped (boolean): Specify true to create a capped collection. If set,
* the size option must also be specified. The default is false.
*
* * flags (integer): Options for the MMAPv1 storage engine only. Must be a
* bitwise combination USE_POWER_OF_2_SIZES and NO_PADDING. The default
* is USE_POWER_OF_2_SIZES.
*
* * max (integer): The maximum number of documents allowed in the capped
* collection. The size option takes precedence over this limit.
*
* * maxTimeMS (integer): The maximum amount of time to allow the query to
* run.
*
* * size (integer): The maximum number of bytes for a capped collection.
*
* * storageEngine (document): Storage engine options.
*
* @see http://source.wiredtiger.com/2.4.1/struct_w_t___s_e_s_s_i_o_n.html#a358ca4141d59c345f401c58501276bbb
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param array $options Command options
* @throws InvalidArgumentException
*/
public
function
__construct
(
$databaseName
,
$collectionName
,
array
$options
=
array
())
{
if
(
isset
(
$options
[
'autoIndexId'
])
&&
!
is_bool
(
$options
[
'autoIndexId'
]))
{
throw
new
InvalidArgumentTypeException
(
'"autoIndexId" option'
,
$options
[
'autoIndexId'
],
'boolean'
);
}
if
(
isset
(
$options
[
'capped'
])
&&
!
is_bool
(
$options
[
'capped'
]))
{
throw
new
InvalidArgumentTypeException
(
'"capped" option'
,
$options
[
'capped'
],
'boolean'
);
}
if
(
isset
(
$options
[
'flags'
])
&&
!
is_integer
(
$options
[
'flags'
]))
{
throw
new
InvalidArgumentTypeException
(
'"flags" option'
,
$options
[
'flags'
],
'integer'
);
}
if
(
isset
(
$options
[
'max'
])
&&
!
is_integer
(
$options
[
'max'
]))
{
throw
new
InvalidArgumentTypeException
(
'"max" option'
,
$options
[
'max'
],
'integer'
);
}
if
(
isset
(
$options
[
'maxTimeMS'
])
&&
!
is_integer
(
$options
[
'maxTimeMS'
]))
{
throw
new
InvalidArgumentTypeException
(
'"maxTimeMS" option'
,
$options
[
'maxTimeMS'
],
'integer'
);
}
if
(
isset
(
$options
[
'size'
])
&&
!
is_integer
(
$options
[
'size'
]))
{
throw
new
InvalidArgumentTypeException
(
'"size" option'
,
$options
[
'size'
],
'integer'
);
}
if
(
isset
(
$options
[
'storageEngine'
])
&&
!
is_array
(
$options
[
'storageEngine'
])
&&
!
is_object
(
$options
[
'storageEngine'
]))
{
throw
new
InvalidArgumentTypeException
(
'"storageEngine" option'
,
$options
[
'storageEngine'
],
'array or object'
);
}
$this
->
databaseName
=
(
string
)
$databaseName
;
$this
->
collectionName
=
(
string
)
$collectionName
;
$this
->
options
=
$options
;
}
/**
* Execute the operation.
*
* For servers < 2.6, this will actually perform an insert operation on the
* database's "system.indexes" collection.
*
* @see Executable::execute()
* @param Server $server
* @return object Command result document
*/
public
function
execute
(
Server
$server
)
{
$cursor
=
$server
->
executeCommand
(
$this
->
databaseName
,
$this
->
createCommand
());
$result
=
current
(
$cursor
->
toArray
());
if
(
empty
(
$result
[
'ok'
]))
{
throw
new
RuntimeException
(
isset
(
$result
[
'errmsg'
])
?
$result
[
'errmsg'
]
:
'Unknown error'
);
}
return
$result
;
}
/**
* Create the create command.
*
* @return Command
*/
private
function
createCommand
()
{
$cmd
=
array
(
'create'
=>
$this
->
collectionName
);
foreach
(
array
(
'autoIndexId'
,
'capped'
,
'flags'
,
'max'
,
'maxTimeMS'
,
'size'
)
as
$option
)
{
if
(
isset
(
$this
->
options
[
$option
]))
{
$cmd
[
$option
]
=
$this
->
options
[
$option
];
}
}
if
(
!
empty
(
$this
->
options
[
'storageEngine'
]))
{
$cmd
[
'storageEngine'
]
=
(
object
)
$this
->
options
[
'storageEngine'
];
}
return
new
Command
(
$cmd
);
}
}
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