Failed Conditions
Pull Request — develop (#3523)
by
unknown
62:01
created

PDOSqlsrvConnection   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 32
dl 0
loc 126
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A quote() 0 10 2
A __construct() 0 4 1
A getServerVersion() 0 3 1
A rollBack() 0 3 1
A beginTransaction() 0 3 1
A exec() 0 3 1
A commit() 0 3 1
A query() 0 9 2
A lastInsertId() 0 14 3
A requiresQueryForServerVersion() 0 3 1
A prepare() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver\PDOSqlsrv;
6
7
use Doctrine\DBAL\Driver\Connection;
8
use Doctrine\DBAL\Driver\PDOConnection;
9
use Doctrine\DBAL\Driver\PDOException;
10
use Doctrine\DBAL\Driver\ResultStatement;
11
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
12
use Doctrine\DBAL\Driver\Statement;
13
use function strpos;
14
use function substr;
15
16
/**
17
 * Sqlsrv Connection implementation.
18
 */
19
class PDOSqlsrvConnection implements Connection, ServerInfoAwareConnection
20
{
21
    /** @var PDOConnection */
22
    protected $conn;
23
24
    /** @var LastInsertId */
25
    protected $lastInsertId;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function __construct($dsn, $user = null, $password = null, ?array $options = null)
31
    {
32
        $this->conn         = new PDOConnection($dsn, $user, $password, $options);
33
        $this->lastInsertId = new LastInsertId();
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39
    public function prepare($sql) : Statement
40
    {
41
        $this->lastInsertId = new LastInsertId();
42
43
        try {
44
            return new PDOSqlsrvStatement($this->conn, $sql, $this->lastInsertId);
45
        } catch (\PDOException $exception) {
46
            throw new PDOException($exception);
47
        }
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function query(string $sql) : ResultStatement
54
    {
55
        try {
56
            $stmt = $this->prepare($sql);
57
            $stmt->execute();
58
59
            return $stmt;
60
        } catch (\PDOException $exception) {
61
            throw new PDOException($exception);
62
        }
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public function exec($statement) : int
69
    {
70
        return $this->conn->exec($statement);
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    public function quote(string $input) : string
77
    {
78
        $val = $this->conn->quote($input);
79
80
        // Fix for a driver version terminating all values with null byte
81
        if (strpos($val, "\0") !== false) {
82
            $val = substr($val, 0, -1);
83
        }
84
85
        return $val;
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91
    public function lastInsertId($name = null)
92
    {
93
        if ($this->lastInsertId->getId()) {
94
            return $this->lastInsertId->getId();
95
        }
96
97
        if ($name === null) {
98
            return $this->conn->lastInsertId();
99
        }
100
101
        $stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
102
        $stmt->execute([$name]);
103
104
        return (string) $stmt->fetchColumn();
105
    }
106
107
    /**
108
     * {@inheritDoc}
109
     */
110
    public function beginTransaction() : void
111
    {
112
        $this->conn->beginTransaction();
113
    }
114
115
    /**
116
     * {@inheritDoc}
117
     */
118
    public function commit() : void
119
    {
120
        $this->conn->commit();
121
    }
122
123
    /**
124
     * {@inheritDoc}
125
     */
126
    public function rollBack() : void
127
    {
128
        $this->conn->rollBack();
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function getServerVersion()
135
    {
136
        return $this->conn->getServerVersion();
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function requiresQueryForServerVersion()
143
    {
144
        return $this->conn->requiresQueryForServerVersion();
145
    }
146
}
147