Class NullExpression

java.lang.Object
com.groiss.store.NullExpression

public class NullExpression extends Object
Convenience wrapper for construction of SQL expressions with bind parameters which may be null.

Due to the three-valued logic in SQL, the two expressions "a = ?" and "a <> ?" would never be true but rather "UNKNOWN" when the value substituted for the parameter is null. Comparisons with null are done properly with the IS NULL operator like "a IS NULL" or "NOT a IS NULL".

NullExpression instances can be used to

  • construct the appropriate part of the where clause (a = ? when the value is not null and a Is Null when the value is null)
  • and in the Object[] of the bind variables to achieve conditional binding.
Example:
  NullExpression ne1 = NullExpression.wrap("a1",v1);
  NullExpression ne2 = NullExpression.wrap("a2",v2);
  Store.getInstance().list(classname,
            "a0 = ? and "+ne1+" and not "+ne2",v0,ne1,ne2);
  
If v1 != null and v2 == null, then the resulting where clause would be
   a0 = ? and a1 = ? and NOT a2 Is NULL
  
,the parameters for a0 and a1 will be set, no parameter will be used for a2.

Note that usage for parameters in value lists of inserts and "set column = value" assignments of update statements is not intended (see Nil for this).

  • Method Details

    • toString

      public String toString()
      Constructs the appropriate expression depending on the value of getValue(). If getValue() is null, then the returned string will be "attributeName IS Null", else it will be "attributeName = ?".
      Overrides:
      toString in class Object
      See Also:
    • getValue

      public Object getValue()
      Return the wrapped value.
      Returns:
      the wrapped value
    • wrap

      public static NullExpression wrap(String attributeName, Object value)
      Wrap up an attribute name and a value for null expression construction.
      Parameters:
      attributeName - the name of the attribute
      value - the value to be wrapped (can be null)
      Returns:
      the wrapper