Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix breaks named parameters in Oracle #3738

Merged
merged 1 commit into from
Dec 21, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,15 @@ public function bindValue($param, $value, $type = ParameterType::STRING)
/**
* {@inheritdoc}
*/
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null)
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null)
{
$column = $this->_paramMap[$column];
if (is_int($param)) {
if (! isset($this->_paramMap[$param])) {
throw new OCI8Exception(sprintf('Could not find variable mapping with index %d, in the SQL statement', $param));
Copy link
Member

Choose a reason for hiding this comment

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

The idea behind throwing an exception is that in this case, it wouldn't have worked previously anyways, so we're just making the error more explicit. Need to double-check that before merging.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

any news?

Copy link
Member

@morozov morozov Dec 20, 2019

Choose a reason for hiding this comment

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

@eisberg I forgot about it. Thank you for the reminder.

This is a valid code example that works on 2.9 and a build from this PR:

$stmt = $conn->prepare('SELECT :p FROM DUAL');
$stmt->bindValue('p', 'foo');
$stmt->execute();
echo $stmt->fetchColumn(), PHP_EOL;
// foo

This is an example of invalid code that is not supposed to work:

$stmt = $conn->prepare('SELECT :p FROM DUAL');
$stmt->bindValue(1, 'foo');
$stmt->execute();
echo $stmt->fetchColumn(), PHP_EOL;

On 2.9 it produces:

Warning: oci_bind_by_name(): ORA-01036: illegal variable name/number in lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php on line 287
Uncaught Doctrine\DBAL\Driver\OCI8\OCI8Exception: ORA-01008: not all variables bound

On a build from this patch it produces:

Uncaught Doctrine\DBAL\Driver\OCI8\OCI8Exception: Could not find variable mapping with index 1, in the SQL statement

Which is perfectly fine. DBAL prevents a knowingly invalid operation on the underlying driver.

/cc @beberlei (#3779).

}

$param = $this->_paramMap[$param];
}

if ($type === ParameterType::LARGE_OBJECT) {
$lob = oci_new_descriptor($this->_dbh, OCI_D_LOB);
Expand All @@ -291,11 +297,11 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l
$variable =& $lob;
}

$this->boundValues[$column] =& $variable;
$this->boundValues[$param] =& $variable;

return oci_bind_by_name(
$this->_sth,
$column,
$param,
$variable,
$length ?? -1,
$this->convertParameterType($type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,41 @@ public function testQueryConversion(string $query, array $params, array $expecte
);
}

/**
* Low-level approach to working with parameter binding
*
* @param mixed[] $params
* @param mixed[] $expected
*
* @dataProvider queryConversionProvider
*/
public function testStatementBindParameters(string $query, array $params, array $expected) : void
{
$stmt = $this->connection->prepare($query);
$stmt->execute($params);
morozov marked this conversation as resolved.
Show resolved Hide resolved

self::assertEquals(
$expected,
$stmt->fetch()
);
}

/**
* @return array<string, array<int, mixed>>
*/
public static function queryConversionProvider() : iterable
{
return [
'simple' => [
'positional' => [
'SELECT ? COL1 FROM DUAL',
[1],
['COL1' => 1],
],
'named' => [
eisberg marked this conversation as resolved.
Show resolved Hide resolved
'SELECT :COL COL1 FROM DUAL',
[':COL' => 1],
['COL1' => 1],
],
'literal-with-placeholder' => [
"SELECT '?' COL1, ? COL2 FROM DUAL",
[2],
Expand Down