1<?php
2
3namespace Doctrine\DBAL\Schema;
4
5use Doctrine\DBAL\Platforms\SQLAnywherePlatform;
6use Doctrine\DBAL\Types\Type;
7
8use function assert;
9use function preg_replace;
10
11/**
12 * SAP Sybase SQL Anywhere schema manager.
13 */
14class SQLAnywhereSchemaManager extends AbstractSchemaManager
15{
16    /**
17     * {@inheritdoc}
18     *
19     * Starts a database after creation
20     * as SQL Anywhere needs a database to be started
21     * before it can be used.
22     *
23     * @see startDatabase
24     */
25    public function createDatabase($database)
26    {
27        parent::createDatabase($database);
28        $this->startDatabase($database);
29    }
30
31    /**
32     * {@inheritdoc}
33     *
34     * Tries stopping a database before dropping
35     * as SQL Anywhere needs a database to be stopped
36     * before it can be dropped.
37     *
38     * @see stopDatabase
39     */
40    public function dropDatabase($database)
41    {
42        $this->tryMethod('stopDatabase', $database);
43        parent::dropDatabase($database);
44    }
45
46    /**
47     * Starts a database.
48     *
49     * @param string $database The name of the database to start.
50     *
51     * @return void
52     */
53    public function startDatabase($database)
54    {
55        assert($this->_platform instanceof SQLAnywherePlatform);
56        $this->_execSql($this->_platform->getStartDatabaseSQL($database));
57    }
58
59    /**
60     * Stops a database.
61     *
62     * @param string $database The name of the database to stop.
63     *
64     * @return void
65     */
66    public function stopDatabase($database)
67    {
68        assert($this->_platform instanceof SQLAnywherePlatform);
69        $this->_execSql($this->_platform->getStopDatabaseSQL($database));
70    }
71
72    /**
73     * {@inheritdoc}
74     */
75    protected function _getPortableDatabaseDefinition($database)
76    {
77        return $database['name'];
78    }
79
80    /**
81     * {@inheritdoc}
82     */
83    protected function _getPortableSequenceDefinition($sequence)
84    {
85        return new Sequence($sequence['sequence_name'], $sequence['increment_by'], $sequence['start_with']);
86    }
87
88    /**
89     * {@inheritdoc}
90     */
91    protected function _getPortableTableColumnDefinition($tableColumn)
92    {
93        $type                   = $this->_platform->getDoctrineTypeMapping($tableColumn['type']);
94        $type                   = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
95        $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
96        $precision              = null;
97        $scale                  = null;
98        $fixed                  = false;
99        $default                = null;
100
101        if ($tableColumn['default'] !== null) {
102            // Strip quotes from default value.
103            $default = preg_replace(["/^'(.*)'$/", "/''/"], ['$1', "'"], $tableColumn['default']);
104
105            if ($default === 'autoincrement') {
106                $default = null;
107            }
108        }
109
110        switch ($tableColumn['type']) {
111            case 'binary':
112            case 'char':
113            case 'nchar':
114                $fixed = true;
115                break;
116        }
117
118        switch ($type) {
119            case 'decimal':
120            case 'float':
121                $precision = $tableColumn['length'];
122                $scale     = $tableColumn['scale'];
123                break;
124        }
125
126        return new Column(
127            $tableColumn['column_name'],
128            Type::getType($type),
129            [
130                'length'        => $type === 'string' ? $tableColumn['length'] : null,
131                'precision'     => $precision,
132                'scale'         => $scale,
133                'unsigned'      => (bool) $tableColumn['unsigned'],
134                'fixed'         => $fixed,
135                'notnull'       => (bool) $tableColumn['notnull'],
136                'default'       => $default,
137                'autoincrement' => (bool) $tableColumn['autoincrement'],
138                'comment'       => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
139                    ? $tableColumn['comment']
140                    : null,
141            ]
142        );
143    }
144
145    /**
146     * {@inheritdoc}
147     */
148    protected function _getPortableTableDefinition($table)
149    {
150        return $table['table_name'];
151    }
152
153    /**
154     * {@inheritdoc}
155     */
156    protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
157    {
158        return new ForeignKeyConstraint(
159            $tableForeignKey['local_columns'],
160            $tableForeignKey['foreign_table'],
161            $tableForeignKey['foreign_columns'],
162            $tableForeignKey['name'],
163            $tableForeignKey['options']
164        );
165    }
166
167    /**
168     * {@inheritdoc}
169     */
170    protected function _getPortableTableForeignKeysList($tableForeignKeys)
171    {
172        $foreignKeys = [];
173
174        foreach ($tableForeignKeys as $tableForeignKey) {
175            if (! isset($foreignKeys[$tableForeignKey['index_name']])) {
176                $foreignKeys[$tableForeignKey['index_name']] = [
177                    'local_columns'   => [$tableForeignKey['local_column']],
178                    'foreign_table'   => $tableForeignKey['foreign_table'],
179                    'foreign_columns' => [$tableForeignKey['foreign_column']],
180                    'name'            => $tableForeignKey['index_name'],
181                    'options'         => [
182                        'notnull'           => $tableForeignKey['notnull'],
183                        'match'             => $tableForeignKey['match'],
184                        'onUpdate'          => $tableForeignKey['on_update'],
185                        'onDelete'          => $tableForeignKey['on_delete'],
186                        'check_on_commit'   => $tableForeignKey['check_on_commit'],
187                        'clustered'         => $tableForeignKey['clustered'],
188                        'for_olap_workload' => $tableForeignKey['for_olap_workload'],
189                    ],
190                ];
191            } else {
192                $foreignKeys[$tableForeignKey['index_name']]['local_columns'][]   = $tableForeignKey['local_column'];
193                $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
194            }
195        }
196
197        return parent::_getPortableTableForeignKeysList($foreignKeys);
198    }
199
200    /**
201     * {@inheritdoc}
202     */
203    protected function _getPortableTableIndexesList($tableIndexes, $tableName = null)
204    {
205        foreach ($tableIndexes as &$tableIndex) {
206            $tableIndex['primary'] = (bool) $tableIndex['primary'];
207            $tableIndex['flags']   = [];
208
209            if ($tableIndex['clustered']) {
210                $tableIndex['flags'][] = 'clustered';
211            }
212
213            if ($tableIndex['with_nulls_not_distinct']) {
214                $tableIndex['flags'][] = 'with_nulls_not_distinct';
215            }
216
217            if (! $tableIndex['for_olap_workload']) {
218                continue;
219            }
220
221            $tableIndex['flags'][] = 'for_olap_workload';
222        }
223
224        return parent::_getPortableTableIndexesList($tableIndexes, $tableName);
225    }
226
227    /**
228     * {@inheritdoc}
229     */
230    protected function _getPortableViewDefinition($view)
231    {
232        $definition = preg_replace('/^.*\s+as\s+SELECT(.*)/i', 'SELECT$1', $view['view_def']);
233
234        return new View($view['table_name'], $definition);
235    }
236}
237