Commit 1d6020c4 authored by zYne's avatar zYne

fixes #533, using DQL identifier aliases before column names is now optional...

fixes #533, using DQL identifier aliases before column names is now optional when selecting from single component
parent 0695a6ab
...@@ -40,6 +40,57 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -40,6 +40,57 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
const STATE_LOCKED = 4; const STATE_LOCKED = 4;
protected static $_keywords = array('ALL',
'AND',
'ANY',
'AS',
'ASC',
'AVG',
'BETWEEN',
'BIT_LENGTH',
'BY',
'CHARACTER_LENGTH',
'CHAR_LENGTH',
'CURRENT_DATE',
'CURRENT_TIME',
'CURRENT_TIMESTAMP',
'DELETE',
'DESC',
'DISTINCT',
'EMPTY',
'EXISTS',
'FALSE',
'FETCH',
'FROM',
'GROUP',
'HAVING',
'IN',
'INDEXBY',
'INNER',
'IS',
'JOIN',
'LEFT',
'LIKE',
'LOWER',
'MEMBER',
'MOD',
'NEW',
'NOT',
'NULL',
'OBJECT',
'OF',
'OR',
'ORDER',
'OUTER',
'POSITION',
'SELECT',
'SOME',
'TRIM',
'TRUE',
'UNKNOWN',
'UPDATE',
'WHERE');
protected $subqueryAliases = array(); protected $subqueryAliases = array();
...@@ -576,6 +627,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -576,6 +627,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
*/ */
public function parseClause($clause) public function parseClause($clause)
{ {
$clause = trim($clause);
if (is_numeric($clause)) { if (is_numeric($clause)) {
return $clause; return $clause;
} }
...@@ -585,7 +638,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -585,7 +638,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
$str = ''; $str = '';
foreach ($terms as $term) { foreach ($terms as $term) {
$pos = strpos($term[0], '('); $pos = strpos($term[0], '(');
if ($pos !== false) { if ($pos !== false) {
$name = substr($term[0], 0, $pos); $name = substr($term[0], 0, $pos);
if ($name !== '') { if ($name !== '') {
...@@ -621,6 +674,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -621,6 +674,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
} }
} else { } else {
if (substr($term[0], 0, 1) !== "'" && substr($term[0], -1) !== "'") { if (substr($term[0], 0, 1) !== "'" && substr($term[0], -1) !== "'") {
if (strpos($term[0], '.') !== false) { if (strpos($term[0], '.') !== false) {
if ( ! is_numeric($term[0])) { if ( ! is_numeric($term[0])) {
$e = explode('.', $term[0]); $e = explode('.', $term[0]);
...@@ -638,7 +692,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -638,7 +692,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
if ( ! isset($this->_aliasMap[$componentAlias])) { if ( ! isset($this->_aliasMap[$componentAlias])) {
throw new Doctrine_Query_Exception('Unknown component alias ' . $componentAlias); throw new Doctrine_Query_Exception('Unknown component alias ' . $componentAlias);
} }
$table = $this->_aliasMap[$componentAlias]['table']; $table = $this->_aliasMap[$componentAlias]['table'];
// get the actual field name from alias // get the actual field name from alias
...@@ -650,7 +704,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -650,7 +704,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
} }
$tableAlias = $this->getTableAlias($componentAlias); $tableAlias = $this->getTableAlias($componentAlias);
// build sql expression // build sql expression
$term[0] = $this->_conn->quoteIdentifier($tableAlias) $term[0] = $this->_conn->quoteIdentifier($tableAlias)
. '.' . '.'
...@@ -660,6 +714,47 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -660,6 +714,47 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
$term[0] = $this->_conn->quoteIdentifier($field); $term[0] = $this->_conn->quoteIdentifier($field);
} }
} }
} else {
if ( ! empty($term[0]) &&
! in_array(strtoupper($term[0]), self::$_keywords) &&
! is_numeric($term[0])) {
$componentAlias = $this->getRootAlias();
$found = false;
if ($componentAlias !== false &&
$componentAlias !== null) {
$table = $this->_aliasMap[$componentAlias]['table'];
// check column existence
if ($table->hasColumn($term[0])) {
$found = true;
// get the actual field name from alias
$term[0] = $table->getColumnName($term[0]);
$tableAlias = $this->getTableAlias($componentAlias);
if ($this->getType() === Doctrine_Query::SELECT) {
// build sql expression
$term[0] = $this->_conn->quoteIdentifier($tableAlias)
. '.'
. $this->_conn->quoteIdentifier($term[0]);
} else {
// build sql expression
$term[0] = $this->_conn->quoteIdentifier($term[0]);
}
} else {
$found = false;
}
}
if ( ! $found) {
$term[0] = $this->getAggregateAlias($term[0]);
}
}
} }
} }
} }
......
...@@ -44,19 +44,22 @@ class Doctrine_Query_Groupby extends Doctrine_Query_Part ...@@ -44,19 +44,22 @@ class Doctrine_Query_Groupby extends Doctrine_Query_Part
$r = array(); $r = array();
foreach (explode(',', $str) as $reference) { foreach (explode(',', $str) as $reference) {
$reference = trim($reference); $reference = trim($reference);
$e = explode('.', $reference);
/**
if (count($e) > 1) { if (count($e) > 1) {
$field = array_pop($e); $field = array_pop($e);
$ref = implode('.', $e); $ref = implode('.', $e);
$this->query->load($ref); $this->query->load($ref);
$r[] = $this->query->getTableAlias($ref) . '.' . $field; $r[] = $this->query->getTableAlias($ref) . '.' . $field;
} else { } else {
$alias = end($e); $alias = end($e);
$r[] = $this->query->getAggregateAlias($alias); $r[] = $this->query->getAggregateAlias($alias);
} }
*/
$r[] = $this->query->parseClause($reference);
} }
return implode(', ', $r); return implode(', ', $r);
} }
} }
\ No newline at end of file
...@@ -44,31 +44,10 @@ class Doctrine_Query_Orderby extends Doctrine_Query_Part ...@@ -44,31 +44,10 @@ class Doctrine_Query_Orderby extends Doctrine_Query_Part
$ret = array(); $ret = array();
foreach (explode(',', trim($str)) as $r) { foreach (explode(',', trim($str)) as $r) {
$r = trim($r); $r = $this->query->parseClause($r);
$e = explode(' ', $r);
$a = explode('.', $e[0]);
if (count($a) > 1) {
$field = array_pop($a);
$reference = implode('.', $a);
$name = end($a);
$map = $this->query->load($reference, false);
$tableAlias = $this->query->getTableAlias($reference);
$r = $tableAlias . '.' . $field;
} else {
$field = $this->query->getAggregateAlias($e[0]);
$r = $field;
}
if (isset($e[1])) {
$r .= ' ' . $e[1];
}
$ret[] = $r; $ret[] = $r;
} }
return $ret; return $ret;
} }
} }
\ No newline at end of file
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