diff --git a/model/MongoDbCommand.php b/model/MongoDbCommand.php index 0cc81b5..4eae91e 100644 --- a/model/MongoDbCommand.php +++ b/model/MongoDbCommand.php @@ -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; @@ -100,6 +102,7 @@ class FindMongoCommand extends MongoDbCommand var_dump($this->condition); $condition = ob_get_clean(); $result .= 'Condition: ' . $condition . PHP_EOL; + $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 = 'Collection: ' . $this->collection . PHP_EOL; + $result .= 'Type: COUNT' . PHP_EOL; + ob_start(); + var_dump($this->condition); + $condition = ob_get_clean(); + $result .= 'Condition: ' . $condition . PHP_EOL; + $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; @@ -155,6 +200,7 @@ class InsertMongoCommand extends MongoDbCommand { if ($this->checkParams()) { $result = 'Collection: ' . $this->collection . PHP_EOL; + $result .= 'Type: INSERT' . PHP_EOL; ob_start(); var_dump($this->data); $data = ob_get_clean(); @@ -203,6 +249,7 @@ class UpdateMongoCommand extends MongoDbCommand { if ($this->checkParams()) { $result = 'Collection: ' . $this->collection . PHP_EOL; + $result .= 'Type: UPDATE' . PHP_EOL; ob_start(); var_dump($this->condition); $condition = ob_get_clean(); @@ -248,6 +295,7 @@ class RemoveMongoCommand extends MongoDbCommand { if ($this->checkParams()) { $result = 'Collection: ' . $this->collection . PHP_EOL; + $result .= 'Type: REMOVE' . PHP_EOL; ob_start(); var_dump($this->condition); $condition = ob_get_clean(); @@ -288,6 +336,7 @@ class CommandMongoCommand extends MongoDbCommand { if ($this->checkParams()) { $result = 'Collection: ' . $this->collection . PHP_EOL; + $result .= 'Type: COMMAND' . PHP_EOL; ob_start(); var_dump($this->command); $command = ob_get_clean(); diff --git a/model/MongoDriver.php b/model/MongoDriver.php index a6c4ad6..455b0a4 100644 --- a/model/MongoDriver.php +++ b/model/MongoDriver.php @@ -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()) diff --git a/model/MongoStatement.php b/model/MongoStatement.php index 3e43f0a..02c5254 100644 --- a/model/MongoStatement.php +++ b/model/MongoStatement.php @@ -112,6 +112,8 @@ class MongoStatement extends DbStatement } else { return false; } + } elseif (is_int($this->result)) { + return $this->result; } return false; } @@ -151,6 +153,8 @@ class MongoStatement extends DbStatement if (is_array($result) && isset($result['values'])) { $this->result = $result['values']; } + } elseif (is_int($result)) { + $this->result = $result; } if ($request instanceof InsertMongoCommand) { $this->insertId = $request->getInsertId();