|
1 <?php |
|
2 /* |
|
3 * $Id$ |
|
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 namespace Doctrine\DBAL\Schema; |
|
23 |
|
24 use Doctrine\DBAL\Schema\Visitor\CreateSchemaSqlCollector; |
|
25 use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector; |
|
26 use Doctrine\DBAL\Schema\Visitor\Visitor; |
|
27 |
|
28 /** |
|
29 * Object representation of a database schema |
|
30 * |
|
31 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
|
32 * @link www.doctrine-project.org |
|
33 * @since 2.0 |
|
34 * @version $Revision$ |
|
35 * @author Benjamin Eberlei <kontakt@beberlei.de> |
|
36 */ |
|
37 class Schema extends AbstractAsset |
|
38 { |
|
39 /** |
|
40 * @var array |
|
41 */ |
|
42 protected $_tables = array(); |
|
43 |
|
44 /** |
|
45 * @var array |
|
46 */ |
|
47 protected $_sequences = array(); |
|
48 |
|
49 /** |
|
50 * @var SchemaConfig |
|
51 */ |
|
52 protected $_schemaConfig = false; |
|
53 |
|
54 /** |
|
55 * @param array $tables |
|
56 * @param array $sequences |
|
57 * @param array $views |
|
58 * @param array $triggers |
|
59 * @param SchemaConfig $schemaConfig |
|
60 */ |
|
61 public function __construct(array $tables=array(), array $sequences=array(), SchemaConfig $schemaConfig=null) |
|
62 { |
|
63 if ($schemaConfig == null) { |
|
64 $schemaConfig = new SchemaConfig(); |
|
65 } |
|
66 $this->_schemaConfig = $schemaConfig; |
|
67 |
|
68 foreach ($tables AS $table) { |
|
69 $this->_addTable($table); |
|
70 } |
|
71 foreach ($sequences AS $sequence) { |
|
72 $this->_addSequence($sequence); |
|
73 } |
|
74 } |
|
75 |
|
76 /** |
|
77 * @return bool |
|
78 */ |
|
79 public function hasExplicitForeignKeyIndexes() |
|
80 { |
|
81 return $this->_schemaConfig->hasExplicitForeignKeyIndexes(); |
|
82 } |
|
83 |
|
84 /** |
|
85 * @param Table $table |
|
86 */ |
|
87 protected function _addTable(Table $table) |
|
88 { |
|
89 $tableName = strtolower($table->getName()); |
|
90 if(isset($this->_tables[$tableName])) { |
|
91 throw SchemaException::tableAlreadyExists($tableName); |
|
92 } |
|
93 |
|
94 $this->_tables[$tableName] = $table; |
|
95 $table->setSchemaConfig($this->_schemaConfig); |
|
96 } |
|
97 |
|
98 /** |
|
99 * @param Sequence $sequence |
|
100 */ |
|
101 protected function _addSequence(Sequence $sequence) |
|
102 { |
|
103 $seqName = strtolower($sequence->getName()); |
|
104 if (isset($this->_sequences[$seqName])) { |
|
105 throw SchemaException::sequenceAlreadyExists($seqName); |
|
106 } |
|
107 $this->_sequences[$seqName] = $sequence; |
|
108 } |
|
109 |
|
110 /** |
|
111 * Get all tables of this schema. |
|
112 * |
|
113 * @return array |
|
114 */ |
|
115 public function getTables() |
|
116 { |
|
117 return $this->_tables; |
|
118 } |
|
119 |
|
120 /** |
|
121 * @param string $tableName |
|
122 * @return Table |
|
123 */ |
|
124 public function getTable($tableName) |
|
125 { |
|
126 $tableName = strtolower($tableName); |
|
127 if (!isset($this->_tables[$tableName])) { |
|
128 throw SchemaException::tableDoesNotExist($tableName); |
|
129 } |
|
130 |
|
131 return $this->_tables[$tableName]; |
|
132 } |
|
133 |
|
134 /** |
|
135 * Does this schema have a table with the given name? |
|
136 * |
|
137 * @param string $tableName |
|
138 * @return Schema |
|
139 */ |
|
140 public function hasTable($tableName) |
|
141 { |
|
142 $tableName = strtolower($tableName); |
|
143 return isset($this->_tables[$tableName]); |
|
144 } |
|
145 |
|
146 /** |
|
147 * @param string $sequenceName |
|
148 * @return bool |
|
149 */ |
|
150 public function hasSequence($sequenceName) |
|
151 { |
|
152 $sequenceName = strtolower($sequenceName); |
|
153 return isset($this->_sequences[$sequenceName]); |
|
154 } |
|
155 |
|
156 /** |
|
157 * @throws SchemaException |
|
158 * @param string $sequenceName |
|
159 * @return Doctrine\DBAL\Schema\Sequence |
|
160 */ |
|
161 public function getSequence($sequenceName) |
|
162 { |
|
163 $sequenceName = strtolower($sequenceName); |
|
164 if(!$this->hasSequence($sequenceName)) { |
|
165 throw SchemaException::sequenceDoesNotExist($sequenceName); |
|
166 } |
|
167 return $this->_sequences[$sequenceName]; |
|
168 } |
|
169 |
|
170 /** |
|
171 * @return Doctrine\DBAL\Schema\Sequence[] |
|
172 */ |
|
173 public function getSequences() |
|
174 { |
|
175 return $this->_sequences; |
|
176 } |
|
177 |
|
178 /** |
|
179 * Create a new table |
|
180 * |
|
181 * @param string $tableName |
|
182 * @return Table |
|
183 */ |
|
184 public function createTable($tableName) |
|
185 { |
|
186 $table = new Table($tableName); |
|
187 $this->_addTable($table); |
|
188 return $table; |
|
189 } |
|
190 |
|
191 /** |
|
192 * Rename a table |
|
193 * |
|
194 * @param string $oldTableName |
|
195 * @param string $newTableName |
|
196 * @return Schema |
|
197 */ |
|
198 public function renameTable($oldTableName, $newTableName) |
|
199 { |
|
200 $table = $this->getTable($oldTableName); |
|
201 $table->_setName($newTableName); |
|
202 |
|
203 $this->dropTable($oldTableName); |
|
204 $this->_addTable($table); |
|
205 return $this; |
|
206 } |
|
207 |
|
208 /** |
|
209 * Drop a table from the schema. |
|
210 * |
|
211 * @param string $tableName |
|
212 * @return Schema |
|
213 */ |
|
214 public function dropTable($tableName) |
|
215 { |
|
216 $tableName = strtolower($tableName); |
|
217 $table = $this->getTable($tableName); |
|
218 unset($this->_tables[$tableName]); |
|
219 return $this; |
|
220 } |
|
221 |
|
222 /** |
|
223 * Create a new sequence |
|
224 * |
|
225 * @param string $sequenceName |
|
226 * @param int $allocationSize |
|
227 * @param int $initialValue |
|
228 * @return Sequence |
|
229 */ |
|
230 public function createSequence($sequenceName, $allocationSize=1, $initialValue=1) |
|
231 { |
|
232 $seq = new Sequence($sequenceName, $allocationSize, $initialValue); |
|
233 $this->_addSequence($seq); |
|
234 return $seq; |
|
235 } |
|
236 |
|
237 /** |
|
238 * @param string $sequenceName |
|
239 * @return Schema |
|
240 */ |
|
241 public function dropSequence($sequenceName) |
|
242 { |
|
243 $sequenceName = strtolower($sequenceName); |
|
244 unset($this->_sequences[$sequenceName]); |
|
245 return $this; |
|
246 } |
|
247 |
|
248 /** |
|
249 * Return an array of necessary sql queries to create the schema on the given platform. |
|
250 * |
|
251 * @param AbstractPlatform $platform |
|
252 * @return array |
|
253 */ |
|
254 public function toSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform) |
|
255 { |
|
256 $sqlCollector = new CreateSchemaSqlCollector($platform); |
|
257 $this->visit($sqlCollector); |
|
258 |
|
259 return $sqlCollector->getQueries(); |
|
260 } |
|
261 |
|
262 /** |
|
263 * Return an array of necessary sql queries to drop the schema on the given platform. |
|
264 * |
|
265 * @param AbstractPlatform $platform |
|
266 * @return array |
|
267 */ |
|
268 public function toDropSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform) |
|
269 { |
|
270 $dropSqlCollector = new DropSchemaSqlCollector($platform); |
|
271 $this->visit($dropSqlCollector); |
|
272 |
|
273 return $dropSqlCollector->getQueries(); |
|
274 } |
|
275 |
|
276 /** |
|
277 * @param Schema $toSchema |
|
278 * @param AbstractPlatform $platform |
|
279 */ |
|
280 public function getMigrateToSql(Schema $toSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) |
|
281 { |
|
282 $comparator = new Comparator(); |
|
283 $schemaDiff = $comparator->compare($this, $toSchema); |
|
284 return $schemaDiff->toSql($platform); |
|
285 } |
|
286 |
|
287 /** |
|
288 * @param Schema $fromSchema |
|
289 * @param AbstractPlatform $platform |
|
290 */ |
|
291 public function getMigrateFromSql(Schema $fromSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) |
|
292 { |
|
293 $comparator = new Comparator(); |
|
294 $schemaDiff = $comparator->compare($fromSchema, $this); |
|
295 return $schemaDiff->toSql($platform); |
|
296 } |
|
297 |
|
298 /** |
|
299 * @param Visitor $visitor |
|
300 */ |
|
301 public function visit(Visitor $visitor) |
|
302 { |
|
303 $visitor->acceptSchema($this); |
|
304 |
|
305 foreach ($this->_tables AS $table) { |
|
306 $table->visit($visitor); |
|
307 } |
|
308 foreach ($this->_sequences AS $sequence) { |
|
309 $sequence->visit($visitor); |
|
310 } |
|
311 } |
|
312 |
|
313 /** |
|
314 * Cloning a Schema triggers a deep clone of all related assets. |
|
315 * |
|
316 * @return void |
|
317 */ |
|
318 public function __clone() |
|
319 { |
|
320 foreach ($this->_tables AS $k => $table) { |
|
321 $this->_tables[$k] = clone $table; |
|
322 } |
|
323 foreach ($this->_sequences AS $k => $sequence) { |
|
324 $this->_sequences[$k] = clone $sequence; |
|
325 } |
|
326 } |
|
327 } |