Completed
Pull Request — master (#3807)
by Sergei
155:06 queued 90:16
created

SQLSrvConnection::requiresQueryForServerVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1.037
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver\SQLSrv;
6
7
use Doctrine\DBAL\Driver\ResultStatement;
8
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
9
use Doctrine\DBAL\Driver\Statement as DriverStatement;
10
use function sqlsrv_begin_transaction;
11
use function sqlsrv_commit;
12
use function sqlsrv_configure;
13
use function sqlsrv_connect;
14
use function sqlsrv_query;
15
use function sqlsrv_rollback;
16
use function sqlsrv_rows_affected;
17
use function sqlsrv_server_info;
18
use function str_replace;
19
20
/**
21
 * SQL Server implementation for the Connection interface.
22
 */
23
class SQLSrvConnection implements ServerInfoAwareConnection
24
{
25
    /** @var resource */
26
    protected $conn;
27
28
    /** @var LastInsertId */
29
    protected $lastInsertId;
30
31
    /**
32
     * @param array<string, mixed> $connectionOptions
33
     *
34
     * @throws SQLSrvException
35
     */
36 27
    public function __construct(string $serverName, array $connectionOptions)
37
    {
38 27
        if (! sqlsrv_configure('WarningsReturnAsErrors', 0)) {
39
            throw SQLSrvException::fromSqlSrvErrors();
40
        }
41
42 27
        $conn = sqlsrv_connect($serverName, $connectionOptions);
43
44 27
        if ($conn === false) {
45
            throw SQLSrvException::fromSqlSrvErrors();
46
        }
47
48 27
        $this->conn         = $conn;
49 27
        $this->lastInsertId = new LastInsertId();
50 27
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getServerVersion() : string
56
    {
57
        $serverInfo = sqlsrv_server_info($this->conn);
58
59
        return $serverInfo['SQLServerVersion'];
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65 1
    public function prepare(string $sql) : DriverStatement
66
    {
67 1
        return new SQLSrvStatement($this->conn, $sql, $this->lastInsertId);
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73 324
    public function query(string $sql) : ResultStatement
74
    {
75 324
        $stmt = $this->prepare($sql);
76
        $stmt->execute();
77
78
        return $stmt;
79
    }
80
81 217
    /**
82
     * {@inheritDoc}
83 217
     */
84 217
    public function quote(string $input) : string
85
    {
86 217
        return "'" . str_replace("'", "''", $input) . "'";
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92 5
    public function exec(string $statement) : int
93
    {
94 5
        $stmt = sqlsrv_query($this->conn, $statement);
95
96
        if ($stmt === false) {
97
            throw SQLSrvException::fromSqlSrvErrors();
98
        }
99
100 165
        $rowsAffected = sqlsrv_rows_affected($stmt);
101
102 165
        if ($rowsAffected === false) {
103
            throw SQLSrvException::fromSqlSrvErrors();
104 165
        }
105 72
106
        return $rowsAffected;
107
    }
108 158
109
    /**
110 158
     * {@inheritDoc}
111
     */
112
    public function lastInsertId(?string $name = null) : string
113
    {
114 158
        if ($name !== null) {
115
            $stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
116
            $stmt->execute([$name]);
117
        } else {
118
            $stmt = $this->query('SELECT @@IDENTITY');
119
        }
120 3
121
        return $stmt->fetchColumn();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $stmt->fetchColumn() could return the type false which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
122 3
    }
123 1
124 1
    /**
125
     * {@inheritDoc}
126 2
     */
127
    public function beginTransaction() : void
128
    {
129 3
        if (! sqlsrv_begin_transaction($this->conn)) {
130
            throw SQLSrvException::fromSqlSrvErrors();
131
        }
132
    }
133
134
    /**
135 16
     * {@inheritDoc}
136
     */
137 16
    public function commit() : void
138
    {
139
        if (! sqlsrv_commit($this->conn)) {
140 16
            throw SQLSrvException::fromSqlSrvErrors();
141
        }
142
    }
143
144
    /**
145 6
     * {@inheritDoc}
146
     */
147 6
    public function rollBack() : void
148
    {
149
        if (! sqlsrv_rollback($this->conn)) {
150 6
            throw SQLSrvException::fromSqlSrvErrors();
151
        }
152
    }
153
}
154