Passed
Pull Request — master (#28)
by
unknown
06:43
created

Connection   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 70.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 67
ccs 17
cts 24
cp 0.7083
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createCommand() 0 9 2
A initConnection() 0 6 3
A isSybase() 0 3 1
A getDriverName() 0 3 1
A sybase() 0 3 1
A createPdoInstance() 0 9 2
A getSchema() 0 3 1
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
10
use function in_array;
11
12
/**
13
 * Database connection class prefilled for MSSQL Server.
14
 */
15
final class Connection extends AbstractConnection
16
{
17
    private bool $isSybase = false;
18
    private ?Schema $schema = null;
19
20 195
    public function createCommand(?string $sql = null, array $params = []): Command
21
    {
22 195
        if ($sql !== null) {
23 195
            $sql = $this->quoteSql($sql);
24
        }
25
26 195
        $command = new Command($this->getProfiler(), $this->getLogger(), $this, $sql);
27
28 195
        return $command->bindValues($params);
29
    }
30
31
    /**
32
     * Returns the schema information for the database opened by this connection.
33
     *
34
     * @return Schema the schema information for the database opened by this connection.
35
     */
36 326
    public function getSchema(): Schema
37
    {
38 326
        return $this->schema ?? ($this->schema = new Schema($this));
39
    }
40
41
    public function isSybase(): bool
42
    {
43
        return $this->isSybase;
44
    }
45
46
    /**
47
     * @param bool $value set the database connected via pdo_dblib is SyBase, for default it's false.
48
     */
49
    public function sybase(bool $value): void
50
    {
51
        $this->isSybase = $value;
52
    }
53
54 198
    protected function createPdoInstance(): \PDO
55
    {
56 198
        if ($this->getDriverName() === 'sqlsrv') {
57 198
            $pdo = new SqlsrvPDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getAttributes());
58
        } else {
59
            $pdo = new PDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getAttributes());
60
        }
61
62 198
        return $pdo;
63
    }
64
65 198
    protected function initConnection(): void
66
    {
67 198
        $this->getPDO()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
68
69 198
        if (!$this->isSybase && in_array($this->getDriverName(), ['mssql', 'dblib'], true)) {
70
            $this->getPDO()->exec('SET ANSI_NULL_DFLT_ON ON');
71
        }
72 198
    }
73
74
    /**
75
     * Returns the name of the DB driver.
76
     *
77
     * @return string name of the DB driver
78
     */
79 199
    public function getDriverName(): string
80
    {
81 199
        return 'sqlsrv';
82
    }
83
}
84