Completed
Pull Request — 2.10.x (#3994)
by Grégoire
11:52
created

DB2Connection::lastInsertId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1.037
1
<?php
2
3
namespace Doctrine\DBAL\Driver\IBMDB2;
4
5
use Doctrine\DBAL\Driver\Connection;
6
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
7
use Doctrine\DBAL\ParameterType;
8
use stdClass;
9
use function assert;
10
use function db2_autocommit;
11
use function db2_commit;
12
use function db2_conn_error;
13
use function db2_conn_errormsg;
14
use function db2_connect;
15
use function db2_escape_string;
16
use function db2_exec;
17
use function db2_last_insert_id;
18
use function db2_num_rows;
19
use function db2_pconnect;
20
use function db2_prepare;
21
use function db2_rollback;
22
use function db2_server_info;
23
use function db2_stmt_errormsg;
24
use function func_get_args;
25
use function is_bool;
26
use const DB2_AUTOCOMMIT_OFF;
27
use const DB2_AUTOCOMMIT_ON;
28
29
class DB2Connection implements Connection, ServerInfoAwareConnection
30
{
31
    /** @var resource */
32
    private $conn = null;
33
34
    /**
35
     * @param mixed[] $params
36
     * @param string  $username
37
     * @param string  $password
38
     * @param mixed[] $driverOptions
39
     *
40
     * @throws DB2Exception
41
     */
42 225
    public function __construct(array $params, $username, $password, $driverOptions = [])
43
    {
44 225
        $isPersistent = (isset($params['persistent']) && $params['persistent'] === true);
45
46 225
        if ($isPersistent) {
47
            $conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
48
        } else {
49 225
            $conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
50
        }
51
52 225
        if ($conn === false) {
53
            throw new DB2Exception(db2_conn_errormsg());
54
        }
55
56 225
        $this->conn = $conn;
57 225
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getServerVersion()
63
    {
64
        $serverInfo = db2_server_info($this->conn);
65
        assert($serverInfo instanceof stdClass);
66
67
        return $serverInfo->DBMS_VER;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 226
    public function requiresQueryForServerVersion()
74
    {
75 226
        return false;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 225
    public function prepare($sql)
82
    {
83 225
        $stmt = @db2_prepare($this->conn, $sql);
84 225
        if (! $stmt) {
0 ignored issues
show
introduced by
$stmt is of type false|resource, thus it always evaluated to false.
Loading history...
85
            throw new DB2Exception(db2_stmt_errormsg());
86
        }
87
88 225
        return new DB2Statement($stmt);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 225
    public function query()
95
    {
96 225
        $args = func_get_args();
97 225
        $sql  = $args[0];
98 225
        $stmt = $this->prepare($sql);
99 225
        $stmt->execute();
100
101 225
        return $stmt;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 225
    public function quote($input, $type = ParameterType::STRING)
108
    {
109 225
        $input = db2_escape_string($input);
110
111 225
        if ($type === ParameterType::INTEGER) {
112
            return $input;
113
        }
114
115 225
        return "'" . $input . "'";
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 225
    public function exec($statement)
122
    {
123 225
        $stmt = @db2_exec($this->conn, $statement);
124
125 225
        if ($stmt === false) {
126 225
            throw new DB2Exception(db2_stmt_errormsg());
127
        }
128
129 225
        return db2_num_rows($stmt);
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 7
    public function lastInsertId($name = null)
136
    {
137 7
        return db2_last_insert_id($this->conn);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 218
    public function beginTransaction()
144
    {
145 218
        $result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF);
146 218
        assert(is_bool($result));
147
148 218
        return $result;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154 214
    public function commit()
155
    {
156 214
        if (! db2_commit($this->conn)) {
157
            throw new DB2Exception(db2_conn_errormsg($this->conn));
158
        }
159
160 214
        $result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
161 214
        assert(is_bool($result));
162
163 214
        return $result;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169 218
    public function rollBack()
170
    {
171 218
        if (! db2_rollback($this->conn)) {
172
            throw new DB2Exception(db2_conn_errormsg($this->conn));
173
        }
174
175 218
        $result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
176 218
        assert(is_bool($result));
177
178 218
        return $result;
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function errorCode()
185
    {
186
        return db2_conn_error($this->conn);
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192
    public function errorInfo()
193
    {
194
        return [
195
            0 => db2_conn_errormsg($this->conn),
196
            1 => $this->errorCode(),
197
        ];
198
    }
199
}
200