|
0
|
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 |
|
|
|
20 |
namespace Doctrine\DBAL\Driver\OCI8; |
|
|
21 |
|
|
|
22 |
/** |
|
|
23 |
* OCI8 implementation of the Connection interface. |
|
|
24 |
* |
|
|
25 |
* @since 2.0 |
|
|
26 |
*/ |
|
|
27 |
class OCI8Connection implements \Doctrine\DBAL\Driver\Connection |
|
|
28 |
{ |
|
|
29 |
private $_dbh; |
|
|
30 |
|
|
|
31 |
private $_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($input, $type=\PDO::PARAM_STR) |
|
|
85 |
{ |
|
|
86 |
return is_numeric($input) ? $input : "'$input'"; |
|
|
87 |
} |
|
|
88 |
|
|
|
89 |
/** |
|
|
90 |
* |
|
|
91 |
* @param string $statement |
|
|
92 |
* @return int |
|
|
93 |
*/ |
|
|
94 |
public function exec($statement) |
|
|
95 |
{ |
|
|
96 |
$stmt = $this->prepare($statement); |
|
|
97 |
$stmt->execute(); |
|
|
98 |
return $stmt->rowCount(); |
|
|
99 |
} |
|
|
100 |
|
|
|
101 |
public function lastInsertId($name = null) |
|
|
102 |
{ |
|
|
103 |
//TODO: throw exception or support sequences? |
|
|
104 |
} |
|
|
105 |
|
|
|
106 |
/** |
|
|
107 |
* Start a transactiom |
|
|
108 |
* |
|
|
109 |
* Oracle has to explicitly set the autocommit mode off. That means |
|
|
110 |
* after connection, a commit or rollback there is always automatically |
|
|
111 |
* opened a new transaction. |
|
|
112 |
* |
|
|
113 |
* @return bool |
|
|
114 |
*/ |
|
|
115 |
public function beginTransaction() |
|
|
116 |
{ |
|
|
117 |
$this->_executeMode = OCI_NO_AUTO_COMMIT; |
|
|
118 |
return true; |
|
|
119 |
} |
|
|
120 |
|
|
|
121 |
/** |
|
|
122 |
* @throws OCI8Exception |
|
|
123 |
* @return bool |
|
|
124 |
*/ |
|
|
125 |
public function commit() |
|
|
126 |
{ |
|
|
127 |
if (!oci_commit($this->_dbh)) { |
|
|
128 |
throw OCI8Exception::fromErrorInfo($this->errorInfo()); |
|
|
129 |
} |
|
|
130 |
$this->_executeMode = OCI_COMMIT_ON_SUCCESS; |
|
|
131 |
return true; |
|
|
132 |
} |
|
|
133 |
|
|
|
134 |
/** |
|
|
135 |
* @throws OCI8Exception |
|
|
136 |
* @return bool |
|
|
137 |
*/ |
|
|
138 |
public function rollBack() |
|
|
139 |
{ |
|
|
140 |
if (!oci_rollback($this->_dbh)) { |
|
|
141 |
throw OCI8Exception::fromErrorInfo($this->errorInfo()); |
|
|
142 |
} |
|
|
143 |
$this->_executeMode = OCI_COMMIT_ON_SUCCESS; |
|
|
144 |
return true; |
|
|
145 |
} |
|
|
146 |
|
|
|
147 |
public function errorCode() |
|
|
148 |
{ |
|
|
149 |
$error = oci_error($this->_dbh); |
|
|
150 |
if ($error !== false) { |
|
|
151 |
$error = $error['code']; |
|
|
152 |
} |
|
|
153 |
return $error; |
|
|
154 |
} |
|
|
155 |
|
|
|
156 |
public function errorInfo() |
|
|
157 |
{ |
|
|
158 |
return oci_error($this->_dbh); |
|
|
159 |
} |
|
|
160 |
} |