Mongo driver with separate CRUD commsnds, without profiler

This commit is contained in:
Anton Grebnev
2011-11-15 11:12:20 +04:00
parent 3c5a5d9603
commit 488655b788
3 changed files with 151 additions and 13 deletions

View File

@ -13,30 +13,65 @@
class MongoDriver extends NoSqlDbDriver
{
public function find($data, $params = array(), $cache_key = null)
{
}
public function insert($table, $bind, $on_duplicate = array())
protected $last_inset_id = 0;
protected $db_name = 'admin';
protected function getCollection($name)
{
return parent::insert($table, $bind, $on_duplicate);
return $this->connection->selectCollection($this->db, $name);
}
public function update($table, $bind, $where = '')
public function find($collection, $condition = array(), $fields = array(), $cache_key = null)
{
return parent::update($table, $bind, $where);
return $this->getCollection($collection)->find($condition, $fields);
}
public function delete($table, $where = '')
public function get($collection, $condition, $fields = array())
{
return parent::delete($table, $where);
return $this->getCollection($collection)->findOne($condition, $fields);
}
public function insert($collection, $data, $safe = true)
{
$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 update($collection, $data, $condition = array(), $upsert = false, $safe = true)
{
$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']);
}
}

View File

@ -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);
}