Failed Conditions
Push — phpstan ( 752f9f...1acc4a )
by Michael
21:57
created

DB2Connection::lastInsertId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Driver\IBMDB2;
21
22
use Doctrine\DBAL\Driver\Connection;
23
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
24
use Doctrine\DBAL\ParameterType;
25
use stdClass;
26
use const DB2_AUTOCOMMIT_OFF;
27
use const DB2_AUTOCOMMIT_ON;
28
use function db2_autocommit;
29
use function db2_commit;
30
use function db2_conn_error;
31
use function db2_conn_errormsg;
32
use function db2_connect;
33
use function db2_escape_string;
34
use function db2_exec;
35
use function db2_last_insert_id;
36
use function db2_num_rows;
37
use function db2_pconnect;
38
use function db2_prepare;
39
use function db2_rollback;
40
use function db2_server_info;
41
use function db2_stmt_errormsg;
42
use function func_get_args;
43
44
class DB2Connection implements Connection, ServerInfoAwareConnection
45
{
46
    /**
47
     * @var resource
48
     */
49
    private $_conn = null;
50
51
    /**
52
     * @param array  $params
53
     * @param string $username
54
     * @param string $password
55
     * @param array  $driverOptions
56
     *
57
     * @throws \Doctrine\DBAL\Driver\IBMDB2\DB2Exception
58
     */
59
    public function __construct(array $params, $username, $password, $driverOptions = [])
60
    {
61
        $isPersistent = (isset($params['persistent']) && $params['persistent'] == true);
62
63
        if ($isPersistent) {
64
            $this->_conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
65
        } else {
66
            $this->_conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
67
        }
68
        if ( ! $this->_conn) {
69
            throw new DB2Exception(db2_conn_errormsg());
70
        }
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getServerVersion()
77
    {
78
        /** @var stdClass $serverInfo */
79
        $serverInfo = db2_server_info($this->_conn);
80
81
        return $serverInfo->DBMS_VER;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function requiresQueryForServerVersion()
88
    {
89
        return false;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function prepare($sql)
96
    {
97
        $stmt = @db2_prepare($this->_conn, $sql);
98
        if ( ! $stmt) {
0 ignored issues
show
introduced by
$stmt is of type resource, thus it always evaluated to false.
Loading history...
99
            throw new DB2Exception(db2_stmt_errormsg());
100
        }
101
102
        return new DB2Statement($stmt);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function query()
109
    {
110
        $args = func_get_args();
111
        $sql = $args[0];
112
        $stmt = $this->prepare($sql);
113
        $stmt->execute();
114
115
        return $stmt;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function quote($input, $type = ParameterType::STRING)
122
    {
123
        $input = db2_escape_string($input);
124
125
        if ($type === ParameterType::INTEGER) {
126
            return $input;
127
        }
128
129
        return "'".$input."'";
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function exec($statement)
136
    {
137
        $stmt = @db2_exec($this->_conn, $statement);
138
139
        if (false === $stmt) {
140
            throw new DB2Exception(db2_stmt_errormsg());
141
        }
142
143
        return db2_num_rows($stmt);
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function lastInsertId($name = null)
150
    {
151
        return db2_last_insert_id($this->_conn);
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function beginTransaction()
158
    {
159
        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

159
        db2_autocommit($this->_conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_OFF);
Loading history...
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function commit()
166
    {
167
        if (!db2_commit($this->_conn)) {
168
            throw new DB2Exception(db2_conn_errormsg($this->_conn));
169
        }
170
        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

170
        db2_autocommit($this->_conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_ON);
Loading history...
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function rollBack()
177
    {
178
        if (!db2_rollback($this->_conn)) {
179
            throw new DB2Exception(db2_conn_errormsg($this->_conn));
180
        }
181
        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

181
        db2_autocommit($this->_conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_ON);
Loading history...
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187
    public function errorCode()
188
    {
189
        return db2_conn_error($this->_conn);
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195
    public function errorInfo()
196
    {
197
        return [
198
            0 => db2_conn_errormsg($this->_conn),
199
            1 => $this->errorCode(),
200
        ];
201
    }
202
}
203