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

112 lines
2.3 KiB
PHP
Raw Normal View History

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());
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() {
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);
}
}