Passed
Pull Request — master (#14)
by Wilmer
25:49 queued 10:47
created

MssqlConnection   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 83
rs 10
c 1
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A sybase() 0 3 1
A createPdoInstance() 0 23 2
A isSybase() 0 3 1
A initConnection() 0 6 3
A getSchema() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql\Connection;
6
7
use Yiisoft\Db\Connection\Connection as Connection;
8
use Yiisoft\Db\Mssql\Pdo\PDO;
9
use Yiisoft\Db\Mssql\Pdo\SqlsrvPDO;
10
use Yiisoft\Db\Mssql\Schema\MssqlSchema;
11
12
/**
13
 * Database connection class prefilled for MSSQL Server.
14
 */
15
final class MssqlConnection extends Connection
16
{
17
    protected array $schemaMap = [
18
        '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...
19
        'mssql' => Schema::class, // older MSSQL driver on MS Windows hosts
20
        'dblib' => Schema::class, // dblib drivers on GNU/Linux (and maybe other OSes) hosts
21
    ];
22
    private bool $isSybase = false;
23
    private ?MssqlSchema $schema = null;
24
25
    /**
26
     * Returns the schema information for the database opened by this connection.
27
     *
28
     * @throws Exception
29
     * @throws InvalidConfigException
30
     * @throws NotSupportedException if there is no support for the current driver type
31
     *
32
     * @return Schema the schema information for the database opened by this connection.
33
     */
34
    public function getSchema(): MssqlSchema
35
    {
36
        if ($this->schema !== null) {
37
            return $this->schema;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->schema returns the type Yiisoft\Db\Mssql\Schema\MssqlSchema which is incompatible with the documented return type Yiisoft\Db\Mssql\Connection\Schema.
Loading history...
38
        }
39
40
        return $this->schema = new MssqlSchema($this);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->schema = n...hema\MssqlSchema($this) returns the type Yiisoft\Db\Mssql\Schema\MssqlSchema which is incompatible with the documented return type Yiisoft\Db\Mssql\Connection\Schema.
Loading history...
41
    }
42
43
    public function isSybase(): bool
44
    {
45
        return $this->isSybase;
46
    }
47
48
    /**
49
     * @param bool $value set the database connected via pdo_dblib is SyBase, for default it's false.
50
     */
51
    public function sybase(bool $value): void
52
    {
53
        $this->isSybase = $value;
54
    }
55
56
    protected function createPdoInstance(): \PDO
57
    {
58
        switch ($this->getDriverName()) {
59
            case 'sqlsrv':
60
                $pdo = new SqlsrvPDO(
61
                    $this->getDsn(),
62
                    $this->getUsername(),
63
                    $this->getPassword(),
64
                    $this->getAttributes()
65
                );
66
                break;
67
68
            default:
69
                $pdo = new PDO(
70
                    $this->getDsn(),
71
                    $this->getUsername(),
72
                    $this->getPassword(),
73
                    $this->getAttributes()
74
                );
75
                break;
76
        }
77
78
        return $pdo;
79
    }
80
81
    /**
82
     * Initializes the DB connection.
83
     *
84
     * This method is invoked right after the DB connection is established.
85
     *
86
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
87
     *
88
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
89
     *
90
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
91
     */
92
    protected function initConnection(): void
93
    {
94
        $this->getPDO()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
95
96
        if (!$this->isSybase && in_array($this->getDriverName(), ['mssql', 'dblib'], true)) {
97
            $this->getPDO()->exec('SET ANSI_NULL_DFLT_ON ON');
98
        }
99
100
        //$this->trigger(self::EVENT_AFTER_OPEN);
101
    }
102
}
103