Failed Conditions
Pull Request — develop (#3335)
by
unknown
12:27
created

Connection::quote()

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 10
ccs 4
cts 7
cp 0.5714
c 0
b 0
f 0
nc 2
nop 1
1
<?php
2
3
namespace Doctrine\DBAL\Driver\PDOSqlsrv;
4
5
use Doctrine\DBAL\Driver\AbstractDriverException;
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 3
    public function lastInsertId(?string $name = null) : string
20
    {
21 3
        if ($name === null) {
22 2
            return parent::lastInsertId($name);
23
        }
24
25 1
        $stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
26 1
        $stmt->execute([$name]);
27
28 1
        $result = $stmt->fetchColumn();
29
30 1
        if ($result === false) {
31
            // WIP regarding exceptions, see:
32
            // https://github.com/doctrine/dbal/pull/3335#discussion_r234381175
33
            throw new class ('No sequence with name "' . $name . '" found.') extends AbstractDriverException {
34
            };
35
        }
36
37 1
        return (string) $result;
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43 7
    public function quote(string $value) : string
44
    {
45 7
        $val = parent::quote($value);
46
47
        // Fix for a driver version terminating all values with null byte
48 7
        if (strpos($val, "\0") !== false) {
49
            $val = substr($val, 0, -1);
50
        }
51
52 7
        return $val;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58 245
    protected function createStatement(\PDOStatement $stmt) : PDOStatement
59
    {
60 245
        return new Statement($stmt);
61
    }
62
}
63