Просмотр файла engine/classes/query_builder.class.php

Размер файла: 4.19Kb
  1. <?php
  2. /**
  3. * @author Tadochi aka Patsifist <Tadochi@spaces.ru>
  4. * @version 0.1
  5. * Класс Query builder. Наверное и так понятно для чего она
  6. * Пример / Example
  7. * Core::get('query_builder');
  8. * $builder = new Query_builder;
  9. * $builder->
  10. * select('id', 'nick')->
  11. * from('user')->
  12. * where('id', '=', '1')->
  13. * or_where('id', '=', '2')->
  14. * and_where('nick', 'in', '1,2,3,4,5');
  15. * Чтобы сделать запрос
  16. * $sql->query($builder->query);
  17. */
  18.  
  19.  
  20. Class Query_builder
  21. {
  22.  
  23. /**
  24. * $var $operation string Содержит название операции
  25. */
  26.  
  27. public $operation;
  28.  
  29. /**
  30. * $var operands array все допустимые операнды
  31. */
  32. public $operands = array('=', '>', '<', '<>', '!=', '<=', '>=', 'LIKE', 'IN', '<>');
  33.  
  34. /**
  35. * $var query содержит сам запрос
  36. */
  37.  
  38. public $query;
  39.  
  40.  
  41.  
  42. /**
  43. * Хелпер для установки типа операции
  44. */
  45.  
  46. public function operation($operation)
  47. {
  48. if (empty($this->operation))
  49. $this->operation = $operation;
  50. else
  51. exit('Тип операции уже определен');
  52. }
  53.  
  54.  
  55. /**
  56. * Хелпер - конструктор строки с полями
  57. */
  58.  
  59. public function fields_maker(array $fields, $set = false)
  60. {
  61. $i = 0;
  62. $count = count($fields);
  63. $string = null;
  64.  
  65. if ($set)
  66. {
  67. foreach ($fields as $field => $value)
  68. {
  69. $string .= '`' . $field . "` = '".$value."'" . (++$i != $count ? ', ' : null);
  70. }
  71. }
  72. else
  73. {
  74. foreach ($fields as $field)
  75. {
  76. $string .= '`' . $field . '`' . (++$i != $count ? ', ' : null);
  77. }
  78. }
  79. return $string;
  80. }
  81.  
  82. /*
  83. * Хелпер констуктор условий
  84. */
  85.  
  86. public function cond_maker($cond1, $operand, $cond2)
  87. {
  88. if (in_array($operand, $this->operands))
  89. {
  90. if ($operand == 'IN')
  91. return "`".$cond1."` in('".$cond2."') ";
  92. else
  93. return "`".$cond1."` ".$operand." '".$cond2."' ";
  94. }
  95. else
  96. exit('Неизвестный операнд '. $operand . PHP_EOL . '<br />Список операндов: '. implode(',', $this->operands));
  97. }
  98.  
  99.  
  100. /**
  101. * Хелпер для сброса запроса
  102. */
  103.  
  104. public function reset()
  105. {
  106. $this->query = $this->operation = null;
  107. return $this;
  108. }
  109.  
  110. // Operations
  111.  
  112. public function select($fields)
  113. {
  114. $this->operation('select');
  115. $args = func_get_args();
  116. $this->query = 'SELECT ' . (is_array($args[0]) ? $this->fields_maker($args[0]) : $this->fields_maker($args)) . ' ';
  117. return $this;
  118. }
  119.  
  120. public function delete($table)
  121. {
  122. $this->operation('delete');
  123. $this->query = 'DELETE FROM `'.$table.'` ';
  124. return $this;
  125. }
  126.  
  127. public function update($table)
  128. {
  129. $this->operation('update');
  130. $this->query = 'UPDATE `'.$table.'` ';
  131. return $this;
  132. }
  133.  
  134. public function insert($table)
  135. {
  136. $this->operation('insert');
  137. $this->query = 'INSERT INTO `'.$table.'` ';
  138. return $this;
  139. }
  140.  
  141.  
  142.  
  143. public function from($table)
  144. {
  145. $this->operation == 'SELECT' ? $this->query .= ' FROM `' . $table . '` ' : null;
  146. return $this;
  147. }
  148.  
  149. public function order($field, $type)
  150. {
  151. if ($this->operation == 'SELECT')
  152. $this->query .= 'ORDER BY `'.$field.'` ' . (strtoupper($type) == 'DESC' ? 'DESC' : 'ASC') . ' ';
  153. return $this;
  154. }
  155.  
  156. public function limit($limit)
  157. {
  158. $this->query .= $this->operation != 'INSERT' ? 'LIMIT ' . $limit : null;
  159. return $this;
  160. }
  161.  
  162. public function where($cond1, $operand, $cond2)
  163. {
  164. $this->query .= $this->operation != 'INSERT' ? 'WHERE ' . $this->cond_maker($cond1, strtoupper($operand), $cond2) : null;
  165. return $this;
  166. }
  167.  
  168. public function or_where($cond1, $operand, $cond2)
  169. {
  170. $this->query .= $this->operation != 'INSERT' ? 'OR ' . $this->cond_maker($cond1, strtoupper($operand), $cond2) : null;
  171. return $this;
  172. }
  173.  
  174. public function and_where($cond1, $operand, $cond2)
  175. {
  176. $this->query .= $this->operation != 'INSERT' ? 'AND ' . $this->cond_maker($cond1, strtoupper($operand), $cond2) : null;
  177. return $this;
  178. }
  179. }