added MongoStatement class, modified DbDriver hierarchy
This commit is contained in:
@ -12,66 +12,90 @@
|
||||
*/
|
||||
class MongoDriver extends NoSqlDbDriver
|
||||
{
|
||||
|
||||
|
||||
protected $last_inset_id = 0;
|
||||
protected $last_insert_id = 0;
|
||||
|
||||
protected $db_name = 'admin';
|
||||
|
||||
protected $db = null;
|
||||
|
||||
protected function getCollection($name)
|
||||
{
|
||||
if (!$this->connection) {
|
||||
$this->connect();
|
||||
}
|
||||
return $this->connection->selectCollection($this->db, $name);
|
||||
}
|
||||
|
||||
public function find($collection, $condition = array(), $fields = array(), $cache_key = null)
|
||||
public function find($collection, $condition = array(), $fields = array())
|
||||
{
|
||||
return $this->getCollection($collection)->find($condition, $fields);
|
||||
$command = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->getCollection($collection));
|
||||
$params = array(
|
||||
'condition' => $condition,
|
||||
'fields' => $fields
|
||||
);
|
||||
|
||||
return $this->query($command, $params);
|
||||
}
|
||||
|
||||
public function get($collection, $condition, $fields = array())
|
||||
{
|
||||
return $this->getCollection($collection)->findOne($condition, $fields);
|
||||
$command = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->getCollection($collection));
|
||||
$params = array(
|
||||
'condition' => $condition,
|
||||
'fields' => $fields,
|
||||
'multiple' => false
|
||||
);
|
||||
return $this->query($command, $params);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
$command = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->getCollection($collection));
|
||||
$params = array(
|
||||
'data' => $data,
|
||||
'safe' => $safe
|
||||
);
|
||||
|
||||
return $this->query($command, $params)->affectedRows();
|
||||
}
|
||||
|
||||
public function update($collection, $data, $condition = array(), $upsert = false, $safe = true)
|
||||
public function update($collection, $data, $condition = array(), $multiple = true, $upsert = false, $safe = true)
|
||||
{
|
||||
$result = $this->getCollection($collection)->update($condition, $data, array('upsert' => $upsert, 'safe' => $safe));
|
||||
$command = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->getCollection($collection));
|
||||
$params = array(
|
||||
'data' => $data,
|
||||
'condition' => $condition,
|
||||
'multiple' => $multiple,
|
||||
'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;
|
||||
}
|
||||
return $this->query($command, $params)->affectedRows();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
$command = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->getCollection($collection));
|
||||
$params = array(
|
||||
'condition' => $condition,
|
||||
'safe' => $safe
|
||||
);
|
||||
|
||||
return $this->query($command, $params)->affectedRows();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $request
|
||||
* @return DbStatement
|
||||
*/
|
||||
public function prepare($request)
|
||||
{
|
||||
return new MongoStatement($this, $request);
|
||||
}
|
||||
|
||||
public function getInsertId($table = null, $key = null)
|
||||
{
|
||||
return $this->last_inset_id;
|
||||
return $this->last_insert_id;
|
||||
}
|
||||
|
||||
public function isConnected()
|
||||
@ -110,4 +134,3 @@ class MongoDriver extends NoSqlDbDriver
|
||||
$this->db = $this->connection->selectDB($this->config['db']);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user