QueryBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder;
8
use Yiisoft\Db\Schema\Builder\ColumnInterface;
9
use Yiisoft\Db\Schema\QuoterInterface;
10
use Yiisoft\Db\Schema\SchemaInterface;
11
12
use function preg_replace;
13
14
/**
15
 * Implements the MSSQL Server specific query builder.
16
 */
17
final class QueryBuilder extends AbstractQueryBuilder
18
{
19
    /**
20
     * @psalm-var string[] $typeMap Mapping from abstract column types (keys) to physical column types (values).
21
     */
22
    protected array $typeMap = [
23
        SchemaInterface::TYPE_PK => 'int IDENTITY PRIMARY KEY',
24
        SchemaInterface::TYPE_UPK => 'int IDENTITY PRIMARY KEY',
25
        SchemaInterface::TYPE_BIGPK => 'bigint IDENTITY PRIMARY KEY',
26
        SchemaInterface::TYPE_UBIGPK => 'bigint IDENTITY PRIMARY KEY',
27
        SchemaInterface::TYPE_CHAR => 'nchar(1)',
28
        SchemaInterface::TYPE_STRING => 'nvarchar(255)',
29
        SchemaInterface::TYPE_TEXT => 'nvarchar(max)',
30
        SchemaInterface::TYPE_TINYINT => 'tinyint',
31
        SchemaInterface::TYPE_SMALLINT => 'smallint',
32
        SchemaInterface::TYPE_INTEGER => 'int',
33
        SchemaInterface::TYPE_BIGINT => 'bigint',
34
        SchemaInterface::TYPE_FLOAT => 'float',
35
        SchemaInterface::TYPE_DOUBLE => 'float',
36
        SchemaInterface::TYPE_DECIMAL => 'decimal(18,0)',
37
        SchemaInterface::TYPE_DATETIME => 'datetime',
38
        SchemaInterface::TYPE_TIMESTAMP => 'datetime',
39
        SchemaInterface::TYPE_TIME => 'time',
40
        SchemaInterface::TYPE_DATE => 'date',
41
        SchemaInterface::TYPE_BINARY => 'varbinary(max)',
42
        SchemaInterface::TYPE_BOOLEAN => 'bit',
43
        SchemaInterface::TYPE_MONEY => 'decimal(19,4)',
44
        SchemaInterface::TYPE_UUID => 'UNIQUEIDENTIFIER',
45
        SchemaInterface::TYPE_UUID_PK => 'UNIQUEIDENTIFIER PRIMARY KEY',
46
    ];
47
48 924
    public function __construct(QuoterInterface $quoter, SchemaInterface $schema)
49
    {
50 924
        $ddlBuilder = new DDLQueryBuilder($this, $quoter, $schema);
51 924
        $dmlBuilder = new DMLQueryBuilder($this, $quoter, $schema);
52 924
        $dqlBuilder = new DQLQueryBuilder($this, $quoter);
53
54 924
        parent::__construct($quoter, $schema, $ddlBuilder, $dmlBuilder, $dqlBuilder);
55
    }
56
57 194
    public function getColumnType(ColumnInterface|string $type): string
58
    {
59 194
        $columnType = parent::getColumnType($type);
60
61
        /** remove unsupported keywords*/
62 194
        $columnType = preg_replace("/\s*comment '.*'/i", '', $columnType);
63 194
        return preg_replace('/ first$/i', '', $columnType);
64
    }
65
}
66