Commit 97dbdfc7 authored by Jens Segers's avatar Jens Segers

Connection tweaks, fixes #413

parent ca1a9292
...@@ -278,7 +278,7 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model { ...@@ -278,7 +278,7 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
// Check the connection type // Check the connection type
if ($connection instanceof \Jenssegers\Mongodb\Connection) if ($connection instanceof \Jenssegers\Mongodb\Connection)
{ {
return new QueryBuilder($connection); return new QueryBuilder($connection, $connection->getPostProcessor());
} }
return parent::newBaseQueryBuilder(); return parent::newBaseQueryBuilder();
......
<?php namespace Jenssegers\Mongodb; <?php namespace Jenssegers\Mongodb;
use Exception; use Exception, MongoCollection;
use MongoCollection;
use Jenssegers\Mongodb\Connection; use Jenssegers\Mongodb\Connection;
class Collection { class Collection {
......
<?php namespace Jenssegers\Mongodb; <?php namespace Jenssegers\Mongodb;
use Jenssegers\Mongodb\Collection;
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
use MongoClient; use MongoClient;
class Connection extends \Illuminate\Database\Connection { class Connection extends \Illuminate\Database\Connection {
...@@ -34,13 +32,25 @@ class Connection extends \Illuminate\Database\Connection { ...@@ -34,13 +32,25 @@ class Connection extends \Illuminate\Database\Connection {
$dsn = $this->getDsn($config); $dsn = $this->getDsn($config);
// You can pass options directly to the MongoClient constructor // You can pass options directly to the MongoClient constructor
$options = array_get($config, 'options', array()); $options = array_get($config, 'options', []);
// Create the connection // Create the connection
$this->connection = $this->createConnection($dsn, $config, $options); $this->connection = $this->createConnection($dsn, $config, $options);
// Select database // Select database
$this->db = $this->connection->{$config['database']}; $this->db = $this->connection->{$config['database']};
$this->useDefaultPostProcessor();
}
/**
* Get the default post processor instance.
*
* @return Query\Processor
*/
protected function getDefaultPostProcessor()
{
return new Query\Processor;
} }
/** /**
...@@ -51,7 +61,9 @@ class Connection extends \Illuminate\Database\Connection { ...@@ -51,7 +61,9 @@ class Connection extends \Illuminate\Database\Connection {
*/ */
public function collection($collection) public function collection($collection)
{ {
$query = new QueryBuilder($this); $processor = $this->getPostProcessor();
$query = new Query\Builder($this, $processor);
return $query->from($collection); return $query->from($collection);
} }
...@@ -120,12 +132,12 @@ class Connection extends \Illuminate\Database\Connection { ...@@ -120,12 +132,12 @@ class Connection extends \Illuminate\Database\Connection {
{ {
// Add credentials as options, this makes sure the connection will not fail if // Add credentials as options, this makes sure the connection will not fail if
// the username or password contains strange characters. // the username or password contains strange characters.
if (isset($config['username']) && $config['username']) if ( ! empty($config['username']))
{ {
$options['username'] = $config['username']; $options['username'] = $config['username'];
} }
if (isset($config['password']) && $config['password']) if ( ! empty($config['password']))
{ {
$options['password'] = $config['password']; $options['password'] = $config['password'];
} }
...@@ -156,13 +168,19 @@ class Connection extends \Illuminate\Database\Connection { ...@@ -156,13 +168,19 @@ class Connection extends \Illuminate\Database\Connection {
// need to establish the MongoClient and return them back for use. // need to establish the MongoClient and return them back for use.
extract($config); extract($config);
// Check if the user passed a complete dsn to the configuration.
if ( ! empty($dsn))
{
return $dsn;
}
// Treat host option as array of hosts // Treat host option as array of hosts
$hosts = is_array($config['host']) ? $config['host'] : array($config['host']); $hosts = is_array($host) ? $host : [$host];
// Add ports to hosts
foreach ($hosts as &$host) foreach ($hosts as &$host)
{ {
if (isset($config['port'])) // Check if we need to add a port to the host
if (strpos($host, ':') === false and isset($port))
{ {
$host = "{$host}:{$port}"; $host = "{$host}:{$port}";
} }
......
<?php namespace Jenssegers\Mongodb\Query; <?php namespace Jenssegers\Mongodb\Query;
use MongoId; use MongoId, MongoRegex, MongoDate, DateTime, Closure;
use MongoRegex; use Illuminate\Database\Query\Builder as BaseBuilder;
use MongoDate;
use DateTime;
use Closure;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Query\Expression; use Illuminate\Database\Query\Expression;
use Jenssegers\Mongodb\Connection; use Jenssegers\Mongodb\Connection;
class Builder extends QueryBuilder { class Builder extends BaseBuilder {
/** /**
* The database collection * The database collection
...@@ -69,9 +64,10 @@ class Builder extends QueryBuilder { ...@@ -69,9 +64,10 @@ class Builder extends QueryBuilder {
* @param Connection $connection * @param Connection $connection
* @return void * @return void
*/ */
public function __construct(Connection $connection) public function __construct(Connection $connection, Processor $processor)
{ {
$this->connection = $connection; $this->connection = $connection;
$this->processor = $processor;
} }
/** /**
...@@ -656,7 +652,7 @@ class Builder extends QueryBuilder { ...@@ -656,7 +652,7 @@ class Builder extends QueryBuilder {
*/ */
public function newQuery() public function newQuery()
{ {
return new Builder($this->connection); return new Builder($this->connection, $this->processor);
} }
/** /**
......
<?php namespace Jenssegers\Mongodb\Query;
use Illuminate\Database\Query\Processors\Processor as BaseProcessor;
class Processor extends BaseProcessor {
}
...@@ -119,4 +119,15 @@ class ConnectionTest extends TestCase { ...@@ -119,4 +119,15 @@ class ConnectionTest extends TestCase {
$connection = DB::connection('mongodb'); $connection = DB::connection('mongodb');
} }
public function testHostWithPorts()
{
$hosts = ['localhost:27001', 'localhost:27002'];
Config::set('database.connections.mongodb.port', 27000);
Config::set('database.connections.mongodb.host', ['localhost:27001', 'localhost:27002']);
$database = Config::get('database.connections.mongodb.database');
$this->setExpectedException('MongoConnectionException', "Failed to connect to: " . $hosts[0] . ": Connection refused; Failed to connect to: " . $hosts[1] . ": Connection refused");
$connection = DB::connection('mongodb');
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment