Failed Conditions
Pull Request — develop (#3335)
by
unknown
12:27
created

DB2Connection   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Test Coverage

Coverage 46.84%

Importance

Changes 0
Metric Value
wmc 20
eloc 36
dl 0
loc 148
ccs 37
cts 79
cp 0.4684
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A prepare() 0 8 2
A getServerVersion() 0 6 1
A __construct() 0 11 4
A requiresQueryForServerVersion() 0 3 1
A exec() 0 9 2
A query() 0 6 1
A errorCode() 0 3 1
A rollBack() 0 6 2
A lastInsertId() 0 3 1
A errorInfo() 0 5 1
A quote() 0 5 1
A beginTransaction() 0 3 1
A commit() 0 6 2
1
<?php
2
3
namespace Doctrine\DBAL\Driver\IBMDB2;
4
5
use Doctrine\DBAL\Driver\Connection;
6
use Doctrine\DBAL\Driver\ResultStatement;
7
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
8
use Doctrine\DBAL\Driver\Statement as DriverStatement;
9
use stdClass;
10
use const DB2_AUTOCOMMIT_OFF;
11
use const DB2_AUTOCOMMIT_ON;
12
use function db2_autocommit;
13
use function db2_commit;
14
use function db2_conn_error;
15
use function db2_conn_errormsg;
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
use function db2_stmt_errormsg;
26
27
class DB2Connection implements Connection, ServerInfoAwareConnection
28
{
29
    /** @var resource */
30
    private $conn = null;
31
32
    /**
33
     * @param mixed[] $params
34
     * @param string  $username
35
     * @param string  $password
36
     * @param mixed[] $driverOptions
37
     *
38
     * @throws DB2Exception
39
     */
40 20
    public function __construct(array $params, $username, $password, $driverOptions = [])
41
    {
42 20
        $isPersistent = (isset($params['persistent']) && $params['persistent'] === true);
43
44 20
        if ($isPersistent) {
45
            $this->conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
46
        } else {
47 20
            $this->conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
48
        }
49 20
        if (! $this->conn) {
50
            throw new DB2Exception(db2_conn_errormsg());
51
        }
52 20
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getServerVersion()
58
    {
59
        /** @var stdClass $serverInfo */
60
        $serverInfo = db2_server_info($this->conn);
61
62
        return $serverInfo->DBMS_VER;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 1
    public function requiresQueryForServerVersion()
69
    {
70 1
        return false;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 229
    public function prepare(string $sql) : DriverStatement
77
    {
78 229
        $stmt = @db2_prepare($this->conn, $sql);
79 229
        if (! $stmt) {
0 ignored issues
show
introduced by
$stmt is of type resource, thus it always evaluated to false.
Loading history...
80
            throw new DB2Exception(db2_stmt_errormsg());
81
        }
82
83 229
        return new DB2Statement($stmt);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 161
    public function query(string $sql) : ResultStatement
90
    {
91 161
        $stmt = $this->prepare($sql);
92 161
        $stmt->execute();
93
94 161
        return $stmt;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 56
    public function quote(string $input) : string
101
    {
102 56
        $input = db2_escape_string($input);
103
104 56
        return "'" . $input . "'";
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 144
    public function exec(string $statement) : int
111
    {
112 144
        $stmt = @db2_exec($this->conn, $statement);
113
114 144
        if ($stmt === false) {
115 86
            throw new DB2Exception(db2_stmt_errormsg());
116
        }
117
118 119
        return db2_num_rows($stmt);
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 2
    public function lastInsertId(?string $name = null) : string
125
    {
126 2
        return db2_last_insert_id($this->conn);
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 13
    public function beginTransaction() : void
133
    {
134 13
        db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF);
0 ignored issues
show
Bug introduced by
DB2_AUTOCOMMIT_OFF of type integer is incompatible with the type boolean expected by parameter $value of db2_autocommit(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
        db2_autocommit($this->conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_OFF);
Loading history...
135 13
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 5
    public function commit() : void
141
    {
142 5
        if (! db2_commit($this->conn)) {
143
            throw new DB2Exception(db2_conn_errormsg($this->conn));
144
        }
145 5
        db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
0 ignored issues
show
Bug introduced by
DB2_AUTOCOMMIT_ON of type integer is incompatible with the type boolean expected by parameter $value of db2_autocommit(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

145
        db2_autocommit($this->conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_ON);
Loading history...
146 5
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 9
    public function rollBack() : void
152
    {
153 9
        if (! db2_rollback($this->conn)) {
154
            throw new DB2Exception(db2_conn_errormsg($this->conn));
155
        }
156 9
        db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
0 ignored issues
show
Bug introduced by
DB2_AUTOCOMMIT_ON of type integer is incompatible with the type boolean expected by parameter $value of db2_autocommit(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

156
        db2_autocommit($this->conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_ON);
Loading history...
157 9
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function errorCode()
163
    {
164
        return db2_conn_error($this->conn);
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function errorInfo()
171
    {
172
        return [
173
            0 => db2_conn_errormsg($this->conn),
174
            1 => $this->errorCode(),
175
        ];
176
    }
177
}
178