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

147 lines
3.9 KiB
PHP
Raw Normal View History

2006-06-13 22:04:04 +04:00
<?php
2006-09-21 01:40:16 +04:00
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
2006-06-13 22:04:04 +04:00
/**
* Doctrine_View
*
* this class represents a database view
2006-09-21 01:40:16 +04:00
*
* @author Konsta Vesterinen
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
2006-06-13 22:04:04 +04:00
*/
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';
/**
2006-09-21 01:40:16 +04:00
* @var string $name the name of the view
2006-06-13 22:04:04 +04:00
*/
protected $name;
/**
2006-09-21 01:40:16 +04:00
* @var Doctrine_Query $query the DQL query object this view is hooked into
2006-06-13 22:04:04 +04:00
*/
protected $query;
/**
2006-09-21 01:40:16 +04:00
* @var Doctrine_Connection $conn the connection object
2006-06-13 22:04:04 +04:00
*/
2006-09-21 01:40:16 +04:00
protected $conn;
2006-06-13 22:04:04 +04:00
/**
* 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-09-21 01:40:16 +04:00
$this->conn = $query->getConnection();
2006-06-13 22:04:04 +04:00
}
/**
2006-09-21 01:40:16 +04:00
* getQuery
* returns the associated query object
2006-06-13 22:04:04 +04:00
*
* @return Doctrine_Query
*/
public function getQuery() {
return $this->query;
}
/**
2006-09-21 01:40:16 +04:00
* getName
2006-06-13 22:04:04 +04:00
* returns the name of this view
*
* @return string
*/
public function getName() {
return $this->name;
}
/**
2006-09-21 01:40:16 +04:00
* getConnection
* returns the connection object
2006-06-13 22:04:04 +04:00
*
2006-09-21 01:40:16 +04:00
* @return Doctrine_Connection
2006-06-13 22:04:04 +04:00
*/
2006-09-21 01:40:16 +04:00
public function getConnection() {
return $this->conn;
2006-06-13 22:04:04 +04:00
}
/**
2006-09-21 01:40:16 +04:00
* create
2006-06-13 22:04:04 +04:00
* creates this view
*
2006-09-21 01:40:16 +04:00
* @throws Doctrine_View_Exception
2006-06-13 22:04:04 +04:00
* @return void
*/
public function create() {
$sql = sprintf(self::CREATE, $this->name, $this->query->getQuery());
try {
2006-09-21 01:40:16 +04:00
$this->conn->getDBH()->query($sql);
} catch(Exception $e) {
throw new Doctrine_View_Exception($e->__toString());
}
2006-06-13 22:04:04 +04:00
}
/**
2006-09-21 01:40:16 +04:00
* drop
* drops this view from the database
2006-06-13 22:04:04 +04:00
*
2006-09-21 01:40:16 +04:00
* @throws Doctrine_View_Exception
2006-06-13 22:04:04 +04:00
* @return void
*/
public function drop() {
try {
2006-09-21 01:40:16 +04:00
$this->conn->getDBH()->query(sprintf(self::DROP, $this->name));
} catch(Exception $e) {
throw new Doctrine_View_Exception($e->__toString());
}
2006-06-13 22:04:04 +04:00
}
/**
2006-09-21 01:40:16 +04:00
* execute
2006-06-13 22:04:04 +04:00
* executes the view
2006-09-21 01:40:16 +04:00
* returns a collection of Doctrine_Record objects
*
2006-06-13 22:04:04 +04:00
* @return Doctrine_Collection
*/
public function execute() {
return $this->query->execute();
}
/**
2006-09-21 01:40:16 +04:00
* getSelectSql
* returns the select sql for this view
*
2006-06-13 22:04:04 +04:00
* @return string
*/
public function getSelectSql() {
return sprintf(self::SELECT, $this->name);
}
}