Completed
Pull Request — master (#3885)
by Grégoire
37:52 queued 34:57
created

DB2Connection::beginTransaction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 2
cts 4
cp 0.5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.5
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 function assert;
12
use function db2_autocommit;
13
use function db2_commit;
14
use function db2_connect;
15
use function db2_escape_string;
16
use function db2_exec;
17
use function db2_last_insert_id;
18
use function db2_num_rows;
19
use function db2_pconnect;
20
use function db2_prepare;
21
use function db2_rollback;
22
use function db2_server_info;
23
use const DB2_AUTOCOMMIT_OFF;
24
use const DB2_AUTOCOMMIT_ON;
25
26
final class DB2Connection implements ServerInfoAwareConnection
27
{
28
    /** @var resource */
29
    private $conn;
30
31
    /**
32
     * @param array<string, mixed> $params
33
     * @param array<string, mixed> $driverOptions
34
     *
35
     * @throws DB2Exception
36
     */
37 20
    public function __construct(array $params, string $username, string $password, array $driverOptions = [])
38
    {
39 20
        if (isset($params['persistent']) && $params['persistent'] === true) {
40
            $conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
41
        } else {
42 20
            $conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
43
        }
44
45 20
        if ($conn === false) {
46
            throw DB2Exception::fromConnectionError();
47
        }
48
49 20
        $this->conn = $conn;
50 20
    }
51
52
    public function getServerVersion() : string
53
    {
54
        $serverInfo = db2_server_info($this->conn);
55
        assert($serverInfo instanceof stdClass);
56
57
        return $serverInfo->DBMS_VER;
58
    }
59
60 311
    public function prepare(string $sql) : DriverStatement
61
    {
62 311
        $stmt = @db2_prepare($this->conn, $sql);
63 311
        if (! $stmt) {
0 ignored issues
show
introduced by
$stmt is of type false|resource, thus it always evaluated to false.
Loading history...
64
            throw DB2Exception::fromStatementError();
65
        }
66
67 311
        return new DB2Statement($stmt);
68
    }
69
70 199
    public function query(string $sql) : ResultStatement
71
    {
72 199
        $stmt = $this->prepare($sql);
73 199
        $stmt->execute();
74
75 199
        return $stmt;
76
    }
77
78 2
    public function quote(string $input) : string
79
    {
80 2
        return "'" . db2_escape_string($input) . "'";
81
    }
82
83 149
    public function exec(string $statement) : int
84
    {
85 149
        $stmt = @db2_exec($this->conn, $statement);
86
87 149
        if ($stmt === false) {
88 58
            throw DB2Exception::fromStatementError();
89
        }
90
91 142
        return db2_num_rows($stmt);
92
    }
93
94 2
    public function lastInsertId(?string $name = null) : string
95
    {
96 2
        return db2_last_insert_id($this->conn);
97
    }
98
99 14
    public function beginTransaction() : void
100
    {
101 14
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF)) {
102
            throw DB2Exception::fromConnectionError($this->conn);
103
        }
104 14
    }
105
106 5
    public function commit() : void
107
    {
108 5
        if (! db2_commit($this->conn)) {
109
            throw DB2Exception::fromConnectionError($this->conn);
110
        }
111
112 5
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) {
113
            throw DB2Exception::fromConnectionError($this->conn);
114
        }
115 5
    }
116
117 10
    public function rollBack() : void
118
    {
119 10
        if (! db2_rollback($this->conn)) {
120
            throw DB2Exception::fromConnectionError($this->conn);
121
        }
122
123 10
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) {
124
            throw DB2Exception::fromConnectionError($this->conn);
125
        }
126 10
    }
127
}
128