Source for file RawSql.php
Documentation is available at RawSql.php
* $Id: RawSql.php 1847 2007-06-26 10:05:26Z zYne $
* 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.com>.
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @version $Revision: 1847 $
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* parses given query part
* @param string $queryPartName the name of the query part
* @param string $queryPart query part to be parsed
* @param boolean $append whether or not to append the query part to its stack
* if false is given, this method will overwrite
* the given query part stack with $queryPart
* @return Doctrine_Query this object
public function parseQueryPart($queryPartName, $queryPart, $append =
false)
if ($queryPartName ==
'select') {
$this->parts['select'] =
array();
$this->parts[$queryPartName] =
array($queryPart);
$this->parts[$queryPartName][] =
$queryPart;
* parses an sql query and adds the parts to internal array
* @param string $query query to be parsed
* @return Doctrine_RawSql this object
foreach ($e as $k =>
$part) {
if ( ! isset
($parts[$low])) {
if (isset
($e[$i]) &&
strtolower($e[$i]) ===
'by') {
$parts[$low .
'by'] =
array();
if ( ! isset
($parts[$p][0])) {
$parts[$p][0] .=
' '.
$part;
$this->parts['select'] =
array();
* builds the sql query from the given query parts
* @return string the built sql query
foreach ($this->fields as $field) {
// try to auto-add component
foreach ($this->_aliasMap[$componentAlias]['table']->getColumnNames() as $name) {
$field =
$e[0] .
'.' .
$name;
$select[$componentAlias][$field] =
$field .
' AS ' .
$e[0] .
'__' .
$name;
$field =
$e[0] .
'.' .
$e[1];
$select[$componentAlias][$field] =
$field .
' AS ' .
$e[0] .
'__' .
$e[1];
// force-add all primary key fields
foreach ($map['table']->getPrimaryKeys() as $key) {
$field =
$tableAlias .
'.' .
$key;
if ( ! isset
($this->parts['select'][$field])) {
$select[$componentAlias][$field] =
$field .
' AS ' .
$tableAlias .
'__' .
$key;
// first add the fields of the root component
$q =
'SELECT ' .
implode(', ', $select[$componentAlias]);
unset
($select[$componentAlias]);
foreach ($select as $component =>
$fields) {
$q .=
', ' .
implode(', ', $fields);
$this->parts['where'][] =
$string;
$q .=
( ! empty($this->parts['from']))?
' FROM ' .
implode(' ', $this->parts['from']) :
'';
$q .=
( ! empty($this->parts['where']))?
' WHERE ' .
implode(' AND ', $this->parts['where']) :
'';
$q .=
( ! empty($this->parts['groupby']))?
' GROUP BY ' .
implode(', ', $this->parts['groupby']) :
'';
$q .=
( ! empty($this->parts['having']))?
' HAVING ' .
implode(' AND ', $this->parts['having']) :
'';
$q .=
( ! empty($this->parts['orderby']))?
' ORDER BY ' .
implode(', ', $this->parts['orderby']) :
'';
$q .=
( ! empty($this->parts['limit']))?
' LIMIT ' .
implode(' ', $this->parts['limit']) :
'';
$q .=
( ! empty($this->parts['offset']))?
' OFFSET ' .
implode(' ', $this->parts['offset']) :
'';
* returns the fields associated with this parser
* @return array all the fields associated with this parser
* @param string $tableAlias
* @param string $componentName
* @return Doctrine_RawSql
$originalAlias =
(count($tmp) >
1) ?
end($tmp) :
null;
$fullLength =
strlen($fullPath);
foreach ($e as $k =>
$component) {
// get length of the previous path
// build the current component path
$currPath =
($currPath) ?
$currPath .
'.' .
$component :
$component;
$delimeter =
substr($fullPath, $length, 1);
// if an alias is not given use the current path as an alias identifier
if (strlen($currPath) ===
$fullLength && isset
($originalAlias)) {
$componentAlias =
$originalAlias;
$componentAlias =
$currPath;
->getConnectionForComponent($component);
$table =
$conn->getTable($component);
$this->_aliasMap[$componentAlias] =
array('table' =>
$table);
$relation =
$table->getRelation($component);
$this->_aliasMap[$componentAlias] =
array('table' =>
$relation->getTable(),
'relation' =>
$relation);