Completed
Push — master ( c7757e...39cb21 )
by Luís
16s
created

lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php (2 issues)

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
25
/**
26
 * OCI8 implementation of the Connection interface.
27
 *
28
 * @since 2.0
29
 */
30
class OCI8Connection implements Connection, ServerInfoAwareConnection
31
{
32
    /**
33
     * @var resource
34
     */
35
    protected $dbh;
36
37
    /**
38
     * @var integer
39
     */
40
    protected $executeMode = OCI_COMMIT_ON_SUCCESS;
41
42
    /**
43
     * Creates a Connection to an Oracle Database using oci8 extension.
44
     *
45
     * @param string      $username
46
     * @param string      $password
47
     * @param string      $db
48
     * @param string|null $charset
49
     * @param integer     $sessionMode
50
     * @param boolean     $persistent
51
     *
52
     * @throws OCI8Exception
53
     */
54
    public function __construct($username, $password, $db, $charset = null, $sessionMode = OCI_DEFAULT, $persistent = false)
55
    {
56
        if (!defined('OCI_NO_AUTO_COMMIT')) {
57
            define('OCI_NO_AUTO_COMMIT', 0);
58
        }
59
60
        $this->dbh = $persistent
61
            ? @oci_pconnect($username, $password, $db, $charset, $sessionMode)
62
            : @oci_connect($username, $password, $db, $charset, $sessionMode);
63
64
        if ( ! $this->dbh) {
65
            throw OCI8Exception::fromErrorInfo(oci_error());
66
        }
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     *
72
     * @throws \UnexpectedValueException if the version string returned by the database server
73
     *                                   does not contain a parsable version number.
74
     */
75
    public function getServerVersion()
76
    {
77
        if ( ! preg_match('/\s+(\d+\.\d+\.\d+\.\d+\.\d+)\s+/', oci_server_version($this->dbh), $version)) {
78
            throw new \UnexpectedValueException(
79
                sprintf(
80
                    'Unexpected database version string "%s". Cannot parse an appropriate version number from it. ' .
81
                    'Please report this database version string to the Doctrine team.',
82
                    oci_server_version($this->dbh)
83
                )
84
            );
85
        }
86
87
        return $version[1];
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function requiresQueryForServerVersion()
94
    {
95
        return false;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function prepare($prepareString)
102
    {
103
        return new OCI8Statement($this->dbh, $prepareString, $this);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 View Code Duplication
    public function query()
110
    {
111
        $args = func_get_args();
112
        $sql = $args[0];
113
        //$fetchMode = $args[1];
114
        $stmt = $this->prepare($sql);
115
        $stmt->execute();
116
117
        return $stmt;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 View Code Duplication
    public function quote($value, $type=\PDO::PARAM_STR)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        if (is_int($value) || is_float($value)) {
126
            return $value;
127
        }
128
        $value = str_replace("'", "''", $value);
129
130
        return "'" . addcslashes($value, "\000\n\r\\\032") . "'";
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function exec($statement)
137
    {
138
        $stmt = $this->prepare($statement);
139
        $stmt->execute();
140
141
        return $stmt->rowCount();
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function lastInsertId($name = null)
148
    {
149
        if ($name === null) {
150
            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...
151
        }
152
153
        $sql    = 'SELECT ' . $name . '.CURRVAL FROM DUAL';
154
        $stmt   = $this->query($sql);
155
        $result = $stmt->fetch(\PDO::FETCH_ASSOC);
156
157
        if ($result === false || !isset($result['CURRVAL'])) {
158
            throw new OCI8Exception("lastInsertId failed: Query was executed but no result was returned.");
159
        }
160
161
        return (int) $result['CURRVAL'];
162
    }
163
164
    /**
165
     * Returns the current execution mode.
166
     *
167
     * @return integer
168
     */
169
    public function getExecuteMode()
170
    {
171
        return $this->executeMode;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function beginTransaction()
178
    {
179
        $this->executeMode = OCI_NO_AUTO_COMMIT;
180
181
        return true;
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187 View Code Duplication
    public function commit()
188
    {
189
        if (!oci_commit($this->dbh)) {
190
            throw OCI8Exception::fromErrorInfo($this->errorInfo());
191
        }
192
        $this->executeMode = OCI_COMMIT_ON_SUCCESS;
193
194
        return true;
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200 View Code Duplication
    public function rollBack()
201
    {
202
        if (!oci_rollback($this->dbh)) {
203
            throw OCI8Exception::fromErrorInfo($this->errorInfo());
204
        }
205
        $this->executeMode = OCI_COMMIT_ON_SUCCESS;
206
207
        return true;
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213 View Code Duplication
    public function errorCode()
214
    {
215
        $error = oci_error($this->dbh);
216
        if ($error !== false) {
217
            $error = $error['code'];
218
        }
219
220
        return $error;
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226
    public function errorInfo()
227
    {
228
        return oci_error($this->dbh);
229
    }
230
}
231