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
250 stars 17 forks source link

Query AST analysis? #566

Closed jakubvojacek closed 1 year ago

jakubvojacek commented 1 year ago

Hello @staabm

how far advanced do you think the analysis is, should we start reporting issues? I get errors thrown on some more non-standard queries, such as CROSS join or queries with subqueries.

But I also tested with simple select count(*) from xx and I didnt get a RangeIntegerType. I tried digging deeper and found that it never got past this condition https://github.com/staabm/phpstan-dba/blob/main/src/SqlAst/ParserInference.php#L102 ($valueType was min: 0, max: null but it was not merged to $resultType due to hasOffsetValueType === false)

I am not sure I understand all the parts of the code to start sending PRs though.

Thanks

staabm commented 1 year ago

hey,

thx for asking. for SQL AST to work you need to enable it in your phpstan-dba-bootstrap via RuntimeConfiguration

after thats done and you are using mysql it should work for select count(*) from xx. I would love someone doing more testing with it.

such as CROSS join or queries with subqueries.

I have open todos regarding joins, which I have expressed here https://github.com/staabm/phpstan-dba/issues/527 if you have concrete examples and concrete expectations - at best already in form of a unit test - that would be really awesome.

But I also tested with simple select count(*) from xx and I didnt get a RangeIntegerType

please provide a reproducing case. see https://github.com/staabm/phpstan-dba/blob/main/tests/default/data/sql-ast-narrowing.php for all already existing test-cases

jakubvojacek commented 1 year ago

you need to enable it in your phpstan-dba-bootstrap

yeah, I did that

please provide a reproducing case.

will try, perhaps I am still missunderstanding some parts of it and coming up with repro case will help me understand.

Thanks :+1: