# Source for file Import.php

Documentation is available at Import.php

1. <?php
2. /*
3. * $Id: Import.php 2255 2007-08-16 22:42:35Z 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.com>.
20. */
21. Doctrine::autoload('Doctrine_Connection_Module');
22. /**
23. * class Doctrine_Import
24. * Main responsible of performing import operation. Delegates database schema
25. * reading to a reader object and passes the result to a builder object which
26. * builds a Doctrine data model.
27. *
28. * @package Doctrine
29. * @category Object Relational Mapping
30. * @link www.phpdoctrine.com
31. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
32. * @since 1.0
33. * @version $Revision: 2255 $
34. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
35. * @author Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
36. */
37. class Doctrine_Import extends Doctrine_Connection_Module
38. {
39. protected $sql = array();
40. /**
41. * lists all databases
42. *
43. * @return array
44. */
45. public function listDatabases()
46. {
47. if ( ! isset($this->sql['listDatabases'])) {
48. throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
49. }
51. return $this->conn->fetchColumn($this->sql['listDatabases']);
52. }
53. /**
54. * lists all availible database functions
55. *
56. * @return array
57. */
58. public function listFunctions()
59. {
60. if ( ! isset($this->sql['listFunctions'])) {
61. throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
62. }
64. return $this->conn->fetchColumn($this->sql['listFunctions']);
65. }
66. /**
67. * lists all database triggers
68. *
69. * @param string|null$database
70. * @return array
71. */
72. public function listTriggers($database = null)
73. {
74. throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
75. }
76. /**
77. * lists all database sequences
78. *
79. * @param string|null$database
80. * @return array
81. */
82. public function listSequences($database = null)
83. {
84. if ( ! isset($this->sql['listSequences'])) {
85. throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
86. }
88. return $this->conn->fetchColumn($this->sql['listSequences']);
89. }
90. /**
91. * lists table constraints
92. *
93. * @param string $table database table name
94. * @return array
95. */
96. public function listTableConstraints($table)
97. {
98. throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
99. }
100. /**
101. * lists table constraints
102. *
103. * @param string $table database table name
104. * @return array
105. */
106. public function listTableColumns($table)
107. {
108. throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
109. }
110. /**
111. * lists table constraints
112. *
113. * @param string $table database table name
114. * @return array
115. */
116. public function listTableIndexes($table)
117. {
118. throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
119. }
120. /**
121. * lists tables
122. *
123. * @param string|null$database
124. * @return array
125. */
126. public function listTables($database = null)
127. {
128. throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
129. }
130. /**
131. * lists table triggers
132. *
133. * @param string $table database table name
134. * @return array
135. */
136. public function listTableTriggers($table)
137. {
138. throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
139. }
140. /**
141. * lists table views
142. *
143. * @param string $table database table name
144. * @return array
145. */
146. public function listTableViews($table)
147. {
148. throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
149. }
150. /**
151. * lists database users
152. *
153. * @return array
154. */
155. public function listUsers()
156. {
157. if ( ! isset($this->sql['listUsers'])) {
158. throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
159. }
161. return $this->conn->fetchColumn($this->sql['listUsers']);
162. }
163. /**
164. * lists database views
165. *
166. * @param string|null$database
167. * @return array
168. */
169. public function listViews($database = null)
170. {
171. if ( ! isset($this->sql['listViews'])) {
172. throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
173. }
175. return $this->conn->fetchColumn($this->sql['listViews']);
176. }
177. /**
178. * importSchema
179. *
180. * method for importing existing schema to Doctrine_Record classes
181. *
182. * @param string $directory
183. * @param array $databases
184. * @return array the names of the imported classes
185. */
186. public function importSchema($directory, array $databases = array())
187. {
188. $builder = new Doctrine_Import_Builder();
189. $builder->setTargetPath($directory);
191. $classes = array();
192. foreach ($this->listTables() as $table) {
193. $builder->buildRecord(array('tableName' => $table,
194. 'className' => Doctrine::classify($table)),
195. $this->listTableColumns($table),
196. array());
198. $classes[] = Doctrine::classify($table);
199. }
201. return $classes;
202. }
203. }