PageRenderTime 70ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/propel/util/Join.php

https://bitbucket.org/ealexandru/jobeet
PHP | 251 lines | 188 code | 7 blank | 56 comment | 5 complexity | e679271d6e960f2bff02960c7459c7f7 MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /*
  3. * $Id: Join.php 1126 2009-09-15 12:55:35Z francois $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://propel.phpdb.org>.
  20. */
  21. /**
  22. * Data object to describe a join between two tables, for example
  23. * <pre>
  24. * table_a LEFT JOIN table_b ON table_a.id = table_b.a_id
  25. * </pre>
  26. *
  27. * @author Francois Zaninotto (Propel)
  28. * @author Hans Lellelid <hans@xmpl.org> (Propel)
  29. * @author Kaspars Jaudzems <kaspars.jaudzems@inbox.lv> (Propel)
  30. * @author Frank Y. Kim <frank.kim@clearink.com> (Torque)
  31. * @author John D. McNally <jmcnally@collab.net> (Torque)
  32. * @author Brett McLaughlin <bmclaugh@algx.net> (Torque)
  33. * @author Eric Dobbs <eric@dobbse.net> (Torque)
  34. * @author Henning P. Schmiedehausen <hps@intermeta.de> (Torque)
  35. * @author Sam Joseph <sam@neurogrid.com> (Torque)
  36. * @package propel.util
  37. */
  38. class Join
  39. {
  40. // default comparison type
  41. const EQUAL = "=";
  42. // the left parts of the join condition
  43. protected $left = array();
  44. // the right parts of the join condition
  45. protected $right = array();
  46. // the comparison operators for each pair of columns in the join condition
  47. protected $operator = array();
  48. // the type of the join (LEFT JOIN, ...), or null for an implicit join
  49. protected $joinType = null;
  50. // the number of conditions in the join
  51. protected $count = 0;
  52. /**
  53. * Constructor
  54. * Use it preferably with no arguments, and then use addCondition() and setJoinType()
  55. * Syntax with arguments used mainly for backwards compatibility
  56. *
  57. * @param string $leftColumn The left column of the join condition
  58. * (may contain an alias name)
  59. * @param string $rightColumn The right column of the join condition
  60. * (may contain an alias name)
  61. * @param string $joinType The type of the join. Valid join types are null (implicit join),
  62. * Criteria::LEFT_JOIN, Criteria::RIGHT_JOIN, and Criteria::INNER_JOIN
  63. */
  64. public function __construct($leftColumn = null, $rightColumn = null, $joinType = null)
  65. {
  66. if(!is_null($leftColumn)) {
  67. if (!is_array($leftColumn)) {
  68. // simple join
  69. $this->addCondition($leftColumn, $rightColumn);
  70. } else {
  71. // join with multiple conditions
  72. if (count($leftColumn) != count($rightColumn) ) {
  73. throw new PropelException("Unable to create join because the left column count isn't equal to the right column count");
  74. }
  75. foreach ($leftColumn as $key => $value)
  76. {
  77. $this->addCondition($value, $rightColumn[$key]);
  78. }
  79. }
  80. $this->setJoinType($joinType);
  81. }
  82. }
  83. /**
  84. * Join condition definition
  85. *
  86. * @param string $left The left column of the join condition
  87. * (may contain an alias name)
  88. * @param string $right The right column of the join condition
  89. * (may contain an alias name)
  90. * @param string $joinType The type of the join. Valid join types are null (implicit join),
  91. * Criteria::LEFT_JOIN, Criteria::RIGHT_JOIN, and Criteria::INNER_JOIN
  92. */
  93. public function addCondition($left, $right, $operator = self::EQUAL)
  94. {
  95. $this->left[] = $left;
  96. $this->right[] = $right;
  97. $this->operator[] = $operator;
  98. $this->count++;
  99. }
  100. /**
  101. * Retrieve the number of conditions in the join
  102. *
  103. * @return integer The number of conditions in the join
  104. */
  105. public function countConditions()
  106. {
  107. return $this->count;
  108. }
  109. /**
  110. * Return an array of the join conditions
  111. *
  112. * @return array An array of arrays representing (left, comparison, right) for each condition
  113. */
  114. public function getConditions()
  115. {
  116. $conditions = array();
  117. for ($i=0; $i < $this->count; $i++) {
  118. $conditions[] = array(
  119. 'left' => $this->getLeftColumn($i),
  120. 'operator' => $this->getOperator($i),
  121. 'right' => $this->getRightColumn($i)
  122. );
  123. }
  124. return $conditions;
  125. }
  126. /**
  127. * @return the comparison operator for the join condition
  128. */
  129. public function getOperator($index = 0)
  130. {
  131. return $this->operator[$index];
  132. }
  133. public function getOperators()
  134. {
  135. return $this->operator;
  136. }
  137. /**
  138. * Set the join type
  139. *
  140. * @param string $joinType The type of the join. Valid join types are
  141. * null (adding the join condition to the where clause),
  142. * Criteria::LEFT_JOIN(), Criteria::RIGHT_JOIN(), and Criteria::INNER_JOIN()
  143. */
  144. public function setJoinType($joinType = null)
  145. {
  146. $this->joinType = $joinType;
  147. }
  148. /**
  149. * Get the join type
  150. *
  151. * @return string The type of the join, i.e. Criteria::LEFT_JOIN(), ...,
  152. * or null for adding the join condition to the where Clause
  153. */
  154. public function getJoinType()
  155. {
  156. return $this->joinType;
  157. }
  158. /**
  159. * @return the left column of the join condition
  160. */
  161. public function getLeftColumn($index = 0)
  162. {
  163. return $this->left[$index];
  164. }
  165. /**
  166. * @return all right columns of the join condition
  167. */
  168. public function getLeftColumns()
  169. {
  170. return $this->left;
  171. }
  172. public function getLeftColumnName($index = 0)
  173. {
  174. return substr($this->left[$index], strrpos($this->left[$index], '.') + 1);
  175. }
  176. public function getLeftTableName($index = 0)
  177. {
  178. return substr($this->left[$index], 0, strrpos($this->left[$index], '.'));
  179. }
  180. /**
  181. * @return the right column of the join condition
  182. */
  183. public function getRightColumn($index = 0)
  184. {
  185. return $this->right[$index];
  186. }
  187. /**
  188. * @return all right columns of the join condition
  189. */
  190. public function getRightColumns()
  191. {
  192. return $this->right;
  193. }
  194. public function getRightColumnName($index = 0)
  195. {
  196. return substr($this->right[$index], strrpos($this->right[$index], '.') + 1);
  197. }
  198. public function getRightTableName($index = 0)
  199. {
  200. return substr($this->right[$index], 0, strrpos($this->right[$index], '.'));
  201. }
  202. /**
  203. * returns a String representation of the class,
  204. * mainly for debugging purposes
  205. *
  206. * @return string A String representation of the class
  207. */
  208. public function toString()
  209. {
  210. $result = '';
  211. if ($this->joinType !== null)
  212. {
  213. $result .= $this->joinType . ' : ';
  214. }
  215. foreach ($$this->getConditions() as $index => $condition)
  216. {
  217. $result .= implode($condition);
  218. if ($index + 1 < $this->count) {
  219. $result .= ' AND ';
  220. }
  221. }
  222. $result .= '(ignoreCase not considered)';
  223. return $result;
  224. }
  225. }