2006-06-13 22:04:04 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Doctrine_View
|
|
|
|
*
|
|
|
|
* this class represents a database view
|
|
|
|
*/
|
|
|
|
class Doctrine_View {
|
|
|
|
/**
|
|
|
|
* SQL DROP constant
|
|
|
|
*/
|
|
|
|
const DROP = 'DROP VIEW %s';
|
|
|
|
/**
|
|
|
|
* SQL CREATE constant
|
|
|
|
*/
|
|
|
|
const CREATE = 'CREATE VIEW %s AS %s';
|
|
|
|
/**
|
|
|
|
* SQL SELECT constant
|
|
|
|
*/
|
|
|
|
const SELECT = 'SELECT * FROM %s';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string $name
|
|
|
|
*/
|
|
|
|
protected $name;
|
|
|
|
/**
|
|
|
|
* @var Doctrine_Query $query
|
|
|
|
*/
|
|
|
|
protected $query;
|
|
|
|
/**
|
|
|
|
* @var PDO $dbh
|
|
|
|
*/
|
|
|
|
protected $dbh;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
*
|
|
|
|
* @param Doctrine_Query $query
|
|
|
|
*/
|
2006-08-21 15:18:28 +04:00
|
|
|
public function __construct(Doctrine_Query $query, $viewName) {
|
|
|
|
$this->name = $viewName;
|
2006-06-13 22:04:04 +04:00
|
|
|
$this->query = $query;
|
|
|
|
$this->query->setView($this);
|
2006-08-22 03:19:15 +04:00
|
|
|
$this->dbh = $query->getConnection()->getDBH();
|
2006-06-13 22:04:04 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* simple get method for getting
|
|
|
|
* the associated query object
|
|
|
|
*
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
|
|
|
public function getQuery() {
|
|
|
|
return $this->query;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns the name of this view
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName() {
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns the database handler
|
|
|
|
*
|
|
|
|
* @return PDO
|
|
|
|
*/
|
|
|
|
public function getDBH() {
|
|
|
|
return $this->dbh;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* creates this view
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function create() {
|
|
|
|
$sql = sprintf(self::CREATE, $this->name, $this->query->getQuery());
|
2006-06-14 23:17:38 +04:00
|
|
|
try {
|
|
|
|
$this->dbh->query($sql);
|
|
|
|
} catch(Exception $e) {
|
|
|
|
throw new Doctrine_View_Exception($e->__toString());
|
|
|
|
}
|
2006-06-13 22:04:04 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* drops this view
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function drop() {
|
2006-06-14 23:17:38 +04:00
|
|
|
try {
|
|
|
|
$this->dbh->query(sprintf(self::DROP, $this->name));
|
|
|
|
} catch(Exception $e) {
|
|
|
|
throw new Doctrine_View_Exception($e->__toString());
|
|
|
|
}
|
2006-06-13 22:04:04 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* executes the view
|
|
|
|
*
|
|
|
|
* @return Doctrine_Collection
|
|
|
|
*/
|
|
|
|
public function execute() {
|
|
|
|
return $this->query->execute();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSelectSql() {
|
|
|
|
return sprintf(self::SELECT, $this->name);
|
|
|
|
}
|
|
|
|
}
|
2006-09-04 02:46:30 +04:00
|
|
|
|