Completed
Push — master ( af81c1...f3339a )
by Sergei
21s queued 15s
created

OCI8Connection::getServerVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
ccs 0
cts 12
cp 0
cc 2
eloc 7
nc 2
nop 0
crap 6
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\OCI8;
21
22
use Doctrine\DBAL\Driver\Connection;
23
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
24
use Doctrine\DBAL\ParameterType;
25
use const OCI_COMMIT_ON_SUCCESS;
26
use const OCI_DEFAULT;
27
use const OCI_NO_AUTO_COMMIT;
28
use function addcslashes;
29
use function define;
30
use function defined;
31
use function func_get_args;
32
use function is_float;
33
use function is_int;
34
use function oci_commit;
35
use function oci_connect;
36
use function oci_error;
37
use function oci_pconnect;
38
use function oci_rollback;
39
use function oci_server_version;
40
use function preg_match;
41
use function sprintf;
42
use function str_replace;
43
44
/**
45
 * OCI8 implementation of the Connection interface.
46
 *
47
 * @since 2.0
48
 */
49
class OCI8Connection implements Connection, ServerInfoAwareConnection
50
{
51
    /**
52
     * @var resource
53
     */
54
    protected $dbh;
55
56
    /**
57
     * @var int
58
     */
59
    protected $executeMode = OCI_COMMIT_ON_SUCCESS;
60
61
    /**
62
     * Creates a Connection to an Oracle Database using oci8 extension.
63
     *
64
     * @param string      $username
65
     * @param string      $password
66
     * @param string      $db
67
     * @param string|null $charset
68
     * @param int         $sessionMode
69
     * @param bool        $persistent
70
     *
71
     * @throws OCI8Exception
72
     */
73 78
    public function __construct($username, $password, $db, $charset = null, $sessionMode = OCI_DEFAULT, $persistent = false)
74
    {
75 78
        if (!defined('OCI_NO_AUTO_COMMIT')) {
76
            define('OCI_NO_AUTO_COMMIT', 0);
77
        }
78
79 78
        $this->dbh = $persistent
80
            ? @oci_pconnect($username, $password, $db, $charset, $sessionMode)
81 78
            : @oci_connect($username, $password, $db, $charset, $sessionMode);
82
83 78
        if ( ! $this->dbh) {
84 3
            throw OCI8Exception::fromErrorInfo(oci_error());
85
        }
86 75
    }
87
88
    /**
89
     * {@inheritdoc}
90
     *
91
     * @throws \UnexpectedValueException if the version string returned by the database server
92
     *                                   does not contain a parsable version number.
93
     */
94
    public function getServerVersion()
95
    {
96
        if ( ! preg_match('/\s+(\d+\.\d+\.\d+\.\d+\.\d+)\s+/', oci_server_version($this->dbh), $version)) {
97
            throw new \UnexpectedValueException(
98
                sprintf(
99
                    'Unexpected database version string "%s". Cannot parse an appropriate version number from it. ' .
100
                    'Please report this database version string to the Doctrine team.',
101
                    oci_server_version($this->dbh)
102
                )
103
            );
104
        }
105
106
        return $version[1];
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 1
    public function requiresQueryForServerVersion()
113
    {
114 1
        return false;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 256
    public function prepare($prepareString)
121
    {
122 256
        return new OCI8Statement($this->dbh, $prepareString, $this);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 167
    public function query()
129
    {
130 167
        $args = func_get_args();
131 167
        $sql = $args[0];
132
        //$fetchMode = $args[1];
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
133 167
        $stmt = $this->prepare($sql);
134 167
        $stmt->execute();
135
136 164
        return $stmt;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 4
    public function quote($value, $type = ParameterType::STRING)
143
    {
144 4
        if (is_int($value) || is_float($value)) {
145 2
            return $value;
146
        }
147 3
        $value = str_replace("'", "''", $value);
148
149 3
        return "'" . addcslashes($value, "\000\n\r\\\032") . "'";
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 169
    public function exec($statement)
156
    {
157 169
        $stmt = $this->prepare($statement);
158 169
        $stmt->execute();
159
160 155
        return $stmt->rowCount();
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 4
    public function lastInsertId($name = null)
167
    {
168 4
        if ($name === null) {
169 1
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Doctrine\DBAL\Driver\Connection::lastInsertId() of string.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
170
        }
171
172 3
        $sql    = 'SELECT ' . $name . '.CURRVAL FROM DUAL';
173 3
        $stmt   = $this->query($sql);
174 3
        $result = $stmt->fetchColumn();
175
176 3
        if ($result === false) {
177
            throw new OCI8Exception("lastInsertId failed: Query was executed but no result was returned.");
178
        }
179
180 3
        return (int) $result;
181
    }
182
183
    /**
184
     * Returns the current execution mode.
185
     *
186
     * @return int
187
     */
188 251
    public function getExecuteMode()
189
    {
190 251
        return $this->executeMode;
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196 15
    public function beginTransaction()
197
    {
198 15
        $this->executeMode = OCI_NO_AUTO_COMMIT;
199
200 15
        return true;
201
    }
202
203
    /**
204
     * {@inheritdoc}
205
     */
206 7
    public function commit()
207
    {
208 7
        if (!oci_commit($this->dbh)) {
209
            throw OCI8Exception::fromErrorInfo($this->errorInfo());
210
        }
211 7
        $this->executeMode = OCI_COMMIT_ON_SUCCESS;
212
213 7
        return true;
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219 9
    public function rollBack()
220
    {
221 9
        if (!oci_rollback($this->dbh)) {
222
            throw OCI8Exception::fromErrorInfo($this->errorInfo());
223
        }
224 9
        $this->executeMode = OCI_COMMIT_ON_SUCCESS;
225
226 9
        return true;
227
    }
228
229
    /**
230
     * {@inheritdoc}
231
     */
232
    public function errorCode()
233
    {
234
        $error = oci_error($this->dbh);
235
        if ($error !== false) {
236
            $error = $error['code'];
237
        }
238
239
        return $error;
240
    }
241
242
    /**
243
     * {@inheritdoc}
244
     */
245
    public function errorInfo()
246
    {
247
        return oci_error($this->dbh);
248
    }
249
}
250