1
0
mirror of synced 2024-12-14 23:26:04 +03:00

Unified abstract class for RawSql and Query components

This commit is contained in:
zYne 2007-05-19 18:06:42 +00:00
parent 8fc2028b5b
commit 5d4b8c45e6
3 changed files with 43 additions and 267 deletions

View File

@ -62,7 +62,7 @@ class Doctrine_Hydrate_Alias
return $alias; return $alias;
} }
public function hasAliasFor($tableName) public function hasAlias($tableName)
{ {
return (isset($this->shortAliases[$tableName])); return (isset($this->shortAliases[$tableName]));
} }
@ -99,6 +99,10 @@ class Doctrine_Hydrate_Alias
return $alias; return $alias;
} }
public function getAliases()
{
return $this->shortAliases;
}
public function addAlias($tableAlias, $componentAlias) public function addAlias($tableAlias, $componentAlias)
{ {
$this->shortAliases[$tableAlias] = $componentAlias; $this->shortAliases[$tableAlias] = $componentAlias;

View File

@ -18,7 +18,7 @@
* and is licensed under the LGPL. For more information, see * and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>. * <http://www.phpdoctrine.com>.
*/ */
Doctrine::autoload('Doctrine_Hydrate'); Doctrine::autoload('Doctrine_Query_Abstract');
/** /**
* Doctrine_Query * Doctrine_Query
* *
@ -30,7 +30,7 @@ Doctrine::autoload('Doctrine_Hydrate');
* @version $Revision$ * @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi> * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/ */
class Doctrine_Query extends Doctrine_Hydrate implements Countable class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
{ {
/** /**
* @param array $subqueryAliases the table aliases needed in some LIMIT subqueries * @param array $subqueryAliases the table aliases needed in some LIMIT subqueries
@ -180,6 +180,21 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable
return $this->_parsers[$name]; return $this->_parsers[$name];
} }
/**
* parseQueryPart
* 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)
{
return $this->getParser($queryPartName)->parse($queryPart);
}
/** /**
* processPendingFields * processPendingFields
* the fields in SELECT clause cannot be parsed until the components * the fields in SELECT clause cannot be parsed until the components
@ -1050,251 +1065,4 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable
{ {
return $this->aliasHandler->getShortAlias($componentAlias, $tableName); return $this->aliasHandler->getShortAlias($componentAlias, $tableName);
} }
/**
* addSelect
* adds fields to the SELECT part of the query
*
* @param string $select DQL SELECT part
* @return Doctrine_Query
*/
public function addSelect($select)
{
return $this->getParser('select')->parse($select, true);
}
/**
* addWhere
* adds conditions to the WHERE part of the query
*
* @param string $where DQL WHERE part
* @param mixed $params an array of parameters or a simple scalar
* @return Doctrine_Query
*/
public function addWhere($where, $params = array())
{
if (is_array($params)) {
$this->params = array_merge($this->params, $params);
} else {
$this->params[] = $params;
}
return $this->getParser('where')->parse($where, true);
}
/**
* addGroupBy
* adds fields to the GROUP BY part of the query
*
* @param string $groupby DQL GROUP BY part
* @return Doctrine_Query
*/
public function addGroupBy($groupby)
{
return $this->getParser('groupby')->parse($groupby, true);
}
/**
* addHaving
* adds conditions to the HAVING part of the query
*
* @param string $having DQL HAVING part
* @param mixed $params an array of parameters or a simple scalar
* @return Doctrine_Query
*/
public function addHaving($having, $params = array())
{
if (is_array($params)) {
$this->params = array_merge($this->params, $params);
} else {
$this->params[] = $params;
}
return $this->getParser('having')->parse($having, true);
}
/**
* addOrderBy
* adds fields to the ORDER BY part of the query
*
* @param string $orderby DQL ORDER BY part
* @return Doctrine_Query
*/
public function addOrderBy($orderby)
{
return $this->getParser('orderby')->parse($orderby, true);
}
/**
* select
* sets the SELECT part of the query
*
* @param string $select DQL SELECT part
* @return Doctrine_Query
*/
public function select($select)
{
return $this->getParser('select')->parse($select);
}
/**
* distinct
* Makes the query SELECT DISTINCT.
*
* @param bool $flag Whether or not the SELECT is DISTINCT (default true).
* @return Doctrine_Query
*/
public function distinct($flag = true)
{
$this->_parts['distinct'] = (bool) $flag;
return $this;
}
/**
* forUpdate
* Makes the query SELECT FOR UPDATE.
*
* @param bool $flag Whether or not the SELECT is FOR UPDATE (default true).
* @return Doctrine_Query
*/
public function forUpdate($flag = true)
{
$this->_parts[self::FOR_UPDATE] = (bool) $flag;
return $this;
}
/**
* delete
* sets the query type to DELETE
*
* @return Doctrine_Query
*/
public function delete()
{
$this->type = self::DELETE;
return $this;
}
/**
* update
* sets the UPDATE part of the query
*
* @param string $update DQL UPDATE part
* @return Doctrine_Query
*/
public function update($update)
{
$this->type = self::UPDATE;
return $this->getParser('from')->parse($update);
}
/**
* set
* sets the SET part of the query
*
* @param string $update DQL UPDATE part
* @return Doctrine_Query
*/
public function set($key, $value)
{
return $this->getParser('set')->parse($key . ' = ' . $value);
}
/**
* from
* sets the FROM part of the query
*
* @param string $from DQL FROM part
* @return Doctrine_Query
*/
public function from($from)
{
return $this->getParser('from')->parse($from);
}
/**
* innerJoin
* appends an INNER JOIN to the FROM part of the query
*
* @param string $join DQL INNER JOIN
* @return Doctrine_Query
*/
public function innerJoin($join)
{
return $this->getParser('from')->parse('INNER JOIN ' . $join);
}
/**
* leftJoin
* appends a LEFT JOIN to the FROM part of the query
*
* @param string $join DQL LEFT JOIN
* @return Doctrine_Query
*/
public function leftJoin($join)
{
return $this->getParser('from')->parse('LEFT JOIN ' . $join);
}
/**
* groupBy
* sets the GROUP BY part of the query
*
* @param string $groupby DQL GROUP BY part
* @return Doctrine_Query
*/
public function groupBy($groupby)
{
return $this->getParser('groupby')->parse($groupby);
}
/**
* where
* sets the WHERE part of the query
*
* @param string $join DQL WHERE part
* @param mixed $params an array of parameters or a simple scalar
* @return Doctrine_Query
*/
public function where($where, $params = array())
{
$this->params = (array) $params;
return $this->getParser('where')->parse($where);
}
/**
* having
* sets the HAVING part of the query
*
* @param string $having DQL HAVING part
* @param mixed $params an array of parameters or a simple scalar
* @return Doctrine_Query
*/
public function having($having, $params)
{
$this->params = (array) $params;
return $this->getParser('having')->parse($having);
}
/**
* orderBy
* sets the ORDER BY part of the query
*
* @param string $orderby DQL ORDER BY part
* @return Doctrine_Query
*/
public function orderBy($orderby)
{
return $this->getParser('orderby')->parse($orderby);
}
/**
* limit
* sets the DQL query limit
*
* @param integer $limit limit to be used for limiting the query results
* @return Doctrine_Query
*/
public function limit($limit)
{
return $this->getParser('limit')->parse($limit);
}
/**
* offset
* sets the DQL query offset
*
* @param integer $offset offset to be used for paginating the query
* @return Doctrine_Query
*/
public function offset($offset)
{
return $this->getParser('offset')->parse($offset);
}
} }

View File

@ -18,7 +18,7 @@
* and is licensed under the LGPL. For more information, see * and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>. * <http://www.phpdoctrine.com>.
*/ */
Doctrine::autoload('Doctrine_Hydrate'); Doctrine::autoload('Doctrine_Query_Abstract');
/** /**
* Doctrine_RawSql * Doctrine_RawSql
* *
@ -30,32 +30,36 @@ Doctrine::autoload('Doctrine_Hydrate');
* @version $Revision$ * @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi> * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/ */
class Doctrine_RawSql extends Doctrine_Hydrate class Doctrine_RawSql extends Doctrine_Query_Abstract
{ {
/** /**
* @var array $fields * @var array $fields
*/ */
private $fields = array(); private $fields = array();
/** /**
* __call * parseQueryPart
* method overloader * parses given query part
* *
* @param string $name * @param string $queryPartName the name of the query part
* @param array $args * @param string $queryPart query part to be parsed
* @return Doctrine_RawSql * @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 __call($name, $args) public function parseQueryPart($queryPartName, $queryPart, $append = false)
{ {
if ( ! isset($this->parts[$name])) { if ($queryPartName == 'select') {
throw new Doctrine_RawSql_Exception("Unknown overload method $name. Availible overload methods are ".implode(" ",array_keys($this->parts))); preg_match_all('/{([^}{]*)}/U', $queryPart, $m);
}
if ($name == 'select') {
preg_match_all('/{([^}{]*)}/U', $args[0], $m);
$this->fields = $m[1]; $this->fields = $m[1];
$this->parts["select"] = array(); $this->parts['select'] = array();
} else { } else {
$this->parts[$name][] = $args[0]; if ( ! $append) {
$this->parts[$queryPartName] = array($queryPart);
} else {
$this->parts[$queryPartName][] = $queryPart;
}
} }
return $this; return $this;
} }
@ -109,8 +113,8 @@ class Doctrine_RawSql extends Doctrine_Hydrate
} else { } else {
$parts[$p][0] .= ' '.$part; $parts[$p][0] .= ' '.$part;
} }
}; }
}; }
$this->parts = $parts; $this->parts = $parts;
$this->parts['select'] = array(); $this->parts['select'] = array();