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

Connection::createCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
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
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