Failed Conditions
Pull Request — develop (#3367)
by Benjamin
14:32
created

Connection   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 62.5%

Importance

Changes 0
Metric Value
wmc 6
eloc 13
dl 0
loc 44
ccs 15
cts 24
cp 0.625
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A quote() 0 10 2
A createStatement() 0 3 1
A lastInsertId() 0 16 3
1
<?php
2
3
namespace Doctrine\DBAL\Driver\PDOSqlsrv;
4
5
use Doctrine\DBAL\Driver\DriverException;
6
use Doctrine\DBAL\Driver\PDOConnection;
7
use Doctrine\DBAL\Driver\PDOStatement;
8
use function strpos;
9
use function substr;
10
11
/**
12
 * Sqlsrv Connection implementation.
13
 */
14
class Connection extends PDOConnection implements \Doctrine\DBAL\Driver\Connection
15
{
16
    /**
17
     * {@inheritDoc}
18
     */
19 5
    public function lastInsertId(?string $name = null) : string
20
    {
21 5
        if ($name === null) {
22 3
            return parent::lastInsertId($name);
23
        }
24
25 2
        $stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
26 2
        $stmt->execute([$name]);
27
28 2
        $result = $stmt->fetchColumn();
29
30 2
        if ($result === false) {
31 1
            throw DriverException::noSuchSequence($name);
32
        }
33
34 1
        return (string) $result;
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 7
    public function quote(string $value) : string
41
    {
42 7
        $val = parent::quote($value);
43
44
        // Fix for a driver version terminating all values with null byte
45 7
        if (strpos($val, "\0") !== false) {
46
            $val = substr($val, 0, -1);
47
        }
48
49 7
        return $val;
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55 246
    protected function createStatement(\PDOStatement $stmt) : PDOStatement
56
    {
57 246
        return new Statement($stmt);
58
    }
59
}
60