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.

192 lines
4.3 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. return new ModelSelectResult($res);
  52. case 'insert':
  53. return new ModelInsertResult($this->handler);
  54. default:
  55. return new ModelChangeResult($this->handler);
  56. }
  57. }
  58. /**
  59. * Экранирует строку
  60. *
  61. * @param string $data - строка для экранирования
  62. * @return string
  63. */
  64. function escape($data)
  65. {
  66. return mysqli_real_escape_string($this->handler, $data);
  67. }
  68. //////////////////////////
  69. function update($id, $data)
  70. {
  71. $sql = '';
  72. foreach ($data as $key => $val) {
  73. $sql .= $key."='".$this->escape($val)."', ";
  74. }
  75. return $this->query('UPDATE '.$this->table.' SET '.rtrim($sql, ', ').' WHERE '.$this->primary_key.'='.(int) $id);
  76. }
  77. function insert($data, $postfix = '')
  78. {
  79. $sql = '';
  80. foreach ($data as $key => $val) {
  81. $sql .= $key.'="'.$this->escape($val).'", ';
  82. }
  83. return $this->query('INSERT '.$this->table.' SET '.rtrim($sql, ', ').' '.$postfix);
  84. }
  85. function delete($id)
  86. {
  87. return $this->query('DELETE FROM '.$this->table.' WHERE '.$this->primary_key.'='.(int) $id);
  88. }
  89. function get($id)
  90. {
  91. return $this->query('SELECT * FROM '.$this->table.' WHERE '.$this->primary_key.'='.(int) $id);
  92. }
  93. function getList($limit = false, $sort = 'ASC')
  94. {
  95. return $this->query('SELECT * FROM '.$this->table.' ORDER BY '.$this->primary_key.' '.($sort == 'ASC' ? 'ASC' : 'DESC').($limit === false ? ' LIMIT '.(int) $limit : ''));
  96. }
  97. }
  98. class ModelResult
  99. {
  100. function __call($name, $args)
  101. {
  102. throw new MJException('Call undeclared method "'.$name.'" in "'.get_class($this).'" class', -1);
  103. }
  104. }
  105. class ModelSelectResult extends ModelResult
  106. {
  107. public $result;
  108. function __construct($res)
  109. {
  110. $this->result = $res;
  111. }
  112. function fetch()
  113. {
  114. return mysqli_fetch_object($this->result);
  115. }
  116. function fetchField($field, $default = false)
  117. {
  118. $row = $this->fetch();
  119. return isset($row->$field) ? $row->$field : $default;
  120. }
  121. function fetchAll()
  122. {
  123. $array = array();
  124. while ($row = mysqli_fetch_object($this->result)) {
  125. $array[] = $row;
  126. }
  127. return $array;
  128. }
  129. function count()
  130. {
  131. return mysqli_num_rows($this->result);
  132. }
  133. function free()
  134. {
  135. mysqli_free_result($this->result);
  136. }
  137. function __destruct() {
  138. $this->free();
  139. }
  140. }
  141. class ModelChangeResult extends ModelResult
  142. {
  143. public $affected;
  144. function __construct($resource)
  145. {
  146. $this->affected = mysql_affected_rows($resource);
  147. }
  148. function count()
  149. {
  150. return $this->affected;
  151. }
  152. }
  153. class ModelInsertResult extends ModelResult
  154. {
  155. public $id;
  156. function __construct($resource)
  157. {
  158. $this->id = mysqli_insert_id($resource);
  159. }
  160. function getId()
  161. {
  162. return $this->id;
  163. }
  164. }
  165. ?>