* @package Mammut\DB\Sql */ class View extends StrictObject implements iSql { protected $root = NULL; protected $database = NULL; protected $schema = NULL; protected $name = NULL; protected $select = NULL; /** * Creates a new SQL view model object. * @param Select $select the SELECT statement which this view should be based on, or NULL * @param string $name the name of the view * @param string $schema the schema/namespace of the object * @param string $database the database * @param string $root the server element in linked server environments */ public function __construct(Select $select, $name, $schema = NULL, $database = NULL, $root = NULL) { if (is_array($name)) { if (is_string(key($name)) && count($name) == 1) $this->setTable(current($name)); else { $i = count($name) - 1; $this->setName($name[$i--]); $this->setSchema($i >=0 ? $name[$i--] : NULL); $this->setDatabase($i >=0 ? $name[$i--] : NULL); $this->setRoot($i >=0 ? $name[$i--] : NULL); } } else { $this->setRoot($root); $this->setDatabase($database); $this->setSchema($schema); $this->setName($name); } if (!is_null($select)) $this->setSelect($select); } /** * * @param string $root * the database root, possibly the name of the linked server */ public function setRoot($root) { $this->root = $root; } /** * * @return string */ public function getRoot() { return $this->root; } /** * * @param string $schema * the schema of the table */ public function setDatabase($database) { $this->database = $database; } /** * * @return string */ public function getDatabase() { return $this->database; } /** * * @param string $schema * the schema of the table */ public function setSchema($schema) { $this->schema = $schema; } /** * * @return string */ public function getSchema() { return $this->schema; } /** * * @param string $name */ public function setName($name) { if (!is_string($name)) throw new \InvalidArgumentException('$name has to be a string'); $this->name = $name; } /** * * @return string */ public function getName() { return $this->name; } /** * * @param string $name */ public function setSelect(Select $select) { if (is_null($select)) throw new \InvalidArgumentException('$$select has to be not NULL'); $this->select = $select; } /** * * @return string */ public function getSelect() { return $this->table; } public function getSql(iDialect $dialect = NULL) { $tbl = array(); if($this->root) $tbl[] = $this->root; if($this->database) $tbl[] = $this->database; if($this->schema) $tbl[] = $this->schema; $tbl[] = $this->table; return $dialect->quoteIdent($tbl); } }