Coverage for Doctrine_Query_JoinCondition

Back to coverage report

1 <?php
2 /*
3  *  $Id: JoinCondition.php 3884 2008-02-22 18:26:35Z jwage $
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: 3884 $
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             $conn      = $this->query->getConnection();
49             $alias     = $this->query->getTableAlias($reference);
50             $map       = $this->query->getAliasDeclaration($reference);
51             $table     = $map['table'];
52             // check if value is enumerated value
53             $enumIndex = $table->enumIndex($field, trim($value, "'"));
54
55             if (false !== $enumIndex && $conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) {
56                 $enumIndex = $conn->quote($enumIndex, 'text');
57             }
58
59             if (substr($value, 0, 1) == '(') {
60                 // trim brackets
61                 $trimmed   = $this->_tokenizer->bracketTrim($value);
62
63                 if (substr($trimmed, 0, 4) == 'FROM' || substr($trimmed, 0, 6) == 'SELECT') {
64                     // subquery found
65                     $q = $this->query->createSubquery();
66                     $value = '(' . $q->parseQuery($trimmed)->getQuery() . ')';
67                 } elseif (substr($trimmed, 0, 4) == 'SQL:') {
68                     $value = '(' . substr($trimmed, 4) . ')';
69                 } else {
70                     // simple in expression found
71                     $e     = $this->_tokenizer->sqlExplode($trimmed, ',');
72
73                     $value = array();
74                     foreach ($e as $part) {
75                         $index = $table->enumIndex($field, trim($part, "'"));
76
77                         if (false !== $index && $conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) {
78                             $index = $conn->quote($index, 'text');
79                         }
80
81                         if ($index !== false) {
82                             $value[] = $index;
83                         } else {
84                             $value[] = $this->parseLiteralValue($part);
85                         }
86                     }
87                     $value = '(' . implode(', ', $value) . ')';
88                 }
89             } else {
90                 if ($enumIndex !== false) {
91                     $value = $enumIndex;
92                 } else {
93                     $value = $this->parseLiteralValue($value);
94                 }
95             }
96
97             switch ($operator) {
98                 case '<':
99                 case '>':
100                 case '=':
101                 case '!=':
102                     if ($enumIndex !== false) {
103                         $value  = $enumIndex;
104                     }
105                 default:
106                     $condition  = $alias . '.' . $field . ' '
107                                 . $operator . ' ' . $value;
108             }
109
110         }
111         return $condition;
112     }
113 }