php-sql-query-builder icon indicating copy to clipboard operation
php-sql-query-builder copied to clipboard

Comparison against empty string converted to 'NULL' string

Open russell-pa opened this issue 5 years ago • 1 comments

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"
}

russell-pa avatar May 16 '19 16:05 russell-pa

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.

Silic0nS0ldier avatar Jul 16 '20 00:07 Silic0nS0ldier