|
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 |
use \PDO; |
|
|
23 |
|
|
|
24 |
/** |
|
|
25 |
* The OCI8 implementation of the Statement interface. |
|
|
26 |
* |
|
|
27 |
* @since 2.0 |
|
|
28 |
* @author Roman Borschel <roman@code-factory.org> |
|
|
29 |
*/ |
|
|
30 |
class OCI8Statement implements \Doctrine\DBAL\Driver\Statement |
|
|
31 |
{ |
|
|
32 |
/** Statement handle. */ |
|
|
33 |
private $_sth; |
|
|
34 |
private $_executeMode; |
|
|
35 |
private static $_PARAM = ':param'; |
|
|
36 |
private static $fetchStyleMap = array( |
|
|
37 |
PDO::FETCH_BOTH => OCI_BOTH, |
|
|
38 |
PDO::FETCH_ASSOC => OCI_ASSOC, |
|
|
39 |
PDO::FETCH_NUM => OCI_NUM |
|
|
40 |
); |
|
|
41 |
private $_paramMap = array(); |
|
|
42 |
|
|
|
43 |
/** |
|
|
44 |
* Creates a new OCI8Statement that uses the given connection handle and SQL statement. |
|
|
45 |
* |
|
|
46 |
* @param resource $dbh The connection handle. |
|
|
47 |
* @param string $statement The SQL statement. |
|
|
48 |
*/ |
|
|
49 |
public function __construct($dbh, $statement, $executeMode) |
|
|
50 |
{ |
|
|
51 |
list($statement, $paramMap) = self::convertPositionalToNamedPlaceholders($statement); |
|
|
52 |
$this->_sth = oci_parse($dbh, $statement); |
|
|
53 |
$this->_paramMap = $paramMap; |
|
|
54 |
$this->_executeMode = $executeMode; |
|
|
55 |
} |
|
|
56 |
|
|
|
57 |
/** |
|
|
58 |
* Convert positional (?) into named placeholders (:param<num>) |
|
|
59 |
* |
|
|
60 |
* Oracle does not support positional parameters, hence this method converts all |
|
|
61 |
* positional parameters into artificially named parameters. Note that this conversion |
|
|
62 |
* is not perfect. All question marks (?) in the original statement are treated as |
|
|
63 |
* placeholders and converted to a named parameter. |
|
|
64 |
* |
|
|
65 |
* The algorithm uses a state machine with two possible states: InLiteral and NotInLiteral. |
|
|
66 |
* Question marks inside literal strings are therefore handled correctly by this method. |
|
|
67 |
* This comes at a cost, the whole sql statement has to be looped over. |
|
|
68 |
* |
|
|
69 |
* @todo extract into utility class in Doctrine\DBAL\Util namespace |
|
|
70 |
* @todo review and test for lost spaces. we experienced missing spaces with oci8 in some sql statements. |
|
|
71 |
* @param string $statement The SQL statement to convert. |
|
|
72 |
* @return string |
|
|
73 |
*/ |
|
|
74 |
static public function convertPositionalToNamedPlaceholders($statement) |
|
|
75 |
{ |
|
|
76 |
$count = 1; |
|
|
77 |
$inLiteral = false; // a valid query never starts with quotes |
|
|
78 |
$stmtLen = strlen($statement); |
|
|
79 |
$paramMap = array(); |
|
|
80 |
for ($i = 0; $i < $stmtLen; $i++) { |
|
|
81 |
if ($statement[$i] == '?' && !$inLiteral) { |
|
|
82 |
// real positional parameter detected |
|
|
83 |
$paramMap[$count] = ":param$count"; |
|
|
84 |
$len = strlen($paramMap[$count]); |
|
|
85 |
$statement = substr_replace($statement, ":param$count", $i, 1); |
|
|
86 |
$i += $len-1; // jump ahead |
|
|
87 |
$stmtLen = strlen($statement); // adjust statement length |
|
|
88 |
++$count; |
|
|
89 |
} else if ($statement[$i] == "'" || $statement[$i] == '"') { |
|
|
90 |
$inLiteral = ! $inLiteral; // switch state! |
|
|
91 |
} |
|
|
92 |
} |
|
|
93 |
|
|
|
94 |
return array($statement, $paramMap); |
|
|
95 |
} |
|
|
96 |
|
|
|
97 |
/** |
|
|
98 |
* {@inheritdoc} |
|
|
99 |
*/ |
|
|
100 |
public function bindValue($param, $value, $type = null) |
|
|
101 |
{ |
|
|
102 |
return $this->bindParam($param, $value, $type); |
|
|
103 |
} |
|
|
104 |
|
|
|
105 |
/** |
|
|
106 |
* {@inheritdoc} |
|
|
107 |
*/ |
|
|
108 |
public function bindParam($column, &$variable, $type = null) |
|
|
109 |
{ |
|
|
110 |
$column = isset($this->_paramMap[$column]) ? $this->_paramMap[$column] : $column; |
|
|
111 |
|
|
|
112 |
return oci_bind_by_name($this->_sth, $column, $variable); |
|
|
113 |
} |
|
|
114 |
|
|
|
115 |
/** |
|
|
116 |
* Closes the cursor, enabling the statement to be executed again. |
|
|
117 |
* |
|
|
118 |
* @return boolean Returns TRUE on success or FALSE on failure. |
|
|
119 |
*/ |
|
|
120 |
public function closeCursor() |
|
|
121 |
{ |
|
|
122 |
return oci_free_statement($this->_sth); |
|
|
123 |
} |
|
|
124 |
|
|
|
125 |
/** |
|
|
126 |
* {@inheritdoc} |
|
|
127 |
*/ |
|
|
128 |
public function columnCount() |
|
|
129 |
{ |
|
|
130 |
return oci_num_fields($this->_sth); |
|
|
131 |
} |
|
|
132 |
|
|
|
133 |
/** |
|
|
134 |
* {@inheritdoc} |
|
|
135 |
*/ |
|
|
136 |
public function errorCode() |
|
|
137 |
{ |
|
|
138 |
$error = oci_error($this->_sth); |
|
|
139 |
if ($error !== false) { |
|
|
140 |
$error = $error['code']; |
|
|
141 |
} |
|
|
142 |
return $error; |
|
|
143 |
} |
|
|
144 |
|
|
|
145 |
/** |
|
|
146 |
* {@inheritdoc} |
|
|
147 |
*/ |
|
|
148 |
public function errorInfo() |
|
|
149 |
{ |
|
|
150 |
return oci_error($this->_sth); |
|
|
151 |
} |
|
|
152 |
|
|
|
153 |
/** |
|
|
154 |
* {@inheritdoc} |
|
|
155 |
*/ |
|
|
156 |
public function execute($params = null) |
|
|
157 |
{ |
|
|
158 |
if ($params) { |
|
|
159 |
$hasZeroIndex = isset($params[0]); |
|
|
160 |
foreach ($params as $key => $val) { |
|
|
161 |
if ($hasZeroIndex && is_numeric($key)) { |
|
|
162 |
$this->bindValue($key + 1, $val); |
|
|
163 |
} else { |
|
|
164 |
$this->bindValue($key, $val); |
|
|
165 |
} |
|
|
166 |
} |
|
|
167 |
} |
|
|
168 |
|
|
|
169 |
$ret = @oci_execute($this->_sth, $this->_executeMode); |
|
|
170 |
if ( ! $ret) { |
|
|
171 |
throw OCI8Exception::fromErrorInfo($this->errorInfo()); |
|
|
172 |
} |
|
|
173 |
return $ret; |
|
|
174 |
} |
|
|
175 |
|
|
|
176 |
/** |
|
|
177 |
* {@inheritdoc} |
|
|
178 |
*/ |
|
|
179 |
public function fetch($fetchStyle = PDO::FETCH_BOTH) |
|
|
180 |
{ |
|
|
181 |
if ( ! isset(self::$fetchStyleMap[$fetchStyle])) { |
|
|
182 |
throw new \InvalidArgumentException("Invalid fetch style: " . $fetchStyle); |
|
|
183 |
} |
|
|
184 |
|
|
|
185 |
return oci_fetch_array($this->_sth, self::$fetchStyleMap[$fetchStyle] | OCI_RETURN_NULLS | OCI_RETURN_LOBS); |
|
|
186 |
} |
|
|
187 |
|
|
|
188 |
/** |
|
|
189 |
* {@inheritdoc} |
|
|
190 |
*/ |
|
|
191 |
public function fetchAll($fetchStyle = PDO::FETCH_BOTH) |
|
|
192 |
{ |
|
|
193 |
if ( ! isset(self::$fetchStyleMap[$fetchStyle])) { |
|
|
194 |
throw new \InvalidArgumentException("Invalid fetch style: " . $fetchStyle); |
|
|
195 |
} |
|
|
196 |
|
|
|
197 |
$result = array(); |
|
|
198 |
oci_fetch_all($this->_sth, $result, 0, -1, |
|
|
199 |
self::$fetchStyleMap[$fetchStyle] | OCI_RETURN_NULLS | OCI_FETCHSTATEMENT_BY_ROW | OCI_RETURN_LOBS); |
|
|
200 |
|
|
|
201 |
return $result; |
|
|
202 |
} |
|
|
203 |
|
|
|
204 |
/** |
|
|
205 |
* {@inheritdoc} |
|
|
206 |
*/ |
|
|
207 |
public function fetchColumn($columnIndex = 0) |
|
|
208 |
{ |
|
|
209 |
$row = oci_fetch_array($this->_sth, OCI_NUM | OCI_RETURN_NULLS | OCI_RETURN_LOBS); |
|
|
210 |
return $row[$columnIndex]; |
|
|
211 |
} |
|
|
212 |
|
|
|
213 |
/** |
|
|
214 |
* {@inheritdoc} |
|
|
215 |
*/ |
|
|
216 |
public function rowCount() |
|
|
217 |
{ |
|
|
218 |
return oci_num_rows($this->_sth); |
|
|
219 |
} |
|
|
220 |
} |