You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
5.0 KiB

  1. <?php
  2. /**
  3. * Класс модели данных
  4. *
  5. * @copyright
  6. * @link
  7. * @package Majestic
  8. * @subpackage DB
  9. * @since
  10. * @version SVN: $Id$
  11. * @filesource $URL$
  12. */
  13. abstract class Model
  14. {
  15. private $handler;
  16. protected $table = false;
  17. protected $primary_key = 'id';
  18. function __construct()
  19. {
  20. $this->handler = DBConnector::getInstance()->getConnect(Env::getParam('db_settings'));
  21. }
  22. /**
  23. * Выполняет запрос и возвращает сырой результат
  24. *
  25. * @param string $sql
  26. * @return resource
  27. */
  28. function exec($sql)
  29. {
  30. if (DEBUG_ENABLE) {
  31. $time = microtime(true);
  32. }
  33. $res = mysqli_query($this->handler, $sql);
  34. if (mysqli_errno($this->handler)) {
  35. throw new MJException("<b>Query Error:</b>\n".$sql."\n<b>Error:</b>\n".mysqli_error($this->handler), 1);
  36. }
  37. if (DEBUG_ENABLE) {
  38. DBConnector::$queries[] = $sql.'; ('.round((microtime(true)-$time)*1000, 1).'ms)';
  39. }
  40. return $res;
  41. }
  42. /**
  43. * Выполняет запрос и возвращает объект результата
  44. *
  45. * @param string $sql
  46. * @return object
  47. */
  48. function query($sql)
  49. {
  50. $res = $this->exec($sql);
  51. switch (strtolower(substr($sql, 0, 6))) {
  52. case 'select':
  53. case '(selec':
  54. return new ModelSelectResult($res);
  55. case 'insert':
  56. case 'replac':
  57. return new ModelInsertResult($this->handler);
  58. default:
  59. return new ModelChangeResult($this->handler);
  60. }
  61. }
  62. /**
  63. * Экранирует строку
  64. *
  65. * @param mixed $data - строка для экранирования
  66. * @return mixed
  67. */
  68. function escape($data)
  69. {
  70. if(is_array($data)){
  71. foreach($data as $id => $val){
  72. $data[$id] = mysqli_real_escape_string($this->handler, $val);
  73. }
  74. return $data;
  75. }
  76. return mysqli_real_escape_string($this->handler, $data);
  77. }
  78. //////////////////////////
  79. function update($id, $data)
  80. {
  81. $sql = '';
  82. foreach ($data as $key => $val) {
  83. $sql .= $key."='".$this->escape($val)."', ";
  84. }
  85. return $this->query('UPDATE '.$this->table.' SET '.rtrim($sql, ', ').' WHERE '.$this->primary_key.'='.(int) $id);
  86. }
  87. function insert($data, $postfix = '')
  88. {
  89. $sql = '';
  90. foreach ($data as $key => $val) {
  91. $sql .= $key.'="'.$this->escape($val).'", ';
  92. }
  93. return $this->query('INSERT '.$this->table.' SET '.rtrim($sql, ', ').' '.$postfix);
  94. }
  95. function delete($id)
  96. {
  97. return $this->query('DELETE FROM '.$this->table.' WHERE '.$this->primary_key.'='.(int) $id);
  98. }
  99. function get($id)
  100. {
  101. return $this->query('SELECT * FROM '.$this->table.' WHERE '.$this->primary_key.'='.(int) $id);
  102. }
  103. function getList($limit = false, $sort = 'ASC')
  104. {
  105. return $this->query('SELECT *
  106. FROM '.$this->table.'
  107. ORDER BY '.$this->primary_key.' '.($sort == 'ASC' ? 'ASC' : 'DESC')
  108. .($limit !== false ? ' LIMIT '.(int) $limit : ''))->fetchAll();
  109. }
  110. }
  111. class ModelResult
  112. {
  113. function __call($name, $args)
  114. {
  115. throw new MJException('Call undeclared method "'.$name.'" in "'.get_class($this).'" class', -1);
  116. }
  117. }
  118. class ModelSelectResult extends ModelResult
  119. {
  120. public $result;
  121. function __construct($res)
  122. {
  123. $this->result = $res;
  124. }
  125. function fetch($class_name = false)
  126. {
  127. return $class_name ? mysqli_fetch_object($this->result, $class_name) : mysqli_fetch_object($this->result);
  128. }
  129. function fetchField($field, $default = false)
  130. {
  131. $row = $this->fetch();
  132. return isset($row->$field) ? $row->$field : $default;
  133. }
  134. function fetchAll($key = false)
  135. {
  136. $array = array();
  137. if ($key) {
  138. while ($row = mysqli_fetch_object($this->result)) {
  139. $array[$row->$key] = $row;
  140. }
  141. } else {
  142. while ($row = mysqli_fetch_object($this->result)) {
  143. $array[] = $row;
  144. }
  145. }
  146. return $array;
  147. }
  148. function count()
  149. {
  150. return mysqli_num_rows($this->result);
  151. }
  152. function free()
  153. {
  154. mysqli_free_result($this->result);
  155. }
  156. function __destruct() {
  157. $this->free();
  158. }
  159. }
  160. class ModelChangeResult extends ModelResult
  161. {
  162. public $affected;
  163. function __construct($resource)
  164. {
  165. $this->affected = mysqli_affected_rows($resource);
  166. }
  167. function count()
  168. {
  169. return $this->affected;
  170. }
  171. }
  172. class ModelInsertResult extends ModelChangeResult
  173. {
  174. public $id;
  175. function __construct($resource)
  176. {
  177. parent::__construct($resource);
  178. $this->id = mysqli_insert_id($resource);
  179. }
  180. function getId()
  181. {
  182. return $this->id;
  183. }
  184. }
  185. ?>