Passed
Pull Request — master (#24)
by Wilmer
13:33
created

Connection::initConnection()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 3
cts 4
cp 0.75
crap 3.1406
rs 10
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\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 193
    public function createCommand(?string $sql = null, array $params = []): Command
24
    {
25 193
        if ($sql !== null) {
26 193
            $sql = $this->quoteSql($sql);
27
        }
28
29 193
        $command = new Command($this->getProfiler(), $this->getLogger(), $this, $sql);
30
31 193
        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 324
    public function getSchema(): Schema
40
    {
41 324
        if ($this->schema !== null) {
42 242
            return $this->schema;
43
        }
44
45 324
        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 196
    protected function createPdoInstance(): \PDO
62
    {
63 196
        if ($this->getDriverName() === 'sqlsrv') {
64 196
            $pdo = new SqlsrvPDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getAttributes());
65
        } else {
66 3
            $pdo = new PDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getAttributes());
67
        }
68
69 196
        return $pdo;
70
    }
71
72 196
    protected function initConnection(): void
73
    {
74 196
        $this->getPDO()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
75
76 196
        if (!$this->isSybase && in_array($this->getDriverName(), ['mssql', 'dblib'], true)) {
77
            $this->getPDO()->exec('SET ANSI_NULL_DFLT_ON ON');
78
        }
79 196
    }
80
81
    /**
82
     * Returns the name of the DB driver.
83
     *
84
     * @return string name of the DB driver
85
     */
86
    public function getDriverName(): string
87
    {
88
        return 'sqlsrv';
89
    }
90
}
91