Completed
Push — develop ( 361a2b...a96e4b )
by Marco
20s queued 14s
created

SQLSrvConnection::errorInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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