source: pro-violet-viettel/sourcecode/application/libraries/Doctrine/DBAL/Schema/Schema.php @ 347

Last change on this file since 347 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 10.4 KB
Line 
1<?php
2/*
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 *
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the LGPL. For more information, see
17 * <http://www.doctrine-project.org>.
18 */
19
20namespace Doctrine\DBAL\Schema;
21
22use Doctrine\DBAL\Schema\Visitor\CreateSchemaSqlCollector;
23use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
24use Doctrine\DBAL\Schema\Visitor\Visitor;
25
26/**
27 * Object representation of a database schema
28 *
29 * Different vendors have very inconsistent naming with regard to the concept
30 * of a "schema". Doctrine understands a schema as the entity that conceptually
31 * wraps a set of database objects such as tables, sequences, indexes and
32 * foreign keys that belong to each other into a namespace. A Doctrine Schema
33 * has nothing to do with the "SCHEMA" defined as in PostgreSQL, it is more
34 * related to the concept of "DATABASE" that exists in MySQL and PostgreSQL.
35 *
36 * Every asset in the doctrine schema has a name. A name consists of either a
37 * namespace.local name pair or just a local unqualified name.
38 *
39 * The abstraction layer that covers a PostgreSQL schema is the namespace of an
40 * database object (asset). A schema can have a name, which will be used as
41 * default namespace for the unqualified database objects that are created in
42 * the schema.
43 *
44 * In the case of MySQL where cross-database queries are allowed this leads to
45 * databases being "misinterpreted" as namespaces. This is intentional, however
46 * the CREATE/DROP SQL visitors will just filter this queries and do not
47 * execute them. Only the queries for the currently connected database are
48 * executed.
49 *
50 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
51 * @link    www.doctrine-project.org
52 * @since   2.0
53 * @author  Benjamin Eberlei <kontakt@beberlei.de>
54 */
55class Schema extends AbstractAsset
56{
57    /**
58     * @var array
59     */
60    protected $_tables = array();
61
62    /**
63     * @var array
64     */
65    protected $_sequences = array();
66
67    /**
68     * @var SchemaConfig
69     */
70    protected $_schemaConfig = false;
71
72    /**
73     * @param array $tables
74     * @param array $sequences
75     * @param array $views
76     * @param array $triggers
77     * @param SchemaConfig $schemaConfig
78     */
79    public function __construct(array $tables=array(), array $sequences=array(), SchemaConfig $schemaConfig=null)
80    {
81        if ($schemaConfig == null) {
82            $schemaConfig = new SchemaConfig();
83        }
84        $this->_schemaConfig = $schemaConfig;
85        $this->_setName($schemaConfig->getName() ?: 'public');
86
87        foreach ($tables AS $table) {
88            $this->_addTable($table);
89        }
90        foreach ($sequences AS $sequence) {
91            $this->_addSequence($sequence);
92        }
93    }
94
95    /**
96     * @return bool
97     */
98    public function hasExplicitForeignKeyIndexes()
99    {
100        return $this->_schemaConfig->hasExplicitForeignKeyIndexes();
101    }
102
103    /**
104     * @param Table $table
105     */
106    protected function _addTable(Table $table)
107    {
108        $tableName = $table->getFullQualifiedName($this->getName());
109        if(isset($this->_tables[$tableName])) {
110            throw SchemaException::tableAlreadyExists($tableName);
111        }
112
113        $this->_tables[$tableName] = $table;
114        $table->setSchemaConfig($this->_schemaConfig);
115    }
116
117    /**
118     * @param Sequence $sequence
119     */
120    protected function _addSequence(Sequence $sequence)
121    {
122        $seqName = $sequence->getFullQualifiedName($this->getName());
123        if (isset($this->_sequences[$seqName])) {
124            throw SchemaException::sequenceAlreadyExists($seqName);
125        }
126        $this->_sequences[$seqName] = $sequence;
127    }
128
129    /**
130     * Get all tables of this schema.
131     *
132     * @return array
133     */
134    public function getTables()
135    {
136        return $this->_tables;
137    }
138
139    /**
140     * @param string $tableName
141     * @return Table
142     */
143    public function getTable($tableName)
144    {
145        $tableName = $this->getFullQualifiedAssetName($tableName);
146        if (!isset($this->_tables[$tableName])) {
147            throw SchemaException::tableDoesNotExist($tableName);
148        }
149
150        return $this->_tables[$tableName];
151    }
152
153    /**
154     * @return string
155     */
156    private function getFullQualifiedAssetName($name)
157    {
158        if ($this->isQuoted($name)) {
159            $name = $this->trimQuotes($name);
160        }
161        if (strpos($name, ".") === false) {
162            $name = $this->getName() . "." . $name;
163        }
164        return strtolower($name);
165    }
166
167    /**
168     * Does this schema have a table with the given name?
169     *
170     * @param  string $tableName
171     * @return Schema
172     */
173    public function hasTable($tableName)
174    {
175        $tableName = $this->getFullQualifiedAssetName($tableName);
176        return isset($this->_tables[$tableName]);
177    }
178
179    /**
180     * Get all table names, prefixed with a schema name, even the default one
181     * if present.
182     *
183     * @return array
184     */
185    public function getTableNames()
186    {
187        return array_keys($this->_tables);
188    }
189
190    public function hasSequence($sequenceName)
191    {
192        $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
193        return isset($this->_sequences[$sequenceName]);
194    }
195
196    /**
197     * @throws SchemaException
198     * @param  string $sequenceName
199     * @return Doctrine\DBAL\Schema\Sequence
200     */
201    public function getSequence($sequenceName)
202    {
203        $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
204        if(!$this->hasSequence($sequenceName)) {
205            throw SchemaException::sequenceDoesNotExist($sequenceName);
206        }
207        return $this->_sequences[$sequenceName];
208    }
209
210    /**
211     * @return Doctrine\DBAL\Schema\Sequence[]
212     */
213    public function getSequences()
214    {
215        return $this->_sequences;
216    }
217
218    /**
219     * Create a new table
220     *
221     * @param  string $tableName
222     * @return Table
223     */
224    public function createTable($tableName)
225    {
226        $table = new Table($tableName);
227        $this->_addTable($table);
228        return $table;
229    }
230
231    /**
232     * Rename a table
233     *
234     * @param string $oldTableName
235     * @param string $newTableName
236     * @return Schema
237     */
238    public function renameTable($oldTableName, $newTableName)
239    {
240        $table = $this->getTable($oldTableName);
241        $table->_setName($newTableName);
242
243        $this->dropTable($oldTableName);
244        $this->_addTable($table);
245        return $this;
246    }
247
248    /**
249     * Drop a table from the schema.
250     *
251     * @param string $tableName
252     * @return Schema
253     */
254    public function dropTable($tableName)
255    {
256        $tableName = $this->getFullQualifiedAssetName($tableName);
257        $table = $this->getTable($tableName);
258        unset($this->_tables[$tableName]);
259        return $this;
260    }
261
262    /**
263     * Create a new sequence
264     *
265     * @param  string $sequenceName
266     * @param  int $allocationSize
267     * @param  int $initialValue
268     * @return Sequence
269     */
270    public function createSequence($sequenceName, $allocationSize=1, $initialValue=1)
271    {
272        $seq = new Sequence($sequenceName, $allocationSize, $initialValue);
273        $this->_addSequence($seq);
274        return $seq;
275    }
276
277    /**
278     * @param string $sequenceName
279     * @return Schema
280     */
281    public function dropSequence($sequenceName)
282    {
283        $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
284        unset($this->_sequences[$sequenceName]);
285        return $this;
286    }
287
288    /**
289     * Return an array of necessary sql queries to create the schema on the given platform.
290     *
291     * @param AbstractPlatform $platform
292     * @return array
293     */
294    public function toSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
295    {
296        $sqlCollector = new CreateSchemaSqlCollector($platform);
297        $this->visit($sqlCollector);
298
299        return $sqlCollector->getQueries();
300    }
301
302    /**
303     * Return an array of necessary sql queries to drop the schema on the given platform.
304     *
305     * @param AbstractPlatform $platform
306     * @return array
307     */
308    public function toDropSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
309    {
310        $dropSqlCollector = new DropSchemaSqlCollector($platform);
311        $this->visit($dropSqlCollector);
312
313        return $dropSqlCollector->getQueries();
314    }
315
316    /**
317     * @param Schema $toSchema
318     * @param AbstractPlatform $platform
319     */
320    public function getMigrateToSql(Schema $toSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
321    {
322        $comparator = new Comparator();
323        $schemaDiff = $comparator->compare($this, $toSchema);
324        return $schemaDiff->toSql($platform);
325    }
326
327    /**
328     * @param Schema $fromSchema
329     * @param AbstractPlatform $platform
330     */
331    public function getMigrateFromSql(Schema $fromSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
332    {
333        $comparator = new Comparator();
334        $schemaDiff = $comparator->compare($fromSchema, $this);
335        return $schemaDiff->toSql($platform);
336    }
337
338    /**
339     * @param Visitor $visitor
340     */
341    public function visit(Visitor $visitor)
342    {
343        $visitor->acceptSchema($this);
344
345        foreach ($this->_tables AS $table) {
346            $table->visit($visitor);
347        }
348        foreach ($this->_sequences AS $sequence) {
349            $sequence->visit($visitor);
350        }
351    }
352
353    /**
354     * Cloning a Schema triggers a deep clone of all related assets.
355     *
356     * @return void
357     */
358    public function __clone()
359    {
360        foreach ($this->_tables AS $k => $table) {
361            $this->_tables[$k] = clone $table;
362        }
363        foreach ($this->_sequences AS $k => $sequence) {
364            $this->_sequences[$k] = clone $sequence;
365        }
366    }
367}
Note: See TracBrowser for help on using the repository browser.