123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
-
-
- namespace Doctrine\DBAL\Driver\OCI8;
-
- use \PDO;
-
-
- class OCI8Statement implements \Doctrine\DBAL\Driver\Statement
- {
-
- private $_sth;
- private $_executeMode;
- private static $_PARAM = ':param';
- private static $fetchStyleMap = array(
- PDO::FETCH_BOTH => OCI_BOTH,
- PDO::FETCH_ASSOC => OCI_ASSOC,
- PDO::FETCH_NUM => OCI_NUM
- );
- private $_paramMap = array();
-
-
-
- public function __construct($dbh, $statement, $executeMode)
- {
- list($statement, $paramMap) = self::convertPositionalToNamedPlaceholders($statement);
- $this->_sth = oci_parse($dbh, $statement);
- $this->_paramMap = $paramMap;
- $this->_executeMode = $executeMode;
- }
-
-
-
- static public function convertPositionalToNamedPlaceholders($statement)
- {
- $count = 1;
- $inLiteral = false;
- $stmtLen = strlen($statement);
- $paramMap = array();
- for ($i = 0; $i < $stmtLen; $i++) {
- if ($statement[$i] == '?' && !$inLiteral) {
-
- $paramMap[$count] = ":param$count";
- $len = strlen($paramMap[$count]);
- $statement = substr_replace($statement, ":param$count", $i, 1);
- $i += $len-1;
- $stmtLen = strlen($statement);
- ++$count;
- } else if ($statement[$i] == "'" || $statement[$i] == '"') {
- $inLiteral = ! $inLiteral;
- }
- }
-
- return array($statement, $paramMap);
- }
-
-
-
- public function bindValue($param, $value, $type = null)
- {
- return $this->bindParam($param, $value, $type);
- }
-
-
-
- public function bindParam($column, &$variable, $type = null)
- {
- $column = isset($this->_paramMap[$column]) ? $this->_paramMap[$column] : $column;
-
- return oci_bind_by_name($this->_sth, $column, $variable);
- }
-
-
-
- public function closeCursor()
- {
- return oci_free_statement($this->_sth);
- }
-
-
-
- public function columnCount()
- {
- return oci_num_fields($this->_sth);
- }
-
-
-
- public function errorCode()
- {
- $error = oci_error($this->_sth);
- if ($error !== false) {
- $error = $error['code'];
- }
- return $error;
- }
-
-
-
- public function errorInfo()
- {
- return oci_error($this->_sth);
- }
-
-
-
- public function execute($params = null)
- {
- if ($params) {
- $hasZeroIndex = array_key_exists(0, $params);
- foreach ($params as $key => $val) {
- if ($hasZeroIndex && is_numeric($key)) {
- $this->bindValue($key + 1, $val);
- } else {
- $this->bindValue($key, $val);
- }
- }
- }
-
- $ret = @oci_execute($this->_sth, $this->_executeMode);
- if ( ! $ret) {
- throw OCI8Exception::fromErrorInfo($this->errorInfo());
- }
- return $ret;
- }
-
-
-
- public function fetch($fetchStyle = PDO::FETCH_BOTH)
- {
- if ( ! isset(self::$fetchStyleMap[$fetchStyle])) {
- throw new \InvalidArgumentException("Invalid fetch style: " . $fetchStyle);
- }
-
- return oci_fetch_array($this->_sth, self::$fetchStyleMap[$fetchStyle] | OCI_RETURN_NULLS | OCI_RETURN_LOBS);
- }
-
-
-
- public function fetchAll($fetchStyle = PDO::FETCH_BOTH)
- {
- if ( ! isset(self::$fetchStyleMap[$fetchStyle])) {
- throw new \InvalidArgumentException("Invalid fetch style: " . $fetchStyle);
- }
-
- $result = array();
- oci_fetch_all($this->_sth, $result, 0, -1,
- self::$fetchStyleMap[$fetchStyle] | OCI_RETURN_NULLS | OCI_FETCHSTATEMENT_BY_ROW | OCI_RETURN_LOBS);
-
- return $result;
- }
-
-
-
- public function fetchColumn($columnIndex = 0)
- {
- $row = oci_fetch_array($this->_sth, OCI_NUM | OCI_RETURN_NULLS | OCI_RETURN_LOBS);
- return $row[$columnIndex];
- }
-
-
-
- public function rowCount()
- {
- return oci_num_rows($this->_sth);
- }
- }
|