Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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