Skip to content

Commit

Permalink
[DBAL-2646] Fix the reuse of a statement on sqlsrv with explicit bind…
Browse files Browse the repository at this point in the history
…Value() and bindParam()
  • Loading branch information
morozov committed Feb 8, 2017
1 parent 16be549 commit d6a7079
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,14 @@ public function __construct($conn, $sql, LastInsertId $lastInsertId = null)
*/
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;
}

/**
Expand All @@ -154,6 +161,9 @@ public function bindParam($column, &$variable, $type = null, $length = null)

$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;
}

/**
Expand Down Expand Up @@ -215,8 +225,7 @@ public function execute($params = null)
$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);
}
}

Expand Down
34 changes: 34 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,40 @@ public function testReuseStatementWithParameterBoundByReference()
$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
*/
Expand Down

0 comments on commit d6a7079

Please sign in to comment.