Passed
Push — master ( 9b125d...f5b0c0 )
by Alexander
11:25 queued 07:35
created

QueryBuilder::insert()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 20
nc 16
nop 3
dl 0
loc 30
ccs 18
cts 18
cp 1
crap 8
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
use Yiisoft\Db\QueryBuilder\QueryBuilder as AbstractQueryBuilder;
8
use Yiisoft\Db\Schema\ColumnSchemaBuilder;
9
use Yiisoft\Db\Schema\QuoterInterface;
10
use Yiisoft\Db\Schema\Schema;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Db\Mssql\Schema. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
use Yiisoft\Db\Schema\SchemaInterface;
12
13
use function preg_replace;
14
15
final class QueryBuilder extends AbstractQueryBuilder
16
{
17
    /**
18
     * Defines a CLUSTERED index type for {@see createIndex()}.
19
     */
20
    public const INDEX_CLUSTERED = 'CLUSTERED';
21
22
    /**
23
     * Defines a CLUSTERED index type for {@see createIndex()}.
24
     */
25
    public const INDEX_NONCLUSTERED = 'NONCLUSTERED';
26
27
    /**
28
     * @psalm-var string[] $typeMap Mapping from abstract column types (keys) to physical column types (values).
29
     */
30
    protected array $typeMap = [
31
        Schema::TYPE_PK => 'int IDENTITY PRIMARY KEY',
32
        Schema::TYPE_UPK => 'int IDENTITY PRIMARY KEY',
33
        Schema::TYPE_BIGPK => 'bigint IDENTITY PRIMARY KEY',
34
        Schema::TYPE_UBIGPK => 'bigint IDENTITY PRIMARY KEY',
35
        Schema::TYPE_CHAR => 'nchar(1)',
36
        Schema::TYPE_STRING => 'nvarchar(255)',
37
        Schema::TYPE_TEXT => 'nvarchar(max)',
38
        Schema::TYPE_TINYINT => 'tinyint',
39
        Schema::TYPE_SMALLINT => 'smallint',
40
        Schema::TYPE_INTEGER => 'int',
41
        Schema::TYPE_BIGINT => 'bigint',
42
        Schema::TYPE_FLOAT => 'float',
43
        Schema::TYPE_DOUBLE => 'float',
44
        Schema::TYPE_DECIMAL => 'decimal(18,0)',
45
        Schema::TYPE_DATETIME => 'datetime',
46
        Schema::TYPE_TIMESTAMP => 'datetime',
47
        Schema::TYPE_TIME => 'time',
48
        Schema::TYPE_DATE => 'date',
49
        Schema::TYPE_BINARY => 'varbinary(max)',
50
        Schema::TYPE_BOOLEAN => 'bit',
51
        Schema::TYPE_MONEY => 'decimal(19,4)',
52
    ];
53
    private DDLQueryBuilder $ddlBuilder;
54
    private DMLQueryBuilder $dmlBuilder;
55
    private DQLQueryBuilder $dqlBuilder;
56
57 386
    public function __construct(QuoterInterface $quoter, SchemaInterface $schema)
58
    {
59 386
        $this->ddlBuilder = new DDLQueryBuilder($this, $quoter, $schema);
60 386
        $this->dmlBuilder = new DMLQueryBuilder($this, $quoter, $schema);
61 386
        $this->dqlBuilder = new DQLQueryBuilder($this, $quoter, $schema);
62 386
        parent::__construct($quoter, $schema, $this->ddlBuilder, $this->dmlBuilder, $this->dqlBuilder);
63
    }
64
65 13
    public function getColumnType(ColumnSchemaBuilder|string $type): string
66
    {
67 13
        $columnType = parent::getColumnType($type);
68
69
        /** remove unsupported keywords*/
70 13
        $columnType = preg_replace("/\s*comment '.*'/i", '', $columnType);
71 13
        return preg_replace('/ first$/i', '', $columnType);
72
    }
73
}
74