# Source for file View.php

Documentation is available at View.php

1. <?php
2. /*
3. * $Id: View.php 1080 2007-02-10 18:17:08Z romanb $
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.com>.
20. */
21. /**
22. * Doctrine_View
23. *
24. * this class represents a database view
25. *
26. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
27. * @package Doctrine
28. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
29. * @category Object Relational Mapping
30. * @link www.phpdoctrine.com
31. * @since 1.0
32. * @version $Revision: 1080 $
33. */
34. class Doctrine_View
35. {
36. /**
37. * SQL DROP constant
38. */
39. const DROP = 'DROP VIEW %s';
40. /**
41. * SQL CREATE constant
42. */
43. const CREATE = 'CREATE VIEW %s AS %s';
44. /**
45. * SQL SELECT constant
46. */
47. const SELECT = 'SELECT * FROM %s';
49. /**
50. * @var string $name the name of the view
51. */
52. protected $name;
53. /**
54. * @var Doctrine_Query $query the DQL query object this view is hooked into
55. */
56. protected $query;
57. /**
58. * @var Doctrine_Connection $conn the connection object
59. */
60. protected $conn;
62. /**
63. * constructor
64. *
65. * @param Doctrine_Query $query
66. */
67. public function __construct(Doctrine_Query $query, $viewName)
68. {
69. $this->name = $viewName;
70. $this->query = $query;
71. $this->query->setView($this);
72. $this->conn = $query->getConnection();
73. }
74. /**
75. * getQuery
76. * returns the associated query object
77. *
78. * @return Doctrine_Query
79. */
80. public function getQuery()
81. {
82. return $this->query;
83. }
84. /**
85. * getName
86. * returns the name of this view
87. *
88. * @return string
89. */
90. public function getName()
91. {
92. return $this->name;
93. }
94. /**
95. * getConnection
96. * returns the connection object
97. *
98. * @return Doctrine_Connection
99. */
100. public function getConnection()
101. {
102. return $this->conn;
103. }
104. /**
105. * create
106. * creates this view
107. *
108. * @throws Doctrine_View_Exception
109. * @return void
110. */
111. public function create()
112. {
113. $sql = sprintf(self::CREATE, $this->name, $this->query->getQuery());
114. try {
115. $this->conn->execute($sql);
116. } catch(Doctrine_Exception $e) {
117. throw new Doctrine_View_Exception($e->__toString());
118. }
119. }
120. /**
121. * drop
122. * drops this view from the database
123. *
124. * @throws Doctrine_View_Exception
125. * @return void
126. */
127. public function drop()
128. {
129. try {
130. $this->conn->execute(sprintf(self::DROP, $this->name));
131. } catch(Doctrine_Exception $e) {
132. throw new Doctrine_View_Exception($e->__toString());
133. }
134. }
135. /**
136. * execute
137. * executes the view
138. * returns a collection of Doctrine_Record objects
139. *
140. * @return Doctrine_Collection
141. */
142. public function execute()
143. {
144. return $this->query->execute();
145. }
146. /**
147. * getSelectSql
148. * returns the select sql for this view
149. *
150. * @return string
151. */
152. public function getSelectSql()
153. {
154. return sprintf(self::SELECT, $this->name);
155. }
156. }