MSSQL::connectToServer()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 22
ccs 0
cts 15
cp 0
crap 12
rs 9.8666
1
<?php
2
3
namespace kalanis\kw_mapper\Storage\Database\PDO;
4
5
6
use kalanis\kw_mapper\Storage\Database\Dialects;
7
use PDO;
8
9
10
/**
11
 * Class MSSQL
12
 * @package kalanis\kw_mapper\Storage\Database\PDO
13
 * Connection to Microsoft SQL, they based it on TransactSQL
14
 * Can be also used for Sybase DB, because they have similar base
15
 *
16
 * To make driver on Linux use following pages:
17
 * @link https://www.php.net/manual/en/ref.pdo-sqlsrv.php
18
 * @link https://github.com/microsoft/msphpsql
19
 * @link https://learn.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker?view=sql-server-ver16&tabs=cli&pivots=cs1-bash
20
 *
21
 * Also beware that MS needs explicitly state name of column for "ORDER BY" when you use "OFFSET"-"LIMIT"
22
 */
23
class MSSQL extends APDO
24
{
25
    protected string $extension = 'pdo_sqlsrv';
26
27
    public function languageDialect(): string
28
    {
29
        return Dialects\TransactSQL::class;
30
    }
31
32
    protected function connectToServer(): PDO
33
    {
34
        $connection = new PDO(
35
            sprintf('sqlsrv:server=%s;Database=%s;',
36
                $this->config->getLocation(),
37
                $this->config->getDatabase()
38
            ),
39
            $this->config->getUser(),
40
            $this->config->getPassword()
41
        );
42
43
        $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
44
45
        if ($this->config->isPersistent()) {
46
            $connection->setAttribute(PDO::ATTR_PERSISTENT, true);
47
        }
48
49
        foreach ($this->attributes as $key => $value){
50
            $connection->setAttribute($key, $value);
51
        }
52
53
        return $connection;
54
    }
55
}
56