Test Failed
Pull Request — dev (#119)
by Wilmer
02:37
created

QueryBuilderPDOMysql::command()   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 0
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
        QuoterInterface $quoter,
54
        SchemaInterface $schema,
55
        CommandInterface $command
56
    ) {
57 355
        $this->ddlBuilder = new DDLQueryBuilder($command, $this, $quoter, $schema);
58 355
        $this->dmlBuilder = new DMLQueryBuilder($this, $quoter, $schema);
59 355
        $this->dqlBuilder = new DQLQueryBuilder($this, $quoter, $schema);
60 355
        parent::__construct($quoter, $schema, $this->ddlBuilder, $this->dmlBuilder, $this->dqlBuilder);
61
    }
62
63 3
    public function getColumnType(ColumnSchemaBuilder|string $type): string
64
    {
65 3
        $this->typeMap = array_merge($this->typeMap, $this->defaultTimeTypeMap());
66
        return parent::getColumnType($type);
67
    }
68 12
69
    /**
70 12
     * Returns the map for default time type.
71 12
     *
72
     * If the version of MySQL is lower than 5.6.4, then the types will be without fractional seconds, otherwise with
73
     * fractional seconds.
74
     *
75
     * @return array
76
     * @psalm-return array<string, string>
77
     */
78
    private function defaultTimeTypeMap(): array
79
    {
80
        return [
81
            Schema::TYPE_DATETIME => 'datetime(0)',
82
            Schema::TYPE_TIMESTAMP => 'timestamp(0)',
83 12
            Schema::TYPE_TIME => 'time(0)',
84
        ];
85
    }
86
}
87