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

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

collaborator page

File size: 7.0 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.phpdoctrine.org>.
18 */
19
20namespace Doctrine\DBAL\Schema;
21
22use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs;
23use Doctrine\DBAL\Events;
24
25/**
26 * SQL Server Schema Manager
27 *
28 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
29 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
30 * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
31 * @author      Juozas Kaziukenas <juozas@juokaz.com>
32 * @since       2.0
33 */
34class SQLServerSchemaManager extends AbstractSchemaManager
35{
36    /**
37     * @override
38     */
39    protected function _getPortableTableColumnDefinition($tableColumn)
40    {
41        $dbType = strtolower($tableColumn['TYPE_NAME']);
42
43        $autoincrement = false;
44        if (stripos($dbType, 'identity')) {
45            $dbType = trim(str_ireplace('identity', '', $dbType));
46            $autoincrement = true;
47        }
48
49        $type = array();
50        $unsigned = $fixed = null;
51
52        if (!isset($tableColumn['name'])) {
53            $tableColumn['name'] = '';
54        }
55
56        $default = $tableColumn['COLUMN_DEF'];
57
58        while ($default != ($default2 = preg_replace("/^\((.*)\)$/", '$1', $default))) {
59            $default = trim($default2, "'");
60        }
61
62        $length = (int) $tableColumn['LENGTH'];
63
64        $type = $this->_platform->getDoctrineTypeMapping($dbType);
65        switch ($type) {
66            case 'char':
67                if ($tableColumn['LENGTH'] == '1') {
68                    $type = 'boolean';
69                    if (preg_match('/^(is|has)/', $tableColumn['name'])) {
70                        $type = array_reverse($type);
71                    }
72                }
73                $fixed = true;
74                break;
75            case 'text':
76                $fixed = false;
77                break;
78        }
79        switch ($dbType) {
80            case 'nchar':
81            case 'nvarchar':
82            case 'ntext':
83                // Unicode data requires 2 bytes per character
84                $length = $length / 2;
85                break;
86        }
87
88        $options = array(
89            'length' => ($length == 0 || !in_array($type, array('text', 'string'))) ? null : $length,
90            'unsigned' => (bool) $unsigned,
91            'fixed' => (bool) $fixed,
92            'default' => $default !== 'NULL' ? $default : null,
93            'notnull' => (bool) ($tableColumn['IS_NULLABLE'] != 'YES'),
94            'scale' => $tableColumn['SCALE'],
95            'precision' => $tableColumn['PRECISION'],
96            'autoincrement' => $autoincrement,
97        );
98
99        return new Column($tableColumn['COLUMN_NAME'], \Doctrine\DBAL\Types\Type::getType($type), $options);
100    }
101
102    /**
103     * @override
104     */
105    protected function _getPortableTableIndexesList($tableIndexRows, $tableName=null)
106    {
107        $result = array();
108        foreach ($tableIndexRows AS $tableIndex) {
109            $indexName = $keyName = $tableIndex['index_name'];
110            if (strpos($tableIndex['index_description'], 'primary key') !== false) {
111                $keyName = 'primary';
112            }
113            $keyName = strtolower($keyName);
114
115            $result[$keyName] = array(
116                'name' => $indexName,
117                'columns' => explode(', ', $tableIndex['index_keys']),
118                'unique' => strpos($tableIndex['index_description'], 'unique') !== false,
119                'primary' => strpos($tableIndex['index_description'], 'primary key') !== false,
120            );
121        }
122
123        $eventManager = $this->_platform->getEventManager();
124
125        $indexes = array();
126        foreach ($result AS $indexKey => $data) {
127            $index = null;
128            $defaultPrevented = false;
129
130            if (null !== $eventManager && $eventManager->hasListeners(Events::onSchemaIndexDefinition)) {
131                $eventArgs = new SchemaIndexDefinitionEventArgs($data, $tableName, $this->_conn);
132                $eventManager->dispatchEvent(Events::onSchemaIndexDefinition, $eventArgs);
133
134                $defaultPrevented = $eventArgs->isDefaultPrevented();
135                $index = $eventArgs->getIndex();
136            }
137
138            if (!$defaultPrevented) {
139                $index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
140            }
141
142            if ($index) {
143                $indexes[$indexKey] = $index;
144            }
145        }
146
147        return $indexes;
148    }
149
150    /**
151     * @override
152     */
153    public function _getPortableTableForeignKeyDefinition($tableForeignKey)
154    {
155        return new ForeignKeyConstraint(
156                (array) $tableForeignKey['ColumnName'],
157                $tableForeignKey['ReferenceTableName'],
158                (array) $tableForeignKey['ReferenceColumnName'],
159                $tableForeignKey['ForeignKey'],
160                array(
161                    'onUpdate' => str_replace('_', ' ', $tableForeignKey['update_referential_action_desc']),
162                    'onDelete' => str_replace('_', ' ', $tableForeignKey['delete_referential_action_desc']),
163                )
164        );
165    }
166
167    /**
168     * @override
169     */
170    protected function _getPortableTableDefinition($table)
171    {
172        return $table['name'];
173    }
174
175    /**
176     * @override
177     */
178    protected function _getPortableDatabaseDefinition($database)
179    {
180        return $database['name'];
181    }
182
183    /**
184     * @override
185     */
186    protected function _getPortableViewDefinition($view)
187    {
188        // @todo
189        return new View($view['name'], null);
190    }
191
192    /**
193     * List the indexes for a given table returning an array of Index instances.
194     *
195     * Keys of the portable indexes list are all lower-cased.
196     *
197     * @param string $table The name of the table
198     * @return Index[] $tableIndexes
199     */
200    public function listTableIndexes($table)
201    {
202        $sql = $this->_platform->getListTableIndexesSQL($table, $this->_conn->getDatabase());
203
204        try {
205            $tableIndexes = $this->_conn->fetchAll($sql);
206        } catch(\PDOException $e) {
207            if ($e->getCode() == "IMSSP") {
208                return array();
209            } else {
210                throw $e;
211            }
212        }
213
214        return $this->_getPortableTableIndexesList($tableIndexes, $table);
215    }
216}
Note: See TracBrowser for help on using the repository browser.