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

Размер файла: 4.45Kb
  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. //$this->query = ' SET '.$string;
  80. return $string;
  81. }
  82.  
  83. /*
  84. * Хелпер констуктор условий
  85. */
  86.  
  87. public function cond_maker($cond1, $operand, $cond2)
  88. {
  89. if (in_array($operand, $this->operands))
  90. {
  91. if ($operand == 'IN')
  92. return "`".$cond1."` in('".$cond2."') ";
  93. else
  94. return "`".$cond1."` ".$operand." '".$cond2."' ";
  95. }
  96. else
  97. exit('Неизвестный операнд '. $operand . PHP_EOL . '<br />Список операндов: '. implode(',', $this->operands));
  98. }
  99.  
  100.  
  101. /**
  102. * Хелпер для сброса запроса
  103. */
  104.  
  105. public function reset()
  106. {
  107. $this->query = $this->operation = null;
  108. return $this;
  109. }
  110.  
  111. // Operations
  112.  
  113. public function select($fields)
  114. {
  115. $this->operation('SELECT');
  116. $args = func_get_args();
  117. $this->query = 'SELECT ' . (is_array($args[0]) ? $this->fields_maker($args[0]) : $this->fields_maker($args)) . ' ';
  118. return $this;
  119. }
  120.  
  121. public function delete($table)
  122. {
  123. $this->operation('DELETE');
  124. $this->query = 'DELETE FROM `'.$table.'` ';
  125. return $this;
  126. }
  127.  
  128. public function update($table)
  129. {
  130. $this->operation('UPDATE');
  131. $this->query = 'UPDATE `'.$table.'` ';
  132. return $this;
  133. }
  134.  
  135. public function insert($table)
  136. {
  137. $this->operation('INSERT');
  138. $this->query = 'INSERT INTO `'.$table.'` ';
  139. return $this;
  140. }
  141.  
  142.  
  143.  
  144. public function from($table)
  145. {
  146. strtoupper($this->operation) == 'SELECT' ? $this->query .= ' FROM `' . $table . '` ' : null;
  147. return $this;
  148. }
  149.  
  150. public function order($field, $type)
  151. {
  152. if (strtoupper($this->operation) == 'SELECT')
  153. $this->query .= 'ORDER BY `'.$field.'` ' . (strtoupper($type) == 'DESC' ? 'DESC' : 'ASC') . ' ';
  154. return $this;
  155. }
  156.  
  157. public function limit($limit)
  158. {
  159. $this->query .= $this->operation != 'INSERT' ? 'LIMIT ' . $limit : null;
  160. return $this;
  161. }
  162. public function set($args)
  163. {
  164. $set = $this->operation == 'INSERT' || $this->operation = 'UPDATE';
  165. $this->query .= ' SET ' . $this->fields_maker($args, $set);
  166. return $this;
  167. }
  168.  
  169. public function where($cond1, $operand, $cond2)
  170. {
  171. $this->query .= $this->operation != 'INSERT' ? 'WHERE ' . $this->cond_maker($cond1, strtoupper($operand), $cond2) : null;
  172. return $this;
  173. }
  174.  
  175. public function or_where($cond1, $operand, $cond2)
  176. {
  177. $this->query .= $this->operation != 'INSERT' ? 'OR ' . $this->cond_maker($cond1, strtoupper($operand), $cond2) : null;
  178. return $this;
  179. }
  180.  
  181. public function and_where($cond1, $operand, $cond2)
  182. {
  183. $this->query .= $this->operation != 'INSERT' ? 'AND ' . $this->cond_maker($cond1, strtoupper($operand), $cond2) : null;
  184. return $this;
  185. }
  186. }