Passed
Push — master ( add693...b324e9 )
by Wilmer
13:51
created

MssqlConnection::initConnection()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql\Connection;
6
7
use Yiisoft\Db\Connection\Connection;
8
use Yiisoft\Db\Mssql\Pdo\PDO;
9
use Yiisoft\Db\Mssql\Pdo\SqlsrvPDO;
10
use Yiisoft\Db\Mssql\Schema\MssqlSchema;
11
12
use function in_array;
13
14
/**
15
 * Database connection class prefilled for MSSQL Server.
16
 */
17
final class MssqlConnection extends Connection
18
{
19
    protected array $schemaMap = [
20
        'sqlsrv' => Schema::class, // newer MSSQL driver on MS Windows hosts
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Mssql\Connection\Schema was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
        'mssql' => Schema::class, // older MSSQL driver on MS Windows hosts
22
        'dblib' => Schema::class, // dblib drivers on GNU/Linux (and maybe other OSes) hosts
23
    ];
24
25
    private bool $isSybase = false;
26
    private ?MssqlSchema $schema = null;
27
28
    /**
29
     * Returns the schema information for the database opened by this connection.
30
     *
31
     * @return MssqlSchema the schema information for the database opened by this connection.
32
     */
33
    public function getSchema(): MssqlSchema
34
    {
35
        if ($this->schema !== null) {
36
            return $this->schema;
37
        }
38
39
        return $this->schema = new MssqlSchema($this);
40
    }
41
42
    public function isSybase(): bool
43
    {
44
        return $this->isSybase;
45
    }
46
47
    /**
48
     * @param bool $value set the database connected via pdo_dblib is SyBase, for default it's false.
49
     */
50
    public function sybase(bool $value): void
51
    {
52
        $this->isSybase = $value;
53
    }
54
55
    protected function createPdoInstance(): \PDO
56
    {
57
        if ($this->getDriverName() === 'sqlsrv') {
58
            $pdo = new SqlsrvPDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getAttributes());
59
        } else {
60
            $pdo = new PDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getAttributes());
61
        }
62
63
        return $pdo;
64
    }
65
66
    /**
67
     * Initializes the DB connection.
68
     *
69
     * This method is invoked right after the DB connection is established.
70
     *
71
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
72
     *
73
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
74
     *
75
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
76
     */
77
    protected function initConnection(): void
78
    {
79
        $this->getPDO()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
80
81
        if (!$this->isSybase && in_array($this->getDriverName(), ['mssql', 'dblib'], true)) {
82
            $this->getPDO()->exec('SET ANSI_NULL_DFLT_ON ON');
83
        }
84
    }
85
}
86