SQLAnywhereSchemaManager.php 7.27 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Schema;

5
use Doctrine\DBAL\Platforms\SQLAnywherePlatform;
6
use Doctrine\DBAL\Types\Type;
7
use function assert;
Sergei Morozov's avatar
Sergei Morozov committed
8
use function is_string;
9
use function preg_replace;
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

/**
 * SAP Sybase SQL Anywhere schema manager.
 */
class SQLAnywhereSchemaManager extends AbstractSchemaManager
{
    /**
     * {@inheritdoc}
     *
     * Starts a database after creation
     * as SQL Anywhere needs a database to be started
     * before it can be used.
     *
     * @see startDatabase
     */
    public function createDatabase($database)
    {
        parent::createDatabase($database);
        $this->startDatabase($database);
    }

    /**
     * {@inheritdoc}
     *
     * Tries stopping a database before dropping
     * as SQL Anywhere needs a database to be stopped
     * before it can be dropped.
     *
     * @see stopDatabase
     */
    public function dropDatabase($database)
    {
        $this->tryMethod('stopDatabase', $database);
        parent::dropDatabase($database);
    }

    /**
     * Starts a database.
     *
     * @param string $database The name of the database to start.
50 51
     *
     * @return void
52 53 54
     */
    public function startDatabase($database)
    {
55
        assert($this->_platform instanceof SQLAnywherePlatform);
56 57 58 59 60 61 62
        $this->_execSql($this->_platform->getStartDatabaseSQL($database));
    }

    /**
     * Stops a database.
     *
     * @param string $database The name of the database to stop.
63 64
     *
     * @return void
65 66 67
     */
    public function stopDatabase($database)
    {
68
        assert($this->_platform instanceof SQLAnywherePlatform);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
        $this->_execSql($this->_platform->getStopDatabaseSQL($database));
    }

    /**
     * {@inheritdoc}
     */
    protected function _getPortableDatabaseDefinition($database)
    {
        return $database['name'];
    }

    /**
     * {@inheritdoc}
     */
    protected function _getPortableSequenceDefinition($sequence)
    {
        return new Sequence($sequence['sequence_name'], $sequence['increment_by'], $sequence['start_with']);
    }

    /**
     * {@inheritdoc}
     */
    protected function _getPortableTableColumnDefinition($tableColumn)
    {
Steve Müller's avatar
Steve Müller committed
93 94
        $type                   = $this->_platform->getDoctrineTypeMapping($tableColumn['type']);
        $type                   = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
95
        $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
Steve Müller's avatar
Steve Müller committed
96 97 98 99
        $precision              = null;
        $scale                  = null;
        $fixed                  = false;
        $default                = null;
100

101
        if ($tableColumn['default'] !== null) {
Steve Müller's avatar
Steve Müller committed
102
            // Strip quotes from default value.
103
            $default = preg_replace(["/^'(.*)'$/", "/''/"], ['$1', "'"], $tableColumn['default']);
104

105
            if ($default === 'autoincrement') {
106 107
                $default = null;
            }
108 109 110 111 112 113 114
        }

        switch ($tableColumn['type']) {
            case 'binary':
            case 'char':
            case 'nchar':
                $fixed = true;
Sergei Morozov's avatar
Sergei Morozov committed
115
                break;
116 117 118 119 120 121
        }

        switch ($type) {
            case 'decimal':
            case 'float':
                $precision = $tableColumn['length'];
122
                $scale     = $tableColumn['scale'];
Sergei Morozov's avatar
Sergei Morozov committed
123
                break;
124 125 126 127 128
        }

        return new Column(
            $tableColumn['column_name'],
            Type::getType($type),
129
            [
130
                'length'        => $type === 'string' ? $tableColumn['length'] : null,
Steve Müller's avatar
Steve Müller committed
131 132 133 134 135 136
                'precision'     => $precision,
                'scale'         => $scale,
                'unsigned'      => (bool) $tableColumn['unsigned'],
                'fixed'         => $fixed,
                'notnull'       => (bool) $tableColumn['notnull'],
                'default'       => $default,
137
                'autoincrement' => (bool) $tableColumn['autoincrement'],
138
                'comment'       => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
139 140
                    ? $tableColumn['comment']
                    : null,
141 142
            ]
        );
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    }

    /**
     * {@inheritdoc}
     */
    protected function _getPortableTableDefinition($table)
    {
        return $table['table_name'];
    }

    /**
     * {@inheritdoc}
     */
    protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
    {
        return new ForeignKeyConstraint(
            $tableForeignKey['local_columns'],
            $tableForeignKey['foreign_table'],
            $tableForeignKey['foreign_columns'],
            $tableForeignKey['name'],
            $tableForeignKey['options']
        );
    }

    /**
     * {@inheritdoc}
     */
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
    {
172
        $foreignKeys = [];
173 174

        foreach ($tableForeignKeys as $tableForeignKey) {
175
            if (! isset($foreignKeys[$tableForeignKey['index_name']])) {
176 177
                $foreignKeys[$tableForeignKey['index_name']] = [
                    'local_columns'   => [$tableForeignKey['local_column']],
Steve Müller's avatar
Steve Müller committed
178
                    'foreign_table'   => $tableForeignKey['foreign_table'],
179
                    'foreign_columns' => [$tableForeignKey['foreign_column']],
Steve Müller's avatar
Steve Müller committed
180
                    'name'            => $tableForeignKey['index_name'],
181
                    'options'         => [
Steve Müller's avatar
Steve Müller committed
182 183 184 185 186 187
                        'notnull'           => $tableForeignKey['notnull'],
                        'match'             => $tableForeignKey['match'],
                        'onUpdate'          => $tableForeignKey['on_update'],
                        'onDelete'          => $tableForeignKey['on_delete'],
                        'check_on_commit'   => $tableForeignKey['check_on_commit'],
                        'clustered'         => $tableForeignKey['clustered'],
188 189
                        'for_olap_workload' => $tableForeignKey['for_olap_workload'],
                    ],
190
                ];
191
            } else {
192
                $foreignKeys[$tableForeignKey['index_name']]['local_columns'][]   = $tableForeignKey['local_column'];
193 194 195 196 197 198 199 200 201 202 203 204 205
                $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
            }
        }

        return parent::_getPortableTableForeignKeysList($foreignKeys);
    }

    /**
     * {@inheritdoc}
     */
    protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null)
    {
        foreach ($tableIndexRows as &$tableIndex) {
206 207
            $tableIndex['primary'] = (bool) $tableIndex['primary'];
            $tableIndex['flags']   = [];
208 209 210 211 212 213 214 215 216

            if ($tableIndex['clustered']) {
                $tableIndex['flags'][] = 'clustered';
            }

            if ($tableIndex['with_nulls_not_distinct']) {
                $tableIndex['flags'][] = 'with_nulls_not_distinct';
            }

217 218
            if (! $tableIndex['for_olap_workload']) {
                continue;
219
            }
220 221

            $tableIndex['flags'][] = 'for_olap_workload';
222 223 224 225 226 227 228 229 230 231
        }

        return parent::_getPortableTableIndexesList($tableIndexRows, $tableName);
    }

    /**
     * {@inheritdoc}
     */
    protected function _getPortableViewDefinition($view)
    {
Sergei Morozov's avatar
Sergei Morozov committed
232 233 234 235
        $definition = preg_replace('/^.*\s+as\s+SELECT(.*)/i', 'SELECT$1', $view['view_def']);
        assert(is_string($definition));

        return new View($view['table_name'], $definition);
236 237
    }
}