Failed Conditions
Pull Request — master (#2936)
by Sergei
15:06
created

_getPortableDatabaseDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 2
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 MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Schema;
21
22
use Doctrine\DBAL\Types\Type;
23
24
/**
25
 * SAP Sybase SQL Anywhere schema manager.
26
 *
27
 * @author Steve Müller <[email protected]>
28
 * @link   www.doctrine-project.org
29
 * @since  2.5
30
 */
31
class SQLAnywhereSchemaManager extends AbstractSchemaManager
32
{
33
    /**
34
     * {@inheritdoc}
35
     *
36
     * Starts a database after creation
37
     * as SQL Anywhere needs a database to be started
38
     * before it can be used.
39
     *
40
     * @see startDatabase
41
     */
42
    public function createDatabase($database)
43
    {
44
        parent::createDatabase($database);
45
        $this->startDatabase($database);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     *
51
     * Tries stopping a database before dropping
52
     * as SQL Anywhere needs a database to be stopped
53
     * before it can be dropped.
54
     *
55
     * @see stopDatabase
56
     */
57
    public function dropDatabase($database)
58
    {
59
        $this->tryMethod('stopDatabase', $database);
60
        parent::dropDatabase($database);
61
    }
62
63
    /**
64
     * Starts a database.
65
     *
66
     * @param string $database The name of the database to start.
67
     */
68
    public function startDatabase($database)
69
    {
70
        $this->_execSql($this->_platform->getStartDatabaseSQL($database));
0 ignored issues
show
Bug introduced by
The method getStartDatabaseSQL() does not exist on Doctrine\DBAL\Platforms\AbstractPlatform. It seems like you code against a sub-type of Doctrine\DBAL\Platforms\AbstractPlatform such as Doctrine\DBAL\Platforms\SQLAnywherePlatform. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        $this->_execSql($this->_platform->/** @scrutinizer ignore-call */ getStartDatabaseSQL($database));
Loading history...
71
    }
72
73
    /**
74
     * Stops a database.
75
     *
76
     * @param string $database The name of the database to stop.
77
     */
78
    public function stopDatabase($database)
79
    {
80
        $this->_execSql($this->_platform->getStopDatabaseSQL($database));
0 ignored issues
show
Bug introduced by
The method getStopDatabaseSQL() does not exist on Doctrine\DBAL\Platforms\AbstractPlatform. Did you maybe mean getDropDatabaseSQL()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
        $this->_execSql($this->_platform->/** @scrutinizer ignore-call */ getStopDatabaseSQL($database));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    protected function _getPortableDatabaseDefinition($database)
87
    {
88
        return $database['name'];
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    protected function _getPortableSequenceDefinition($sequence)
95
    {
96
        return new Sequence($sequence['sequence_name'], $sequence['increment_by'], $sequence['start_with']);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    protected function _getPortableTableColumnDefinition($tableColumn)
103
    {
104
        $type                   = $this->_platform->getDoctrineTypeMapping($tableColumn['type']);
105
        $type                   = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
106
        $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
107
        $precision              = null;
108
        $scale                  = null;
109
        $fixed                  = false;
110
        $default                = null;
111
112
        if (null !== $tableColumn['default']) {
113
            // Strip quotes from default value.
114
            $default = preg_replace(["/^'(.*)'$/", "/''/"], ["$1", "'"], $tableColumn['default']);
115
116
            if ('autoincrement' == $default) {
117
                $default = null;
118
            }
119
        }
120
121
        switch ($tableColumn['type']) {
122
            case 'binary':
123
            case 'char':
124
            case 'nchar':
125
                $fixed = true;
126
        }
127
128
        switch ($type) {
129
            case 'decimal':
130
            case 'float':
131
                $precision = $tableColumn['length'];
132
                $scale = $tableColumn['scale'];
133
        }
134
135
        return new Column(
136
            $tableColumn['column_name'],
137
            Type::getType($type),
138
            [
139
                'length'        => $type == 'string' ? $tableColumn['length'] : null,
140
                'precision'     => $precision,
141
                'scale'         => $scale,
142
                'unsigned'      => (bool) $tableColumn['unsigned'],
143
                'fixed'         => $fixed,
144
                'notnull'       => (bool) $tableColumn['notnull'],
145
                'default'       => $default,
146
                'autoincrement' => (bool) $tableColumn['autoincrement'],
147
                'comment'       => isset($tableColumn['comment']) && '' !== $tableColumn['comment']
148
                    ? $tableColumn['comment']
149
                    : null,
150
            ]
151
        );
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    protected function _getPortableTableDefinition($table)
158
    {
159
        return $table['table_name'];
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165 View Code Duplication
    protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
    {
167
        return new ForeignKeyConstraint(
168
            $tableForeignKey['local_columns'],
169
            $tableForeignKey['foreign_table'],
170
            $tableForeignKey['foreign_columns'],
171
            $tableForeignKey['name'],
172
            $tableForeignKey['options']
173
        );
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
180
    {
181
        $foreignKeys = [];
182
183
        foreach ($tableForeignKeys as $tableForeignKey) {
184
            if (!isset($foreignKeys[$tableForeignKey['index_name']])) {
185
                $foreignKeys[$tableForeignKey['index_name']] = [
186
                    'local_columns'   => [$tableForeignKey['local_column']],
187
                    'foreign_table'   => $tableForeignKey['foreign_table'],
188
                    'foreign_columns' => [$tableForeignKey['foreign_column']],
189
                    'name'            => $tableForeignKey['index_name'],
190
                    'options'         => [
191
                        'notnull'           => $tableForeignKey['notnull'],
192
                        'match'             => $tableForeignKey['match'],
193
                        'onUpdate'          => $tableForeignKey['on_update'],
194
                        'onDelete'          => $tableForeignKey['on_delete'],
195
                        'check_on_commit'   => $tableForeignKey['check_on_commit'],
196
                        'clustered'         => $tableForeignKey['clustered'],
197
                        'for_olap_workload' => $tableForeignKey['for_olap_workload']
198
                    ]
199
                ];
200 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201
                $foreignKeys[$tableForeignKey['index_name']]['local_columns'][] = $tableForeignKey['local_column'];
202
                $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
203
            }
204
        }
205
206
        return parent::_getPortableTableForeignKeysList($foreignKeys);
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212
    protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null)
213
    {
214
        foreach ($tableIndexRows as &$tableIndex) {
215
            $tableIndex['primary'] = (boolean) $tableIndex['primary'];
216
            $tableIndex['flags'] = [];
217
218
            if ($tableIndex['clustered']) {
219
                $tableIndex['flags'][] = 'clustered';
220
            }
221
222
            if ($tableIndex['with_nulls_not_distinct']) {
223
                $tableIndex['flags'][] = 'with_nulls_not_distinct';
224
            }
225
226
            if ($tableIndex['for_olap_workload']) {
227
                $tableIndex['flags'][] = 'for_olap_workload';
228
            }
229
        }
230
231
        return parent::_getPortableTableIndexesList($tableIndexRows, $tableName);
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237
    protected function _getPortableViewDefinition($view)
238
    {
239
        return new View(
240
            $view['table_name'],
241
            preg_replace('/^.*\s+as\s+SELECT(.*)/i', "SELECT$1", $view['view_def'])
242
        );
243
    }
244
}
245