/** * Implements a cast from any Java primitive to a nullable Java primitive as a simple * assignment. i.e. * * <pre> * [NullablePrimitiveType] lhs; * lhs.[nullIndicator] = ...; * if (! lhs.[nullIndicator]) { * // check overflow ... * // round ... * lhs.[value] = ...; * } * </pre> */ private Expression castPrimitiveToNullablePrimitive() { ensureLhs(); boolean nullableSource = rhsType.isNullable(); Expression rhsIsNull; if (nullableSource) { rhsIsNull = getNullIndicator(rhsExp); rhsExp = getValue(rhsType, rhsExp); } else { rhsIsNull = Literal.constantFalse(); } addStatement(assign(getNullIndicator(lhsExp), rhsIsNull)); StatementList setValueBlock = new StatementList(); StatementList oldList = borrowStmtList(setValueBlock); try { checkOverflow(); roundAsNeeded(); addStatement(assign(getValue(lhsType, lhsExp), new CastExpression(getLhsClass(), rhsExp))); } finally { returnStmtList(oldList); } if (nullableSource) { addStatement(new IfStatement(not(getNullIndicator(lhsExp)), setValueBlock)); } else { addStatementList(setValueBlock); } return lhsExp; }
/** * Gets the right hand expression as a valid value to be assigned to the left hand side. Usually * returns the original rhs. However, if the lhs is of a primitive type, and the rhs is an * explicit null, returns a primitive value instead. */ private Expression rhsAsValue() { if (SqlTypeUtil.isJavaPrimitive(lhsType) && (rhsType.getSqlTypeName() == SqlTypeName.NULL)) { if (lhsType.getSqlTypeName() == SqlTypeName.BOOLEAN) { return Literal.constantFalse(); } else { return Literal.constantZero(); } } return rhsExp; }