Completed
Push — develop ( 361a2b...a96e4b )
by Marco
20s queued 14s
created

DB2Connection::errorCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=0);
4
5
namespace Doctrine\DBAL\Driver\IBMDB2;
6
7
use Doctrine\DBAL\Driver\Connection;
8
use Doctrine\DBAL\Driver\ResultStatement;
9
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
10
use Doctrine\DBAL\Driver\Statement as DriverStatement;
11
use stdClass;
12
use const DB2_AUTOCOMMIT_OFF;
13
use const DB2_AUTOCOMMIT_ON;
14
use function db2_autocommit;
15
use function db2_commit;
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
26
class DB2Connection implements Connection, ServerInfoAwareConnection
27
{
28
    /** @var resource */
29
    private $conn = null;
30
31
    /**
32
     * @param mixed[] $params
33
     * @param string  $username
34
     * @param string  $password
35
     * @param mixed[] $driverOptions
36
     *
37
     * @throws DB2Exception
38
     */
39
    public function __construct(array $params, $username, $password, $driverOptions = [])
40 220
    {
41
        $isPersistent = (isset($params['persistent']) && $params['persistent'] === true);
42 220
43
        if ($isPersistent) {
44 220
            $conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
45
        } else {
46
            $conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
47 220
        }
48
49
        if ($conn === false) {
50 220
            throw DB2Exception::fromConnectionError();
51
        }
52
53
        $this->conn = $conn;
54 220
    }
55 220
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getServerVersion()
60
    {
61
        /** @var stdClass $serverInfo */
62
        $serverInfo = db2_server_info($this->conn);
63
64
        return $serverInfo->DBMS_VER;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function requiresQueryForServerVersion()
71 221
    {
72
        return false;
73 221
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function prepare(string $sql) : DriverStatement
79 220
    {
80
        $stmt = @db2_prepare($this->conn, $sql);
81 220
        if (! $stmt) {
0 ignored issues
show
introduced by
$stmt is of type false|resource, thus it always evaluated to false.
Loading history...
82 220
            throw DB2Exception::fromStatementError();
83
        }
84
85
        return new DB2Statement($stmt);
86 220
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function query(string $sql) : ResultStatement
92 219
    {
93
        $stmt = $this->prepare($sql);
94 219
        $stmt->execute();
95 219
96
        return $stmt;
97 219
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function quote(string $input) : string
103 207
    {
104
        return "'" . db2_escape_string($input) . "'";
105 207
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function exec(string $statement) : int
111 220
    {
112
        $stmt = @db2_exec($this->conn, $statement);
113 220
114
        if ($stmt === false) {
115 220
            throw DB2Exception::fromStatementError();
116 220
        }
117
118
        return db2_num_rows($stmt);
119 220
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function lastInsertId($name = null)
125 7
    {
126
        return db2_last_insert_id($this->conn);
127 7
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function beginTransaction() : void
133 214
    {
134
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF)) {
135 214
            throw DB2Exception::fromConnectionError($this->conn);
136
        }
137
    }
138 214
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function commit() : void
143 211
    {
144
        if (! db2_commit($this->conn)) {
145 211
            throw DB2Exception::fromConnectionError($this->conn);
146
        }
147
148
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) {
149 211
            throw DB2Exception::fromConnectionError($this->conn);
150
        }
151
    }
152 211
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function rollBack() : void
157 214
    {
158
        if (! db2_rollback($this->conn)) {
159 214
            throw DB2Exception::fromConnectionError($this->conn);
160
        }
161
162
        if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) {
163 214
            throw DB2Exception::fromConnectionError($this->conn);
164
        }
165
    }
166
}
167