Test Failed
Pull Request — master (#265)
by Wilmer
18:51 queued 15:04
created

DDLQueryBuilder::addCommentOnColumn()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4.0027

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 21
c 1
b 0
f 0
nc 8
nop 3
dl 0
loc 33
ccs 17
cts 18
cp 0.9444
crap 4.0027
rs 9.584
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\NotSupportedException;
10
use Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder;
11
12
use function preg_match;
13
use function preg_replace;
14
use function trim;
15
16
/**
17
 * Implements a (Data Definition Language) SQL statements for MySQL, MariaDB.
18
 */
19
final class DDLQueryBuilder extends AbstractDDLQueryBuilder
20
{
21
    /**
22
     * @throws NotSupportedException
23
     */
24 522
    public function addCheck(string $table, string $name, string $expression): string
25
    {
26
        throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.');
27
    }
28
29 522
    /**
30
     * @throws Throwable
31
     */
32
    public function addCommentOnColumn(string $table, string $column, string $comment): string
33
    {
34
        /* Strip existing comment which may include escaped quotes */
35 2
        $definition = trim(
36
            preg_replace(
37 2
                "/COMMENT '(?:''|[^'])*'/i",
38
                '',
39
                $this->getColumnDefinition($table, $column)
40
            )
41
        );
42
43 5
        $checkRegex = '/CHECK *(\(([^()]|(?-2))*\))/';
44
        $check = preg_match($checkRegex, $definition, $checkMatches);
45
46 5
        if ($check === 1) {
47 5
            $definition = preg_replace($checkRegex, '', $definition);
48 5
        }
49 5
50 5
        $alterSql = 'ALTER TABLE '
51 5
            . $this->quoter->quoteTableName($table)
0 ignored issues
show
Bug introduced by
The property quoter is declared private in Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder and cannot be accessed from this context.
Loading history...
52 5
            . ' CHANGE '
53
            . $this->quoter->quoteColumnName($column)
54 5
            . ' '
55 5
            . $this->quoter->quoteColumnName($column)
56
            . (empty($definition) ? '' : ' ' . $definition)
57 5
            . ' COMMENT '
58
            . (string) $this->quoter->quoteValue($comment);
59
60
        if ($check === 1) {
61 5
            $alterSql .= ' ' . $checkMatches[0];
62 5
        }
63 5
64 5
        return $alterSql;
65 5
    }
66 5
67 5
    public function addCommentOnTable(string $table, string $comment): string
68 5
    {
69 5
        return 'ALTER TABLE '
70
            . $this->quoter->quoteTableName($table)
0 ignored issues
show
Bug introduced by
The property quoter is declared private in Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder and cannot be accessed from this context.
Loading history...
71 5
            . ' COMMENT '
72
            . (string) $this->quoter->quoteValue($comment);
73
    }
74
75 5
    public function addDefaultValue(string $table, string $name, string $column, mixed $value): string
76
    {
77
        throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.');
78 4
    }
79
80 4
    public function createIndex(
81 4
        string $table,
82 4
        string $name,
83 4
        array|string $columns,
84
        string $indexType = null,
85
        string $indexMethod = null
86 2
    ): string {
87
        return 'CREATE ' . ($indexType ? ($indexType . ' ') : '') . 'INDEX '
88 2
            . $this->quoter->quoteTableName($name)
0 ignored issues
show
Bug introduced by
The property quoter is declared private in Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder and cannot be accessed from this context.
Loading history...
89
            . ($indexMethod !== null ? " USING $indexMethod" : '')
90
            . ' ON ' . $this->quoter->quoteTableName($table)
91 10
            . ' (' . $this->queryBuilder->buildColumns($columns) . ')';
0 ignored issues
show
Bug introduced by
The property queryBuilder is declared private in Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder and cannot be accessed from this context.
Loading history...
92
    }
93
94
    public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string
95
    {
96
        return 'SET FOREIGN_KEY_CHECKS = ' . ($check ? 1 : 0);
97
    }
98 10
99 10
    /**
100 10
     * @throws NotSupportedException
101 10
     */
102 10
    public function dropCheck(string $table, string $name): string
103
    {
104
        throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.');
105 1
    }
106
107 1
    /**
108
     * @throws Exception
109
     * @throws Throwable
110
     */
111
    public function dropCommentFromColumn(string $table, string $column): string
112
    {
113 1
        return $this->addCommentOnColumn($table, $column, '');
114
    }
115 1
116
    /**
117
     * @throws \Exception
118
     */
119
    public function dropCommentFromTable(string $table): string
120
    {
121
        return $this->addCommentOnTable($table, '');
122 2
    }
123
124 2
    public function dropDefaultValue(string $table, string $name): string
125
    {
126
        throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.');
127
    }
128
129
    public function dropForeignKey(string $table, string $name): string
130 2
    {
131
        return 'ALTER TABLE '
132 2
            . $this->quoter->quoteTableName($table)
0 ignored issues
show
Bug introduced by
The property quoter is declared private in Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder and cannot be accessed from this context.
Loading history...
133
            . ' DROP FOREIGN KEY '
134
            . $this->quoter->quoteColumnName($name);
135 1
    }
136
137 1
    public function dropPrimaryKey(string $table, string $name): string
138
    {
139
        return 'ALTER TABLE ' . $this->quoter->quoteTableName($table) . ' DROP PRIMARY KEY';
0 ignored issues
show
Bug introduced by
The property quoter is declared private in Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder and cannot be accessed from this context.
Loading history...
140 2
    }
141
142 2
    public function dropUnique(string $table, string $name): string
143 2
    {
144 2
        return $this->dropIndex($table, $name);
145 2
    }
146
147
    /**
148 3
     * @throws Exception
149
     * @throws Throwable
150 3
     */
151
    public function renameColumn(string $table, string $oldName, string $newName): string
152
    {
153 3
        $quotedTable = $this->quoter->quoteTableName($table);
0 ignored issues
show
Bug introduced by
The property quoter is declared private in Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder and cannot be accessed from this context.
Loading history...
154
155 3
        $columnDefinition = $this->getColumnDefinition($table, $oldName);
156
157
        /* try to give back an SQL anyway */
158
        return "ALTER TABLE $quotedTable CHANGE "
159
            . $this->quoter->quoteColumnName($oldName) . ' '
160
            . $this->quoter->quoteColumnName($newName)
161
            . (!empty($columnDefinition) ? ' ' . $columnDefinition : '');
162 2
    }
163
164 2
    /**
165
     * Gets column definition.
166 2
     *
167
     * @param string $table The table name.
168
     * @param string $column The column name.
169 2
     *
170 2
     * @throws Throwable In case when table doesn't contain a column.
171 2
     *
172 2
     * @return string The column definition.
173
     */
174
    public function getColumnDefinition(string $table, string $column): string
175
    {
176
        $result = '';
177
        $sql = $this->schema->getTableSchema($table)?->getCreateSql();
0 ignored issues
show
Bug introduced by
The property schema is declared private in Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder and cannot be accessed from this context.
Loading history...
178
179
        if (empty($sql)) {
180
            return '';
181
        }
182
183
        if (preg_match_all('/^\s*([`"])(.*?)\\1\s+(.*?),?$/m', $sql, $matches)) {
184
            foreach ($matches[2] as $i => $c) {
185 7
                if ($c === $column) {
186
                    $result = $matches[3][$i];
187 7
                }
188 7
            }
189
        }
190 7
191
        return $result;
192
    }
193
}
194