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
|
2008-01-23 01:52:53 +03:00
|
|
|
* <http://www.phpdoctrine.org>.
|
2006-09-21 01:40:16 +04:00
|
|
|
*/
|
2008-05-30 16:09:24 +04:00
|
|
|
|
|
|
|
#namespace Doctrine::ORM::?;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-06-13 22:04:04 +04:00
|
|
|
/**
|
|
|
|
* Doctrine_View
|
|
|
|
*
|
|
|
|
* this class represents a database view
|
2006-09-21 01:40:16 +04:00
|
|
|
*
|
2006-12-28 00:35:32 +03:00
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage View
|
2006-12-28 00:35:32 +03:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
2008-02-22 21:11:35 +03:00
|
|
|
* @link www.phpdoctrine.org
|
2006-12-28 00:35:32 +03:00
|
|
|
* @since 1.0
|
2008-05-30 16:09:24 +04:00
|
|
|
* @version $Revision$
|
|
|
|
* @todo Maybe needs a reworked implementation and a new place.
|
2006-12-28 00:35:32 +03:00
|
|
|
*/
|
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';
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2006-06-13 22:04:04 +04:00
|
|
|
/**
|
|
|
|
* SQL CREATE constant
|
|
|
|
*/
|
|
|
|
const CREATE = 'CREATE VIEW %s AS %s';
|
2007-10-21 10:23:59 +04:00
|
|
|
|
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;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
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;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
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
|
|
|
}
|
2007-10-21 10:23:59 +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;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
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;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
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
|
|
|
}
|
2007-10-21 10:23:59 +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());
|
2006-06-14 23:17:38 +04:00
|
|
|
try {
|
2006-12-28 00:35:32 +03:00
|
|
|
$this->conn->execute($sql);
|
|
|
|
} catch(Doctrine_Exception $e) {
|
2006-06-14 23:17:38 +04:00
|
|
|
throw new Doctrine_View_Exception($e->__toString());
|
|
|
|
}
|
2006-06-13 22:04:04 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +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()
|
|
|
|
{
|
2006-06-14 23:17:38 +04:00
|
|
|
try {
|
2006-12-28 00:35:32 +03:00
|
|
|
$this->conn->execute(sprintf(self::DROP, $this->name));
|
|
|
|
} catch(Doctrine_Exception $e) {
|
2006-06-14 23:17:38 +04:00
|
|
|
throw new Doctrine_View_Exception($e->__toString());
|
|
|
|
}
|
2006-06-13 22:04:04 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +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
|
2008-05-14 01:20:34 +04:00
|
|
|
* returns a collection of Doctrine_Entity objects
|
2006-09-21 01:40:16 +04:00
|
|
|
*
|
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();
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
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);
|
|
|
|
}
|
2007-10-04 01:43:22 +04:00
|
|
|
}
|