source: pro-violet-viettel/sourcecode/application/libraries/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php @ 353

Last change on this file since 353 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 4.3 KB
Line 
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 LGPL. For more information, see
17 * <http://www.doctrine-project.org>.
18 */
19
20namespace Doctrine\DBAL\Driver\OCI8;
21
22/**
23 * OCI8 implementation of the Connection interface.
24 *
25 * @since 2.0
26 */
27class OCI8Connection implements \Doctrine\DBAL\Driver\Connection
28{
29    protected $_dbh;
30
31    protected $_executeMode = OCI_COMMIT_ON_SUCCESS;
32
33    /**
34     * Create a Connection to an Oracle Database using oci8 extension.
35     *
36     * @param string $username
37     * @param string $password
38     * @param string $db
39     */
40    public function __construct($username, $password, $db, $charset = null, $sessionMode = OCI_DEFAULT)
41    {
42        if (!defined('OCI_NO_AUTO_COMMIT')) {
43            define('OCI_NO_AUTO_COMMIT', 0);
44        }
45
46        $this->_dbh = @oci_connect($username, $password, $db, $charset, $sessionMode);
47        if (!$this->_dbh) {
48            throw OCI8Exception::fromErrorInfo(oci_error());
49        }
50    }
51
52    /**
53     * Create a non-executed prepared statement.
54     *
55     * @param  string $prepareString
56     * @return OCI8Statement
57     */
58    public function prepare($prepareString)
59    {
60        return new OCI8Statement($this->_dbh, $prepareString, $this->_executeMode);
61    }
62
63    /**
64     * @param string $sql
65     * @return OCI8Statement
66     */
67    public function query()
68    {
69        $args = func_get_args();
70        $sql = $args[0];
71        //$fetchMode = $args[1];
72        $stmt = $this->prepare($sql);
73        $stmt->execute();
74        return $stmt;
75    }
76
77    /**
78     * Quote input value.
79     *
80     * @param mixed $input
81     * @param int $type PDO::PARAM*
82     * @return mixed
83     */
84    public function quote($value, $type=\PDO::PARAM_STR)
85    {
86        if (is_int($value) || is_float($value)) {
87            return $value;
88        }
89        $value = str_replace("'", "''", $value);
90        return "'" . addcslashes($value, "\000\n\r\\\032") . "'";
91    }
92
93    /**
94     *
95     * @param  string $statement
96     * @return int
97     */
98    public function exec($statement)
99    {
100        $stmt = $this->prepare($statement);
101        $stmt->execute();
102        return $stmt->rowCount();
103    }
104
105    public function lastInsertId($name = null)
106    {
107        //TODO: throw exception or support sequences?
108    }
109
110    /**
111     * Start a transactiom
112     *
113     * Oracle has to explicitly set the autocommit mode off. That means
114     * after connection, a commit or rollback there is always automatically
115     * opened a new transaction.
116     *
117     * @return bool
118     */
119    public function beginTransaction()
120    {
121        $this->_executeMode = OCI_NO_AUTO_COMMIT;
122        return true;
123    }
124
125    /**
126     * @throws OCI8Exception
127     * @return bool
128     */
129    public function commit()
130    {
131        if (!oci_commit($this->_dbh)) {
132            throw OCI8Exception::fromErrorInfo($this->errorInfo());
133        }
134        $this->_executeMode = OCI_COMMIT_ON_SUCCESS;
135        return true;
136    }
137
138    /**
139     * @throws OCI8Exception
140     * @return bool
141     */
142    public function rollBack()
143    {
144        if (!oci_rollback($this->_dbh)) {
145            throw OCI8Exception::fromErrorInfo($this->errorInfo());
146        }
147        $this->_executeMode = OCI_COMMIT_ON_SUCCESS;
148        return true;
149    }
150
151    public function errorCode()
152    {
153        $error = oci_error($this->_dbh);
154        if ($error !== false) {
155            $error = $error['code'];
156        }
157        return $error;
158    }
159
160    public function errorInfo()
161    {
162        return oci_error($this->_dbh);
163    }
164}
Note: See TracBrowser for help on using the repository browser.