Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: View.php 7490 2010-03-29 19:53:27Z jwage $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * Doctrine_View
24
 *
25
 * this class represents a database view
26
 *
27
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
28
 * @package     Doctrine
29
 * @subpackage  View
30
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
31
 * @link        www.doctrine-project.org
32
 * @since       1.0
33
 * @version     $Revision: 7490 $
34
 */
35
class Doctrine_View
36
{
37
    /**
38
     * SQL DROP constant
39
     */
40
    const DROP   = 'DROP VIEW %s';
41
 
42
    /**
43
     * SQL CREATE constant
44
     */
45
    const CREATE = 'CREATE VIEW %s AS %s';
46
 
47
    /**
48
     * SQL SELECT constant
49
     */
50
    const SELECT = 'SELECT * FROM %s';
51
 
52
    /**
53
     * @var string $name                the name of the view
54
     */
55
    protected $_name;
56
 
57
    /**
58
     * @var Doctrine_Query $query       the DQL query object this view is hooked into
59
     */
60
    protected $_query;
61
 
62
    /**
63
     * @var Doctrine_Connection $conn   the connection object
64
     */
65
    protected $_conn;
66
 
67
    /**
68
     * @var string $_dql The view dql string
69
     */
70
    protected $_dql;
71
 
72
    /**
73
     * @var string $_sql The view sql string
74
     */
75
    protected $_sql;
76
 
77
    /**
78
     * constructor
79
     *
80
     * @param Doctrine_Query $query
81
     */
82
    public function __construct(Doctrine_Query $query, $viewName)
83
    {
84
        $this->_name  = $viewName;
85
        $this->_query = $query;
86
        $this->_query->setView($this);
87
        $this->_conn   = $query->getConnection();
88
        $this->_dql = $query->getDql();
89
        $this->_sql = $query->getSqlQuery();
90
    }
91
 
92
    /**
93
     * returns the associated query object
94
     *
95
     * @return Doctrine_Query
96
     */
97
    public function getQuery()
98
    {
99
        return $this->_query;
100
    }
101
 
102
    /**
103
     * returns the name of this view
104
     *
105
     * @return string
106
     */
107
    public function getName()
108
    {
109
        return $this->_name;
110
    }
111
 
112
    /**
113
     * returns the connection object
114
     *
115
     * @return Doctrine_Connection
116
     */
117
    public function getConnection()
118
    {
119
        return $this->_conn;
120
    }
121
 
122
    /**
123
     * creates this view
124
     *
125
     * @throws Doctrine_View_Exception
126
     * @return void
127
     */
128
    public function create()
129
    {
130
        $sql = sprintf(self::CREATE, $this->_name, $this->_query->getSqlQuery());
131
        try {
132
            $this->_conn->execute($sql, $this->_query->getFlattenedParams());
133
        } catch(Doctrine_Exception $e) {
134
            throw new Doctrine_View_Exception($e->__toString());
135
        }
136
    }
137
 
138
    /**
139
     * drops this view from the database
140
     *
141
     * @throws Doctrine_View_Exception
142
     * @return void
143
     */
144
    public function drop()
145
    {
146
        try {
147
            $this->_conn->execute(sprintf(self::DROP, $this->_name));
148
        } catch(Doctrine_Exception $e) {
149
            throw new Doctrine_View_Exception($e->__toString());
150
        }
151
    }
152
 
153
    /**
154
     * returns a collection of Doctrine_Record objects
155
     *
156
     * @return Doctrine_Collection
157
     */
158
    public function execute()
159
    {
160
        return $this->_query->execute();
161
    }
162
 
163
    /**
164
     * returns the select sql for this view
165
     *
166
     * @return string
167
     */
168
    public function getSelectSql()
169
    {
170
        return sprintf(self::SELECT, $this->_name);
171
    }
172
 
173
    /**
174
     * Get the view sql string
175
     *
176
     * @return string $sql
177
     */
178
    public function getViewSql()
179
    {
180
        return $this->_sql;
181
    }
182
 
183
    /**
184
     * Get the view dql string
185
     *
186
     * @return string $dql
187
     */
188
    public function getViewDql()
189
    {
190
        return $this->_dql;
191
    }
192
}