Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Driver / OCI8 / OCI8Connection.php
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\Platforms\OraclePlatform;
23
24 /**
25  * OCI8 implementation of the Connection interface.
26  *
27  * @since 2.0
28  */
29 class OCI8Connection implements \Doctrine\DBAL\Driver\Connection
30 {
31     /**
32      * @var resource
33      */
34     protected $dbh;
35
36     /**
37      * @var int
38      */
39     protected $executeMode = OCI_COMMIT_ON_SUCCESS;
40
41     /**
42      * Create a Connection to an Oracle Database using oci8 extension.
43      *
44      * @param string $username
45      * @param string $password
46      * @param string $db
47      */
48     public function __construct($username, $password, $db, $charset = null, $sessionMode = OCI_DEFAULT, $persistent = false)
49     {
50         if (!defined('OCI_NO_AUTO_COMMIT')) {
51             define('OCI_NO_AUTO_COMMIT', 0);
52         }
53
54         $this->dbh = $persistent
55             ? @oci_pconnect($username, $password, $db, $charset, $sessionMode)
56             : @oci_connect($username, $password, $db, $charset, $sessionMode);
57
58         if ( ! $this->dbh) {
59             throw OCI8Exception::fromErrorInfo(oci_error());
60         }
61     }
62
63     /**
64      * Create a non-executed prepared statement.
65      *
66      * @param  string $prepareString
67      * @return OCI8Statement
68      */
69     public function prepare($prepareString)
70     {
71         return new OCI8Statement($this->dbh, $prepareString, $this);
72     }
73
74     /**
75      * @param string $sql
76      * @return OCI8Statement
77      */
78     public function query()
79     {
80         $args = func_get_args();
81         $sql = $args[0];
82         //$fetchMode = $args[1];
83         $stmt = $this->prepare($sql);
84         $stmt->execute();
85         return $stmt;
86     }
87
88     /**
89      * Quote input value.
90      *
91      * @param mixed $input
92      * @param int $type PDO::PARAM*
93      * @return mixed
94      */
95     public function quote($value, $type=\PDO::PARAM_STR)
96     {
97         if (is_int($value) || is_float($value)) {
98             return $value;
99         }
100         $value = str_replace("'", "''", $value);
101         return "'" . addcslashes($value, "\000\n\r\\\032") . "'";
102     }
103
104     /**
105      *
106      * @param  string $statement
107      * @return int
108      */
109     public function exec($statement)
110     {
111         $stmt = $this->prepare($statement);
112         $stmt->execute();
113         return $stmt->rowCount();
114     }
115
116     /**
117      * {@inheritDoc}
118      */
119     public function lastInsertId($name = null)
120     {
121         if ($name === null) {
122             return false;
123         }
124
125         OraclePlatform::assertValidIdentifier($name);
126
127         $sql    = 'SELECT ' . $name . '.CURRVAL FROM DUAL';
128         $stmt   = $this->query($sql);
129         $result = $stmt->fetch(\PDO::FETCH_ASSOC);
130
131         if ($result === false || !isset($result['CURRVAL'])) {
132             throw new OCI8Exception("lastInsertId failed: Query was executed but no result was returned.");
133         }
134
135         return (int) $result['CURRVAL'];
136     }
137
138     /**
139      * Return the current execution mode.
140      */
141     public function getExecuteMode()
142     {
143         return $this->executeMode;
144     }
145
146     /**
147      * Start a transactiom
148      *
149      * Oracle has to explicitly set the autocommit mode off. That means
150      * after connection, a commit or rollback there is always automatically
151      * opened a new transaction.
152      *
153      * @return bool
154      */
155     public function beginTransaction()
156     {
157         $this->executeMode = OCI_NO_AUTO_COMMIT;
158         return true;
159     }
160
161     /**
162      * @throws OCI8Exception
163      * @return bool
164      */
165     public function commit()
166     {
167         if (!oci_commit($this->dbh)) {
168             throw OCI8Exception::fromErrorInfo($this->errorInfo());
169         }
170         $this->executeMode = OCI_COMMIT_ON_SUCCESS;
171         return true;
172     }
173
174     /**
175      * @throws OCI8Exception
176      * @return bool
177      */
178     public function rollBack()
179     {
180         if (!oci_rollback($this->dbh)) {
181             throw OCI8Exception::fromErrorInfo($this->errorInfo());
182         }
183         $this->executeMode = OCI_COMMIT_ON_SUCCESS;
184         return true;
185     }
186
187     public function errorCode()
188     {
189         $error = oci_error($this->dbh);
190         if ($error !== false) {
191             $error = $error['code'];
192         }
193         return $error;
194     }
195
196     public function errorInfo()
197     {
198         return oci_error($this->dbh);
199     }
200 }