Commit 4e888855 authored by zYne's avatar zYne

--no commit message

--no commit message
parent c9d9bf19
...@@ -36,8 +36,8 @@ class Doctrine_Column extends Doctrine_Access implements IteratorAggregate, Coun ...@@ -36,8 +36,8 @@ class Doctrine_Column extends Doctrine_Access implements IteratorAggregate, Coun
* @var array $definition * @var array $definition
*/ */
protected $_definition = array( protected $_definition = array(
'type' => null, 'type' => null,
'length' => 0, 'length' => 0,
); );
/** /**
* @var array $definition * @var array $definition
...@@ -86,6 +86,46 @@ class Doctrine_Column extends Doctrine_Access implements IteratorAggregate, Coun ...@@ -86,6 +86,46 @@ class Doctrine_Column extends Doctrine_Access implements IteratorAggregate, Coun
{ {
$this->_definition[$name] = $value; $this->_definition[$name] = $value;
} }
/**
* @param string $field
* @return array
*/
public function getEnumValues()
{
if (isset($this->_definition['values'])) {
return $this->_definition['values'];
} else {
return array();
}
}
/**
* enumValue
*
* @param string $field
* @param integer $index
* @return mixed
*/
public function enumValue($index)
{
if ($index instanceof Doctrine_Null) {
return $index;
}
return isset($this->_definition['values'][$index]) ? $this->_definition['values'][$index] : $index;
}
/**
* enumIndex
*
* @param string $field
* @param mixed $value
* @return mixed
*/
public function enumIndex($field, $value)
{
$values = $this->getEnumValues($field);
return array_search($value, $values);
}
/** /**
* count * count
* *
......
...@@ -99,6 +99,10 @@ class Doctrine_Hydrate_Alias ...@@ -99,6 +99,10 @@ class Doctrine_Hydrate_Alias
return $alias; return $alias;
} }
public function addAlias($tableAlias, $componentAlias)
{
$this->shortAliases[$tableAlias] = $componentAlias;
}
/** /**
* getShortAlias * getShortAlias
* some database such as Oracle need the identifier lengths to be < ~30 chars * some database such as Oracle need the identifier lengths to be < ~30 chars
......
...@@ -35,7 +35,7 @@ class Doctrine_RawSql extends Doctrine_Hydrate ...@@ -35,7 +35,7 @@ class Doctrine_RawSql extends Doctrine_Hydrate
/** /**
* @var array $fields * @var array $fields
*/ */
private $fields; private $fields = array();
/** /**
* __call * __call
* method overloader * method overloader
...@@ -59,16 +59,6 @@ class Doctrine_RawSql extends Doctrine_Hydrate ...@@ -59,16 +59,6 @@ class Doctrine_RawSql extends Doctrine_Hydrate
} }
return $this; return $this;
} }
/**
* get
*/
public function get($name)
{
if ( ! isset($this->parts[$name])) {
throw new Doctrine_RawSql_Exception('Unknown query part ' . $name);
}
return $this->parts[$name];
}
/** /**
* parseQuery * parseQuery
* *
...@@ -82,35 +72,35 @@ class Doctrine_RawSql extends Doctrine_Hydrate ...@@ -82,35 +72,35 @@ class Doctrine_RawSql extends Doctrine_Hydrate
$this->fields = $m[1]; $this->fields = $m[1];
$this->clear(); $this->clear();
$e = Doctrine_Query::sqlExplode($query,' '); $e = Doctrine_Tokenizer::sqlExplode($query,' ');
foreach ($e as $k => $part) { foreach ($e as $k => $part) {
$low = strtolower($part); $low = strtolower($part);
switch (strtolower($part)) { switch (strtolower($part)) {
case "select": case 'select':
case "from": case 'from':
case "where": case 'where':
case "limit": case 'limit':
case "offset": case 'offset':
case "having": case 'having':
$p = $low; $p = $low;
if ( ! isset($parts[$low])) { if ( ! isset($parts[$low])) {
$parts[$low] = array(); $parts[$low] = array();
} }
break; break;
case "order": case 'order':
case "group": case 'group':
$i = ($k + 1); $i = ($k + 1);
if (isset($e[$i]) && strtolower($e[$i]) === "by") { if (isset($e[$i]) && strtolower($e[$i]) === 'by') {
$p = $low; $p = $low;
$p .= "by"; $p .= 'by';
$parts[$low."by"] = array(); $parts[$low . 'by'] = array();
} else { } else {
$parts[$p][] = $part; $parts[$p][] = $part;
} }
break; break;
case "by": case 'by':
continue; continue;
default: default:
if ( ! isset($parts[$p][0])) { if ( ! isset($parts[$p][0])) {
...@@ -122,7 +112,7 @@ class Doctrine_RawSql extends Doctrine_Hydrate ...@@ -122,7 +112,7 @@ class Doctrine_RawSql extends Doctrine_Hydrate
}; };
$this->parts = $parts; $this->parts = $parts;
$this->parts["select"] = array(); $this->parts['select'] = array();
return $this; return $this;
} }
...@@ -139,7 +129,8 @@ class Doctrine_RawSql extends Doctrine_Hydrate ...@@ -139,7 +129,8 @@ class Doctrine_RawSql extends Doctrine_Hydrate
if ( ! isset($e[1])) { if ( ! isset($e[1])) {
throw new Doctrine_RawSql_Exception('All selected fields in Sql query must be in format tableAlias.fieldName'); throw new Doctrine_RawSql_Exception('All selected fields in Sql query must be in format tableAlias.fieldName');
} }
if ( ! isset($this->tables[$e[0]])) { // try to auto-add component
if ( ! $this->aliasHandler->getComponentAlias($e[0])) {
try { try {
$this->addComponent($e[0], ucwords($e[0])); $this->addComponent($e[0], ucwords($e[0]));
} catch(Doctrine_Exception $exception) { } catch(Doctrine_Exception $exception) {
...@@ -149,12 +140,12 @@ class Doctrine_RawSql extends Doctrine_Hydrate ...@@ -149,12 +140,12 @@ class Doctrine_RawSql extends Doctrine_Hydrate
if ($e[1] == '*') { if ($e[1] == '*') {
foreach ($this->tables[$e[0]]->getColumnNames() as $name) { foreach ($this->tables[$e[0]]->getColumnNames() as $name) {
$field = $e[0].'.'.$name; $field = $e[0] . '.' . $name;
$this->parts['select'][$field] = $field.' AS '.$e[0].'__'.$name; $this->parts['select'][$field] = $field . ' AS ' . $e[0] . '__' . $name;
} }
} else { } else {
$field = $e[0].'.'.$e[1]; $field = $e[0] . '.' . $e[1];
$this->parts['select'][$field] = $field.' AS '.$e[0].'__'.$e[1]; $this->parts['select'][$field] = $field . ' AS ' . $e[0] . '__' . $e[1];
} }
} }
...@@ -163,8 +154,8 @@ class Doctrine_RawSql extends Doctrine_Hydrate ...@@ -163,8 +154,8 @@ class Doctrine_RawSql extends Doctrine_Hydrate
foreach ($this->tableAliases as $alias) { foreach ($this->tableAliases as $alias) {
foreach ($this->tables[$alias]->getPrimaryKeys() as $key) { foreach ($this->tables[$alias]->getPrimaryKeys() as $key) {
$field = $alias . '.' . $key; $field = $alias . '.' . $key;
if ( ! isset($this->parts["select"][$field])) { if ( ! isset($this->parts['select'][$field])) {
$this->parts["select"][$field] = $field." AS ".$alias."__".$key; $this->parts['select'][$field] = $field . ' AS ' . $alias . '__' . $key;
} }
} }
} }
...@@ -207,37 +198,60 @@ class Doctrine_RawSql extends Doctrine_Hydrate ...@@ -207,37 +198,60 @@ class Doctrine_RawSql extends Doctrine_Hydrate
* @param string $componentName * @param string $componentName
* @return Doctrine_RawSql * @return Doctrine_RawSql
*/ */
public function addComponent($tableAlias, $componentName) public function addComponent($tableAlias, $path)
{ {
$e = explode('.', $componentName); $tmp = explode(' ', $path);
$originalAlias = (count($tmp) > 1) ? end($tmp) : null;
$e = explode('.', $tmp[0]);
$fullPath = $tmp[0];
$fullLength = strlen($fullPath);
$currPath = '';
$table = null; $table = null;
if (isset($this->_aliasMap[$e[0]])) {
$table = $this->_aliasMap[$e[0]]['table'];
$prevPath = $parent = array_shift($e);
}
$currPath = '';
foreach ($e as $k => $component) { foreach ($e as $k => $component) {
$currPath .= '.' . $component; // get length of the previous path
if ($k == 0) $length = strlen($currPath);
$currPath = substr($currPath,1);
// build the current component path
$prevPath = ($currPath) ? $currPath . '.' . $component : $component;
if (isset($this->tableAliases[$currPath])) { $delimeter = substr($fullPath, $length, 1);
$alias = $this->tableAliases[$currPath];
// if an alias is not given use the current path as an alias identifier
if (strlen($currPath) === $fullLength && isset($originalAlias)) {
$componentAlias = $originalAlias;
} else { } else {
$alias = $tableAlias; $componentAlias = $currPath;
} }
if ( ! isset($table)) {
$conn = Doctrine_Manager::getInstance()
->getConnectionForComponent($name);
$table = $conn->getTable($component);
$this->_aliasMap[$componentAlias] = array('table' => $table);
} else {
$relation = $table->getRelation($name);
if ($table) { $this->_aliasMap[$componentAlias] = array('table' => $relation->getTable(),
$tableName = $table->getAliasName($component); 'parent' => $parent,
'relation' => $relation);
} }
$table = $this->conn->getTable($component); $this->aliasHandler->addAlias($componentAlias, $tableAlias);
$this->tables[$alias] = $table;
$this->fetchModes[$alias] = Doctrine::FETCH_IMMEDIATE;
$this->tableAliases[$currPath] = $alias;
if ($k !== 0) { $this->tableAliases[$currPath] = $alias;
$this->joins[$alias] = $prevAlias;
} $parent = $prevPath;
$prevAlias = $alias;
$prevPath = $currPath;
} }
return $this; return $this;
......
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