Failed Conditions
Pull Request — 3.0.x (#3980)
by Guilherme
06:55
created

DB2Connection   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 166
ccs 0
cts 89
cp 0
rs 10
c 0
b 0
f 0
wmc 21

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getServerVersion() 0 6 1
A errorCode() 0 3 1
A errorInfo() 0 5 1
A exec() 0 9 2
A query() 0 6 1
A prepare() 0 8 2
A __construct() 0 15 4
A rollBack() 0 10 2
A lastInsertId() 0 3 1
A requiresQueryForServerVersion() 0 3 1
A quote() 0 9 2
A beginTransaction() 0 6 1
A commit() 0 10 2
1
<?php
2
3
namespace Doctrine\DBAL\Driver\IBMDB2;
4
5
use Doctrine\DBAL\Driver\ResultStatement;
6
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
7
use Doctrine\DBAL\Driver\Statement as DriverStatement;
8
use Doctrine\DBAL\ParameterType;
9
use stdClass;
10
use const DB2_AUTOCOMMIT_OFF;
11
use const DB2_AUTOCOMMIT_ON;
12
use function assert;
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
use function is_bool;
28
29
class DB2Connection implements ServerInfoAwareConnection
30
{
31
    /** @var resource */
32
    private $conn = null;
33
34
    /**
35
     * @param mixed[] $params
36
     * @param string  $username
37
     * @param string  $password
38
     * @param mixed[] $driverOptions
39
     *
40
     * @throws DB2Exception
41
     */
42
    public function __construct(array $params, $username, $password, $driverOptions = [])
43
    {
44
        $isPersistent = (isset($params['persistent']) && $params['persistent'] === true);
45
46
        if ($isPersistent) {
47
            $conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
48
        } else {
49
            $conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
50
        }
51
52
        if ($conn === false) {
53
            throw new DB2Exception(db2_conn_errormsg());
54
        }
55
56
        $this->conn = $conn;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getServerVersion()
63
    {
64
        /** @var stdClass $serverInfo */
65
        $serverInfo = db2_server_info($this->conn);
66
67
        return $serverInfo->DBMS_VER;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function requiresQueryForServerVersion()
74
    {
75
        return false;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function prepare(string $sql) : DriverStatement
82
    {
83
        $stmt = @db2_prepare($this->conn, $sql);
84
        if ($stmt === false) {
85
            throw new DB2Exception(db2_stmt_errormsg());
86
        }
87
88
        return new DB2Statement($stmt);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function query(string $sql) : ResultStatement
95
    {
96
        $stmt = $this->prepare($sql);
97
        $stmt->execute();
98
99
        return $stmt;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function quote($input, $type = ParameterType::STRING)
106
    {
107
        $input = db2_escape_string($input);
108
109
        if ($type === ParameterType::INTEGER) {
110
            return $input;
111
        }
112
113
        return "'" . $input . "'";
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function exec(string $statement) : int
120
    {
121
        $stmt = @db2_exec($this->conn, $statement);
122
123
        if ($stmt === false) {
124
            throw new DB2Exception(db2_stmt_errormsg());
125
        }
126
127
        return db2_num_rows($stmt);
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function lastInsertId($name = null)
134
    {
135
        return db2_last_insert_id($this->conn);
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function beginTransaction()
142
    {
143
        $result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF);
144
        assert(is_bool($result));
145
146
        return $result;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function commit()
153
    {
154
        if (! db2_commit($this->conn)) {
155
            throw new DB2Exception(db2_conn_errormsg($this->conn));
156
        }
157
158
        $result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
159
        assert(is_bool($result));
160
161
        return $result;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function rollBack()
168
    {
169
        if (! db2_rollback($this->conn)) {
170
            throw new DB2Exception(db2_conn_errormsg($this->conn));
171
        }
172
173
        $result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
174
        assert(is_bool($result));
175
176
        return $result;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function errorCode()
183
    {
184
        return db2_conn_error($this->conn);
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function errorInfo()
191
    {
192
        return [
193
            0 => db2_conn_errormsg($this->conn),
194
            1 => $this->errorCode(),
195
        ];
196
    }
197
}
198