Completed
Push — ci-mysql8 ( 306e40...6f2209 )
by Sergei
02:42
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
 * 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 const DB2_AUTOCOMMIT_OFF;
26
use const DB2_AUTOCOMMIT_ON;
27
use function db2_autocommit;
28
use function db2_commit;
29
use function db2_conn_error;
30
use function db2_conn_errormsg;
31
use function db2_connect;
32
use function db2_escape_string;
33
use function db2_exec;
34
use function db2_last_insert_id;
35
use function db2_num_rows;
36
use function db2_pconnect;
37
use function db2_prepare;
38
use function db2_rollback;
39
use function db2_server_info;
40
use function db2_stmt_errormsg;
41
use function func_get_args;
42
43
class DB2Connection implements Connection, ServerInfoAwareConnection
44
{
45
    /**
46
     * @var resource
47
     */
48
    private $_conn = null;
49
50
    /**
51
     * @param array  $params
52
     * @param string $username
53
     * @param string $password
54
     * @param array  $driverOptions
55
     *
56
     * @throws \Doctrine\DBAL\Driver\IBMDB2\DB2Exception
57
     */
58 21
    public function __construct(array $params, $username, $password, $driverOptions = [])
59
    {
60 21
        $isPersistent = (isset($params['persistent']) && $params['persistent'] == true);
61
62 21
        if ($isPersistent) {
63
            $this->_conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
64
        } else {
65 21
            $this->_conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
66
        }
67 21
        if ( ! $this->_conn) {
68
            throw new DB2Exception(db2_conn_errormsg());
69
        }
70 21
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getServerVersion()
76
    {
77
        $serverInfo = db2_server_info($this->_conn);
78
79
        return $serverInfo->DBMS_VER;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 1
    public function requiresQueryForServerVersion()
86
    {
87 1
        return false;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 223
    public function prepare($sql)
94
    {
95 223
        $stmt = @db2_prepare($this->_conn, $sql);
96 223
        if ( ! $stmt) {
0 ignored issues
show
introduced by
$stmt is of type resource, thus it always evaluated to false.
Loading history...
97
            throw new DB2Exception(db2_stmt_errormsg());
98
        }
99
100 223
        return new DB2Statement($stmt);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 155
    public function query()
107
    {
108 155
        $args = func_get_args();
109 155
        $sql = $args[0];
110 155
        $stmt = $this->prepare($sql);
111 155
        $stmt->execute();
112
113 155
        return $stmt;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 4
    public function quote($input, $type = ParameterType::STRING)
120
    {
121 4
        $input = db2_escape_string($input);
122
123 4
        if ($type === ParameterType::INTEGER) {
124
            return $input;
125
        }
126
127 4
        return "'".$input."'";
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 140
    public function exec($statement)
134
    {
135 140
        $stmt = @db2_exec($this->_conn, $statement);
136
137 140
        if (false === $stmt) {
138 86
            throw new DB2Exception(db2_stmt_errormsg());
139
        }
140
141 115
        return db2_num_rows($stmt);
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 2
    public function lastInsertId($name = null)
148
    {
149 2
        return db2_last_insert_id($this->_conn);
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 13
    public function beginTransaction()
156
    {
157 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

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

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

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