| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: Having.php 7666 2010-06-08 19:23:20Z 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.doctrine-project.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Doctrine_Query_Having
|
|
|
24 |
*
|
|
|
25 |
* @package Doctrine
|
|
|
26 |
* @subpackage Query
|
|
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
28 |
* @link www.doctrine-project.org
|
|
|
29 |
* @since 1.0
|
|
|
30 |
* @version $Revision: 7666 $
|
|
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
32 |
*/
|
|
|
33 |
class Doctrine_Query_Having extends Doctrine_Query_Condition
|
|
|
34 |
{
|
|
|
35 |
/**
|
|
|
36 |
* DQL Aggregate Function parser
|
|
|
37 |
*
|
|
|
38 |
* @param string $func
|
|
|
39 |
* @return mixed
|
|
|
40 |
*/
|
|
|
41 |
private function parseAggregateFunction($func)
|
|
|
42 |
{
|
|
|
43 |
$pos = strpos($func, '(');
|
|
|
44 |
|
|
|
45 |
// Check for subqueries
|
|
|
46 |
if ($pos === 0 && substr($func, 1, 6) == 'SELECT') {
|
|
|
47 |
// This code is taken from WHERE.php
|
|
|
48 |
$sub = $this->_tokenizer->bracketTrim($func);
|
|
|
49 |
$q = $this->query->createSubquery()->parseDqlQuery($sub, false);
|
|
|
50 |
$sql = $q->getSqlQuery();
|
|
|
51 |
$q->free();
|
|
|
52 |
return '(' . $sql . ')';
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
if ($pos !== false) {
|
|
|
56 |
$funcs = array();
|
|
|
57 |
|
|
|
58 |
$name = substr($func, 0, $pos);
|
|
|
59 |
$func = substr($func, ($pos + 1), -1);
|
|
|
60 |
$params = $this->_tokenizer->bracketExplode($func, ',', '(', ')');
|
|
|
61 |
|
|
|
62 |
foreach ($params as $k => $param) {
|
|
|
63 |
$params[$k] = $this->parseAggregateFunction($param);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
$funcs = $name . '(' . implode(', ', $params) . ')';
|
|
|
67 |
|
|
|
68 |
return $funcs;
|
|
|
69 |
} else {
|
|
|
70 |
return $this->_parseAliases($func);
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* _parseAliases
|
|
|
76 |
* Processes part of the query not being an aggregate function
|
|
|
77 |
*
|
|
|
78 |
* @param mixed $value
|
|
|
79 |
* @return string
|
|
|
80 |
*/
|
|
|
81 |
final private function _parseAliases($value)
|
|
|
82 |
{
|
|
|
83 |
if ( ! is_numeric($value)) {
|
|
|
84 |
$a = explode('.', $value);
|
|
|
85 |
|
|
|
86 |
if (count($a) > 1) {
|
|
|
87 |
$field = array_pop($a);
|
|
|
88 |
$ref = implode('.', $a);
|
|
|
89 |
$map = $this->query->load($ref, false);
|
|
|
90 |
$field = $map['table']->getColumnName($field);
|
|
|
91 |
$value = $this->query->getConnection()->quoteIdentifier($this->query->getSqlTableAlias($ref) . '.' . $field);
|
|
|
92 |
} else {
|
|
|
93 |
$field = end($a);
|
|
|
94 |
if ($this->query->hasSqlAggregateAlias($field)) {
|
|
|
95 |
$value = $this->query->getSqlAggregateAlias($field);
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
return $value;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* load
|
|
|
105 |
* returns the parsed query part
|
|
|
106 |
*
|
|
|
107 |
* @param string $having
|
|
|
108 |
* @return string
|
|
|
109 |
*/
|
|
|
110 |
final public function load($having)
|
|
|
111 |
{
|
|
|
112 |
$tokens = $this->_tokenizer->bracketExplode($having, ' ', '(', ')');
|
|
|
113 |
$part = $this->parseAggregateFunction(array_shift($tokens));
|
|
|
114 |
$operator = array_shift($tokens);
|
|
|
115 |
$value = implode(' ', $tokens);
|
|
|
116 |
|
|
|
117 |
// check the RHS for aggregate functions
|
|
|
118 |
$value = $this->parseAggregateFunction($value);
|
|
|
119 |
|
|
|
120 |
$part .= ' ' . $operator . ' ' . $value;
|
|
|
121 |
|
|
|
122 |
return $part;
|
|
|
123 |
}
|
|
|
124 |
}
|