Passed
Push — master ( e5e54a...8779b5 )
by Wilmer
10:52
created

Connection   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createCommand() 0 9 2
A initConnection() 0 6 3
A isSybase() 0 3 1
A sybase() 0 3 1
A createPdoInstance() 0 9 2
A getSchema() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
use Yiisoft\Db\Connection\Connection as AbstractConnection;
8
use Yiisoft\Db\Command\Command;
9
use Yiisoft\Db\Mssql\PDO;
10
use Yiisoft\Db\Mssql\SqlsrvPDO;
11
use Yiisoft\Db\Mssql\Schema;
12
13
use function in_array;
14
15
/**
16
 * Database connection class prefilled for MSSQL Server.
17
 */
18
final class Connection extends AbstractConnection
19
{
20
    private bool $isSybase = false;
21
    private ?Schema $schema = null;
22
23
    public function createCommand(?string $sql = null, array $params = []): Command
24
    {
25
        if ($sql !== null) {
26
            $sql = $this->quoteSql($sql);
27
        }
28
29
        $command = new Command($this->getProfiler(), $this->getLogger(), $this, $sql);
0 ignored issues
show
Bug introduced by
The method getProfiler() does not exist on Yiisoft\Db\Mssql\Connection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        $command = new Command($this->/** @scrutinizer ignore-call */ getProfiler(), $this->getLogger(), $this, $sql);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getLogger() does not exist on Yiisoft\Db\Mssql\Connection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        $command = new Command($this->getProfiler(), $this->/** @scrutinizer ignore-call */ getLogger(), $this, $sql);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
31
        return $command->bindValues($params);
32
    }
33
34
    /**
35
     * Returns the schema information for the database opened by this connection.
36
     *
37
     * @return Schema the schema information for the database opened by this connection.
38
     */
39
    public function getSchema(): Schema
40
    {
41
        if ($this->schema !== null) {
42
            return $this->schema;
43
        }
44
45
        return $this->schema = new Schema($this);
46
    }
47
48
    public function isSybase(): bool
49
    {
50
        return $this->isSybase;
51
    }
52
53
    /**
54
     * @param bool $value set the database connected via pdo_dblib is SyBase, for default it's false.
55
     */
56
    public function sybase(bool $value): void
57
    {
58
        $this->isSybase = $value;
59
    }
60
61
    protected function createPdoInstance(): \PDO
62
    {
63
        if ($this->getDriverName() === 'sqlsrv') {
64
            $pdo = new SqlsrvPDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getAttributes());
65
        } else {
66
            $pdo = new PDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getAttributes());
67
        }
68
69
        return $pdo;
70
    }
71
72
    protected function initConnection(): void
73
    {
74
        $this->getPDO()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
75
76
        if (!$this->isSybase && in_array($this->getDriverName(), ['mssql', 'dblib'], true)) {
77
            $this->getPDO()->exec('SET ANSI_NULL_DFLT_ON ON');
78
        }
79
    }
80
}
81