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.

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