MsSqlSchemaManager.php 5.53 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 23 24 25 26 27

/**
 * xxx
 *
 * @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)
28
 * @author      Juozas Kaziukenas <juozas@juokaz.com>
romanb's avatar
romanb committed
29 30 31
 * @version     $Revision$
 * @since       2.0
 */
32
class MsSqlSchemaManager extends AbstractSchemaManager
33
{
34

romanb's avatar
romanb committed
35
    /**
36
     * @override
romanb's avatar
romanb committed
37
     */
38
    protected function _getPortableTableColumnDefinition($tableColumn)
romanb's avatar
romanb committed
39
    {
40 41
        $dbType = strtolower($tableColumn['TYPE_NAME']);

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

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

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

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

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

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

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

87
        $options = array(
88 89 90 91 92 93 94 95
            '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,
96
        );
97

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

    /**
102
     * @override
romanb's avatar
romanb committed
103
     */
104
    protected function _getPortableTableIndexesList($tableIndexRows, $tableName=null)
romanb's avatar
romanb committed
105
    {
106
        $result = array();
107
        foreach ($tableIndexRows AS $tableIndex) {
108
            $indexName = $keyName = $tableIndex['index_name'];
109
            if (strpos($tableIndex['index_description'], 'primary key') !== false) {
110
                $keyName = 'primary';
romanb's avatar
romanb committed
111
            }
112
            $keyName = strtolower($keyName);
romanb's avatar
romanb committed
113

114 115 116 117 118
            $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
119 120 121
            );
        }

122
        $indexes = array();
123
        foreach ($result AS $indexKey => $data) {
124 125
            $indexes[$indexKey] = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
        }
romanb's avatar
romanb committed
126

127
        return $indexes;
romanb's avatar
romanb committed
128 129 130
    }

    /**
131
     * @override
romanb's avatar
romanb committed
132
     */
133
    public function _getPortableTableForeignKeyDefinition($tableForeignKey)
romanb's avatar
romanb committed
134
    {
135
        return new ForeignKeyConstraint(
136 137 138 139 140 141 142 143
                (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']),
                )
144
        );
romanb's avatar
romanb committed
145 146 147
    }

    /**
148
     * @override
romanb's avatar
romanb committed
149
     */
150
    protected function _getPortableTableDefinition($table)
romanb's avatar
romanb committed
151
    {
152
        return $table['name'];
romanb's avatar
romanb committed
153
    }
154 155

    /**
156 157
     * @override
     */
158
    protected function _getPortableDatabaseDefinition($database)
159 160 161
    {
        return $database['name'];
    }
162 163

    /**
164 165
     * @override
     */
166
    protected function _getPortableViewDefinition($view)
167
    {
168
        // @todo
169 170
        return new View($view['name'], null);
    }
171

172
}