OCI8Connection.php 5.56 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Driver\OCI8;

5
use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
6
use Doctrine\DBAL\Driver\OCI8\Exception\SequenceDoesNotExist;
7
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
8
use Doctrine\DBAL\ParameterType;
9
use UnexpectedValueException;
10

11 12 13 14 15 16 17 18 19 20 21 22 23
use function addcslashes;
use function func_get_args;
use function is_float;
use function is_int;
use function oci_commit;
use function oci_connect;
use function oci_error;
use function oci_pconnect;
use function oci_rollback;
use function oci_server_version;
use function preg_match;
use function sprintf;
use function str_replace;
24

Grégoire Paris's avatar
Grégoire Paris committed
25 26
use const OCI_COMMIT_ON_SUCCESS;
use const OCI_NO_AUTO_COMMIT;
27

28 29
/**
 * OCI8 implementation of the Connection interface.
30 31
 *
 * @deprecated Use {@link Connection} instead
32
 */
33
class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection
34
{
35
    /** @var resource */
36
    protected $dbh;
37

38
    /** @var int */
39
    protected $executeMode = OCI_COMMIT_ON_SUCCESS;
40

41
    /**
Benjamin Morel's avatar
Benjamin Morel committed
42
     * Creates a Connection to an Oracle Database using oci8 extension.
43
     *
Sergei Morozov's avatar
Sergei Morozov committed
44 45 46 47 48 49
     * @param string $username
     * @param string $password
     * @param string $db
     * @param string $charset
     * @param int    $sessionMode
     * @param bool   $persistent
Benjamin Morel's avatar
Benjamin Morel committed
50 51
     *
     * @throws OCI8Exception
52
     */
Sergei Morozov's avatar
Sergei Morozov committed
53 54 55 56 57
    public function __construct(
        $username,
        $password,
        $db,
        $charset = '',
58
        $sessionMode = OCI_NO_AUTO_COMMIT,
Sergei Morozov's avatar
Sergei Morozov committed
59 60
        $persistent = false
    ) {
Sergei Morozov's avatar
Sergei Morozov committed
61
        $dbh = $persistent
62 63
            ? @oci_pconnect($username, $password, $db, $charset, $sessionMode)
            : @oci_connect($username, $password, $db, $charset, $sessionMode);
64

Sergei Morozov's avatar
Sergei Morozov committed
65
        if ($dbh === false) {
66
            throw OCI8Exception::fromErrorInfo(oci_error());
67
        }
Sergei Morozov's avatar
Sergei Morozov committed
68 69

        $this->dbh = $dbh;
70
    }
71

72 73
    /**
     * {@inheritdoc}
74
     *
75 76
     * @throws UnexpectedValueException If the version string returned by the database server
     *                                  does not contain a parsable version number.
77 78 79
     */
    public function getServerVersion()
    {
Sergei Morozov's avatar
Sergei Morozov committed
80 81 82 83 84 85 86
        $version = oci_server_version($this->dbh);

        if ($version === false) {
            throw OCI8Exception::fromErrorInfo(oci_error($this->dbh));
        }

        if (! preg_match('/\s+(\d+\.\d+\.\d+\.\d+\.\d+)\s+/', $version, $matches)) {
87
            throw new UnexpectedValueException(
88 89 90
                sprintf(
                    'Unexpected database version string "%s". Cannot parse an appropriate version number from it. ' .
                    'Please report this database version string to the Doctrine team.',
Sergei Morozov's avatar
Sergei Morozov committed
91
                    $version
92 93
                )
            );
94 95
        }

Sergei Morozov's avatar
Sergei Morozov committed
96
        return $matches[1];
97 98 99 100 101 102 103 104 105 106
    }

    /**
     * {@inheritdoc}
     */
    public function requiresQueryForServerVersion()
    {
        return false;
    }

107
    /**
Benjamin Morel's avatar
Benjamin Morel committed
108
     * {@inheritdoc}
109
     */
110 111
    public function prepare($prepareString)
    {
112
        return new Statement($this->dbh, $prepareString, $this);
113
    }
114 115

    /**
Benjamin Morel's avatar
Benjamin Morel committed
116
     * {@inheritdoc}
117
     */
118 119 120
    public function query()
    {
        $args = func_get_args();
121
        $sql  = $args[0];
122 123 124
        //$fetchMode = $args[1];
        $stmt = $this->prepare($sql);
        $stmt->execute();
Benjamin Morel's avatar
Benjamin Morel committed
125

126 127
        return $stmt;
    }
128 129

    /**
Benjamin Morel's avatar
Benjamin Morel committed
130
     * {@inheritdoc}
131
     */
132
    public function quote($value, $type = ParameterType::STRING)
133
    {
134 135 136
        if (is_int($value) || is_float($value)) {
            return $value;
        }
Grégoire Paris's avatar
Grégoire Paris committed
137

138
        $value = str_replace("'", "''", $value);
Benjamin Morel's avatar
Benjamin Morel committed
139

140
        return "'" . addcslashes($value, "\000\n\r\\\032") . "'";
141
    }
142 143

    /**
Benjamin Morel's avatar
Benjamin Morel committed
144
     * {@inheritdoc}
145
     */
146 147 148 149
    public function exec($statement)
    {
        $stmt = $this->prepare($statement);
        $stmt->execute();
Benjamin Morel's avatar
Benjamin Morel committed
150

151 152
        return $stmt->rowCount();
    }
153

154
    /**
Benjamin Morel's avatar
Benjamin Morel committed
155
     * {@inheritdoc}
156 157
     *
     * @return int|false
158
     */
159 160
    public function lastInsertId($name = null)
    {
161 162 163 164 165 166
        if ($name === null) {
            return false;
        }

        $sql    = 'SELECT ' . $name . '.CURRVAL FROM DUAL';
        $stmt   = $this->query($sql);
167
        $result = $stmt->fetchColumn();
168

169
        if ($result === false) {
170
            throw SequenceDoesNotExist::new();
171 172
        }

173
        return (int) $result;
174
    }
175

176
    /**
Benjamin Morel's avatar
Benjamin Morel committed
177 178
     * Returns the current execution mode.
     *
179
     * @return int
180 181 182
     */
    public function getExecuteMode()
    {
183
        return $this->executeMode;
184 185
    }

186
    /**
Benjamin Morel's avatar
Benjamin Morel committed
187
     * {@inheritdoc}
188
     */
189 190
    public function beginTransaction()
    {
191
        $this->executeMode = OCI_NO_AUTO_COMMIT;
Benjamin Morel's avatar
Benjamin Morel committed
192

193 194
        return true;
    }
195 196

    /**
Benjamin Morel's avatar
Benjamin Morel committed
197
     * {@inheritdoc}
198
     */
199 200
    public function commit()
    {
201
        if (! oci_commit($this->dbh)) {
202 203
            throw OCI8Exception::fromErrorInfo($this->errorInfo());
        }
Grégoire Paris's avatar
Grégoire Paris committed
204

205
        $this->executeMode = OCI_COMMIT_ON_SUCCESS;
Benjamin Morel's avatar
Benjamin Morel committed
206

207
        return true;
208
    }
209 210

    /**
Benjamin Morel's avatar
Benjamin Morel committed
211
     * {@inheritdoc}
212
     */
213 214
    public function rollBack()
    {
215
        if (! oci_rollback($this->dbh)) {
216 217
            throw OCI8Exception::fromErrorInfo($this->errorInfo());
        }
Grégoire Paris's avatar
Grégoire Paris committed
218

219
        $this->executeMode = OCI_COMMIT_ON_SUCCESS;
Benjamin Morel's avatar
Benjamin Morel committed
220

221
        return true;
222
    }
223

Benjamin Morel's avatar
Benjamin Morel committed
224 225
    /**
     * {@inheritdoc}
226 227
     *
     * @deprecated The error information is available via exceptions.
Benjamin Morel's avatar
Benjamin Morel committed
228
     */
229 230
    public function errorCode()
    {
231
        $error = oci_error($this->dbh);
232

233
        if ($error !== false) {
234
            return $error['code'];
235
        }
Benjamin Morel's avatar
Benjamin Morel committed
236

237
        return null;
238
    }
239

Benjamin Morel's avatar
Benjamin Morel committed
240 241
    /**
     * {@inheritdoc}
242 243
     *
     * @deprecated The error information is available via exceptions.
Benjamin Morel's avatar
Benjamin Morel committed
244
     */
245 246
    public function errorInfo()
    {
Sergei Morozov's avatar
Sergei Morozov committed
247 248 249 250 251 252 253
        $error = oci_error($this->dbh);

        if ($error === false) {
            return [];
        }

        return $error;
254
    }
255
}