Completed
Push — master ( 6d673d...7f4ef4 )
by Sergei
40:47 queued 40:43
created

DB2Connection   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
eloc 33
dl 0
loc 126
ccs 0
cts 69
cp 0
rs 10
c 1
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A lastInsertId() 0 3 1
A getServerVersion() 0 6 1
A __construct() 0 13 4
A beginTransaction() 0 4 2
A query() 0 6 1
A exec() 0 9 2
A prepare() 0 8 2
A rollBack() 0 8 3
A quote() 0 3 1
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
final class DB2Connection implements ServerInfoAwareConnection
26
{
27
    /** @var resource */
28
    private $conn;
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 prepare(string $sql) : DriverStatement
66
    {
67
        $stmt = @db2_prepare($this->conn, $sql);
68
        if (! $stmt) {
0 ignored issues
show
introduced by
$stmt is of type false|resource, thus it always evaluated to false.
Loading history...
69
            throw DB2Exception::fromStatementError();
70
        }
71
72
        return new DB2Statement($stmt);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function query(string $sql) : ResultStatement
79
    {
80
        $stmt = $this->prepare($sql);
81
        $stmt->execute();
82
83
        return $stmt;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function quote(string $input) : string
90
    {
91
        return "'" . db2_escape_string($input) . "'";
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function exec(string $statement) : int
98
    {
99
        $stmt = @db2_exec($this->conn, $statement);
100
101
        if ($stmt === false) {
102
            throw DB2Exception::fromStatementError();
103
        }
104
105
        return db2_num_rows($stmt);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function lastInsertId(?string $name = null) : string
112
    {
113
        return db2_last_insert_id($this->conn);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function beginTransaction() : void
120
    {
121
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF)) {
122
            throw DB2Exception::fromConnectionError($this->conn);
123
        }
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function commit() : void
130
    {
131
        if (! db2_commit($this->conn)) {
132
            throw DB2Exception::fromConnectionError($this->conn);
133
        }
134
135
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) {
136
            throw DB2Exception::fromConnectionError($this->conn);
137
        }
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function rollBack() : void
144
    {
145
        if (! db2_rollback($this->conn)) {
146
            throw DB2Exception::fromConnectionError($this->conn);
147
        }
148
149
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) {
150
            throw DB2Exception::fromConnectionError($this->conn);
151
        }
152
    }
153
}
154