Failed Conditions
Push — develop ( 86369f...91f014 )
by Sergei
62:30 queued 62:25
created

DB2Connection   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 21
eloc 34
dl 0
loc 134
ccs 0
cts 73
cp 0
rs 10
c 0
b 0
f 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\Connection;
8
use Doctrine\DBAL\Driver\ResultStatement;
9
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
10
use Doctrine\DBAL\Driver\Statement as DriverStatement;
11
use stdClass;
12
use const DB2_AUTOCOMMIT_OFF;
13
use const DB2_AUTOCOMMIT_ON;
14
use function db2_autocommit;
15
use function db2_commit;
16
use function db2_connect;
17
use function db2_escape_string;
18
use function db2_exec;
19
use function db2_last_insert_id;
20
use function db2_num_rows;
21
use function db2_pconnect;
22
use function db2_prepare;
23
use function db2_rollback;
24
use function db2_server_info;
25
26
class DB2Connection implements Connection, ServerInfoAwareConnection
27
{
28
    /** @var resource */
29
    private $conn = null;
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
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getServerVersion() : string
56
    {
57
        /** @var stdClass $serverInfo */
58
        $serverInfo = db2_server_info($this->conn);
59
60
        return $serverInfo->DBMS_VER;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function requiresQueryForServerVersion() : bool
67
    {
68
        return false;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function prepare(string $sql) : DriverStatement
75
    {
76
        $stmt = @db2_prepare($this->conn, $sql);
77
        if (! $stmt) {
0 ignored issues
show
introduced by
$stmt is of type false|resource, thus it always evaluated to false.
Loading history...
78
            throw DB2Exception::fromStatementError();
79
        }
80
81
        return new DB2Statement($stmt);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function query(string $sql) : ResultStatement
88
    {
89
        $stmt = $this->prepare($sql);
90
        $stmt->execute();
91
92
        return $stmt;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function quote(string $input) : string
99
    {
100
        return "'" . db2_escape_string($input) . "'";
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function exec(string $statement) : int
107
    {
108
        $stmt = @db2_exec($this->conn, $statement);
109
110
        if ($stmt === false) {
111
            throw DB2Exception::fromStatementError();
112
        }
113
114
        return db2_num_rows($stmt);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function lastInsertId(?string $name = null) : string
121
    {
122
        return db2_last_insert_id($this->conn);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function beginTransaction() : void
129
    {
130
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF)) {
131
            throw DB2Exception::fromConnectionError($this->conn);
132
        }
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function commit() : void
139
    {
140
        if (! db2_commit($this->conn)) {
141
            throw DB2Exception::fromConnectionError($this->conn);
142
        }
143
144
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) {
145
            throw DB2Exception::fromConnectionError($this->conn);
146
        }
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function rollBack() : void
153
    {
154
        if (! db2_rollback($this->conn)) {
155
            throw DB2Exception::fromConnectionError($this->conn);
156
        }
157
158
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) {
159
            throw DB2Exception::fromConnectionError($this->conn);
160
        }
161
    }
162
}
163