Skip to content

Commit 6ef2f55

Browse files
committed
Fix parameter binding if query contains question marks
1 parent 7039687 commit 6ef2f55

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed

src/Connection.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,18 @@ public function query($sql, $callback = null, $params = null)
9797
array_shift($args); // Remove $sql parameter.
9898

9999
if (!is_callable($callback)) {
100-
$query->bindParamsFromArray($args);
100+
if ($args) {
101+
$query->bindParamsFromArray($args);
102+
}
101103

102104
return $this->_doCommand($command);
103105
}
104106

105107
array_shift($args); // Remove $callback
106108

107-
$query->bindParamsFromArray($args);
109+
if ($args) {
110+
$query->bindParamsFromArray($args);
111+
}
108112
$this->_doCommand($command);
109113

110114
$command->on('results', function ($rows, $command) use ($callback) {

src/Query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Query
2626

2727
public function __construct($sql)
2828
{
29-
$this->sql = $sql;
29+
$this->sql = $this->builtSql = $sql;
3030
}
3131

3232
/**

tests/QueryTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,41 @@ public function testBindParams()
2626
*/
2727
}
2828

29+
public function testGetSqlReturnsQuestionMarkReplacedWhenBound()
30+
{
31+
$query = new Query('select ?');
32+
$sql = $query->bindParams('hello')->getSql();
33+
$this->assertEquals("select 'hello'", $sql);
34+
}
35+
36+
public function testGetSqlReturnsQuestionMarkReplacedWhenBoundFromLastCall()
37+
{
38+
$query = new Query('select ?');
39+
$sql = $query->bindParams('foo')->bindParams('bar')->getSql();
40+
$this->assertEquals("select 'bar'", $sql);
41+
}
42+
43+
public function testGetSqlReturnsQuestionMarkReplacedWithNullValueWhenBound()
44+
{
45+
$query = new Query('select ?');
46+
$sql = $query->bindParams(null)->getSql();
47+
$this->assertEquals("select NULL", $sql);
48+
}
49+
50+
public function testGetSqlReturnsQuestionMarkReplacedFromBoundWhenBound()
51+
{
52+
$query = new Query('select CONCAT(?, ?)');
53+
$sql = $query->bindParams('hello??', 'world??')->getSql();
54+
$this->assertEquals("select CONCAT('hello??', 'world??')", $sql);
55+
}
56+
57+
public function testGetSqlReturnsQuestionMarksAsIsWhenNotBound()
58+
{
59+
$query = new Query('select "hello?"');
60+
$sql = $query->getSql();
61+
$this->assertEquals("select \"hello?\"", $sql);
62+
}
63+
2964
public function testEscapeChars()
3065
{
3166
$query = new Query('');

tests/ResultQueryTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,63 @@
44

55
class ResultQueryTest extends BaseTestCase
66
{
7+
public function testSelectStaticText()
8+
{
9+
$loop = \React\EventLoop\Factory::create();
10+
11+
$connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
12+
$connection->connect(function () {});
13+
14+
$connection->query('select \'foo\'', function ($command, $conn) {
15+
$this->assertEquals(false, $command->hasError());
16+
$this->assertCount(1, $command->resultRows);
17+
$this->assertCount(1, $command->resultRows[0]);
18+
$this->assertEquals('foo', reset($command->resultRows[0]));
19+
$this->assertInstanceOf('React\MySQL\Connection', $conn);
20+
});
21+
22+
$connection->close();
23+
$loop->run();
24+
}
25+
26+
public function testSelectStaticTextWithParamBinding()
27+
{
28+
$loop = \React\EventLoop\Factory::create();
29+
30+
$connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
31+
$connection->connect(function () {});
32+
33+
$connection->query('select ?', function ($command, $conn) {
34+
$this->assertEquals(false, $command->hasError());
35+
$this->assertCount(1, $command->resultRows);
36+
$this->assertCount(1, $command->resultRows[0]);
37+
$this->assertEquals('foo', reset($command->resultRows[0]));
38+
$this->assertInstanceOf('React\MySQL\Connection', $conn);
39+
}, 'foo');
40+
41+
$connection->close();
42+
$loop->run();
43+
}
44+
45+
public function testSelectStaticTextWithQuestionMark()
46+
{
47+
$loop = \React\EventLoop\Factory::create();
48+
49+
$connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
50+
$connection->connect(function () {});
51+
52+
$connection->query('select \'hello?\'', function ($command, $conn) {
53+
$this->assertEquals(false, $command->hasError());
54+
$this->assertCount(1, $command->resultRows);
55+
$this->assertCount(1, $command->resultRows[0]);
56+
$this->assertEquals('hello?', reset($command->resultRows[0]));
57+
$this->assertInstanceOf('React\MySQL\Connection', $conn);
58+
});
59+
60+
$connection->close();
61+
$loop->run();
62+
}
63+
764
public function testSimpleSelect()
865
{
966
$loop = \React\EventLoop\Factory::create();

0 commit comments

Comments
 (0)