Commit 2eece76f authored by Sergei Morozov's avatar Sergei Morozov Committed by Marco Pivetta

[DBAL-2646] Fix the reuse of a statement on sqlsrv with explicit bindValue() and bindParam()

parent a1add228
......@@ -140,7 +140,14 @@ class SQLSrvStatement implements IteratorAggregate, Statement
*/
public function bindValue($param, $value, $type = null)
{
return $this->bindParam($param, $value, $type, null);
if (!is_numeric($param)) {
throw new SQLSrvException(
'sqlsrv does not support named parameters to queries, use question mark (?) placeholders instead.'
);
}
$this->variables[$param] = $value;
$this->types[$param] = $type;
}
/**
......@@ -154,6 +161,9 @@ class SQLSrvStatement implements IteratorAggregate, Statement
$this->variables[$column] =& $variable;
$this->types[$column] = $type;
// unset the statement resource if it exists as the new one will need to be bound to the new variable
$this->stmt = null;
}
/**
......@@ -215,8 +225,7 @@ class SQLSrvStatement implements IteratorAggregate, Statement
$hasZeroIndex = array_key_exists(0, $params);
foreach ($params as $key => $val) {
$key = ($hasZeroIndex && is_numeric($key)) ? $key + 1 : $key;
$this->variables[$key] = $val;
$this->types[$key] = null;
$this->bindValue($key, $val);
}
}
......
......@@ -174,6 +174,40 @@ EOF
$this->assertEquals(2, $stmt->fetchColumn());
}
public function testReuseStatementWithReboundValue()
{
$this->_conn->insert('stmt_test', array('id' => 1));
$this->_conn->insert('stmt_test', array('id' => 2));
$stmt = $this->_conn->prepare('SELECT id FROM stmt_test WHERE id = ?');
$stmt->bindValue(1, 1);
$stmt->execute();
$this->assertEquals(1, $stmt->fetchColumn());
$stmt->bindValue(1, 2);
$stmt->execute();
$this->assertEquals(2, $stmt->fetchColumn());
}
public function testReuseStatementWithReboundParam()
{
$this->_conn->insert('stmt_test', array('id' => 1));
$this->_conn->insert('stmt_test', array('id' => 2));
$stmt = $this->_conn->prepare('SELECT id FROM stmt_test WHERE id = ?');
$x = 1;
$stmt->bindParam(1, $x);
$stmt->execute();
$this->assertEquals(1, $stmt->fetchColumn());
$y = 2;
$stmt->bindParam(1, $y);
$stmt->execute();
$this->assertEquals(2, $stmt->fetchColumn());
}
/**
* @dataProvider emptyFetchProvider
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment