ENH: Add ServerConfig expressionEqualsWithNullAsNoop - Can eq ne gt ... methods in ExpressionList treat null value as 1=1 ?
When using Ebean.find(Bean.Class).where().eq("prop",null).eq("prop2",prop2Value) , the sql turned out like select columns from table where prop is null But most of my usecases I really need it turned out to be select columns from table where 1=1 and prop2='prop2Value' . to meet this require I must test the value if it is null then call the eq method like this
ExpressionList el = Ebean.find(Bean.class).where().eq("prop2",prop2Value);
if(propValue!=null){
el.eq("prop",propValue)
}
el.findList();
I rewrite the DefaultExpressionFactory to support this feture , now I can wirte the code like
Ebean.find(Bean.class).where().eq("prop",propValue).eq("prop2",prop2Value).findList();
if the propValue is null or blank it will be 1=1 in generated sql instead.
This is a change of behaviour and the change you propose will break other people's code.
To me technically the current behaviour is more correct (that the expression becomes isNull() isNotNull() seems more correct to me). Probably the only way to get this change in is to have it as a configurable option in ServerConfig.
On 29 July 2015 at 16:49, EChrist [email protected] wrote:
I rewrite the DefaultExpressionFactory to support this feture , now I can wirte the code like
Ebean.find(Bean.class).where().eq("prop",propValue).eq("prop2",prop2Value).findList();if the propValue is null or blank it will be 1=1 in generated sql instead.
— Reply to this email directly or view it on GitHub https://github.com/ebean-orm/avaje-ebeanorm/issues/361#issuecomment-125840195 .
I do rewrite just in my porject , But for ebean framework provide a configurable option in ServerConfig to support this is the most elegant way
Thanks&Have a nice day
So this is included as a configuration option on ServerConfig so:
/**
* Set to true if you want eq("someProperty", null) to generate "1=1" rather than "is null" sql expression.
* <p>
* Setting this to true has the effect that eq(propertyName, value), ieq(propertyName, value) and
* ne(propertyName, value) have no effect when the value is null. The expression factory adds a NoopExpression
* which will add "1=1" into the SQL rather than "is null".
* </p>
*/
public void setExpressionEqualsWithNullAsNoop(boolean expressionEqualsWithNullAsNoop) {
This applies to eq(), ne() and ieq().