Browse Source

added methods to MongoDriver for custom commands, findAndModify, count

master
Anton Grebnev 13 years ago
parent
commit
a87845980d
  1. 24
      model/MongoDbCommand.php
  2. 31
      model/MongoDriver.php
  3. 5
      model/MongoModel.php
  4. 12
      model/MongoStatement.php
  5. 22
      tests/model/MongoDbCommandTest.php
  6. 47
      tests/model/MongoDriverTest.php
  7. 12
      tests/model/MongoModelTest.php

24
model/MongoDbCommand.php

@ -18,6 +18,8 @@ class MongoCommandBuilder
const REMOVE = 'Remove';
const COMMAND = 'Command';
static public function factory($type, $collection = null)
{
$class = ucfirst($type) . 'MongoCommand';
@ -167,4 +169,26 @@ class RemoveMongoCommand extends MongoDbCommand
}
}
class CommandMongoCommand extends MongoDbCommand
{
protected $command;
protected function concreteExecute()
{
$db = $this->collection->db;
if($db instanceof MongoDB) {
return $db->command($this->command);
} else {
return false;
}
}
protected function checkParams()
{
if (isset($this->collection) && isset($this->command)) {
return true;
} else {
return false;
}
}
}

31
model/MongoDriver.php

@ -26,6 +26,11 @@ class MongoDriver extends NoSqlDbDriver
return $this->connection->selectCollection($this->db, $name);
}
public function count($collection, $query = array(), $limit = 0, $skip = 0)
{
return $this->getCollection($collection)->count($query, $limit, $skip);
}
public function find($collection, $condition = array(), $fields = array())
{
$command = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->getCollection($collection));
@ -48,6 +53,30 @@ class MongoDriver extends NoSqlDbDriver
return $this->query($command, $params);
}
public function findAndModify($collection, $query, $update, $sort = array(), $field = array(), $upsert = false, $new = false, $remove = false)
{
$command = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $this->getCollection($collection));
$cmd = array(
'findAndModify' => $collection,
'query' => $query,
'update' => $update,
'sort' => $sort,
'fields' => $field,
'new' => $new,
'upsert' => $upsert,
'remove' => $remove
);
$params = array('command' => $cmd);
return $this->query($command, $params);
}
public function command($collection, $cmd)
{
$command = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $this->getCollection($collection));
$params = array('command' => $cmd);
return $this->query($command, $params);
}
public function insert($collection, $data, $safe = true)
{
$command = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->getCollection($collection));
@ -85,6 +114,8 @@ class MongoDriver extends NoSqlDbDriver
return $this->query($command, $params)->affectedRows();
}
/**
* @param mixed $request
* @return DbStatement

5
model/MongoModel.php

@ -13,6 +13,11 @@
abstract class MongoModel extends Model
{
public function count($query = array(), $limit = 0, $skip = 0)
{
return $this->db->count($this->table(), $query, $limit, $skip);
}
public function find($condition = array())
{
return $this->db->find($this->table(), $condition);

12
model/MongoStatement.php

@ -96,8 +96,10 @@ class MongoStatement extends DbStatement
public function affectedRows()
{
if (is_array($this->result)) {
if (isset($this->result['ok']) && $this->result['ok'] == 1 && isset($this->result['n'])) {
if (isset($this->result['ok']) && $this->result['ok'] == 1) {
if (isset($this->result['n'])) {
return $this->result['n'];
}
} else {
return false;
}
@ -115,7 +117,7 @@ class MongoStatement extends DbStatement
}
/**
* @param MongoDbComand $request
* @param MongoDbCommand $request
* @return bool
*/
protected function driverExecute($request)
@ -134,6 +136,12 @@ class MongoStatement extends DbStatement
}
if ($result instanceof MongoCursor || is_array($result)) {
$this->result = $result;
if (is_array($result) && isset($result['value'])) {
$this->result = $result['value'];
}
if (is_array($result) && isset($result['values'])) {
$this->result = $result['values'];
}
}
if ($request instanceof InsertMongoCommand) {
$this->insertId = $request->getInsertId();

22
tests/model/MongoDbCommandTest.php

@ -199,4 +199,26 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
$cmd->execute();
}
/**
* @expectedException Exception
* @expectedExceptionMessage CommandMongoCommand error. Bind all required params first.
*/
public function testCommandCommandNotAllParamsBinded()
{
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $this->collection);
$cmd->execute();
}
public function testCommandCommandNotMongoDb()
{
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
$cmd->bindParam('command', array());
$this->assertFalse($cmd->execute());
}
}
class CollectionMock
{
public $db = array();
}

47
tests/model/MongoDriverTest.php

@ -176,6 +176,20 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
/**
* @runInSeparateProcess
*/
public function testCount()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$mongo = new MongoDriver($this->conf);
$this->assertEquals(5, $mongo->count('items'));
$this->assertEquals(2, $mongo->count('items', array('name' => 'eggs')));
$this->assertEquals(1, $mongo->count('items', array('name' => 'eggs'), 1));
}
/**
* @runInSeparateProcess
*/
public function testGet()
{
if (!defined('DEBUG')) {
@ -279,4 +293,37 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
$this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true, true));
$this->assertEquals('today', $mongo->get('items', array('name' => 'ball'))->fetch()->date);
}
/**
* @runInSeparateProcess
*/
public function testFindAndModify()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$mongo = new MongoDriver($this->conf);
$this->assertEquals(10, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
$result = $mongo->findAndModify('items', array('name' => 'bread'), array('$set' => array('quantity' => 20)));
$this->assertEquals(10, $result->fetch()->quantity);
$this->assertEquals(20, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
}
/**
* @runInSeparateProcess
*/
public function testCommand()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$mongo = new MongoDriver($this->conf);
$result = $mongo->command('items', array('distinct' =>'items', 'key' => 'name'));
$this->assertEquals(4, count($result->fetch(DB::FETCH_ASSOC)));
}
}

12
tests/model/MongoModelTest.php

@ -101,6 +101,18 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
/**
* @runInSeparateProcess
*/
public function testCount()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$this->assertEquals(5, $this->model->count());
$this->assertEquals(2, $this->model->count(array('name' => 'eggs')));
}
/**
* @runInSeparateProcess
*/
public function testFetch()
{
if (!defined('DEBUG')) {

Loading…
Cancel
Save