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
624b294c
Commit
624b294c
authored
Aug 25, 2016
by
pi0
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleanup service provider
parent
cb42e3b9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
101 additions
and
107 deletions
+101
-107
MongodbServiceProvider.php
src/Jenssegers/Mongodb/MongodbServiceProvider.php
+0
-5
Builder.php
src/Jenssegers/Mongodb/Query/Builder.php
+101
-102
No files found.
src/Jenssegers/Mongodb/MongodbServiceProvider.php
View file @
624b294c
...
...
@@ -14,11 +14,6 @@ class MongodbServiceProvider extends ServiceProvider
Model
::
setConnectionResolver
(
$this
->
app
[
'db'
]);
Model
::
setEventDispatcher
(
$this
->
app
[
'events'
]);
if
(
!
defined
(
'SHOULD_RETURN_COLLECTION'
))
{
$s
=
explode
(
'.'
,
\Illuminate\Foundation\Application
::
VERSION
);
define
(
'SHOULD_RETURN_COLLECTION'
,
(
10
*
$s
[
0
]
+
$s
[
1
])
>=
53
);
}
}
/**
...
...
src/Jenssegers/Mongodb/Query/Builder.php
View file @
624b294c
...
...
@@ -4,6 +4,7 @@ use Closure;
use
DateTime
;
use
Illuminate\Database\Query\Builder
as
BaseBuilder
;
use
Illuminate\Database\Query\Expression
;
use
Illuminate\Support\Arr
;
use
Illuminate\Support\Collection
;
use
Jenssegers\Mongodb\Connection
;
use
MongoDB\BSON\ObjectID
;
...
...
@@ -68,32 +69,41 @@ class Builder extends BaseBuilder
* @var array
*/
protected
$conversion
=
[
'='
=>
'='
,
'='
=>
'='
,
'!='
=>
'$ne'
,
'<>'
=>
'$ne'
,
'<'
=>
'$lt'
,
'<'
=>
'$lt'
,
'<='
=>
'$lte'
,
'>'
=>
'$gt'
,
'>'
=>
'$gt'
,
'>='
=>
'$gte'
,
];
/**
* Check if we need to return Collections instead of plain arrays (laravel >= 5.3 )
*
* @var boolean
*/
protected
$use_collection
;
/**
* Create a new query builder instance.
*
* @param Connection $connection
* @param Processor
$processor
* @param Processor $processor
*/
public
function
__construct
(
Connection
$connection
,
Processor
$processor
)
{
$this
->
grammar
=
new
Grammar
;
$this
->
connection
=
$connection
;
$this
->
processor
=
$processor
;
$s
=
explode
(
'.'
,
\Illuminate\Foundation\Application
::
VERSION
);
$this
->
use_collection
=
(
10
*
$s
[
0
]
+
$s
[
1
])
>=
53
;
}
/**
* Set the projections.
*
* @param array
$columns
* @param array $columns
* @return $this
*/
public
function
project
(
$columns
)
...
...
@@ -132,8 +142,8 @@ class Builder extends BaseBuilder
/**
* Execute a query for a single record by ID.
*
* @param mixed
$id
* @param array
$columns
* @param mixed $id
* @param array $columns
* @return mixed
*/
public
function
find
(
$id
,
$columns
=
[])
...
...
@@ -144,7 +154,7 @@ class Builder extends BaseBuilder
/**
* Execute the query as a "select" statement.
*
* @param array
$columns
* @param array $columns
* @return array|static[]|Collection
*/
public
function
get
(
$columns
=
[])
...
...
@@ -155,7 +165,7 @@ class Builder extends BaseBuilder
/**
* Execute the query as a fresh "select" statement.
*
* @param array
$columns
* @param array $columns
* @return array|static[]|Collection
*/
public
function
getFresh
(
$columns
=
[])
...
...
@@ -206,8 +216,7 @@ class Builder extends BaseBuilder
// Translate count into sum.
if
(
$function
==
'count'
)
{
$group
[
'aggregate'
]
=
[
'$sum'
=>
1
];
}
// Pass other functions directly.
}
// Pass other functions directly.
else
{
$group
[
'aggregate'
]
=
[
'$'
.
$function
=>
'$'
.
$column
];
}
...
...
@@ -258,10 +267,8 @@ class Builder extends BaseBuilder
$results
=
iterator_to_array
(
$this
->
collection
->
aggregate
(
$pipeline
,
$options
));
// Return results
return
SHOULD_RETURN_COLLECTION
?
new
Collection
(
$results
)
:
$results
;
}
// Distinct query
return
$this
->
use_collection
?
new
Collection
(
$results
)
:
$results
;
}
// Distinct query
elseif
(
$this
->
distinct
)
{
// Return distinct results directly
$column
=
isset
(
$this
->
columns
[
0
])
?
$this
->
columns
[
0
]
:
'_id'
;
...
...
@@ -273,10 +280,8 @@ class Builder extends BaseBuilder
$result
=
$this
->
collection
->
distinct
(
$column
);
}
return
SHOULD_RETURN_COLLECTION
?
new
Collection
(
$result
)
:
$result
;
}
// Normal query
return
$this
->
use_collection
?
new
Collection
(
$result
)
:
$result
;
}
// Normal query
else
{
$columns
=
[];
...
...
@@ -317,7 +322,7 @@ class Builder extends BaseBuilder
// Return results as an array with numeric keys
$results
=
iterator_to_array
(
$cursor
,
false
);
return
SHOULD_RETURN_COLLECTION
?
new
Collection
(
$results
)
:
$results
;
return
$this
->
use_collection
?
new
Collection
(
$results
)
:
$results
;
}
}
...
...
@@ -331,13 +336,13 @@ class Builder extends BaseBuilder
$key
=
[
'connection'
=>
$this
->
collection
->
getDatabaseName
(),
'collection'
=>
$this
->
collection
->
getCollectionName
(),
'wheres'
=>
$this
->
wheres
,
'columns'
=>
$this
->
columns
,
'groups'
=>
$this
->
groups
,
'orders'
=>
$this
->
orders
,
'offset'
=>
$this
->
offset
,
'limit'
=>
$this
->
limit
,
'aggregate'
=>
$this
->
aggregate
,
'wheres'
=>
$this
->
wheres
,
'columns'
=>
$this
->
columns
,
'groups'
=>
$this
->
groups
,
'orders'
=>
$this
->
orders
,
'offset'
=>
$this
->
offset
,
'limit'
=>
$this
->
limit
,
'aggregate'
=>
$this
->
aggregate
,
];
return
md5
(
serialize
(
array_values
(
$key
)));
...
...
@@ -346,8 +351,8 @@ class Builder extends BaseBuilder
/**
* Execute an aggregate function on the database.
*
* @param string
$function
* @param array
$columns
* @param string $function
* @param array $columns
* @return mixed
*/
public
function
aggregate
(
$function
,
$columns
=
[])
...
...
@@ -363,7 +368,7 @@ class Builder extends BaseBuilder
$this
->
aggregate
=
null
;
if
(
isset
(
$results
[
0
]))
{
$result
=
(
array
)
$results
[
0
];
$result
=
(
array
)
$results
[
0
];
return
$result
[
'aggregate'
];
}
...
...
@@ -376,7 +381,7 @@ class Builder extends BaseBuilder
*/
public
function
exists
()
{
return
!
is_null
(
$this
->
first
());
return
!
is_null
(
$this
->
first
());
}
/**
...
...
@@ -398,8 +403,8 @@ class Builder extends BaseBuilder
/**
* Add an "order by" clause to the query.
*
* @param string
$column
* @param string
$direction
* @param string $column
* @param string $direction
* @return Builder
*/
public
function
orderBy
(
$column
,
$direction
=
'asc'
)
...
...
@@ -420,10 +425,10 @@ class Builder extends BaseBuilder
/**
* Add a where between statement to the query.
*
* @param string
$column
* @param array
$values
* @param string
$boolean
* @param bool
$not
* @param string $column
* @param array $values
* @param string $boolean
* @param bool $not
* @return Builder
*/
public
function
whereBetween
(
$column
,
array
$values
,
$boolean
=
'and'
,
$not
=
false
)
...
...
@@ -438,8 +443,8 @@ class Builder extends BaseBuilder
/**
* Set the limit and offset for a given page.
*
* @param int
$page
* @param int
$perPage
* @param int $page
* @param int $perPage
* @return \Illuminate\Database\Query\Builder|static
*/
public
function
forPage
(
$page
,
$perPage
=
15
)
...
...
@@ -452,7 +457,7 @@ class Builder extends BaseBuilder
/**
* Insert a new record into the database.
*
* @param array
$values
* @param array $values
* @return bool
*/
public
function
insert
(
array
$values
)
...
...
@@ -464,34 +469,34 @@ class Builder extends BaseBuilder
foreach
(
$values
as
$value
)
{
// As soon as we find a value that is not an array we assume the user is
// inserting a single document.
if
(
!
is_array
(
$value
))
{
if
(
!
is_array
(
$value
))
{
$batch
=
false
;
break
;
}
}
if
(
!
$batch
)
{
if
(
!
$batch
)
{
$values
=
[
$values
];
}
// Batch insert
$result
=
$this
->
collection
->
insertMany
(
$values
);
return
(
1
==
(
int
)
$result
->
isAcknowledged
());
return
(
1
==
(
int
)
$result
->
isAcknowledged
());
}
/**
* Insert a new record and get the value of the primary key.
*
* @param array
$values
* @param string
$sequence
* @param array $values
* @param string $sequence
* @return int
*/
public
function
insertGetId
(
array
$values
,
$sequence
=
null
)
{
$result
=
$this
->
collection
->
insertOne
(
$values
);
if
(
1
==
(
int
)
$result
->
isAcknowledged
())
{
if
(
1
==
(
int
)
$result
->
isAcknowledged
())
{
if
(
is_null
(
$sequence
))
{
$sequence
=
'_id'
;
}
...
...
@@ -504,14 +509,14 @@ class Builder extends BaseBuilder
/**
* Update a record in the database.
*
* @param array
$values
* @param array
$options
* @param array $values
* @param array $options
* @return int
*/
public
function
update
(
array
$values
,
array
$options
=
[])
{
// Use $set as default operator.
if
(
!
starts_with
(
key
(
$values
),
'$'
))
{
if
(
!
starts_with
(
key
(
$values
),
'$'
))
{
$values
=
[
'$set'
=>
$values
];
}
...
...
@@ -521,16 +526,16 @@ class Builder extends BaseBuilder
/**
* Increment a column's value by a given amount.
*
* @param string
$column
* @param int
$amount
* @param array
$extra
* @param string $column
* @param int $amount
* @param array $extra
* @return int
*/
public
function
increment
(
$column
,
$amount
=
1
,
array
$extra
=
[],
array
$options
=
[])
{
$query
=
[
'$inc'
=>
[
$column
=>
$amount
]];
if
(
!
empty
(
$extra
))
{
if
(
!
empty
(
$extra
))
{
$query
[
'$set'
]
=
$extra
;
}
...
...
@@ -547,9 +552,9 @@ class Builder extends BaseBuilder
/**
* Decrement a column's value by a given amount.
*
* @param string
$column
* @param int
$amount
* @param array
$extra
* @param string $column
* @param int $amount
* @param array $extra
* @return int
*/
public
function
decrement
(
$column
,
$amount
=
1
,
array
$extra
=
[],
array
$options
=
[])
...
...
@@ -560,8 +565,8 @@ class Builder extends BaseBuilder
/**
* Get an array with the values of a given column.
*
* @param string
$column
* @param string|null
$key
* @param string $column
* @param string|null $key
* @return array
*/
public
function
pluck
(
$column
,
$key
=
null
)
...
...
@@ -574,14 +579,14 @@ class Builder extends BaseBuilder
/**
* Delete a record from the database.
*
* @param mixed
$id
* @param mixed $id
* @return int
*/
public
function
delete
(
$id
=
null
)
{
$wheres
=
$this
->
compileWheres
();
$result
=
$this
->
collection
->
DeleteMany
(
$wheres
);
if
(
1
==
(
int
)
$result
->
isAcknowledged
())
{
if
(
1
==
(
int
)
$result
->
isAcknowledged
())
{
return
$result
->
getDeletedCount
();
}
...
...
@@ -591,7 +596,7 @@ class Builder extends BaseBuilder
/**
* Set the collection which the query is targeting.
*
* @param string
$collection
* @param string $collection
* @return Builder
*/
public
function
from
(
$collection
)
...
...
@@ -610,15 +615,15 @@ class Builder extends BaseBuilder
{
$result
=
$this
->
collection
->
drop
();
return
(
1
==
(
int
)
$result
->
ok
);
return
(
1
==
(
int
)
$result
->
ok
);
}
/**
* Get an array with the values of a given column.
*
* @deprecated
* @param string
$column
* @param string
$key
* @param string $column
* @param string $key
* @return array
*/
public
function
lists
(
$column
,
$key
=
null
)
...
...
@@ -628,7 +633,7 @@ class Builder extends BaseBuilder
// Convert ObjectID's to strings so that lists can do its work.
$results
=
$results
->
map
(
function
(
$item
)
{
$item
[
'_id'
]
=
(
string
)
$item
[
'_id'
];
$item
[
'_id'
]
=
(
string
)
$item
[
'_id'
];
return
$item
;
});
...
...
@@ -642,7 +647,7 @@ class Builder extends BaseBuilder
/**
* Create a raw database expression.
*
* @param closure
$expression
* @param closure $expression
* @return mixed
*/
public
function
raw
(
$expression
=
null
)
...
...
@@ -650,10 +655,8 @@ class Builder extends BaseBuilder
// Execute the closure on the mongodb collection
if
(
$expression
instanceof
Closure
)
{
return
call_user_func
(
$expression
,
$this
->
collection
);
}
// Create an expression for the given value
elseif
(
!
is_null
(
$expression
))
{
}
// Create an expression for the given value
elseif
(
!
is_null
(
$expression
))
{
return
new
Expression
(
$expression
);
}
...
...
@@ -664,8 +667,8 @@ class Builder extends BaseBuilder
/**
* Append one or more values to an array.
*
* @param mixed
$column
* @param mixed
$value
* @param mixed $column
* @param mixed $value
* @return int
*/
public
function
push
(
$column
,
$value
=
null
,
$unique
=
false
)
...
...
@@ -690,8 +693,8 @@ class Builder extends BaseBuilder
/**
* Remove one or more values from an array.
*
* @param mixed
$column
* @param mixed
$value
* @param mixed $column
* @param mixed $value
* @return int
*/
public
function
pull
(
$column
,
$value
=
null
)
...
...
@@ -719,7 +722,7 @@ class Builder extends BaseBuilder
*/
public
function
drop
(
$columns
)
{
if
(
!
is_array
(
$columns
))
{
if
(
!
is_array
(
$columns
))
{
$columns
=
[
$columns
];
}
...
...
@@ -747,20 +750,20 @@ class Builder extends BaseBuilder
/**
* Perform an update query.
*
* @param array
$query
* @param array
$options
* @param array $query
* @param array $options
* @return int
*/
protected
function
performUpdate
(
$query
,
array
$options
=
[])
{
// Update multiple items by default.
if
(
!
array_key_exists
(
'multiple'
,
$options
))
{
if
(
!
array_key_exists
(
'multiple'
,
$options
))
{
$options
[
'multiple'
]
=
true
;
}
$wheres
=
$this
->
compileWheres
();
$result
=
$this
->
collection
->
UpdateMany
(
$wheres
,
$query
,
$options
);
if
(
1
==
(
int
)
$result
->
isAcknowledged
())
{
if
(
1
==
(
int
)
$result
->
isAcknowledged
())
{
return
$result
->
getModifiedCount
()
?
$result
->
getModifiedCount
()
:
$result
->
getUpsertedCount
();
}
...
...
@@ -785,10 +788,10 @@ class Builder extends BaseBuilder
/**
* Add a basic where clause to the query.
*
* @param string
$column
* @param string
$operator
* @param mixed
$value
* @param string
$boolean
* @param string $column
* @param string $operator
* @param mixed $value
* @param string $boolean
* @return \Illuminate\Database\Query\Builder|static
*
* @throws \InvalidArgumentException
...
...
@@ -829,14 +832,14 @@ class Builder extends BaseBuilder
// Operator conversions
$convert
=
[
'regexp'
=>
'regex'
,
'elemmatch'
=>
'elemMatch'
,
'regexp'
=>
'regex'
,
'elemmatch'
=>
'elemMatch'
,
'geointersects'
=>
'geoIntersects'
,
'geowithin'
=>
'geoWithin'
,
'nearsphere'
=>
'nearSphere'
,
'maxdistance'
=>
'maxDistance'
,
'centersphere'
=>
'centerSphere'
,
'uniquedocs'
=>
'uniqueDocs'
,
'geowithin'
=>
'geoWithin'
,
'nearsphere'
=>
'nearSphere'
,
'maxdistance'
=>
'maxDistance'
,
'centersphere'
=>
'centerSphere'
,
'uniquedocs'
=>
'uniqueDocs'
,
];
if
(
array_key_exists
(
$where
[
'operator'
],
$convert
))
{
...
...
@@ -851,9 +854,7 @@ class Builder extends BaseBuilder
foreach
(
$where
[
'values'
]
as
&
$value
)
{
$value
=
$this
->
convertKey
(
$value
);
}
}
// Single value.
}
// Single value.
elseif
(
isset
(
$where
[
'value'
]))
{
$where
[
'value'
]
=
$this
->
convertKey
(
$where
[
'value'
]);
}
...
...
@@ -905,20 +906,18 @@ class Builder extends BaseBuilder
$regex
=
preg_replace
(
'#(^|[^\\\])%#'
,
'$1.*'
,
preg_quote
(
$value
));
// Convert like to regular expression.
if
(
!
starts_with
(
$value
,
'%'
))
{
if
(
!
starts_with
(
$value
,
'%'
))
{
$regex
=
'^'
.
$regex
;
}
if
(
!
ends_with
(
$value
,
'%'
))
{
if
(
!
ends_with
(
$value
,
'%'
))
{
$regex
=
$regex
.
'$'
;
}
$value
=
new
Regex
(
$regex
,
'i'
);
}
// Manipulate regexp operations.
}
// Manipulate regexp operations.
elseif
(
in_array
(
$operator
,
[
'regexp'
,
'not regexp'
,
'regex'
,
'not regex'
]))
{
// Automatically convert regular expression strings to Regex objects.
if
(
!
$value
instanceof
Regex
)
{
if
(
!
$value
instanceof
Regex
)
{
$e
=
explode
(
'/'
,
$value
);
$flag
=
end
(
$e
);
$regstr
=
substr
(
$value
,
1
,
-
(
strlen
(
$flag
)
+
1
));
...
...
@@ -932,7 +931,7 @@ class Builder extends BaseBuilder
}
}
if
(
!
isset
(
$operator
)
or
$operator
==
'='
)
{
if
(
!
isset
(
$operator
)
or
$operator
==
'='
)
{
$query
=
[
$column
=>
$value
];
}
elseif
(
array_key_exists
(
$operator
,
$this
->
conversion
))
{
$query
=
[
$column
=>
[
$this
->
conversion
[
$operator
]
=>
$value
]];
...
...
@@ -1017,8 +1016,8 @@ class Builder extends BaseBuilder
/**
* Handle dynamic method calls into the method.
*
* @param string
$method
* @param array
$parameters
* @param string $method
* @param array $parameters
* @return mixed
*/
public
function
__call
(
$method
,
$parameters
)
...
...
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