Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Revert PR #224 due to issue like #288 #289

Merged
merged 2 commits into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Adapter/Driver/Pdo/Pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,15 @@ public function getPrepareType()
public function formatParameterName($name, $type = null)
{
if ($type === null && ! is_numeric($name) || $type == self::PARAMETERIZATION_NAMED) {
// using MD5 because of the PDO restriction [A-Za-z0-9_] for bindParam name
return ':' . md5($name);
// @see https://bugs.php.net/bug.php?id=43130
if (preg_match('/[^a-zA-Z0-9_]/', $name)) {
throw new Exception\RuntimeException(sprintf(
"The PDO param %s contains characters not allowed. " .
"You can use only letter, digit, and underscore (_)",
$name
));
}
return ':' . $name;
}

return '?';
Expand Down
37 changes: 28 additions & 9 deletions test/Adapter/Driver/Pdo/PdoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ public function testGetDatabasePlatformName()
public function getParamsAndType()
{
return [
[ 'foo', null, ':' . md5('foo')],
[ 'foo-', null, ':' . md5('foo-')],
[ 'foo$', null, ':' . md5('foo$')],
[ 'foo', null, ':foo' ],
[ 'foo_bar', null, ':foo_bar' ],
[ '123foo', null, ':123foo' ],
[ 1, null, '?' ],
[ '1', null, '?'],
[ 'foo', Pdo::PARAMETERIZATION_NAMED, ':' . md5('foo')],
[ 'foo-', Pdo::PARAMETERIZATION_NAMED, ':' . md5('foo-')],
[ 'foo$', Pdo::PARAMETERIZATION_NAMED, ':' . md5('foo$')],
[ 1, Pdo::PARAMETERIZATION_NAMED, ':' . md5('1')],
[ '1', Pdo::PARAMETERIZATION_NAMED, ':' . md5('1')],
[ '1', null, '?' ],
[ 'foo', Pdo::PARAMETERIZATION_NAMED, ':foo' ],
[ 'foo_bar', Pdo::PARAMETERIZATION_NAMED, ':foo_bar' ],
[ '123foo', Pdo::PARAMETERIZATION_NAMED, ':123foo' ],
[ 1, Pdo::PARAMETERIZATION_NAMED, ':1' ],
[ '1', Pdo::PARAMETERIZATION_NAMED, ':1' ],
];
}

Expand All @@ -64,4 +64,23 @@ public function testFormatParameterName($name, $type, $expected)
$result = $this->pdo->formatParameterName($name, $type);
$this->assertEquals($expected, $result);
}

public function getInvalidParamName()
{
return [
[ 'foo%' ],
[ 'foo-' ],
[ 'foo$' ],
[ 'foo0!' ]
];
}

/**
* @dataProvider getInvalidParamName
* @expectedException Zend\Db\Exception\RuntimeException
*/
public function testFormatParameterNameWithInvalidCharacters($name)
{
$this->pdo->formatParameterName($name);
}
}
4 changes: 2 additions & 2 deletions test/Adapter/Driver/Pdo/StatementIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected function tearDown()
public function testStatementExecuteWillConvertPhpBoolToPdoBoolWhenBinding()
{
$this->pdoStatementMock->expects($this->any())->method('bindParam')->with(
$this->equalTo(':' . md5('foo')),
$this->equalTo(':foo'),
$this->equalTo(false),
$this->equalTo(\PDO::PARAM_BOOL)
);
Expand All @@ -65,7 +65,7 @@ public function testStatementExecuteWillConvertPhpBoolToPdoBoolWhenBinding()
public function testStatementExecuteWillUsePdoStrByDefaultWhenBinding()
{
$this->pdoStatementMock->expects($this->any())->method('bindParam')->with(
$this->equalTo(':' . md5('foo')),
$this->equalTo(':foo'),
$this->equalTo('bar'),
$this->equalTo(\PDO::PARAM_STR)
);
Expand Down
24 changes: 0 additions & 24 deletions test/Adapter/Driver/Pdo/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,28 +128,4 @@ public function testExecute()
$this->statement->prepare('SELECT 1');
self::assertInstanceOf('Zend\Db\Adapter\Driver\Pdo\Result', $this->statement->execute());
}

/**
* @see https://github.com/zendframework/zend-db/pull/224
*/
public function testExecuteWithSpecialCharInBindParam()
{
$testSqlite = new TestAsset\SqliteMemoryPdo('CREATE TABLE test (text_ TEXT, text$ TEXT);');
$this->statement->setDriver(new Pdo(new Connection($testSqlite)));
$this->statement->initialize($testSqlite);

$this->statement->prepare(sprintf(
'INSERT INTO test (text_, text$) VALUES (:%s, :%s)',
md5('text_'),
md5('text$')
));
$result = $this->statement->execute([ 'text_' => 'foo', 'text$' => 'bar']);
$this->assertInstanceOf(Result::class, $result);
$this->assertTrue($result->valid());

$result = $testSqlite->query('SELECT * FROM test');
$values = $result->fetch();
$this->assertEquals('foo', $values['text_']);
$this->assertEquals('bar', $values['text$']);
}
}