View.php 4.06 KB
Newer Older
1
<?php
zYne's avatar
zYne committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 *  $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>.
 */
21 22 23 24
/**
 * Doctrine_View
 *
 * this class represents a database view
zYne's avatar
zYne committed
25
 *
26 27 28 29 30 31 32 33
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 */
lsmith's avatar
lsmith committed
34 35
class Doctrine_View
{
36 37 38 39 40 41 42 43 44 45 46 47 48 49
    /**
     * 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';

    /**
zYne's avatar
zYne committed
50
     * @var string $name                the name of the view
51 52 53
     */
    protected $name;
    /**
zYne's avatar
zYne committed
54
     * @var Doctrine_Query $query       the DQL query object this view is hooked into
55 56 57
     */
    protected $query;
    /**
zYne's avatar
zYne committed
58
     * @var Doctrine_Connection $conn   the connection object
59
     */
zYne's avatar
zYne committed
60
    protected $conn;
61 62 63 64 65 66

    /**
     * constructor
     *
     * @param Doctrine_Query $query
     */
lsmith's avatar
lsmith committed
67 68
    public function __construct(Doctrine_Query $query, $viewName)
    {
zYne's avatar
zYne committed
69
        $this->name  = $viewName;
70 71
        $this->query = $query;
        $this->query->setView($this);
zYne's avatar
zYne committed
72
        $this->conn   = $query->getConnection();
73 74
    }
    /**
zYne's avatar
zYne committed
75 76
     * getQuery
     * returns the associated query object
77 78 79
     *
     * @return Doctrine_Query
     */
lsmith's avatar
lsmith committed
80 81
    public function getQuery()
    {
82 83 84
        return $this->query;
    }
    /**
zYne's avatar
zYne committed
85
     * getName
86 87 88 89
     * returns the name of this view
     *
     * @return string
     */
lsmith's avatar
lsmith committed
90 91
    public function getName()
    {
92 93 94
        return $this->name;
    }
    /**
zYne's avatar
zYne committed
95 96
     * getConnection
     * returns the connection object
97
     *
zYne's avatar
zYne committed
98
     * @return Doctrine_Connection
99
     */
lsmith's avatar
lsmith committed
100 101
    public function getConnection()
    {
zYne's avatar
zYne committed
102
        return $this->conn;
103 104
    }
    /**
zYne's avatar
zYne committed
105
     * create
106 107
     * creates this view
     *
zYne's avatar
zYne committed
108
     * @throws Doctrine_View_Exception
109 110
     * @return void
     */
lsmith's avatar
lsmith committed
111 112
    public function create()
    {
113
        $sql = sprintf(self::CREATE, $this->name, $this->query->getQuery());
114
        try {
115 116
            $this->conn->execute($sql);
        } catch(Doctrine_Exception $e) {
117 118
            throw new Doctrine_View_Exception($e->__toString());
        }
119 120
    }
    /**
zYne's avatar
zYne committed
121 122
     * drop
     * drops this view from the database
123
     *
zYne's avatar
zYne committed
124
     * @throws Doctrine_View_Exception
125 126
     * @return void
     */
lsmith's avatar
lsmith committed
127 128
    public function drop()
    {
129
        try {
130 131
            $this->conn->execute(sprintf(self::DROP, $this->name));
        } catch(Doctrine_Exception $e) {
132 133
            throw new Doctrine_View_Exception($e->__toString());
        }
134 135
    }
    /**
zYne's avatar
zYne committed
136
     * execute
137
     * executes the view
zYne's avatar
zYne committed
138 139
     * returns a collection of Doctrine_Record objects
     *
140 141
     * @return Doctrine_Collection
     */
lsmith's avatar
lsmith committed
142 143
    public function execute()
    {
144 145 146
        return $this->query->execute();
    }
    /**
zYne's avatar
zYne committed
147 148 149
     * getSelectSql
     * returns the select sql for this view
     *
150 151
     * @return string
     */
lsmith's avatar
lsmith committed
152 153
    public function getSelectSql()
    {
154 155 156
        return sprintf(self::SELECT, $this->name);
    }
}