Commit 3b5ee4fb by Paul Klimov

Mongo Command created.

parent c929268b
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace yii\mongo; namespace yii\mongo;
use \yii\base\Component; use \yii\base\Component;
use Yii;
/** /**
* Class Command * Class Command
...@@ -23,10 +24,136 @@ class Command extends Component ...@@ -23,10 +24,136 @@ class Command extends Component
public $db; public $db;
/** /**
* Drop the current database * Drops the current database
*/ */
public function dropDb() public function dropDb()
{ {
$this->db->db->drop(); $this->db->db->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 $fields
* @return \MongoCursor
*/
public function find($collection, $query = [], $fields = [])
{
$collection = $this->db->getCollection($collection);
return $collection->find($query, $fields);
}
/**
* @param $collection
* @param array $query
* @param array $fields
* @return array
*/
public function findAll($collection, $query = [], $fields = [])
{
$cursor = $this->find($collection, $query, $fields);
$result = [];
foreach ($cursor as $data) {
$result[] = $data;
}
return $result;
}
/**
* Inserts new data into collection.
* @param string $collection name of the collection.
* @param array|object $data data to be inserted.
* @param array $options list of options in format: optionName => optionValue.
* @return \MongoId new record id instance.
* @throws Exception on failure.
*/
public function insert($collection, $data, $options = [])
{
$token = 'Inserting data into ' . $collection;
Yii::info($token, __METHOD__);
try {
Yii::beginProfile($token, __METHOD__);
$collection = $this->db->getCollection($collection);
$this->tryResultError($collection->insert($data, $options));
Yii::endProfile($token, __METHOD__);
return is_array($data) ? $data['_id'] : $data->_id;
} catch (\Exception $e) {
Yii::endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
}
}
/**
* 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 $options list of options in format: optionName => optionValue.
* @return \MongoId updated/new record id instance.
* @throws Exception on failure.
*/
public function save($collection, $data, $options = [])
{
$token = 'Saving data into ' . $collection;
Yii::info($token, __METHOD__);
try {
Yii::beginProfile($token, __METHOD__);
$collection = $this->db->getCollection($collection);
$this->tryResultError($collection->save($data, $options));
Yii::endProfile($token, __METHOD__);
return is_array($data) ? $data['_id'] : $data->_id;
} catch (\Exception $e) {
Yii::endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
}
}
/**
* Removes data from the collection.
* @param string $collection name of the collection.
* @param array $criteria description of records to remove.
* @param array $options list of options in format: optionName => optionValue.
* @return boolean whether operation was successful.
* @throws Exception on failure.
*/
public function remove($collection, $criteria = [], $options = [])
{
$token = 'Removing data from ' . $collection;
Yii::info($token, __METHOD__);
try {
Yii::beginProfile($token, __METHOD__);
$collection = $this->db->getCollection($collection);
$this->tryResultError($collection->remove($criteria, $options));
Yii::endProfile($token, __METHOD__);
return true;
} catch (\Exception $e) {
Yii::endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
}
}
/**
* Checks if command execution result ended with an error.
* @param mixed $result raw command execution result.
* @throws Exception if an error occurred.
*/
protected function tryResultError($result)
{
if (is_array($result)) {
if (!empty($result['err'])) {
throw new Exception($result['errmsg'], (int)$result['code']);
}
} elseif (!$result) {
throw new Exception('Unknown error, use "w=1" option to enable error tracking');
}
}
} }
\ No newline at end of file
...@@ -9,7 +9,6 @@ namespace yii\mongo; ...@@ -9,7 +9,6 @@ namespace yii\mongo;
use yii\base\Component; use yii\base\Component;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\db\Exception;
use Yii; use Yii;
/** /**
...@@ -102,12 +101,13 @@ class Connection extends Component ...@@ -102,12 +101,13 @@ 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;
$this->client = new \MongoClient($this->dsn, $options); $this->client = new \MongoClient($this->dsn, $options);
$this->db = $this->client->selectDB($this->dbName); $this->db = $this->client->selectDB($this->dbName);
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
} catch (\Exception $e) { } catch (\Exception $e) {
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), [], (int)$e->getCode(), $e); throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
} }
} }
} }
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mongo;
/**
* Exception represents an exception that is caused by some Mongo-related operations.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class Exception extends \yii\base\Exception
{
/**
* @return string the user-friendly name of this exception
*/
public function getName()
{
return \Yii::t('yii', 'Mongo Exception');
}
}
\ No newline at end of file
<?php
namespace yiiunit\extensions\mongo;
/**
* @group mongo
*/
class CommandTest extends MongoTestCase
{
protected function tearDown()
{
$this->dropCollection('customer');
parent::tearDown();
}
// Tests :
public function testInsert()
{
$command = $this->getConnection()->createCommand();
$data = [
'name' => 'customer 1',
'address' => 'customer 1 address',
];
$id = $command->insert('customer', $data);
$this->assertTrue($id instanceof \MongoId);
$this->assertNotEmpty($id->__toString());
}
/**
* @depends testInsert
*/
public function testFindAll()
{
$command = $this->getConnection()->createCommand();
$data = [
'name' => 'customer 1',
'address' => 'customer 1 address',
];
$id = $command->insert('customer', $data);
$rows = $command->findAll('customer');
$this->assertEquals(1, count($rows));
$this->assertEquals($id, $rows[0]['_id']);
}
public function testSave()
{
$command = $this->getConnection()->createCommand();
$data = [
'name' => 'customer 1',
'address' => 'customer 1 address',
];
$id = $command->save('customer', $data);
$this->assertTrue($id instanceof \MongoId);
$this->assertNotEmpty($id->__toString());
}
/**
* @depends testSave
*/
public function testUpdate()
{
$command = $this->getConnection()->createCommand();
$data = [
'name' => 'customer 1',
'address' => 'customer 1 address',
];
$newId = $command->save('customer', $data);
$updatedId = $command->save('customer', $data);
$this->assertEquals($newId, $updatedId, 'Unable to update data!');
$data['_id'] = $newId->__toString();
$updatedId = $command->save('customer', $data);
$this->assertEquals($newId, $updatedId, 'Unable to updated data by string id!');
}
/**
* @depends testFindAll
*/
public function testRemove()
{
$command = $this->getConnection()->createCommand();
$data = [
'name' => 'customer 1',
'address' => 'customer 1 address',
];
$id = $command->insert('customer', $data);
$command->remove('customer', ['_id' => $id]);
$rows = $command->findAll('customer');
$this->assertEquals(0, count($rows));
}
}
\ No newline at end of file
...@@ -2,9 +2,11 @@ ...@@ -2,9 +2,11 @@
namespace yiiunit\extensions\mongo; namespace yiiunit\extensions\mongo;
use yii\mongo\Connection; use yii\mongo\Connection;
/**
* @group mongo
*/
class ConnectionTest extends MongoTestCase class ConnectionTest extends MongoTestCase
{ {
public function testConstruct() public function testConstruct()
...@@ -38,7 +40,7 @@ class ConnectionTest extends MongoTestCase ...@@ -38,7 +40,7 @@ class ConnectionTest extends MongoTestCase
$connection = new Connection; $connection = new Connection;
$connection->dsn = 'unknown::memory:'; $connection->dsn = 'unknown::memory:';
$this->setExpectedException('yii\db\Exception'); $this->setExpectedException('yii\mongo\Exception');
$connection->open(); $connection->open();
} }
......
...@@ -88,4 +88,15 @@ class MongoTestCase extends TestCase ...@@ -88,4 +88,15 @@ class MongoTestCase extends TestCase
$this->mongo = $db; $this->mongo = $db;
return $db; return $db;
} }
/**
* Drops the specified collection.
* @param string $name collection name.
*/
protected function dropCollection($name)
{
if ($this->mongo) {
$this->mongo->getCollection($name)->drop();
}
}
} }
\ No newline at end of file
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