| 
									
										
										
										
											2011-11-15 16:55:12 +04:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @copyright NetMonsters <team@netmonsters.ru> | 
					
						
							|  |  |  |  * @link http://netmonsters.ru | 
					
						
							|  |  |  |  * @package Majestic | 
					
						
							|  |  |  |  * @subpackage db | 
					
						
							|  |  |  |  * @since 2011-11-15 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @property MongoDriver $driver | 
					
						
							|  |  |  |  * @property MongoCursor $result | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class MongoStatement extends DbStatement | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-17 15:12:47 +04:00
										 |  |  |     protected $insertId = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-22 16:29:00 +04:00
										 |  |  |     public function order($sort = array()) | 
					
						
							| 
									
										
										
										
											2011-11-15 19:19:30 +04:00
										 |  |  |     { | 
					
						
							|  |  |  |         if ($this->result instanceof MongoCursor) { | 
					
						
							|  |  |  |             $this->result->sort($sort); | 
					
						
							|  |  |  |             return $this; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             throw new Exception('MongoStatement error. Impossible order results of opened cursor.'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function skip($skip = 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($this->result instanceof MongoCursor) { | 
					
						
							|  |  |  |             $this->result->skip($skip); | 
					
						
							|  |  |  |             return $this; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             throw new Exception('MongoStatement error. Impossible skip results of opened cursor.'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function limit($limit = 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($this->result instanceof MongoCursor) { | 
					
						
							|  |  |  |             $this->result->limit($limit); | 
					
						
							|  |  |  |             return $this; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             throw new Exception('MongoStatement error. Impossible limit results of opened cursor.'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-15 16:55:12 +04:00
										 |  |  |     public function fetch($style = Db::FETCH_OBJ) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!$this->result) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $row = false; | 
					
						
							|  |  |  |         switch ($style) { | 
					
						
							|  |  |  |             case Db::FETCH_OBJ: | 
					
						
							|  |  |  |                 $row = $this->fetchObject(); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case Db::FETCH_ASSOC: | 
					
						
							|  |  |  |                 if ($this->result instanceof MongoCursor) { | 
					
						
							|  |  |  |                     $row = $this->result->getNext(); | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     $row = $this->result; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 throw new Exception('Invalid fetch mode "' . $style . '" specified'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $row; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function fetchObject($class = 'stdClass') | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($this->result instanceof MongoCursor) { | 
					
						
							|  |  |  |             $row = $this->result->getNext(); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $row = $this->result; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (is_array($row) && isset($row['_id'])) { | 
					
						
							|  |  |  |             $row = new ArrayObject($row, ArrayObject::ARRAY_AS_PROPS); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $row = false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $row; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function close() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-11-15 18:06:35 +04:00
										 |  |  |         $this->result = null; | 
					
						
							| 
									
										
										
										
											2011-11-15 16:55:12 +04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function affectedRows() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (is_array($this->result)) { | 
					
						
							| 
									
										
										
										
											2011-11-22 16:29:00 +04:00
										 |  |  |             if (isset($this->result['ok']) && $this->result['ok'] == 1) { | 
					
						
							|  |  |  |                 if (isset($this->result['n'])) { | 
					
						
							|  |  |  |                     return $this->result['n']; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2011-11-15 16:55:12 +04:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function numRows() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($this->result instanceof MongoCursor) { | 
					
						
							|  |  |  |             return $this->result->count(); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2011-11-22 16:29:00 +04:00
										 |  |  |      * @param MongoDbCommand $request | 
					
						
							| 
									
										
										
										
											2011-11-15 16:55:12 +04:00
										 |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function driverExecute($request) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $mongo = $this->driver->getConnection(); | 
					
						
							|  |  |  |         if ($mongo instanceof Mongo) { | 
					
						
							|  |  |  |             if (DEBUG) { | 
					
						
							|  |  |  |                 $profiler = Profiler::getInstance()->profilerCommand('Mongo', $request); | 
					
						
							|  |  |  |                 $result = $request->execute(); | 
					
						
							|  |  |  |                 $profiler->end(); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $result = $request->execute(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if ($result === false) { | 
					
						
							|  |  |  |                 throw new Exception('MongoDB request error.'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if ($result instanceof MongoCursor || is_array($result)) { | 
					
						
							|  |  |  |                 $this->result = $result; | 
					
						
							| 
									
										
										
										
											2011-11-22 16:29:00 +04:00
										 |  |  |                 if (is_array($result) && isset($result['value'])) { | 
					
						
							|  |  |  |                     $this->result = $result['value']; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (is_array($result) && isset($result['values'])) { | 
					
						
							|  |  |  |                     $this->result = $result['values']; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2011-11-15 16:55:12 +04:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2011-11-22 16:29:00 +04:00
										 |  |  |             if ($request instanceof InsertMongoCommand) { | 
					
						
							| 
									
										
										
										
											2011-11-17 15:12:47 +04:00
										 |  |  |                 $this->insertId = $request->getInsertId(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2011-11-15 16:55:12 +04:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             throw new Exception('No connection to MongoDB server.'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function bindParam($param, &$value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->request->bindParam($param, $value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function assemble() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->request; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-11-17 15:12:47 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function getInsertId() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->insertId; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-11-15 16:55:12 +04:00
										 |  |  | } |