You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
4.9 KiB
172 lines
4.9 KiB
11 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
11 years ago
|
namespace yii\mongodb;
|
||
11 years ago
|
|
||
|
use yii\base\Object;
|
||
|
use Yii;
|
||
11 years ago
|
use yii\helpers\Json;
|
||
11 years ago
|
|
||
|
/**
|
||
|
* Database represents the Mongo database information.
|
||
|
*
|
||
11 years ago
|
* @property string $name name of this database. This property is read-only.
|
||
|
*
|
||
11 years ago
|
* @author Paul Klimov <klimov.paul@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class Database extends Object
|
||
|
{
|
||
|
/**
|
||
|
* @var \MongoDB Mongo database instance.
|
||
|
*/
|
||
|
public $mongoDb;
|
||
|
/**
|
||
|
* @var Collection[] list of collections.
|
||
|
*/
|
||
|
private $_collections = [];
|
||
11 years ago
|
/**
|
||
|
* @var file\Collection[] list of GridFS collections.
|
||
|
*/
|
||
|
private $_fileCollections = [];
|
||
11 years ago
|
|
||
|
/**
|
||
11 years ago
|
* @return string name of this database.
|
||
|
*/
|
||
|
public function getName()
|
||
|
{
|
||
|
return $this->mongoDb->__toString();
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Returns the Mongo collection with the given name.
|
||
|
* @param string $name collection name
|
||
11 years ago
|
* @param boolean $refresh whether to reload the collection instance even if it is found in the cache.
|
||
11 years ago
|
* @return Collection Mongo collection instance.
|
||
11 years ago
|
*/
|
||
|
public function getCollection($name, $refresh = false)
|
||
|
{
|
||
|
if ($refresh || !array_key_exists($name, $this->_collections)) {
|
||
|
$this->_collections[$name] = $this->selectCollection($name);
|
||
|
}
|
||
|
return $this->_collections[$name];
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Returns Mongo GridFS collection with given prefix.
|
||
|
* @param string $prefix collection prefix.
|
||
11 years ago
|
* @param boolean $refresh whether to reload the collection instance even if it is found in the cache.
|
||
11 years ago
|
* @return file\Collection Mongo GridFS collection.
|
||
11 years ago
|
*/
|
||
|
public function getFileCollection($prefix = 'fs', $refresh = false)
|
||
|
{
|
||
|
if ($refresh || !array_key_exists($prefix, $this->_fileCollections)) {
|
||
|
$this->_fileCollections[$prefix] = $this->selectFileCollection($prefix);
|
||
|
}
|
||
|
return $this->_fileCollections[$prefix];
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Selects collection with given name.
|
||
|
* @param string $name collection name.
|
||
|
* @return Collection collection instance.
|
||
|
*/
|
||
|
protected function selectCollection($name)
|
||
|
{
|
||
|
return Yii::createObject([
|
||
11 years ago
|
'class' => 'yii\mongodb\Collection',
|
||
11 years ago
|
'mongoCollection' => $this->mongoDb->selectCollection($name)
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Selects GridFS collection with given prefix.
|
||
|
* @param string $prefix file collection prefix.
|
||
|
* @return file\Collection file collection instance.
|
||
|
*/
|
||
|
protected function selectFileCollection($prefix)
|
||
|
{
|
||
|
return Yii::createObject([
|
||
11 years ago
|
'class' => 'yii\mongodb\file\Collection',
|
||
11 years ago
|
'mongoCollection' => $this->mongoDb->getGridFS($prefix)
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Creates new collection.
|
||
|
* Note: Mongo creates new collections automatically on the first demand,
|
||
|
* this method makes sense only for the migration script or for the case
|
||
|
* you need to create collection with the specific options.
|
||
|
* @param string $name name of the collection
|
||
|
* @param array $options collection options in format: "name" => "value"
|
||
11 years ago
|
* @return \MongoCollection new Mongo collection instance.
|
||
11 years ago
|
* @throws Exception on failure.
|
||
11 years ago
|
*/
|
||
|
public function createCollection($name, $options = [])
|
||
|
{
|
||
11 years ago
|
$token = $this->getName() . '.create(' . $name . ', ' . Json::encode($options) . ')';
|
||
|
Yii::info($token, __METHOD__);
|
||
|
try {
|
||
|
Yii::beginProfile($token, __METHOD__);
|
||
|
$result = $this->mongoDb->createCollection($name, $options);
|
||
|
Yii::endProfile($token, __METHOD__);
|
||
|
return $result;
|
||
|
} catch (\Exception $e) {
|
||
|
Yii::endProfile($token, __METHOD__);
|
||
|
throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
|
||
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
/**
|
||
|
* Executes Mongo command.
|
||
|
* @param array $command command specification.
|
||
|
* @param array $options options in format: "name" => "value"
|
||
|
* @return array database response.
|
||
11 years ago
|
* @throws Exception on failure.
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function executeCommand($command, $options = [])
|
||
11 years ago
|
{
|
||
11 years ago
|
$token = $this->getName() . '.$cmd(' . Json::encode($command) . ', ' . Json::encode($options) . ')';
|
||
|
Yii::info($token, __METHOD__);
|
||
|
try {
|
||
|
Yii::beginProfile($token, __METHOD__);
|
||
|
$result = $this->mongoDb->command($command, $options);
|
||
|
$this->tryResultError($result);
|
||
|
Yii::endProfile($token, __METHOD__);
|
||
|
return $result;
|
||
|
} catch (\Exception $e) {
|
||
|
Yii::endProfile($token, __METHOD__);
|
||
|
throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Checks if command execution result ended with an error.
|
||
|
* @param mixed $result raw command execution result.
|
||
|
* @throws Exception if an error occurred.
|
||
|
*/
|
||
|
protected function tryResultError($result)
|
||
|
{
|
||
|
if (is_array($result)) {
|
||
|
if (!empty($result['errmsg'])) {
|
||
|
$errorMessage = $result['errmsg'];
|
||
|
} elseif (!empty($result['err'])) {
|
||
|
$errorMessage = $result['err'];
|
||
|
}
|
||
|
if (isset($errorMessage)) {
|
||
|
if (array_key_exists('ok', $result)) {
|
||
|
$errorCode = (int)$result['ok'];
|
||
|
} else {
|
||
|
$errorCode = 0;
|
||
|
}
|
||
|
throw new Exception($errorMessage, $errorCode);
|
||
|
}
|
||
|
} elseif (!$result) {
|
||
|
throw new Exception('Unknown error, use "w=1" option to enable error tracking');
|
||
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|