Passed
Push — dev ( 1e6c50...6dcd8f )
by Def
05:36 queued 03:04
created

DDLQueryBuilder::dropUnique()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql;
6
7
use Throwable;
8
use Yiisoft\Db\Exception\Exception;
9
use Yiisoft\Db\Exception\InvalidArgumentException;
10
use Yiisoft\Db\Exception\NotSupportedException;
11
use Yiisoft\Db\Mysql\PDO\QueryBuilderPDOMysql;
12
use Yiisoft\Db\Query\DDLQueryBuilder as AbstractDDLQueryBuilder;
13
use Yiisoft\Db\Query\QueryBuilderInterface;
14
15
use function array_values;
16
use function in_array;
17
use function preg_match;
18
use function preg_replace;
19
use function trim;
20
21
final class DDLQueryBuilder extends AbstractDDLQueryBuilder
22
{
23 355
    public function __construct(private QueryBuilderInterface $queryBuilder)
24
    {
25 355
        parent::__construct($queryBuilder);
26
    }
27
28 1
    public function addCheck(string $name, string $table, string $expression): string
29
    {
30 1
        throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.');
31
    }
32
33
    /**
34
     * @throws Exception|Throwable
35
     */
36 1
    public function addCommentOnColumn(string $table, string $column, string $comment): string
37
    {
38
        /* Strip existing comment which may include escaped quotes */
39 1
        $definition = trim(
40 1
            preg_replace(
41
                "/COMMENT '(?:''|[^'])*'/i",
42
                '',
43 1
                $this->getColumnDefinition($table, $column)
44
            )
45
        );
46
47 1
        $checkRegex = '/CHECK *(\(([^()]|(?-2))*\))/';
48 1
        $check = preg_match($checkRegex, $definition, $checkMatches);
49
50 1
        if ($check === 1) {
51
            $definition = preg_replace($checkRegex, '', $definition);
52
        }
53
54 1
        $alterSql = 'ALTER TABLE '
55 1
            . $this->quoter->quoteTableName($table)
56
            . ' CHANGE '
57 1
            . $this->quoter->quoteColumnName($column)
58
            . ' '
59 1
            . $this->quoter->quoteColumnName($column)
60 1
            . (empty($definition) ? '' : ' ' . $definition)
61
            . ' COMMENT '
62 1
            . (string) $this->quoter->quoteValue($comment);
63
64 1
        if ($check === 1) {
65
            $alterSql .= ' ' . $checkMatches[0];
66
        }
67
68 1
        return $alterSql;
69
    }
70
71
    /**
72
     * @throws \Exception
73
     */
74 1
    public function addCommentOnTable(string $table, string $comment): string
75
    {
76
        return 'ALTER TABLE '
77 1
            . $this->quoter->quoteTableName($table)
78
            . ' COMMENT '
79 1
            . (string) $this->quoter->quoteValue($comment);
80
    }
81
82
    /**
83
     * @throws Exception|InvalidArgumentException
84
     */
85 6
    public function createIndex(string $name, string $table, array|string $columns, bool $unique = false): string
86
    {
87
        return 'ALTER TABLE '
88 6
            . $this->quoter->quoteTableName($table)
89 6
            . ($unique ? ' ADD UNIQUE INDEX ' : ' ADD INDEX ')
90 6
            . $this->quoter->quoteTableName($name)
91 6
            . ' (' . $this->queryBuilder->buildColumns($columns) . ')';
92
    }
93
94 1
    public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string
95
    {
96 1
        return 'SET FOREIGN_KEY_CHECKS = ' . ($check ? 1 : 0);
97
    }
98
99 1
    public function dropCheck(string $name, string $table): string
100
    {
101 1
        throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.');
102
    }
103
104 1
    public function dropCommentFromColumn(string $table, string $column): string
105
    {
106 1
        return $this->addCommentOnColumn($table, $column, '');
107
    }
108
109
    /**
110
     * @throws \Exception
111
     */
112 1
    public function dropCommentFromTable(string $table): string
113
    {
114 1
        return $this->addCommentOnTable($table, '');
115
    }
116
117 2
    public function dropForeignKey(string $name, string $table): string
118
    {
119
        return 'ALTER TABLE '
120 2
            . $this->quoter->quoteTableName($table)
121
            . ' DROP FOREIGN KEY '
122 2
            . $this->quoter->quoteColumnName($name);
123
    }
124
125 2
    public function dropPrimaryKey(string $name, string $table): string
126
    {
127 2
        return 'ALTER TABLE ' . $this->quoter->quoteTableName($table) . ' DROP PRIMARY KEY';
128
    }
129
130 2
    public function dropUnique(string $name, string $table): string
131
    {
132 2
        return $this->dropIndex($name, $table);
133
    }
134
135
    /**
136
     * @throws Exception|Throwable
137
     */
138 2
    public function renameColumn(string $table, string $oldName, string $newName): string
139
    {
140 2
        $quotedTable = $this->quoter->quoteTableName($table);
141
142 2
        $columnDefinition = $this->getColumnDefinition($table, $oldName);
143
144
        /* try to give back a SQL anyway */
145 1
        return "ALTER TABLE $quotedTable CHANGE "
146 1
            . $this->quoter->quoteColumnName($oldName) . ' '
147 1
            . $this->quoter->quoteColumnName($newName)
148 1
            . (!empty($columnDefinition) ? ' ' . $columnDefinition : '');
149
    }
150
151
    /**
152
     * @todo need work with bit, tinybit and other new not suppoerted types (v.5.7 and later)
153
     */
154
//    public function getColumnDefinitionFromSchema($table, $oldName): string
155
//    {
156
//        $schema = $this->schema;
157
//        $tableSchema = $schema->getTableSchema($table, true);
158
//        if ($tableSchema === null) {
159
//            throw new InvalidArgumentException("Table not found: $table");
160
//        }
161
//
162
//        $oldColumnSchema = $tableSchema->getColumn($oldName);
163
//        if ($oldColumnSchema === null) {
164
//            return '';
165
//        }
166
//
167
//        $columnSchemaBuilder = $this->schema->createColumnSchemaBuilder(
168
//            $oldColumnSchema->getType(),
169
//            $oldColumnSchema->getPrecision() ?? $oldColumnSchema->getSize()
170
//        );
171
//
172
//        $defaultValue = $oldColumnSchema->getDefaultValue();
173
//
174
//        if ($oldColumnSchema->isAllowNull()) {
175
//            if ($defaultValue === null) {
176
//                if (!in_array($oldColumnSchema->getType(), [Schema::TYPE_TEXT, Schema::TYPE_BINARY], true)) {
177
//                    $columnSchemaBuilder->defaultValue('NULL');
178
//                }
179
//                if ($oldColumnSchema->getType() === Schema::TYPE_TIMESTAMP) {
180
//                    $columnSchemaBuilder->null();
181
//                }
182
//            } elseif ($oldColumnSchema->getType() !== Schema::TYPE_BINARY) {
183
//                $columnSchemaBuilder->defaultValue($defaultValue);
184
//            }
185
//        } else {
186
//            $columnSchemaBuilder->notNull();
187
//            if ($defaultValue !== null && ($oldColumnSchema->getDbType() !== 'bit(1)' || !empty($defaultValue))) {
188
//                $columnSchemaBuilder->defaultValue($defaultValue);
189
//            }
190
//        }
191
//
192
//        if (!empty($oldColumnSchema->getComment())) {
193
//            $columnSchemaBuilder->comment($oldColumnSchema->getComment());
194
//        }
195
//
196
//        if ($oldColumnSchema->isUnsigned()) {
197
//            $columnSchemaBuilder->unsigned();
198
//        }
199
//
200
//        if ($oldColumnSchema->isAutoIncrement()) {
201
//            $columnSchemaBuilder->append('AUTO_INCREMENT');
202
//        } elseif (!empty($oldColumnSchema->getExtra())) {
203
//            $columnSchemaBuilder->append($oldColumnSchema->getExtra());
204
//        }
205
//
206
//        return $this->queryBuilder->getColumnType($columnSchemaBuilder);
207
//    }
208
209
    /**
210
     * Gets column definition.
211
     *
212
     * @param string $table table name.
213
     * @param string $column column name.
214
     *
215
     * @throws Exception|Throwable in case when table does not contain column.
216
     *
217
     * @return string the column definition.
218
     *
219
     * @todo need change to getColumnDefinitionFromSchema with deep research
220
     */
221 3
    public function getColumnDefinition(string $table, string $column): string
222
    {
223 3
        $result = '';
224 3
        $quotedTable = $this->quoter->quoteTableName($table);
225
226
        /** @var QueryBuilderPDOMysql $qb */
227 3
        $qb = $this->queryBuilder;
228
229
        /** @var array<array-key, string> $row */
230 3
        $row = $qb->command()->setSql('SHOW CREATE TABLE ' . $quotedTable)->queryOne();
231
232 2
        if (!isset($row['Create Table'])) {
233
            $row = array_values($row);
0 ignored issues
show
Bug introduced by
$row of type false is incompatible with the type array expected by parameter $array of array_values(). ( Ignorable by Annotation )

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

233
            $row = array_values(/** @scrutinizer ignore-type */ $row);
Loading history...
234
            $sql = $row[1];
235
        } else {
236 2
            $sql = $row['Create Table'];
237
        }
238
239 2
        if (preg_match_all('/^\s*`(.*?)`\s+(.*?),?$/m', $sql, $matches)) {
240 2
            foreach ($matches[1] as $i => $c) {
241 2
                if ($c === $column) {
242 2
                    $result = $matches[2][$i];
243
                }
244
            }
245
        }
246
247 2
        return $result;
248
    }
249
}
250