Failed Conditions
Push — type-registry ( 0931f1...33c798 )
by Michael
23:21
created

DB2Connection   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 21
eloc 41
dl 0
loc 157
ccs 0
cts 85
cp 0
rs 10
c 0
b 0
f 0

13 Methods

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