|
|
<?php /** * @copyright NetMonsters <team@netmonsters.ru> * @link http://netmonsters.ru * @package Majestic * @subpackage db * @since 2011-11-10 */
/** * @property Mongo $connection */ class MongoDriver extends NoSqlDbDriver {
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 get($collection, $condition, $fields = array()) { 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) { return $this->last_inset_id; }
public function isConnected() { if (!is_null($this->connection)) { return $this->connection->connected; } else { return false; } }
public function disconnect() { if ($this->isConnected()) { $this->connection->close(); } $this->connection = null; }
protected function connect() { if ($this->connection) { return; }
$host = $this->config['hostname']; $port = isset($this->config['port']) ? ':' . (string) $this->config['port'] : '';
$this->config = array( 'username' => $this->config['username'], 'password' => $this->config['password'], 'db' => $this->config['database'] );
$this->connection = new Mongo('mongodb://' . $host . $port, $this->config); $this->db = $this->connection->selectDB($this->config['db']); } }
|