# Coverage for Doctrine_Import

Back to coverage report

1 | <?php
2 | /*
3 | * $Id: Import.php 3062 2007-11-01 23:54:27Z 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 | 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 | * @subpackage Import
30 | * @link www.phpdoctrine.org
31 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
32 | * @since 1.0
33 | * @version $Revision: 3062 $
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 | /**
42 | * lists all databases
43 | *
44 | * @return array
45 | */
46 | public function listDatabases()
47 | {
48 | if ( ! isset($this->sql['listDatabases'])) {
49 | throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
50 | }
51 |
52 | return $this->conn->fetchColumn($this->sql['listDatabases']);
53 | }
54 |
55 | /**
56 | * lists all availible database functions
57 | *
58 | * @return array
59 | */
60 | public function listFunctions()
61 | {
62 | if ( ! isset($this->sql['listFunctions'])) {
63 | throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
64 | }
65 |
66 | return $this->conn->fetchColumn($this->sql['listFunctions']);
67 | }
68 |
69 | /**
70 | * lists all database triggers
71 | *
72 | * @param string|null $database
73 | * @return array
74 | */
75 | public function listTriggers($database = null)
76 | {
77 | throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
78 | }
79 |
80 | /**
81 | * lists all database sequences
82 | *
83 | * @param string|null $database
84 | * @return array
85 | */
86 | public function listSequences($database = null)
87 | {
88 | if ( ! isset($this->sql['listSequences'])) {
89 | throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
90 | }
91 |
92 | return $this->conn->fetchColumn($this->sql['listSequences']);
93 | }
94 |
95 | /**
96 | * lists table constraints
97 | *
98 | * @param string $table database table name
99 | * @return array
100 | */
101 | public function listTableConstraints($table)
102 | {
103 | throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
104 | }
105 |
106 | /**
107 | * lists table constraints
108 | *
109 | * @param string $table database table name
110 | * @return array
111 | */
112 | public function listTableColumns($table)
113 | {
114 | throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
115 | }
116 |
117 | /**
118 | * lists table constraints
119 | *
120 | * @param string $table database table name
121 | * @return array
122 | */
123 | public function listTableIndexes($table)
124 | {
125 | throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
126 | }
127 |
128 | /**
129 | * lists tables
130 | *
131 | * @param string|null $database
132 | * @return array
133 | */
134 | public function listTables($database = null)
135 | {
136 | throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
137 | }
138 |
139 | /**
140 | * lists table triggers
141 | *
142 | * @param string $table database table name
143 | * @return array
144 | */
145 | public function listTableTriggers($table)
146 | {
147 | throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
148 | }
149 |
150 | /**
151 | * lists table views
152 | *
153 | * @param string $table database table name
154 | * @return array
155 | */
156 | public function listTableViews($table)
157 | {
158 | throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
159 | }
160 |
161 | /**
162 | * lists database users
163 | *
164 | * @return array
165 | */
166 | public function listUsers()
167 | {
168 | if ( ! isset($this->sql['listUsers'])) {
169 | throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
170 | }
171 |
172 | return $this->conn->fetchColumn($this->sql['listUsers']);
173 | }
174 |
175 | /**
176 | * lists database views
177 | *
178 | * @param string|null $database
179 | * @return array
180 | */
181 | public function listViews($database = null)
182 | {
183 | if ( ! isset($this->sql['listViews'])) {
184 | throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
185 | }
186 |
187 | return $this->conn->fetchColumn($this->sql['listViews']);
188 | }
189 |
190 | /**
191 | * importSchema
192 | *
193 | * method for importing existing schema to Doctrine_Record classes
194 | *
195 | * @param string $directory
196 | * @param array $databases
197 | * @return array the names of the imported classes
198 | */
199 | public function importSchema($directory, array $databases = array(), array $options = array())
200 | {
201 | $connections = Doctrine_Manager::getInstance()->getConnections();
202 |
203 | foreach ($connections as $name => $connection) {
204 | // Limit the databases to the ones specified by $databases.
205 | // Check only happens if array is not empty
206 | if ( ! empty($databases) && !in_array($name, $databases)) {
207 | continue;
208 | }
209 |
210 | $builder = new Doctrine_Import_Builder();
211 | $builder->setTargetPath($directory);
212 | $builder->setOptions($options);
213 |
214 | $classes = array();
215 | foreach ($connection->import->listTables() as $table) {
216 | $builder->buildRecord(array('tableName' => $table,
217 | 'className' => Doctrine::classify($table)),
218 | $connection->import->listTableColumns($table),
219 | array());
220 |
221 | $classes[] = Doctrine::classify($table);
222 | }
223 | }
224 |
225 | return $classes;
226 | }
227 | }