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

Fix PDO parameter names #178

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion src/Adapter/Driver/Pdo/Pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public function getPrepareType()
public function formatParameterName($name, $type = null)
{
if ($type === null && !is_numeric($name) || $type == self::PARAMETERIZATION_NAMED) {
return ':' . $name;
return ':' . preg_replace('/[^a-zA-Z0-9_]/', '_', $name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This replace can produce conflicts in bindParam name. For instance, if you have 2 fields text- and text$ they will be replaced with text_ and text_ both.

}

return '?';
Expand Down
9 changes: 7 additions & 2 deletions src/Sql/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,13 @@ protected function processInsert(PlatformInterface $platform, DriverInterface $d
foreach ($this->columns as $column => $value) {
$columns[] = $platform->quoteIdentifier($column);
if (is_scalar($value) && $parameterContainer) {
$values[] = $driver->formatParameterName($column);
$parameterContainer->offsetSet($column, $value);
$parameterName = $driver->formatParameterName($column);
$values[] = $parameterName;
if ($driver->getPrepareType() == $driver::PARAMETERIZATION_NAMED and $parameterName != '?') {
$parameterContainer->offsetSet(substr($parameterName, 1), $value);
} else {
$parameterContainer->offsetSet($column, $value);
}
} else {
$values[] = $this->resolveColumnValue(
$value,
Expand Down
9 changes: 7 additions & 2 deletions src/Sql/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,13 @@ protected function processSet(PlatformInterface $platform, DriverInterface $driv
foreach ($this->set as $column => $value) {
$prefix = $platform->quoteIdentifier($column) . ' = ';
if (is_scalar($value) && $parameterContainer) {
$setSql[] = $prefix . $driver->formatParameterName($column);
$parameterContainer->offsetSet($column, $value);
$parameterName = $driver->formatParameterName($column);
$setSql[] = $prefix . $parameterName;
if ($driver->getPrepareType() == $driver::PARAMETERIZATION_NAMED and $parameterName != '?') {
$parameterContainer->offsetSet(substr($parameterName, 1), $value);
} else {
$parameterContainer->offsetSet($column, $value);
}
} else {
$setSql[] = $prefix . $this->resolveColumnValue(
$value,
Expand Down
13 changes: 13 additions & 0 deletions test/Adapter/Driver/Pdo/PdoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,17 @@ public function testGetDatabasePlatformName()
$this->assertEquals('SqlServer', $this->pdo->getDatabasePlatformName());
$this->assertEquals('SQLServer', $this->pdo->getDatabasePlatformName(DriverInterface::NAME_FORMAT_NATURAL));
}

/**
* @covers Zend\Db\Adapter\Driver\Pdo\Pdo::formatParameterName
*/
public function testFormatParameterName()
{
$this->assertEquals('?', $this->pdo->formatParameterName(1));
$this->assertEquals(':testName', $this->pdo->formatParameterName('testName'));
$this->assertEquals(':testName_', $this->pdo->formatParameterName('testName_'));
$this->assertEquals(':testName1', $this->pdo->formatParameterName('testName1'));
$this->assertEquals(':testName_', $this->pdo->formatParameterName('testName$'));
$this->assertEquals(':testName_', $this->pdo->formatParameterName('testName#'));
}
}