Skip to content

Commit ec6eee8

Browse files
committed
Merge pull request #24 from cbalda/1.1
Fix empty array or null parameter in IN operand
2 parents 88a35ec + 393e4a3 commit ec6eee8

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

.travis.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
language: php
22

33
php:
4-
- 5.5
4+
- 5.6
5+
- 7.0
56

67
before_script:
7-
- wget http://getcomposer.org/composer.phar
8-
- php composer.phar install --no-interaction
8+
- composer install --no-interaction
99

1010
script:
11-
- mkdir -p build/logs
12-
- phpunit --coverage-clover build/logs/clover.xml
11+
- phpunit
1312

1413
after_script:
1514
- php vendor/bin/coveralls -v

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"twig/twig": "~1.14"
2323
},
2424
"require-dev": {
25-
"phpunit/phpunit": "~4.0",
26-
"satooshi/php-coveralls": "dev-master",
25+
"phpunit/phpunit": "~5.0",
26+
"satooshi/php-coveralls": "~1.0",
2727
"doctrine/dbal": "~2.5"
2828
},
2929
"suggest": {

phpunit.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@
2727
<directory suffix=".php">src/</directory>
2828
</whitelist>
2929
</filter>
30+
<logging>
31+
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
32+
<log type="coverage-clover" target="build/logs/clover.xml"/>
33+
</logging>
3034
</phpunit>

src/SQLParser/Node/In.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
namespace SQLParser\Node;
4+
use Doctrine\DBAL\Connection;
5+
use Mouf\Database\MagicQueryException;
46

57
/**
68
* This class represents an In operation in an SQL expression.
@@ -18,4 +20,19 @@ protected function getOperatorSymbol()
1820
{
1921
return 'IN';
2022
}
23+
24+
protected function getSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY)
25+
{
26+
$rightOperand = $this->getRightOperand();
27+
if ($rightOperand instanceof Parameter) {
28+
if (!isset($parameters[$rightOperand->getName()])) {
29+
throw new MagicQueryException("Missing parameter '" . $rightOperand->getName() . "' for 'IN' operand.");
30+
}
31+
if ($parameters[$rightOperand->getName()] === []) {
32+
return "FALSE";
33+
}
34+
}
35+
36+
return parent::getSql($parameters, $dbConnection, $indent, $conditionsMode);
37+
}
2138
}

tests/Mouf/Database/MagicQueryTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ public function testStandardSelect()
101101
// Test CASE WHERE like SWITCH CASE
102102
$sql = "SELECT CASE status WHEN 'on' THEN '1' WHEN 'off' THEN '0' ELSE '-1' END AS my_case FROM users";
103103
$this->assertEquals("SELECT CASE status WHEN 'on' THEN '1' WHEN 'off' THEN '0' ELSE '-1' END AS my_case FROM users", self::simplifySql($magicQuery->build($sql)));
104+
105+
$sql = 'SELECT * FROM users WHERE status IN :statuses!';
106+
$this->assertEquals('SELECT * FROM users WHERE FALSE', self::simplifySql($magicQuery->build($sql, ['statuses' => []])));
107+
}
108+
109+
/**
110+
* @expectedException \Mouf\Database\MagicQueryException
111+
*/
112+
public function testInNullException() {
113+
$magicQuery = new MagicQuery();
114+
115+
$sql = 'SELECT * FROM users WHERE status IN :statuses!';
116+
$magicQuery->build($sql, ['statuses' => NULL]);
104117
}
105118

106119
public function testWithCache()

0 commit comments

Comments
 (0)