Failed Conditions
Pull Request — develop (#3368)
by Benjamin
42:38 queued 39:25
created

DB2Connection::lastInsertId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 5
cts 7
cp 0.7143
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0932
1
<?php
2
3
namespace Doctrine\DBAL\Driver\IBMDB2;
4
5
use Doctrine\DBAL\Driver\Connection;
6
use Doctrine\DBAL\Driver\DriverException;
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_conn_error;
16
use function db2_conn_errormsg;
17
use function db2_connect;
18
use function db2_escape_string;
19
use function db2_exec;
20
use function db2_last_insert_id;
21
use function db2_num_rows;
22
use function db2_pconnect;
23
use function db2_prepare;
24
use function db2_rollback;
25
use function db2_server_info;
26
use function db2_stmt_errormsg;
27
28
class DB2Connection implements Connection, ServerInfoAwareConnection
29
{
30
    /** @var resource */
31
    private $conn = null;
32
33
    /**
34
     * @param mixed[] $params
35
     * @param string  $username
36
     * @param string  $password
37
     * @param mixed[] $driverOptions
38
     *
39
     * @throws DriverException
40
     */
41 21
    public function __construct(array $params, $username, $password, $driverOptions = [])
42
    {
43 21
        $isPersistent = (isset($params['persistent']) && $params['persistent'] === true);
44
45 21
        if ($isPersistent) {
46
            $this->conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
47
        } else {
48 21
            $this->conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
49
        }
50 21
        if (! $this->conn) {
51
            throw new DriverException(db2_conn_errormsg());
52
        }
53 21
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getServerVersion()
59
    {
60
        /** @var stdClass $serverInfo */
61
        $serverInfo = db2_server_info($this->conn);
62
63
        return $serverInfo->DBMS_VER;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 1
    public function requiresQueryForServerVersion()
70
    {
71 1
        return false;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 229
    public function prepare(string $sql) : DriverStatement
78
    {
79 229
        $stmt = @db2_prepare($this->conn, $sql);
80 229
        if (! $stmt) {
0 ignored issues
show
introduced by
$stmt is of type resource, thus it always evaluated to false.
Loading history...
81
            throw new DriverException(db2_stmt_errormsg());
82
        }
83
84 229
        return new DB2Statement($stmt);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 161
    public function query(string $sql) : ResultStatement
91
    {
92 161
        $stmt = $this->prepare($sql);
93 161
        $stmt->execute();
94
95 161
        return $stmt;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 56
    public function quote(string $input) : string
102
    {
103 56
        $input = db2_escape_string($input);
104
105 56
        return "'" . $input . "'";
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 146
    public function exec(string $statement) : int
112
    {
113 146
        $stmt = @db2_exec($this->conn, $statement);
114
115 146
        if ($stmt === false) {
116 88
            throw new DriverException(db2_stmt_errormsg());
117
        }
118
119 121
        return db2_num_rows($stmt);
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 3
    public function lastInsertId() : string
126
    {
127 3
        $lastInsertId = db2_last_insert_id($this->conn);
128
129 3
        if ($lastInsertId === '') {
130 1
            throw DriverException::noInsertId();
131
        }
132
133 2
        return $lastInsertId;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 1
    public function getSequenceNumber(string $name) : string
140
    {
141 1
        throw new DriverException('Sequences on IBM DB2 are not currently supported.');
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 13
    public function beginTransaction() : void
148
    {
149 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

149
        db2_autocommit($this->conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_OFF);
Loading history...
150 13
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 5
    public function commit() : void
156
    {
157 5
        if (! db2_commit($this->conn)) {
158
            throw new DriverException(db2_conn_errormsg($this->conn));
159
        }
160 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

160
        db2_autocommit($this->conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_ON);
Loading history...
161 5
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 9
    public function rollBack() : void
167
    {
168 9
        if (! db2_rollback($this->conn)) {
169
            throw new DriverException(db2_conn_errormsg($this->conn));
170
        }
171 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

171
        db2_autocommit($this->conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_ON);
Loading history...
172 9
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function errorCode()
178
    {
179
        return db2_conn_error($this->conn);
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public function errorInfo()
186
    {
187
        return [
188
            0 => db2_conn_errormsg($this->conn),
189
            1 => $this->errorCode(),
190
        ];
191
    }
192
}
193