/** * Creates an instance. * * @param variable the variable to determine whether to include the AND on, not null * @param matchValue the value to match, null to match on existence */ ConditionalSqlFragment(String variable, String matchValue) { _variable = extractVariableName(variable); _matchValue = matchValue; }
@Override public String toString() { return getClass().getSimpleName() + ":" + _variable + " " + getFragments(); }
@Override void toSQL(StringBuilder buf, SqlFragments fragments, SqlParams params, int[] loopIndex) { if (isMatch(params, loopIndex)) { super.toSQL(buf, fragments, params, loopIndex); } }
boolean isMatch(SqlParams params, int[] loopIndex) { String var = applyLoopIndex(_variable, loopIndex); if (params.contains(var) == false) { return false; } Object value = params.get(var); if (value == null) { return false; } if (_matchValue != null) { return _matchValue.equalsIgnoreCase(value.toString()); } if (value instanceof Boolean) { return ((Boolean) value).booleanValue(); } return true; }
@Override void toSQL(StringBuilder buf, SqlFragments fragments, SqlParams params, int[] loopIndex) { if (isMatch(params, loopIndex)) { if (endsWith(buf, " WHERE ") == false && endsWith(buf, " AND ") == false) { buf.append("AND "); } super.toSQL(buf, fragments, params, loopIndex); } }
@Override void toSQL(StringBuilder buf, SqlFragments fragments, SqlParams params, int[] loopIndex) { if (isMatch(params, loopIndex)) { if (endsWith(buf, " WHERE ") == false && endsWith(buf, " OR ") == false) { buf.append("OR "); } super.toSQL(buf, fragments, params, loopIndex); } }