diff --git a/lib/Doctrine/Hydrate.php b/lib/Doctrine/Hydrate.php index 512b31dc5..f1619cbd6 100644 --- a/lib/Doctrine/Hydrate.php +++ b/lib/Doctrine/Hydrate.php @@ -34,6 +34,30 @@ Doctrine::autoload('Doctrine_Access'); */ abstract class Doctrine_Hydrate extends Doctrine_Access { + /** + * QUERY TYPE CONSTANTS + */ + + /** + * constant for SELECT queries + */ + const SELECT = 0; + /** + * constant for DELETE queries + */ + const DELETE = 1; + /** + * constant for UPDATE queries + */ + const UPDATE = 2; + /** + * constant for INSERT queries + */ + const INSERT = 3; + /** + * constant for CREATE queries + */ + const CREATE = 4; /** * @var array $fetchmodes an array containing all fetchmodes */ @@ -109,6 +133,12 @@ abstract class Doctrine_Hydrate extends Doctrine_Access 'limit' => false, 'offset' => false, ); + /** + * @var integer $type the query type + * + * @see Doctrine_Query::* constants + */ + protected $type = self::SELECT; /** * constructor * @@ -605,6 +635,24 @@ abstract class Doctrine_Hydrate extends Doctrine_Access } return false; } + /** + * getType + * + * returns the type of this query object + * by default the type is Doctrine_Hydrate::SELECT but if update() or delete() + * are being called the type is Doctrine_Hydrate::UPDATE and Doctrine_Hydrate::DELETE, + * respectively + * + * @see Doctrine_Hydrate::SELECT + * @see Doctrine_Hydrate::UPDATE + * @see Doctrine_Hydrate::DELETE + * + * @return integer return the query type + */ + public function getType() + { + return $this->type; + } /** * applyInheritance * applies column aggregation inheritance to DQL / SQL query @@ -627,14 +675,22 @@ abstract class Doctrine_Hydrate extends Doctrine_Access $index = 0; foreach ($array as $tableAlias => $maps) { $a = array(); + + // don't use table aliases if the query isn't a select query + if ($this->type !== Doctrine_Query::SELECT) { + $tableAlias = ''; + } else { + $tableAlias .= '.'; + } + foreach ($maps as $map) { $b = array(); foreach ($map as $field => $value) { if ($index > 0) { - $b[] = '(' . $tableAlias . '.' . $field . ' = ' . $value - . ' OR ' . $tableAlias . '.' . $field . ' IS NULL)'; + $b[] = '(' . $tableAlias . $field . ' = ' . $value + . ' OR ' . $tableAlias . $field . ' IS NULL)'; } else { - $b[] = $tableAlias . '.' . $field . ' = ' . $value; + $b[] = $tableAlias . $field . ' = ' . $value; } } diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index 072aaba70..c9ac1787a 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -31,30 +31,6 @@ Doctrine::autoload('Doctrine_Hydrate'); * @author Konsta Vesterinen */ class Doctrine_Query extends Doctrine_Hydrate implements Countable { - /** - * QUERY TYPE CONSTANTS - */ - - /** - * constant for SELECT queries - */ - const SELECT = 0; - /** - * constant for DELETE queries - */ - const DELETE = 1; - /** - * constant for UPDATE queries - */ - const UPDATE = 2; - /** - * constant for INSERT queries - */ - const INSERT = 3; - /** - * constant for CREATE queries - */ - const CREATE = 4; /** * @param array $subqueryAliases the table aliases needed in some LIMIT subqueries */ @@ -87,12 +63,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { */ private $pendingFields = array(); - /** - * @var integer $type the query type - * - * @see Doctrine_Query::* constants - */ - protected $type = self::SELECT; + /** * create @@ -126,6 +97,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { $this->isSubquery = (bool) $bool; return $this; } + /** * getAggregateAlias * diff --git a/lib/Doctrine/Query/Set.php b/lib/Doctrine/Query/Set.php index 5d98bb293..2e7c56831 100644 --- a/lib/Doctrine/Query/Set.php +++ b/lib/Doctrine/Query/Set.php @@ -46,9 +46,9 @@ class Doctrine_Query_Set extends Doctrine_Query_Part $reference = implode('.', $e); $alias = $this->query->getTableAlias($reference); + $table = $this->query->getTable($alias); - $fieldname = $alias ? $alias . '.' . $field : $field; - $result[] = $fieldname . ' = ' . $set[1]; + $result[] = $table->getColumnName($field) . ' = ' . $set[1]; } return implode(', ', $result); diff --git a/lib/Doctrine/Query/Where.php b/lib/Doctrine/Query/Where.php index 1f63dfc52..370301726 100644 --- a/lib/Doctrine/Query/Where.php +++ b/lib/Doctrine/Query/Where.php @@ -132,7 +132,7 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition $trimmed = Doctrine_Query::bracketTrim($value); if (substr($trimmed, 0, 4) == 'FROM' || substr($trimmed, 0, 6) == 'SELECT') { - + // subquery found $q = new Doctrine_Query(); $value = '(' . $q->isSubquery(true)->parseQuery($trimmed)->getQuery() . ')'; @@ -171,7 +171,13 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition $value = $enumIndex; } default: - $fieldname = $alias ? $alias . '.' . $field : $field; + + if ($this->query->getType() === Doctrine_Query::SELECT) { + $fieldname = $alias ? $alias . '.' . $field : $field; + } else { + $fieldname = $field; + } + $where = $fieldname . ' ' . $operator . ' ' . $value; } diff --git a/tests/Query/DeleteTestCase.php b/tests/Query/DeleteTestCase.php index 70ef408ae..f96f53945 100644 --- a/tests/Query/DeleteTestCase.php +++ b/tests/Query/DeleteTestCase.php @@ -5,13 +5,13 @@ class Doctrine_Query_Delete_TestCase extends Doctrine_UnitTestCase { $q->parseQuery('DELETE FROM User'); - $this->assertEqual($q->getQuery(), 'DELETE FROM entity WHERE (e.type = 0)'); + $this->assertEqual($q->getQuery(), 'DELETE FROM entity WHERE (type = 0)'); $q = new Doctrine_Query(); $q->delete()->from('User'); - $this->assertEqual($q->getQuery(), 'DELETE FROM entity WHERE (e.type = 0)'); + $this->assertEqual($q->getQuery(), 'DELETE FROM entity WHERE (type = 0)'); } public function testDeleteAll() { $q = new Doctrine_Query(); diff --git a/tests/Query/UpdateTestCase.php b/tests/Query/UpdateTestCase.php index 777b93d85..e503625f7 100644 --- a/tests/Query/UpdateTestCase.php +++ b/tests/Query/UpdateTestCase.php @@ -5,26 +5,33 @@ class Doctrine_Query_Update_TestCase extends Doctrine_UnitTestCase { $q->parseQuery("UPDATE User u SET u.name = 'someone'"); - $this->assertEqual($q->getQuery(), "UPDATE entity e SET e.name = 'someone' WHERE (e.type = 0)"); + $this->assertEqual($q->getQuery(), "UPDATE entity SET name = 'someone' WHERE (type = 0)"); $q = new Doctrine_Query(); $q->update('User u')->set('u.name', "'someone'"); - $this->assertEqual($q->getQuery(), "UPDATE entity e SET e.name = 'someone' WHERE (e.type = 0)"); + $this->assertEqual($q->getQuery(), "UPDATE entity SET name = 'someone' WHERE (type = 0)"); } public function testUpdateWorksWithMultipleColumns() { $q = new Doctrine_Query(); $q->parseQuery("UPDATE User u SET u.name = 'someone', u.email_id = 5"); - $this->assertEqual($q->getQuery(), "UPDATE entity e SET e.name = 'someone', e.email_id = 5 WHERE (e.type = 0)"); + $this->assertEqual($q->getQuery(), "UPDATE entity SET name = 'someone', email_id = 5 WHERE (type = 0)"); $q = new Doctrine_Query(); $q->update('User u')->set('u.name', "'someone'")->set('u.email_id', 5); - $this->assertEqual($q->getQuery(), "UPDATE entity e SET e.name = 'someone', e.email_id = 5 WHERE (e.type = 0)"); + $this->assertEqual($q->getQuery(), "UPDATE entity SET name = 'someone', email_id = 5 WHERE (type = 0)"); + } + public function testUpdateSupportsConditions() { + $q = new Doctrine_Query(); + + $q->parseQuery("UPDATE User u SET u.name = 'someone' WHERE u.id = 5"); + + $this->assertEqual($q->getQuery(), "UPDATE entity SET name = 'someone' WHERE id = 5 AND (type = 0)"); } } ?> diff --git a/tests/run.php b/tests/run.php index 6ef688db7..f7e8b21db 100644 --- a/tests/run.php +++ b/tests/run.php @@ -62,7 +62,7 @@ $test = new GroupTest('Doctrine Framework Unit Tests'); // DATABASE ABSTRACTION tests - +/** // Connection drivers (not yet fully tested) $test->addTestCase(new Doctrine_Connection_Pgsql_TestCase()); $test->addTestCase(new Doctrine_Connection_Oracle_TestCase()); @@ -196,7 +196,7 @@ $test->addTestCase(new Doctrine_CustomResultSetOrderTestCase()); //$test->addTestCase(new Doctrine_Collection_Offset_TestCase()); -// Query tests +// Query tests */ $test->addTestCase(new Doctrine_Query_MultiJoin_TestCase()); $test->addTestCase(new Doctrine_Query_ReferenceModel_TestCase()); @@ -205,7 +205,7 @@ $test->addTestCase(new Doctrine_Query_ComponentAlias_TestCase()); $test->addTestCase(new Doctrine_Query_TestCase()); $test->addTestCase(new Doctrine_Query_ShortAliases_TestCase()); -$test->addTestCase(new Doctrine_Query_Delete_TestCase()); + $test->addTestCase(new Doctrine_Query_Where_TestCase()); $test->addTestCase(new Doctrine_Query_Limit_TestCase()); $test->addTestCase(new Doctrine_Query_IdentifierQuoting_TestCase()); @@ -229,6 +229,8 @@ $test->addTestCase(new Doctrine_Cache_Apc_TestCase()); $test->addTestCase(new Doctrine_Cache_Memcache_TestCase()); $test->addTestCase(new Doctrine_Cache_Sqlite_TestCase()); +$test->addTestCase(new Doctrine_Query_Delete_TestCase()); + // Cache tests //$test->addTestCase(new Doctrine_Cache_Query_SqliteTestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase());