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 25
/**
 * Doctrine_View
 *
 * this class represents a database view
zYne's avatar
zYne committed
26
 *
27 28
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @package     Doctrine
29
 * @subpackage  View
30 31 32 33 34
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 */
lsmith's avatar
lsmith committed
35 36
class Doctrine_View
{
37 38 39 40
    /**
     * SQL DROP constant
     */
    const DROP   = 'DROP VIEW %s';
41

42 43 44 45
    /**
     * SQL CREATE constant
     */
    const CREATE = 'CREATE VIEW %s AS %s';
46

47 48 49 50 51 52
    /**
     * SQL SELECT constant
     */
    const SELECT = 'SELECT * FROM %s';

    /**
zYne's avatar
zYne committed
53
     * @var string $name                the name of the view
54 55
     */
    protected $name;
56

57
    /**
zYne's avatar
zYne committed
58
     * @var Doctrine_Query $query       the DQL query object this view is hooked into
59 60
     */
    protected $query;
61

62
    /**
zYne's avatar
zYne committed
63
     * @var Doctrine_Connection $conn   the connection object
64
     */
zYne's avatar
zYne committed
65
    protected $conn;
66 67 68 69 70 71

    /**
     * constructor
     *
     * @param Doctrine_Query $query
     */
lsmith's avatar
lsmith committed
72 73
    public function __construct(Doctrine_Query $query, $viewName)
    {
zYne's avatar
zYne committed
74
        $this->name  = $viewName;
75 76
        $this->query = $query;
        $this->query->setView($this);
zYne's avatar
zYne committed
77
        $this->conn   = $query->getConnection();
78
    }
79

80
    /**
zYne's avatar
zYne committed
81 82
     * getQuery
     * returns the associated query object
83 84 85
     *
     * @return Doctrine_Query
     */
lsmith's avatar
lsmith committed
86 87
    public function getQuery()
    {
88 89
        return $this->query;
    }
90

91
    /**
zYne's avatar
zYne committed
92
     * getName
93 94 95 96
     * returns the name of this view
     *
     * @return string
     */
lsmith's avatar
lsmith committed
97 98
    public function getName()
    {
99 100
        return $this->name;
    }
101

102
    /**
zYne's avatar
zYne committed
103 104
     * getConnection
     * returns the connection object
105
     *
zYne's avatar
zYne committed
106
     * @return Doctrine_Connection
107
     */
lsmith's avatar
lsmith committed
108 109
    public function getConnection()
    {
zYne's avatar
zYne committed
110
        return $this->conn;
111
    }
112

113
    /**
zYne's avatar
zYne committed
114
     * create
115 116
     * creates this view
     *
zYne's avatar
zYne committed
117
     * @throws Doctrine_View_Exception
118 119
     * @return void
     */
lsmith's avatar
lsmith committed
120 121
    public function create()
    {
122
        $sql = sprintf(self::CREATE, $this->name, $this->query->getQuery());
123
        try {
124 125
            $this->conn->execute($sql);
        } catch(Doctrine_Exception $e) {
126 127
            throw new Doctrine_View_Exception($e->__toString());
        }
128
    }
129

130
    /**
zYne's avatar
zYne committed
131 132
     * drop
     * drops this view from the database
133
     *
zYne's avatar
zYne committed
134
     * @throws Doctrine_View_Exception
135 136
     * @return void
     */
lsmith's avatar
lsmith committed
137 138
    public function drop()
    {
139
        try {
140 141
            $this->conn->execute(sprintf(self::DROP, $this->name));
        } catch(Doctrine_Exception $e) {
142 143
            throw new Doctrine_View_Exception($e->__toString());
        }
144
    }
145

146
    /**
zYne's avatar
zYne committed
147
     * execute
148
     * executes the view
zYne's avatar
zYne committed
149 150
     * returns a collection of Doctrine_Record objects
     *
151 152
     * @return Doctrine_Collection
     */
lsmith's avatar
lsmith committed
153 154
    public function execute()
    {
155 156
        return $this->query->execute();
    }
157

158
    /**
zYne's avatar
zYne committed
159 160 161
     * getSelectSql
     * returns the select sql for this view
     *
162 163
     * @return string
     */
lsmith's avatar
lsmith committed
164 165
    public function getSelectSql()
    {
166 167
        return sprintf(self::SELECT, $this->name);
    }
168
}