Completed
Push — master ( 5dd66e...5b5c2c )
by Marco
114:19 queued 111:15
created

lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php (4 issues)

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 223
    public function __construct(array $params, $username, $password, $driverOptions = [])
41
    {
42 223
        $isPersistent = (isset($params['persistent']) && $params['persistent'] === true);
43
44 223
        if ($isPersistent) {
45
            $conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
46
        } else {
47 223
            $conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
48
        }
49
50 223
        if ($conn === false) {
51
            throw new DB2Exception(db2_conn_errormsg());
52
        }
53
54 223
        $this->conn = $conn;
55 223
    }
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 224
    public function requiresQueryForServerVersion()
72
    {
73 224
        return false;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 223
    public function prepare($sql)
80
    {
81 223
        $stmt = @db2_prepare($this->conn, $sql);
82 223
        if (! $stmt) {
0 ignored issues
show
$stmt is of type resource, thus it always evaluated to false.
Loading history...
83
            throw new DB2Exception(db2_stmt_errormsg());
84
        }
85
86 223
        return new DB2Statement($stmt);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 223
    public function query()
93
    {
94 223
        $args = func_get_args();
95 223
        $sql  = $args[0];
96 223
        $stmt = $this->prepare($sql);
97 223
        $stmt->execute();
98
99 223
        return $stmt;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 223
    public function quote($input, $type = ParameterType::STRING)
106
    {
107 223
        $input = db2_escape_string($input);
108
109 223
        if ($type === ParameterType::INTEGER) {
110
            return $input;
111
        }
112
113 223
        return "'" . $input . "'";
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 223
    public function exec($statement)
120
    {
121 223
        $stmt = @db2_exec($this->conn, $statement);
122
123 223
        if ($stmt === false) {
124 223
            throw new DB2Exception(db2_stmt_errormsg());
125
        }
126
127 223
        return db2_num_rows($stmt);
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 7
    public function lastInsertId($name = null)
134
    {
135 7
        return db2_last_insert_id($this->conn);
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 217
    public function beginTransaction()
142
    {
143 217
        db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF);
0 ignored issues
show
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

143
        db2_autocommit($this->conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_OFF);
Loading history...
144 217
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 214
    public function commit()
150
    {
151 214
        if (! db2_commit($this->conn)) {
152
            throw new DB2Exception(db2_conn_errormsg($this->conn));
153
        }
154 214
        db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
0 ignored issues
show
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

154
        db2_autocommit($this->conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_ON);
Loading history...
155 214
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 217
    public function rollBack()
161
    {
162 217
        if (! db2_rollback($this->conn)) {
163
            throw new DB2Exception(db2_conn_errormsg($this->conn));
164
        }
165 217
        db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
0 ignored issues
show
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

165
        db2_autocommit($this->conn, /** @scrutinizer ignore-type */ DB2_AUTOCOMMIT_ON);
Loading history...
166 217
    }
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