nilportugues / php-sql-query-builder

An elegant lightweight and efficient SQL Query Builder with fluid interface SQL syntax supporting bindings and complicated query generation.
http://nilportugues.com
MIT License
417 stars 114 forks source link

Comparison against empty string converted to 'NULL' string #106

Open russell-pa opened 5 years ago

russell-pa commented 5 years ago

Trying to use an empty string for a comparison causes the query builder to replace it with a string of NULL.

Example:

$builder = new \NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;

$select = $builder->select('SomeTable');
$select->where()->equals('someField', '');

$sql = $builder->writeFormatted($select);
$parameters = $builder->getValues();

var_dump($sql, $parameters);
string(80) "SELECT
    SomeTable.*
FROM
    SomeTable
WHERE
    (SomeTable.someField = :v1)
"
array(1) {
  [":v1"]=>
  string(4) "NULL"
}
Silic0nS0ldier commented 4 years ago

This is caused by NilPortugues\Sql\QueryBuilder\Builder\Syntax\PlaceholderWriter@writeNullSqlString where an empty string is converted to NULL (presumably to be the sql keyword, however its treated as a string value). Given values as passed via paramter binding, the correct behaviour here would be to not convert the value at all (leaving null as null). This issue appears to hail back from the initial implementation.