OpenGamma/ElSql

Support for Null parameters in conditionals

Closed this issue · 3 comments

It would be nice if there was a way (there doesn't appear to be one that I can see) to either match on a column or add an IS NULL if there is no parameter (or the parameter value is null).

Perhaps something like this?

@AND(:name)
     a.name = :name
@NOT @AND(:name)
     a.name IS NULL

Trying to do @AND(:name = null) currently gives a NPE, and changing it so that a null value matches a string of "null" (e.g. using Objects.toString(value) instead of value.toString() in ConditionalSqlFragment#isMatch) might be bad if you actually had strings with value "null" in your DB - however that would be a simpler solution.

I'm willing to work on implementing something if I can get some advice on whether this type of syntax would be good or if something else would be more in keeping with the rest of the library.

I'm finding ElSql to be a useful library to externalize SQL and support multiple databases.

Given that we have a.name @LIKE :name today, the obvious format looks like a.name @EQUALS :name.

I suspect that @LIKE should also be enhanced to handle null.

I've fixed up some issues around null, but I'll let you decide if that proposed syntax works for you.

There's a lot of overlap between @LIKE and @EQUALS especially if @LIKE is changed to output IS NULL when the parameter exists but is null.
To get something working I basically copied LikeSqlFragment to EqualsSqlFragment and copied the parsing in ElSqlParser, but there's a lot of duplication between the two that I'd like to get rid of.

I'll push something up to my fork when I'm happier with it.

Merged, thanks