Package com.groiss.store
Class NullExpression
java.lang.Object
com.groiss.store.NullExpression
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 anda Is Null
when the value is null) - and in the
Object[]
of the bind variables to achieve conditional binding.
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
,the parameters fora0 = ? and a1 = ? and NOT a2 Is NULL
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 Summary
Modifier and TypeMethodDescriptiongetValue()
Return the wrapped value.toString()
Constructs the appropriate expression depending on the value of getValue().static NullExpression
Wrap up an attribute name and a value for null expression construction.
-
Method Details
-
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 = ?". -
getValue
Return the wrapped value.- Returns:
- the wrapped value
-
wrap
Wrap up an attribute name and a value for null expression construction.- Parameters:
attributeName
- the name of the attributevalue
- the value to be wrapped (can be null)- Returns:
- the wrapper
-