MySQL::getTypeDecimal()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
rs 9.9332
cc 3
nc 3
nop 1
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   https://www.platine-php.com
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
{
57
    /**
58
     * @inheritdoc
59
     * @var string
60
     */
61
    protected string $identifier = '`%s`';
62
63
    /**
64
     * @inheritdoc
65
     */
66
    protected function getTypeInteger(BaseColumn $column): string
67
    {
68
        $type = 'INT';
69
        switch ($column->get('size', 'normal')) {
70
            case 'tiny':
71
                $type = 'TINYINT';
72
                break;
73
            case 'small':
74
                $type = 'SMALLINT';
75
                break;
76
            case 'medium':
77
                $type = 'MEDIUMINT';
78
                break;
79
            case 'big':
80
                $type = 'BIGINT';
81
                break;
82
        }
83
        return $type;
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    protected function getTypeDecimal(BaseColumn $column): string
90
    {
91
        $type = 'DECIMAL';
92
        $length = $column->get('length');
93
        $precision = $column->get('precision');
94
95
        if ($length !== null) {
96
            if ($precision === null) {
97
                $type = 'DECIMAL(' . $this->value($length) . ')';
98
            } else {
99
                $type = 'DECIMAL(' . $this->value($length) . ', '
100
                        . $this->value($precision) . ')';
101
            }
102
        }
103
104
        return $type;
105
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110
    protected function getTypeEnum(BaseColumn $column): string
111
    {
112
        $type = 'ENUM';
113
        $values = $column->get('values');
114
115
        if (!empty($values)) {
116
            $values = array_map([$this, 'value'], $values);
117
            $type = 'ENUM(' . implode(',', $values) . ')';
118
        }
119
120
        return $type;
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126
    protected function getTypeBoolean(BaseColumn $column): string
127
    {
128
        return 'TINYINT(1)';
129
    }
130
131
    /**
132
     * @inheritdoc
133
     */
134
    protected function getTypeText(BaseColumn $column): string
135
    {
136
        $type = 'TEXT';
137
        switch ($column->get('size', 'normal')) {
138
            case 'tiny':
139
            case 'small':
140
                $type = 'TINYTEXT';
141
                break;
142
            case 'medium':
143
                $type = 'MEDIUMTEXT';
144
                break;
145
            case 'big':
146
                $type = 'LONGTEXT';
147
                break;
148
        }
149
        return $type;
150
    }
151
152
    /**
153
     * @inheritdoc
154
     */
155
    protected function getTypeBinary(BaseColumn $column): string
156
    {
157
        $type = 'BLOB';
158
        switch ($column->get('size', 'normal')) {
159
            case 'tiny':
160
            case 'small':
161
                $type = 'TINYBLOB';
162
                break;
163
            case 'medium':
164
                $type = 'MEDIUMBLOB';
165
                break;
166
            case 'big':
167
                $type = 'LONGBLOB';
168
                break;
169
        }
170
        return $type;
171
    }
172
173
    /**
174
     * @inheritdoc
175
     */
176
    protected function getDropPrimaryKey(AlterTable $schema, mixed $data): string
177
    {
178
        return sprintf(
179
            'ALTER TABLE %s DROP PRIMARY KEY',
180
            $this->quoteIdentifier($schema->getTableName())
181
        );
182
    }
183
184
    /**
185
     * @inheritdoc
186
     */
187
    protected function getDropUniqueKey(AlterTable $schema, mixed $data): string
188
    {
189
        return sprintf(
190
            'ALTER TABLE %s DROP INDEX %s',
191
            $this->quoteIdentifier($schema->getTableName()),
192
            $this->quoteIdentifier($data)
193
        );
194
    }
195
196
    /**
197
     * @inheritdoc
198
     */
199
    protected function getDropIndex(AlterTable $schema, mixed $data): string
200
    {
201
        return sprintf(
202
            'ALTER TABLE %s DROP INDEX %s',
203
            $this->quoteIdentifier($schema->getTableName()),
204
            $this->quoteIdentifier($data)
205
        );
206
    }
207
208
    /**
209
     * @inheritdoc
210
     */
211
    protected function getDropForeignKey(AlterTable $schema, mixed $data): string
212
    {
213
        return sprintf(
214
            'ALTER TABLE %s DROP FOREIGN KEY %s',
215
            $this->quoteIdentifier($schema->getTableName()),
216
            $this->quoteIdentifier($data)
217
        );
218
    }
219
220
    /**
221
     * @inheritdoc
222
     */
223
    protected function getSetDefaultValue(AlterTable $schema, mixed $data): string
224
    {
225
        return sprintf(
226
            'ALTER TABLE %s ALTER %s SET DEFAULT %s',
227
            $this->quoteIdentifier($schema->getTableName()),
228
            $this->quoteIdentifier($data['column']),
229
            $this->value($data['value'])
230
        );
231
    }
232
233
    /**
234
     * @inheritdoc
235
     */
236
    protected function getDropDefaultValue(AlterTable $schema, mixed $data): string
237
    {
238
        return sprintf(
239
            'ALTER TABLE %s ALTER %s DROP DEFAULT',
240
            $this->quoteIdentifier($schema->getTableName()),
241
            $this->quoteIdentifier($data)
242
        );
243
    }
244
245
    /**
246
     * @inheritdoc
247
     */
248
    protected function getRenameColumn(AlterTable $schema, mixed $data): string
249
    {
250
        $tableName = $schema->getTableName();
251
        $columnName = $data['from'];
252
        /** @var BaseColumn  $column */
253
        $column = $data['column'];
254
        $newName = $column->getName();
255
256
        /** @var array<string, array<string, string>> $columns */
257
        $columns = $this->connection
258
                ->getSchema()
259
                ->getColumns($tableName, false, false);
260
261
        $columnType = 'integer';
262
        if (isset($columns[$columnName]) && isset($columns[$columnName]['type'])) {
263
            $columnType = $columns[$columnName]['type'];
264
        }
265
266
        return sprintf(
267
            'ALTER TABLE %s CHANGE %s %s %s',
268
            $this->quoteIdentifier($tableName),
269
            $this->quoteIdentifier($columnName),
270
            $this->quoteIdentifier($newName),
271
            $columnType
272
        );
273
    }
274
}
275