staabm / phpstan-dba

PHPStan based SQL static analysis and type inference for the database access layer
https://staabm.github.io/archive.html#phpstan-dba
MIT License
258 stars 17 forks source link

properly simulate `IS NULL` #297

Open staabm opened 2 years ago

staabm commented 2 years ago

instead of

SELECT `adp`.*, `ada`.`spracheid`
                FROM `adp`
                INNER JOIN `ada` ON ada.adaid = adp.adaid
                WHERE (adpid = NULL)
                LIMIT 1

we should simulate

SELECT `adp`.*, `ada`.`spracheid`
                FROM `adp`
                INNER JOIN `ada` ON ada.adaid = adp.adaid
                WHERE adpid IS NULL
                LIMIT 1

for code like

            $query = '
                SELECT `adp`.*, `ada`.`spracheid`
                FROM `adp`
                INNER JOIN `ada` ON ada.adaid = adp.adaid
                WHERE (adpid = ?)
                LIMIT 1';

            $adpMapper = new Application_Model_Mapper_Adp();
            $adpModel = $adpMapper->fetchRowByStatement(new ClxProductNet_DbStatement($query, [$adpid]));
staabm commented 2 years ago

Related https://stackoverflow.com/a/10340795

staabm commented 1 year ago

The actual query simulation might be unnecessary.. but we could determine non-nullable types, when IS NULL is used in WHERE

hemberger commented 1 year ago

This is partially completed by #596. We still need to support the WHERE x=NULL case.