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

DB2Connection   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Test Coverage

Coverage 48.84%

Importance

Changes 0
Metric Value
wmc 22
eloc 41
dl 0
loc 158
ccs 42
cts 86
cp 0.4884
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getServerVersion() 0 6 1
A query() 0 6 1
A prepare() 0 8 2
A __construct() 0 11 4
A requiresQueryForServerVersion() 0 3 1
A exec() 0 9 2
A errorCode() 0 3 1
A rollBack() 0 6 2
A lastInsertId() 0 13 3
A errorInfo() 0 5 1
A quote() 0 5 1
A beginTransaction() 0 3 1
A commit() 0 6 2
1
<?php
2
3
namespace Doctrine\DBAL\Driver\IBMDB2;
4
5
use Doctrine\DBAL\Driver\Connection;
6
use Doctrine\DBAL\Driver\DriverException;
7
use Doctrine\DBAL\Driver\ResultStatement;
8
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
9
use Doctrine\DBAL\Driver\Statement as DriverStatement;
10
use stdClass;
11
use const DB2_AUTOCOMMIT_OFF;
12
use const DB2_AUTOCOMMIT_ON;
13
use function db2_autocommit;
14
use function db2_commit;
15
use function db2_conn_error;
16
use function db2_conn_errormsg;
17
use function db2_connect;
18
use function db2_escape_string;
19
use function db2_exec;
20
use function db2_last_insert_id;
21
use function db2_num_rows;
22
use function db2_pconnect;
23
use function db2_prepare;
24
use function db2_rollback;
25
use function db2_server_info;
26
use function db2_stmt_errormsg;
27
28
class DB2Connection implements Connection, ServerInfoAwareConnection
29
{
30
    /** @var resource */
31
    private $conn = null;
32
33
    /**
34
     * @param mixed[] $params
35
     * @param string  $username
36
     * @param string  $password
37
     * @param mixed[] $driverOptions
38
     *
39
     * @throws DriverException
40
     */
41 21
    public function __construct(array $params, $username, $password, $driverOptions = [])
42
    {
43 21
        $isPersistent = (isset($params['persistent']) && $params['persistent'] === true);
44
45 21
        if ($isPersistent) {
46
            $this->conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
47
        } else {
48 21
            $this->conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
49
        }
50 21
        if (! $this->conn) {
51
            throw new DriverException(db2_conn_errormsg());
52
        }
53 21
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getServerVersion()
59
    {
60
        /** @var stdClass $serverInfo */
61
        $serverInfo = db2_server_info($this->conn);
62
63
        return $serverInfo->DBMS_VER;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 1
    public function requiresQueryForServerVersion()
70
    {
71 1
        return false;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 229
    public function prepare(string $sql) : DriverStatement
78
    {
79 229
        $stmt = @db2_prepare($this->conn, $sql);
80 229
        if (! $stmt) {
0 ignored issues
show
introduced by
$stmt is of type resource, thus it always evaluated to false.
Loading history...
81
            throw new DriverException(db2_stmt_errormsg());
82
        }
83
84 229
        return new DB2Statement($stmt);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 161
    public function query(string $sql) : ResultStatement
91
    {
92 161
        $stmt = $this->prepare($sql);
93 161
        $stmt->execute();
94
95 161
        return $stmt;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 56
    public function quote(string $input) : string
102
    {
103 56
        $input = db2_escape_string($input);
104
105 56
        return "'" . $input . "'";
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 146
    public function exec(string $statement) : int
112
    {
113 146
        $stmt = @db2_exec($this->conn, $statement);
114
115 146
        if ($stmt === false) {
116 88
            throw new DriverException(db2_stmt_errormsg());
117
        }
118
119 121
        return db2_num_rows($stmt);
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 4
    public function lastInsertId(?string $name = null) : string
126
    {
127 4
        if ($name !== null) {
128 1
            throw new DriverException('Sequences on IBM DB2 are not currently supported.');
129
        }
130
131 3
        $lastInsertId = db2_last_insert_id($this->conn);
132
133 3
        if ($lastInsertId === '') {
134 1
            throw DriverException::noInsertId();
135
        }
136
137 2
        return $lastInsertId;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 13
    public function beginTransaction() : void
144
    {
145 13
        db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF);
0 ignored issues
show
Bug introduced by
DB2_AUTOCOMMIT_OFF of type integer is incompatible with the type boolean expected by parameter $value of db2_autocommit(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

145
        db2_autocommit($this->conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_OFF);
Loading history...
146 13
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 5
    public function commit() : void
152
    {
153 5
        if (! db2_commit($this->conn)) {
154
            throw new DriverException(db2_conn_errormsg($this->conn));
155
        }
156 5
        db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
0 ignored issues
show
Bug introduced by
DB2_AUTOCOMMIT_ON of type integer is incompatible with the type boolean expected by parameter $value of db2_autocommit(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

156
        db2_autocommit($this->conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_ON);
Loading history...
157 5
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162 9
    public function rollBack() : void
163
    {
164 9
        if (! db2_rollback($this->conn)) {
165
            throw new DriverException(db2_conn_errormsg($this->conn));
166
        }
167 9
        db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
0 ignored issues
show
Bug introduced by
DB2_AUTOCOMMIT_ON of type integer is incompatible with the type boolean expected by parameter $value of db2_autocommit(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

167
        db2_autocommit($this->conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_ON);
Loading history...
168 9
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function errorCode()
174
    {
175
        return db2_conn_error($this->conn);
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function errorInfo()
182
    {
183
        return [
184
            0 => db2_conn_errormsg($this->conn),
185
            1 => $this->errorCode(),
186
        ];
187
    }
188
}
189