1
0
mirror of synced 2025-01-20 15:31:40 +03:00

Changed Doctrine_Query to accept a Doctrine_EntityManager instead of a Doctrine_Connection. Updated Doctrine_EntityManager. Updated test cases and included one new passing test case for SELECT generation. Fixed whitespace bug in DELETE and UPDATE statements.

This commit is contained in:
guilhermeblanco 2008-05-27 04:52:50 +00:00
parent bc236c59da
commit dfbc7c1cff
14 changed files with 365 additions and 363 deletions

View File

@ -246,7 +246,7 @@ class Doctrine_EntityManager
{ {
$query = new Doctrine_Query($this); $query = new Doctrine_Query($this);
if ( ! empty($dql)) { if ( ! empty($dql)) {
$query->parseQuery($dql); $query->setDql($dql);
} }
return $query; return $query;

View File

@ -37,9 +37,9 @@
class Doctrine_Query extends Doctrine_Query_Abstract class Doctrine_Query extends Doctrine_Query_Abstract
{ {
/** /**
* @var Doctrine_Connection The connection used by this query object. * @var Doctrine_EntityManager The entity manager used by this query object.
*/ */
protected $_connection; protected $_entityManager;
/** /**
* @var Doctrine_Hydrator The hydrator object used to hydrate query results. * @var Doctrine_Hydrator The hydrator object used to hydrate query results.
@ -93,15 +93,10 @@ class Doctrine_Query extends Doctrine_Query_Abstract
// End of Caching Stuff // End of Caching Stuff
public function __construct(Doctrine_Connection $conn = null, Doctrine_Hydrator_Abstract $hydrator = null) public function __construct(Doctrine_EntityManager $entityManager)
{ {
$this->setConnection($conn); $this->_entityManager = $entityManager;
$this->_hydrator = new Doctrine_Hydrator($entityManager);
if ($hydrator === null) {
$hydrator = new Doctrine_Hydrator(Doctrine_EntityManager::getManager());
}
$this->_hydrator = $hydrator;
$this->free(); $this->free();
} }
@ -120,29 +115,13 @@ class Doctrine_Query extends Doctrine_Query_Abstract
/** /**
* Retrieves the assocated Doctrine_Connection to this Doctrine_Query * Retrieves the assocated Doctrine_EntityManager to this Doctrine_Query
* *
* @return Doctrine_Connection * @return Doctrine_EntityManager
*/ */
public function getConnection() public function getEntityManager()
{ {
return $this->_connection; return $this->_entityManager;
}
/**
* Defines an assocated Doctrine_Connection to this Doctrine_Query
*
* @param Doctrine_Connection $conn A valid Doctrine_Connection
* @return void
*/
public function setConnection(Doctrine_Connection $conn = null)
{
if ($conn === null) {
$conn = Doctrine_EntityManager::getManager()->getConnection();
}
$this->_connection = $conn;
} }
@ -230,7 +209,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract
public function parse() public function parse()
{ {
if ($this->_state === self::STATE_DIRTY) { if ($this->_state === self::STATE_DIRTY) {
$parser = new Doctrine_Query_Parser($this->getDql()); $parser = new Doctrine_Query_Parser($this);
$this->_parserResult = $parser->parse(); $this->_parserResult = $parser->parse();
$this->_state = self::STATE_CLEAN; $this->_state = self::STATE_CLEAN;
} }
@ -313,7 +292,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract
protected function _execute2($params) protected function _execute2($params)
{ {
// If there is a CacheDriver associated to cache queries... // If there is a CacheDriver associated to cache queries...
if ($this->_queryCache || $this->_connection->getAttribute(Doctrine::ATTR_QUERY_CACHE)) { if ($this->_queryCache || $this->_entityManager->getConnection()->getAttribute(Doctrine::ATTR_QUERY_CACHE)) {
$queryCacheDriver = $this->getQueryCacheDriver(); $queryCacheDriver = $this->getQueryCacheDriver();
// Calculate hash for dql query. // Calculate hash for dql query.
@ -347,7 +326,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract
// Double the params if we are using limit-subquery algorithm // Double the params if we are using limit-subquery algorithm
// We always have an instance of Doctrine_Query_ParserResult on hands... // We always have an instance of Doctrine_Query_ParserResult on hands...
if ($this->_parserResult->isLimitSubqueryUsed() && if ($this->_parserResult->isLimitSubqueryUsed() &&
$this->_connection->getAttribute(Doctrine::ATTR_DRIVER_NAME) !== 'mysql') { $this->_entityManager->getConnection()->getAttribute(Doctrine::ATTR_DRIVER_NAME) !== 'mysql') {
$params = array_merge($params, $params); $params = array_merge($params, $params);
} }
@ -362,7 +341,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract
protected function _prepareParams(array $params) protected function _prepareParams(array $params)
{ {
// Convert boolean params // Convert boolean params
$params = $this->_connection->convertBooleans($params); $params = $this->_entityManager->getConnection()->convertBooleans($params);
// Convert enum params // Convert enum params
return $this->convertEnums($params); return $this->convertEnums($params);
@ -399,7 +378,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract
if ($this->_resultCache instanceof Doctrine_Cache_Interface) { if ($this->_resultCache instanceof Doctrine_Cache_Interface) {
return $this->_resultCache; return $this->_resultCache;
} else { } else {
return $this->_connection->getResultCacheDriver(); return $this->_entityManager->getConnection()->getResultCacheDriver();
} }
} }
@ -488,7 +467,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract
if ($this->_queryCache instanceof Doctrine_Cache_Interface) { if ($this->_queryCache instanceof Doctrine_Cache_Interface) {
return $this->_queryCache; return $this->_queryCache;
} else { } else {
return $this->_connection->getQueryCacheDriver(); return $this->_entityManager->getConnection()->getQueryCacheDriver();
} }
} }

View File

@ -111,10 +111,10 @@ class Doctrine_Query_Parser
* @param string $dql DQL to be parsed. * @param string $dql DQL to be parsed.
* @param Doctrine_Connection $connection The connection to use * @param Doctrine_Connection $connection The connection to use
*/ */
public function __construct($dql, Doctrine_Connection $connection = null) public function __construct(Doctrine_Query $query)
{ {
$this->_scanner = new Doctrine_Query_Scanner($dql); $this->_scanner = new Doctrine_Query_Scanner($query->getDql());
$this->_sqlBuilder = Doctrine_Query_SqlBuilder::fromConnection($connection); $this->_sqlBuilder = Doctrine_Query_SqlBuilder::fromConnection($query->getEntityManager());
$this->_keywordTable = new Doctrine_Query_Token(); $this->_keywordTable = new Doctrine_Query_Token();
$this->_parserResult = new Doctrine_Query_ParserResult( $this->_parserResult = new Doctrine_Query_ParserResult(

View File

@ -74,7 +74,7 @@ class Doctrine_Query_ParserResult extends Doctrine_Query_AbstractResult
* *
* @param array $queryFields Query fields. * @param array $queryFields Query fields.
*/ */
public function setQueryComponents(array $queryFields) public function setQueryFields(array $queryFields)
{ {
$this->_queryFields = $queryFields; $this->_queryFields = $queryFields;
} }

View File

@ -54,6 +54,6 @@ class Doctrine_Query_Production_DeleteStatement extends Doctrine_Query_Productio
// The 1=1 is needed to workaround the affected_rows in MySQL. // The 1=1 is needed to workaround the affected_rows in MySQL.
// Simple "DELETE FROM table_name" gives 0 affected rows. // Simple "DELETE FROM table_name" gives 0 affected rows.
return $this->_deleteClause->buildSql() . (($this->_whereClause !== null) return $this->_deleteClause->buildSql() . (($this->_whereClause !== null)
? $this->_whereClause->buildSql() : ' WHERE 1 = 1'); ? ' ' . $this->_whereClause->buildSql() : ' WHERE 1 = 1');
} }
} }

View File

@ -54,6 +54,6 @@ class Doctrine_Query_Production_UpdateStatement extends Doctrine_Query_Productio
// The 1=1 is needed to workaround the affected_rows in MySQL. // The 1=1 is needed to workaround the affected_rows in MySQL.
// Simple "UPDATE table_name SET column_name = value" gives 0 affected rows. // Simple "UPDATE table_name SET column_name = value" gives 0 affected rows.
return $this->_updateClause->buildSql() . (($this->_whereClause !== null) return $this->_updateClause->buildSql() . (($this->_whereClause !== null)
? $this->_whereClause->buildSql() : ' WHERE 1 = 1'); ? ' ' . $this->_whereClause->buildSql() : ' WHERE 1 = 1');
} }
} }

View File

@ -42,11 +42,9 @@ abstract class Doctrine_Query_SqlBuilder
protected $_connection; protected $_connection;
public static function fromConnection(Doctrine_Connection $connection = null) public static function fromConnection(Doctrine_EntityManager $entityManager)
{ {
if ($connection === null) { $connection = $entityManager->getConnection();
$connection = Doctrine_EntityManager::getManager()->getConnection();
}
$className = "Doctrine_Query_SqlBuilder_" . $connection->getDriverName(); $className = "Doctrine_Query_SqlBuilder_" . $connection->getDriverName();
$sqlBuilder = new $className(); $sqlBuilder = new $className();

View File

@ -37,25 +37,38 @@ require_once 'lib/DoctrineTestInit.php';
*/ */
class Orm_Query_DeleteSqlGenerationTest extends Doctrine_OrmTestCase class Orm_Query_DeleteSqlGenerationTest extends Doctrine_OrmTestCase
{ {
public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed)
{
try {
$entityManager = Doctrine_EntityManager::getManager();
$query = $entityManager->createQuery($dqlToBeTested);
parent::assertEquals($sqlToBeConfirmed, $query->getSql());
$query->free();
} catch (Doctrine_Exception $e) {
$this->fail($e->getMessage());
}
}
public function testWithoutWhere() public function testWithoutWhere()
{ {
$q = new Doctrine_Query();
// NO WhereClause // NO WhereClause
$q->setDql('DELETE CmsUser u'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE 1 = 1', $q->getSql()); 'DELETE CmsUser u',
$q->free(); 'DELETE FROM cms_user cu WHERE 1 = 1'
);
$q->setDql('DELETE FROM CmsUser u'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE 1 = 1', $q->getSql()); 'DELETE FROM CmsUser u',
$q->free(); 'DELETE FROM cms_user cu WHERE 1 = 1'
);
} }
public function testWithWhere() public function testWithWhere()
{ {
$q = new Doctrine_Query();
// "WHERE" ConditionalExpression // "WHERE" ConditionalExpression
// ConditionalExpression = ConditionalTerm {"OR" ConditionalTerm} // ConditionalExpression = ConditionalTerm {"OR" ConditionalTerm}
// ConditionalTerm = ConditionalFactor {"AND" ConditionalFactor} // ConditionalTerm = ConditionalFactor {"AND" ConditionalFactor}
@ -66,96 +79,66 @@ class Orm_Query_DeleteSqlGenerationTest extends Doctrine_OrmTestCase
// | InExpression | NullComparisonExpression) | ExistsExpression // | InExpression | NullComparisonExpression) | ExistsExpression
// If this one test fail, all others will fail too. That's the simplest case possible // If this one test fail, all others will fail too. That's the simplest case possible
$q->setDql('DELETE CmsUser u WHERE id = ?'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.id = ?', $q->getSql()); 'DELETE CmsUser u WHERE id = ?',
$q->free(); 'DELETE FROM cms_user cu WHERE cu.id = ?'
);
} }
public function testWithConditionalExpressions() public function testWithConditionalExpressions()
{ {
$q = new Doctrine_Query(); $this->assertSqlGeneration(
'DELETE CmsUser u WHERE u.username = ? OR u.name = ?',
$q->setDql('DELETE CmsUser u WHERE u.username = ? OR u.name = ?'); 'DELETE FROM cms_user cu WHERE cu.username = ? OR cu.name = ?'
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.username = ? OR cu.name = ?', $q->getSql());
$q->free();
$q->setDql('DELETE CmsUser u WHERE u.id = ? OR ( u.username = ? OR u.name = ? )');
$this->assertEquals(
'DELETE FROM cms_user cu WHERE cu.id = ? OR (cu.username = ? OR cu.name = ?)',
$q->getSql()
); );
$q->free();
$q->setDql('DELETE FROM CmsUser WHERE id = ?'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.id = ?', $q->getSql()); 'DELETE CmsUser u WHERE u.id = ? OR ( u.username = ? OR u.name = ? )',
$q->free(); 'DELETE FROM cms_user cu WHERE cu.id = ? OR (cu.username = ? OR cu.name = ?)'
} );
$this->assertSqlGeneration(
public function testInvalidSyntaxIsRejected() 'DELETE FROM CmsUser WHERE id = ?',
{ 'DELETE FROM cms_user cu WHERE cu.id = ?'
$q = new Doctrine_Query(); );
$invalidDql = 'FOOBAR CmsUser';
$q->setDql($invalidDql);
try {
$q->getSql();
$this->fail("Invalid DQL '$invalidDql' was not rejected.");
} catch (Doctrine_Exception $parseEx) {}
$q->free();
$invalidDql = 'DELETE FROM CmsUser.articles';
$q->setDql($invalidDql);
try {
$q->getSql();
$this->fail("Invalid DQL '$invalidDql' was not rejected.");
} catch (Doctrine_Exception $parseEx) {}
$q->free();
$invalidDql = 'DELETE FROM CmsUser cu WHERE cu.articles.id > ?';
$q->setDql($invalidDql);
try {
$q->getSql();
$this->fail("Invalid DQL '$invalidDql' was not rejected.");
} catch (Doctrine_Exception $parseEx) {}
$q->free();
} }
public function testParserIsCaseAgnostic() public function testParserIsCaseAgnostic()
{ {
$q = new Doctrine_Query(); $this->assertSqlGeneration(
$q->setDql('delete from CmsUser u where u.username = ?'); "delete from CmsUser u where u.username = ?",
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.username = ?', $q->getSql()); "DELETE FROM cms_user cu WHERE cu.username = ?"
);
} }
public function testWithConditionalTerms() public function testWithConditionalTerms()
{ {
$q = new Doctrine_Query(); $this->assertSqlGeneration(
"DELETE CmsUser u WHERE u.username = ? AND u.name = ?",
$q->setDql('DELETE CmsUser u WHERE u.username = ? AND u.name = ?'); "DELETE FROM cms_user cu WHERE cu.username = ? AND cu.name = ?"
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.username = ? AND cu.name = ?', $q->getSql()); );
$q->free();
} }
public function testWithConditionalFactors() public function testWithConditionalFactors()
{ {
$q = new Doctrine_Query(); $this->assertSqlGeneration(
"DELETE CmsUser u WHERE NOT id != ?",
"DELETE FROM cms_user cu WHERE NOT cu.id <> ?"
);
$q->setDql('DELETE CmsUser u WHERE NOT id != ?'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE NOT cu.id <> ?', $q->getSql()); "DELETE CmsUser u WHERE NOT ( id != ? )",
$q->free(); "DELETE FROM cms_user cu WHERE NOT (cu.id <> ?)"
);
$q->setDql('DELETE CmsUser u WHERE NOT ( id != ? )'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE NOT (cu.id <> ?)', $q->getSql()); "DELETE CmsUser u WHERE NOT ( id != ? AND username = ? )",
$q->free(); "DELETE FROM cms_user cu WHERE NOT (cu.id <> ? AND cu.username = ?)"
);
$q->setDql('DELETE CmsUser u WHERE NOT ( id != ? AND username = ? )');
$this->assertEquals('DELETE FROM cms_user cu WHERE NOT (cu.id <> ? AND cu.username = ?)', $q->getSql());
$q->free();
} }
@ -164,93 +147,96 @@ class Orm_Query_DeleteSqlGenerationTest extends Doctrine_OrmTestCase
public function testWithExprAndComparison() public function testWithExprAndComparison()
{ {
$q = new Doctrine_Query();
// id = ? was already tested (see testDeleteWithWhere()) // id = ? was already tested (see testDeleteWithWhere())
$this->assertSqlGeneration(
"DELETE CmsUser u WHERE id > ?",
"DELETE FROM cms_user cu WHERE cu.id > ?"
);
$q->setDql('DELETE CmsUser u WHERE id > ?'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.id > ?', $q->getSql()); "DELETE CmsUser u WHERE id >= ?",
$q->free(); "DELETE FROM cms_user cu WHERE cu.id >= ?"
);
$q->setDql('DELETE CmsUser u WHERE id >= ?'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.id >= ?', $q->getSql()); "DELETE CmsUser u WHERE id < ?",
$q->free(); "DELETE FROM cms_user cu WHERE cu.id < ?"
);
$q->setDql('DELETE CmsUser u WHERE id < ?'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.id < ?', $q->getSql()); "DELETE CmsUser u WHERE id <= ?",
$q->free(); "DELETE FROM cms_user cu WHERE cu.id <= ?"
);
$q->setDql('DELETE CmsUser u WHERE id <= ?'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.id <= ?', $q->getSql()); "DELETE CmsUser u WHERE id <> ?",
$q->free(); "DELETE FROM cms_user cu WHERE cu.id <> ?"
);
$q->setDql('DELETE CmsUser u WHERE id <> ?'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.id <> ?', $q->getSql()); "DELETE CmsUser u WHERE id != ?",
$q->free(); "DELETE FROM cms_user cu WHERE cu.id <> ?"
);
$q->setDql('DELETE CmsUser u WHERE id != ?');
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.id <> ?', $q->getSql());
$q->free();
} }
public function testWithExprAndBetween() public function testWithExprAndBetween()
{ {
$q = new Doctrine_Query();
// "WHERE" Expression BetweenExpression // "WHERE" Expression BetweenExpression
$q->setDql('DELETE CmsUser u WHERE u.id NOT BETWEEN ? AND ?'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.id NOT BETWEEN ? AND ?', $q->getSql()); "DELETE CmsUser u WHERE u.id NOT BETWEEN ? AND ?",
$q->free(); "DELETE FROM cms_user cu WHERE cu.id NOT BETWEEN ? AND ?"
);
$q->setDql('DELETE CmsUser u WHERE u.id BETWEEN ? AND ? AND u.username != ?'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.id BETWEEN ? AND ? AND cu.username <> ?', $q->getSql()); "DELETE CmsUser u WHERE u.id BETWEEN ? AND ? AND u.username != ?",
$q->free(); "DELETE FROM cms_user cu WHERE cu.id BETWEEN ? AND ? AND cu.username <> ?"
);
} }
public function testWithExprAndLike() public function testWithExprAndLike()
{ {
$q = new Doctrine_Query();
// "WHERE" Expression LikeExpression // "WHERE" Expression LikeExpression
$q->setDql('DELETE CmsUser u WHERE u.username NOT LIKE ?'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.username NOT LIKE ?', $q->getSql()); 'DELETE CmsUser u WHERE u.username NOT LIKE ?',
$q->free(); 'DELETE FROM cms_user cu WHERE cu.username NOT LIKE ?'
);
$q->setDql("DELETE CmsUser u WHERE u.username LIKE ? ESCAPE '\\'"); $this->assertSqlGeneration(
$this->assertEquals("DELETE FROM cms_user cu WHERE cu.username LIKE ? ESCAPE '\\'", $q->getSql()); "DELETE CmsUser u WHERE u.username LIKE ? ESCAPE '\\'",
$q->free(); "DELETE FROM cms_user cu WHERE cu.username LIKE ? ESCAPE '\\'"
);
} }
public function testWithExprAndIn() public function testWithExprAndIn()
{ {
$q = new Doctrine_Query();
// "WHERE" Expression InExpression // "WHERE" Expression InExpression
$q->setDql('DELETE CmsUser u WHERE u.id IN ( ?, ?, ?, ? )'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.id IN (?, ?, ?, ?)', $q->getSql()); 'DELETE CmsUser u WHERE u.id IN ( ?, ?, ?, ? )',
$q->free(); 'DELETE FROM cms_user cu WHERE cu.id IN (?, ?, ?, ?)'
);
$q->setDql('DELETE CmsUser u WHERE u.id NOT IN ( ?, ? )'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.id NOT IN (?, ?)', $q->getSql()); 'DELETE CmsUser u WHERE u.id NOT IN ( ?, ? )',
$q->free(); 'DELETE FROM cms_user cu WHERE cu.id NOT IN (?, ?)'
);
} }
public function testWithExprAndNull() public function testWithExprAndNull()
{ {
$q = new Doctrine_Query();
// "WHERE" Expression NullComparisonExpression // "WHERE" Expression NullComparisonExpression
$q->setDql('DELETE CmsUser u WHERE u.name IS NULL'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.name IS NULL', $q->getSql()); 'DELETE CmsUser u WHERE u.name IS NULL',
$q->free(); 'DELETE FROM cms_user cu WHERE cu.name IS NULL'
);
$q->setDql('DELETE CmsUser u WHERE u.name IS NOT NULL'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.name IS NOT NULL', $q->getSql()); 'DELETE CmsUser u WHERE u.name IS NOT NULL',
$q->free(); 'DELETE FROM cms_user cu WHERE cu.name IS NOT NULL'
);
} }
@ -258,15 +244,15 @@ class Orm_Query_DeleteSqlGenerationTest extends Doctrine_OrmTestCase
public function testWithPrimaryAsAtom() public function testWithPrimaryAsAtom()
{ {
$q = new Doctrine_Query();
// Atom = string | integer | float | boolean | input_parameter // Atom = string | integer | float | boolean | input_parameter
$q->setDql('DELETE CmsUser u WHERE 1 = 1'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE 1 = 1', $q->getSql()); 'DELETE CmsUser u WHERE 1 = 1',
$q->free(); 'DELETE FROM cms_user cu WHERE 1 = 1'
);
$q->setDql('DELETE CmsUser u WHERE ? = 1'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE ? = 1', $q->getSql()); 'DELETE CmsUser u WHERE ? = 1',
$q->free(); 'DELETE FROM cms_user cu WHERE ? = 1'
);
} }
} }

View File

@ -34,216 +34,215 @@
*/ */
class Orm_Query_DqlGenerationTest extends Doctrine_OrmTestCase class Orm_Query_DqlGenerationTest extends Doctrine_OrmTestCase
{ {
const QueryClass = 'Doctrine_Query'; protected function createQuery()
{
$entityManager = Doctrine_EntityManager::getManager();
return $entityManager->createQuery();
}
public function testSelect() public function testSelect()
{ {
$class = self::QueryClass; $query = $this->createQuery();
$q = new $class();
// select and from // select and from
$q->setDql('FROM User u'); $query->setDql('FROM User u');
$this->assertEquals('FROM User u', $q->getDql()); // Internally we use SELECT * FROM User u to process the DQL $this->assertEquals('FROM User u', $query->getDql()); // Internally we use SELECT * FROM User u to process the DQL
$q->free(); $query->free();
$q->select()->from('User u'); $query->select()->from('User u');
$this->assertEquals('SELECT * FROM User u', $q->getDql()); $this->assertEquals('SELECT * FROM User u', $query->getDql());
$q->free(); $query->free();
$q->select('u.*')->from('User u'); $query->select('u.*')->from('User u');
$this->assertEquals('SELECT u.* FROM User u', $q->getDql()); $this->assertEquals('SELECT u.* FROM User u', $query->getDql());
$q->free(); $query->free();
$q->select('u.id')->from('User u'); $query->select('u.id')->from('User u');
$this->assertEquals('SELECT u.id FROM User u', $q->getDql()); $this->assertEquals('SELECT u.id FROM User u', $query->getDql());
$q->free(); $query->free();
$q->select('u.id, u.name')->from('User u'); $query->select('u.id, u.name')->from('User u');
$this->assertEquals('SELECT u.id, u.name FROM User u', $q->getDql()); $this->assertEquals('SELECT u.id, u.name FROM User u', $query->getDql());
$q->free(); $query->free();
$q->select('u.name AS myCustomName')->from('User u'); $query->select('u.name AS myCustomName')->from('User u');
$this->assertEquals('SELECT u.name AS myCustomName FROM User u', $q->getDql()); $this->assertEquals('SELECT u.name AS myCustomName FROM User u', $query->getDql());
$q->free(); $query->free();
$q->select('u.id')->select('u.name')->from('User u'); $query->select('u.id')->select('u.name')->from('User u');
$this->assertEquals('SELECT u.id, u.name FROM User u', $q->getDql()); $this->assertEquals('SELECT u.id, u.name FROM User u', $query->getDql());
$q->free(); $query->free();
} }
public function testSelectDistinct() public function testSelectDistinct()
{ {
$class = self::QueryClass; $query = $this->createQuery();
$q = new $class();
$q->select()->distinct()->from('User u'); $query->select()->distinct()->from('User u');
$this->assertEquals('SELECT DISTINCT * FROM User u', $q->getDql()); $this->assertEquals('SELECT DISTINCT * FROM User u', $query->getDql());
$q->free(); $query->free();
$q->select('u.name')->distinct(false)->from('User u'); $query->select('u.name')->distinct(false)->from('User u');
$this->assertEquals('SELECT u.name FROM User u', $q->getDql()); $this->assertEquals('SELECT u.name FROM User u', $query->getDql());
$q->free(); $query->free();
$q->select()->distinct(false)->from('User u'); $query->select()->distinct(false)->from('User u');
$this->assertEquals('SELECT * FROM User u', $q->getDql()); $this->assertEquals('SELECT * FROM User u', $query->getDql());
$q->free(); $query->free();
$q->select('u.name')->distinct()->from('User u'); $query->select('u.name')->distinct()->from('User u');
$this->assertEquals('SELECT DISTINCT u.name FROM User u', $q->getDql()); $this->assertEquals('SELECT DISTINCT u.name FROM User u', $query->getDql());
$q->free(); $query->free();
$q->select('u.name, u.email')->distinct()->from('User u'); $query->select('u.name, u.email')->distinct()->from('User u');
$this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $q->getDql()); $this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $query->getDql());
$q->free(); $query->free();
$q->select('u.name')->select('u.email')->distinct()->from('User u'); $query->select('u.name')->select('u.email')->distinct()->from('User u');
$this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $q->getDql()); $this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $query->getDql());
$q->free(); $query->free();
$q->select('DISTINCT u.name')->from('User u'); $query->select('DISTINCT u.name')->from('User u');
$this->assertEquals('SELECT DISTINCT u.name FROM User u', $q->getDql()); $this->assertEquals('SELECT DISTINCT u.name FROM User u', $query->getDql());
$q->free(); $query->free();
$q->select('DISTINCT u.name, u.email')->from('User u'); $query->select('DISTINCT u.name, u.email')->from('User u');
$this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $q->getDql()); $this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $query->getDql());
$q->free(); $query->free();
$q->select('DISTINCT u.name')->select('u.email')->from('User u'); $query->select('DISTINCT u.name')->select('u.email')->from('User u');
$this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $q->getDql()); $this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $query->getDql());
$q->free(); $query->free();
} }
public function testSelectJoin() public function testSelectJoin()
{ {
$class = self::QueryClass; $query = $this->createQuery();
$q = new $class();
$q->select('u.*')->from('User u')->join('u.Group g')->where('g.id = ?', 1); $query->select('u.*')->from('User u')->join('u.Group g')->where('g.id = ?', 1);
$this->assertEquals('SELECT u.* FROM User u INNER JOIN u.Group g WHERE g.id = ?', $q->getDql()); $this->assertEquals('SELECT u.* FROM User u INNER JOIN u.Group g WHERE g.id = ?', $query->getDql());
$this->assertEquals(array(1), $q->getParams()); $this->assertEquals(array(1), $query->getParams());
$q->free(); $query->free();
$q->select('u.*')->from('User u')->innerJoin('u.Group g')->where('g.id = ?', 1); $query->select('u.*')->from('User u')->innerJoin('u.Group g')->where('g.id = ?', 1);
$this->assertEquals('SELECT u.* FROM User u INNER JOIN u.Group g WHERE g.id = ?', $q->getDql()); $this->assertEquals('SELECT u.* FROM User u INNER JOIN u.Group g WHERE g.id = ?', $query->getDql());
$this->assertEquals(array(1), $q->getParams()); $this->assertEquals(array(1), $query->getParams());
$q->free(); $query->free();
$q->select('u.*')->from('User u')->leftJoin('u.Group g')->where('g.id IS NULL'); $query->select('u.*')->from('User u')->leftJoin('u.Group g')->where('g.id IS NULL');
$this->assertEquals('SELECT u.* FROM User u LEFT JOIN u.Group g WHERE g.id IS NULL', $q->getDql()); $this->assertEquals('SELECT u.* FROM User u LEFT JOIN u.Group g WHERE g.id IS NULL', $query->getDql());
$q->free(); $query->free();
$q->select('u.*')->from('User u')->leftJoin('u.UserGroup ug')->leftJoin('ug.Group g')->where('g.name = ?', 'admin'); $query->select('u.*')->from('User u')->leftJoin('u.UserGroup ug')->leftJoin('ug.Group g')->where('g.name = ?', 'admin');
$this->assertEquals('SELECT u.* FROM User u LEFT JOIN u.UserGroup ug LEFT JOIN ug.Group g WHERE g.name = ?', $q->getDql()); $this->assertEquals('SELECT u.* FROM User u LEFT JOIN u.UserGroup ug LEFT JOIN ug.Group g WHERE g.name = ?', $query->getDql());
$q->free(); $query->free();
} }
public function testSelectWhere() public function testSelectWhere()
{ {
$class = self::QueryClass; $query = $this->createQuery();
$q = new $class();
$q->select('u.name')->from('User u')->where('u.id = ?', 1); $query->select('u.name')->from('User u')->where('u.id = ?', 1);
$this->assertEquals('SELECT u.name FROM User u WHERE u.id = ?', $q->getDql()); $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ?', $query->getDql());
$this->assertEquals(array(1), $q->getParams()); $this->assertEquals(array(1), $query->getParams());
$q->free(); $query->free();
$q->select('u.name')->from('User u')->where('u.id = ? AND u.type != ?', array(1, 'admin')); $query->select('u.name')->from('User u')->where('u.id = ? AND u.type != ?', array(1, 'admin'));
$this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? AND u.type != ?', $q->getDql()); $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? AND u.type != ?', $query->getDql());
$this->assertEquals(array(1, 'admin'), $q->getParams()); $this->assertEquals(array(1, 'admin'), $query->getParams());
$q->free(); $query->free();
$q->select('u.name')->from('User u')->where('u.id = ?', 1)->andWhere('u.type != ?', 'admin'); $query->select('u.name')->from('User u')->where('u.id = ?', 1)->andWhere('u.type != ?', 'admin');
$this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? AND u.type != ?', $q->getDql()); $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? AND u.type != ?', $query->getDql());
$this->assertEquals(array(1, 'admin'), $q->getParams()); $this->assertEquals(array(1, 'admin'), $query->getParams());
$q->free(); $query->free();
$q->select('u.name')->from('User u')->where('( u.id = ?', 1)->andWhere('u.type != ? )', 'admin'); $query->select('u.name')->from('User u')->where('( u.id = ?', 1)->andWhere('u.type != ? )', 'admin');
$this->assertEquals('SELECT u.name FROM User u WHERE ( u.id = ? AND u.type != ? )', $q->getDql()); $this->assertEquals('SELECT u.name FROM User u WHERE ( u.id = ? AND u.type != ? )', $query->getDql());
$this->assertEquals(array(1, 'admin'), $q->getParams()); $this->assertEquals(array(1, 'admin'), $query->getParams());
$q->free(); $query->free();
$q->select('u.name')->from('User u')->where('u.id = ? OR u.type != ?', array(1, 'admin')); $query->select('u.name')->from('User u')->where('u.id = ? OR u.type != ?', array(1, 'admin'));
$this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? OR u.type != ?', $q->getDql()); $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? OR u.type != ?', $query->getDql());
$this->assertEquals(array(1, 'admin'), $q->getParams()); $this->assertEquals(array(1, 'admin'), $query->getParams());
$q->free(); $query->free();
$q->select('u.name')->from('User u')->where('u.id = ?', 1)->orWhere('u.type != ?', 'admin'); $query->select('u.name')->from('User u')->where('u.id = ?', 1)->orWhere('u.type != ?', 'admin');
$this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? OR u.type != ?', $q->getDql()); $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? OR u.type != ?', $query->getDql());
$this->assertEquals(array(1, 'admin'), $q->getParams()); $this->assertEquals(array(1, 'admin'), $query->getParams());
$q->free(); $query->free();
$q->select('u.name')->from('User u')->andwhere('u.id = ?', 1)->andWhere('u.type != ?', 'admin')->orWhere('u.email = ?', 'admin@localhost'); $query->select('u.name')->from('User u')->andwhere('u.id = ?', 1)->andWhere('u.type != ?', 'admin')->orWhere('u.email = ?', 'admin@localhost');
$this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? AND u.type != ? OR u.email = ?', $q->getDql()); $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? AND u.type != ? OR u.email = ?', $query->getDql());
$this->assertEquals(array(1, 'admin', 'admin@localhost'), $q->getParams()); $this->assertEquals(array(1, 'admin', 'admin@localhost'), $query->getParams());
$q->free(); $query->free();
} }
public function testSelectWhereIn() public function testSelectWhereIn()
{ {
$class = self::QueryClass; $query = $this->createQuery();
$q = new $class();
$q->select('u.name')->from('User u')->whereIn('u.id', array(1, 2, 3, 4, 5)); $query->select('u.name')->from('User u')->whereIn('u.id', array(1, 2, 3, 4, 5));
$this->assertEquals('SELECT u.name FROM User u WHERE u.id IN (?, ?, ?, ?, ?)', $q->getDql()); $this->assertEquals('SELECT u.name FROM User u WHERE u.id IN (?, ?, ?, ?, ?)', $query->getDql());
$this->assertEquals(array(1, 2, 3, 4, 5), $q->getParams()); $this->assertEquals(array(1, 2, 3, 4, 5), $query->getParams());
$q->free(); $query->free();
$q->select('u.name')->from('User u')->whereNotIn('u.id', array(1, 2, 3)); $query->select('u.name')->from('User u')->whereNotIn('u.id', array(1, 2, 3));
$this->assertEquals('SELECT u.name FROM User u WHERE u.id NOT IN (?, ?, ?)', $q->getDql()); $this->assertEquals('SELECT u.name FROM User u WHERE u.id NOT IN (?, ?, ?)', $query->getDql());
$this->assertEquals(array(1, 2, 3), $q->getParams()); $this->assertEquals(array(1, 2, 3), $query->getParams());
$q->free(); $query->free();
$q->select('u.name')->from('User u')->where('u.type = ?', 'admin')->andWhereIn('u.id', array(1, 2)); $query->select('u.name')->from('User u')->where('u.type = ?', 'admin')->andWhereIn('u.id', array(1, 2));
$this->assertEquals('SELECT u.name FROM User u WHERE u.type = ? AND u.id IN (?, ?)', $q->getDql()); $this->assertEquals('SELECT u.name FROM User u WHERE u.type = ? AND u.id IN (?, ?)', $query->getDql());
$this->assertEquals(array('admin', 1, 2), $q->getParams()); $this->assertEquals(array('admin', 1, 2), $query->getParams());
$q->free(); $query->free();
$q->select('u.name')->from('User u')->where('u.type = ?', 'admin')->andWhereNotIn('u.id', array(1, 2)); $query->select('u.name')->from('User u')->where('u.type = ?', 'admin')->andWhereNotIn('u.id', array(1, 2));
$this->assertEquals('SELECT u.name FROM User u WHERE u.type = ? AND u.id NOT IN (?, ?)', $q->getDql()); $this->assertEquals('SELECT u.name FROM User u WHERE u.type = ? AND u.id NOT IN (?, ?)', $query->getDql());
$this->assertEquals(array('admin', 1, 2), $q->getParams()); $this->assertEquals(array('admin', 1, 2), $query->getParams());
$q->free(); $query->free();
$q->select('u.name')->from('User u')->whereIn('u.type', array('admin', 'moderator'))->andWhereNotIn('u.id', array(1, 2, 3, 4)); $query->select('u.name')->from('User u')->whereIn('u.type', array('admin', 'moderator'))->andWhereNotIn('u.id', array(1, 2, 3, 4));
$this->assertEquals('SELECT u.name FROM User u WHERE u.type IN (?, ?) AND u.id NOT IN (?, ?, ?, ?)', $q->getDql()); $this->assertEquals('SELECT u.name FROM User u WHERE u.type IN (?, ?) AND u.id NOT IN (?, ?, ?, ?)', $query->getDql());
$this->assertEquals(array('admin', 'moderator', 1, 2, 3, 4), $q->getParams()); $this->assertEquals(array('admin', 'moderator', 1, 2, 3, 4), $query->getParams());
$q->free(); $query->free();
$q->select('u.name')->from('User u')->whereIn('u.type', array('admin', 'moderator'))->orWhereIn('u.id', array(1, 2, 3, 4)); $query->select('u.name')->from('User u')->whereIn('u.type', array('admin', 'moderator'))->orWhereIn('u.id', array(1, 2, 3, 4));
$this->assertEquals('SELECT u.name FROM User u WHERE u.type IN (?, ?) OR u.id IN (?, ?, ?, ?)', $q->getDql()); $this->assertEquals('SELECT u.name FROM User u WHERE u.type IN (?, ?) OR u.id IN (?, ?, ?, ?)', $query->getDql());
$this->assertEquals(array('admin', 'moderator', 1, 2, 3, 4), $q->getParams()); $this->assertEquals(array('admin', 'moderator', 1, 2, 3, 4), $query->getParams());
$q->free(); $query->free();
$q->select('u.name')->from('User u')->whereIn('u.type', array('admin', 'moderator'))->andWhereNotIn('u.id', array(1, 2))->orWhereNotIn('u.type', array('admin', 'moderator'))->andWhereNotIn('u.email', array('user@localhost', 'guest@localhost')); $query->select('u.name')->from('User u')->whereIn('u.type', array('admin', 'moderator'))->andWhereNotIn('u.id', array(1, 2))->orWhereNotIn('u.type', array('admin', 'moderator'))->andWhereNotIn('u.email', array('user@localhost', 'guest@localhost'));
$this->assertEquals('SELECT u.name FROM User u WHERE u.type IN (?, ?) AND u.id NOT IN (?, ?) OR u.type NOT IN (?, ?) AND u.email NOT IN (?, ?)', $q->getDql()); $this->assertEquals('SELECT u.name FROM User u WHERE u.type IN (?, ?) AND u.id NOT IN (?, ?) OR u.type NOT IN (?, ?) AND u.email NOT IN (?, ?)', $query->getDql());
$this->assertEquals(array('admin', 'moderator', 1, 2, 'admin', 'moderator', 'user@localhost', 'guest@localhost'), $q->getParams()); $this->assertEquals(array('admin', 'moderator', 1, 2, 'admin', 'moderator', 'user@localhost', 'guest@localhost'), $query->getParams());
$q->free(); $query->free();
} }
public function testDelete() public function testDelete()
{ {
$class = self::QueryClass; $query = $this->createQuery();
$q = new $class();
$q->setDql('DELETE CmsUser u'); $query->setDql('DELETE CmsUser u');
$this->assertEquals('DELETE CmsUser u', $q->getDql()); $this->assertEquals('DELETE CmsUser u', $query->getDql());
$q->free(); $query->free();
$q->delete()->from('CmsUser u'); $query->delete()->from('CmsUser u');
$this->assertEquals('DELETE FROM CmsUser u', $q->getDql()); $this->assertEquals('DELETE FROM CmsUser u', $query->getDql());
$q->free(); $query->free();
$q->delete()->from('CmsUser u')->where('u.id = ?', 1); $query->delete()->from('CmsUser u')->where('u.id = ?', 1);
$this->assertEquals('DELETE FROM CmsUser u WHERE u.id = ?', $q->getDql()); $this->assertEquals('DELETE FROM CmsUser u WHERE u.id = ?', $query->getDql());
$q->free(); $query->free();
$q->delete()->from('CmsUser u')->where('u.username = ?', 'gblanco')->orWhere('u.name = ?', 'Guilherme'); $query->delete()->from('CmsUser u')->where('u.username = ?', 'gblanco')->orWhere('u.name = ?', 'Guilherme');
$this->assertEquals('DELETE FROM CmsUser u WHERE u.username = ? OR u.name = ?', $q->getDql()); $this->assertEquals('DELETE FROM CmsUser u WHERE u.username = ? OR u.name = ?', $query->getDql());
$q->free(); $query->free();
} }
} }

View File

@ -39,8 +39,8 @@ class Orm_Query_IdentifierRecognitionTest extends Doctrine_OrmTestCase
public function testSingleAliasDeclarationIsSupported() public function testSingleAliasDeclarationIsSupported()
{ {
$query = new Doctrine_Query; $entityManager = Doctrine_EntityManager::getManager();
$query->setDql('SELECT u.* FROM CmsUser u'); $query = $entityManager->createQuery('SELECT u.* FROM CmsUser u');
$parserResult = $query->parse(); $parserResult = $query->parse();
$decl = $parserResult->getQueryComponent('u'); $decl = $parserResult->getQueryComponent('u');
@ -54,8 +54,8 @@ class Orm_Query_IdentifierRecognitionTest extends Doctrine_OrmTestCase
public function testSingleAliasDeclarationWithIndexByIsSupported() public function testSingleAliasDeclarationWithIndexByIsSupported()
{ {
$query = new Doctrine_Query; $entityManager = Doctrine_EntityManager::getManager();
$query->setDql('SELECT u.* FROM CmsUser u INDEX BY id'); $query = $entityManager->createQuery('SELECT u.* FROM CmsUser u INDEX BY id');
$parserResult = $query->parse(); $parserResult = $query->parse();
$decl = $parserResult->getQueryComponent('u'); $decl = $parserResult->getQueryComponent('u');
@ -69,8 +69,8 @@ class Orm_Query_IdentifierRecognitionTest extends Doctrine_OrmTestCase
public function testQueryParserSupportsMultipleAliasDeclarations() public function testQueryParserSupportsMultipleAliasDeclarations()
{ {
$query = new Doctrine_Query; $entityManager = Doctrine_EntityManager::getManager();
$query->setDql('SELECT u.* FROM CmsUser u INDEX BY id LEFT JOIN u.phonenumbers p'); $query = $entityManager->createQuery('SELECT u.* FROM CmsUser u INDEX BY id LEFT JOIN u.phonenumbers p');
$parserResult = $query->parse(); $parserResult = $query->parse();
$decl = $parserResult->getQueryComponent('u'); $decl = $parserResult->getQueryComponent('u');
@ -93,8 +93,8 @@ class Orm_Query_IdentifierRecognitionTest extends Doctrine_OrmTestCase
public function testQueryParserSupportsMultipleAliasDeclarationsWithIndexBy() public function testQueryParserSupportsMultipleAliasDeclarationsWithIndexBy()
{ {
$query = new Doctrine_Query; $entityManager = Doctrine_EntityManager::getManager();
$query->setDql('SELECT u.* FROM CmsUser u INDEX BY id LEFT JOIN u.articles a INNER JOIN u.phonenumbers pn INDEX BY phonenumber'); $query = $entityManager->createQuery('SELECT u.* FROM CmsUser u INDEX BY id LEFT JOIN u.articles a INNER JOIN u.phonenumbers pn INDEX BY phonenumber');
$parserResult = $query->parse(); $parserResult = $query->parse();
$decl = $parserResult->getQueryComponent('u'); $decl = $parserResult->getQueryComponent('u');

View File

@ -38,11 +38,11 @@ require_once 'lib/DoctrineTestInit.php';
*/ */
class Orm_Query_LanguageRecognitionTest extends Doctrine_OrmTestCase class Orm_Query_LanguageRecognitionTest extends Doctrine_OrmTestCase
{ {
public function assertValidDql($dql, $method = '') public function assertValidDql($dql)
{ {
try { try {
$query = new Doctrine_Query; $entityManager = Doctrine_EntityManager::getManager();
$query->setDql($dql); $query = $entityManager->createQuery($dql);
$parserResult = $query->parse(); $parserResult = $query->parse();
} catch (Doctrine_Exception $e) { } catch (Doctrine_Exception $e) {
$this->fail($e->getMessage()); $this->fail($e->getMessage());
@ -52,7 +52,8 @@ class Orm_Query_LanguageRecognitionTest extends Doctrine_OrmTestCase
public function assertInvalidDql($dql) public function assertInvalidDql($dql)
{ {
try { try {
$query = new Doctrine_Query; $entityManager = Doctrine_EntityManager::getManager();
$query = $entityManager->createQuery($dql);
$query->setDql($dql); $query->setDql($dql);
$parserResult = $query->parse(); $parserResult = $query->parse();
@ -382,4 +383,14 @@ class Orm_Query_LanguageRecognitionTest extends Doctrine_OrmTestCase
$this->assertValidDql("SELECT u.id FROM CmsUser u WHERE u.name LIKE 'z|%' ESCAPE '|'"); $this->assertValidDql("SELECT u.id FROM CmsUser u WHERE u.name LIKE 'z|%' ESCAPE '|'");
} }
public function testInvalidSyntaxIsRejected()
{
$this->assertInvalidDql("FOOBAR CmsUser");
$this->assertInvalidDql("DELETE FROM CmsUser.articles");
$this->assertInvalidDql("DELETE FROM CmsUser cu WHERE cu.articles.id > ?");
}
} }

View File

@ -37,25 +37,38 @@ require_once 'lib/DoctrineTestInit.php';
*/ */
class Orm_Query_SelectSqlGenerationTest extends Doctrine_OrmTestCase class Orm_Query_SelectSqlGenerationTest extends Doctrine_OrmTestCase
{ {
public function testWithoutWhere() public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed)
{ {
$q = new Doctrine_Query(); try {
$entityManager = Doctrine_EntityManager::getManager();
$query = $entityManager->createQuery($dqlToBeTested);
// NO WhereClause parent::assertEquals($sqlToBeConfirmed, $query->getSql());
$q->setDql('SELECT u.id FROM CmsUser u');
$this->assertEquals('SELECT cu.id AS cu__id FROM cms_user cu WHERE 1 = 1', $q->getSql());
$q->free();
$q->setDql('SELECT u.* FROM CmsUser u'); $query->free();
$this->assertEquals('SELECT cu.id AS cu__id, cu.status AS cu__status, cu.username AS cu__username, cu.name AS cu__name FROM cms_user cu WHERE 1 = 1', $q->getSql()); } catch (Doctrine_Exception $e) {
$q->free(); $this->fail($e->getMessage());
}
} }
/*
public function testWithoutWhere()
{
// NO WhereClause
$this->assertSqlGeneration(
'SELECT u.id FROM CmsUser u',
'SELECT cu.id AS cu__id FROM cms_user cu WHERE 1 = 1'
);
$this->assertSqlGeneration(
'SELECT u.* FROM CmsUser u',
'SELECT cu.id AS cu__id, cu.status AS cu__status, cu.username AS cu__username, cu.name AS cu__name FROM cms_user cu WHERE 1 = 1'
);
}
public function testWithWhere() public function testWithWhere()
{ {
$q = new Doctrine_Query();
// "WHERE" ConditionalExpression // "WHERE" ConditionalExpression
// ConditionalExpression = ConditionalTerm {"OR" ConditionalTerm} // ConditionalExpression = ConditionalTerm {"OR" ConditionalTerm}
// ConditionalTerm = ConditionalFactor {"AND" ConditionalFactor} // ConditionalTerm = ConditionalFactor {"AND" ConditionalFactor}
@ -66,9 +79,10 @@ class Orm_Query_SelectSqlGenerationTest extends Doctrine_OrmTestCase
// | InExpression | NullComparisonExpression) | ExistsExpression // | InExpression | NullComparisonExpression) | ExistsExpression
// If this one test fail, all others will fail too. That's the simplest case possible // If this one test fail, all others will fail too. That's the simplest case possible
$q->setDql('SELECT u.* FROM CmsUser u WHERE id = ?'); $this->assertSqlGeneration(
$this->assertEquals('DELETE FROM cms_user cu WHERE cu.id = ?', $q->getSql()); 'SELECT u.* FROM CmsUser u WHERE id = ?',
$q->free(); 'SELECT cu.id AS cu__id, cu.status AS cu__status, cu.username AS cu__username, cu.name AS cu__name FROM cms_user cu WHERE cu.id = ?'
);
} }
*/
} }

View File

@ -37,17 +37,32 @@
*/ */
class Orm_Query_UpdateSqlGenerationTest extends Doctrine_OrmTestCase class Orm_Query_UpdateSqlGenerationTest extends Doctrine_OrmTestCase
{ {
public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed)
{
try {
$entityManager = Doctrine_EntityManager::getManager();
$query = $entityManager->createQuery($dqlToBeTested);
parent::assertEquals($sqlToBeConfirmed, $query->getSql());
$query->free();
} catch (Doctrine_Exception $e) {
$this->fail($e->getMessage());
}
}
public function testWithoutWhere() public function testWithoutWhere()
{ {
$q = new Doctrine_Query();
// NO WhereClause // NO WhereClause
$q->setDql('UPDATE CmsUser u SET name = ?'); $this->assertSqlGeneration(
$this->assertEquals('UPDATE cms_user cu SET cu.name = ? WHERE 1 = 1', $q->getSql()); 'UPDATE CmsUser u SET name = ?',
$q->free(); 'UPDATE cms_user cu SET cu.name = ? WHERE 1 = 1'
);
$q->setDql('UPDATE CmsUser u SET name = ?, username = ?'); $this->assertSqlGeneration(
$this->assertEquals('UPDATE cms_user cu SET cu.name = ?, cu.username = ? WHERE 1 = 1', $q->getSql()); 'UPDATE CmsUser u SET name = ?, username = ?',
$q->free(); 'UPDATE cms_user cu SET cu.name = ?, cu.username = ? WHERE 1 = 1'
);
} }
} }