From 488655b7884c46db661cf678c7912cd841384740 Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Tue, 15 Nov 2011 11:12:20 +0400 Subject: [PATCH] Mongo driver with separate CRUD commsnds, without profiler --- model/MongoDriver.php | 54 +++++++++++++++++---- model/NoSqlDbDriver.php | 3 +- tests/model/MongoDriverTest.php | 103 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 149 insertions(+), 11 deletions(-) diff --git a/model/MongoDriver.php b/model/MongoDriver.php index c1d48e0..8f75db0 100644 --- a/model/MongoDriver.php +++ b/model/MongoDriver.php @@ -13,30 +13,65 @@ class MongoDriver extends NoSqlDbDriver { - public function find($data, $params = array(), $cache_key = null) + + protected $last_inset_id = 0; + + protected $db_name = 'admin'; + + protected function getCollection($name) { + return $this->connection->selectCollection($this->db, $name); + } + public function find($collection, $condition = array(), $fields = array(), $cache_key = null) + { + return $this->getCollection($collection)->find($condition, $fields); } - - public function insert($table, $bind, $on_duplicate = array()) + + public function get($collection, $condition, $fields = array()) { - return parent::insert($table, $bind, $on_duplicate); + return $this->getCollection($collection)->findOne($condition, $fields); } - public function update($table, $bind, $where = '') + public function insert($collection, $data, $safe = true) { - return parent::update($table, $bind, $where); + $result = $this->getCollection($collection)->insert($data, array('safe' => $safe)); + if (isset($result['ok']) && $result['ok'] == 1) { + $this->last_inset_id = (string) $data['_id']; + return true; + } else { + return false; + } } - public function delete($table, $where = '') + public function update($collection, $data, $condition = array(), $upsert = false, $safe = true) { - return parent::delete($table, $where); + $result = $this->getCollection($collection)->update($condition, $data, array('upsert' => $upsert, 'safe' => $safe)); + + if (isset($result['ok']) && $result['ok'] == 1) { + if(isset($result['updatedExisting']) && isset($result['upserted'])) { + $this->last_inset_id = (string) $result['upserted']; + } + return $result['n']; + } else { + return false; + } + } + + public function delete($collection, $condition = array(), $safe = true) + { + $result = $this->getCollection($collection)->remove($condition, array('safe' => $safe)); + if (isset($result['ok']) && $result['ok'] == 1) { + return $result['n']; + } else { + return false; + } } public function getInsertId($table = null, $key = null) { - // TODO: Implement getInsertId() method. + return $this->last_inset_id; } public function isConnected() @@ -72,6 +107,7 @@ class MongoDriver extends NoSqlDbDriver ); $this->connection = new Mongo('mongodb://' . $host . $port, $this->config); + $this->db = $this->connection->selectDB($this->config['db']); } } diff --git a/model/NoSqlDbDriver.php b/model/NoSqlDbDriver.php index 66a1f8f..b82e3d3 100644 --- a/model/NoSqlDbDriver.php +++ b/model/NoSqlDbDriver.php @@ -9,5 +9,6 @@ abstract class NoSqlDbDriver extends DbDriver { - abstract function find($data, $params = array(), $cache_key = null); + + abstract function find($collection, $condition = array(), $fields = array(), $cache_key = null); } \ No newline at end of file diff --git a/tests/model/MongoDriverTest.php b/tests/model/MongoDriverTest.php index bcd0159..39aa4bc 100644 --- a/tests/model/MongoDriverTest.php +++ b/tests/model/MongoDriverTest.php @@ -28,7 +28,45 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase 'password' => '1234', 'port' => 27017 ); + + $data = array( + array( + 'name' => 'bread', + 'price' => 3.2, + 'quantity' => 10 + ), + array( + 'name' => 'eggs', + 'price' => 2.1, + 'quantity' => 20 + ), + array( + 'name' => 'fish', + 'price' => 13.2, + 'quantity' => 2 + ), + array( + 'name' => 'milk', + 'price' => 3.8, + 'quantity' => 1 + ), + array( + 'name' => 'eggs', + 'price' => 2.3, + 'quantity' => 5 + ) + ); + $connection = new Mongo('mongodb://' . $this->conf['hostname'] . ':' . $this->conf['port']); + $db = $connection->selectDB($this->conf['database']); + $db->authenticate($this->conf['username'], $this->conf['password']); + $collection = 'items'; + $db->dropCollection($collection); + $collection = $db->selectCollection($collection); + foreach($data as $document) { + $collection->insert($document); + } } + /** * @expectedException Exception * @expectedExceptionMessage Configuration must have a "hostname". @@ -48,7 +86,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase { $this->conf['password'] = 'nopass'; $mongo = new MongoDriver($this->conf); - $this->assertInstanceOf('Mongo', $mongo->getConnection()); + $this->assertInstanceOf('MongoDB', $mongo->getConnection()); } public function testGetConnection() @@ -58,8 +96,71 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase $this->assertFalse($mongo->isConnected()); $this->assertInstanceOf('Mongo', $mongo->getConnection()); $this->assertTrue($mongo->isConnected()); + $mongo->getConnection(); $mongo->disconnect(); $this->assertFalse($mongo->isConnected()); } + public function testFind() + { + $mongo = new MongoDriver($this->conf); + $mongo->getConnection(); + $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->count()); + $this->assertEquals(2, $mongo->find('items', array('name' => 'eggs'))->count()); + $this->assertEquals(3, $mongo->find('items', array('price' => array('$lt' => 3.5)))->count()); + $this->assertEquals(5, $mongo->find('items', array())->count()); + } + + public function testGet() + { + $mongo = new MongoDriver($this->conf); + $mongo->getConnection(); + $bread = $mongo->get('items', array('name' => 'bread')); + $this->assertEquals(3.2, $bread['price']); + } + + public function testRemove() + { + $mongo = new MongoDriver($this->conf); + $mongo->getConnection(); + $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->count()); + $this->assertEquals(0, $mongo->delete('items', array('name' => 'esggs'))); + $this->assertEquals(2, $mongo->delete('items', array('name' => 'eggs'))); + $this->assertEquals(1, $mongo->delete('items', array('name' => 'bread'))); + $this->assertEquals(0, $mongo->find('items', array('name' => 'bread'))->count()); + } + + public function testInsert() + { + $mongo = new MongoDriver($this->conf); + $mongo->getConnection(); + $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->count()); + $this->assertTrue($mongo->insert('items', array('name' => 'bread'))); + $this->assertNotEmpty($mongo->getInsertId()); + $this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->count()); + } + + public function testUpdate() + { + $mongo = new MongoDriver($this->conf); + $mongo->getConnection(); + $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->count()); + $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'fish')); + $fish = $mongo->get('items', array('name' => 'fish')); + $this->assertEquals(200, $fish['price']); + $this->assertEquals('today', $fish['date']); + $this->assertEquals(0, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'))); + } + + public function testUpsert() + { + $mongo = new MongoDriver($this->conf); + $mongo->getConnection(); + + $this->assertTrue($mongo->insert('items', array('name' => 'bread'))); + $id = $mongo->getInsertId(); + $this->assertNotEmpty($id); + $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true)); + $this->assertNotEquals($id, $mongo->getInsertId()); + } } \ No newline at end of file