Failed Conditions
Pull Request — master (#3973)
by Grégoire
03:04
created

DB2Connection   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 99
ccs 0
cts 70
cp 0
rs 10
c 0
b 0
f 0
wmc 20

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getServerVersion() 0 6 1
A query() 0 6 1
A exec() 0 9 2
A prepare() 0 8 2
A __construct() 0 13 4
A lastInsertId() 0 3 1
A rollBack() 0 8 3
A quote() 0 3 1
A beginTransaction() 0 4 2
A commit() 0 8 3
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
    public function __construct(array $params, string $username, string $password, array $driverOptions = [])
38
    {
39
        if (isset($params['persistent']) && $params['persistent'] === true) {
40
            $conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
41
        } else {
42
            $conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
43
        }
44
45
        if ($conn === false) {
46
            throw DB2Exception::fromConnectionError();
47
        }
48
49
        $this->conn = $conn;
50
    }
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
    public function prepare(string $sql) : DriverStatement
61
    {
62
        $stmt = @db2_prepare($this->conn, $sql);
63
        if ($stmt === false) {
64
            throw DB2Exception::fromStatementError();
65
        }
66
67
        return new DB2Statement($stmt);
68
    }
69
70
    public function query(string $sql) : ResultStatement
71
    {
72
        $stmt = $this->prepare($sql);
73
        $stmt->execute();
74
75
        return $stmt;
76
    }
77
78
    public function quote(string $input) : string
79
    {
80
        return "'" . db2_escape_string($input) . "'";
81
    }
82
83
    public function exec(string $statement) : int
84
    {
85
        $stmt = @db2_exec($this->conn, $statement);
86
87
        if ($stmt === false) {
88
            throw DB2Exception::fromStatementError();
89
        }
90
91
        return db2_num_rows($stmt);
92
    }
93
94
    public function lastInsertId(?string $name = null) : string
95
    {
96
        return db2_last_insert_id($this->conn);
97
    }
98
99
    public function beginTransaction() : void
100
    {
101
        if (db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF) !== true) {
102
            throw DB2Exception::fromConnectionError($this->conn);
103
        }
104
    }
105
106
    public function commit() : void
107
    {
108
        if (! db2_commit($this->conn)) {
109
            throw DB2Exception::fromConnectionError($this->conn);
110
        }
111
112
        if (db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON) !== true) {
113
            throw DB2Exception::fromConnectionError($this->conn);
114
        }
115
    }
116
117
    public function rollBack() : void
118
    {
119
        if (! db2_rollback($this->conn)) {
120
            throw DB2Exception::fromConnectionError($this->conn);
121
        }
122
123
        if (db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON) !== true) {
124
            throw DB2Exception::fromConnectionError($this->conn);
125
        }
126
    }
127
}
128