From b249dc7b516dfed039b1d7705b6d578ead9300e6 Mon Sep 17 00:00:00 2001 From: "Paul M. Jones" Date: Tue, 3 Feb 2015 10:46:29 -0600 Subject: [PATCH 1/4] throw CannotBindValue when bindValue() encounters non-bindable value --- src/Exception/CannotBindValue.php | 23 +++++++++++++++ src/ExtendedPdo.php | 11 ++++++++ tests/unit/src/AbstractExtendedPdoTest.php | 33 ++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 src/Exception/CannotBindValue.php diff --git a/src/Exception/CannotBindValue.php b/src/Exception/CannotBindValue.php new file mode 100644 index 00000000..ffc8144e --- /dev/null +++ b/src/Exception/CannotBindValue.php @@ -0,0 +1,23 @@ +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); } } diff --git a/tests/unit/src/AbstractExtendedPdoTest.php b/tests/unit/src/AbstractExtendedPdoTest.php index 822d5474..e2270532 100755 --- a/tests/unit/src/AbstractExtendedPdoTest.php +++ b/tests/unit/src/AbstractExtendedPdoTest.php @@ -2,6 +2,7 @@ namespace Aura\Sql; use PDO; +use StdClass; abstract class AbstractExtendedPdoTest extends \PHPUnit_Framework_TestCase { @@ -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, ['id' => 1]); + $this->assertInstanceOf('PDOStatement', $sth); + + // PDO::PARAM_BOOL + $sth = $this->pdo->prepareWithValues($stm, ['id' => true]); + $this->assertInstanceOf('PDOStatement', $sth); + + // PDO::PARAM_NULL + $sth = $this->pdo->prepareWithValues($stm, ['id' => null]); + $this->assertInstanceOf('PDOStatement', $sth); + + // string (not a special type) + $sth = $this->pdo->prepareWithValues($stm, ['id' => 'xyz']); + $this->assertInstanceOf('PDOStatement', $sth); + + // float (also not a special type) + $sth = $this->pdo->prepareWithValues($stm, ['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, ['id' => new StdClass]); + } } From 212aa8f0256cd8175225c67ba5bf46794be6a35e Mon Sep 17 00:00:00 2001 From: "Paul M. Jones" Date: Tue, 3 Feb 2015 10:47:14 -0600 Subject: [PATCH 2/4] update comment --- src/Exception/CannotBindValue.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Exception/CannotBindValue.php b/src/Exception/CannotBindValue.php index ffc8144e..336ddd29 100644 --- a/src/Exception/CannotBindValue.php +++ b/src/Exception/CannotBindValue.php @@ -13,7 +13,7 @@ /** * * Could not bind a value to a placeholder in a statement, generally because - * the value is non-scalar (array, object, or resource). + * the value is an array, object, or resource. * * @package Aura.Sql * From 11aa4b206f407afb6c342db6d35637b32b7894ca Mon Sep 17 00:00:00 2001 From: "Paul M. Jones" Date: Tue, 3 Feb 2015 13:01:16 -0600 Subject: [PATCH 3/4] php 5.3 compat --- src/ExtendedPdo.php | 2 +- tests/unit/src/AbstractExtendedPdoTest.php | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ExtendedPdo.php b/src/ExtendedPdo.php index e970cf32..24f4fe01 100644 --- a/src/ExtendedPdo.php +++ b/src/ExtendedPdo.php @@ -8,9 +8,9 @@ */ namespace Aura\Sql; +use Aura\Sql\Exception; use PDO; use PDOStatement; -use Aura\Sql\Exception; /** * diff --git a/tests/unit/src/AbstractExtendedPdoTest.php b/tests/unit/src/AbstractExtendedPdoTest.php index e2270532..b1258cb0 100755 --- a/tests/unit/src/AbstractExtendedPdoTest.php +++ b/tests/unit/src/AbstractExtendedPdoTest.php @@ -571,23 +571,23 @@ public function testBindValues() $stm = 'SELECT * FROM pdotest WHERE id = :id'; // PDO::PARAM_INT - $sth = $this->pdo->prepareWithValues($stm, ['id' => 1]); + $sth = $this->pdo->prepareWithValues($stm, array('id' => 1)); $this->assertInstanceOf('PDOStatement', $sth); // PDO::PARAM_BOOL - $sth = $this->pdo->prepareWithValues($stm, ['id' => true]); + $sth = $this->pdo->prepareWithValues($stm, array('id' => true)); $this->assertInstanceOf('PDOStatement', $sth); // PDO::PARAM_NULL - $sth = $this->pdo->prepareWithValues($stm, ['id' => null]); + $sth = $this->pdo->prepareWithValues($stm, array('id' => null)); $this->assertInstanceOf('PDOStatement', $sth); // string (not a special type) - $sth = $this->pdo->prepareWithValues($stm, ['id' => 'xyz']); + $sth = $this->pdo->prepareWithValues($stm, array('id' => 'xyz')); $this->assertInstanceOf('PDOStatement', $sth); // float (also not a special type) - $sth = $this->pdo->prepareWithValues($stm, ['id' => 1.23]); + $sth = $this->pdo->prepareWithValues($stm, array('id' => 1.23)); $this->assertInstanceOf('PDOStatement', $sth); // non-bindable @@ -595,6 +595,6 @@ public function testBindValues() 'Aura\Sql\Exception\CannotBindValue', "Cannot bind value of type 'object' to placeholder 'id'" ); - $sth = $this->pdo->prepareWithValues($stm, ['id' => new StdClass]); + $sth = $this->pdo->prepareWithValues($stm, array(('id' => new StdClass)); } } From f3137dd4b45e99ba2ca52b65398009f5ba56c0de Mon Sep 17 00:00:00 2001 From: "Paul M. Jones" Date: Tue, 3 Feb 2015 13:03:44 -0600 Subject: [PATCH 4/4] typo fix --- tests/unit/src/AbstractExtendedPdoTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/src/AbstractExtendedPdoTest.php b/tests/unit/src/AbstractExtendedPdoTest.php index b1258cb0..54b96aec 100755 --- a/tests/unit/src/AbstractExtendedPdoTest.php +++ b/tests/unit/src/AbstractExtendedPdoTest.php @@ -595,6 +595,6 @@ public function testBindValues() 'Aura\Sql\Exception\CannotBindValue', "Cannot bind value of type 'object' to placeholder 'id'" ); - $sth = $this->pdo->prepareWithValues($stm, array(('id' => new StdClass)); + $sth = $this->pdo->prepareWithValues($stm, array('id' => new StdClass)); } }