Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-mongodb
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
laravel-mongodb
Commits
a6714df2
Commit
a6714df2
authored
Oct 05, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Query logging, check PR #45
parent
7b2867bd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
6 deletions
+70
-6
Builder.php
src/Jenssegers/Mongodb/Builder.php
+58
-5
Connection.php
src/Jenssegers/Mongodb/Connection.php
+12
-1
No files found.
src/Jenssegers/Mongodb/Builder.php
View file @
a6714df2
...
@@ -59,6 +59,8 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -59,6 +59,8 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/
*/
public
function
getFresh
(
$columns
=
array
(
'*'
))
public
function
getFresh
(
$columns
=
array
(
'*'
))
{
{
$start
=
microtime
(
true
);
// If no columns have been specified for the select statement, we will set them
// If no columns have been specified for the select statement, we will set them
// here to either the passed columns, or the standard default of retrieving
// here to either the passed columns, or the standard default of retrieving
// all of the columns on the table using the "wildcard" column character.
// all of the columns on the table using the "wildcard" column character.
...
@@ -138,6 +140,11 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -138,6 +140,11 @@ class Builder extends \Illuminate\Database\Query\Builder {
// Execute aggregation
// Execute aggregation
$results
=
$this
->
collection
->
aggregate
(
$pipeline
);
$results
=
$this
->
collection
->
aggregate
(
$pipeline
);
// Log query
$this
->
connection
->
logQuery
(
$this
->
from
.
'.aggregate('
.
json_encode
(
$pipeline
)
.
')'
,
array
(),
$this
->
connection
->
getElapsedTime
(
$start
));
// Return results
// Return results
return
$results
[
'result'
];
return
$results
[
'result'
];
}
}
...
@@ -147,7 +154,16 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -147,7 +154,16 @@ class Builder extends \Illuminate\Database\Query\Builder {
{
{
// Return distinct results directly
// Return distinct results directly
$column
=
isset
(
$this
->
columns
[
0
])
?
$this
->
columns
[
0
]
:
'_id'
;
$column
=
isset
(
$this
->
columns
[
0
])
?
$this
->
columns
[
0
]
:
'_id'
;
return
$this
->
collection
->
distinct
(
$column
,
$wheres
);
// Execute distinct
$result
=
$this
->
collection
->
distinct
(
$column
,
$wheres
);
// Log query
$this
->
connection
->
logQuery
(
$this
->
from
.
'.distinct("'
.
$column
.
'", '
.
json_encode
(
$wheres
)
.
')'
,
array
(),
$this
->
connection
->
getElapsedTime
(
$start
));
return
$result
;
}
}
// Normal query
// Normal query
...
@@ -167,6 +183,11 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -167,6 +183,11 @@ class Builder extends \Illuminate\Database\Query\Builder {
if
(
$this
->
offset
)
$cursor
->
skip
(
$this
->
offset
);
if
(
$this
->
offset
)
$cursor
->
skip
(
$this
->
offset
);
if
(
$this
->
limit
)
$cursor
->
limit
(
$this
->
limit
);
if
(
$this
->
limit
)
$cursor
->
limit
(
$this
->
limit
);
// Log query
$this
->
connection
->
logQuery
(
$this
->
from
.
'.find('
.
json_encode
(
$wheres
)
.
', '
.
json_encode
(
$columns
)
.
')'
,
array
(),
$this
->
connection
->
getElapsedTime
(
$start
));
// Return results as an array with numeric keys
// Return results as an array with numeric keys
return
iterator_to_array
(
$cursor
,
false
);
return
iterator_to_array
(
$cursor
,
false
);
}
}
...
@@ -275,6 +296,8 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -275,6 +296,8 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/
*/
public
function
insert
(
array
$values
)
public
function
insert
(
array
$values
)
{
{
$start
=
microtime
(
true
);
// Since every insert gets treated like a batch insert, we will have to detect
// Since every insert gets treated like a batch insert, we will have to detect
// if the user is inserting a single document or an array of documents.
// if the user is inserting a single document or an array of documents.
$batch
=
true
;
$batch
=
true
;
...
@@ -291,7 +314,14 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -291,7 +314,14 @@ class Builder extends \Illuminate\Database\Query\Builder {
if
(
!
$batch
)
$values
=
array
(
$values
);
if
(
!
$batch
)
$values
=
array
(
$values
);
// Batch insert
// Batch insert
return
$this
->
collection
->
batchInsert
(
$values
);
$result
=
$this
->
collection
->
batchInsert
(
$values
);
// Log query
$this
->
connection
->
logQuery
(
$this
->
from
.
'.batchInsert('
.
json_encode
(
$values
)
.
')'
,
array
(),
$this
->
connection
->
getElapsedTime
(
$start
));
return
$result
;
}
}
/**
/**
...
@@ -303,8 +333,15 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -303,8 +333,15 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/
*/
public
function
insertGetId
(
array
$values
,
$sequence
=
null
)
public
function
insertGetId
(
array
$values
,
$sequence
=
null
)
{
{
$start
=
microtime
(
true
);
$result
=
$this
->
collection
->
insert
(
$values
);
$result
=
$this
->
collection
->
insert
(
$values
);
// Log query
$this
->
connection
->
logQuery
(
$this
->
from
.
'.insert('
.
json_encode
(
$values
)
.
')'
,
array
(),
$this
->
connection
->
getElapsedTime
(
$start
));
if
(
1
==
(
int
)
$result
[
'ok'
])
if
(
1
==
(
int
)
$result
[
'ok'
])
{
{
if
(
!
$sequence
)
if
(
!
$sequence
)
...
@@ -391,7 +428,15 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -391,7 +428,15 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/
*/
public
function
delete
(
$id
=
null
)
public
function
delete
(
$id
=
null
)
{
{
$result
=
$this
->
collection
->
remove
(
$this
->
compileWheres
());
$start
=
microtime
(
true
);
$wheres
=
$this
->
compileWheres
();
$result
=
$this
->
collection
->
remove
(
$wheres
);
// Log query
$this
->
connection
->
logQuery
(
$this
->
from
.
'.remove('
.
json_encode
(
$wheres
)
.
')'
,
array
(),
$this
->
connection
->
getElapsedTime
(
$start
));
if
(
1
==
(
int
)
$result
[
'ok'
])
if
(
1
==
(
int
)
$result
[
'ok'
])
{
{
...
@@ -414,7 +459,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -414,7 +459,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
$this
->
collection
=
$this
->
connection
->
getCollection
(
$collection
);
$this
->
collection
=
$this
->
connection
->
getCollection
(
$collection
);
}
}
return
$this
;
return
parent
::
from
(
$collection
)
;
}
}
/**
/**
...
@@ -528,13 +573,21 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -528,13 +573,21 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/
*/
protected
function
performUpdate
(
$query
,
array
$options
=
array
())
protected
function
performUpdate
(
$query
,
array
$options
=
array
())
{
{
$start
=
microtime
(
true
);
// Default options
// Default options
$default
=
array
(
'multiple'
=>
true
);
$default
=
array
(
'multiple'
=>
true
);
// Merge options and override default options
// Merge options and override default options
$options
=
array_merge
(
$default
,
$options
);
$options
=
array_merge
(
$default
,
$options
);
$result
=
$this
->
collection
->
update
(
$this
->
compileWheres
(),
$query
,
$options
);
$wheres
=
$this
->
compileWheres
();
$result
=
$this
->
collection
->
update
(
$wheres
,
$query
,
$options
);
// Log query
$this
->
connection
->
logQuery
(
$this
->
from
.
'.update('
.
json_encode
(
$wheres
)
.
', '
.
json_encode
(
$query
)
.
', '
.
json_encode
(
$options
)
.
')'
,
array
(),
$this
->
connection
->
getElapsedTime
(
$start
));
if
(
1
==
(
int
)
$result
[
'ok'
])
if
(
1
==
(
int
)
$result
[
'ok'
])
{
{
...
...
src/Jenssegers/Mongodb/Connection.php
View file @
a6714df2
...
@@ -157,11 +157,22 @@ class Connection extends \Illuminate\Database\Connection {
...
@@ -157,11 +157,22 @@ class Connection extends \Illuminate\Database\Connection {
}
}
}
}
// The database name needs to be in the connection string, otherwise it will
// The database name needs to be in the connection string, otherwise it will
// authenticate to the admin database, which may result in permission errors.
// authenticate to the admin database, which may result in permission errors.
return
"mongodb://"
.
implode
(
','
,
$hosts
)
.
"/
{
$database
}
"
;
return
"mongodb://"
.
implode
(
','
,
$hosts
)
.
"/
{
$database
}
"
;
}
}
/**
* Get the elapsed time since a given starting point.
*
* @param int $start
* @return float
*/
public
function
getElapsedTime
(
$start
)
{
return
parent
::
getElapsedTime
(
$start
);
}
/**
/**
* Dynamically pass methods to the connection.
* Dynamically pass methods to the connection.
*
*
...
...
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