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

DB2Connection::commit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
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 function assert;
11
use function db2_autocommit;
12
use function db2_commit;
13
use function db2_conn_error;
14
use function db2_conn_errormsg;
15
use function db2_connect;
16
use function db2_escape_string;
17
use function db2_exec;
18
use function db2_last_insert_id;
19
use function db2_num_rows;
20
use function db2_pconnect;
21
use function db2_prepare;
22
use function db2_rollback;
23
use function db2_server_info;
24
use function db2_stmt_errormsg;
25
use function is_bool;
26
use const DB2_AUTOCOMMIT_OFF;
27
use const DB2_AUTOCOMMIT_ON;
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
        $serverInfo = db2_server_info($this->conn);
65
        assert($serverInfo instanceof stdClass);
66
67
        return $serverInfo->DBMS_VER;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function requiresQueryForServerVersion()
74
    {
75
        return false;
76
    }
77
78
    public function prepare(string $sql) : DriverStatement
79
    {
80
        $stmt = @db2_prepare($this->conn, $sql);
81
        if ($stmt === false) {
82
            throw new DB2Exception(db2_stmt_errormsg());
83
        }
84
85
        return new DB2Statement($stmt);
86
    }
87
88
    public function query(string $sql) : ResultStatement
89
    {
90
        $stmt = $this->prepare($sql);
91
        $stmt->execute();
92
93
        return $stmt;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function quote($input, $type = ParameterType::STRING)
100
    {
101
        $input = db2_escape_string($input);
102
103
        if ($type === ParameterType::INTEGER) {
104
            return $input;
105
        }
106
107
        return "'" . $input . "'";
108
    }
109
110
    public function exec(string $statement) : int
111
    {
112
        $stmt = @db2_exec($this->conn, $statement);
113
114
        if ($stmt === false) {
115
            throw new DB2Exception(db2_stmt_errormsg());
116
        }
117
118
        return db2_num_rows($stmt);
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function lastInsertId($name = null)
125
    {
126
        return db2_last_insert_id($this->conn);
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function beginTransaction()
133
    {
134
        $result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF);
135
        assert(is_bool($result));
136
137
        return $result;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function commit()
144
    {
145
        if (! db2_commit($this->conn)) {
146
            throw new DB2Exception(db2_conn_errormsg($this->conn));
147
        }
148
149
        $result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
150
        assert(is_bool($result));
151
152
        return $result;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function rollBack()
159
    {
160
        if (! db2_rollback($this->conn)) {
161
            throw new DB2Exception(db2_conn_errormsg($this->conn));
162
        }
163
164
        $result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
165
        assert(is_bool($result));
166
167
        return $result;
168
    }
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