Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
- Reaname fields
- Ignore ROWID field (convert to null)
  • Loading branch information
miramir committed May 5, 2015
1 parent cc05020 commit 7bad790
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
23 changes: 5 additions & 18 deletions Oci8.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,17 @@ public function __construct($dsn, $username, $password, $options = [])
*/
public function prepare($statement, $options = null)
{
// Get instance options
if ($options == null) {
$options = $this->options;
}

// Prepare the statement
$sth = @oci_parse($this->dbh, $statement);

if (!$sth) {
$e = oci_error($this->dbh);
throw new Oci8Exception($e['message']);
}

if ($options === null) {
$options = $this->options;
}

if (!is_array($options)) {
$options = [];
}
Expand All @@ -104,17 +102,6 @@ public function beginTransaction()
return true;
}

/**
* Returns true if the current process is in a transaction
*
* @deprecated Use inTransaction() instead
* @return bool
*/
public function isTransaction()
{
return $this->inTransaction();
}

/**
* Checks if inside a transaction
*
Expand Down Expand Up @@ -393,7 +380,7 @@ public function checkSequence($name)
* @param mixed $default
* @return string|null
*/
protected function parseDsn($dsn, $param, $default = null)
private function parseDsn($dsn, $param, $default = null)
{
if (preg_match('/' . $param . '=(?<param>[^;]+)/', $dsn, $mathes)) {
return $mathes['param'];
Expand Down
51 changes: 34 additions & 17 deletions Oci8Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Oci8Statement extends PDOStatement
/**
* @var \Intersvyaz\Pdo\Oci8 PDO Oci8 driver
*/
private $pdoOci8;
private $connection;
/**
* @var boolean flag to convert LOB to string or not
*/
Expand Down Expand Up @@ -67,11 +67,11 @@ class Oci8Statement extends PDOStatement
* Constructor
*
* @param resource $sth Statement handle created with oci_parse()
* @param Oci8 $pdoOci8 The Pdo_Oci8 object for this statement
* @param Oci8 $connection The Pdo_Oci8 object for this statement
* @param array $options Options for the statement handle
* @throws Oci8Exception
*/
public function __construct($sth, Oci8 $pdoOci8, array $options = [])
public function __construct($sth, Oci8 $connection, array $options = [])
{

if (strtolower(get_resource_type($sth)) !== 'oci8 statement') {
Expand All @@ -81,7 +81,7 @@ public function __construct($sth, Oci8 $pdoOci8, array $options = [])
}

$this->sth = $sth;
$this->pdoOci8 = $pdoOci8;
$this->connection = $connection;
$this->options = $options;
}

Expand All @@ -96,7 +96,7 @@ public function __construct($sth, Oci8 $pdoOci8, array $options = [])
public function execute($inputParams = null)
{
$mode = OCI_COMMIT_ON_SUCCESS;
if ($this->pdoOci8->inTransaction() || count($this->saveLobs) > 0 || count($this->writeLobs) > 0) {
if ($this->connection->inTransaction() || count($this->saveLobs) > 0 || count($this->writeLobs) > 0) {
$mode = OCI_DEFAULT;
}

Expand Down Expand Up @@ -127,8 +127,8 @@ public function execute($inputParams = null)
throw new Oci8Exception($e['message'], $e['code']);
}

if (!$this->pdoOci8->inTransaction() && (count($this->saveLobs) > 0 || count($this->writeLobs) > 0)) {
return $this->pdoOci8->commit();
if (!$this->connection->inTransaction() && (count($this->saveLobs) > 0 || count($this->writeLobs) > 0)) {
return $this->connection->commit();
}

return $result;
Expand Down Expand Up @@ -245,6 +245,13 @@ public function fetchColumn($colNumber = 0)
{
$rs = oci_fetch_array($this->sth, OCI_NUM + OCI_RETURN_NULLS + ($this->returnLobs ? OCI_RETURN_LOBS : 0));
if (is_array($rs) && array_key_exists((int)$colNumber, $rs)) {
/**
* @todo KLUDGE No read column with type ROWID
*/
if (oci_field_type($this->sth, (int)$colNumber) === 'ROWID') {
return null;
}

return $rs[(int)$colNumber];
}

Expand All @@ -267,7 +274,7 @@ public function fetchColumn($colNumber = 0)
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
{
if ($fetchMode !== null) {
$this->setFetchMode($fetchMode, $fetchArgument, $ctorArgs?:[]);
$this->setFetchMode($fetchMode, $fetchArgument, $ctorArgs ?: []);
}

$results = [];
Expand All @@ -287,7 +294,7 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n
*/
public function fetchObject($className = null, $ctorArgs = null)
{
$className = $className?:$this->fetchClassName;
$className = $className ?: $this->fetchClassName;

$toLowercase = ($this->getAttribute(PDO::ATTR_CASE) === PDO::CASE_LOWER);
$rs = $this->fetchArray(OCI_ASSOC + OCI_RETURN_NULLS, $toLowercase);
Expand Down Expand Up @@ -357,11 +364,11 @@ public function bindParam(
case Oci8::PARAM_CLOB:
$oci_type = $dataType;

$this->lobsValue[$parameter] = $variable;
$variable = $this->pdoOci8->getNewDescriptor(OCI_D_LOB);
$this->lobsValue[$parameter] = &$variable;
$variable = $this->connection->getNewDescriptor(OCI_D_LOB);

if (in_array(Oci8::LOB_SQL, $options)) {
$this->saveLobs[$parameter] = $variable;
$this->saveLobs[$parameter] = &$variable;
} elseif (in_array(Oci8::LOB_PL_SQL, $options)) {
$this->writeLobs[$parameter] = ['type' => $oci_type, 'object' => $variable];
}
Expand All @@ -371,17 +378,17 @@ public function bindParam(
$oci_type = OCI_B_CURSOR;

// Result sets require a cursor
$variable = $this->pdoOci8->getNewCursor();
$variable = $this->connection->getNewCursor();
break;

case SQLT_NTY:
$oci_type = SQLT_NTY;

$schema = isset($options['schema']) ? $options['schema'] : '';
$type_name = isset($options['type_name']) ? $options['type_name'] : '';
$schema = array_key_exists('schema', $options) ? $options['schema'] : '';
$type_name = array_key_exists('type_name', $options) ? $options['type_name'] : '';

// set params required to use custom type.
$variable = $this->pdoOci8->getNewCollection($type_name, $schema);
$variable = $this->connection->getNewCollection($type_name, $schema);
break;

default:
Expand Down Expand Up @@ -490,7 +497,7 @@ public function setAttribute($attribute, $value)
*/
public function getAttribute($attribute)
{
if (isset($this->options[$attribute])) {
if (array_key_exists($attribute, $this->options)) {
return $this->options[$attribute];
}

Expand Down Expand Up @@ -548,6 +555,7 @@ public function getColumnMeta($column)
}

/**
* Fetch row from db
* @param integer $mode
* @param bool $keyToLowercase
* @return array|bool
Expand All @@ -562,6 +570,15 @@ private function fetchArray($mode, $keyToLowercase)
$rs = array_change_key_case($rs);
}

/**
* @todo KLUDGE No read column with type ROWID
*/
foreach ($rs as $name => $value) {
if (oci_field_type($this->sth, $name) === 'ROWID') {
$rs[$name] = null;
}
}

return $rs;
}

Expand Down

0 comments on commit 7bad790

Please sign in to comment.