# Coverage for Doctrine_Query_JoinCondition

Back to coverage report

1 | <?php
2 | /*
3 | * $Id: JoinCondition.php 3212 2007-11-24 18:58:33Z romanb $
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, see
19 | * <http://www.phpdoctrine.org>.
20 | */
21 | Doctrine::autoload('Doctrine_Query_Part');
22 | /**
23 | * Doctrine_Query_JoinCondition
24 | *
25 | * @package Doctrine
26 | * @subpackage Query
27 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
28 | * @link www.phpdoctrine.org
29 | * @since 1.0
30 | * @version $Revision: 3212 $
31 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
32 | */
33 | class Doctrine_Query_JoinCondition extends Doctrine_Query_Condition
34 | {
35 | public function load($condition)
36 | {
37 | $condition = trim($condition);
38 |
39 | $e = $this->_tokenizer->sqlExplode($condition);
40 |
41 | if (count($e) > 2) {
42 | $a = explode('.', $e[0]);
43 | $field = array_pop($a);
44 | $reference = implode('.', $a);
45 | $operator = $e[1];
46 | $value = $e[2];
47 |
48 | $alias = $this->query->getTableAlias($reference);
49 | $map = $this->query->getAliasDeclaration($reference);
50 | $table = $map['table'];
51 | // check if value is enumerated value
52 | $enumIndex = $table->enumIndex($field, trim($value, "'"));
53 |
54 |
55 | if (substr($value, 0, 1) == '(') {
56 | // trim brackets
57 | $trimmed = $this->_tokenizer->bracketTrim($value);
58 |
59 | if (substr($trimmed, 0, 4) == 'FROM' || substr($trimmed, 0, 6) == 'SELECT') {
60 | // subquery found
61 | $q = $this->query->createSubquery();
62 | $value = '(' . $q->parseQuery($trimmed)->getQuery() . ')';
63 | } elseif (substr($trimmed, 0, 4) == 'SQL:') {
64 | $value = '(' . substr($trimmed, 4) . ')';
65 | } else {
66 | // simple in expression found
67 | $e = $this->_tokenizer->sqlExplode($trimmed, ',');
68 |
69 | $value = array();
70 | foreach ($e as $part) {
71 | $index = $table->enumIndex($field, trim($part, "'"));
72 | if ($index !== false) {
73 | $value[] = $index;
74 | } else {
75 | $value[] = $this->parseLiteralValue($part);
76 | }
77 | }
78 | $value = '(' . implode(', ', $value) . ')';
79 | }
80 | } else {
81 | if ($enumIndex !== false) {
82 | $value = $enumIndex;
83 | } else {
84 | $value = $this->parseLiteralValue($value);
85 | }
86 | }
87 |
88 | switch ($operator) {
89 | case '<':
90 | case '>':
91 | case '=':
92 | case '!=':
93 | if ($enumIndex !== false) {
94 | $value = $enumIndex;
95 | }
96 | default:
97 | $condition = $alias . '.' . $field . ' '
98 | . $operator . ' ' . $value;
99 | }
100 |
101 | }
102 | return $condition;
103 | }
104 | }