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.

185 lines
4.2 KiB

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