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 parameter binding if query contains question marks #40

Merged
merged 1 commit into from
Apr 3, 2018
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
8 changes: 6 additions & 2 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,18 @@ public function query($sql, $callback = null, $params = null)
array_shift($args); // Remove $sql parameter.

if (!is_callable($callback)) {
$query->bindParamsFromArray($args);
if ($args) {
$query->bindParamsFromArray($args);
}

return $this->_doCommand($command);
}

array_shift($args); // Remove $callback

$query->bindParamsFromArray($args);
if ($args) {
$query->bindParamsFromArray($args);
}
$this->_doCommand($command);

$command->on('results', function ($rows, $command) use ($callback) {
Expand Down
2 changes: 1 addition & 1 deletion src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Query

public function __construct($sql)
{
$this->sql = $sql;
$this->sql = $this->builtSql = $sql;
}

/**
Expand Down
35 changes: 35 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,41 @@ public function testBindParams()
*/
}

public function testGetSqlReturnsQuestionMarkReplacedWhenBound()
{
$query = new Query('select ?');
$sql = $query->bindParams('hello')->getSql();
$this->assertEquals("select 'hello'", $sql);
}

public function testGetSqlReturnsQuestionMarkReplacedWhenBoundFromLastCall()
{
$query = new Query('select ?');
$sql = $query->bindParams('foo')->bindParams('bar')->getSql();
$this->assertEquals("select 'bar'", $sql);
}

public function testGetSqlReturnsQuestionMarkReplacedWithNullValueWhenBound()
{
$query = new Query('select ?');
$sql = $query->bindParams(null)->getSql();
$this->assertEquals("select NULL", $sql);
}

public function testGetSqlReturnsQuestionMarkReplacedFromBoundWhenBound()
{
$query = new Query('select CONCAT(?, ?)');
$sql = $query->bindParams('hello??', 'world??')->getSql();
$this->assertEquals("select CONCAT('hello??', 'world??')", $sql);
}

public function testGetSqlReturnsQuestionMarksAsIsWhenNotBound()
{
$query = new Query('select "hello?"');
$sql = $query->getSql();
$this->assertEquals("select \"hello?\"", $sql);
}

public function testEscapeChars()
{
$query = new Query('');
Expand Down
57 changes: 57 additions & 0 deletions tests/ResultQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,63 @@

class ResultQueryTest extends BaseTestCase
{
public function testSelectStaticText()
{
$loop = \React\EventLoop\Factory::create();

$connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
$connection->connect(function () {});

$connection->query('select \'foo\'', function ($command, $conn) {
$this->assertEquals(false, $command->hasError());
$this->assertCount(1, $command->resultRows);
$this->assertCount(1, $command->resultRows[0]);
$this->assertEquals('foo', reset($command->resultRows[0]));
$this->assertInstanceOf('React\MySQL\Connection', $conn);
});

$connection->close();
$loop->run();
}

public function testSelectStaticTextWithParamBinding()
{
$loop = \React\EventLoop\Factory::create();

$connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
$connection->connect(function () {});

$connection->query('select ?', function ($command, $conn) {
$this->assertEquals(false, $command->hasError());
$this->assertCount(1, $command->resultRows);
$this->assertCount(1, $command->resultRows[0]);
$this->assertEquals('foo', reset($command->resultRows[0]));
$this->assertInstanceOf('React\MySQL\Connection', $conn);
}, 'foo');

$connection->close();
$loop->run();
}

public function testSelectStaticTextWithQuestionMark()
{
$loop = \React\EventLoop\Factory::create();

$connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
$connection->connect(function () {});

$connection->query('select \'hello?\'', function ($command, $conn) {
$this->assertEquals(false, $command->hasError());
$this->assertCount(1, $command->resultRows);
$this->assertCount(1, $command->resultRows[0]);
$this->assertEquals('hello?', reset($command->resultRows[0]));
$this->assertInstanceOf('React\MySQL\Connection', $conn);
});

$connection->close();
$loop->run();
}

public function testSimpleSelect()
{
$loop = \React\EventLoop\Factory::create();
Expand Down