Skip to content

Commit

Permalink
first draft shortcut function for procedure fetch all
Browse files Browse the repository at this point in the history
  • Loading branch information
mstaack committed Aug 24, 2016
1 parent 1617c9a commit abdf6fd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ build
composer.lock
docs
vendor
.idea
61 changes: 50 additions & 11 deletions src/Oci8/Oci8Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ class Oci8Connection extends Connection

/**
* @param PDO|\Closure $pdo
* @param string $database
* @param string $tablePrefix
* @param array $config
* @param string $database
* @param string $tablePrefix
* @param array $config
*/
public function __construct($pdo, $database = '', $tablePrefix = '', array $config = [])
{
parent::__construct($pdo, $database, $tablePrefix, $config);
$this->sequence = new Sequence($this);
$this->trigger = new Trigger($this);
$this->trigger = new Trigger($this);
}

/**
Expand All @@ -64,7 +64,7 @@ public function getSchema()
public function setSchema($schema)
{
$this->schema = $schema;
$sessionVars = [
$sessionVars = [
'CURRENT_SCHEMA' => $schema,
];

Expand Down Expand Up @@ -212,17 +212,17 @@ protected function getDoctrineDriver()
* 'bye'], PDO::PARAM_LOB)
*
* @author Tylerian - [email protected]
* @param string $sql (mixed)
* @param array $bindings (kvp array)
* @param int $returnType (PDO::PARAM_*)
* @param string $sql (mixed)
* @param array $bindings (kvp array)
* @param int $returnType (PDO::PARAM_*)
* @return mixed $returnType
*/
public function executeFunction($sql, array $bindings = [], $returnType = PDO::PARAM_STR)
{
$query = $this->getPdo()->prepare('begin :result := ' . $sql . '; end;');

foreach ($bindings as $key => &$value) {
if (! preg_match('/^:(.*)$/i', $key)) {
if (!preg_match('/^:(.*)$/i', $key)) {
$key = ':' . $key;
}

Expand All @@ -235,11 +235,50 @@ public function executeFunction($sql, array $bindings = [], $returnType = PDO::P
return $result;
}

/**
* Execute a PL/SQL Procedure and return its result.
* Usage: DB::executeProcedure($procedureName, $bindings)
*
* $bindings looks like:
* $bindings = [
* 'p_userid' => [
* 'value' => $id,
* 'type' => PDO::PARAM_INT,
* ],
*
* @param $procedureName
* @param $bindings
* @return array
*/
private function executeProcedure($procedureName, $bindings)
{
$pdo = DB::getPdo();

$command = sprintf('begin %s(:%s, :cursor); end;', $procedureName, implode(', :', array_keys($bindings)));

$stmt = $pdo->prepare($command);

foreach ($bindings as $bindingName => $bindingOption) {
$stmt->bindParam(':' . $bindingName, $bindingOption['value'], $bindingOption['type']);
}

$cursor = null;

$stmt->bindParam(':cursor', $cursor, PDO::PARAM_STMT);
$stmt->execute();

oci_execute($cursor, OCI_DEFAULT);
oci_fetch_all($cursor, $array, 0, -1, OCI_FETCHSTATEMENT_BY_ROW + OCI_ASSOC);
oci_free_cursor($cursor);

return $array;
}

/**
* Bind values to their parameters in the given statement.
*
* @param \PDOStatement $statement
* @param array $bindings
* @param array $bindings
* @return void
*/
public function bindValues($statement, $bindings)
Expand All @@ -248,7 +287,7 @@ public function bindValues($statement, $bindings)
$statement->bindParam(
$key,
$bindings[$key],
! is_string($value) && is_numeric($value) ? PDO::PARAM_INT : PDO::PARAM_STR
!is_string($value) && is_numeric($value) ? PDO::PARAM_INT : PDO::PARAM_STR
);
}
}
Expand Down

0 comments on commit abdf6fd

Please sign in to comment.