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

DB2Connection::errorInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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 21
    public function __construct(array $params, $username, $password, $driverOptions = [])
41
    {
42 21
        $isPersistent = (isset($params['persistent']) && $params['persistent'] === true);
43
44 21
        if ($isPersistent) {
45
            $this->conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
46
        } else {
47 21
            $this->conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
48
        }
49 21
        if (! $this->conn) {
50
            throw new DB2Exception(db2_conn_errormsg());
51
        }
52 21
    }
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 146
    public function exec(string $statement) : int
111
    {
112 146
        $stmt = @db2_exec($this->conn, $statement);
113
114 146
        if ($stmt === false) {
115 88
            throw new DB2Exception(db2_stmt_errormsg());
116
        }
117
118 121
        return db2_num_rows($stmt);
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 4
    public function lastInsertId(?string $name = null) : string
125
    {
126 4
        $lastInsertId = db2_last_insert_id($this->conn);
127
128 4
        if ($lastInsertId === '0' || $lastInsertId === '') {
129 1
            throw new DB2Exception('The last statement did not return an insert ID.');
130
        }
131
132 3
        return $lastInsertId;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 13
    public function beginTransaction() : void
139
    {
140 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

140
        db2_autocommit($this->conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_OFF);
Loading history...
141 13
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 5
    public function commit() : void
147
    {
148 5
        if (! db2_commit($this->conn)) {
149
            throw new DB2Exception(db2_conn_errormsg($this->conn));
150
        }
151 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

151
        db2_autocommit($this->conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_ON);
Loading history...
152 5
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157 9
    public function rollBack() : void
158
    {
159 9
        if (! db2_rollback($this->conn)) {
160
            throw new DB2Exception(db2_conn_errormsg($this->conn));
161
        }
162 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

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