Commit ba4c83ef authored by zYne's avatar zYne

Total rewrite for DQL alias model: now using short aliases instead of long...

Total rewrite for DQL alias model: now using short aliases instead of long aliases (needed for Oracle portability). 
parent 185c3347
...@@ -28,6 +28,43 @@ ...@@ -28,6 +28,43 @@
* @package Doctrine * @package Doctrine
*/ */
class Doctrine_DB2 implements Countable, IteratorAggregate { class Doctrine_DB2 implements Countable, IteratorAggregate {
/**
* A connection operation or selecting a database.
*/
const CONNECT = 1;
/**
* Any general database query that does not fit into the other constants.
*/
const QUERY = 2;
/**
* Adding new data to the database, such as SQL's INSERT.
*/
const INSERT = 4;
/**
* Updating existing information in the database, such as SQL's UPDATE.
*
*/
const UPDATE = 8;
/**
* An operation related to deleting data in the database,
* such as SQL's DELETE.
*/
const DELETE = 16;
/**
* Retrieving information from the database, such as SQL's SELECT.
*/
const SELECT = 32;
/**
* Transactional operation, such as start transaction, commit, or rollback.
*/
const TRANSACTION = 64;
/** /**
* default DSN * default DSN
*/ */
...@@ -60,10 +97,16 @@ class Doctrine_DB2 implements Countable, IteratorAggregate { ...@@ -60,10 +97,16 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
* @var Doctrine_DB_EventListener_Interface|Doctrine_Overloadable $listener listener for listening events * @var Doctrine_DB_EventListener_Interface|Doctrine_Overloadable $listener listener for listening events
*/ */
protected $listener; protected $listener;
/**
private static $driverMap = array("oracle" => "oci8", * @var integer $querySequence
"postgres" => "pgsql", */
"oci" => "oci8"); protected $querySequence = 0;
private static $driverMap = array('oracle' => 'oci8',
'postgres' => 'pgsql',
'oci' => 'oci8',
'sqlite2' => 'sqlite',
'sqlite3' => 'sqlite');
/** /**
...@@ -79,6 +122,13 @@ class Doctrine_DB2 implements Countable, IteratorAggregate { ...@@ -79,6 +122,13 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
$this->password = $password; $this->password = $password;
$this->listener = new Doctrine_DB_EventListener(); $this->listener = new Doctrine_DB_EventListener();
} }
/**
* getQuerySequence
*/
public function getQuerySequence() {
return $this->querySequence;
}
/** /**
* getDBH * getDBH
*/ */
...@@ -303,31 +353,36 @@ class Doctrine_DB2 implements Countable, IteratorAggregate { ...@@ -303,31 +353,36 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
$args = func_get_args(); $args = func_get_args();
$this->listener->onPrePrepare($this, $args); $this->listener->onPrePrepare($this, $statement, $args);
$stmt = $this->dbh->prepare($statement); $stmt = $this->dbh->prepare($statement);
$this->listener->onPrepare($this, $args); $this->listener->onPrepare($this, $statement, $args, $this->querySequence);
$this->querySequence++;
return $stmt; return $stmt;
} }
/** /**
* query * query
* *
* @param string $statement * @param string $statement
* @param array $params
* @return Doctrine_DB_Statement|boolean * @return Doctrine_DB_Statement|boolean
*/ */
public function query($statement, array $params = array()) { public function query($statement, array $params = array()) {
$this->connect(); $this->connect();
$this->listener->onPreQuery($this, $params); $this->listener->onPreQuery($this, $statement, $params);
if( ! empty($params)) if( ! empty($params))
$stmt = $this->dbh->query($statement)->execute($params); $stmt = $this->dbh->query($statement)->execute($params);
else else
$stmt = $this->dbh->query($statement); $stmt = $this->dbh->query($statement);
$this->listener->onQuery($this, $params); $this->listener->onQuery($this, $statement, $params, $this->querySequence);
$this->querySequence++;
return $stmt; return $stmt;
} }
...@@ -355,11 +410,11 @@ class Doctrine_DB2 implements Countable, IteratorAggregate { ...@@ -355,11 +410,11 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
$args = func_get_args(); $args = func_get_args();
$this->listener->onPreExec($this, $args); $this->listener->onPreExec($this, $statement, $args);
$rows = $this->dbh->exec($statement); $rows = $this->dbh->exec($statement);
$this->listener->onExec($this, $args); $this->listener->onExec($this, $statement, $args);
return $rows; return $rows;
} }
...@@ -477,7 +532,8 @@ class Doctrine_DB2 implements Countable, IteratorAggregate { ...@@ -477,7 +532,8 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
* @return ArrayIterator * @return ArrayIterator
*/ */
public function getIterator() { public function getIterator() {
return new ArrayIterator($this->queries); if($this->listener instanceof Doctrine_DB_Profiler)
return $this->listener;
} }
/** /**
* count * count
...@@ -486,7 +542,7 @@ class Doctrine_DB2 implements Countable, IteratorAggregate { ...@@ -486,7 +542,7 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
* @return integer * @return integer
*/ */
public function count() { public function count() {
return count($this->queries); return $this->querySequence;
} }
} }
......
...@@ -152,12 +152,15 @@ final class Doctrine { ...@@ -152,12 +152,15 @@ final class Doctrine {
/** /**
* automatic length validations attribute * automatic length validations attribute
*/ */
const ATTR_AUTO_LENGTH_VLD = 19; const ATTR_AUTO_LENGTH_VLD = 19;
/** /**
* automatic type validations attribute * automatic type validations attribute
*/ */
const ATTR_AUTO_TYPE_VLD = 20; const ATTR_AUTO_TYPE_VLD = 20;
/**
* short aliases attribute
*/
const ATTR_SHORT_ALIASES = 21;
/** /**
* LIMIT CONSTANTS * LIMIT CONSTANTS
......
...@@ -117,6 +117,7 @@ abstract class Doctrine_Configurable { ...@@ -117,6 +117,7 @@ abstract class Doctrine_Configurable {
case Doctrine::ATTR_VLD: case Doctrine::ATTR_VLD:
case Doctrine::ATTR_AUTO_LENGTH_VLD: case Doctrine::ATTR_AUTO_LENGTH_VLD:
case Doctrine::ATTR_AUTO_TYPE_VLD: case Doctrine::ATTR_AUTO_TYPE_VLD:
case Doctrine::ATTR_SHORT_ALIASES:
case Doctrine::ATTR_QUERY_LIMIT: case Doctrine::ATTR_QUERY_LIMIT:
break; break;
...@@ -190,8 +191,8 @@ abstract class Doctrine_Configurable { ...@@ -190,8 +191,8 @@ abstract class Doctrine_Configurable {
public function getAttribute($attribute) { public function getAttribute($attribute) {
$attribute = (int) $attribute; $attribute = (int) $attribute;
if($attribute < 1 || $attribute > 20) if($attribute < 1 || $attribute > 21)
throw new InvalidKeyException(); throw new Doctrine_Exception('Unknown attribute.');
if( ! isset($this->attributes[$attribute])) { if( ! isset($this->attributes[$attribute])) {
if(isset($this->parent)) if(isset($this->parent))
......
...@@ -444,6 +444,12 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -444,6 +444,12 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
* returns an iterator that iterators through all * returns an iterator that iterators through all
* initialized table objects * initialized table objects
* *
* <code>
* foreach($conn as $index => $table) {
* print $table; // get a string representation of each table object
* }
* </code>
*
* @return ArrayIterator * @return ArrayIterator
*/ */
public function getIterator() { public function getIterator() {
......
...@@ -26,17 +26,17 @@ ...@@ -26,17 +26,17 @@
* @package Doctrine * @package Doctrine
*/ */
class Doctrine_DB_EventListener implements Doctrine_DB_EventListener_Interface { class Doctrine_DB_EventListener implements Doctrine_DB_EventListener_Interface {
public function onPreQuery(Doctrine_DB2 $dbh, array $args) { } public function onPreQuery(Doctrine_DB2 $dbh, $statement, array $args) { }
public function onQuery(Doctrine_DB2 $dbh, array $args) { } public function onQuery(Doctrine_DB2 $dbh, $statement, array $args, $queryId) { }
public function onPrePrepare(Doctrine_DB2 $dbh, array $args) { } public function onPrePrepare(Doctrine_DB2 $dbh, $statement, array $args) { }
public function onPrepare(Doctrine_DB2 $dbh, array $args) { } public function onPrepare(Doctrine_DB2 $dbh, $statement, array $args, $queryId) { }
public function onPreCommit(Doctrine_DB2 $dbh) { } public function onPreCommit(Doctrine_DB2 $dbh) { }
public function onCommit(Doctrine_DB2 $dbh) { } public function onCommit(Doctrine_DB2 $dbh) { }
public function onPreExec(Doctrine_DB2 $dbh, array $args) { } public function onPreExec(Doctrine_DB2 $dbh, $statement, array $args) { }
public function onExec(Doctrine_DB2 $dbh, array $args) { } public function onExec(Doctrine_DB2 $dbh, $statement, array $args) { }
public function onPreRollBack(Doctrine_DB2 $dbh) { } public function onPreRollBack(Doctrine_DB2 $dbh) { }
public function onRollBack(Doctrine_DB2 $dbh) { } public function onRollBack(Doctrine_DB2 $dbh) { }
......
...@@ -54,12 +54,12 @@ class Doctrine_DB_EventListener_Chain extends Doctrine_Access implements Doctrin ...@@ -54,12 +54,12 @@ class Doctrine_DB_EventListener_Chain extends Doctrine_Access implements Doctrin
$this->listeners[$name] = $listener; $this->listeners[$name] = $listener;
} }
public function onPreQuery(Doctrine_DB2 $dbh, array $args) { public function onQuery(Doctrine_DB2 $dbh, $statement, array $args, $queryId) {
foreach($this->listeners as $listener) { foreach($this->listeners as $listener) {
$listener->onPreQuery($dbh, $args); $listener->onPreQuery($dbh, $args);
} }
} }
public function onQuery(Doctrine_DB2 $dbh, array $args) { public function onPreQuery(Doctrine_DB2 $dbh, $statement, array $args) {
foreach($this->listeners as $listener) { foreach($this->listeners as $listener) {
$listener->onQuery($dbh, $args); $listener->onQuery($dbh, $args);
} }
......
...@@ -26,14 +26,14 @@ ...@@ -26,14 +26,14 @@
* @package Doctrine * @package Doctrine
*/ */
interface Doctrine_DB_EventListener_Interface { interface Doctrine_DB_EventListener_Interface {
public function onPreQuery(Doctrine_DB2 $dbh, array $args); public function onPreQuery(Doctrine_DB2 $dbh, $statement, array $args);
public function onQuery(Doctrine_DB2 $dbh, array $args); public function onQuery(Doctrine_DB2 $dbh, $statement, array $args, $queryId);
public function onPrePrepare(Doctrine_DB2 $dbh, array $args); public function onPrePrepare(Doctrine_DB2 $dbh, $statement, array $args);
public function onPrepare(Doctrine_DB2 $dbh, array $args); public function onPrepare(Doctrine_DB2 $dbh, $statement, array $args, $queryId);
public function onPreExec(Doctrine_DB2 $dbh, array $args); public function onPreExec(Doctrine_DB2 $dbh, $statement, array $args);
public function onExec(Doctrine_DB2 $dbh, array $args); public function onExec(Doctrine_DB2 $dbh, $statement, array $args);
public function onPreCommit(Doctrine_DB2 $dbh); public function onPreCommit(Doctrine_DB2 $dbh);
public function onCommit(Doctrine_DB2 $dbh); public function onCommit(Doctrine_DB2 $dbh);
......
...@@ -26,25 +26,225 @@ ...@@ -26,25 +26,225 @@
* @package Doctrine * @package Doctrine
*/ */
class Doctrine_DB_Profiler extends Doctrine_DB_EventListener { class Doctrine_DB_Profiler extends Doctrine_DB_EventListener {
private $queries; public function onPreQuery(Doctrine_DB2 $dbh, $statement, array $args) {
$this->queryStart($statement);
}
public function onQuery(Doctrine_DB2 $dbh, $statement, array $args, $queryId) {
$this->queryEnd($queryId);
}
public function onPrePrepare(Doctrine_DB2 $dbh, $statement, array $args) {
$this->prepareTimes[$dbh->getQuerySequence()] = microtime(true);
}
public function onPrepare(Doctrine_DB2 $dbh, $statement, array $args, $queryId) {
$this->prepareTimes[$queryId] = (microtime(true) - $this->prepareTimes[$queryId]);
}
public function onPreQuery(Doctrine_DB $dbh, array $args) { public function onPreCommit(Doctrine_DB2 $dbh) { }
$this->queries[] = $args[0]; public function onCommit(Doctrine_DB2 $dbh) { }
public function onPreRollBack(Doctrine_DB2 $dbh) { }
public function onRollBack(Doctrine_DB2 $dbh) { }
public function onPreBeginTransaction(Doctrine_DB2 $dbh) { }
public function onBeginTransaction(Doctrine_DB2 $dbh) { }
public function onPreExecute(Doctrine_DB_Statement $stmt, array $params) {
$this->queryStart($stmt->getQuery(), $stmt->getQuerySequence());
} }
public function onQuery(Doctrine_DB $dbh, array $args) { } public function onExecute(Doctrine_DB_Statement $stmt, array $params) {
$this->queryEnd($stmt->getQuerySequence());
}
/**
* Array of Zend_Db_Profiler_Query objects.
*
* @var Zend_Db_Profiler_Query
*/
protected $_queryProfiles = array();
public function onPrePrepare(Doctrine_DB $dbh, array $args) { } protected $_prepareTimes = array();
public function onPrepare(Doctrine_DB $dbh, array $args) { } /**
* Stores the number of seconds to filter. NULL if filtering by time is
* disabled. If an integer is stored here, profiles whose elapsed time
* is less than this value in seconds will be unset from
* the self::$_queryProfiles array.
*
* @var integer
*/
protected $_filterElapsedSecs = null;
public function onPreCommit(Doctrine_DB $dbh) { } /**
public function onCommit(Doctrine_DB $dbh) { } * Logical OR of any of the filter constants. NULL if filtering by query
* type is disable. If an integer is stored here, it is the logical OR of
* any of the query type constants. When the query ends, if it is not
* one of the types specified, it will be unset from the
* self::$_queryProfiles array.
*
* @var integer
*/
protected $_filterTypes = null;
public function onPreRollBack(Doctrine_DB $dbh) { }
public function onRollBack(Doctrine_DB $dbh) { }
public function onPreBeginTransaction(Doctrine_DB $dbh) { } /**
public function onBeginTransaction(Doctrine_DB $dbh) { } * Start a query. Creates a new query profile object (Zend_Db_Profiler_Query)
* and returns the "query profiler handle". Run the query, then call
* queryEnd() and pass it this handle to make the query as ended and
* record the time. If the profiler is not enabled, this takes no
* action and immediately runs.
*
* @param string $queryText SQL statement
* @param int $queryType Type of query, one of the Zend_Db_Profiler::* constants
* @return mixed
*/
public function queryStart($queryText, $querySequence = -1) {
$prepareTime = (isset($this->prepareTimes[$querySequence])) ? $this->prepareTimes[$querySequence] : null;
$this->_queryProfiles[] = new Doctrine_DB_Profiler_Query($queryText, $prepareTime);
}
/**
* Ends a query. Pass it the handle that was returned by queryStart().
* This will mark the query as ended and save the time.
*
* @param integer $queryId
* @throws Zend_Db_Profiler_Exception
* @return boolean
*/
public function queryEnd($queryId = null) {
// Check for a valid query handle.
if($queryId === null)
$qp = end($this->_queryProfiles);
else
$qp = $this->_queryProfiles[$queryId];
if($qp === null || $qp->hasEnded()) {
throw new Zend_Db_Profiler_Exception('Query with profiler handle "'
. $queryId .'" has already ended.');
}
// End the query profile so that the elapsed time can be calculated.
$qp->end();
}
public function onPreExecute(Doctrine_DB_Statement $stmt, array $params) { } /**
public function onExecute(Doctrine_DB_Statement $stmt, array $params) { } * Get a profile for a query. Pass it the same handle that was returned
* by queryStart() and it will return a Zend_Db_Profiler_Query object.
*
* @param int $queryId
* @throws Zend_Db_Profiler_Exception
* @return Zend_Db_Profiler_Query
*/
public function getQueryProfile($queryId)
{
if (!array_key_exists($queryId, $this->_queryProfiles)) {
throw new Zend_Db_Profiler_Exception("Query handle \"$queryId\" not found in profiler log.");
}
return $this->_queryProfiles[$queryId];
}
/**
* Get an array of query profiles (Zend_Db_Profiler_Query objects). If $queryType
* is set to one of the Zend_Db_Profiler::* constants then only queries of that
* type will be returned. Normally, queries that have not yet ended will
* not be returned unless $showUnfinished is set to True. If no
* queries were found, False is returned.
*
* @param string $queryType
* @param bool $showUnfinished
* @return mixed
*/
public function getQueryProfiles($queryType=null, $showUnfinished=false)
{
$queryProfiles = array();
foreach ($this->_queryProfiles as $key=>$qp) {
/* @var $qp Zend_Db_Profiler_Query */
if ($queryType===null) {
$condition=true;
} else {
$condition=($qp->getQueryType() & $queryType);
}
if (($qp->hasEnded() || $showUnfinished) && $condition) {
$queryProfiles[$key] = $qp;
}
}
if (empty($queryProfiles)) {
$queryProfiles = false;
}
return $queryProfiles;
}
/**
* Get the total elapsed time (in seconds) of all of the profiled queries.
* Only queries that have ended will be counted. If $queryType is set to
* one of the Zend_Db_Profiler::* constants, the elapsed time will be calculated
* only for queries of that type.
*
* @param int $queryType
* @return int
*/
public function getTotalElapsedSecs($queryType = null)
{
$elapsedSecs = 0;
foreach ($this->_queryProfiles as $key=>$qp) {
/* @var $qp Zend_Db_Profiler_Query */
is_null($queryType)? $condition=true : $condition=($qp->getQueryType() & $queryType);
if (($qp->hasEnded()) && $condition) {
$elapsedSecs += $qp->getElapsedSecs();
}
}
return $elapsedSecs;
}
/**
* Get the total number of queries that have been profiled. Only queries that have ended will
* be counted. If $queryType is set to one of the Zend_Db_Profiler::* constants, only queries of
* that type will be counted.
*
* @param int $queryType
* @return int
*/
public function getTotalNumQueries($queryType = null)
{
if (is_null($queryType)) {
return sizeof($this->_queryProfiles);
}
$numQueries = 0;
foreach ($this->_queryProfiles as $qp) {
/* @var $qp Zend_Db_Profiler_Query */
is_null($queryType)? $condition=true : $condition=($qp->getQueryType() & $queryType);
if ($qp->hasEnded() && $condition) {
$numQueries++;
}
}
return $numQueries;
}
public function pop() {
return array_pop($this->_queryProfiles);
}
/**
* Get the Zend_Db_Profiler_Query object for the last query that was run, regardless if it has
* ended or not. If the query has not ended, it's end time will be Null.
*
* @return Zend_Db_Profiler_Query
*/
public function lastQuery() {
if (empty($this->_queryProfiles)) {
return false;
}
end($this->_queryProfiles);
return current($this->_queryProfiles);
}
} }
<?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.com>.
*/
/**
* Doctrine_DB_Profiler_Query
*
* @author Konsta Vesterinen
* @license LGPL
* @package Doctrine
*/
class Doctrine_DB_Profiler_Query {
/**
* @var string SQL query string or user comment, set by $query argument in constructor.
*/
protected $query ='';
/**
* @var integer One of the Zend_Db_Profiler constants for query type, set by $queryType argument in constructor.
*/
protected $queryType = 0;
protected $prepareTime;
/**
* @var float|null Unix timestamp with microseconds when instantiated.
*/
protected $startedMicrotime;
/**
* Unix timestamp with microseconds when self::queryEnd() was called.
*
* @var null|integer
*/
protected $endedMicrotime;
/**
* Class constructor. A query is about to be started, save the query text ($query) and its
* type (one of the Zend_Db_Profiler::* constants).
*
* @param string $query
* @param int $queryType
* @return bool
*/
public function __construct($query, $prepareTime = null)
{
$this->query = $query;
$this->prepareTime = $prepareTime;
$this->startedMicrotime = microtime(true);
return true;
}
/**
* The query has ended. Record the time so that the elapsed time can be determined later.
*
* @return bool
*/
public function end() {
$this->endedMicrotime = microtime(true);
return true;
}
public function getPrepareTime() {
return $this->prepareTime;
}
/**
* Has this query ended?
*
* @return bool
*/
public function hasEnded() {
return ($this->endedMicrotime != null);
}
/**
* Get the original SQL text of the query.
*
* @return string
*/
public function getQuery() {
return $this->query;
}
/**
* Get the type of this query (one of the Zend_Db_Profiler::* constants)
*
* @return int
*/
public function getQueryType() {
return $this->queryType;
}
/**
* Get the elapsed time (in seconds) that the query ran. If the query has
* not yet ended, return false.
*
* @return mixed
*/
public function getElapsedSecs() {
if (is_null($this->endedMicrotime) && ! $this->prepareTime) {
return false;
}
return ($this->prepareTime + ($this->endedMicrotime - $this->startedMicrotime));
}
}
...@@ -27,12 +27,22 @@ ...@@ -27,12 +27,22 @@
*/ */
class Doctrine_DB_Statement extends PDOStatement { class Doctrine_DB_Statement extends PDOStatement {
protected $dbh; protected $dbh;
protected $querySequence;
protected function __construct($dbh) { protected function __construct($dbh) {
$this->dbh = $dbh; $this->dbh = $dbh;
$this->querySequence = $this->dbh->getQuerySequence();
} }
public function execute($params) { public function getQuerySequence() {
return $this->querySequence;
}
public function getQuery() {
return $this->queryString;
}
public function execute(array $params) {
$this->dbh->getListener()->onPreExecute($this, $params); $this->dbh->getListener()->onPreExecute($this, $params);
$ret = parent::execute($params); $ret = parent::execute($params);
......
...@@ -274,7 +274,7 @@ class Doctrine_DataDict_Sqlite extends Doctrine_DataDict { ...@@ -274,7 +274,7 @@ class Doctrine_DataDict_Sqlite extends Doctrine_DataDict {
*/ */
public function listTableColumns($table) { public function listTableColumns($table) {
$sql = "PRAGMA table_info($table)"; $sql = 'PRAGMA table_info(' . $table . ')';
$result = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); $result = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);
$description = array(); $description = array();
......
...@@ -88,6 +88,10 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { ...@@ -88,6 +88,10 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
protected $pendingAggregates = array(); protected $pendingAggregates = array();
protected $aggregateMap = array(); protected $aggregateMap = array();
protected $shortAliases = array();
protected $shortAliasIndexes = array();
/** /**
* @var array $parts SQL query string parts * @var array $parts SQL query string parts
*/ */
...@@ -227,6 +231,8 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { ...@@ -227,6 +231,8 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
$this->joins = array(); $this->joins = array();
$this->tableIndexes = array(); $this->tableIndexes = array();
$this->tableAliases = array(); $this->tableAliases = array();
$this->shortAliases = array();
$this->shortAliasIndexes = array();
} }
/** /**
* getConnection * getConnection
...@@ -371,6 +377,8 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { ...@@ -371,6 +377,8 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
foreach($data as $key => $row) { foreach($data as $key => $row) {
if(empty($row)) if(empty($row))
continue; continue;
//$key = array_search($key, $this->shortAliases);
foreach($this->tables as $k => $t) { foreach($this->tables as $k => $t) {
if ( ! strcasecmp($key, $k)) if ( ! strcasecmp($key, $k))
...@@ -391,9 +399,7 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { ...@@ -391,9 +399,7 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
if(isset($row[0])) { if(isset($row[0])) {
$path = array_search($name, $this->tableAliases); $path = array_search($name, $this->tableAliases);
$alias = $this->getPathAlias($path); $alias = $this->getPathAlias($path);
//print_r($this->pendingAggregates);
// map each aggregate value // map each aggregate value
foreach($row as $index => $value) { foreach($row as $index => $value) {
$agg = false; $agg = false;
...@@ -487,7 +493,7 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { ...@@ -487,7 +493,7 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
$pointer = $this->joins[$name]; $pointer = $this->joins[$name];
$path = array_search($name, $this->tableAliases); $path = array_search($name, $this->tableAliases);
$tmp = explode(".", $path); $tmp = explode('.', $path);
$alias = end($tmp); $alias = end($tmp);
$fk = $this->tables[$pointer]->getRelation($alias); $fk = $this->tables[$pointer]->getRelation($alias);
...@@ -533,6 +539,36 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { ...@@ -533,6 +539,36 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
} }
return false; return false;
} }
public function getShortAliasIndex($alias) {
if( ! isset($this->shortAliasIndexes[$alias]))
return 0;
return $this->shortAliasIndexes[$alias];
}
public function generateShortAlias($tableName) {
$char = strtolower(substr($tableName, 0, 1));
$alias = $char;
if( ! isset($this->shortAliasIndexes[$alias]))
$this->shortAliasIndexes[$alias] = 1;
while(isset($this->shortAliases[$alias])) {
$alias = $char . ++$this->shortAliasIndexes[$alias];
}
$this->shortAliases[$alias] = $tableName;
return $alias;
}
public function getShortAlias($tableName) {
$alias = array_search($tableName, $this->shortAliases);
if($alias !== false)
return $alias;
return $this->generateShortAlias($tableName);
}
/** /**
* applyInheritance * applyInheritance
* applies column aggregation inheritance to DQL / SQL query * applies column aggregation inheritance to DQL / SQL query
...@@ -552,23 +588,29 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { ...@@ -552,23 +588,29 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
$c = array(); $c = array();
$index = 0; $index = 0;
foreach($array as $tname => $maps) { foreach($array as $tableAlias => $maps) {
$a = array(); $a = array();
foreach($maps as $map) { foreach($maps as $map) {
$b = array(); $b = array();
foreach($map as $field => $value) { foreach($map as $field => $value) {
if($index > 0) if($index > 0)
$b[] = "(".$tname.".$field = $value OR $tname.$field IS NULL)"; $b[] = '(' . $tableAlias . '.' . $field . ' = ' . $value . ' OR ' . $tableAlias . '.' . $field . ' IS NULL)';
else else
$b[] = $tname.".$field = $value"; $b[] = $tableAlias . '.' . $field . ' = ' . $value;
} }
if( ! empty($b)) $a[] = implode(" AND ",$b);
if( ! empty($b))
$a[] = implode(' AND ', $b);
} }
if( ! empty($a)) $c[] = implode(" AND ",$a);
if( ! empty($a))
$c[] = implode(' AND ', $a);
$index++; $index++;
} }
$str .= implode(" AND ",$c); $str .= implode(' AND ', $c);
return $str; return $str;
} }
...@@ -576,6 +618,7 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { ...@@ -576,6 +618,7 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
* parseData * parseData
* parses the data returned by PDOStatement * parses the data returned by PDOStatement
* *
* @param PDOStatement $stmt
* @return array * @return array
*/ */
public function parseData(PDOStatement $stmt) { public function parseData(PDOStatement $stmt) {
......
...@@ -93,7 +93,8 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -93,7 +93,8 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
Doctrine::ATTR_AUTO_LENGTH_VLD => true, Doctrine::ATTR_AUTO_LENGTH_VLD => true,
Doctrine::ATTR_AUTO_TYPE_VLD => true, Doctrine::ATTR_AUTO_TYPE_VLD => true,
Doctrine::ATTR_CREATE_TABLES => true, Doctrine::ATTR_CREATE_TABLES => true,
Doctrine::ATTR_QUERY_LIMIT => Doctrine::LIMIT_RECORDS Doctrine::ATTR_QUERY_LIMIT => Doctrine::LIMIT_RECORDS,
Doctrine::ATTR_SHORT_ALIASES => false,
); );
foreach($attributes as $attribute => $value) { foreach($attributes as $attribute => $value) {
$old = $this->getAttribute($attribute); $old = $this->getAttribute($attribute);
......
This diff is collapsed.
...@@ -292,6 +292,16 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -292,6 +292,16 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
} }
$this->options[$name] = $value; $this->options[$name] = $value;
} }
public function usesInheritanceMap() {
return ( ! empty($this->options['inheritanceMap']));
}
public function getOption($name) {
if(isset($this->options[$name]))
return $this->options[$name];
return null;
}
/** /**
* setColumn * setColumn
* @param string $name * @param string $name
......
This diff is collapsed.
<?php
class Doctrine_Db_Profiler_TestCase extends Doctrine_UnitTestCase {
protected $dbh;
protected $profiler;
public function prepareTables() {}
public function prepareData() {}
public function testQuery() {
$this->dbh = Doctrine_DB2::getConnection('sqlite::memory:');
$this->profiler = new Doctrine_DB_Profiler();
$this->dbh->setListener($this->profiler);
$this->dbh->query('CREATE TABLE test (id INT)');
$this->assertEqual($this->profiler->lastQuery()->getQuery(), 'CREATE TABLE test (id INT)');
$this->assertTrue($this->profiler->lastQuery()->hasEnded());
$this->assertTrue(is_numeric($this->profiler->lastQuery()->getElapsedSecs()));
$this->assertEqual($this->dbh->count(), 1);
}
public function testPrepareAndExecute() {
$stmt = $this->dbh->prepare('INSERT INTO test (id) VALUES (?)');
$this->assertEqual($this->profiler->lastQuery()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
$this->assertFalse($this->profiler->lastQuery()->hasEnded());
$this->assertTrue(is_numeric($this->profiler->lastQuery()->getElapsedSecs()));
$stmt->execute(array(1));
$this->assertEqual($this->profiler->lastQuery()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
$this->assertTrue($this->profiler->lastQuery()->hasEnded());
$this->assertTrue(is_numeric($this->profiler->lastQuery()->getElapsedSecs()));
$this->assertEqual($this->dbh->count(), 2);
}
public function testMultiplePrepareAndExecute() {
$stmt = $this->dbh->prepare('INSERT INTO test (id) VALUES (?)');
$this->assertEqual($this->profiler->lastQuery()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
$this->assertFalse($this->profiler->lastQuery()->hasEnded());
$this->assertTrue(is_numeric($this->profiler->lastQuery()->getElapsedSecs()));
$stmt2 = $this->dbh->prepare('INSERT INTO test (id) VALUES (?)');
$this->assertEqual($this->profiler->lastQuery()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
$this->assertFalse($this->profiler->lastQuery()->hasEnded());
$this->assertTrue(is_numeric($this->profiler->lastQuery()->getElapsedSecs()));
$stmt->execute(array(1));
$stmt2->execute(array(1));
$this->assertEqual($this->profiler->lastQuery()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
$this->assertTrue($this->profiler->lastQuery()->hasEnded());
$this->assertTrue(is_numeric($this->profiler->lastQuery()->getElapsedSecs()));
$this->assertEqual($this->dbh->count(), 4);
}
/**
public function testExecuteStatementMultipleTimes() {
try {
$stmt = $this->dbh->prepare('INSERT INTO test (id) VALUES (?)');
$stmt->execute(array(1));
$stmt->execute(array(1));
$this->pass();
} catch(Doctrine_Db_Exception $e) {
$this->fail();
}
$this->assertEqual($this->profiler->lastQuery()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
$this->assertTrue($this->profiler->lastQuery()->hasEnded());
$this->assertTrue(is_numeric($this->profiler->lastQuery()->getElapsedSecs()));
$this->assertEqual($this->profiler->lastQuery()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
$this->assertTrue($this->profiler->lastQuery()->hasEnded());
$this->assertTrue(is_numeric($this->profiler->lastQuery()->getElapsedSecs()));
} */
}
?>
...@@ -13,7 +13,7 @@ class Doctrine_Query_ComponentAlias_TestCase extends Doctrine_UnitTestCase { ...@@ -13,7 +13,7 @@ class Doctrine_Query_ComponentAlias_TestCase extends Doctrine_UnitTestCase {
$this->assertEqual($users->count(), 8); $this->assertEqual($users->count(), 8);
$this->assertTrue($users[0]->Phonenumber instanceof Doctrine_Collection); $this->assertTrue($users[0]->Phonenumber instanceof Doctrine_Collection);
$this->assertEqual($q->getQuery(), $this->assertEqual($q->getQuery(),
"SELECT entity.id AS entity__id, entity.name AS entity__name, entity.loginname AS entity__loginname, entity.password AS entity__password, entity.type AS entity__type, entity.created AS entity__created, entity.updated AS entity__updated, entity.email_id AS entity__email_id, phonenumber.id AS phonenumber__id, phonenumber.phonenumber AS phonenumber__phonenumber, phonenumber.entity_id AS phonenumber__entity_id FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE (entity.type = 0)"); "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0)");
$this->assertEqual($count, count($this->dbh)); $this->assertEqual($count, count($this->dbh));
} }
...@@ -30,7 +30,7 @@ class Doctrine_Query_ComponentAlias_TestCase extends Doctrine_UnitTestCase { ...@@ -30,7 +30,7 @@ class Doctrine_Query_ComponentAlias_TestCase extends Doctrine_UnitTestCase {
$this->assertEqual($users->count(), 8); $this->assertEqual($users->count(), 8);
$this->assertTrue($users[0]->Phonenumber instanceof Doctrine_Collection); $this->assertTrue($users[0]->Phonenumber instanceof Doctrine_Collection);
$this->assertEqual($q->getQuery(), $this->assertEqual($q->getQuery(),
"SELECT entity.id AS entity__id, entity.name AS entity__name, entity.loginname AS entity__loginname, entity.password AS entity__password, entity.type AS entity__type, entity.created AS entity__created, entity.updated AS entity__updated, entity.email_id AS entity__email_id, entity2.id AS entity2__id, entity2.name AS entity2__name, entity2.loginname AS entity2__loginname, entity2.password AS entity2__password, entity2.type AS entity2__type, entity2.created AS entity2__created, entity2.updated AS entity2__updated, entity2.email_id AS entity2__email_id, phonenumber.id AS phonenumber__id, phonenumber.phonenumber AS phonenumber__phonenumber, phonenumber.entity_id AS phonenumber__entity_id FROM entity LEFT JOIN groupuser ON entity.id = groupuser.user_id LEFT JOIN entity AS entity2 ON entity2.id = groupuser.group_id LEFT JOIN phonenumber ON entity2.id = phonenumber.entity_id WHERE (entity.type = 0 AND (entity2.type = 1 OR entity2.type IS NULL))"); "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e LEFT JOIN groupuser ON e.id = groupuser.user_id LEFT JOIN entity e2 ON e2.id = groupuser.group_id LEFT JOIN phonenumber p ON e2.id = p.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$this->assertEqual(($count + 1), count($this->dbh)); $this->assertEqual(($count + 1), count($this->dbh));
} }
...@@ -47,7 +47,7 @@ class Doctrine_Query_ComponentAlias_TestCase extends Doctrine_UnitTestCase { ...@@ -47,7 +47,7 @@ class Doctrine_Query_ComponentAlias_TestCase extends Doctrine_UnitTestCase {
$this->assertEqual($users->count(), 8); $this->assertEqual($users->count(), 8);
$this->assertTrue($users[0]->Phonenumber instanceof Doctrine_Collection); $this->assertTrue($users[0]->Phonenumber instanceof Doctrine_Collection);
$this->assertEqual($q->getQuery(), $this->assertEqual($q->getQuery(),
"SELECT entity.id AS entity__id, entity.name AS entity__name, entity.loginname AS entity__loginname, entity.password AS entity__password, entity.type AS entity__type, entity.created AS entity__created, entity.updated AS entity__updated, entity.email_id AS entity__email_id, phonenumber.id AS phonenumber__id, phonenumber.phonenumber AS phonenumber__phonenumber, phonenumber.entity_id AS phonenumber__entity_id, entity2.id AS entity2__id, entity2.name AS entity2__name, entity2.loginname AS entity2__loginname, entity2.password AS entity2__password, entity2.type AS entity2__type, entity2.created AS entity2__created, entity2.updated AS entity2__updated, entity2.email_id AS entity2__email_id, phonenumber2.id AS phonenumber2__id, phonenumber2.phonenumber AS phonenumber2__phonenumber, phonenumber2.entity_id AS phonenumber2__entity_id FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id LEFT JOIN groupuser ON entity.id = groupuser.user_id LEFT JOIN entity AS entity2 ON entity2.id = groupuser.group_id LEFT JOIN phonenumber AS phonenumber2 ON entity2.id = phonenumber2.entity_id WHERE (entity.type = 0 AND (entity2.type = 1 OR entity2.type IS NULL))"); "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id, p2.id AS p2__id, p2.p AS p2__p, p2.entity_id AS p2__entity_id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id LEFT JOIN groupuser ON e.id = groupuser.user_id LEFT JOIN entity e2 ON e2.id = groupuser.group_id LEFT JOIN p AS p2 ON e2.id = p2.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$this->assertEqual($count, count($this->dbh)); $this->assertEqual($count, count($this->dbh));
} }
} }
......
...@@ -15,7 +15,7 @@ class Doctrine_Query_Condition_TestCase extends Doctrine_UnitTestCase { ...@@ -15,7 +15,7 @@ class Doctrine_Query_Condition_TestCase extends Doctrine_UnitTestCase {
$query->from("User(id)")->where("User.name LIKE 'z%' || User.name LIKE 's%'"); $query->from("User(id)")->where("User.name LIKE 'z%' || User.name LIKE 's%'");
$sql = "SELECT entity.id AS entity__id FROM entity WHERE (entity.name LIKE 'z%' OR entity.name LIKE 's%') AND (entity.type = 0)"; $sql = "SELECT e.id AS e__id FROM entity e WHERE (e.name LIKE 'z%' OR e.name LIKE 's%') AND (e.type = 0)";
$this->assertEqual($query->getQuery(), $sql); $this->assertEqual($query->getQuery(), $sql);
$query->where("(User.name LIKE 'z%') || (User.name LIKE 's%')"); $query->where("(User.name LIKE 'z%') || (User.name LIKE 's%')");
...@@ -32,7 +32,7 @@ class Doctrine_Query_Condition_TestCase extends Doctrine_UnitTestCase { ...@@ -32,7 +32,7 @@ class Doctrine_Query_Condition_TestCase extends Doctrine_UnitTestCase {
$query->where("(User.name LIKE 'z%') || User.name LIKE 's%' && User.name LIKE 'a%'"); $query->where("(User.name LIKE 'z%') || User.name LIKE 's%' && User.name LIKE 'a%'");
$sql = "SELECT entity.id AS entity__id FROM entity WHERE ((entity.name LIKE 'z%' OR entity.name LIKE 's%') AND entity.name LIKE 'a%') AND (entity.type = 0)"; $sql = "SELECT e.id AS e__id FROM entity e WHERE ((e.name LIKE 'z%' OR e.name LIKE 's%') AND e.name LIKE 'a%') AND (e.type = 0)";
$this->assertEqual($query->getQuery(), $sql); $this->assertEqual($query->getQuery(), $sql);
...@@ -52,7 +52,7 @@ class Doctrine_Query_Condition_TestCase extends Doctrine_UnitTestCase { ...@@ -52,7 +52,7 @@ class Doctrine_Query_Condition_TestCase extends Doctrine_UnitTestCase {
$query->from("User(id)")->where("User.name LIKE 'z%' || User.name LIKE 's%'"); $query->from("User(id)")->where("User.name LIKE 'z%' || User.name LIKE 's%'");
$sql = "SELECT entity.id AS entity__id FROM entity WHERE (entity.name LIKE 'z%' OR entity.name LIKE 's%') AND (entity.type = 0)"; $sql = "SELECT e.id AS e__id FROM entity e WHERE (e.name LIKE 'z%' OR e.name LIKE 's%') AND (e.type = 0)";
$this->assertEqual($query->getQuery(), $sql); $this->assertEqual($query->getQuery(), $sql);
$query->where("(User.name LIKE 'z%') OR (User.name LIKE 's%')"); $query->where("(User.name LIKE 'z%') OR (User.name LIKE 's%')");
...@@ -69,7 +69,7 @@ class Doctrine_Query_Condition_TestCase extends Doctrine_UnitTestCase { ...@@ -69,7 +69,7 @@ class Doctrine_Query_Condition_TestCase extends Doctrine_UnitTestCase {
$query->where("(User.name LIKE 'z%') OR User.name LIKE 's%' AND User.name LIKE 'a%'"); $query->where("(User.name LIKE 'z%') OR User.name LIKE 's%' AND User.name LIKE 'a%'");
$sql = "SELECT entity.id AS entity__id FROM entity WHERE ((entity.name LIKE 'z%' OR entity.name LIKE 's%') AND entity.name LIKE 'a%') AND (entity.type = 0)"; $sql = "SELECT e.id AS e__id FROM entity e WHERE ((e.name LIKE 'z%' OR e.name LIKE 's%') AND e.name LIKE 'a%') AND (e.type = 0)";
$this->assertEqual($query->getQuery(), $sql); $this->assertEqual($query->getQuery(), $sql);
......
...@@ -5,65 +5,65 @@ class Doctrine_Query_Delete_TestCase extends Doctrine_UnitTestCase { ...@@ -5,65 +5,65 @@ class Doctrine_Query_Delete_TestCase extends Doctrine_UnitTestCase {
$q->parseQuery('DELETE FROM User'); $q->parseQuery('DELETE FROM User');
$this->assertEqual($q->getQuery(), 'DELETE FROM entity WHERE (entity.type = 0)'); $this->assertEqual($q->getQuery(), 'DELETE FROM entity e WHERE (e.type = 0)');
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$q->delete()->from('User'); $q->delete()->from('User');
$this->assertEqual($q->getQuery(), 'DELETE FROM entity WHERE (entity.type = 0)'); $this->assertEqual($q->getQuery(), 'DELETE FROM entity e WHERE (e.type = 0)');
} }
public function testDeleteAll() { public function testDeleteAll() {
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$q->parseQuery('DELETE FROM Entity'); $q->parseQuery('DELETE FROM Entity');
$this->assertEqual($q->getQuery(), 'DELETE FROM entity'); $this->assertEqual($q->getQuery(), 'DELETE FROM entity e');
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$q->delete()->from('Entity'); $q->delete()->from('Entity');
$this->assertEqual($q->getQuery(), 'DELETE FROM entity'); $this->assertEqual($q->getQuery(), 'DELETE FROM entity e');
} }
public function testDeleteWithCondition() { public function testDeleteWithCondition() {
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$q->parseQuery('DELETE FROM Entity WHERE id = 3'); $q->parseQuery('DELETE FROM Entity WHERE id = 3');
$this->assertEqual($q->getQuery(), 'DELETE FROM entity WHERE id = 3'); $this->assertEqual($q->getQuery(), 'DELETE FROM entity e WHERE id = 3');
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$q->delete()->from('Entity')->where('id = 3'); $q->delete()->from('Entity')->where('id = 3');
$this->assertEqual($q->getQuery(), 'DELETE FROM entity WHERE id = 3'); $this->assertEqual($q->getQuery(), 'DELETE FROM entity e WHERE id = 3');
} }
public function testDeleteWithLimit() { public function testDeleteWithLimit() {
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$q->parseQuery('DELETE FROM Entity LIMIT 20'); $q->parseQuery('DELETE FROM Entity LIMIT 20');
$this->assertEqual($q->getQuery(), 'DELETE FROM entity LIMIT 20'); $this->assertEqual($q->getQuery(), 'DELETE FROM entity e LIMIT 20');
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$q->delete()->from('Entity')->limit(20); $q->delete()->from('Entity')->limit(20);
$this->assertEqual($q->getQuery(), 'DELETE FROM entity LIMIT 20'); $this->assertEqual($q->getQuery(), 'DELETE FROM entity e LIMIT 20');
} }
public function testDeleteWithLimitAndOffset() { public function testDeleteWithLimitAndOffset() {
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$q->parseQuery('DELETE FROM Entity LIMIT 10 OFFSET 20'); $q->parseQuery('DELETE FROM Entity LIMIT 10 OFFSET 20');
$this->assertEqual($q->getQuery(), 'DELETE FROM entity LIMIT 10 OFFSET 20'); $this->assertEqual($q->getQuery(), 'DELETE FROM entity e LIMIT 10 OFFSET 20');
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$q->delete()->from('Entity')->limit(10)->offset(20); $q->delete()->from('Entity')->limit(10)->offset(20);
$this->assertEqual($q->getQuery(), 'DELETE FROM entity LIMIT 10 OFFSET 20'); $this->assertEqual($q->getQuery(), 'DELETE FROM entity e LIMIT 10 OFFSET 20');
} }
} }
?> ?>
This diff is collapsed.
This diff is collapsed.
...@@ -7,21 +7,21 @@ class Doctrine_Query_Select_TestCase extends Doctrine_UnitTestCase { ...@@ -7,21 +7,21 @@ class Doctrine_Query_Select_TestCase extends Doctrine_UnitTestCase {
$q->parseQuery('SELECT COUNT(u.id) FROM User u'); $q->parseQuery('SELECT COUNT(u.id) FROM User u');
$this->assertEqual($q->getQuery(), 'SELECT COUNT(entity.id) AS entity__0 FROM entity WHERE (entity.type = 0)'); $this->assertEqual($q->getQuery(), 'SELECT COUNT(e.id) AS e__0 FROM entity e WHERE (e.type = 0)');
} }
public function testMultipleAggregateFunctions() { public function testMultipleAggregateFunctions() {
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$q->parseQuery('SELECT MAX(u.id), MIN(u.name) FROM User u'); $q->parseQuery('SELECT MAX(u.id), MIN(u.name) FROM User u');
$this->assertEqual($q->getQuery(), 'SELECT MAX(entity.id) AS entity__0, MIN(entity.name) AS entity__1 FROM entity WHERE (entity.type = 0)'); $this->assertEqual($q->getQuery(), 'SELECT MAX(e.id) AS e__0, MIN(e.name) AS e__1 FROM entity e WHERE (e.type = 0)');
} }
public function testMultipleAggregateFunctionsWithMultipleComponents() { public function testMultipleAggregateFunctionsWithMultipleComponents() {
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$q->parseQuery('SELECT MAX(u.id), MIN(u.name), COUNT(p.id) FROM User u, u.Phonenumber p'); $q->parseQuery('SELECT MAX(u.id), MIN(u.name), COUNT(p.id) FROM User u, u.Phonenumber p');
$this->assertEqual($q->getQuery(), 'SELECT MAX(entity.id) AS entity__0, MIN(entity.name) AS entity__1, COUNT(phonenumber.id) AS phonenumber__2 FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE (entity.type = 0)'); $this->assertEqual($q->getQuery(), 'SELECT MAX(e.id) AS e__0, MIN(e.name) AS e__1, COUNT(p.id) AS p__2 FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0)');
} }
public function testEmptySelectPart() { public function testEmptySelectPart() {
$q = new Doctrine_Query(); $q = new Doctrine_Query();
...@@ -97,28 +97,28 @@ class Doctrine_Query_Select_TestCase extends Doctrine_UnitTestCase { ...@@ -97,28 +97,28 @@ class Doctrine_Query_Select_TestCase extends Doctrine_UnitTestCase {
$q->parseQuery('SELECT u.* FROM User u'); $q->parseQuery('SELECT u.* FROM User u');
$this->assertEqual($q->getQuery(), 'SELECT entity.id AS entity__id, entity.name AS entity__name, entity.loginname AS entity__loginname, entity.password AS entity__password, entity.type AS entity__type, entity.created AS entity__created, entity.updated AS entity__updated, entity.email_id AS entity__email_id FROM entity WHERE (entity.type = 0)'); $this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id FROM entity e WHERE (e.type = 0)');
} }
public function testSingleComponentWithMultipleColumns() { public function testSingleComponentWithMultipleColumns() {
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$q->parseQuery('SELECT u.name, u.type FROM User u'); $q->parseQuery('SELECT u.name, u.type FROM User u');
$this->assertEqual($q->getQuery(), 'SELECT entity.id AS entity__id, entity.name AS entity__name, entity.type AS entity__type FROM entity WHERE (entity.type = 0)'); $this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name, e.type AS e__type FROM entity e WHERE (e.type = 0)');
} }
public function testMultipleComponentsWithAsterisk() { public function testMultipleComponentsWithAsterisk() {
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$q->parseQuery('SELECT u.*, p.* FROM User u, u.Phonenumber p'); $q->parseQuery('SELECT u.*, p.* FROM User u, u.Phonenumber p');
$this->assertEqual($q->getQuery(),'SELECT entity.id AS entity__id, entity.name AS entity__name, entity.loginname AS entity__loginname, entity.password AS entity__password, entity.type AS entity__type, entity.created AS entity__created, entity.updated AS entity__updated, entity.email_id AS entity__email_id, phonenumber.id AS phonenumber__id, phonenumber.phonenumber AS phonenumber__phonenumber, phonenumber.entity_id AS phonenumber__entity_id FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE (entity.type = 0)'); $this->assertEqual($q->getQuery(),'SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0)');
} }
public function testMultipleComponentsWithMultipleColumns() { public function testMultipleComponentsWithMultipleColumns() {
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$q->parseQuery('SELECT u.id, u.name, p.id FROM User u, u.Phonenumber p'); $q->parseQuery('SELECT u.id, u.name, p.id FROM User u, u.Phonenumber p');
$this->assertEqual($q->getQuery(),'SELECT entity.id AS entity__id, entity.name AS entity__name, phonenumber.id AS phonenumber__id FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE (entity.type = 0)'); $this->assertEqual($q->getQuery(),'SELECT e.id AS e__id, e.name AS e__name, p.id AS p__id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0)');
} }
} }
......
<?php
class Doctrine_Query_ShortAliases_TestCase extends Doctrine_UnitTestCase {
public function testShortAliasesWithSingleComponent() {
$q = new Doctrine_Query();
$q->select('u.name')->from('User u');
$this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name FROM entity e WHERE (e.type = 0)');
}
public function testShortAliasesWithOneToManyLeftJoin() {
$q = new Doctrine_Query();
$q->select('u.name, p.id')->from('User u LEFT JOIN u.Phonenumber p');
$this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name, p.id AS p__id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0)');
$users = $q->execute();
$this->assertEqual($users->count(), 8);
}
}
...@@ -5,7 +5,7 @@ class Doctrine_Query_Subquery_TestCase extends Doctrine_UnitTestCase { ...@@ -5,7 +5,7 @@ class Doctrine_Query_Subquery_TestCase extends Doctrine_UnitTestCase {
$q->from('User')->where("User.id NOT IN (FROM User(id) WHERE User.name = 'zYne')"); $q->from('User')->where("User.id NOT IN (FROM User(id) WHERE User.name = 'zYne')");
$this->assertEqual($q->getQuery(), $this->assertEqual($q->getQuery(),
"SELECT entity.id AS entity__id, entity.name AS entity__name, entity.loginname AS entity__loginname, entity.password AS entity__password, entity.type AS entity__type, entity.created AS entity__created, entity.updated AS entity__updated, entity.email_id AS entity__email_id FROM entity WHERE entity.id NOT IN (SELECT entity.id AS entity__id FROM entity WHERE entity.name = 'zYne' AND (entity.type = 0)) AND (entity.type = 0)"); "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id FROM entity e WHERE e.id NOT IN (SELECT e.id AS e__id FROM entity e WHERE e.name = 'zYne' AND (e.type = 0)) AND (e.type = 0)");
$users = $q->execute(); $users = $q->execute();
......
This diff is collapsed.
...@@ -5,13 +5,13 @@ class Doctrine_Query_Update_TestCase extends Doctrine_UnitTestCase { ...@@ -5,13 +5,13 @@ class Doctrine_Query_Update_TestCase extends Doctrine_UnitTestCase {
$q->parseQuery("UPDATE User u SET u.name = 'someone'"); $q->parseQuery("UPDATE User u SET u.name = 'someone'");
$this->assertEqual($q->getQuery(), "UPDATE entity SET entity.name = 'someone' WHERE (entity.type = 0)"); $this->assertEqual($q->getQuery(), "UPDATE entity SET e.name = 'someone' WHERE (e.type = 0)");
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$q->update('User u')->set('u.name', 'someone'); $q->update('User u')->set('u.name', 'someone');
$this->assertEqual($q->getQuery(), "UPDATE entity SET entity.name = 'someone' WHERE (entity.type = 0)"); $this->assertEqual($q->getQuery(), "UPDATE entity SET e.name = 'someone' WHERE (e.type = 0)");
} }
} }
?> ?>
...@@ -80,7 +80,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { ...@@ -80,7 +80,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase {
$users = $q->execute(); $users = $q->execute();
$this->assertEqual($users->count(), 2); $this->assertEqual($users->count(), 2);
$this->assertEqual($users[0]->name, 'someone'); $this->assertEqual($users[0]->name, 'someone');
$this->assertEqual($users[1]->name, 'someone.2'); $this->assertEqual($users[1]->name, 'someone.2');
} }
public function testComponentAliases() { public function testComponentAliases() {
$q = new Doctrine_Query(); $q = new Doctrine_Query();
...@@ -118,7 +118,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { ...@@ -118,7 +118,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase {
$users = $q->execute(); $users = $q->execute();
$this->assertEqual($users->count(), 1); $this->assertEqual($users->count(), 1);
$this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id FROM entity WHERE entity.name = 'someone' AND (entity.type = 0)"); $this->assertEqual($q->getQuery(), "SELECT e.id AS e__id FROM entity e WHERE e.name = 'someone' AND (e.type = 0)");
} }
public function testOperatorWithNoTrailingSpaces2() { public function testOperatorWithNoTrailingSpaces2() {
$q = new Doctrine_Query(); $q = new Doctrine_Query();
...@@ -128,7 +128,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { ...@@ -128,7 +128,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase {
$users = $q->execute(); $users = $q->execute();
$this->assertEqual($users->count(), 0); $this->assertEqual($users->count(), 0);
$this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id FROM entity WHERE entity.name = 'foo.bar' AND (entity.type = 0)"); $this->assertEqual($q->getQuery(), "SELECT e.id AS e__id FROM entity e WHERE e.name = 'foo.bar' AND (e.type = 0)");
} }
public function testOperatorWithSingleTrailingSpace() { public function testOperatorWithSingleTrailingSpace() {
$q = new Doctrine_Query(); $q = new Doctrine_Query();
...@@ -138,7 +138,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { ...@@ -138,7 +138,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase {
$users = $q->execute(); $users = $q->execute();
$this->assertEqual($users->count(), 0); $this->assertEqual($users->count(), 0);
$this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id FROM entity WHERE entity.name = 'foo.bar' AND (entity.type = 0)"); $this->assertEqual($q->getQuery(), "SELECT e.id AS e__id FROM entity e WHERE e.name = 'foo.bar' AND (e.type = 0)");
} }
public function testOperatorWithSingleTrailingSpace2() { public function testOperatorWithSingleTrailingSpace2() {
$q = new Doctrine_Query(); $q = new Doctrine_Query();
...@@ -148,7 +148,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { ...@@ -148,7 +148,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase {
$users = $q->execute(); $users = $q->execute();
$this->assertEqual($users->count(), 0); $this->assertEqual($users->count(), 0);
$this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id FROM entity WHERE entity.name = 'foo.bar' AND (entity.type = 0)"); $this->assertEqual($q->getQuery(), "SELECT e.id AS e__id FROM entity e WHERE e.name = 'foo.bar' AND (e.type = 0)");
} }
} }
......
...@@ -39,6 +39,7 @@ require_once('QueryConditionTestCase.php'); ...@@ -39,6 +39,7 @@ require_once('QueryConditionTestCase.php');
require_once('QueryComponentAliasTestCase.php'); require_once('QueryComponentAliasTestCase.php');
require_once('QuerySubqueryTestCase.php'); require_once('QuerySubqueryTestCase.php');
require_once('QuerySelectTestCase.php'); require_once('QuerySelectTestCase.php');
require_once('QueryShortAliasesTestCase.php');
require_once('QueryDeleteTestCase.php'); require_once('QueryDeleteTestCase.php');
require_once('QueryUpdateTestCase.php'); require_once('QueryUpdateTestCase.php');
...@@ -47,7 +48,9 @@ require_once('RelationTestCase.php'); ...@@ -47,7 +48,9 @@ require_once('RelationTestCase.php');
require_once('RelationManyToManyTestCase.php'); require_once('RelationManyToManyTestCase.php');
require_once('DBTestCase.php'); require_once('DbTestCase.php');
require_once('DbProfilerTestCase.php');
require_once('SchemaTestCase.php'); require_once('SchemaTestCase.php');
require_once('ImportTestCase.php'); require_once('ImportTestCase.php');
require_once('BooleanTestCase.php'); require_once('BooleanTestCase.php');
...@@ -64,8 +67,17 @@ print '<pre>'; ...@@ -64,8 +67,17 @@ print '<pre>';
$test = new GroupTest('Doctrine Framework Unit Tests'); $test = new GroupTest('Doctrine Framework Unit Tests');
//$test->addTestCase(new Doctrine_Db_Profiler_TestCase());
//$test->addTestCase(new Doctrine_DB_TestCase());
$test->addTestCase(new Doctrine_Query_MultiJoin_TestCase());
$test->addTestCase(new Doctrine_Record_TestCase()); $test->addTestCase(new Doctrine_Record_TestCase());
$test->addTestCase(new Doctrine_ConnectionTestCase());
$test->addTestCase(new Doctrine_DataDict_Pgsql_TestCase()); $test->addTestCase(new Doctrine_DataDict_Pgsql_TestCase());
$test->addTestCase(new Doctrine_Relation_ManyToMany_TestCase()); $test->addTestCase(new Doctrine_Relation_ManyToMany_TestCase());
...@@ -74,21 +86,17 @@ $test->addTestCase(new Doctrine_Relation_TestCase()); ...@@ -74,21 +86,17 @@ $test->addTestCase(new Doctrine_Relation_TestCase());
$test->addTestCase(new Doctrine_Record_State_TestCase()); $test->addTestCase(new Doctrine_Record_State_TestCase());
$test->addTestCase(new Doctrine_Import_TestCase()); //$test->addTestCase(new Doctrine_Import_TestCase());
$test->addTestCase(new Doctrine_SchemaTestCase()); $test->addTestCase(new Doctrine_SchemaTestCase());
$test->addTestCase(new Doctrine_ValidatorTestCase()); $test->addTestCase(new Doctrine_ValidatorTestCase());
$test->addTestCase(new Doctrine_Query_MultiJoin_TestCase());
$test->addTestCase(new Doctrine_EventListenerTestCase()); $test->addTestCase(new Doctrine_EventListenerTestCase());
$test->addTestCase(new Doctrine_Connection_Transaction_TestCase()); $test->addTestCase(new Doctrine_Connection_Transaction_TestCase());
$test->addTestCase(new Doctrine_ConnectionTestCase());
$test->addTestCase(new Doctrine_DB_TestCase());
$test->addTestCase(new Doctrine_AccessTestCase()); $test->addTestCase(new Doctrine_AccessTestCase());
...@@ -136,8 +144,12 @@ $test->addTestCase(new Doctrine_Query_ComponentAlias_TestCase()); ...@@ -136,8 +144,12 @@ $test->addTestCase(new Doctrine_Query_ComponentAlias_TestCase());
$test->addTestCase(new Doctrine_Query_Subquery_TestCase()); $test->addTestCase(new Doctrine_Query_Subquery_TestCase());
$test->addTestCase(new Doctrine_EnumTestCase());
$test->addTestCase(new Doctrine_Query_TestCase()); $test->addTestCase(new Doctrine_Query_TestCase());
$test->addTestCase(new Doctrine_Query_ShortAliases_TestCase());
$test->addTestCase(new Doctrine_Query_Where_TestCase()); $test->addTestCase(new Doctrine_Query_Where_TestCase());
$test->addTestCase(new Doctrine_Query_From_TestCase()); $test->addTestCase(new Doctrine_Query_From_TestCase());
...@@ -150,7 +162,7 @@ $test->addTestCase(new Doctrine_Query_Update_TestCase()); ...@@ -150,7 +162,7 @@ $test->addTestCase(new Doctrine_Query_Update_TestCase());
$test->addTestCase(new Doctrine_Query_Limit_TestCase()); $test->addTestCase(new Doctrine_Query_Limit_TestCase());
$test->addTestCase(new Doctrine_EnumTestCase());
//$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase());
//$test->addTestCase(new Doctrine_Cache_SqliteTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment