1
0
mirror of synced 2024-12-13 22:56:04 +03:00
doctrine2/Doctrine/Query.php

1015 lines
33 KiB
PHP
Raw Normal View History

2006-05-30 12:42:10 +04:00
<?php
require_once("Access.php");
/**
* Doctrine_Query
*
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
* @version 1.0 alpha
*/
class Doctrine_Query extends Doctrine_Access {
/**
2006-06-13 22:04:04 +04:00
* @var array $fetchmodes an array containing all fetchmodes
2006-05-30 12:42:10 +04:00
*/
private $fetchModes = array();
/**
2006-06-13 22:04:04 +04:00
* @var array $tables an array containing all the tables used in the query
2006-05-30 12:42:10 +04:00
*/
private $tables = array();
/**
2006-06-13 22:04:04 +04:00
* @var array $collections an array containing all collections this parser has created/will create
2006-05-30 12:42:10 +04:00
*/
private $collections = array();
2006-06-22 00:02:40 +04:00
/**
* @var array $joins an array containing all table joins
*/
2006-05-30 12:42:10 +04:00
private $joins = array();
/**
2006-06-13 22:04:04 +04:00
* @var array $data fetched data
2006-05-30 12:42:10 +04:00
*/
private $data = array();
/**
2006-06-13 22:04:04 +04:00
* @var Doctrine_Session $session Doctrine_Session object
2006-05-30 12:42:10 +04:00
*/
private $session;
2006-06-13 22:04:04 +04:00
/**
* @var Doctrine_View $view Doctrine_View object
*/
private $view;
2006-05-30 12:42:10 +04:00
private $inheritanceApplied = false;
private $aggregate = false;
/**
* @var array $tableAliases
*/
private $tableAliases = array();
/**
* @var array $tableIndexes
*/
private $tableIndexes = array();
/**
* @var array $paths
*/
private $paths = array();
2006-05-30 12:42:10 +04:00
/**
* @var array $parts SQL query string parts
*/
protected $parts = array(
"columns" => array(),
"from" => array(),
"join" => array(),
"where" => array(),
"groupby" => array(),
2006-05-30 12:42:10 +04:00
"having" => array(),
"orderby" => array(),
"limit" => false,
"offset" => false,
);
/**
* constructor
*
* @param Doctrine_Session $session
*/
public function __construct(Doctrine_Session $session) {
$this->session = $session;
}
2006-06-13 22:04:04 +04:00
/**
* @return Doctrine_Session
*/
public function getSession() {
return $this->session;
}
/**
* setView
* sets a database view this query object uses
* this method should only be called internally by doctrine
*
* @param Doctrine_View $view database view
* @return void
*/
public function setView(Doctrine_View $view) {
$this->view = $view;
}
/**
* getView
*
* @return Doctrine_View
*/
public function getView() {
return $this->view;
}
2006-05-30 12:42:10 +04:00
/**
* clear
* resets all the variables
*
* @return void
*/
private function clear() {
$this->fetchModes = array();
$this->tables = array();
$this->parts = array(
"columns" => array(),
"from" => array(),
"join" => array(),
"where" => array(),
"groupby" => array(),
2006-05-30 12:42:10 +04:00
"having" => array(),
"orderby" => array(),
"limit" => false,
"offset" => false,
);
$this->inheritanceApplied = false;
$this->aggregate = false;
$this->data = array();
2006-06-30 03:04:39 +04:00
$this->collections = array();
$this->joins = array();
$this->tableIndexes = array();
$this->tableAliases = array();
2006-05-30 12:42:10 +04:00
}
/**
* loadFields
* loads fields for a given table and
* constructs a little bit of sql for every field
*
* fields of the tables become: [tablename].[fieldname] as [tablename]__[fieldname]
*
* @access private
2006-06-26 22:55:42 +04:00
* @param object Doctrine_Table $table a Doctrine_Table object
* @param integer $fetchmode fetchmode the table is using eg. Doctrine::FETCH_LAZY
* @param array $names fields to be loaded (only used in lazy property loading)
2006-05-30 12:42:10 +04:00
* @return void
*/
private function loadFields(Doctrine_Table $table, $fetchmode, array $names, $cpath) {
2006-05-30 12:42:10 +04:00
$name = $table->getComponentName();
switch($fetchmode):
case Doctrine::FETCH_OFFSET:
$this->limit = $table->getAttribute(Doctrine::ATTR_COLL_LIMIT);
case Doctrine::FETCH_IMMEDIATE:
if( ! empty($names))
$names = array_unique(array_merge($table->getPrimaryKeys(), $names));
else
$names = $table->getColumnNames();
2006-05-30 12:42:10 +04:00
break;
case Doctrine::FETCH_LAZY_OFFSET:
$this->limit = $table->getAttribute(Doctrine::ATTR_COLL_LIMIT);
case Doctrine::FETCH_LAZY:
case Doctrine::FETCH_BATCH:
$names = array_unique(array_merge($table->getPrimaryKeys(), $names));
2006-05-30 12:42:10 +04:00
break;
default:
throw new Doctrine_Exception("Unknown fetchmode.");
endswitch;
$component = $table->getComponentName();
$tablename = $this->tableAliases[$cpath];
$this->fetchModes[$tablename] = $fetchmode;
2006-05-30 12:42:10 +04:00
$count = count($this->tables);
2006-06-09 02:11:36 +04:00
2006-05-30 12:42:10 +04:00
foreach($names as $name) {
if($count == 0) {
$this->parts["columns"][] = $tablename.".".$name;
} else {
$this->parts["columns"][] = $tablename.".".$name." AS ".$tablename."__".$name;
2006-05-30 12:42:10 +04:00
}
}
}
/**
* addFrom
*
* @param strint $from
*/
public function addFrom($from) {
$class = "Doctrine_Query_From";
$parser = new $class($this);
$parser->parse($from);
}
/**
* addWhere
*
* @param string $where
*/
public function addWhere($where) {
$class = "Doctrine_Query_Where";
$parser = new $class($this);
$this->parts['where'][] = $parser->parse($where);
}
/**
2006-05-30 12:42:10 +04:00
* sets a query part
*
* @param string $name
* @param array $args
* @return void
*/
public function __call($name, $args) {
$name = strtolower($name);
2006-05-30 12:42:10 +04:00
if(isset($this->parts[$name])) {
$method = "parse".ucwords($name);
switch($name):
2006-06-30 03:04:39 +04:00
case "from":
$this->parts['from'] = array();
$this->parts['columns'] = array();
$this->parts['join'] = array();
$this->joins = array();
$this->tables = array();
$this->fetchModes = array();
$this->tableIndexes = array();
$this->tableAliases = array();
$class = "Doctrine_Query_".ucwords($name);
$parser = new $class($this);
$parser->parse($args[0]);
break;
2006-05-30 12:42:10 +04:00
case "where":
2006-06-30 03:04:39 +04:00
case "having":
case "orderby":
case "groupby":
$class = "Doctrine_Query_".ucwords($name);
$parser = new $class($this);
$this->parts[$name] = array($parser->parse($args[0]));
2006-05-30 12:42:10 +04:00
break;
case "limit":
case "offset":
if($args[0] == null)
$args[0] = false;
$this->parts[$name] = $args[0];
break;
default:
$this->parts[$name] = array();
$this->$method($args[0]);
endswitch;
} else
throw new Doctrine_Query_Exception("Unknown overload method");
2006-05-30 12:42:10 +04:00
return $this;
}
/**
* returns a query part
*
* @param $name query part name
* @return mixed
*/
public function get($name) {
if( ! isset($this->parts[$name]))
return false;
return $this->parts[$name];
}
/**
* sets a query part
*
* @param $name query part name
* @param $value query part value
* @return boolean
*/
public function set($name, $value) {
if(isset($this->parts[$name])) {
$method = "parse".ucwords($name);
switch($name):
case "where":
case "having":
2006-05-30 12:42:10 +04:00
$this->parts[$name] = array($this->$method($value));
break;
case "limit":
case "offset":
if($value == null)
$value = false;
$this->parts[$name] = $value;
break;
case "from":
$this->parts['columns'] = array();
$this->parts['join'] = array();
2006-05-30 12:42:10 +04:00
$this->joins = array();
$this->tables = array();
$this->fetchModes = array();
$this->tableIndexes = array();
$this->tableAliases = array();
2006-05-30 12:42:10 +04:00
default:
$this->parts[$name] = array();
$this->$method($value);
endswitch;
return true;
}
return false;
}
/**
* returns the built sql query
*
* @return string
*/
final public function getQuery() {
if(empty($this->parts["columns"]) || empty($this->parts["from"]))
return false;
// build the basic query
$q = "SELECT ".implode(", ",$this->parts["columns"]).
" FROM ";
foreach($this->parts["from"] as $tname => $bool) {
$a[] = $tname;
}
$q .= implode(", ",$a);
if( ! empty($this->parts['join'])) {
foreach($this->parts['join'] as $part) {
$q .= " ".implode(' ', $part);
}
}
2006-06-26 22:55:42 +04:00
$string = $this->applyInheritance();
if( ! empty($this->parts["where"])) {
2006-05-30 12:42:10 +04:00
$q .= " WHERE ".implode(" ",$this->parts["where"]);
2006-06-26 22:55:42 +04:00
if( ! empty($string))
$q .= " AND (".$string.")";
} else {
if( ! empty($string))
$q .= " WHERE (".$string.")";
}
2006-05-30 12:42:10 +04:00
2006-06-30 03:04:39 +04:00
$q .= ( ! empty($this->parts['groupby']))?" GROUP BY ".implode(", ",$this->parts["groupby"]):'';
$q .= ( ! empty($this->parts['having']))?" HAVING ".implode(" ",$this->parts["having"]):'';
$q .= ( ! empty($this->parts['orderby']))?" ORDER BY ".implode(" ",$this->parts["orderby"]):'';
2006-05-30 12:42:10 +04:00
if( ! empty($this->parts["limit"]) || ! empty($this->offset))
$q = $this->session->modifyLimitQuery($q,$this->parts["limit"],$this->offset);
return $q;
}
/**
* applyInheritance
* applies column aggregation inheritance to DQL query
*
2006-06-26 22:55:42 +04:00
* @return string
2006-05-30 12:42:10 +04:00
*/
final public function applyInheritance() {
// get the inheritance maps
$array = array();
foreach($this->tables as $alias => $table):
$array[$alias][] = $table->getInheritanceMap();
2006-05-30 12:42:10 +04:00
endforeach;
// apply inheritance maps
$str = "";
$c = array();
$index = 0;
2006-05-30 12:42:10 +04:00
foreach($array as $tname => $maps) {
$a = array();
foreach($maps as $map) {
$b = array();
foreach($map as $field => $value) {
if($index > 0)
$b[] = "(".$tname.".$field = $value OR $tname.$field IS NULL)";
else
$b[] = $tname.".$field = $value";
2006-05-30 12:42:10 +04:00
}
if( ! empty($b)) $a[] = implode(" AND ",$b);
}
if( ! empty($a)) $c[] = implode(" AND ",$a);
$index++;
2006-05-30 12:42:10 +04:00
}
$str .= implode(" AND ",$c);
2006-05-30 12:42:10 +04:00
2006-06-26 22:55:42 +04:00
return $str;
2006-05-30 12:42:10 +04:00
}
/**
* getData
* @param $key the component name
* @return array the data row for the specified component
*/
final public function getData($key) {
if(isset($this->data[$key]) && is_array($this->data[$key]))
return $this->data[$key];
return array();
}
/**
* execute
* executes the dql query and populates all collections
*
* @param string $params
* @return Doctrine_Collection the root collection
*/
public function execute($params = array()) {
$this->data = array();
$this->collections = array();
2006-06-13 22:04:04 +04:00
if( ! $this->view)
$query = $this->getQuery();
else
$query = $this->view->getSelectSql();
2006-05-30 12:42:10 +04:00
switch(count($this->tables)):
case 0:
throw new DQLException();
break;
case 1:
$keys = array_keys($this->tables);
$name = $this->tables[$keys[0]]->getComponentName();
$stmt = $this->session->execute($query,$params);
while($data = $stmt->fetch(PDO::FETCH_ASSOC)):
foreach($data as $key => $value):
$e = explode("__",$key);
if(count($e) > 1) {
$data[$e[1]] = $value;
} else {
$data[$e[0]] = $value;
}
unset($data[$key]);
endforeach;
$this->data[$name][] = $data;
endwhile;
return $this->getCollection($keys[0]);
break;
default:
$keys = array_keys($this->tables);
$root = $keys[0];
2006-05-30 12:42:10 +04:00
$stmt = $this->session->execute($query,$params);
2006-05-30 12:42:10 +04:00
$previd = array();
$coll = $this->getCollection($root);
$prev[$root] = $coll;
$array = $this->parseData($stmt);
2006-06-05 14:24:14 +04:00
$colls = array();
2006-05-30 12:42:10 +04:00
foreach($array as $data) {
/**
* remove duplicated data rows and map data into objects
*/
foreach($data as $key => $row) {
if(empty($row))
continue;
2006-06-09 02:11:36 +04:00
2006-05-30 12:42:10 +04:00
$ids = $this->tables[$key]->getIdentifier();
2006-06-09 02:11:36 +04:00
$emptyID = false;
2006-05-30 12:42:10 +04:00
if(is_array($ids)) {
2006-06-09 02:11:36 +04:00
foreach($ids as $id) {
if($row[$id] == null) {
$emptyID = true;
break;
2006-05-30 12:42:10 +04:00
}
2006-06-09 02:11:36 +04:00
}
2006-05-30 12:42:10 +04:00
} else {
if($row[$ids] === null)
2006-06-09 02:11:36 +04:00
$emptyID = true;
}
$name = $key;
2006-06-09 02:11:36 +04:00
if($emptyID) {
2006-06-18 02:46:03 +04:00
2006-06-09 02:11:36 +04:00
$pointer = $this->joins[$name];
$path = array_search($name, $this->tableAliases);
2006-06-26 22:55:42 +04:00
$tmp = explode(".", $path);
$alias = end($tmp);
unset($tmp);
2006-06-09 02:11:36 +04:00
$fk = $this->tables[$pointer]->getForeignKey($alias);
if( ! isset($prev[$pointer]) )
2006-06-18 02:46:03 +04:00
continue;
2006-06-09 02:11:36 +04:00
$last = $prev[$pointer]->getLast();
switch($fk->getType()):
case Doctrine_Relation::ONE_COMPOSITE:
case Doctrine_Relation::ONE_AGGREGATE:
break;
default:
if($last instanceof Doctrine_Record) {
if( ! $last->hasReference($alias)) {
$prev[$name] = $this->getCollection($name);
$last->initReference($prev[$name],$fk);
2006-06-09 02:11:36 +04:00
}
}
endswitch;
continue;
2006-05-30 12:42:10 +04:00
}
if( ! isset($previd[$name]))
$previd[$name] = array();
if($previd[$name] !== $row) {
// set internal data
2006-05-30 12:42:10 +04:00
$this->tables[$name]->setData($row);
// initialize a new record
$record = $this->tables[$name]->getRecord();
if($name == $root) {
// add record into root collection
$coll->add($record);
2006-06-27 01:18:19 +04:00
unset($previd);
2006-05-30 12:42:10 +04:00
} else {
2006-06-09 02:11:36 +04:00
2006-05-30 12:42:10 +04:00
$pointer = $this->joins[$name];
$path = array_search($name, $this->tableAliases);
2006-06-26 22:55:42 +04:00
$tmp = explode(".", $path);
$alias = end($tmp);
unset($tmp);
2006-06-09 02:11:36 +04:00
$fk = $this->tables[$pointer]->getForeignKey($alias);
$last = $prev[$pointer]->getLast();
2006-05-30 12:42:10 +04:00
switch($fk->getType()):
case Doctrine_Relation::ONE_COMPOSITE:
case Doctrine_Relation::ONE_AGGREGATE:
2006-06-27 01:18:19 +04:00
// one-to-one relation
$last->internalSet($fk->getLocal(), $record->getIncremented());
2006-05-30 12:42:10 +04:00
2006-06-27 01:18:19 +04:00
$last->initSingleReference($record, $fk);
2006-05-30 12:42:10 +04:00
$prev[$name] = $record;
break;
default:
2006-06-27 01:18:19 +04:00
2006-05-30 12:42:10 +04:00
// one-to-many relation or many-to-many relation
if( ! $last->hasReference($alias)) {
2006-05-30 12:42:10 +04:00
$prev[$name] = $this->getCollection($name);
$last->initReference($prev[$name], $fk);
} else {
// previous entry found from identityMap
$prev[$name] = $last->get($alias);
2006-05-30 12:42:10 +04:00
}
2006-06-26 22:55:42 +04:00
$last->addReference($record, $fk);
2006-05-30 12:42:10 +04:00
endswitch;
}
}
$previd[$name] = $row;
}
}
return $coll;
endswitch;
}
/**
* parseData
* parses the data returned by PDOStatement
*
* @return array
*/
public function parseData(PDOStatement $stmt) {
$array = array();
2006-06-05 14:24:14 +04:00
2006-05-30 12:42:10 +04:00
while($data = $stmt->fetch(PDO::FETCH_ASSOC)):
/**
* parse the data into two-dimensional array
*/
foreach($data as $key => $value):
$e = explode("__",$key);
if(count($e) > 1) {
2006-06-05 14:24:14 +04:00
$data[$e[0]][$e[1]] = $value;
2006-05-30 12:42:10 +04:00
} else {
$data[0][$e[0]] = $value;
}
unset($data[$key]);
endforeach;
$array[] = $data;
endwhile;
$stmt->closeCursor();
return $array;
}
/**
* returns a Doctrine_Table for given name
*
* @param string $name component name
* @return Doctrine_Table
*/
public function getTable($name) {
return $this->tables[$name];
}
/**
* getCollection
*
* @parma string $name component name
* @param integer $index
*/
private function getCollection($name) {
$table = $this->tables[$name];
2006-05-30 12:42:10 +04:00
switch($this->fetchModes[$name]):
case Doctrine::FETCH_BATCH:
$coll = new Doctrine_Collection_Batch($table);
break;
case Doctrine::FETCH_LAZY:
$coll = new Doctrine_Collection_Lazy($table);
break;
case Doctrine::FETCH_OFFSET:
$coll = new Doctrine_Collection_Offset($table);
break;
case Doctrine::FETCH_IMMEDIATE:
$coll = new Doctrine_Collection_Immediate($table);
break;
case Doctrine::FETCH_LAZY_OFFSET:
$coll = new Doctrine_Collection_LazyOffset($table);
break;
default:
throw new Doctrine_Exception("Unknown fetchmode");
2006-05-30 12:42:10 +04:00
endswitch;
$coll->populate($this);
return $coll;
}
/**
* query the database with DQL (Doctrine Query Language)
*
* @param string $query DQL query
* @param array $params parameters
*/
public function query($query,$params = array()) {
$this->parseQuery($query);
if($this->aggregate) {
$keys = array_keys($this->tables);
$query = $this->getQuery();
$stmt = $this->tables[$keys[0]]->getSession()->select($query,$this->parts["limit"],$this->offset);
$data = $stmt->fetch(PDO::FETCH_ASSOC);
if(count($data) == 1) {
return current($data);
} else {
return $data;
}
} else {
return $this->execute($params);
}
}
/**
* DQL PARSER
* parses a DQL query
* first splits the query in parts and then uses individual
* parsers for each part
2006-05-30 12:42:10 +04:00
*
* @param string $query DQL query
* @return void
*/
final public function parseQuery($query) {
$this->clear();
$e = self::bracketExplode($query," ","(",")");
2006-05-30 12:42:10 +04:00
$parts = array();
foreach($e as $k=>$part):
switch(strtolower($part)):
case "select":
case "from":
case "where":
case "limit":
case "offset":
2006-06-30 03:04:39 +04:00
case "having":
2006-05-30 12:42:10 +04:00
$p = $part;
$parts[$part] = array();
break;
case "order":
2006-06-30 03:04:39 +04:00
case "group":
$i = ($k + 1);
if(isset($e[$i]) && strtolower($e[$i]) === "by") {
$p = $part;
2006-05-30 12:42:10 +04:00
$parts[$part] = array();
2006-06-30 03:04:39 +04:00
} else
$parts[$p][] = $part;
2006-05-30 12:42:10 +04:00
break;
case "by":
continue;
default:
$parts[$p][] = $part;
endswitch;
endforeach;
foreach($parts as $k => $part) {
$part = implode(" ",$part);
switch(strtoupper($k)):
case "SELECT":
$this->parseSelect($part);
break;
case "FROM":
2006-06-30 03:04:39 +04:00
$class = "Doctrine_Query_".ucwords(strtolower($k));
$parser = new $class($this);
$parser->parse($part);
2006-05-30 12:42:10 +04:00
break;
2006-06-30 03:04:39 +04:00
case "GROUP":
case "ORDER":
$k .= "by";
2006-05-30 12:42:10 +04:00
case "WHERE":
2006-06-30 03:04:39 +04:00
case "HAVING":
$class = "Doctrine_Query_".ucwords(strtolower($k));
$parser = new $class($this);
$name = strtolower($k);
$this->parts[$name][] = $parser->parse($part);
2006-05-30 12:42:10 +04:00
break;
case "LIMIT":
$this->parts["limit"] = trim($part);
break;
case "OFFSET":
$this->offset = trim($part);
break;
endswitch;
}
}
/**
* DQL ORDER BY PARSER
* parses the order by part of the query string
*
* @param string $str
* @return void
*/
2006-06-30 03:04:39 +04:00
final public function parseOrderBy($str) {
$parser = new Doctrine_Query_Part_Orderby($this);
return $parser->parse($str);
2006-05-30 12:42:10 +04:00
}
/**
* returns Doctrine::FETCH_* constant
*
* @param string $mode
* @return integer
*/
2006-06-30 03:04:39 +04:00
final public function parseFetchMode($mode) {
2006-05-30 12:42:10 +04:00
switch(strtolower($mode)):
case "i":
case "immediate":
$fetchmode = Doctrine::FETCH_IMMEDIATE;
break;
case "b":
case "batch":
$fetchmode = Doctrine::FETCH_BATCH;
break;
case "l":
case "lazy":
$fetchmode = Doctrine::FETCH_LAZY;
break;
case "o":
case "offset":
$fetchmode = Doctrine::FETCH_OFFSET;
break;
case "lo":
case "lazyoffset":
$fetchmode = Doctrine::FETCH_LAZYOFFSET;
default:
throw new DQLException("Unknown fetchmode '$mode'. The availible fetchmodes are 'i', 'b' and 'l'.");
endswitch;
return $fetchmode;
}
/**
* trims brackets
*
* @param string $str
* @param string $e1 the first bracket, usually '('
* @param string $e2 the second bracket, usually ')'
*/
public static function bracketTrim($str,$e1,$e2) {
if(substr($str,0,1) == $e1 && substr($str,-1) == $e2)
return substr($str,1,-1);
else
return $str;
}
/**
* bracketExplode
* usage:
* $str = (age < 20 AND age > 18) AND email LIKE 'John@example.com'
* now exploding $str with parameters $d = ' AND ', $e1 = '(' and $e2 = ')'
* would return an array:
* array("(age < 20 AND age > 18)", "email LIKE 'John@example.com'")
*
* @param string $str
* @param string $d the delimeter which explodes the string
* @param string $e1 the first bracket, usually '('
* @param string $e2 the second bracket, usually ')'
*
*/
public static function bracketExplode($str,$d,$e1,$e2) {
$str = explode("$d",$str);
$i = 0;
$term = array();
foreach($str as $key=>$val) {
if (empty($term[$i])) {
$term[$i] = trim($val);
$s1 = substr_count($term[$i],"$e1");
$s2 = substr_count($term[$i],"$e2");
if($s1 == $s2) $i++;
} else {
$term[$i] .= "$d".trim($val);
$c1 = substr_count($term[$i],"$e1");
$c2 = substr_count($term[$i],"$e2");
if($c1 == $c2) $i++;
}
}
return $term;
}
2006-05-30 12:42:10 +04:00
/**
* generateAlias
*
* @param string $tableName
* @return string
*/
final public function generateAlias($tableName) {
2006-06-27 01:18:19 +04:00
if(isset($this->tableIndexes[$tableName])) {
return $tableName.++$this->tableIndexes[$tableName];
2006-06-27 01:18:19 +04:00
} else {
$this->tableIndexes[$tableName] = 1;
return $tableName;
}
}
/**
* getTableAlias
*
* @param string $path
* @return string
*/
final public function getTableAlias($path) {
if( ! isset($this->tableAliases[$path]))
return false;
return $this->tableAliases[$path];
}
/**
* loads a component
*
2006-05-30 12:42:10 +04:00
* @param string $path the path of the loadable component
* @param integer $fetchmode optional fetchmode, if not set the components default fetchmode will be used
* @throws DQLException
* @return Doctrine_Table
2006-05-30 12:42:10 +04:00
*/
final public function load($path, $loadFields = true) {
$e = preg_split("/[.:]/",$path);
$index = 0;
$currPath = '';
2006-05-30 12:42:10 +04:00
foreach($e as $key => $fullname) {
try {
$copy = $e;
$e2 = preg_split("/[-(]/",$fullname);
$name = $e2[0];
$currPath .= ".".$name;
2006-05-30 12:42:10 +04:00
if($key == 0) {
$currPath = substr($currPath,1);
2006-05-30 12:42:10 +04:00
$table = $this->session->getTable($name);
$tname = $table->getTableName();
2006-06-27 01:18:19 +04:00
if( ! isset($this->tableAliases[$currPath]))
$this->tableIndexes[$tname] = 1;
2006-05-30 12:42:10 +04:00
$this->parts["from"][$tname] = true;
$this->tableAliases[$currPath] = $tname;
$tableName = $tname;
2006-05-30 12:42:10 +04:00
} else {
$index += strlen($e[($key - 1)]) + 1;
// the mark here is either '.' or ':'
$mark = substr($path,($index - 1),1);
if(isset($this->tableAliases[$prevPath])) {
$tname = $this->tableAliases[$prevPath];
} else
$tname = $table->getTableName();
2006-05-30 12:42:10 +04:00
$fk = $table->getForeignKey($name);
$name = $fk->getTable()->getComponentName();
$original = $fk->getTable()->getTableName();
if(isset($this->tableAliases[$currPath])) {
$tname2 = $this->tableAliases[$currPath];
} else
$tname2 = $this->generateAlias($original);
2006-05-30 12:42:10 +04:00
if($original !== $tname2)
$aliasString = $original." AS ".$tname2;
else
$aliasString = $original;
2006-05-30 12:42:10 +04:00
switch($mark):
case ":":
$join = 'INNER JOIN ';
break;
case ".":
$join = 'LEFT JOIN ';
break;
default:
throw new Doctrine_Exception("Unknown operator '$mark'");
endswitch;
2006-05-30 12:42:10 +04:00
if($fk instanceof Doctrine_ForeignKey ||
$fk instanceof Doctrine_LocalKey) {
2006-06-26 22:55:42 +04:00
$this->parts["join"][$tname][$tname2] = $join.$aliasString." ON ".$tname.".".$fk->getLocal()." = ".$tname2.".".$fk->getForeign();
2006-05-30 12:42:10 +04:00
} elseif($fk instanceof Doctrine_Association) {
$asf = $fk->getAssociationFactory();
$assocTableName = $asf->getTableName();
2006-05-30 12:42:10 +04:00
$this->parts["join"][$tname][$assocTableName] = $join.$assocTableName." ON ".$tname.".id = ".$assocTableName.".".$fk->getLocal();
$this->parts["join"][$tname][$tname2] = $join.$aliasString." ON ".$tname2.".id = ".$assocTableName.".".$fk->getForeign();
2006-05-30 12:42:10 +04:00
}
$this->joins[$tname2] = $prevTable;
2006-05-30 12:42:10 +04:00
$table = $fk->getTable();
$this->tableAliases[$currPath] = $tname2;
2006-05-30 12:42:10 +04:00
$tableName = $tname2;
2006-05-30 12:42:10 +04:00
}
if( ! isset($this->tables[$tableName])) {
$this->tables[$tableName] = $table;
2006-05-30 12:42:10 +04:00
if($loadFields && ! $this->aggregate) {
$this->parseFields($fullname, $tableName, $e2, $currPath);
2006-05-30 12:42:10 +04:00
}
}
$prevPath = $currPath;
$prevTable = $tableName;
2006-05-30 12:42:10 +04:00
} catch(Exception $e) {
throw new DQLException($e->__toString());
2006-05-30 12:42:10 +04:00
}
}
return $table;
2006-05-30 12:42:10 +04:00
}
/**
* parseFields
*
* @param string $fullName
* @param string $tableName
* @param array $exploded
* @param string $currPath
* @return void
*/
final public function parseFields($fullName, $tableName, $exploded, $currPath) {
$table = $this->tables[$tableName];
$fields = array();
if(strpos($fullName, "-") === false) {
$fetchmode = $table->getAttribute(Doctrine::ATTR_FETCHMODE);
if(isset($exploded[1]))
$fields = explode(",",substr($exploded[1],0,-1));
} else {
if(isset($exploded[1])) {
$fetchmode = $this->parseFetchMode($exploded[1]);
} else
$fetchmode = $table->getAttribute(Doctrine::ATTR_FETCHMODE);
if(isset($exploded[2]))
$fields = explode(",",substr($exploded[2],0,-1));
}
$this->loadFields($table, $fetchmode, $fields, $currPath);
}
2006-05-30 12:42:10 +04:00
}
?>