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

QueryBuilderPDOMysql::addCommentOnColumn()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql\PDO;
6
7
use Yiisoft\Db\Command\CommandInterface;
8
use Yiisoft\Db\Mysql\DDLQueryBuilder;
9
use Yiisoft\Db\Mysql\DMLQueryBuilder;
10
use Yiisoft\Db\Mysql\DQLQueryBuilder;
11
use Yiisoft\Db\Query\QueryBuilder;
12
use Yiisoft\Db\Schema\ColumnSchemaBuilder;
13
use Yiisoft\Db\Schema\QuoterInterface;
14
use Yiisoft\Db\Schema\Schema;
15
use Yiisoft\Db\Schema\SchemaInterface;
16
17
use function array_merge;
18
19
/**
20
 * The class QueryBuilder is the query builder for Mysql databases.
21
 */
22
final class QueryBuilderPDOMysql extends QueryBuilder
23
{
24
    /**
25
     * @psalm-var string[] $typeMap Mapping from abstract column types (keys) to physical column types (values).
26
     */
27
    protected array $typeMap = [
28
        Schema::TYPE_PK => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY',
29
        Schema::TYPE_UPK => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY',
30
        Schema::TYPE_BIGPK => 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY',
31
        Schema::TYPE_UBIGPK => 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY',
32
        Schema::TYPE_CHAR => 'char(1)',
33
        Schema::TYPE_STRING => 'varchar(255)',
34
        Schema::TYPE_TEXT => 'text',
35
        Schema::TYPE_TINYINT => 'tinyint(3)',
36
        Schema::TYPE_SMALLINT => 'smallint(6)',
37
        Schema::TYPE_INTEGER => 'int(11)',
38
        Schema::TYPE_BIGINT => 'bigint(20)',
39
        Schema::TYPE_FLOAT => 'float',
40
        Schema::TYPE_DOUBLE => 'double',
41
        Schema::TYPE_DECIMAL => 'decimal(10,0)',
42
        Schema::TYPE_DATE => 'date',
43
        Schema::TYPE_BINARY => 'blob',
44
        Schema::TYPE_BOOLEAN => 'tinyint(1)',
45
        Schema::TYPE_MONEY => 'decimal(19,4)',
46
        Schema::TYPE_JSON => 'json',
47
    ];
48
    private DDLQueryBuilder $ddlBuilder;
49
    private DMLQueryBuilder $dmlBuilder;
50
    private DQLQueryBuilder $dqlBuilder;
51
52 355
    public function __construct(
53
        protected QuoterInterface $quoter,
54
        protected SchemaInterface $schema,
55
        private CommandInterface $command
56
    ) {
57 355
        $this->ddlBuilder = new DDLQueryBuilder($this);
58 355
        $this->dmlBuilder = new DMLQueryBuilder($this);
59 355
        $this->dqlBuilder = new DQLQueryBuilder($this);
60 355
        parent::__construct($quoter, $schema, $this->ddlBuilder, $this->dmlBuilder, $this->dqlBuilder);
61
    }
62
63 3
    public function command(): CommandInterface
64
    {
65 3
        return $this->command;
66
    }
67
68 12
    public function getColumnType(ColumnSchemaBuilder|string $type): string
69
    {
70 12
        $this->typeMap = array_merge($this->typeMap, $this->defaultTimeTypeMap());
71 12
        return parent::getColumnType($type);
72
    }
73
74
    /**
75
     * Returns the map for default time type.
76
     *
77
     * If the version of MySQL is lower than 5.6.4, then the types will be without fractional seconds, otherwise with
78
     * fractional seconds.
79
     *
80
     * @return array
81
     * @psalm-return array<string, string>
82
     */
83 12
    private function defaultTimeTypeMap(): array
84
    {
85
        return [
86 12
            Schema::TYPE_DATETIME => 'datetime(0)',
87
            Schema::TYPE_TIMESTAMP => 'timestamp(0)',
88
            Schema::TYPE_TIME => 'time(0)',
89
        ];
90
    }
91
}
92