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
42b6cb95
Commit
42b6cb95
authored
May 25, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Removed backup source folder
parent
e80273b4
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
0 additions
and
782 deletions
+0
-782
Connection.php
srcb/Jenssegers/Mongodb/Connection.php
+0
-104
ConnectionResolver.php
srcb/Jenssegers/Mongodb/ConnectionResolver.php
+0
-100
Model.php
srcb/Jenssegers/Mongodb/Model.php
+0
-81
MongodbServiceProvider.php
srcb/Jenssegers/Mongodb/MongodbServiceProvider.php
+0
-35
Query.php
srcb/Jenssegers/Mongodb/Query.php
+0
-462
No files found.
srcb/Jenssegers/Mongodb/Connection.php
deleted
100644 → 0
View file @
e80273b4
<?php
namespace
Jenssegers\Mongodb
;
class
Connection
{
/**
* The MongoDB database handler.
*
* @var resource
*/
protected
$db
;
/**
* The MongoClient connection handler.
*
* @var resource
*/
protected
$connection
;
/**
* The database connection configuration options.
*
* @var string
*/
protected
$config
=
array
();
/**
* Create a new database connection instance.
*
* @param array $config
* @return void
*/
public
function
__construct
(
array
$config
)
{
if
(
!
is_null
(
$this
->
connection
))
return
;
// Store configuration
$this
->
config
=
$config
;
// Check for connection options
$options
=
array_get
(
$config
,
'options'
,
array
());
// Create connection
$this
->
connection
=
new
\MongoClient
(
$this
->
getDsn
(
$config
),
$options
);
// Select database
$this
->
db
=
$this
->
connection
->
{
$config
[
'database'
]};
return
$this
;
}
/**
* Get a mongodb collection.
*
* @param string $name
* @return MongoDB
*/
public
function
getCollection
(
$name
)
{
return
$this
->
db
->
{
$name
};
}
/**
* Get the mongodb database object.
*
* @return MongoDB
*/
public
function
getDb
()
{
return
$this
->
db
;
}
/**
* Create a DSN string from a configuration.
*
* @param array $config
* @return string
*/
protected
function
getDsn
(
array
$config
)
{
// First we will create the basic DSN setup as well as the port if it is in
// in the configuration options. This will give us the basic DSN we will
// need to establish the MongoClient and return them back for use.
extract
(
$config
);
$dsn
=
"mongodb://"
;
if
(
isset
(
$config
[
'username'
])
and
isset
(
$config
[
'password'
]))
{
$dsn
.=
"
{
$username
}
:
{
$password
}
@"
;
}
$dsn
.=
"
{
$host
}
"
;
if
(
isset
(
$config
[
'port'
]))
{
$dsn
.=
":
{
$port
}
"
;
}
$dsn
.=
"/
{
$database
}
"
;
return
$dsn
;
}
}
\ No newline at end of file
srcb/Jenssegers/Mongodb/ConnectionResolver.php
deleted
100644 → 0
View file @
e80273b4
<?php
namespace
Jenssegers\Mongodb
;
use
Illuminate\Database\ConnectionResolverInterface
;
use
Jenssegers\Mongodb\Connection
;
class
ConnectionResolver
implements
ConnectionResolverInterface
{
/**
* The application instance.
*
* @var Illuminate\Foundation\Application
*/
protected
$app
;
/**
* All of the registered connections.
*
* @var array
*/
protected
$connections
=
array
();
/**
* The default connection name.
*
* @var string
*/
protected
$default
=
'mongodb'
;
/**
* Create a new database manager instance.
*
* @param Illuminate\Foundation\Application $app
* @return void
*/
public
function
__construct
(
$app
)
{
$this
->
app
=
$app
;
}
/**
* Get a database connection instance.
*
* @param string $name
* @return Connection
*/
public
function
connection
(
$name
=
null
)
{
if
(
is_null
(
$name
))
$name
=
$this
->
getDefaultConnection
();
// If we haven't created this connection, we'll create it based on the config
// provided in the application.
if
(
!
isset
(
$this
->
connections
[
$name
]))
{
// Get connection configuration
$connections
=
$this
->
app
[
'config'
][
'database'
][
'connections'
];
if
(
is_null
(
$config
=
array_get
(
$connections
,
$name
)))
{
throw
new
\InvalidArgumentException
(
"MongoDB [
$name
] not configured."
);
}
// Make connection
$this
->
connections
[
$name
]
=
$this
->
makeConnection
(
$connections
[
$name
]);
}
return
$this
->
connections
[
$name
];
}
/**
* Create a connection.
*
* @param array $config
*/
public
function
makeConnection
(
$config
)
{
return
new
Connection
(
$config
);
}
/**
* Get the default connection name.
*
* @return string
*/
public
function
getDefaultConnection
()
{
return
$this
->
default
;
}
/**
* Set the default connection name.
*
* @param string $name
* @return void
*/
public
function
setDefaultConnection
(
$name
)
{
$this
->
default
=
$name
;
}
}
\ No newline at end of file
srcb/Jenssegers/Mongodb/Model.php
deleted
100644 → 0
View file @
e80273b4
<?php
namespace
Jenssegers\Mongodb
;
use
Illuminate\Database\ConnectionResolverInterface
as
Resolver
;
use
Illuminate\Database\Eloquent\Collection
;
use
Jenssegers\Mongodb\Query
as
QueryBuilder
;
use
DateTime
;
use
MongoDate
;
abstract
class
Model
extends
\Illuminate\Database\Eloquent\Model
{
/**
* The collection associated with the model.
*
* @var string
*/
protected
$collection
;
/**
* The primary key for the model.
*
* @var string
*/
protected
$primaryKey
=
'_id'
;
/**
* Convert a DateTime to a storable string.
*
* @param DateTime $value
* @return MongoDate
*/
protected
function
fromDateTime
(
DateTime
$value
)
{
return
new
MongoDate
(
$value
->
getTimestamp
());
}
/**
* Return a timestamp as DateTime object.
*
* @param mixed $value
* @return DateTime
*/
protected
function
asDateTime
(
$value
)
{
if
(
$value
instanceof
MongoDate
)
{
$value
=
$value
->
sec
;
}
if
(
is_int
(
$value
))
{
$value
=
"@
$value
"
;
}
return
new
DateTime
(
$value
);
}
/**
* Get the table associated with the model.
*
* @return string
*/
public
function
getTable
()
{
if
(
isset
(
$this
->
collection
))
return
$this
->
collection
;
return
parent
::
getTable
();
}
/**
* Get a new query builder instance for the connection.
*
* @return Builder
*/
protected
function
newBaseQueryBuilder
()
{
$connection
=
$this
->
getConnection
();
return
new
QueryBuilder
(
$connection
);
}
}
\ No newline at end of file
srcb/Jenssegers/Mongodb/MongodbServiceProvider.php
deleted
100644 → 0
View file @
e80273b4
<?php
namespace
Jenssegers\Mongodb
;
use
Jenssegers\Mongodb\Model
;
use
Jenssegers\Mongodb\ConnectionResolver
;
use
Illuminate\Support\ServiceProvider
;
class
MongodbServiceProvider
extends
ServiceProvider
{
/**
* Bootstrap the application events.
*
* @return void
*/
public
function
boot
()
{
Model
::
setConnectionResolver
(
$this
->
app
[
'db.mongodb'
]);
}
/**
* Register the service provider.
*
* @return void
*/
public
function
register
()
{
// The database manager is used to resolve various connections, since multiple
// connections might be managed. It also implements the connection resolver
// interface which may be used by other components requiring connections.
$this
->
app
[
'db.mongodb'
]
=
$this
->
app
->
share
(
function
(
$app
)
{
return
new
ConnectionResolver
(
$app
);
});
}
}
\ No newline at end of file
srcb/Jenssegers/Mongodb/Query.php
deleted
100644 → 0
View file @
e80273b4
<?php
namespace
Jenssegers\Mongodb
;
use
MongoID
;
use
MongoRegex
;
class
Query
extends
\Illuminate\Database\Query\Builder
{
/**
* The database collection
*
* @var string
*/
public
$collection
;
/**
* All of the available operators.
*
* @var array
*/
protected
$conversion
=
array
(
'='
=>
'='
,
'!='
=>
'$ne'
,
'<'
=>
'$lt'
,
'<='
=>
'$lte'
,
'>'
=>
'$gt'
,
'>='
=>
'$gte'
,
);
/**
* Create a new query builder instance.
*
* @param Connection $connection
* @return void
*/
public
function
__construct
(
Connection
$connection
)
{
$this
->
connection
=
$connection
;
}
/**
* Execute a query for a single record by ID.
*
* @param int $id
* @param array $columns
* @return mixed
*/
public
function
find
(
$id
,
$columns
=
array
(
'*'
))
{
return
$this
->
where
(
'_id'
,
'='
,
new
MongoID
((
string
)
$id
))
->
first
(
$columns
);
}
/**
* Execute the query as a "select" statement.
*
* @param array $columns
* @return array
*/
public
function
get
(
$columns
=
array
(
'*'
))
{
// 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
// all of the columns on the table using the "wildcard" column character.
if
(
is_null
(
$this
->
columns
))
{
$this
->
columns
=
$columns
;
}
// Drop all columns if * is present
if
(
in_array
(
'*'
,
$this
->
columns
))
{
$this
->
columns
=
array
();
}
// Compile wheres
$wheres
=
$this
->
compileWheres
();
// Use aggregation framework if needed
if
(
$this
->
groups
||
$this
->
aggregate
)
{
$pipeline
=
array
();
$group
=
array
();
// Grouping
if
(
$this
->
groups
)
{
foreach
(
$this
->
groups
as
$column
)
{
$group
[
'_id'
][
$column
]
=
'$'
.
$column
;
$group
[
$column
]
=
array
(
'$last'
=>
'$'
.
$column
);
}
}
else
{
$group
[
'_id'
]
=
0
;
}
// Columns
foreach
(
$this
->
columns
as
$column
)
{
$group
[
$column
]
=
array
(
'$last'
=>
'$'
.
$column
);
}
// Apply aggregation functions
if
(
$this
->
aggregate
)
{
$function
=
$this
->
aggregate
[
'function'
];
foreach
(
$this
->
aggregate
[
'columns'
]
as
$column
)
{
if
(
$function
==
'count'
)
{
$group
[
$column
]
=
array
(
'$sum'
=>
1
);
}
else
{
$group
[
$column
]
=
array
(
'$'
.
$function
=>
'$'
.
$column
);
}
}
}
if
(
$wheres
)
$pipeline
[]
=
array
(
'$match'
=>
$wheres
);
$pipeline
[]
=
array
(
'$group'
=>
$group
);
// Apply order and limit
if
(
$this
->
orders
)
$pipeline
[]
=
array
(
'$sort'
=>
$this
->
orders
);
if
(
$this
->
limit
)
$pipeline
[]
=
array
(
'$limit'
=>
$this
->
limit
);
$results
=
$this
->
collection
->
aggregate
(
$pipeline
);
// Return results
return
$results
[
'result'
];
}
else
{
// Execute distinct
if
(
$this
->
distinct
)
{
$column
=
isset
(
$this
->
columns
[
0
])
?
$this
->
columns
[
0
]
:
'_id'
;
return
$this
->
collection
->
distinct
(
$column
,
$wheres
);
}
// Columns
$columns
=
array
();
foreach
(
$this
->
columns
as
$column
)
{
$columns
[
$column
]
=
true
;
}
// Get the MongoCursor
$cursor
=
$this
->
collection
->
find
(
$wheres
,
$columns
);
// Apply order, offset and limit
if
(
$this
->
orders
)
$cursor
->
sort
(
$this
->
orders
);
if
(
$this
->
offset
)
$cursor
->
skip
(
$this
->
offset
);
if
(
$this
->
limit
)
$cursor
->
limit
(
$this
->
limit
);
// Return results
return
iterator_to_array
(
$cursor
);
}
}
/**
* Execute an aggregate function on the database.
*
* @param string $function
* @param array $columns
* @return mixed
*/
public
function
aggregate
(
$function
,
$columns
=
array
(
'*'
))
{
$this
->
aggregate
=
compact
(
'function'
,
'columns'
);
$results
=
$this
->
get
(
$columns
);
if
(
isset
(
$results
[
0
]))
{
return
$results
[
0
][
$columns
[
0
]];
}
}
/**
* Force the query to only return distinct results.
*
* @return Builder
*/
public
function
distinct
(
$column
=
false
)
{
$this
->
distinct
=
true
;
if
(
$column
)
{
$this
->
columns
=
array
(
$column
);
}
return
$this
;
}
/**
* Add an "order by" clause to the query.
*
* @param string $column
* @param string $direction
* @return Builder
*/
public
function
orderBy
(
$column
,
$direction
=
'asc'
)
{
$this
->
orders
[
$column
]
=
(
$direction
==
'asc'
?
1
:
-
1
);
return
$this
;
}
/**
* Add a where between statement to the query.
*
* @param string $column
* @param array $values
* @param string $boolean
* @return \Illuminate\Database\Query\Builder
*/
public
function
whereBetween
(
$column
,
array
$values
,
$boolean
=
'and'
)
{
$type
=
'between'
;
$this
->
wheres
[]
=
compact
(
'column'
,
'type'
,
'boolean'
,
'values'
);
$this
->
bindings
=
array_merge
(
$this
->
bindings
,
$values
);
return
$this
;
}
/**
* Insert a new record into the database.
*
* @param array $values
* @return bool
*/
public
function
insert
(
array
$values
)
{
$result
=
$this
->
collection
->
insert
(
$values
);
return
(
1
==
(
int
)
$result
[
'ok'
]);
}
/**
* Insert a new record and get the value of the primary key.
*
* @param array $values
* @param string $sequence
* @return int
*/
public
function
insertGetId
(
array
$values
,
$sequence
=
null
)
{
$result
=
$this
->
collection
->
insert
(
$values
);
if
(
1
==
(
int
)
$result
[
'ok'
])
{
return
$values
[
'_id'
];
}
}
/**
* Update a record in the database.
*
* @param array $values
* @return int
*/
public
function
update
(
array
$values
)
{
$update
=
array
(
'$set'
=>
$values
);
$result
=
$this
->
collection
->
update
(
$this
->
compileWheres
(),
$update
,
array
(
'multiple'
=>
true
));
if
(
1
==
(
int
)
$result
[
'ok'
])
{
return
$result
[
'n'
];
}
return
0
;
}
/**
* Delete a record from the database.
*
* @param mixed $id
* @return int
*/
public
function
delete
(
$id
=
null
)
{
$query
=
$this
->
compileWheres
(
$this
);
$result
=
$this
->
collection
->
remove
(
$query
);
if
(
1
==
(
int
)
$result
[
'ok'
])
{
return
$result
[
'n'
];
}
return
0
;
}
/**
* Set the collection which the query is targeting.
*
* @param string $collection
* @return Builder
*/
public
function
from
(
$collection
)
{
if
(
$collection
)
{
$this
->
collection
=
$this
->
connection
->
getCollection
(
$collection
);
}
return
$this
;
}
/**
* Run a truncate statement on the table.
*
* @return void
*/
public
function
truncate
()
{
$result
=
$this
->
collection
->
drop
();
return
(
1
==
(
int
)
$result
[
'ok'
]);
}
/**
* Compile the where array
*
* @return array
*/
private
function
compileWheres
()
{
if
(
!
$this
->
wheres
)
return
array
();
$wheres
=
array
();
foreach
(
$this
->
wheres
as
$i
=>&
$where
)
{
// Convert id's
if
(
isset
(
$where
[
'column'
])
&&
$where
[
'column'
]
==
'_id'
)
{
if
(
isset
(
$where
[
'values'
]))
{
foreach
(
$where
[
'values'
]
as
&
$value
)
{
$value
=
(
$value
instanceof
MongoID
)
?
$value
:
new
MongoID
(
$value
);
}
}
else
{
$where
[
'value'
]
=
(
$where
[
'value'
]
instanceof
MongoID
)
?
$where
[
'value'
]
:
new
MongoID
(
$where
[
'value'
]);
}
}
// First item of chain
if
(
$i
==
0
&&
count
(
$this
->
wheres
)
>
1
&&
$where
[
'boolean'
]
==
'and'
)
{
// Copy over boolean value of next item in chain
$where
[
'boolean'
]
=
$this
->
wheres
[
$i
+
1
][
'boolean'
];
}
// Delegate
$method
=
"compileWhere
{
$where
[
'type'
]
}
"
;
$compiled
=
$this
->
{
$method
}(
$where
);
// Check for or
if
(
$where
[
'boolean'
]
==
'or'
)
{
$compiled
=
array
(
'$or'
=>
array
(
$compiled
));
}
// Merge compiled where
$wheres
=
array_merge_recursive
(
$wheres
,
$compiled
);
}
return
$wheres
;
}
private
function
compileWhereBasic
(
$where
)
{
extract
(
$where
);
// Replace like with MongoRegex
if
(
$operator
==
'like'
)
{
$operator
=
'='
;
$regex
=
str_replace
(
'%'
,
''
,
$value
);
// Prepare regex
if
(
substr
(
$value
,
0
,
1
)
!=
'%'
)
$regex
=
'^'
.
$regex
;
if
(
substr
(
$value
,
-
1
)
!=
'%'
)
$regex
=
$regex
.
'$'
;
$value
=
new
MongoRegex
(
"/
$regex
/i"
);
}
if
(
!
isset
(
$operator
)
||
$operator
==
'='
)
{
$query
=
array
(
$column
=>
$value
);
}
else
{
$query
=
array
(
$column
=>
array
(
$this
->
conversion
[
$operator
]
=>
$value
));
}
return
$query
;
}
private
function
compileWhereNested
(
$where
)
{
extract
(
$where
);
return
$query
->
compileWheres
();
}
private
function
compileWhereIn
(
$where
)
{
extract
(
$where
);
return
array
(
$column
=>
array
(
'$in'
=>
$values
));
}
private
function
compileWhereNull
(
$where
)
{
$where
[
'operator'
]
=
'='
;
$where
[
'value'
]
=
null
;
return
$this
->
compileWhereBasic
(
$where
);
}
private
function
compileWhereNotNull
(
$where
)
{
$where
[
'operator'
]
=
'!='
;
$where
[
'value'
]
=
null
;
return
$this
->
compileWhereBasic
(
$where
);
}
private
function
compileWherebetween
(
$where
)
{
extract
(
$where
);
return
array
(
$column
=>
array
(
'$gte'
=>
$values
[
0
],
'$lte'
=>
$values
[
1
])
);
}
/**
* Get a new instance of the query builder.
*
* @return Builder
*/
public
function
newQuery
()
{
$connection
=
$this
->
getConnection
();
return
new
Query
(
$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