Passed
Push — develop ( e333c2...b3554d )
by nguereza
02:25
created

MySQL::getTypeEnum()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Platine Database
5
 *
6
 * Platine Database is the abstraction layer using PDO with support of query and schema builder
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Database
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file MySQL.php
33
 *
34
 *  The MySQL Driver class
35
 *
36
 *  @package    Platine\Database\Driver
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   http://www.iacademy.cf
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
declare(strict_types=1);
45
46
namespace Platine\Database\Driver;
47
48
use Platine\Database\Schema\AlterTable;
49
use Platine\Database\Schema\BaseColumn;
50
51
/**
52
 * Class MySQL
53
 * @package Platine\Database\Driver
54
 */
55
class MySQL extends Driver
56
{
0 ignored issues
show
Coding Style introduced by
Opening brace must not be followed by a blank line
Loading history...
57
58
    /**
59
     * @inheritdoc
60
     * @var string
61
     */
62
    protected string $identifier = '`%s`';
63
64
    /**
65
     * @inheritdoc
66
     */
67
    protected function getTypeInteger(BaseColumn $column): string
68
    {
69
        $type = 'INT';
70
        switch ($column->get('size', 'normal')) {
71
            case 'tiny':
72
                $type = 'TINYINT';
73
                break;
74
            case 'small':
75
                $type = 'SMALLINT';
76
                break;
77
            case 'medium':
78
                $type = 'MEDIUMINT';
79
                break;
80
            case 'big':
81
                $type = 'BIGINT';
82
                break;
83
        }
84
        return $type;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    protected function getTypeDecimal(BaseColumn $column): string
91
    {
92
        $type = 'DECIMAL';
93
        $length = $column->get('length');
94
        $precision = $column->get('precision');
95
96
        if ($length !== null) {
97
            if ($precision === null) {
98
                $type = 'DECIMAL(' . $this->value($length) . ')';
99
            } else {
100
                $type = 'DECIMAL(' . $this->value($length) . ', '
101
                        . $this->value($precision) . ')';
102
            }
103
        }
104
105
        return $type;
106
    }
107
    
108
    /**
109
     * @inheritdoc
110
     */
111
    protected function getTypeEnum(BaseColumn $column): string
112
    {
113
        $type = 'ENUM';
114
        $values = $column->get('values');
115
116
        if (!empty($values)) {
117
           $values = array_map([$this, 'value'], $values);
118
           $type = 'ENUM(' . implode(',', $values) . ')';
119
        }
120
121
        return $type;
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    protected function getTypeBoolean(BaseColumn $column): string
128
    {
129
        return 'TINYINT(1)';
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135
    protected function getTypeText(BaseColumn $column): string
136
    {
137
        $type = 'TEXT';
138
        switch ($column->get('size', 'normal')) {
139
            case 'tiny':
140
            case 'small':
141
                $type = 'TINYTEXT';
142
                break;
143
            case 'medium':
144
                $type = 'MEDIUMTEXT';
145
                break;
146
            case 'big':
147
                $type = 'LONGTEXT';
148
                break;
149
        }
150
        return $type;
151
    }
152
153
    /**
154
     * @inheritdoc
155
     */
156
    protected function getTypeBinary(BaseColumn $column): string
157
    {
158
        $type = 'BLOB';
159
        switch ($column->get('size', 'normal')) {
160
            case 'tiny':
161
            case 'small':
162
                $type = 'TINYBLOB';
163
                break;
164
            case 'medium':
165
                $type = 'MEDIUMBLOB';
166
                break;
167
            case 'big':
168
                $type = 'LONGBLOB';
169
                break;
170
        }
171
        return $type;
172
    }
173
174
    /**
175
     * @inheritdoc
176
     */
177
    protected function getDropPrimaryKey(AlterTable $schema, $data): string
178
    {
179
        return sprintf(
180
            'ALTER TABLE %s DROP PRIMARY KEY',
181
            $this->quoteIdentifier($schema->getTableName())
182
        );
183
    }
184
185
    /**
186
     * @inheritdoc
187
     */
188
    protected function getDropUniqueKey(AlterTable $schema, $data): string
189
    {
190
        return sprintf(
191
            'ALTER TABLE %s DROP INDEX %s',
192
            $this->quoteIdentifier($schema->getTableName()),
193
            $this->quoteIdentifier($data)
194
        );
195
    }
196
197
    /**
198
     * @inheritdoc
199
     */
200
    protected function getDropIndex(AlterTable $schema, $data): string
201
    {
202
        return sprintf(
203
            'ALTER TABLE %s DROP INDEX %s',
204
            $this->quoteIdentifier($schema->getTableName()),
205
            $this->quoteIdentifier($data)
206
        );
207
    }
208
209
    /**
210
     * @inheritdoc
211
     */
212
    protected function getDropForeignKey(AlterTable $schema, $data): string
213
    {
214
        return sprintf(
215
            'ALTER TABLE %s DROP FOREIGN KEY %s',
216
            $this->quoteIdentifier($schema->getTableName()),
217
            $this->quoteIdentifier($data)
218
        );
219
    }
220
221
    /**
222
     * @inheritdoc
223
     */
224
    protected function getSetDefaultValue(AlterTable $schema, $data): string
225
    {
226
        return sprintf(
227
            'ALTER TABLE %s ALTER %s SET DEFAULT %s',
228
            $this->quoteIdentifier($schema->getTableName()),
229
            $this->quoteIdentifier($data['column']),
230
            $this->value($data['value'])
231
        );
232
    }
233
234
    /**
235
     * @inheritdoc
236
     */
237
    protected function getDropDefaultValue(AlterTable $schema, $data): string
238
    {
239
        return sprintf(
240
            'ALTER TABLE %s ALTER %s DROP DEFAULT',
241
            $this->quoteIdentifier($schema->getTableName()),
242
            $this->quoteIdentifier($data)
243
        );
244
    }
245
246
    /**
247
     * @inheritdoc
248
     */
249
    protected function getRenameColumn(AlterTable $schema, $data): string
250
    {
251
        $tableName = $schema->getTableName();
252
        $columnName = $data['from'];
253
        /** @var BaseColumn  $column */
254
        $column = $data['column'];
255
        $newName = $column->getName();
256
257
        /** @var array<string, array<string, string>> $columns */
258
        $columns = $this->connection
259
                ->getSchema()
260
                ->getColumns($tableName, false, false);
261
262
        $columnType = 'integer';
263
        if (isset($columns[$columnName]) && isset($columns[$columnName]['type'])) {
264
            $columnType = $columns[$columnName]['type'];
265
        }
266
267
        return sprintf(
268
            'ALTER TABLE %s CHANGE %s %s %s',
269
            $this->quoteIdentifier($tableName),
270
            $this->quoteIdentifier($columnName),
271
            $this->quoteIdentifier($newName),
272
            $columnType
273
        );
274
    }
275
}
276