2007-09-21 22:01:08 +04:00
|
|
|
<?php
|
2007-09-23 02:02:58 +04:00
|
|
|
/*
|
|
|
|
* $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.com>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Doctrine_Resource_Query
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage Resource
|
|
|
|
* @author Jonathan H. Wage <jwage@mac.com>
|
2007-09-23 02:02:58 +04:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @version $Revision$
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
*/
|
2007-09-24 08:58:57 +04:00
|
|
|
class Doctrine_Resource_Query
|
2007-09-21 22:01:08 +04:00
|
|
|
{
|
2007-09-24 08:58:57 +04:00
|
|
|
protected $_parts = array();
|
|
|
|
protected $_dql = null;
|
2007-09-21 22:01:08 +04:00
|
|
|
|
2007-09-24 08:58:57 +04:00
|
|
|
public function getConfig($key = null)
|
2007-09-21 22:01:08 +04:00
|
|
|
{
|
2007-09-24 08:58:57 +04:00
|
|
|
return Doctrine_Resource_Client::getInstance()->getConfig($key);
|
2007-09-21 22:01:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function query($dql, $params = array())
|
|
|
|
{
|
2007-09-24 08:58:57 +04:00
|
|
|
$this->_dql = $dql;
|
2007-09-21 22:01:08 +04:00
|
|
|
|
|
|
|
return $this->execute($params);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute($params = array())
|
|
|
|
{
|
2007-09-24 23:29:56 +04:00
|
|
|
$params = is_array($params) ? $params:array($params);
|
|
|
|
|
2007-09-24 08:58:57 +04:00
|
|
|
$request = new Doctrine_Resource_Request();
|
|
|
|
$request->set('dql', $this->getDql());
|
|
|
|
$request->set('params', $params);
|
2007-09-27 01:12:52 +04:00
|
|
|
$request->set('action', 'query');
|
2007-09-26 01:39:38 +04:00
|
|
|
$request->set('model', $this->getModel());
|
2007-09-21 22:01:08 +04:00
|
|
|
|
2007-09-24 08:58:57 +04:00
|
|
|
$response = $request->execute();
|
2007-09-21 22:01:08 +04:00
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! empty($response)) {
|
2007-09-26 01:39:38 +04:00
|
|
|
|
|
|
|
$collection = new Doctrine_Resource_Collection($this->getModel());
|
2007-09-27 01:12:52 +04:00
|
|
|
$collection->fromArray($response);
|
|
|
|
} else {
|
|
|
|
$model = $this->getModel();
|
2007-09-26 01:39:38 +04:00
|
|
|
|
2007-09-27 01:12:52 +04:00
|
|
|
$collection = new Doctrine_Resource_Collection($model);
|
|
|
|
$collection[] = new $model();
|
2007-09-24 23:29:56 +04:00
|
|
|
}
|
2007-09-27 01:12:52 +04:00
|
|
|
|
|
|
|
return $collection;
|
2007-09-21 22:01:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDql()
|
|
|
|
{
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! $this->_dql && !empty($this->_parts)) {
|
2007-09-21 22:01:08 +04:00
|
|
|
$q = '';
|
2007-09-24 08:58:57 +04:00
|
|
|
$q .= ( ! empty($this->_parts['select']))? 'SELECT ' . implode(', ', $this->_parts['select']) : '';
|
|
|
|
$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']) : '';
|
2007-09-21 22:01:08 +04:00
|
|
|
|
|
|
|
return $q;
|
|
|
|
} else {
|
2007-09-24 08:58:57 +04:00
|
|
|
return $this->_dql;
|
2007-09-21 22:01:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function buildUrl($array)
|
|
|
|
{
|
|
|
|
$url = '';
|
|
|
|
|
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
$url .= $this->buildUrl($value);
|
|
|
|
} else {
|
|
|
|
$url .= $key.'='.$value.'&';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getModel()
|
|
|
|
{
|
|
|
|
$dql = $this->getDql();
|
|
|
|
|
|
|
|
$e = explode('FROM ', $dql);
|
|
|
|
$e = explode(' ', $e[1]);
|
|
|
|
|
|
|
|
return $e[0];
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* addSelect
|
|
|
|
* adds fields to the SELECT part of the query
|
|
|
|
*
|
|
|
|
* @param string $select Query SELECT part
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
|
|
|
public function addSelect($select)
|
|
|
|
{
|
|
|
|
return $this->parseQueryPart('select', $select, true);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* addFrom
|
|
|
|
* adds fields to the FROM part of the query
|
|
|
|
*
|
|
|
|
* @param string $from Query FROM part
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
|
|
|
public function addFrom($from)
|
|
|
|
{
|
|
|
|
return $this->parseQueryPart('from', $from, true);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* addWhere
|
|
|
|
* adds conditions to the WHERE part of the query
|
|
|
|
*
|
|
|
|
* @param string $where Query WHERE part
|
|
|
|
* @param mixed $params an array of parameters or a simple scalar
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
2007-09-24 23:29:56 +04:00
|
|
|
public function addWhere($where)
|
2007-09-21 22:01:08 +04:00
|
|
|
{
|
|
|
|
return $this->parseQueryPart('where', $where, true);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* addGroupBy
|
|
|
|
* adds fields to the GROUP BY part of the query
|
|
|
|
*
|
|
|
|
* @param string $groupby Query GROUP BY part
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
|
|
|
public function addGroupBy($groupby)
|
|
|
|
{
|
|
|
|
return $this->parseQueryPart('groupby', $groupby, true);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* addHaving
|
|
|
|
* adds conditions to the HAVING part of the query
|
|
|
|
*
|
|
|
|
* @param string $having Query HAVING part
|
|
|
|
* @param mixed $params an array of parameters or a simple scalar
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
2007-09-24 23:29:56 +04:00
|
|
|
public function addHaving($having)
|
2007-09-21 22:01:08 +04:00
|
|
|
{
|
|
|
|
return $this->parseQueryPart('having', $having, true);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* addOrderBy
|
|
|
|
* adds fields to the ORDER BY part of the query
|
|
|
|
*
|
|
|
|
* @param string $orderby Query ORDER BY part
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
|
|
|
public function addOrderBy($orderby)
|
|
|
|
{
|
|
|
|
return $this->parseQueryPart('orderby', $orderby, true);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* select
|
|
|
|
* sets the SELECT part of the query
|
|
|
|
*
|
|
|
|
* @param string $select Query SELECT part
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
|
|
|
public function select($select)
|
|
|
|
{
|
|
|
|
return $this->parseQueryPart('select', $select);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2007-09-24 23:29:56 +04:00
|
|
|
$this->parseQueryPart('distinct', (bool) $flag);
|
2007-09-21 22:01:08 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* from
|
|
|
|
* sets the FROM part of the query
|
|
|
|
*
|
|
|
|
* @param string $from Query FROM part
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
|
|
|
public function from($from)
|
|
|
|
{
|
|
|
|
return $this->parseQueryPart('from', $from);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* innerJoin
|
|
|
|
* appends an INNER JOIN to the FROM part of the query
|
|
|
|
*
|
|
|
|
* @param string $join Query INNER JOIN
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
|
|
|
public function innerJoin($join)
|
|
|
|
{
|
|
|
|
return $this->parseQueryPart('from', 'INNER JOIN ' . $join, true);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* leftJoin
|
|
|
|
* appends a LEFT JOIN to the FROM part of the query
|
|
|
|
*
|
|
|
|
* @param string $join Query LEFT JOIN
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
|
|
|
public function leftJoin($join)
|
|
|
|
{
|
|
|
|
return $this->parseQueryPart('from', 'LEFT JOIN ' . $join, true);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* groupBy
|
|
|
|
* sets the GROUP BY part of the query
|
|
|
|
*
|
|
|
|
* @param string $groupby Query GROUP BY part
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
|
|
|
public function groupBy($groupby)
|
|
|
|
{
|
|
|
|
return $this->parseQueryPart('groupby', $groupby);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* where
|
|
|
|
* sets the WHERE part of the query
|
|
|
|
*
|
|
|
|
* @param string $join Query WHERE part
|
|
|
|
* @param mixed $params an array of parameters or a simple scalar
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
2007-09-24 23:29:56 +04:00
|
|
|
public function where($where)
|
2007-09-21 22:01:08 +04:00
|
|
|
{
|
|
|
|
return $this->parseQueryPart('where', $where);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* having
|
|
|
|
* sets the HAVING part of the query
|
|
|
|
*
|
|
|
|
* @param string $having Query HAVING part
|
|
|
|
* @param mixed $params an array of parameters or a simple scalar
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
2007-09-24 23:29:56 +04:00
|
|
|
public function having($having)
|
2007-09-21 22:01:08 +04:00
|
|
|
{
|
|
|
|
return $this->parseQueryPart('having', $having);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* orderBy
|
|
|
|
* sets the ORDER BY part of the query
|
|
|
|
*
|
|
|
|
* @param string $orderby Query ORDER BY part
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
|
|
|
public function orderBy($orderby)
|
|
|
|
{
|
|
|
|
return $this->parseQueryPart('orderby', $orderby);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* limit
|
|
|
|
* sets the Query query limit
|
|
|
|
*
|
|
|
|
* @param integer $limit limit to be used for limiting the query results
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
|
|
|
public function limit($limit)
|
|
|
|
{
|
|
|
|
return $this->parseQueryPart('limit', $limit);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* offset
|
|
|
|
* sets the Query query offset
|
|
|
|
*
|
|
|
|
* @param integer $offset offset to be used for paginating the query
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
|
|
|
public function offset($offset)
|
|
|
|
{
|
|
|
|
return $this->parseQueryPart('offset', $offset);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
/**
|
|
|
|
* parseQueryPart
|
|
|
|
* parses given DQL query part
|
|
|
|
*
|
|
|
|
* @param string $queryPartName the name of the query part
|
|
|
|
* @param string $queryPart query part to be parsed
|
|
|
|
* @return Doctrine_Query this object
|
|
|
|
*/
|
|
|
|
public function parseQueryPart($queryPartName, $queryPart)
|
|
|
|
{
|
2007-09-24 08:58:57 +04:00
|
|
|
$this->_parts[$queryPartName][] = $queryPart;
|
2007-09-21 22:01:08 +04:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2007-09-23 02:02:58 +04:00
|
|
|
}
|