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

168 lines
4.1 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 <kvesteri@cc.hut.fi>
* @package Doctrine
* @subpackage View
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
2006-12-29 17:40:47 +03:00
class Doctrine_View
{
2006-06-13 22:04:04 +04:00
/**
* SQL DROP constant
*/
const DROP = 'DROP VIEW %s';
2006-06-13 22:04:04 +04:00
/**
* SQL CREATE constant
*/
const CREATE = 'CREATE VIEW %s AS %s';
2006-06-13 22:04:04 +04:00
/**
* 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-06-13 22:04:04 +04:00
/**
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-06-13 22:04:04 +04:00
/**
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-12-29 17:40:47 +03:00
public function __construct(Doctrine_Query $query, $viewName)
{
2006-08-21 15:18:28 +04:00
$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-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
*/
2006-12-29 17:40:47 +03:00
public function getQuery()
{
2006-06-13 22:04:04 +04:00
return $this->query;
}
2006-06-13 22:04:04 +04:00
/**
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
*/
2006-12-29 17:40:47 +03:00
public function getName()
{
2006-06-13 22:04:04 +04:00
return $this->name;
}
2006-06-13 22:04:04 +04:00
/**
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-12-29 17:40:47 +03:00
public function getConnection()
{
2006-09-21 01:40:16 +04:00
return $this->conn;
2006-06-13 22:04:04 +04:00
}
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
*/
2006-12-29 17:40:47 +03:00
public function create()
{
2006-06-13 22:04:04 +04:00
$sql = sprintf(self::CREATE, $this->name, $this->query->getQuery());
try {
$this->conn->execute($sql);
} catch(Doctrine_Exception $e) {
throw new Doctrine_View_Exception($e->__toString());
}
2006-06-13 22:04:04 +04:00
}
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
*/
2006-12-29 17:40:47 +03:00
public function drop()
{
try {
$this->conn->execute(sprintf(self::DROP, $this->name));
} catch(Doctrine_Exception $e) {
throw new Doctrine_View_Exception($e->__toString());
}
2006-06-13 22:04:04 +04:00
}
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
*/
2006-12-29 17:40:47 +03:00
public function execute()
{
2006-06-13 22:04:04 +04:00
return $this->query->execute();
}
2006-06-13 22:04:04 +04:00
/**
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
*/
2006-12-29 17:40:47 +03:00
public function getSelectSql()
{
2006-06-13 22:04:04 +04:00
return sprintf(self::SELECT, $this->name);
}
}