# Coverage for Doctrine_View

Back to coverage report

1 | <?php
2 | /*
3 | * $Id: View.php 2963 2007-10-21 06:23:59Z Jonathan.Wage $
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.phpdoctrine.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.phpdoctrine.org
32 | * @since 1.0
33 | * @version $Revision: 2963 $
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 | * constructor
69 | *
70 | * @param Doctrine_Query $query
71 | */
72 | public function __construct(Doctrine_Query $query, $viewName)
73 | {
74 | $this->name = $viewName;
75 | $this->query = $query;
76 | $this->query->setView($this);
77 | $this->conn = $query->getConnection();
78 | }
79 |
80 | /**
81 | * getQuery
82 | * returns the associated query object
83 | *
84 | * @return Doctrine_Query
85 | */
86 | public function getQuery()
87 | {
88 | return $this->query;
89 | }
90 |
91 | /**
92 | * getName
93 | * returns the name of this view
94 | *
95 | * @return string
96 | */
97 | public function getName()
98 | {
99 | return $this->name;
100 | }
101 |
102 | /**
103 | * getConnection
104 | * returns the connection object
105 | *
106 | * @return Doctrine_Connection
107 | */
108 | public function getConnection()
109 | {
110 | return $this->conn;
111 | }
112 |
113 | /**
114 | * create
115 | * creates this view
116 | *
117 | * @throws Doctrine_View_Exception
118 | * @return void
119 | */
120 | public function create()
121 | {
122 | $sql = sprintf(self::CREATE, $this->name, $this->query->getQuery());
123 | try {
124 | $this->conn->execute($sql);
125 | } catch(Doctrine_Exception $e) {
126 | throw new Doctrine_View_Exception($e->__toString());
127 | }
128 | }
129 |
130 | /**
131 | * drop
132 | * drops this view from the database
133 | *
134 | * @throws Doctrine_View_Exception
135 | * @return void
136 | */
137 | public function drop()
138 | {
139 | try {
140 | $this->conn->execute(sprintf(self::DROP, $this->name));
141 | } catch(Doctrine_Exception $e) {
142 | throw new Doctrine_View_Exception($e->__toString());
143 | }
144 | }
145 |
146 | /**
147 | * execute
148 | * executes the view
149 | * returns a collection of Doctrine_Record objects
150 | *
151 | * @return Doctrine_Collection
152 | */
153 | public function execute()
154 | {
155 | return $this->query->execute();
156 | }
157 |
158 | /**
159 | * getSelectSql
160 | * returns the select sql for this view
161 | *
162 | * @return string
163 | */
164 | public function getSelectSql()
165 | {
166 | return sprintf(self::SELECT, $this->name);
167 | }
168 | }