PageRenderTime 25ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/database/query/builder.php

http://github.com/fuel/core
PHP | 217 lines | 119 code | 29 blank | 69 comment | 13 complexity | 5574aca032e576f7946f142e378fb280 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP 5.4+ framework.
  4. *
  5. * @package Fuel
  6. * @version 1.9-dev
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2019 Fuel Development Team
  10. * @copyright 2008 - 2009 Kohana Team
  11. * @link https://fuelphp.com
  12. */
  13. namespace Fuel\Core;
  14. abstract class Database_Query_Builder extends \Database_Query
  15. {
  16. /**
  17. * Compiles an array of JOIN statements into an SQL partial.
  18. *
  19. * @param object $db Database instance
  20. * @param array $joins join statements
  21. *
  22. * @return string
  23. */
  24. protected function _compile_join(\Database_Connection$db, array $joins)
  25. {
  26. $statements = array();
  27. foreach ($joins as $join)
  28. {
  29. // Compile each of the join statements
  30. $statements[] = $join->compile($db);
  31. }
  32. return implode(' ', $statements);
  33. }
  34. /**
  35. * Compiles an array of conditions into an SQL partial. Used for WHERE
  36. * and HAVING.
  37. *
  38. * @param object $db Database instance
  39. * @param array $conditions condition statements
  40. *
  41. * @return string
  42. */
  43. protected function _compile_conditions(\Database_Connection$db, array $conditions)
  44. {
  45. $last_condition = NULL;
  46. $sql = '';
  47. foreach ($conditions as $group)
  48. {
  49. // Process groups of conditions
  50. foreach ($group as $logic => $condition)
  51. {
  52. if ($condition === '(')
  53. {
  54. if ( ! empty($sql) AND $last_condition !== '(')
  55. {
  56. // Include logic operator
  57. $sql .= ' '.$logic.' ';
  58. }
  59. $sql .= '(';
  60. }
  61. elseif ($condition === ')')
  62. {
  63. $sql .= ')';
  64. }
  65. else
  66. {
  67. if ( ! empty($sql) AND $last_condition !== '(')
  68. {
  69. // Add the logic operator
  70. $sql .= ' '.$logic.' ';
  71. }
  72. // Split the condition
  73. list($column, $op, $value) = $condition;
  74. // Support DB::expr() as where clause
  75. if ($column instanceOf Database_Expression and $op === null and $value === null)
  76. {
  77. $sql .= (string) $column;
  78. }
  79. else
  80. {
  81. if ($value === NULL)
  82. {
  83. if ($op === '=')
  84. {
  85. // Convert "val = NULL" to "val IS NULL"
  86. $op = 'IS';
  87. }
  88. elseif ($op === '!=')
  89. {
  90. // Convert "val != NULL" to "valu IS NOT NULL"
  91. $op = 'IS NOT';
  92. }
  93. }
  94. // Database operators are always uppercase
  95. $op = strtoupper($op);
  96. if (($op === 'BETWEEN' OR $op === 'NOT BETWEEN') AND is_array($value))
  97. {
  98. // BETWEEN always has exactly two arguments
  99. list($min, $max) = $value;
  100. if (is_string($min) AND array_key_exists($min, $this->_parameters))
  101. {
  102. // Set the parameter as the minimum
  103. $min = $this->_parameters[$min];
  104. }
  105. if (is_string($max) AND array_key_exists($max, $this->_parameters))
  106. {
  107. // Set the parameter as the maximum
  108. $max = $this->_parameters[$max];
  109. }
  110. // Quote the min and max value
  111. $value = $db->quote($min).' AND '.$db->quote($max);
  112. }
  113. else
  114. {
  115. if (is_string($value) AND array_key_exists($value, $this->_parameters))
  116. {
  117. // Set the parameter as the value
  118. $value = $this->_parameters[$value];
  119. }
  120. // Quote the entire value normally
  121. $value = $db->quote($value);
  122. }
  123. // Append the statement to the query
  124. $sql .= $db->quote_identifier($column).' '.$op.' '.$value;
  125. }
  126. }
  127. $last_condition = $condition;
  128. }
  129. }
  130. return $sql;
  131. }
  132. /**
  133. * Compiles an array of set values into an SQL partial. Used for UPDATE.
  134. *
  135. * @param object $db Database instance
  136. * @param array $values updated values
  137. *
  138. * @return string
  139. */
  140. protected function _compile_set(\Database_Connection$db, array $values)
  141. {
  142. $set = array();
  143. foreach ($values as $group)
  144. {
  145. // Split the set
  146. list($column, $value) = $group;
  147. // Quote the column name
  148. $column = $db->quote_identifier($column);
  149. if (is_string($value) AND array_key_exists($value, $this->_parameters))
  150. {
  151. // Use the parameter value
  152. $value = $this->_parameters[$value];
  153. }
  154. $set[$column] = $column.' = '.$db->quote($value);
  155. }
  156. return implode(', ', $set);
  157. }
  158. /**
  159. * Compiles an array of ORDER BY statements into an SQL partial.
  160. *
  161. * @param object $db Database instance
  162. * @param array $columns sorting columns
  163. *
  164. * @return string
  165. */
  166. protected function _compile_order_by(\Database_Connection $db, array $columns)
  167. {
  168. $sort = array();
  169. foreach ($columns as $group)
  170. {
  171. list($column, $direction) = $group;
  172. $direction = strtoupper($direction);
  173. if ( ! empty($direction))
  174. {
  175. // Make the direction uppercase
  176. $direction = ' '.($direction == 'ASC' ? 'ASC' : 'DESC');
  177. }
  178. $sort[] = $db->quote_identifier($column).$direction;
  179. }
  180. return 'ORDER BY '.implode(', ', $sort);
  181. }
  182. /**
  183. * Reset the current builder status.
  184. *
  185. * @return $this
  186. */
  187. abstract public function reset();
  188. }