OCI8Connection.php 4.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Driver\OCI8;

22 23
use Doctrine\DBAL\Platforms\OraclePlatform;

24 25 26 27 28
/**
 * OCI8 implementation of the Connection interface.
 *
 * @since 2.0
 */
29
class OCI8Connection implements \Doctrine\DBAL\Driver\Connection
30
{
31 32 33 34
    /**
     * @var resource
     */
    protected $dbh;
35

36
    /**
Benjamin Morel's avatar
Benjamin Morel committed
37
     * @var integer
38 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
     *
Benjamin Morel's avatar
Benjamin Morel committed
44 45 46 47 48 49 50 51
     * @param string      $username
     * @param string      $password
     * @param string      $db
     * @param string|null $charset
     * @param integer     $sessionMode
     * @param boolean     $persistent
     *
     * @throws OCI8Exception
52
     */
53
    public function __construct($username, $password, $db, $charset = null, $sessionMode = OCI_DEFAULT, $persistent = false)
54
    {
55 56 57 58
        if (!defined('OCI_NO_AUTO_COMMIT')) {
            define('OCI_NO_AUTO_COMMIT', 0);
        }

59
        $this->dbh = $persistent
60 61
            ? @oci_pconnect($username, $password, $db, $charset, $sessionMode)
            : @oci_connect($username, $password, $db, $charset, $sessionMode);
62

63
        if ( ! $this->dbh) {
64
            throw OCI8Exception::fromErrorInfo(oci_error());
65
        }
66
    }
67 68

    /**
Benjamin Morel's avatar
Benjamin Morel committed
69
     * {@inheritdoc}
70
     */
71 72
    public function prepare($prepareString)
    {
73
        return new OCI8Statement($this->dbh, $prepareString, $this);
74
    }
75 76

    /**
Benjamin Morel's avatar
Benjamin Morel committed
77
     * {@inheritdoc}
78
     */
79 80 81 82 83 84 85
    public function query()
    {
        $args = func_get_args();
        $sql = $args[0];
        //$fetchMode = $args[1];
        $stmt = $this->prepare($sql);
        $stmt->execute();
Benjamin Morel's avatar
Benjamin Morel committed
86

87 88
        return $stmt;
    }
89 90

    /**
Benjamin Morel's avatar
Benjamin Morel committed
91
     * {@inheritdoc}
92
     */
93
    public function quote($value, $type=\PDO::PARAM_STR)
94
    {
95 96 97 98
        if (is_int($value) || is_float($value)) {
            return $value;
        }
        $value = str_replace("'", "''", $value);
Benjamin Morel's avatar
Benjamin Morel committed
99

100
        return "'" . addcslashes($value, "\000\n\r\\\032") . "'";
101
    }
102 103

    /**
Benjamin Morel's avatar
Benjamin Morel committed
104
     * {@inheritdoc}
105
     */
106 107 108 109
    public function exec($statement)
    {
        $stmt = $this->prepare($statement);
        $stmt->execute();
Benjamin Morel's avatar
Benjamin Morel committed
110

111 112
        return $stmt->rowCount();
    }
113

114
    /**
Benjamin Morel's avatar
Benjamin Morel committed
115
     * {@inheritdoc}
116
     */
117 118
    public function lastInsertId($name = null)
    {
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        if ($name === null) {
            return false;
        }

        OraclePlatform::assertValidIdentifier($name);

        $sql    = 'SELECT ' . $name . '.CURRVAL FROM DUAL';
        $stmt   = $this->query($sql);
        $result = $stmt->fetch(\PDO::FETCH_ASSOC);

        if ($result === false || !isset($result['CURRVAL'])) {
            throw new OCI8Exception("lastInsertId failed: Query was executed but no result was returned.");
        }

        return (int) $result['CURRVAL'];
134
    }
135

136
    /**
Benjamin Morel's avatar
Benjamin Morel committed
137 138 139
     * Returns the current execution mode.
     *
     * @return integer
140 141 142
     */
    public function getExecuteMode()
    {
143
        return $this->executeMode;
144 145
    }

146
    /**
Benjamin Morel's avatar
Benjamin Morel committed
147
     * {@inheritdoc}
148
     */
149 150
    public function beginTransaction()
    {
151
        $this->executeMode = OCI_NO_AUTO_COMMIT;
Benjamin Morel's avatar
Benjamin Morel committed
152

153 154
        return true;
    }
155 156

    /**
Benjamin Morel's avatar
Benjamin Morel committed
157
     * {@inheritdoc}
158
     */
159 160
    public function commit()
    {
161
        if (!oci_commit($this->dbh)) {
162 163
            throw OCI8Exception::fromErrorInfo($this->errorInfo());
        }
164
        $this->executeMode = OCI_COMMIT_ON_SUCCESS;
Benjamin Morel's avatar
Benjamin Morel committed
165

166
        return true;
167
    }
168 169

    /**
Benjamin Morel's avatar
Benjamin Morel committed
170
     * {@inheritdoc}
171
     */
172 173
    public function rollBack()
    {
174
        if (!oci_rollback($this->dbh)) {
175 176
            throw OCI8Exception::fromErrorInfo($this->errorInfo());
        }
177
        $this->executeMode = OCI_COMMIT_ON_SUCCESS;
Benjamin Morel's avatar
Benjamin Morel committed
178

179
        return true;
180
    }
181

Benjamin Morel's avatar
Benjamin Morel committed
182 183 184
    /**
     * {@inheritdoc}
     */
185 186
    public function errorCode()
    {
187
        $error = oci_error($this->dbh);
188 189 190
        if ($error !== false) {
            $error = $error['code'];
        }
Benjamin Morel's avatar
Benjamin Morel committed
191

192 193
        return $error;
    }
194

Benjamin Morel's avatar
Benjamin Morel committed
195 196 197
    /**
     * {@inheritdoc}
     */
198 199
    public function errorInfo()
    {
200
        return oci_error($this->dbh);
201
    }
202
}