Join.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Database query builder for JOIN statements. See [Query Builder](/database/query/builder) for usage and examples.
  4. *
  5. * @package Kohana/Database
  6. * @category Query
  7. * @author Kohana Team
  8. * @copyright (c) Kohana Team
  9. * @license https://koseven.ga/LICENSE.md
  10. */
  11. class Kohana_Database_Query_Builder_Join extends Database_Query_Builder {
  12. // Type of JOIN
  13. protected $_type;
  14. // JOIN ...
  15. protected $_table;
  16. // ON ...
  17. protected $_on = [];
  18. // USING ...
  19. protected $_using = [];
  20. /**
  21. * Creates a new JOIN statement for a table. Optionally, the type of JOIN
  22. * can be specified as the second parameter.
  23. *
  24. * @param mixed $table column name or array($column, $alias) or object
  25. * @param string $type type of JOIN: INNER, RIGHT, LEFT, etc
  26. * @return void
  27. */
  28. public function __construct($table, $type = NULL)
  29. {
  30. // Set the table to JOIN on
  31. $this->_table = $table;
  32. if ($type !== NULL)
  33. {
  34. // Set the JOIN type
  35. $this->_type = (string) $type;
  36. }
  37. }
  38. /**
  39. * Adds a new condition for joining.
  40. *
  41. * @param mixed $c1 column name or array($column, $alias) or object
  42. * @param string $op logic operator
  43. * @param mixed $c2 column name or array($column, $alias) or object
  44. * @return $this
  45. */
  46. public function on($c1, $op, $c2)
  47. {
  48. if ( ! empty($this->_using))
  49. {
  50. throw new Kohana_Exception('JOIN ... ON ... cannot be combined with JOIN ... USING ...');
  51. }
  52. $this->_on[] = [$c1, $op, $c2];
  53. return $this;
  54. }
  55. /**
  56. * Adds a new condition for joining.
  57. *
  58. * @param string $columns column name
  59. * @return $this
  60. */
  61. public function using($columns)
  62. {
  63. if ( ! empty($this->_on))
  64. {
  65. throw new Kohana_Exception('JOIN ... ON ... cannot be combined with JOIN ... USING ...');
  66. }
  67. $columns = func_get_args();
  68. $this->_using = array_merge($this->_using, $columns);
  69. return $this;
  70. }
  71. /**
  72. * Compile the SQL partial for a JOIN statement and return it.
  73. *
  74. * @param mixed $db Database instance or name of instance
  75. * @return string
  76. */
  77. public function compile($db = NULL)
  78. {
  79. if ( ! is_object($db))
  80. {
  81. // Get the database instance
  82. $db = Database::instance($db);
  83. }
  84. if ($this->_type)
  85. {
  86. $sql = strtoupper($this->_type).' JOIN';
  87. }
  88. else
  89. {
  90. $sql = 'JOIN';
  91. }
  92. // Quote the table name that is being joined
  93. $sql .= ' '.$db->quote_table($this->_table);
  94. if ( ! empty($this->_using))
  95. {
  96. // Quote and concat the columns
  97. $sql .= ' USING ('.implode(', ', array_map([$db, 'quote_column'], $this->_using)).')';
  98. }
  99. else
  100. {
  101. $conditions = [];
  102. foreach ($this->_on as $condition)
  103. {
  104. // Split the condition
  105. list($c1, $op, $c2) = $condition;
  106. if ($op)
  107. {
  108. // Make the operator uppercase and spaced
  109. $op = ' '.strtoupper($op);
  110. }
  111. // Quote each of the columns used for the condition
  112. $conditions[] = $db->quote_column($c1).$op.' '.$db->quote_column($c2);
  113. }
  114. // Concat the conditions "... AND ..."
  115. $sql .= ' ON ('.implode(' AND ', $conditions).')';
  116. }
  117. return $sql;
  118. }
  119. public function reset()
  120. {
  121. $this->_type =
  122. $this->_table = NULL;
  123. $this->_on = [];
  124. }
  125. } // End Database_Query_Builder_Join