Failed Conditions
Pull Request — develop (#3367)
by Benjamin
10:59
created

Connection::lastInsertId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.1406

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 16
ccs 9
cts 12
cp 0.75
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.1406
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