Commit 529af8ed by Klimov Paul

Mongo "Database" and "Collection" classes introduced.

parent 3b5ee4fb
...@@ -7,61 +7,48 @@ ...@@ -7,61 +7,48 @@
namespace yii\mongo; namespace yii\mongo;
use \yii\base\Component; use yii\base\Object;
use Yii; use Yii;
/** /**
* Class Command * Collection represents the Mongo collection information.
* *
* @author Paul Klimov <klimov.paul@gmail.com> * @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class Command extends Component class Collection extends Object
{ {
/** /**
* @var Connection the Mongo connection that this command is associated with * @var \MongoCollection Mongo collection instance.
*/ */
public $db; public $mongoCollection;
/** /**
* Drops the current database * Drops this collection.
*/ */
public function dropDb() public function drop()
{ {
$this->db->db->drop(); $this->mongoCollection->drop();
} }
/** /**
* Drops the specified collection.
* @param string $name collection name.
*/
public function dropCollection($name)
{
$collection = $this->db->getCollection($name);
$collection->drop();
}
/**
* @param $collection
* @param array $query * @param array $query
* @param array $fields * @param array $fields
* @return \MongoCursor * @return \MongoCursor
*/ */
public function find($collection, $query = [], $fields = []) public function find($query = [], $fields = [])
{ {
$collection = $this->db->getCollection($collection); return $this->mongoCollection->find($query, $fields);
return $collection->find($query, $fields);
} }
/** /**
* @param $collection
* @param array $query * @param array $query
* @param array $fields * @param array $fields
* @return array * @return array
*/ */
public function findAll($collection, $query = [], $fields = []) public function findAll($query = [], $fields = [])
{ {
$cursor = $this->find($collection, $query, $fields); $cursor = $this->find($query, $fields);
$result = []; $result = [];
foreach ($cursor as $data) { foreach ($cursor as $data) {
$result[] = $data; $result[] = $data;
...@@ -71,20 +58,18 @@ class Command extends Component ...@@ -71,20 +58,18 @@ class Command extends Component
/** /**
* Inserts new data into collection. * Inserts new data into collection.
* @param string $collection name of the collection.
* @param array|object $data data to be inserted. * @param array|object $data data to be inserted.
* @param array $options list of options in format: optionName => optionValue. * @param array $options list of options in format: optionName => optionValue.
* @return \MongoId new record id instance. * @return \MongoId new record id instance.
* @throws Exception on failure. * @throws Exception on failure.
*/ */
public function insert($collection, $data, $options = []) public function insert($data, $options = [])
{ {
$token = 'Inserting data into ' . $collection; $token = 'Inserting data into ' . $this->mongoCollection->getName();
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
$collection = $this->db->getCollection($collection); $this->tryResultError($this->mongoCollection->insert($data, $options));
$this->tryResultError($collection->insert($data, $options));
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
return is_array($data) ? $data['_id'] : $data->_id; return is_array($data) ? $data['_id'] : $data->_id;
} catch (\Exception $e) { } catch (\Exception $e) {
...@@ -95,20 +80,18 @@ class Command extends Component ...@@ -95,20 +80,18 @@ class Command extends Component
/** /**
* Update the existing database data, otherwise insert this data * Update the existing database data, otherwise insert this data
* @param string $collection name of the collection.
* @param array|object $data data to be updated/inserted. * @param array|object $data data to be updated/inserted.
* @param array $options list of options in format: optionName => optionValue. * @param array $options list of options in format: optionName => optionValue.
* @return \MongoId updated/new record id instance. * @return \MongoId updated/new record id instance.
* @throws Exception on failure. * @throws Exception on failure.
*/ */
public function save($collection, $data, $options = []) public function save($data, $options = [])
{ {
$token = 'Saving data into ' . $collection; $token = 'Saving data into ' . $this->mongoCollection->getName();
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
$collection = $this->db->getCollection($collection); $this->tryResultError($this->mongoCollection->save($data, $options));
$this->tryResultError($collection->save($data, $options));
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
return is_array($data) ? $data['_id'] : $data->_id; return is_array($data) ? $data['_id'] : $data->_id;
} catch (\Exception $e) { } catch (\Exception $e) {
...@@ -119,20 +102,18 @@ class Command extends Component ...@@ -119,20 +102,18 @@ class Command extends Component
/** /**
* Removes data from the collection. * Removes data from the collection.
* @param string $collection name of the collection.
* @param array $criteria description of records to remove. * @param array $criteria description of records to remove.
* @param array $options list of options in format: optionName => optionValue. * @param array $options list of options in format: optionName => optionValue.
* @return boolean whether operation was successful. * @return boolean whether operation was successful.
* @throws Exception on failure. * @throws Exception on failure.
*/ */
public function remove($collection, $criteria = [], $options = []) public function remove($criteria = [], $options = [])
{ {
$token = 'Removing data from ' . $collection; $token = 'Removing data from ' . $this->mongoCollection->getName();
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
$collection = $this->db->getCollection($collection); $this->tryResultError($this->mongoCollection->remove($criteria, $options));
$this->tryResultError($collection->remove($criteria, $options));
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
return true; return true;
} catch (\Exception $e) { } catch (\Exception $e) {
......
...@@ -24,14 +24,16 @@ use Yii; ...@@ -24,14 +24,16 @@ use Yii;
class Connection extends Component class Connection extends Component
{ {
/** /**
* @var \MongoCollection[] list of Mongo collection available in database. * @var string host:port
*/ *
private $_collections = []; * Correct syntax is:
* mongodb://[username:password@]host1[:port1][,host2[:port2:],...][/dbname]
/** * For example:
* @var \MongoClient mongo client instance. * mongodb://localhost:27017
* mongodb://developer:somepassword@localhost:27017
* mongodb://developer:somepassword@localhost:27017/mydatabase
*/ */
public $client; public $dsn;
/** /**
* @var array connection options. * @var array connection options.
* for example: * for example:
...@@ -45,34 +47,83 @@ class Connection extends Component ...@@ -45,34 +47,83 @@ class Connection extends Component
*/ */
public $options = []; public $options = [];
/** /**
* @var string host:port * @var string name of the Mongo database to use by default.
*
* Correct syntax is:
* mongodb://[username:password@]host1[:port1][,host2[:port2:],...]
* For example: mongodb://localhost:27017
*/ */
public $dsn; public $defaultDatabaseName;
/**
* @var \MongoClient mongo client instance.
*/
public $mongoClient;
/**
* @var Database[] list of Mongo databases
*/
private $_databases = [];
/**
* Returns the Mongo collection with the given name.
* @param string|null $name collection name, if null default one will be used.
* @param boolean $refresh whether to reload the table schema even if it is found in the cache.
* @return Database database instance.
*/
public function getDatabase($name = null, $refresh = false)
{
if ($name === null) {
$name = $this->fetchDefaultDatabaseName();
}
if ($refresh || !array_key_exists($name, $this->_databases)) {
$this->_databases[$name] = $this->selectDatabase($name);
}
return $this->_databases[$name];
}
/** /**
* @var string name of the Mongo database to use * Returns [[defaultDatabaseName]] value, if it is not set,
* attempts to determine it from [[dsn]] value.
* @return string default database name
* @throws \yii\base\InvalidConfigException if unable to determine default database name.
*/ */
public $dbName; protected function fetchDefaultDatabaseName()
{
if ($this->defaultDatabaseName === null) {
if (preg_match('/^mongodb:\\/\\/.+\\/(.+)$/s', $this->dsn, $matches)) {
$this->defaultDatabaseName = $matches[1];
} else {
throw new InvalidConfigException("Unable to determine default database name from dsn.");
}
}
return $this->defaultDatabaseName;
}
/** /**
* @var \MongoDb Mongo database instance. * Selects the database with given name.
* @param string $name database name.
* @return Database database instance.
*/ */
public $db; protected function selectDatabase($name)
{
$this->open();
return Yii::createObject([
'class' => 'yii\mongo\Database',
'mongoDb' => $this->mongoClient->selectDB($name)
]);
}
/** /**
* Returns the Mongo collection with the given name. * Returns the Mongo collection with the given name.
* @param string $name collection name * @param string|array $name collection name. If string considered as the name of the collection
* inside the default database. If array - first element considered as the name of the database,
* second - as name of collection inside that database
* @param boolean $refresh whether to reload the table schema even if it is found in the cache. * @param boolean $refresh whether to reload the table schema even if it is found in the cache.
* @return \MongoCollection mongo collection instance. * @return Collection Mongo collection instance.
*/ */
public function getCollection($name, $refresh = false) public function getCollection($name, $refresh = false)
{ {
if ($refresh || !array_key_exists($name, $this->_collections)) { if (is_array($name)) {
$this->_collections[$name] = $this->client->selectCollection($this->dbName, $name); list ($dbName, $collectionName) = $name;
return $this->getDatabase($dbName)->getCollection($collectionName, $refresh);
} else {
return $this->getDatabase()->getCollection($name, $refresh);
} }
return $this->_collections[$name];
} }
/** /**
...@@ -81,7 +132,7 @@ class Connection extends Component ...@@ -81,7 +132,7 @@ class Connection extends Component
*/ */
public function getIsActive() public function getIsActive()
{ {
return is_object($this->client) && $this->client->connected; return is_object($this->mongoClient) && $this->mongoClient->connected;
} }
/** /**
...@@ -91,7 +142,7 @@ class Connection extends Component ...@@ -91,7 +142,7 @@ class Connection extends Component
*/ */
public function open() public function open()
{ {
if ($this->client === null) { if ($this->mongoClient === null) {
if (empty($this->dsn)) { if (empty($this->dsn)) {
throw new InvalidConfigException($this->className() . '::dsn cannot be empty.'); throw new InvalidConfigException($this->className() . '::dsn cannot be empty.');
} }
...@@ -101,9 +152,10 @@ class Connection extends Component ...@@ -101,9 +152,10 @@ class Connection extends Component
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
$options = $this->options; $options = $this->options;
$options['connect'] = true; $options['connect'] = true;
$options['db'] = $this->dbName; if ($this->defaultDatabaseName !== null) {
$this->client = new \MongoClient($this->dsn, $options); $options['db'] = $this->defaultDatabaseName;
$this->db = $this->client->selectDB($this->dbName); }
$this->mongoClient = new \MongoClient($this->dsn, $options);
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
} catch (\Exception $e) { } catch (\Exception $e) {
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
...@@ -118,32 +170,10 @@ class Connection extends Component ...@@ -118,32 +170,10 @@ class Connection extends Component
*/ */
public function close() public function close()
{ {
if ($this->client !== null) { if ($this->mongoClient !== null) {
Yii::trace('Closing Mongo connection: ' . $this->dsn, __METHOD__); Yii::trace('Closing Mongo connection: ' . $this->dsn, __METHOD__);
$this->client = null; $this->mongoClient = null;
$this->db = null; $this->_databases = [];
} }
} }
/**
* Returns the query builder for the current DB connection.
* @return QueryBuilder the query builder for the current DB connection.
*/
public function getQueryBuilder()
{
return new QueryBuilder($this);
}
/**
* Creates a command for execution.
* @return Command the Mongo command
*/
public function createCommand()
{
$this->open();
$command = new Command([
'db' => $this,
]);
return $command;
}
} }
\ No newline at end of file
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mongo;
use yii\base\Object;
use Yii;
/**
* Database represents the Mongo database information.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class Database extends Object
{
/**
* @var \MongoDB Mongo database instance.
*/
public $mongoDb;
/**
* @var Collection[] list of collections.
*/
private $_collections = [];
/**
* Returns the Mongo collection with the given name.
* @param string $name collection name
* @param boolean $refresh whether to reload the table schema even if it is found in the cache.
* @return Collection mongo collection instance.
*/
public function getCollection($name, $refresh = false)
{
if ($refresh || !array_key_exists($name, $this->_collections)) {
$this->_collections[$name] = $this->selectCollection($name);
}
return $this->_collections[$name];
}
/**
* Selects collection with given name.
* @param string $name collection name.
* @return Collection collection instance.
*/
protected function selectCollection($name)
{
return Yii::createObject([
'class' => 'yii\mongo\Collection',
'mongoCollection' => $this->mongoDb->selectCollection($name)
]);
}
/**
* Drops this database.
*/
public function drop()
{
$this->mongoDb->drop();
}
}
\ No newline at end of file
...@@ -5,7 +5,7 @@ namespace yiiunit\extensions\mongo; ...@@ -5,7 +5,7 @@ namespace yiiunit\extensions\mongo;
/** /**
* @group mongo * @group mongo
*/ */
class CommandTest extends MongoTestCase class CollectionTest extends MongoTestCase
{ {
protected function tearDown() protected function tearDown()
{ {
...@@ -17,12 +17,12 @@ class CommandTest extends MongoTestCase ...@@ -17,12 +17,12 @@ class CommandTest extends MongoTestCase
public function testInsert() public function testInsert()
{ {
$command = $this->getConnection()->createCommand(); $collection = $this->getConnection()->getCollection('customer');
$data = [ $data = [
'name' => 'customer 1', 'name' => 'customer 1',
'address' => 'customer 1 address', 'address' => 'customer 1 address',
]; ];
$id = $command->insert('customer', $data); $id = $collection->insert($data);
$this->assertTrue($id instanceof \MongoId); $this->assertTrue($id instanceof \MongoId);
$this->assertNotEmpty($id->__toString()); $this->assertNotEmpty($id->__toString());
} }
...@@ -32,26 +32,26 @@ class CommandTest extends MongoTestCase ...@@ -32,26 +32,26 @@ class CommandTest extends MongoTestCase
*/ */
public function testFindAll() public function testFindAll()
{ {
$command = $this->getConnection()->createCommand(); $collection = $this->getConnection()->getCollection('customer');
$data = [ $data = [
'name' => 'customer 1', 'name' => 'customer 1',
'address' => 'customer 1 address', 'address' => 'customer 1 address',
]; ];
$id = $command->insert('customer', $data); $id = $collection->insert($data);
$rows = $command->findAll('customer'); $rows = $collection->findAll();
$this->assertEquals(1, count($rows)); $this->assertEquals(1, count($rows));
$this->assertEquals($id, $rows[0]['_id']); $this->assertEquals($id, $rows[0]['_id']);
} }
public function testSave() public function testSave()
{ {
$command = $this->getConnection()->createCommand(); $collection = $this->getConnection()->getCollection('customer');
$data = [ $data = [
'name' => 'customer 1', 'name' => 'customer 1',
'address' => 'customer 1 address', 'address' => 'customer 1 address',
]; ];
$id = $command->save('customer', $data); $id = $collection->save($data);
$this->assertTrue($id instanceof \MongoId); $this->assertTrue($id instanceof \MongoId);
$this->assertNotEmpty($id->__toString()); $this->assertNotEmpty($id->__toString());
} }
...@@ -61,18 +61,18 @@ class CommandTest extends MongoTestCase ...@@ -61,18 +61,18 @@ class CommandTest extends MongoTestCase
*/ */
public function testUpdate() public function testUpdate()
{ {
$command = $this->getConnection()->createCommand(); $collection = $this->getConnection()->getCollection('customer');
$data = [ $data = [
'name' => 'customer 1', 'name' => 'customer 1',
'address' => 'customer 1 address', 'address' => 'customer 1 address',
]; ];
$newId = $command->save('customer', $data); $newId = $collection->save($data);
$updatedId = $command->save('customer', $data); $updatedId = $collection->save($data);
$this->assertEquals($newId, $updatedId, 'Unable to update data!'); $this->assertEquals($newId, $updatedId, 'Unable to update data!');
$data['_id'] = $newId->__toString(); $data['_id'] = $newId->__toString();
$updatedId = $command->save('customer', $data); $updatedId = $collection->save($data);
$this->assertEquals($newId, $updatedId, 'Unable to updated data by string id!'); $this->assertEquals($newId, $updatedId, 'Unable to updated data by string id!');
} }
...@@ -81,16 +81,16 @@ class CommandTest extends MongoTestCase ...@@ -81,16 +81,16 @@ class CommandTest extends MongoTestCase
*/ */
public function testRemove() public function testRemove()
{ {
$command = $this->getConnection()->createCommand(); $collection = $this->getConnection()->getCollection('customer');
$data = [ $data = [
'name' => 'customer 1', 'name' => 'customer 1',
'address' => 'customer 1 address', 'address' => 'customer 1 address',
]; ];
$id = $command->insert('customer', $data); $id = $collection->insert($data);
$command->remove('customer', ['_id' => $id]); $collection->remove(['_id' => $id]);
$rows = $command->findAll('customer'); $rows = $collection->findAll();
$this->assertEquals(0, count($rows)); $this->assertEquals(0, count($rows));
} }
} }
\ No newline at end of file
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
namespace yiiunit\extensions\mongo; namespace yiiunit\extensions\mongo;
use yii\mongo\Collection;
use yii\mongo\Connection; use yii\mongo\Connection;
use yii\mongo\Database;
/** /**
* @group mongo * @group mongo
...@@ -17,7 +19,7 @@ class ConnectionTest extends MongoTestCase ...@@ -17,7 +19,7 @@ class ConnectionTest extends MongoTestCase
$connection->open(); $connection->open();
$this->assertEquals($params['dsn'], $connection->dsn); $this->assertEquals($params['dsn'], $connection->dsn);
$this->assertEquals($params['dbName'], $connection->dbName); $this->assertEquals($params['defaultDatabaseName'], $connection->defaultDatabaseName);
$this->assertEquals($params['options'], $connection->options); $this->assertEquals($params['options'], $connection->options);
} }
...@@ -26,17 +28,15 @@ class ConnectionTest extends MongoTestCase ...@@ -26,17 +28,15 @@ class ConnectionTest extends MongoTestCase
$connection = $this->getConnection(false, false); $connection = $this->getConnection(false, false);
$this->assertFalse($connection->isActive); $this->assertFalse($connection->isActive);
$this->assertEquals(null, $connection->client); $this->assertEquals(null, $connection->mongoClient);
$connection->open(); $connection->open();
$this->assertTrue($connection->isActive); $this->assertTrue($connection->isActive);
$this->assertTrue(is_object($connection->client)); $this->assertTrue(is_object($connection->mongoClient));
$this->assertTrue(is_object($connection->db));
$connection->close(); $connection->close();
$this->assertFalse($connection->isActive); $this->assertFalse($connection->isActive);
$this->assertEquals(null, $connection->client); $this->assertEquals(null, $connection->mongoClient);
$this->assertEquals(null, $connection->db);
$connection = new Connection; $connection = new Connection;
$connection->dsn = 'unknown::memory:'; $connection->dsn = 'unknown::memory:';
...@@ -44,10 +44,52 @@ class ConnectionTest extends MongoTestCase ...@@ -44,10 +44,52 @@ class ConnectionTest extends MongoTestCase
$connection->open(); $connection->open();
} }
public function testGetDatabase()
{
$connection = $this->getConnection();
$database = $connection->getDatabase($connection->defaultDatabaseName);
$this->assertTrue($database instanceof Database);
$this->assertTrue($database->mongoDb instanceof \MongoDB);
$database2 = $connection->getDatabase($connection->defaultDatabaseName);
$this->assertTrue($database === $database2);
$databaseRefreshed = $connection->getDatabase($connection->defaultDatabaseName, true);
$this->assertFalse($database === $databaseRefreshed);
}
/**
* @depends testGetDatabase
*/
public function testGetDefaultDatabase()
{
$connection = new Connection();
$connection->dsn = $this->mongoConfig['dsn'];
$connection->defaultDatabaseName = $this->mongoConfig['defaultDatabaseName'];
$database = $connection->getDatabase();
$this->assertTrue($database instanceof Database, 'Unable to get default database!');
$connection = new Connection();
$connection->dsn = $this->mongoConfig['dsn'] . '/' . $this->mongoConfig['defaultDatabaseName'];
$database = $connection->getDatabase();
$this->assertTrue($database instanceof Database, 'Unable to determine default database from dsn!');
}
/**
* @depends testGetDefaultDatabase
*/
public function testGetCollection() public function testGetCollection()
{ {
$connection = $this->getConnection(false); $connection = $this->getConnection();
$collection = $connection->getCollection('customer'); $collection = $connection->getCollection('customer');
$this->assertTrue($collection instanceof \MongoCollection); $this->assertTrue($collection instanceof Collection);
$collection2 = $connection->getCollection('customer');
$this->assertTrue($collection === $collection2);
$collection2 = $connection->getCollection('customer', true);
$this->assertFalse($collection === $collection2);
} }
} }
\ No newline at end of file
<?php
namespace yiiunit\extensions\mongo;
use yii\mongo\Collection;
/**
* @group mongo
*/
class DatabaseTest extends MongoTestCase
{
protected function tearDown()
{
$this->dropCollection('customer');
parent::tearDown();
}
// Tests :
public function testGetCollection()
{
$database = $connection = $this->getConnection()->getDatabase();
$collection = $database->getCollection('customer');
$this->assertTrue($collection instanceof Collection);
$this->assertTrue($collection->mongoCollection instanceof \MongoCollection);
$collection2 = $database->getCollection('customer');
$this->assertTrue($collection === $collection2);
$collectionRefreshed = $database->getCollection('customer', true);
$this->assertFalse($collection === $collectionRefreshed);
}
}
\ No newline at end of file
...@@ -14,7 +14,7 @@ class MongoTestCase extends TestCase ...@@ -14,7 +14,7 @@ class MongoTestCase extends TestCase
*/ */
protected $mongoConfig = [ protected $mongoConfig = [
'dsn' => 'mongodb://localhost:27017', 'dsn' => 'mongodb://localhost:27017',
'dbName' => 'yii2test', 'defaultDatabaseName' => 'yii2test',
]; ];
/** /**
* @var Connection Mongo connection instance. * @var Connection Mongo connection instance.
...@@ -76,8 +76,8 @@ class MongoTestCase extends TestCase ...@@ -76,8 +76,8 @@ class MongoTestCase extends TestCase
} }
$db = new Connection; $db = new Connection;
$db->dsn = $this->mongoConfig['dsn']; $db->dsn = $this->mongoConfig['dsn'];
if (isset($this->mongoConfig['dbName'])) { if (isset($this->mongoConfig['defaultDatabaseName'])) {
$db->dbName = $this->mongoConfig['dbName']; $db->defaultDatabaseName = $this->mongoConfig['defaultDatabaseName'];
} }
if (isset($this->mongoConfig['options'])) { if (isset($this->mongoConfig['options'])) {
$db->options = $this->mongoConfig['options']; $db->options = $this->mongoConfig['options'];
......
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