Skip to content

Commit

Permalink
Merge pull request #200 from yajra/added-procedure-shortcut
Browse files Browse the repository at this point in the history
Implement executeProcedure method.
Fix ORA-01790: expression must have same datatype as corresponding expression.
  • Loading branch information
yajra committed Aug 30, 2016
2 parents 1617c9a + 5bf7e68 commit 2bdb453
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,4 +1,4 @@
build
composer.lock
docs
vendor
vendor
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -13,7 +13,7 @@
"php": ">=5.6.4",
"illuminate/support": "5.3.*",
"illuminate/database": "5.3.*",
"yajra/laravel-pdo-via-oci8": "1.*"
"yajra/laravel-pdo-via-oci8": "^1.2"
},
"require-dev": {
"mockery/mockery": "~0.9.4",
Expand Down
66 changes: 51 additions & 15 deletions src/Oci8/Oci8Connection.php
Expand Up @@ -14,6 +14,7 @@
use Yajra\Oci8\Schema\OracleBuilder as SchemaBuilder;
use Yajra\Oci8\Schema\Sequence;
use Yajra\Oci8\Schema\Trigger;
use Yajra\Pdo\Oci8\Statement;

class Oci8Connection extends Connection
{
Expand Down Expand Up @@ -88,7 +89,7 @@ public function setSessionVars(array $sessionVars)
}
}
if ($vars) {
$sql = "ALTER SESSION SET " . implode(" ", $vars);
$sql = 'ALTER SESSION SET ' . implode(' ', $vars);
$this->statement($sql);
}

Expand Down Expand Up @@ -154,7 +155,7 @@ public function getSchemaBuilder()
/**
* Begin a fluent query against a database table.
*
* @param string $table
* @param string $table
* @return \Yajra\Oci8\Query\OracleBuilder
*/
public function table($table)
Expand Down Expand Up @@ -203,13 +204,13 @@ public function getDoctrineConnection()
*/
protected function getDoctrineDriver()
{
return new DoctrineDriver;
return new DoctrineDriver();
}

/**
* Execute a PL/SQL Function and return its value.
* Usage: DB::executeFunction('function_name(:binding_1,:binding_n)', [':binding_1' => 'hi', ':binding_n' =>
* 'bye'], PDO::PARAM_LOB)
* 'bye'], PDO::PARAM_LOB).
*
* @author Tylerian - jairo.eog@outlook.com
* @param string $sql (mixed)
Expand All @@ -235,21 +236,56 @@ 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' => $id
* ];
*
* @param string $procedureName
* @param array $bindings
* @param mixed $returnType
* @return array
*/
public function executeProcedure($procedureName, $bindings, $returnType = PDO::PARAM_STMT)
{
$command = sprintf('begin %s(:%s, :cursor); end;', $procedureName, implode(', :', array_keys($bindings)));

$stmt = $this->getPdo()->prepare($command);

foreach ($bindings as $bindingName => &$bindingValue) {
$stmt->bindParam(':' . $bindingName, $bindingValue);
}

$cursor = null;

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

if ($returnType === PDO::PARAM_STMT) {
$statement = new Statement($cursor, $this->getPdo(), $this->getPdo()->getOptions());
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$statement->closeCursor();

return $results;
}

return $cursor;
}

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

Expand All @@ -260,7 +296,7 @@ public function bindValues($statement, $bindings)
*/
protected function getDefaultQueryGrammar()
{
return $this->withTablePrefix(new QueryGrammar);
return $this->withTablePrefix(new QueryGrammar());
}

/**
Expand Down Expand Up @@ -304,7 +340,7 @@ protected function getConfigSchemaPrefix()
*/
protected function getDefaultSchemaGrammar()
{
return $this->withTablePrefix(new SchemaGrammar);
return $this->withTablePrefix(new SchemaGrammar());
}

/**
Expand All @@ -314,6 +350,6 @@ protected function getDefaultSchemaGrammar()
*/
protected function getDefaultPostProcessor()
{
return new Processor;
return new Processor();
}
}

0 comments on commit 2bdb453

Please sign in to comment.