SQLServerSchemaManager.php 7.02 KB
Newer Older
romanb's avatar
romanb committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.org>.
 */

20
namespace Doctrine\DBAL\Schema;
romanb's avatar
romanb committed
21

22
use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs;
23
use Doctrine\DBAL\Events;
24

romanb's avatar
romanb committed
25
/**
26
 * SQL Server Schema Manager
romanb's avatar
romanb committed
27 28 29 30
 *
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
31
 * @author      Juozas Kaziukenas <juozas@juokaz.com>
romanb's avatar
romanb committed
32 33
 * @since       2.0
 */
34
class SQLServerSchemaManager extends AbstractSchemaManager
35
{
romanb's avatar
romanb committed
36
    /**
37
     * @override
romanb's avatar
romanb committed
38
     */
39
    protected function _getPortableTableColumnDefinition($tableColumn)
romanb's avatar
romanb committed
40
    {
41 42
        $dbType = strtolower($tableColumn['TYPE_NAME']);

43
        $autoincrement = false;
44 45
        if (stripos($dbType, 'identity')) {
            $dbType = trim(str_ireplace('identity', '', $dbType));
46
            $autoincrement = true;
romanb's avatar
romanb committed
47 48
        }

49 50
        $type = array();
        $unsigned = $fixed = null;
romanb's avatar
romanb committed
51

52
        if (!isset($tableColumn['name'])) {
53
            $tableColumn['name'] = '';
romanb's avatar
romanb committed
54
        }
55

56 57
        $default = $tableColumn['COLUMN_DEF'];

58
        while ($default != ($default2 = preg_replace("/^\((.*)\)$/", '$1', $default))) {
59
            $default = trim($default2, "'");
60
        }
61 62 63 64

        $length = (int) $tableColumn['LENGTH'];

        $type = $this->_platform->getDoctrineTypeMapping($dbType);
65
        switch ($type) {
66 67 68 69 70 71
            case 'char':
                if ($tableColumn['LENGTH'] == '1') {
                    $type = 'boolean';
                    if (preg_match('/^(is|has)/', $tableColumn['name'])) {
                        $type = array_reverse($type);
                    }
romanb's avatar
romanb committed
72
                }
73
                $fixed = true;
74
                break;
75
            case 'text':
76 77
                $fixed = false;
                break;
romanb's avatar
romanb committed
78
        }
79 80 81 82
        switch ($dbType) {
            case 'nchar':
            case 'nvarchar':
            case 'ntext':
83 84 85
                // Unicode data requires 2 bytes per character
                $length = $length / 2;
                break;
86
        }
87

88
        $options = array(
89 90 91 92 93 94 95 96
            'length' => ($length == 0 || !in_array($type, array('text', 'string'))) ? null : $length,
            'unsigned' => (bool) $unsigned,
            'fixed' => (bool) $fixed,
            'default' => $default !== 'NULL' ? $default : null,
            'notnull' => (bool) ($tableColumn['IS_NULLABLE'] != 'YES'),
            'scale' => $tableColumn['SCALE'],
            'precision' => $tableColumn['PRECISION'],
            'autoincrement' => $autoincrement,
97
        );
98

99
        return new Column($tableColumn['COLUMN_NAME'], \Doctrine\DBAL\Types\Type::getType($type), $options);
romanb's avatar
romanb committed
100 101 102
    }

    /**
103
     * @override
romanb's avatar
romanb committed
104
     */
105
    protected function _getPortableTableIndexesList($tableIndexRows, $tableName=null)
romanb's avatar
romanb committed
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';
romanb's avatar
romanb committed
112
            }
113
            $keyName = strtolower($keyName);
romanb's avatar
romanb committed
114

115 116 117 118 119
            $result[$keyName] = array(
                'name' => $indexName,
                'columns' => explode(', ', $tableIndex['index_keys']),
                'unique' => strpos($tableIndex['index_description'], 'unique') !== false,
                'primary' => strpos($tableIndex['index_description'], 'primary key') !== false,
romanb's avatar
romanb committed
120 121 122
            );
        }

123 124
        $eventManager = $this->_platform->getEventManager();

125
        $indexes = array();
126
        foreach ($result AS $indexKey => $data) {
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
            $index = null;
            $defaultPrevented = false;

            if (null !== $eventManager && $eventManager->hasListeners(Events::onSchemaIndexDefinition)) {
                $eventArgs = new SchemaIndexDefinitionEventArgs($data, $tableName, $this->_conn);
                $eventManager->dispatchEvent(Events::onSchemaIndexDefinition, $eventArgs);

                $defaultPrevented = $eventArgs->isDefaultPrevented();
                $index = $eventArgs->getIndex();
            }

            if (!$defaultPrevented) {
                $index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
            }

            if ($index) {
                $indexes[$indexKey] = $index;
            }
145
        }
romanb's avatar
romanb committed
146

147
        return $indexes;
romanb's avatar
romanb committed
148 149 150
    }

    /**
151
     * @override
romanb's avatar
romanb committed
152
     */
153
    public function _getPortableTableForeignKeyDefinition($tableForeignKey)
romanb's avatar
romanb committed
154
    {
155
        return new ForeignKeyConstraint(
156 157 158 159 160 161 162 163
                (array) $tableForeignKey['ColumnName'],
                $tableForeignKey['ReferenceTableName'],
                (array) $tableForeignKey['ReferenceColumnName'],
                $tableForeignKey['ForeignKey'],
                array(
                    'onUpdate' => str_replace('_', ' ', $tableForeignKey['update_referential_action_desc']),
                    'onDelete' => str_replace('_', ' ', $tableForeignKey['delete_referential_action_desc']),
                )
164
        );
romanb's avatar
romanb committed
165 166 167
    }

    /**
168
     * @override
romanb's avatar
romanb committed
169
     */
170
    protected function _getPortableTableDefinition($table)
romanb's avatar
romanb committed
171
    {
172
        return $table['name'];
romanb's avatar
romanb committed
173
    }
174 175

    /**
176 177
     * @override
     */
178
    protected function _getPortableDatabaseDefinition($database)
179 180 181
    {
        return $database['name'];
    }
182 183

    /**
184 185
     * @override
     */
186
    protected function _getPortableViewDefinition($view)
187
    {
188
        // @todo
189 190
        return new View($view['name'], null);
    }
191

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    /**
     * List the indexes for a given table returning an array of Index instances.
     *
     * Keys of the portable indexes list are all lower-cased.
     *
     * @param string $table The name of the table
     * @return Index[] $tableIndexes
     */
    public function listTableIndexes($table)
    {
        $sql = $this->_platform->getListTableIndexesSQL($table, $this->_conn->getDatabase());

        try {
            $tableIndexes = $this->_conn->fetchAll($sql);
        } catch(\PDOException $e) {
            if ($e->getCode() == "IMSSP") {
                return array();
            } else {
                throw $e;
            }
        }

        return $this->_getPortableTableIndexesList($tableIndexes, $table);
    }
216
}