Completed
Push — develop ( fa42c1...0ef7d4 )
by Sergei
22:52
created

DB2Connection   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 46.34%

Importance

Changes 0
Metric Value
wmc 21
eloc 38
dl 0
loc 152
ccs 38
cts 82
cp 0.4634
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A errorCode() 0 3 1
A getServerVersion() 0 6 1
A errorInfo() 0 5 1
A exec() 0 9 2
A query() 0 6 1
A prepare() 0 8 2
A __construct() 0 11 4
A rollBack() 0 6 2
A lastInsertId() 0 3 1
A requiresQueryForServerVersion() 0 3 1
A quote() 0 9 2
A beginTransaction() 0 3 1
A commit() 0 6 2
1
<?php
2
3
namespace Doctrine\DBAL\Driver\IBMDB2;
4
5
use Doctrine\DBAL\Driver\Connection;
6
use Doctrine\DBAL\Driver\ResultStatement;
7
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
8
use Doctrine\DBAL\Driver\Statement as DriverStatement;
9
use Doctrine\DBAL\ParameterType;
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 DB2Exception
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 DB2Exception(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 DB2Exception(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 57
    public function quote($input, $type = ParameterType::STRING)
102
    {
103 57
        $input = db2_escape_string($input);
104
105 57
        if ($type === ParameterType::INTEGER) {
106
            return $input;
107
        }
108
109 57
        return "'" . $input . "'";
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 144
    public function exec(string $statement) : int
116
    {
117 144
        $stmt = @db2_exec($this->conn, $statement);
118
119 144
        if ($stmt === false) {
120 86
            throw new DB2Exception(db2_stmt_errormsg());
121
        }
122
123 119
        return db2_num_rows($stmt);
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 2
    public function lastInsertId($name = null)
130
    {
131 2
        return db2_last_insert_id($this->conn);
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 13
    public function beginTransaction()
138
    {
139 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

139
        db2_autocommit($this->conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_OFF);
Loading history...
140 13
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 5
    public function commit()
146
    {
147 5
        if (! db2_commit($this->conn)) {
148
            throw new DB2Exception(db2_conn_errormsg($this->conn));
149
        }
150 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

150
        db2_autocommit($this->conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_ON);
Loading history...
151 5
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156 9
    public function rollBack()
157
    {
158 9
        if (! db2_rollback($this->conn)) {
159
            throw new DB2Exception(db2_conn_errormsg($this->conn));
160
        }
161 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

161
        db2_autocommit($this->conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_ON);
Loading history...
162 9
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function errorCode()
168
    {
169
        return db2_conn_error($this->conn);
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function errorInfo()
176
    {
177
        return [
178
            0 => db2_conn_errormsg($this->conn),
179
            1 => $this->errorCode(),
180
        ];
181
    }
182
}
183