Skip to content

Commit

Permalink
Merge pull request #94 from auraphp/cannotbindvalue
Browse files Browse the repository at this point in the history
Throw CannotBindValue when bindValue() encounters non-bindable value
  • Loading branch information
Paul M. Jones committed Feb 3, 2015
2 parents 516d54e + f3137dd commit 1b4972f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Exception/CannotBindValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
*
* This file is part of Aura for PHP.
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
*/
namespace Aura\Sql\Exception;

use Aura\Sql\Exception;

/**
*
* Could not bind a value to a placeholder in a statement, generally because
* the value is an array, object, or resource.
*
* @package Aura.Sql
*
*/
class CannotBindValue extends Exception
{
}
10 changes: 10 additions & 0 deletions src/ExtendedPdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,9 @@ public function prepareWithValues($statement, array $values = array())
*
* @return null
*
* @throws Exception\CannotBindValue when the value to be bound is not
* bindable (e.g., array, object, or resource).
*
*/
protected function bindValue(PDOStatement $sth, $key, $val)
{
Expand All @@ -1019,6 +1022,13 @@ protected function bindValue(PDOStatement $sth, $key, $val)
return $sth->bindValue($key, $val, self::PARAM_NULL);
}

if (! is_scalar($val)) {
$type = gettype($val);
throw new Exception\CannotBindValue(
"Cannot bind value of type '{$type}' to placeholder '{$key}'"
);
}

$sth->bindValue($key, $val);
}
}
33 changes: 33 additions & 0 deletions tests/unit/src/AbstractExtendedPdoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Aura\Sql;

use PDO;
use StdClass;

abstract class AbstractExtendedPdoTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -564,4 +565,36 @@ public function testPdoDependency()
}
$this->assertEquals($expect, $actual);
}

public function testBindValues()
{
$stm = 'SELECT * FROM pdotest WHERE id = :id';

// PDO::PARAM_INT
$sth = $this->pdo->prepareWithValues($stm, array('id' => 1));
$this->assertInstanceOf('PDOStatement', $sth);

// PDO::PARAM_BOOL
$sth = $this->pdo->prepareWithValues($stm, array('id' => true));
$this->assertInstanceOf('PDOStatement', $sth);

// PDO::PARAM_NULL
$sth = $this->pdo->prepareWithValues($stm, array('id' => null));
$this->assertInstanceOf('PDOStatement', $sth);

// string (not a special type)
$sth = $this->pdo->prepareWithValues($stm, array('id' => 'xyz'));
$this->assertInstanceOf('PDOStatement', $sth);

// float (also not a special type)
$sth = $this->pdo->prepareWithValues($stm, array('id' => 1.23));
$this->assertInstanceOf('PDOStatement', $sth);

// non-bindable
$this->setExpectedException(
'Aura\Sql\Exception\CannotBindValue',
"Cannot bind value of type 'object' to placeholder 'id'"
);
$sth = $this->pdo->prepareWithValues($stm, array('id' => new StdClass));
}
}

0 comments on commit 1b4972f

Please sign in to comment.