Join.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * A class used to join two SQL tables together.
  4. *
  5. * @author Ironpilot
  6. * @copyright Copywrite (c) 2011, STAPLE CODE
  7. *
  8. * This file is part of the STAPLE Framework.
  9. *
  10. * The STAPLE Framework is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by the
  12. * Free Software Foundation, either version 3 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * The STAPLE Framework is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. class Staple_Query_Join
  24. {
  25. const JOIN = "JOIN";
  26. const LEFT = "LEFT OUTER JOIN";
  27. const RIGHT = "RIGHT OUTER JOIN";
  28. const INNER = "INNER JOIN";
  29. const CROSS = "CROSS JOIN";
  30. const STRAIGHT = "STRAIGHT_JOIN";
  31. const NATURAL = "NATURAL JOIN";
  32. const NATURAL_LEFT = "NATURAL LEFT JOIN";
  33. const NATURAL_RIGHT = "NATURAL RIGHT JOIN";
  34. /**
  35. * Left Table
  36. * @var string
  37. */
  38. protected $lefttable;
  39. /**
  40. * Left Join Column(s)
  41. * @var string | array
  42. */
  43. protected $leftcolumn;
  44. /**
  45. * Right Table - The table that is being added to the left table.
  46. * @var string
  47. */
  48. protected $table;
  49. /**
  50. * Right Column(s)
  51. * @var string | array
  52. */
  53. protected $rightcolumn;
  54. /**
  55. * Join Condition
  56. * @var string
  57. */
  58. protected $condition;
  59. /**
  60. * Join Type
  61. * @var string
  62. */
  63. protected $type;
  64. /**
  65. * Default Constructor
  66. * @param string $type
  67. * @param string $table
  68. * @param string $condition
  69. * @param string $lefttable
  70. * @param string $leftcolumn
  71. * @param string $rightcolumn
  72. */
  73. public function __construct($type = self::INNER, $table = NULL, $condition = NULL, $lefttable = NULL, $leftcolumn = NULL, $rightcolumn = NULL)
  74. {
  75. $this->setType($type);
  76. if(isset($table))
  77. {
  78. $this->setTable($table);
  79. }
  80. if(isset($condition))
  81. {
  82. $this->setCondition($condition);
  83. }
  84. }
  85. public function __toString()
  86. {
  87. return $this->build();
  88. }
  89. /**
  90. * @return the $lefttable
  91. */
  92. public function getLefttable()
  93. {
  94. return $this->lefttable;
  95. }
  96. /**
  97. * @return the $leftcolumn
  98. */
  99. public function getLeftcolumn()
  100. {
  101. return $this->leftcolumn;
  102. }
  103. /**
  104. * @return the $table
  105. */
  106. public function getTable()
  107. {
  108. return $this->table;
  109. }
  110. /**
  111. * @return the $rightcolumn
  112. */
  113. public function getRightcolumn()
  114. {
  115. return $this->rightcolumn;
  116. }
  117. /**
  118. * @return the $condition
  119. */
  120. public function getCondition()
  121. {
  122. return $this->condition;
  123. }
  124. /**
  125. * @return the $type
  126. */
  127. public function getType()
  128. {
  129. return $this->type;
  130. }
  131. /**
  132. * @param string $lefttable
  133. */
  134. public function setLefttable($lefttable)
  135. {
  136. $this->lefttable = $lefttable;
  137. return $this;
  138. }
  139. /**
  140. * @param string $leftcolumn
  141. */
  142. public function setLeftcolumn($leftcolumn)
  143. {
  144. $this->leftcolumn = $leftcolumn;
  145. return $this;
  146. }
  147. /**
  148. * @param string $table
  149. */
  150. public function setTable($table)
  151. {
  152. $this->table = $table;
  153. return $this;
  154. }
  155. /**
  156. * @param string $rightcolumn
  157. */
  158. public function setRightcolumn($rightcolumn)
  159. {
  160. $this->rightcolumn = $rightcolumn;
  161. return $this;
  162. }
  163. /**
  164. * @param string $condition
  165. */
  166. public function setCondition($condition)
  167. {
  168. $this->condition = $condition;
  169. return $this;
  170. }
  171. /**
  172. * @param string $type
  173. */
  174. public function setType($type)
  175. {
  176. $this->type = $type;
  177. return $this;
  178. }
  179. public function build()
  180. {
  181. $join = $this->getType().' '.$this->table;
  182. if(isset($this->condition))
  183. {
  184. $join .= ' ON '.$this->condition;
  185. }
  186. elseif(isset($this->lefttable) && isset($this->leftcolumn) && isset($this->rightcolumn))
  187. {
  188. $join .= ' ON '.$this->lefttable.'.'.$this->leftcolumn.'='.$this->table.'.'.$this->rightcolumn;
  189. }
  190. return $join;
  191. }
  192. public static function inner($table, $condition)
  193. {
  194. return new static(self::INNER,$table,$condition);
  195. }
  196. public static function left($table,$condition)
  197. {
  198. return new static(self::LEFT,$table,$condition);
  199. }
  200. }
  201. ?>