Having.php 3.46 KB
Newer Older
doctrine's avatar
doctrine committed
1
<?php
zYne's avatar
zYne committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.phpdoctrine.org>.
zYne's avatar
zYne committed
20 21 22 23 24 25
 */
Doctrine::autoload('Doctrine_Query_Condition');
/**
 * Doctrine_Query_Having
 *
 * @package     Doctrine
26
 * @subpackage  Query
zYne's avatar
zYne committed
27
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28
 * @link        www.phpdoctrine.org
zYne's avatar
zYne committed
29 30 31 32
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
lsmith's avatar
lsmith committed
33 34
class Doctrine_Query_Having extends Doctrine_Query_Condition
{
doctrine's avatar
doctrine committed
35 36 37 38 39 40
    /**
     * DQL Aggregate Function parser
     *
     * @param string $func
     * @return mixed
     */
lsmith's avatar
lsmith committed
41 42
    private function parseAggregateFunction($func)
    {
43
        $pos = strpos($func, '(');
doctrine's avatar
doctrine committed
44

lsmith's avatar
lsmith committed
45
        if ($pos !== false) {
doctrine's avatar
doctrine committed
46 47 48 49
            $funcs  = array();

            $name   = substr($func, 0, $pos);
            $func   = substr($func, ($pos + 1), -1);
50
            $params = $this->_tokenizer->bracketExplode($func, ',', '(', ')');
doctrine's avatar
doctrine committed
51

lsmith's avatar
lsmith committed
52
            foreach ($params as $k => $param) {
doctrine's avatar
doctrine committed
53 54 55
                $params[$k] = $this->parseAggregateFunction($param);
            }

56
            $funcs = $name . '(' . implode(', ', $params) . ')';
doctrine's avatar
doctrine committed
57 58 59 60

            return $funcs;

        } else {
lsmith's avatar
lsmith committed
61
            if ( ! is_numeric($func)) {
62
                $a = explode('.', $func);
zYne's avatar
zYne committed
63

zYne's avatar
zYne committed
64
                if (count($a) > 1) {
zYne's avatar
zYne committed
65
                    $field     = array_pop($a);
zYne's avatar
zYne committed
66
                    $reference = implode('.', $a);
zYne's avatar
zYne committed
67 68
                    $map       = $this->query->load($reference, false);
                    $field     = $map['table']->getColumnName($field);
zYne's avatar
zYne committed
69 70
                    $func      = $this->query->getTableAlias($reference) . '.' . $field;
                } else {
zYne's avatar
zYne committed
71
                    $field = end($a);
zYne's avatar
zYne committed
72
                    $func  = $this->query->getAggregateAlias($field);
zYne's avatar
zYne committed
73
                }
doctrine's avatar
doctrine committed
74 75 76 77 78 79
                return $func;
            } else {
                return $func;
            }
        }
    }
80

81

doctrine's avatar
doctrine committed
82 83 84 85 86 87 88
    /**
     * load
     * returns the parsed query part
     *
     * @param string $having
     * @return string
     */
lsmith's avatar
lsmith committed
89 90
    final public function load($having)
    {
91
        $tokens = $this->_tokenizer->bracketExplode($having, ' ', '(', ')');
92 93 94 95
        $part = $this->parseAggregateFunction(array_shift($tokens));
        $operator  = array_shift($tokens);
        $value     = implode(' ', $tokens);
        $part .= ' ' . $operator . ' ' . $value;
96 97 98 99
        // check the RHS for aggregate functions
        if (strpos($value, '(') !== false) {
          $value = $this->parseAggregateFunction($value);
        }
100
        return $part;
doctrine's avatar
doctrine committed
101
    }
102
}