question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Detect parameter nullability from query and suggest correct parameter functions

See original GitHub issue

Right 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:closed
  • Created 3 years ago
  • Comments:24 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
Zaid-Ajajcommented, Aug 27, 2020

@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

1reaction
rojicommented, Aug 20, 2020

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 @p parameter 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

C# 11.0 preview: parameter null checking
C# 11.0 has a new syntax, !!, which makes it easier to detect and reject null arguments. It is simple, but misunderstood.
Read more >
sql query if parameter is null select all
Try this: SELECT * FROM MY_TABLE WHERE @parameter IS NULL OR NAME = @parameter;.
Read more >
Resolve nullable warnings
Several compiler warnings indicate code that isn't null-safe. Learn how to address those warnings by making your code more resilient.
Read more >
Working with SQL NULL values
This article will show functions and operators for handling SQL NULL values.
Read more >
How do I check for a null or empty table-valued parameter?
I have a stored procedure (SS2k8) with a couple table-valued parameters that will sometimes be null or empty. I have seen this StackOverflow ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found