Completed
Pull Request — master (#3759)
by Benjamin
131:22 queued 67:31
created

DB2Connection::commit()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4.125

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 2
cts 4
cp 0.5
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 4.125
1
<?php
2
3
declare(strict_types=0);
4
5
namespace Doctrine\DBAL\Driver\IBMDB2;
6
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_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
25
class DB2Connection implements ServerInfoAwareConnection
26
{
27
    /** @var resource */
28
    private $conn = null;
29
30
    /**
31
     * @param array<string, mixed> $params
32
     * @param array<string, mixed> $driverOptions
33
     *
34
     * @throws DB2Exception
35
     */
36
    public function __construct(array $params, string $username, string $password, array $driverOptions = [])
37
    {
38
        if (isset($params['persistent']) && $params['persistent'] === true) {
39
            $conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
40 227
        } else {
41
            $conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
42 227
        }
43
44 227
        if ($conn === false) {
45
            throw DB2Exception::fromConnectionError();
46
        }
47 227
48
        $this->conn = $conn;
49
    }
50 227
51
    /**
52
     * {@inheritdoc}
53
     */
54 227
    public function getServerVersion() : string
55 227
    {
56
        /** @var stdClass $serverInfo */
57
        $serverInfo = db2_server_info($this->conn);
58
59
        return $serverInfo->DBMS_VER;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function requiresQueryForServerVersion() : bool
66
    {
67
        return false;
68
    }
69
70
    /**
71 228
     * {@inheritdoc}
72
     */
73 228
    public function prepare(string $sql) : DriverStatement
74
    {
75
        $stmt = @db2_prepare($this->conn, $sql);
76
        if (! $stmt) {
0 ignored issues
show
introduced by
$stmt is of type false|resource, thus it always evaluated to false.
Loading history...
77
            throw DB2Exception::fromStatementError();
78
        }
79 227
80
        return new DB2Statement($stmt);
81 227
    }
82 227
83
    /**
84
     * {@inheritdoc}
85
     */
86 227
    public function query(string $sql) : ResultStatement
87
    {
88
        $stmt = $this->prepare($sql);
89
        $stmt->execute();
90
91
        return $stmt;
92 227
    }
93
94 227
    /**
95 227
     * {@inheritdoc}
96 227
     */
97 227
    public function quote(string $input) : string
98
    {
99 227
        return "'" . db2_escape_string($input) . "'";
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 227
    public function exec(string $statement) : int
106
    {
107 227
        $stmt = @db2_exec($this->conn, $statement);
108
109 227
        if ($stmt === false) {
110
            throw DB2Exception::fromStatementError();
111
        }
112
113 227
        return db2_num_rows($stmt);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 227
    public function lastInsertId() : string
120
    {
121 227
        return db2_last_insert_id($this->conn);
122
    }
123 227
124 227
    /**
125
     * {@inheritdoc}
126
     */
127 227
    public function getSequenceNumber(string $name) : string
128
    {
129
        throw new DB2Exception('Sequences on IBM DB2 are not currently supported.');
130
    }
131
132
    /**
133 7
     * {@inheritdoc}
134
     */
135 7
    public function beginTransaction() : void
136
    {
137
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF)) {
138
            throw DB2Exception::fromConnectionError($this->conn);
139
        }
140
    }
141 220
142
    /**
143 220
     * {@inheritdoc}
144 220
     */
145
    public function commit() : void
146
    {
147
        if (! db2_commit($this->conn)) {
148
            throw DB2Exception::fromConnectionError($this->conn);
149 216
        }
150
151 216
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) {
152
            throw DB2Exception::fromConnectionError($this->conn);
153
        }
154 216
    }
155 216
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function rollBack() : void
160 220
    {
161
        if (! db2_rollback($this->conn)) {
162 220
            throw DB2Exception::fromConnectionError($this->conn);
163
        }
164
165 220
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) {
166 220
            throw DB2Exception::fromConnectionError($this->conn);
167
        }
168
    }
169
}
170