Completed
Push — master ( 1a9812...b70610 )
by Sergei
19s queued 14s
created

DB2Connection   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
eloc 34
c 1
b 0
f 0
dl 0
loc 134
rs 10
ccs 0
cts 73
cp 0

11 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 requiresQueryForServerVersion() 0 3 1
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 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
        } else {
41
            $conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
42
        }
43
44
        if ($conn === false) {
45
            throw DB2Exception::fromConnectionError();
46
        }
47
48
        $this->conn = $conn;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getServerVersion() : string
55
    {
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
     * {@inheritdoc}
72
     */
73
    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
80
        return new DB2Statement($stmt);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function query(string $sql) : ResultStatement
87
    {
88
        $stmt = $this->prepare($sql);
89
        $stmt->execute();
90
91
        return $stmt;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function quote(string $input) : string
98
    {
99
        return "'" . db2_escape_string($input) . "'";
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function exec(string $statement) : int
106
    {
107
        $stmt = @db2_exec($this->conn, $statement);
108
109
        if ($stmt === false) {
110
            throw DB2Exception::fromStatementError();
111
        }
112
113
        return db2_num_rows($stmt);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function lastInsertId(?string $name = null) : string
120
    {
121
        return db2_last_insert_id($this->conn);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function beginTransaction() : void
128
    {
129
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF)) {
130
            throw DB2Exception::fromConnectionError($this->conn);
131
        }
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function commit() : void
138
    {
139
        if (! db2_commit($this->conn)) {
140
            throw DB2Exception::fromConnectionError($this->conn);
141
        }
142
143
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) {
144
            throw DB2Exception::fromConnectionError($this->conn);
145
        }
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function rollBack() : void
152
    {
153
        if (! db2_rollback($this->conn)) {
154
            throw DB2Exception::fromConnectionError($this->conn);
155
        }
156
157
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) {
158
            throw DB2Exception::fromConnectionError($this->conn);
159
        }
160
    }
161
}
162