Detect parameter nullability from query and suggest correct parameter functions
See original GitHub issueRight now, when we derive the required parameters for a query, we only know what the type is of the parameter. However, we don’t know whether a parameter is allowed to be null or not. Currently when the analyzer detects incorrect usage of SQL parameter, it suggests Sql.{type}, Sql.{type}orNone and Sql.dbnull. For non-nullable parameter types, it should only suggest Sql.{type} to use as parameter.
According to Npgsql#3115 Unable to detect parameter nullability when using DeriveParameters, this is a limitation of PostgreSQL itself when it describes the types of the available parameters.
Our best bet is build a SQL parser for the query and detect parameter nullability ourselves by using the definitions from the schema of the database. As well as detecting untyped parameters and inferring their types for example WHERE @p > 10 should infer that @p is int or bigint
Issue Analytics
- State:
- Created 3 years ago
- Comments:24 (13 by maintainers)

Top Related StackOverflow Question
@roji @YohDeadfall @costa100 in case you are interested, today I went live on stream and actually implemented an initial prototype of how this would look like. I managed to make the analyzer give correct suggestions to INSERT queries when using non-nullable columns. You can watch the stream here on Twitch
OK, really interesting stuff - I’d be very interested in seeing where it goes. Note that Npgsql itself contains a basic lexical parser for the PG SQL dialect, since it needs to convert from named
@pparameter placeholders to the PostgreSQL native positional ones:$1(and also to split on statement boundaries). The parser only needs to understand when something is inside a string, so it’s quite basic - but it may be a bit useful to you.