| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2008-12-05 13:32:38 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Класс модели данных | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2008-12-09 05:58:08 +00:00
										 |  |  |  * @copyright | 
					
						
							|  |  |  |  * @link | 
					
						
							| 
									
										
										
										
											2008-12-05 13:32:38 +00:00
										 |  |  |  * @package Majestic | 
					
						
							|  |  |  |  * @subpackage DB | 
					
						
							| 
									
										
										
										
											2008-12-09 05:58:08 +00:00
										 |  |  |  * @since | 
					
						
							| 
									
										
										
										
											2008-12-05 13:32:38 +00:00
										 |  |  |  * @version SVN: $Id$ | 
					
						
							|  |  |  |  * @filesource $URL$ | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | abstract class Model | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     private $handler; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected $table = false; | 
					
						
							|  |  |  |     protected $primary_key = 'id'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-04-24 12:34:29 +00:00
										 |  |  |         $this->handler = DBConnector::getConnect(Env::getParam('db_settings')); | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |     * Выполняет запрос и возвращает сырой результат | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * @param string $sql | 
					
						
							|  |  |  |     * @return resource | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |     function exec($sql) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-02-03 10:49:08 +00:00
										 |  |  |         if (DEBUG_ENABLE) { | 
					
						
							|  |  |  |             $time = microtime(true); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-04-24 12:34:29 +00:00
										 |  |  |         $res = DBConnector::query($this->handler, $sql); | 
					
						
							|  |  |  |         if ($error = DBConnector::error($this->handler)) { | 
					
						
							|  |  |  |             throw new MJException("<b>Query Error:</b>\n".$sql."\n<b>Error:</b>\n".$error, 1); | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (DEBUG_ENABLE) { | 
					
						
							|  |  |  |             DBConnector::$queries[] = $sql.'; ('.round((microtime(true)-$time)*1000, 1).'ms)'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $res; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |     * Выполняет запрос и возвращает объект результата | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * @param string $sql | 
					
						
							|  |  |  |     * @return object | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |     function query($sql) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $res = $this->exec($sql); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         switch (strtolower(substr($sql, 0, 6))) { | 
					
						
							|  |  |  |             case 'select': | 
					
						
							| 
									
										
										
										
											2008-12-09 13:19:13 +00:00
										 |  |  |             case '(selec': | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  |                 return new ModelSelectResult($res); | 
					
						
							|  |  |  |             case 'insert': | 
					
						
							| 
									
										
										
										
											2008-12-16 08:13:54 +00:00
										 |  |  |             case 'replac': | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  |                 return new ModelInsertResult($this->handler); | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 return new ModelChangeResult($this->handler); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2008-12-23 13:19:29 +00:00
										 |  |  |      * Экранирует строку | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param mixed $data - строка для экранирования | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  |     function escape($data) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-12-23 13:19:29 +00:00
										 |  |  |         if(is_array($data)){ | 
					
						
							|  |  |  |             foreach($data as $id => $val){ | 
					
						
							| 
									
										
										
										
											2009-04-24 12:34:29 +00:00
										 |  |  |                 $data[$id] = DBConnector::escape($this->handler, $val); | 
					
						
							| 
									
										
										
										
											2008-12-23 13:19:29 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |             return $data; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-04-24 12:34:29 +00:00
										 |  |  |         return DBConnector::escape($this->handler, $data); | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //////////////////////////
 | 
					
						
							|  |  |  |     function update($id, $data) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $sql = ''; | 
					
						
							|  |  |  |         foreach ($data as $key => $val) { | 
					
						
							|  |  |  |             $sql .= $key."='".$this->escape($val)."', "; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $this->query('UPDATE '.$this->table.' SET '.rtrim($sql, ', ').' WHERE '.$this->primary_key.'='.(int) $id); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function insert($data, $postfix = '') | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $sql = ''; | 
					
						
							|  |  |  |         foreach ($data as $key => $val) { | 
					
						
							|  |  |  |             $sql .= $key.'="'.$this->escape($val).'", '; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $this->query('INSERT '.$this->table.' SET '.rtrim($sql, ', ').' '.$postfix); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function delete($id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->query('DELETE FROM '.$this->table.' WHERE '.$this->primary_key.'='.(int) $id); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function get($id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->query('SELECT * FROM '.$this->table.' WHERE '.$this->primary_key.'='.(int) $id); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function getList($limit = false, $sort = 'ASC') | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-12-29 16:16:58 +00:00
										 |  |  |         return $this->query('SELECT * | 
					
						
							|  |  |  |                                 FROM '.$this->table.' | 
					
						
							|  |  |  |                                 ORDER BY '.$this->primary_key.' '.($sort == 'ASC' ? 'ASC' : 'DESC') | 
					
						
							|  |  |  |                                 .($limit !== false ? ' LIMIT '.(int) $limit : ''))->fetchAll(); | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-04-06 09:37:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function setAutocommit($set) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-04-24 12:34:29 +00:00
										 |  |  |         return DBConnector::autocommit($this->handler, (bool) $set); | 
					
						
							| 
									
										
										
										
											2009-04-06 09:37:23 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function commit() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-04-24 12:34:29 +00:00
										 |  |  |         return DBConnector::commit($this->handler); | 
					
						
							| 
									
										
										
										
											2009-04-06 09:37:23 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function rollback() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-04-24 12:34:29 +00:00
										 |  |  |         return DBConnector::rollback($this->handler); | 
					
						
							| 
									
										
										
										
											2009-04-06 09:37:23 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ModelResult | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     function __call($name, $args) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         throw new MJException('Call undeclared method "'.$name.'" in "'.get_class($this).'" class', -1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ModelSelectResult extends ModelResult | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public $result; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function __construct($res) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->result  = $res; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-09 05:58:08 +00:00
										 |  |  |     function fetch($class_name = false) | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-04-24 12:34:29 +00:00
										 |  |  |         return DBConnector::fetchObject($this->result, $class_name); | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function fetchField($field, $default = false) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $row = $this->fetch(); | 
					
						
							|  |  |  |         return isset($row->$field) ? $row->$field : $default; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-03 11:22:33 +00:00
										 |  |  |     function fetchAll($key = false) | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         $array = array(); | 
					
						
							| 
									
										
										
										
											2008-12-03 11:22:33 +00:00
										 |  |  |         if ($key) { | 
					
						
							| 
									
										
										
										
											2009-04-24 12:34:29 +00:00
										 |  |  |             while ($row = DBConnector::fetchObject($this->result)) { | 
					
						
							| 
									
										
										
										
											2008-12-03 11:22:33 +00:00
										 |  |  |                 $array[$row->$key] = $row; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2009-04-24 12:34:29 +00:00
										 |  |  |             while ($row = DBConnector::fetchObject($this->result)) { | 
					
						
							| 
									
										
										
										
											2008-12-03 11:22:33 +00:00
										 |  |  |                 $array[] = $row; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         return $array; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function count() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-04-24 12:34:29 +00:00
										 |  |  |         return DBConnector::numRows($this->result); | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function free() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-04-24 12:34:29 +00:00
										 |  |  |         DBConnector::free($this->result); | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function __destruct() { | 
					
						
							|  |  |  |         $this->free(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ModelChangeResult extends ModelResult | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public $affected; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function __construct($resource) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-04-24 12:34:29 +00:00
										 |  |  |         $this->affected = DBConnector::affectedRows($resource); | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function count() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->affected; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 13:29:49 +00:00
										 |  |  | class ModelInsertResult extends ModelChangeResult | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     public $id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function __construct($resource) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-12-17 13:29:49 +00:00
										 |  |  |         parent::__construct($resource); | 
					
						
							| 
									
										
										
										
											2009-04-24 12:34:29 +00:00
										 |  |  |         $this->id = DBConnector::getId($resource); | 
					
						
							| 
									
										
										
										
											2008-12-02 09:03:33 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function getId() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->id; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ?>
 |