This commit is contained in:
parent
c9d9bf1905
commit
4e888855d1
3 changed files with 112 additions and 54 deletions
|
@ -36,8 +36,8 @@ class Doctrine_Column extends Doctrine_Access implements IteratorAggregate, Coun
|
|||
* @var array $definition
|
||||
*/
|
||||
protected $_definition = array(
|
||||
'type' => null,
|
||||
'length' => 0,
|
||||
'type' => null,
|
||||
'length' => 0,
|
||||
);
|
||||
/**
|
||||
* @var array $definition
|
||||
|
@ -86,6 +86,46 @@ class Doctrine_Column extends Doctrine_Access implements IteratorAggregate, Coun
|
|||
{
|
||||
$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
|
||||
*
|
||||
|
|
|
@ -99,6 +99,10 @@ class Doctrine_Hydrate_Alias
|
|||
|
||||
return $alias;
|
||||
}
|
||||
public function addAlias($tableAlias, $componentAlias)
|
||||
{
|
||||
$this->shortAliases[$tableAlias] = $componentAlias;
|
||||
}
|
||||
/**
|
||||
* getShortAlias
|
||||
* some database such as Oracle need the identifier lengths to be < ~30 chars
|
||||
|
|
|
@ -35,7 +35,7 @@ class Doctrine_RawSql extends Doctrine_Hydrate
|
|||
/**
|
||||
* @var array $fields
|
||||
*/
|
||||
private $fields;
|
||||
private $fields = array();
|
||||
/**
|
||||
* __call
|
||||
* method overloader
|
||||
|
@ -59,16 +59,6 @@ class Doctrine_RawSql extends Doctrine_Hydrate
|
|||
}
|
||||
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
|
||||
*
|
||||
|
@ -82,35 +72,35 @@ class Doctrine_RawSql extends Doctrine_Hydrate
|
|||
$this->fields = $m[1];
|
||||
$this->clear();
|
||||
|
||||
$e = Doctrine_Query::sqlExplode($query,' ');
|
||||
$e = Doctrine_Tokenizer::sqlExplode($query,' ');
|
||||
|
||||
foreach ($e as $k => $part) {
|
||||
$low = strtolower($part);
|
||||
switch (strtolower($part)) {
|
||||
case "select":
|
||||
case "from":
|
||||
case "where":
|
||||
case "limit":
|
||||
case "offset":
|
||||
case "having":
|
||||
case 'select':
|
||||
case 'from':
|
||||
case 'where':
|
||||
case 'limit':
|
||||
case 'offset':
|
||||
case 'having':
|
||||
$p = $low;
|
||||
if ( ! isset($parts[$low])) {
|
||||
$parts[$low] = array();
|
||||
}
|
||||
break;
|
||||
case "order":
|
||||
case "group":
|
||||
case 'order':
|
||||
case 'group':
|
||||
$i = ($k + 1);
|
||||
if (isset($e[$i]) && strtolower($e[$i]) === "by") {
|
||||
if (isset($e[$i]) && strtolower($e[$i]) === 'by') {
|
||||
$p = $low;
|
||||
$p .= "by";
|
||||
$parts[$low."by"] = array();
|
||||
$p .= 'by';
|
||||
$parts[$low . 'by'] = array();
|
||||
|
||||
} else {
|
||||
$parts[$p][] = $part;
|
||||
}
|
||||
break;
|
||||
case "by":
|
||||
case 'by':
|
||||
continue;
|
||||
default:
|
||||
if ( ! isset($parts[$p][0])) {
|
||||
|
@ -122,7 +112,7 @@ class Doctrine_RawSql extends Doctrine_Hydrate
|
|||
};
|
||||
|
||||
$this->parts = $parts;
|
||||
$this->parts["select"] = array();
|
||||
$this->parts['select'] = array();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -139,7 +129,8 @@ class Doctrine_RawSql extends Doctrine_Hydrate
|
|||
if ( ! isset($e[1])) {
|
||||
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 {
|
||||
$this->addComponent($e[0], ucwords($e[0]));
|
||||
} catch(Doctrine_Exception $exception) {
|
||||
|
@ -149,12 +140,12 @@ class Doctrine_RawSql extends Doctrine_Hydrate
|
|||
|
||||
if ($e[1] == '*') {
|
||||
foreach ($this->tables[$e[0]]->getColumnNames() as $name) {
|
||||
$field = $e[0].'.'.$name;
|
||||
$this->parts['select'][$field] = $field.' AS '.$e[0].'__'.$name;
|
||||
$field = $e[0] . '.' . $name;
|
||||
$this->parts['select'][$field] = $field . ' AS ' . $e[0] . '__' . $name;
|
||||
}
|
||||
} else {
|
||||
$field = $e[0].'.'.$e[1];
|
||||
$this->parts['select'][$field] = $field.' AS '.$e[0].'__'.$e[1];
|
||||
$field = $e[0] . '.' . $e[1];
|
||||
$this->parts['select'][$field] = $field . ' AS ' . $e[0] . '__' . $e[1];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -163,8 +154,8 @@ class Doctrine_RawSql extends Doctrine_Hydrate
|
|||
foreach ($this->tableAliases as $alias) {
|
||||
foreach ($this->tables[$alias]->getPrimaryKeys() as $key) {
|
||||
$field = $alias . '.' . $key;
|
||||
if ( ! isset($this->parts["select"][$field])) {
|
||||
$this->parts["select"][$field] = $field." AS ".$alias."__".$key;
|
||||
if ( ! isset($this->parts['select'][$field])) {
|
||||
$this->parts['select'][$field] = $field . ' AS ' . $alias . '__' . $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -207,37 +198,60 @@ class Doctrine_RawSql extends Doctrine_Hydrate
|
|||
* @param string $componentName
|
||||
* @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);
|
||||
|
||||
$table = null;
|
||||
|
||||
if (isset($this->_aliasMap[$e[0]])) {
|
||||
$table = $this->_aliasMap[$e[0]]['table'];
|
||||
|
||||
$prevPath = $parent = array_shift($e);
|
||||
}
|
||||
|
||||
|
||||
$currPath = '';
|
||||
$table = null;
|
||||
|
||||
foreach ($e as $k => $component) {
|
||||
$currPath .= '.' . $component;
|
||||
if ($k == 0)
|
||||
$currPath = substr($currPath,1);
|
||||
// get length of the previous path
|
||||
$length = strlen($currPath);
|
||||
|
||||
if (isset($this->tableAliases[$currPath])) {
|
||||
$alias = $this->tableAliases[$currPath];
|
||||
// build the current component path
|
||||
$prevPath = ($currPath) ? $currPath . '.' . $component : $component;
|
||||
|
||||
$delimeter = substr($fullPath, $length, 1);
|
||||
|
||||
// if an alias is not given use the current path as an alias identifier
|
||||
if (strlen($currPath) === $fullLength && isset($originalAlias)) {
|
||||
$componentAlias = $originalAlias;
|
||||
} 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) {
|
||||
$tableName = $table->getAliasName($component);
|
||||
$this->_aliasMap[$componentAlias] = array('table' => $relation->getTable(),
|
||||
'parent' => $parent,
|
||||
'relation' => $relation);
|
||||
}
|
||||
$table = $this->conn->getTable($component);
|
||||
$this->tables[$alias] = $table;
|
||||
$this->fetchModes[$alias] = Doctrine::FETCH_IMMEDIATE;
|
||||
$this->aliasHandler->addAlias($componentAlias, $tableAlias);
|
||||
|
||||
$this->tableAliases[$currPath] = $alias;
|
||||
|
||||
if ($k !== 0) {
|
||||
$this->joins[$alias] = $prevAlias;
|
||||
}
|
||||
$prevAlias = $alias;
|
||||
$prevPath = $currPath;
|
||||
|
||||
$parent = $prevPath;
|
||||
}
|
||||
|
||||
return $this;
|
||||
|
|
Loading…
Add table
Reference in a new issue