ebean icon indicating copy to clipboard operation
ebean copied to clipboard

ENH: Add ServerConfig expressionEqualsWithNullAsNoop - Can eq ne gt ... methods in ExpressionList treat null value as 1=1 ?

Open archer-christ opened this issue 11 years ago • 4 comments

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();

archer-christ avatar Jul 29 '15 04:07 archer-christ

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.

archer-christ avatar Jul 29 '15 04:07 archer-christ

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 .

rbygrave avatar Jul 29 '15 08:07 rbygrave

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

archer-christ avatar Jul 29 '15 09:07 archer-christ

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().

rbygrave avatar Jul 29 '15 18:07 rbygrave