SelectStatement.php 4.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<?php
/*
 *  $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
 * <http://www.phpdoctrine.org>.
 */

/**
 * SelectStatement = SelectClause FromClause [WhereClause] [GroupByClause] [HavingClause] [OrderByClause]
 *
 * @package     Doctrine
 * @subpackage  Query
 * @author      Guilherme Blanco <guilhermeblanco@hotmail.com>
 * @author      Janne Vanhala <jpvanhal@cc.hut.fi>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link        http://www.phpdoctrine.org
31
 * @since       2.0
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
 * @version     $Revision$
 */
class Doctrine_Query_Production_SelectStatement extends Doctrine_Query_Production
{
    protected $_selectClause;

    protected $_fromClause;

    protected $_whereClause;

    protected $_groupByClause;

    protected $_havingClause;

    protected $_orderByClause;


    public function syntax($paramHolder)
    {
        // SelectStatement = SelectClause FromClause [WhereClause] [GroupByClause] [HavingClause] [OrderByClause]

        // Disable the semantical check for SelectClause now. This is needed
        // since we dont know the query components yet (will be known only
        // when the FROM clause be processed).
        $paramHolder->set('semanticalCheck', false);
        $this->_selectClause = $this->AST('SelectClause', $paramHolder);
        $paramHolder->remove('semanticalCheck');

        $this->_fromClause = $this->AST('FromClause', $paramHolder);

        if ($this->_isNextToken(Doctrine_Query_Token::T_WHERE)) {
            $this->_whereClause = $this->AST('WhereClause', $paramHolder);
        }

        if ($this->_isNextToken(Doctrine_Query_Token::T_GROUP)) {
            $this->_groupByClause = $this->AST('GroupByClause', $paramHolder);
        }

        if ($this->_isNextToken(Doctrine_Query_Token::T_HAVING)) {
            $this->_havingClause = $this->AST('HavingClause', $paramHolder);
        }

        if ($this->_isNextToken(Doctrine_Query_Token::T_ORDER)) {
            $this->_orderByClause = $this->AST('OrderByClause', $paramHolder);
        }
    }


    public function semantical($paramHolder)
    {
        // We need to invoke the semantical check of SelectClause here, since
        // it was not yet checked.
        $this->_selectClause->semantical($paramHolder);
    }


    public function buildSql()
    {
        return $this->_selectClause->buildSql() . ' ' . $this->_fromClause->buildSql()
             . (($this->_whereClause !== null) ? ' ' . $this->_whereClause->buildSql() : ' WHERE 1 = 1')
             . (($this->_groupByClause !== null) ? ' ' . $this->_groupByClause->buildSql() : '')
             . (($this->_havingClause !== null) ? ' ' . $this->_havingClause->buildSql() : '')
             . (($this->_orderByClause !== null) ? ' ' . $this->_orderByClause->buildSql() : '');
    }
96 97


98 99 100 101 102
    /* Getters */
    public function getSelectClause()
    {
        return $this->_selectClause;
    }
103 104


105 106 107 108
    public function getFromClause()
    {
        return $this->_fromClause;
    }
109 110


111 112 113 114
    public function getWhereClause()
    {
        return $this->_whereClause;
    }
115 116


117 118 119 120
    public function getGroupByClause()
    {
        return $this->_groupByClause;
    }
121 122


123 124 125 126
    public function getHavingClause()
    {
        return $this->_havingClause;
    }
127 128


129 130 131 132
    public function getOrderByClause()
    {
        return $this->_orderByClause;
    }
133
}