Browse Source

Merge branch 'mongo'

master
Anton Grebnev 13 years ago
parent
commit
f58fa2a48a
  1. 19
      model/MongoDbCommand.php
  2. 5
      model/MongoDriver.php
  3. 13
      model/MongoModel.php
  4. 48
      tests/model/MongoDbCommandTest.php
  5. 31
      tests/model/MongoDriverTest.php
  6. 21
      tests/model/MongoModelTest.php

19
model/MongoDbCommand.php

@ -121,10 +121,19 @@ class InsertMongoCommand extends MongoDbCommand
protected $insertId = false; protected $insertId = false;
protected $multiple = false;
protected function concreteExecute() protected function concreteExecute()
{ {
$result = $this->collection->insert($this->data, array('safe' => $this->safe));
$this->insertId = $this->data['_id'];
$result = null;
if (!$this->multiple) {
$result = $this->collection->insert($this->data, array('safe' => $this->safe));
$this->insertId = $this->data['_id'];
} else {
if (count($this->data)) {
$result = $this->collection->batchInsert($this->data, array('safe' => $this->safe));
}
}
return $result; return $result;
} }
@ -150,6 +159,8 @@ class InsertMongoCommand extends MongoDbCommand
var_dump($this->data); var_dump($this->data);
$data = ob_get_clean(); $data = ob_get_clean();
$result .= 'Data: ' . $data . PHP_EOL; $result .= 'Data: ' . $data . PHP_EOL;
$mult = $this->multiple ? 'TRUE' : 'FALSE';
$result .= 'Bulk insert: ' . $mult . PHP_EOL;
$safe = $this->safe ? 'TRUE' : 'FALSE'; $safe = $this->safe ? 'TRUE' : 'FALSE';
$result .= 'Safe operation: ' . $safe . PHP_EOL; $result .= 'Safe operation: ' . $safe . PHP_EOL;
return $result; return $result;
@ -174,13 +185,13 @@ class UpdateMongoCommand extends MongoDbCommand
protected function concreteExecute() protected function concreteExecute()
{ {
return $this->collection->update($this->condition, $this->data, return $this->collection->update($this->condition, $this->data,
array('multiple' => $this->multiple, 'upsert' => $this->upsert, 'safe' => $this->safe));
array('multiple' => $this->multiple, 'upsert' => $this->upsert, 'safe' => $this->safe));
} }
protected function checkParams() protected function checkParams()
{ {
if (isset($this->collection) && isset($this->condition) && isset($this->data) && if (isset($this->collection) && isset($this->condition) && isset($this->data) &&
isset($this->upsert) && isset($this->safe)
isset($this->upsert) && isset($this->safe)
) { ) {
return true; return true;
} else { } else {

5
model/MongoDriver.php

@ -77,12 +77,13 @@ class MongoDriver extends NoSqlDbDriver
return $this->query($command, $params); return $this->query($command, $params);
} }
public function insert($collection, $data, $safe = true)
public function insert($collection, $data, $multiple = false, $safe = true)
{ {
$command = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->getCollection($collection)); $command = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->getCollection($collection));
$params = array( $params = array(
'data' => $data, 'data' => $data,
'safe' => $safe
'safe' => $safe,
'multiple' => $multiple
); );
$result = $this->query($command, $params); $result = $this->query($command, $params);
$this->last_insert_id = $result->getInsertId(); $this->last_insert_id = $result->getInsertId();

13
model/MongoModel.php

@ -32,18 +32,23 @@ abstract class MongoModel extends Model
public function get($id) public function get($id)
{ {
if($this->useMongoId) {
if(! $id instanceof MongoId) {
if ($this->useMongoId) {
if (!$id instanceof MongoId) {
$id = new MongoId($id); $id = new MongoId($id);
} }
} }
return $this->db->get($this->table(), array('_id' => $id))->fetch(); return $this->db->get($this->table(), array('_id' => $id))->fetch();
} }
public function batchInsert($data)
{
return $this->db->insert($this->table(), $data, true);
}
public function delete($id) public function delete($id)
{ {
if($this->useMongoId) {
if(! $id instanceof MongoId) {
if ($this->useMongoId) {
if (!$id instanceof MongoId) {
$id = new MongoId($id); $id = new MongoId($id);
} }
} }

48
tests/model/MongoDbCommandTest.php

@ -132,6 +132,45 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
/** /**
* @group Mongo * @group Mongo
*/ */
public function testInsertCommandMultipleObjects()
{
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
$data = array(
array('name' => 'first object'),
array('name' => 'second object'),
array('name' => 'equal object'),
array('name' => 'equal object')
);
$cmd
->bindParam('data', $data)
->bindParam('multiple', true)
->bindParam('safe', true);
$this->assertFalse($cmd->getInsertId());
$this->assertArrayHasKey('n', $cmd->execute());
$cmd->bindParam('data', array());
$cmd->execute();
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
$cmd->bindParam('condition', array('name' => 'first object'))->bindParam('fields', array());
$result = $cmd->execute();
$this->assertEquals(1, $result->count());
$cmd->bindParam('condition', array('name' => 'second object'))->bindParam('fields', array());
$result = $cmd->execute();
$this->assertEquals(1, $result->count());
$cmd->bindParam('condition', array('name' => 'equal object'))->bindParam('fields', array());
$result = $cmd->execute();
$this->assertEquals(2, $result->count());
}
/**
* @group Mongo
*/
public function testInsertCommandNotAllParamsBinded() public function testInsertCommandNotAllParamsBinded()
{ {
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection); $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
@ -277,6 +316,15 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
->bindParam('data', array('name' => 'insert')) ->bindParam('data', array('name' => 'insert'))
->bindParam('safe', true); ->bindParam('safe', true);
$this->assertStringStartsWith('Collection: ', $cmd->__toString()); $this->assertStringStartsWith('Collection: ', $cmd->__toString());
$this->assertContains('Bulk insert: FALSE', $cmd->__toString());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
$this->assertSame('Command properties not set', $cmd->__toString());
$cmd
->bindParam('data', array('name' => 'insert'))
->bindParam('multiple', true)
->bindParam('safe', true);
$this->assertContains('Bulk insert: TRUE', $cmd->__toString());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection); $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
$this->assertSame('Command properties not set', $cmd->__toString()); $this->assertSame('Command properties not set', $cmd->__toString());

31
tests/model/MongoDriverTest.php

@ -67,7 +67,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
$collection = 'items'; $collection = 'items';
$db->dropCollection($collection); $db->dropCollection($collection);
$collection = $db->selectCollection($collection); $collection = $db->selectCollection($collection);
foreach($data as $document) {
foreach ($data as $document) {
$collection->insert($document); $collection->insert($document);
} }
} }
@ -131,7 +131,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
$this->assertEquals(3, $mongo->find('items', array('price' => array('$lt' => 3.5)))->numRows()); $this->assertEquals(3, $mongo->find('items', array('price' => array('$lt' => 3.5)))->numRows());
$data = $mongo->find('items', array('price' => array('$lt' => 3.5))); $data = $mongo->find('items', array('price' => array('$lt' => 3.5)));
$count = 0; $count = 0;
while($row = $data->fetch(Db::FETCH_ASSOC)) {
while ($row = $data->fetch(Db::FETCH_ASSOC)) {
$count++; $count++;
$this->assertLessThan(3.5, $row['price']); $this->assertLessThan(3.5, $row['price']);
} }
@ -173,7 +173,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
$data->order(array('name' => -1)); $data->order(array('name' => -1));
$data->skip(3); $data->skip(3);
$data->limit(1); $data->limit(1);
while($row = $data->fetch()) {
while ($row = $data->fetch()) {
$this->assertEquals('fdsbssc', $row->name); $this->assertEquals('fdsbssc', $row->name);
} }
} }
@ -252,6 +252,29 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
* @runInSeparateProcess * @runInSeparateProcess
* @group Mongo * @group Mongo
*/ */
public function testBatchInsert()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$data = array(
array('name' => 'first object'),
array('name' => 'second object'),
array('name' => 'equal object'),
array('name' => 'equal object')
);
$mongo = new MongoDriver($this->conf);
$mongo->insert('items', $data, true);
$this->assertEquals(1, $mongo->find('items', array('name' => 'first object'))->numRows());
$this->assertEquals(1, $mongo->find('items', array('name' => 'second object'))->numRows());
$this->assertEquals(2, $mongo->find('items', array('name' => 'equal object'))->numRows());
}
/**
* @runInSeparateProcess
* @group Mongo
*/
public function testGetInsertId() public function testGetInsertId()
{ {
if (!defined('DEBUG')) { if (!defined('DEBUG')) {
@ -337,7 +360,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
define('DEBUG', false); define('DEBUG', false);
} }
$mongo = new MongoDriver($this->conf); $mongo = new MongoDriver($this->conf);
$result = $mongo->command('items', array('distinct' =>'items', 'key' => 'name'));
$result = $mongo->command('items', array('distinct' => 'items', 'key' => 'name'));
$this->assertEquals(4, count($result->fetch(DB::FETCH_ASSOC))); $this->assertEquals(4, count($result->fetch(DB::FETCH_ASSOC)));
} }
} }

21
tests/model/MongoModelTest.php

@ -112,6 +112,27 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
* @runInSeparateProcess * @runInSeparateProcess
* @group Mongo * @group Mongo
*/ */
public function testBatchInsert()
{
if (!defined('DEBUG')) {
define('DEBUG', false);
}
$data = array(
array('name' => 'first object'),
array('name' => 'second object'),
array('name' => 'equal object'),
array('name' => 'equal object')
);
$this->model->batchInsert($data);
$this->assertEquals(1, $this->model->count(array('name' => 'first object')));
$this->assertEquals(2, $this->model->count(array('name' => 'equal object')));
$this->model->batchInsert(array());
}
/**
* @runInSeparateProcess
* @group Mongo
*/
public function testDeleteAll() public function testDeleteAll()
{ {
if (!defined('DEBUG')) { if (!defined('DEBUG')) {

Loading…
Cancel
Save