modified MongoCommand classes

This commit is contained in:
Anton Grebnev
2011-11-15 13:14:31 +04:00
parent 1f0bfb131b
commit 229a39d682
2 changed files with 64 additions and 33 deletions

View File

@ -18,15 +18,19 @@ class MongoCommandBuilder
const REMOVE = 'Remove';
static public function factory($type)
static public function factory($type, $collection = null)
{
$class = ucfirst($type) . 'MongoCommand';
return new $class();
$command = new $class();
$command->setCollection($collection);
return $command;
}
}
abstract class MongoDbCommand
{
protected $collection;
public function execute()
{
if ($this->checkParams()) {
@ -38,17 +42,29 @@ abstract class MongoDbCommand
public function bindParam($name, $value)
{
$this->$name = $value;
if (property_exists($this, $name)) {
$this->$name = $value;
}
return $this;
}
abstract protected function concreteExecute();
public function setCollection($collection)
{
$this->collection = $collection;
return $this;
}
abstract protected function concreteExecute();
abstract protected function checkParams();
}
class FindMongoCommand extends MongoDbCommand
{
protected $condition;
protected $fields;
protected function concreteExecute()
{
return $this->collection->find($this->condition, $this->fields);
@ -66,6 +82,10 @@ class FindMongoCommand extends MongoDbCommand
class InsertMongoCommand extends MongoDbCommand
{
protected $data;
protected $safe;
protected function concreteExecute()
{
return $this->collection->insert($this->data, array('safe' => $this->safe));
@ -83,6 +103,14 @@ class InsertMongoCommand extends MongoDbCommand
class UpdateMongoCommand extends MongoDbCommand
{
protected $condition;
protected $data;
protected $upsert;
protected $safe;
protected function concreteExecute()
{
return $this->collection->update($this->condition, $this->data, array('upsert' => $this->upsert, 'safe' => $this->safe));
@ -102,6 +130,10 @@ class UpdateMongoCommand extends MongoDbCommand
class RemoveMongoCommand extends MongoDbCommand
{
protected $condition;
protected $safe;
protected function concreteExecute()
{
return $this->collection->remove($this->condition, array('safe' => $this->safe));