this.booleanNumeric = true;
//number -> boolean
this.addTypeConversion(new FunctionModifier() {
@Override
public List<?> translate(Function function) {
Expression stringValue = function.getParameters().get(0);
return Arrays.asList("CASE WHEN ", stringValue, " = 0 THEN 0 WHEN ", stringValue, " IS NOT NULL THEN 1 END"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
}, FunctionModifier.BOOLEAN);
this.addConvert(FunctionModifier.BOOLEAN, FunctionModifier.STRING, new FunctionModifier() {
@Override
public List<?> translate(Function function) {
Expression booleanValue = function.getParameters().get(0);
if (booleanValue instanceof Function) {
Function nested = (Function)booleanValue;
if (nested.getName().equalsIgnoreCase("convert") && Number.class.isAssignableFrom(nested.getParameters().get(0).getType())) { //$NON-NLS-1$
booleanValue = nested.getParameters().get(0);
}
}
if (!booleanNullable) {
return Arrays.asList("CASE WHEN ", booleanValue, " = 0 THEN 'false' ELSE 'true' END"); //$NON-NLS-1$ //$NON-NLS-2$
}
return Arrays.asList("CASE WHEN ", booleanValue, " = 0 THEN 'false' WHEN ", booleanValue, " IS NOT NULL THEN 'true' END"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
});
this.addConvert(FunctionModifier.STRING, FunctionModifier.BOOLEAN, new FunctionModifier() {
@Override
public List<?> translate(Function function) {
Expression stringValue = function.getParameters().get(0);
return Arrays.asList("CASE WHEN ", stringValue, " IN ('false', '0') THEN 0 WHEN ", stringValue, " IS NOT NULL THEN 1 END"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
});
}