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
2dcb4595
Commit
2dcb4595
authored
Jun 17, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use wrapper for MongoCollection, fixes #209
parent
1c4df65a
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
75 additions
and
51 deletions
+75
-51
Collection.php
src/Jenssegers/Mongodb/Collection.php
+68
-0
Connection.php
src/Jenssegers/Mongodb/Connection.php
+4
-3
Builder.php
src/Jenssegers/Mongodb/Query/Builder.php
+0
-45
ConnectionTest.php
tests/ConnectionTest.php
+1
-1
QueryBuilderTest.php
tests/QueryBuilderTest.php
+2
-2
No files found.
src/Jenssegers/Mongodb/Collection.php
0 → 100644
View file @
2dcb4595
<?php
namespace
Jenssegers\Mongodb
;
use
Exception
;
use
MongoCollection
;
use
Jenssegers\Mongodb\Connection
;
class
Collection
{
/**
* The connection instance.
*
* @var Connection
*/
protected
$connection
;
/**
* The MongoCollection instance..
*
* @var MongoCollection
*/
protected
$collection
;
/**
* Constructor.
*/
public
function
__construct
(
Connection
$connection
,
MongoCollection
$collection
)
{
$this
->
connection
=
$connection
;
$this
->
collection
=
$collection
;
}
/**
* Handle dynamic method calls.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public
function
__call
(
$method
,
$parameters
)
{
// Build the query string.
$query
=
$parameters
;
foreach
(
$query
as
&
$param
)
{
try
{
$param
=
json_encode
(
$param
);
}
catch
(
Exception
$e
)
{
$param
=
'{...}'
;
}
}
$start
=
microtime
(
true
);
// Execute the query.
$result
=
call_user_func_array
(
array
(
$this
->
collection
,
$method
),
$parameters
);
// Log the query.
$this
->
connection
->
logQuery
(
$this
->
collection
->
getName
()
.
'.'
.
$method
.
'('
.
join
(
','
,
$query
)
.
')'
,
array
(),
$this
->
connection
->
getElapsedTime
(
$start
));
return
$result
;
}
}
src/Jenssegers/Mongodb/Connection.php
View file @
2dcb4595
<?php
namespace
Jenssegers\Mongodb
;
<?php
namespace
Jenssegers\Mongodb
;
use
Jenssegers\Mongodb\Collection
;
use
Jenssegers\Mongodb\Query\Builder
as
QueryBuilder
;
use
Jenssegers\Mongodb\Query\Builder
as
QueryBuilder
;
use
MongoClient
;
use
MongoClient
;
...
@@ -8,14 +9,14 @@ class Connection extends \Illuminate\Database\Connection {
...
@@ -8,14 +9,14 @@ class Connection extends \Illuminate\Database\Connection {
/**
/**
* The MongoDB database handler.
* The MongoDB database handler.
*
*
* @var
resource
* @var
MongoDB
*/
*/
protected
$db
;
protected
$db
;
/**
/**
* The MongoClient connection handler.
* The MongoClient connection handler.
*
*
* @var
resource
* @var
MongoClient
*/
*/
protected
$connection
;
protected
$connection
;
...
@@ -74,7 +75,7 @@ class Connection extends \Illuminate\Database\Connection {
...
@@ -74,7 +75,7 @@ class Connection extends \Illuminate\Database\Connection {
*/
*/
public
function
getCollection
(
$name
)
public
function
getCollection
(
$name
)
{
{
return
$this
->
db
->
{
$name
}
;
return
new
Collection
(
$this
,
$this
->
db
->
selectCollection
(
$name
))
;
}
}
/**
/**
...
...
src/Jenssegers/Mongodb/Query/Builder.php
View file @
2dcb4595
...
@@ -79,8 +79,6 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -79,8 +79,6 @@ 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.
...
@@ -161,11 +159,6 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -161,11 +159,6 @@ 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'
];
}
}
...
@@ -179,11 +172,6 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -179,11 +172,6 @@ class Builder extends \Illuminate\Database\Query\Builder {
// Execute distinct
// Execute distinct
$result
=
$this
->
collection
->
distinct
(
$column
,
$wheres
);
$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
;
return
$result
;
}
}
...
@@ -204,11 +192,6 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -204,11 +192,6 @@ 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
);
}
}
...
@@ -327,8 +310,6 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -327,8 +310,6 @@ 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
;
...
@@ -347,11 +328,6 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -347,11 +328,6 @@ class Builder extends \Illuminate\Database\Query\Builder {
// Batch insert
// Batch insert
$result
=
$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
(
1
==
(
int
)
$result
[
'ok'
]);
return
(
1
==
(
int
)
$result
[
'ok'
]);
}
}
...
@@ -364,15 +340,8 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -364,15 +340,8 @@ 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
)
...
@@ -467,16 +436,9 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -467,16 +436,9 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/
*/
public
function
delete
(
$id
=
null
)
public
function
delete
(
$id
=
null
)
{
{
$start
=
microtime
(
true
);
$wheres
=
$this
->
compileWheres
();
$wheres
=
$this
->
compileWheres
();
$result
=
$this
->
collection
->
remove
(
$wheres
);
$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'
])
{
{
return
$result
[
'n'
];
return
$result
[
'n'
];
...
@@ -623,8 +585,6 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -623,8 +585,6 @@ 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
);
...
@@ -634,11 +594,6 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -634,11 +594,6 @@ class Builder extends \Illuminate\Database\Query\Builder {
$wheres
=
$this
->
compileWheres
();
$wheres
=
$this
->
compileWheres
();
$result
=
$this
->
collection
->
update
(
$wheres
,
$query
,
$options
);
$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'
])
{
{
return
$result
[
'n'
];
return
$result
[
'n'
];
...
...
tests/ConnectionTest.php
View file @
2dcb4595
...
@@ -31,7 +31,7 @@ class ConnectionTest extends TestCase {
...
@@ -31,7 +31,7 @@ class ConnectionTest extends TestCase {
public
function
testCollection
()
public
function
testCollection
()
{
{
$collection
=
DB
::
connection
(
'mongodb'
)
->
getCollection
(
'unittest'
);
$collection
=
DB
::
connection
(
'mongodb'
)
->
getCollection
(
'unittest'
);
$this
->
assertInstanceOf
(
'
Mongo
Collection'
,
$collection
);
$this
->
assertInstanceOf
(
'
Jenssegers\Mongodb\
Collection'
,
$collection
);
$collection
=
DB
::
connection
(
'mongodb'
)
->
collection
(
'unittests'
);
$collection
=
DB
::
connection
(
'mongodb'
)
->
collection
(
'unittests'
);
$this
->
assertInstanceOf
(
'Jenssegers\Mongodb\Query\Builder'
,
$collection
);
$this
->
assertInstanceOf
(
'Jenssegers\Mongodb\Query\Builder'
,
$collection
);
...
...
tests/QueryBuilderTest.php
View file @
2dcb4595
...
@@ -188,10 +188,10 @@ class QueryBuilderTest extends TestCase {
...
@@ -188,10 +188,10 @@ class QueryBuilderTest extends TestCase {
$this
->
assertEquals
(
1
,
$cursor
->
count
());
$this
->
assertEquals
(
1
,
$cursor
->
count
());
$collection
=
DB
::
collection
(
'users'
)
->
raw
();
$collection
=
DB
::
collection
(
'users'
)
->
raw
();
$this
->
assertInstanceOf
(
'
Mongo
Collection'
,
$collection
);
$this
->
assertInstanceOf
(
'
Jenssegers\Mongodb\
Collection'
,
$collection
);
$collection
=
User
::
raw
();
$collection
=
User
::
raw
();
$this
->
assertInstanceOf
(
'
Mongo
Collection'
,
$collection
);
$this
->
assertInstanceOf
(
'
Jenssegers\Mongodb\
Collection'
,
$collection
);
$results
=
DB
::
collection
(
'users'
)
->
whereRaw
(
array
(
'age'
=>
20
))
->
get
();
$results
=
DB
::
collection
(
'users'
)
->
whereRaw
(
array
(
'age'
=>
20
))
->
get
();
$this
->
assertEquals
(
1
,
count
(
$results
));
$this
->
assertEquals
(
1
,
count
(
$results
));
...
...
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