Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
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
PSDI Army
yii2
Commits
99b6ae27
Commit
99b6ae27
authored
Dec 05, 2013
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Logging and profiling at Mongo improved.
parent
e2f587a3
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
78 additions
and
9 deletions
+78
-9
Collection.php
extensions/mongo/Collection.php
+2
-2
Database.php
extensions/mongo/Database.php
+64
-3
Query.php
extensions/mongo/Query.php
+2
-2
DatabaseTest.php
tests/unit/extensions/mongo/DatabaseTest.php
+10
-2
No files found.
extensions/mongo/Collection.php
View file @
99b6ae27
...
...
@@ -426,11 +426,11 @@ class Collection extends Object
*/
public
function
aggregate
(
$pipeline
,
$pipelineOperator
=
[])
{
$token
=
'Aggregating from '
.
$this
->
getFullName
();
$args
=
func_get_args
();
$token
=
$this
->
composeLogToken
(
'aggregate'
,
$args
);
Yii
::
info
(
$token
,
__METHOD__
);
try
{
Yii
::
beginProfile
(
$token
,
__METHOD__
);
$args
=
func_get_args
();
$result
=
call_user_func_array
([
$this
->
mongoCollection
,
'aggregate'
],
$args
);
$this
->
tryResultError
(
$result
);
Yii
::
endProfile
(
$token
,
__METHOD__
);
...
...
extensions/mongo/Database.php
View file @
99b6ae27
...
...
@@ -9,10 +9,13 @@ namespace yii\mongo;
use
yii\base\Object
;
use
Yii
;
use
yii\helpers\Json
;
/**
* Database represents the Mongo database information.
*
* @property string $name name of this database. This property is read-only.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
...
...
@@ -32,6 +35,14 @@ class Database extends Object
private
$_fileCollections
=
[];
/**
* @return string name of this database.
*/
public
function
getName
()
{
return
$this
->
mongoDb
->
__toString
();
}
/**
* Returns the Mongo collection with the given name.
* @param string $name collection name
* @param boolean $refresh whether to reload the collection instance even if it is found in the cache.
...
...
@@ -93,10 +104,21 @@ class Database extends Object
* @param string $name name of the collection
* @param array $options collection options in format: "name" => "value"
* @return \MongoCollection new mongo collection instance.
* @throws Exception on failure.
*/
public
function
createCollection
(
$name
,
$options
=
[])
{
return
$this
->
mongoDb
->
createCollection
(
$name
,
$options
);
$token
=
$this
->
getName
()
.
'.create('
.
$name
.
', '
.
Json
::
encode
(
$options
)
.
')'
;
Yii
::
info
(
$token
,
__METHOD__
);
try
{
Yii
::
beginProfile
(
$token
,
__METHOD__
);
$result
=
$this
->
mongoDb
->
createCollection
(
$name
,
$options
);
Yii
::
endProfile
(
$token
,
__METHOD__
);
return
$result
;
}
catch
(
\Exception
$e
)
{
Yii
::
endProfile
(
$token
,
__METHOD__
);
throw
new
Exception
(
$e
->
getMessage
(),
(
int
)
$e
->
getCode
(),
$e
);
}
}
/**
...
...
@@ -104,9 +126,47 @@ class Database extends Object
* @param array $command command specification.
* @param array $options options in format: "name" => "value"
* @return array database response.
* @throws Exception on failure.
*/
public
function
execute
(
$command
,
$options
=
[])
public
function
execute
Command
(
$command
,
$options
=
[])
{
return
$this
->
mongoDb
->
command
(
$command
,
$options
);
$token
=
$this
->
getName
()
.
'.$cmd('
.
Json
::
encode
(
$command
)
.
', '
.
Json
::
encode
(
$options
)
.
')'
;
Yii
::
info
(
$token
,
__METHOD__
);
try
{
Yii
::
beginProfile
(
$token
,
__METHOD__
);
$result
=
$this
->
mongoDb
->
command
(
$command
,
$options
);
$this
->
tryResultError
(
$result
);
Yii
::
endProfile
(
$token
,
__METHOD__
);
return
$result
;
}
catch
(
\Exception
$e
)
{
Yii
::
endProfile
(
$token
,
__METHOD__
);
throw
new
Exception
(
$e
->
getMessage
(),
(
int
)
$e
->
getCode
(),
$e
);
}
}
/**
* Checks if command execution result ended with an error.
* @param mixed $result raw command execution result.
* @throws Exception if an error occurred.
*/
protected
function
tryResultError
(
$result
)
{
if
(
is_array
(
$result
))
{
if
(
!
empty
(
$result
[
'errmsg'
]))
{
$errorMessage
=
$result
[
'errmsg'
];
}
elseif
(
!
empty
(
$result
[
'err'
]))
{
$errorMessage
=
$result
[
'err'
];
}
if
(
isset
(
$errorMessage
))
{
if
(
array_key_exists
(
'ok'
,
$result
))
{
$errorCode
=
(
int
)
$result
[
'ok'
];
}
else
{
$errorCode
=
0
;
}
throw
new
Exception
(
$errorMessage
,
$errorCode
);
}
}
elseif
(
!
$result
)
{
throw
new
Exception
(
'Unknown error, use "w=1" option to enable error tracking'
);
}
}
}
\ No newline at end of file
extensions/mongo/Query.php
View file @
99b6ae27
...
...
@@ -131,7 +131,7 @@ class Query extends Component implements QueryInterface
*/
protected
function
fetchRows
(
$cursor
,
$all
=
true
,
$indexBy
=
null
)
{
$token
=
'
Querying: '
.
Json
::
encode
(
$cursor
->
info
())
;
$token
=
'
find('
.
Json
::
encode
(
$cursor
->
info
())
.
')'
;
Yii
::
info
(
$token
,
__METHOD__
);
try
{
Yii
::
beginProfile
(
$token
,
__METHOD__
);
...
...
@@ -213,7 +213,7 @@ class Query extends Component implements QueryInterface
public
function
count
(
$q
=
'*'
,
$db
=
null
)
{
$cursor
=
$this
->
buildCursor
(
$db
);
$token
=
'
Counting: '
.
Json
::
encode
(
$cursor
->
info
())
;
$token
=
'
find.count('
.
Json
::
encode
(
$cursor
->
info
())
.
')'
;
Yii
::
info
(
$token
,
__METHOD__
);
try
{
Yii
::
beginProfile
(
$token
,
__METHOD__
);
...
...
tests/unit/extensions/mongo/DatabaseTest.php
View file @
99b6ae27
...
...
@@ -49,15 +49,22 @@ class DatabaseTest extends MongoTestCase
$this
->
assertFalse
(
$collection
===
$collectionRefreshed
);
}
public
function
testCommand
()
public
function
test
Execute
Command
()
{
$database
=
$connection
=
$this
->
getConnection
()
->
getDatabase
();
$result
=
$database
->
execute
([
$result
=
$database
->
execute
Command
([
'distinct'
=>
'customer'
,
'key'
=>
'name'
]);
$this
->
assertTrue
(
array_key_exists
(
'ok'
,
$result
));
$this
->
assertTrue
(
array_key_exists
(
'values'
,
$result
));
}
public
function
testCreateCollection
()
{
$database
=
$connection
=
$this
->
getConnection
()
->
getDatabase
();
$collection
=
$database
->
createCollection
(
'customer'
);
$this
->
assertTrue
(
$collection
instanceof
\MongoCollection
);
}
}
\ No newline at end of file
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