Join.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. /**
  10. * Data object to describe a join between two tables, for example
  11. * <pre>
  12. * table_a LEFT JOIN table_b ON table_a.id = table_b.a_id
  13. * </pre>
  14. *
  15. * @author Francois Zaninotto (Propel)
  16. * @author Hans Lellelid <hans@xmpl.org> (Propel)
  17. * @author Kaspars Jaudzems <kaspars.jaudzems@inbox.lv> (Propel)
  18. * @author Frank Y. Kim <frank.kim@clearink.com> (Torque)
  19. * @author John D. McNally <jmcnally@collab.net> (Torque)
  20. * @author Brett McLaughlin <bmclaugh@algx.net> (Torque)
  21. * @author Eric Dobbs <eric@dobbse.net> (Torque)
  22. * @author Henning P. Schmiedehausen <hps@intermeta.de> (Torque)
  23. * @author Sam Joseph <sam@neurogrid.com> (Torque)
  24. * @package propel.runtime.query
  25. */
  26. class Join
  27. {
  28. // default comparison type
  29. const EQUAL = "=";
  30. // the left parts of the join condition
  31. protected $left = array();
  32. // the right parts of the join condition
  33. protected $right = array();
  34. // the comparison operators for each pair of columns in the join condition
  35. protected $operator = array();
  36. // the type of the join (LEFT JOIN, ...), or null for an implicit join
  37. protected $joinType = null;
  38. // the number of conditions in the join
  39. protected $count = 0;
  40. /**
  41. * Constructor
  42. * Use it preferably with no arguments, and then use addCondition() and setJoinType()
  43. * Syntax with arguments used mainly for backwards compatibility
  44. *
  45. * @param string $leftColumn The left column of the join condition
  46. * (may contain an alias name)
  47. * @param string $rightColumn The right column of the join condition
  48. * (may contain an alias name)
  49. * @param string $joinType The type of the join. Valid join types are null (implicit join),
  50. * Criteria::LEFT_JOIN, Criteria::RIGHT_JOIN, and Criteria::INNER_JOIN
  51. */
  52. public function __construct($leftColumn = null, $rightColumn = null, $joinType = null)
  53. {
  54. if(!is_null($leftColumn)) {
  55. if (!is_array($leftColumn)) {
  56. // simple join
  57. $this->addCondition($leftColumn, $rightColumn);
  58. } else {
  59. // join with multiple conditions
  60. if (count($leftColumn) != count($rightColumn) ) {
  61. throw new PropelException("Unable to create join because the left column count isn't equal to the right column count");
  62. }
  63. foreach ($leftColumn as $key => $value)
  64. {
  65. $this->addCondition($value, $rightColumn[$key]);
  66. }
  67. }
  68. $this->setJoinType($joinType);
  69. }
  70. }
  71. /**
  72. * Join condition definition
  73. *
  74. * @param string $left The left column of the join condition
  75. * (may contain an alias name)
  76. * @param string $right The right column of the join condition
  77. * (may contain an alias name)
  78. * @param string $operator The comparison operator of the join condition, default Join::EQUAL
  79. */
  80. public function addCondition($left, $right, $operator = self::EQUAL)
  81. {
  82. $this->left[] = $left;
  83. $this->right[] = $right;
  84. $this->operator[] = $operator;
  85. $this->count++;
  86. }
  87. /**
  88. * Retrieve the number of conditions in the join
  89. *
  90. * @return integer The number of conditions in the join
  91. */
  92. public function countConditions()
  93. {
  94. return $this->count;
  95. }
  96. /**
  97. * Return an array of the join conditions
  98. *
  99. * @return array An array of arrays representing (left, comparison, right) for each condition
  100. */
  101. public function getConditions()
  102. {
  103. $conditions = array();
  104. for ($i=0; $i < $this->count; $i++) {
  105. $conditions[] = array(
  106. 'left' => $this->getLeftColumn($i),
  107. 'operator' => $this->getOperator($i),
  108. 'right' => $this->getRightColumn($i)
  109. );
  110. }
  111. return $conditions;
  112. }
  113. /**
  114. * @return the comparison operator for the join condition
  115. */
  116. public function getOperator($index = 0)
  117. {
  118. return $this->operator[$index];
  119. }
  120. public function getOperators()
  121. {
  122. return $this->operator;
  123. }
  124. /**
  125. * Set the join type
  126. *
  127. * @param string $joinType The type of the join. Valid join types are
  128. * null (adding the join condition to the where clause),
  129. * Criteria::LEFT_JOIN(), Criteria::RIGHT_JOIN(), and Criteria::INNER_JOIN()
  130. */
  131. public function setJoinType($joinType = null)
  132. {
  133. $this->joinType = $joinType;
  134. }
  135. /**
  136. * Get the join type
  137. *
  138. * @return string The type of the join, i.e. Criteria::LEFT_JOIN(), ...,
  139. * or null for adding the join condition to the where Clause
  140. */
  141. public function getJoinType()
  142. {
  143. return $this->joinType;
  144. }
  145. /**
  146. * @return the left column of the join condition
  147. */
  148. public function getLeftColumn($index = 0)
  149. {
  150. return $this->left[$index];
  151. }
  152. /**
  153. * @return all right columns of the join condition
  154. */
  155. public function getLeftColumns()
  156. {
  157. return $this->left;
  158. }
  159. public function getLeftColumnName($index = 0)
  160. {
  161. return substr($this->left[$index], strrpos($this->left[$index], '.') + 1);
  162. }
  163. public function getLeftTableName($index = 0)
  164. {
  165. return substr($this->left[$index], 0, strrpos($this->left[$index], '.'));
  166. }
  167. /**
  168. * @return the right column of the join condition
  169. */
  170. public function getRightColumn($index = 0)
  171. {
  172. return $this->right[$index];
  173. }
  174. /**
  175. * @return all right columns of the join condition
  176. */
  177. public function getRightColumns()
  178. {
  179. return $this->right;
  180. }
  181. public function getRightColumnName($index = 0)
  182. {
  183. return substr($this->right[$index], strrpos($this->right[$index], '.') + 1);
  184. }
  185. public function getRightTableName($index = 0)
  186. {
  187. return substr($this->right[$index], 0, strrpos($this->right[$index], '.'));
  188. }
  189. public function equals($join)
  190. {
  191. return $join !== null
  192. && $join instanceof Join
  193. && $this->joinType == $join->getJoinType()
  194. && $this->getConditions() == $join->getConditions();
  195. }
  196. /**
  197. * returns a String representation of the class,
  198. * mainly for debugging purposes
  199. *
  200. * @return string A String representation of the class
  201. */
  202. public function toString()
  203. {
  204. $result = '';
  205. if ($this->joinType !== null) {
  206. $result .= $this->joinType . ' : ';
  207. }
  208. foreach ($this->getConditions() as $index => $condition) {
  209. $result .= implode($condition);
  210. if ($index + 1 < $this->count) {
  211. $result .= ' AND ';
  212. }
  213. }
  214. $result .= '(ignoreCase not considered)';
  215. return $result;
  216. }
  217. public function __toString()
  218. {
  219. return $this->toString();
  220. }
  221. }