Merge branch 'master' into mongo

This commit is contained in:
Anton Grebnev
2012-04-12 21:02:23 +04:00
14 changed files with 195 additions and 69 deletions

View File

@ -12,6 +12,8 @@ class MongoCommandBuilder
const FIND = 'Find';
const COUNT = 'Count';
const INSERT = 'Insert';
const UPDATE = 'Update';
@ -68,9 +70,9 @@ abstract class MongoDbCommand
class FindMongoCommand extends MongoDbCommand
{
protected $condition;
protected $condition = array();
protected $fields;
protected $fields = array();
protected $multiple = true;
@ -95,11 +97,12 @@ class FindMongoCommand extends MongoDbCommand
public function __toString()
{
if ($this->checkParams()) {
$result = 'Collection: ' . $this->collection . PHP_EOL;
$result = PHP_EOL . 'Collection: ' . $this->collection . PHP_EOL;
ob_start();
var_dump($this->condition);
$condition = ob_get_clean();
$result .= 'Condition: ' . $condition . PHP_EOL;
$result .= 'Condition: ' . $condition;
$result .= 'Type: FIND' . PHP_EOL;
ob_start();
var_dump($this->fields);
$fields = ob_get_clean();
@ -113,6 +116,48 @@ class FindMongoCommand extends MongoDbCommand
}
}
class CountMongoCommand extends MongoDbCommand
{
protected $condition = array();
protected $limit = 0;
protected $skip = 0;
protected $multiple = true;
protected function concreteExecute()
{
return $this->collection->count($this->condition, $this->limit, $this->skip);
}
protected function checkParams()
{
if (isset($this->collection) && isset($this->condition) && isset($this->limit) && isset($this->skip)) {
return true;
} else {
return false;
}
}
public function __toString()
{
if ($this->checkParams()) {
$result = PHP_EOL . 'Collection: ' . $this->collection . PHP_EOL;
$result .= 'Type: COUNT' . PHP_EOL;
ob_start();
var_dump($this->condition);
$condition = ob_get_clean();
$result .= 'Condition: ' . $condition;
$result .= 'Limit: ' . $this->limit . PHP_EOL;
$result .= 'Skip: ' . $this->skip . PHP_EOL;
return $result;
} else {
return 'Command properties not set';
}
}
}
class InsertMongoCommand extends MongoDbCommand
{
protected $data;
@ -154,11 +199,12 @@ class InsertMongoCommand extends MongoDbCommand
public function __toString()
{
if ($this->checkParams()) {
$result = 'Collection: ' . $this->collection . PHP_EOL;
$result = PHP_EOL . 'Collection: ' . $this->collection . PHP_EOL;
$result .= 'Type: INSERT' . PHP_EOL;
ob_start();
var_dump($this->data);
$data = ob_get_clean();
$result .= 'Data: ' . $data . PHP_EOL;
$result .= 'Data: ' . $data;
$mult = $this->multiple ? 'TRUE' : 'FALSE';
$result .= 'Bulk insert: ' . $mult . PHP_EOL;
$safe = $this->safe ? 'TRUE' : 'FALSE';
@ -202,15 +248,16 @@ class UpdateMongoCommand extends MongoDbCommand
public function __toString()
{
if ($this->checkParams()) {
$result = 'Collection: ' . $this->collection . PHP_EOL;
$result = PHP_EOL . 'Collection: ' . $this->collection . PHP_EOL;
$result .= 'Type: UPDATE' . PHP_EOL;
ob_start();
var_dump($this->condition);
$condition = ob_get_clean();
$result .= 'Condition: ' . $condition . PHP_EOL;
$result .= 'Condition: ' . $condition;
ob_start();
var_dump($this->data);
$data = ob_get_clean();
$result .= 'Data: ' . $data . PHP_EOL;
$result .= 'Data: ' . $data;
$mult = $this->multiple ? 'TRUE' : 'FALSE';
$result .= 'Multiple fields: ' . $mult . PHP_EOL;
$upsert = $this->upsert ? 'TRUE' : 'FALSE';
@ -247,11 +294,12 @@ class RemoveMongoCommand extends MongoDbCommand
public function __toString()
{
if ($this->checkParams()) {
$result = 'Collection: ' . $this->collection . PHP_EOL;
$result = PHP_EOL . 'Collection: ' . $this->collection . PHP_EOL;
$result .= 'Type: REMOVE' . PHP_EOL;
ob_start();
var_dump($this->condition);
$condition = ob_get_clean();
$result .= 'Condition: ' . $condition . PHP_EOL;
$result .= 'Condition: ' . $condition;
$safe = $this->safe ? 'TRUE' : 'FALSE';
$result .= 'Safe operation: ' . $safe . PHP_EOL;
return $result;
@ -287,11 +335,12 @@ class CommandMongoCommand extends MongoDbCommand
public function __toString()
{
if ($this->checkParams()) {
$result = 'Collection: ' . $this->collection . PHP_EOL;
$result = PHP_EOL . 'Collection: ' . $this->collection . PHP_EOL;
$result .= 'Type: COMMAND' . PHP_EOL;
ob_start();
var_dump($this->command);
$command = ob_get_clean();
$result .= 'Command: ' . $command . PHP_EOL;
$result .= 'Command: ' . $command;
return $result;
} else {
return 'Command properties not set';

View File

@ -28,7 +28,14 @@ class MongoDriver extends NoSqlDbDriver
public function count($collection, $query = array(), $limit = 0, $skip = 0)
{
return $this->getCollection($collection)->count($query, $limit, $skip);
$command = MongoCommandBuilder::factory(MongoCommandBuilder::COUNT, $this->getCollection($collection));
$params = array(
'condition' => $query,
'limit' => $limit,
'skip' => $skip
);
return $this->query($command, $params)->affectedRows();
}
public function find($collection, $condition = array(), $fields = array())