public void helpTest(Expression srcExpression, String tgtType, String expectedExpression) throws Exception { Function func = LANG_FACTORY.createFunction( "convert", //$NON-NLS-1$ Arrays.asList(srcExpression, LANG_FACTORY.createLiteral(tgtType, String.class)), TypeFacility.getDataTypeClass(tgtType)); assertEquals( "Error converting from " + srcExpression.getType() + " to " + tgtType, expectedExpression, helpGetString(func)); }
@Test public void testStringToTimestamp() throws Exception { helpTest( LANG_FACTORY.createLiteral("2004-06-29 23:59:59.987", String.class), "timestamp", "to_timestamp('2004-06-29 23:59:59.987', 'YYYY-MM-DD HH24:MI:SS.MS')"); }
@Test public void testStringToTime() throws Exception { helpTest( LANG_FACTORY.createLiteral("23:59:59", String.class), "time", "to_timestamp('23:59:59', 'HH24:MI:SS')"); }
@Test public void testBigIntegerToBoolean() throws Exception { helpTest( LANG_FACTORY.createLiteral(new BigInteger("1"), BigInteger.class), "boolean", "CASE WHEN 1 = 0 THEN '0' WHEN 1 IS NOT NULL THEN '1' END"); }
@Test public void testStringToBigInteger() throws Exception { helpTest( LANG_FACTORY.createLiteral("5", String.class), "biginteger", "cast('5' AS numeric(38))"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ }
@Test public void testShortToString() throws Exception { helpTest( LANG_FACTORY.createLiteral(new Short((short) 1), Short.class), "string", "cast(1 AS varchar(4000))"); }
@Test public void testByteToBoolean() throws Exception { helpTest( LANG_FACTORY.createLiteral(new Byte((byte) 1), Byte.class), "boolean", "CASE WHEN 1 = 0 THEN '0' WHEN 1 IS NOT NULL THEN '1' END"); }
@Test public void testByteToString() throws Exception { helpTest( LANG_FACTORY.createLiteral(new Byte((byte) 1), Byte.class), "string", "cast(1 AS varchar(4000))"); }
@Test public void testBooleanToBigDecimal() throws Exception { helpTest( LANG_FACTORY.createLiteral(Boolean.FALSE, Boolean.class), "bigdecimal", "(CASE WHEN 0 IN ( '0', 'FALSE') THEN 0 WHEN 0 IS NOT NULL THEN 1 END)"); }
@Test public void testShortToBoolean() throws Exception { helpTest( LANG_FACTORY.createLiteral(new Short((short) 1), Short.class), "boolean", "CASE WHEN 1 = 0 THEN '0' WHEN 1 IS NOT NULL THEN '1' END"); }
@Test public void testFloatToString() throws Exception { helpTest( LANG_FACTORY.createLiteral(new Float(1.2f), Float.class), "string", "cast(1.2 AS varchar(4000))"); }
@Test public void testFloatToBoolean() throws Exception { helpTest( LANG_FACTORY.createLiteral(new Float(1.2f), Float.class), "boolean", "CASE WHEN 1.2 = 0 THEN '0' WHEN 1.2 IS NOT NULL THEN '1' END"); }
@Test public void testBigDecimalToString() throws Exception { helpTest( LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "string", "cast(1.0 AS varchar(4000))"); }
@Test public void testStringToBoolean() throws Exception { helpTest( LANG_FACTORY.createLiteral("5", String.class), "boolean", "CASE WHEN '5' IN ('false', '0') THEN '0' WHEN '5' IS NOT NULL THEN '1' END"); }
@Test public void testBigDecimalToBoolean() throws Exception { helpTest( LANG_FACTORY.createLiteral(new BigDecimal("1.0"), BigDecimal.class), "boolean", "CASE WHEN 1.0 = 0 THEN '0' WHEN 1.0 IS NOT NULL THEN '1' END"); }
@Test public void testDoubleToString() throws Exception { helpTest( LANG_FACTORY.createLiteral(new Double(1.2), Double.class), "string", "cast(1.2 AS varchar(4000))"); }
@Test public void testDoubleToBoolean() throws Exception { helpTest( LANG_FACTORY.createLiteral(new Double(1.2), Double.class), "boolean", "CASE WHEN 1.2 = 0 THEN '0' WHEN 1.2 IS NOT NULL THEN '1' END"); }
@Test public void testBooleanToString() throws Exception { helpTest( LANG_FACTORY.createLiteral(Boolean.TRUE, Boolean.class), "string", "CASE WHEN 1 = '0' THEN 'false' WHEN 1 IS NOT NULL THEN 'true' END"); }
@Test public void testStringToDate() throws Exception { helpTest( LANG_FACTORY.createLiteral("2004-06-29", String.class), "date", "to_date('2004-06-29', 'YYYY-MM-DD')"); }
@Test public void testBigIntegerToString() throws Exception { helpTest( LANG_FACTORY.createLiteral(new BigInteger("1"), BigInteger.class), "string", "cast(1 AS varchar(4000))"); }
@Test public void testLongToBoolean() throws Exception { helpTest( LANG_FACTORY.createLiteral(new Long(1), Long.class), "boolean", "CASE WHEN 1 = 0 THEN '0' WHEN 1 IS NOT NULL THEN '1' END"); }
@Test public void testStringToInteger() throws Exception { helpTest(LANG_FACTORY.createLiteral("5", String.class), "integer", "cast('5' AS integer)"); }
@Test public void testStringToShort() throws Exception { helpTest(LANG_FACTORY.createLiteral("5", String.class), "short", "cast('5' AS smallint)"); }
@Test public void testStringToByte() throws Exception { helpTest(LANG_FACTORY.createLiteral("5", String.class), "byte", "cast('5' AS byteint)"); }
// Source = STRING @Test public void testStringToChar() throws Exception { helpTest(LANG_FACTORY.createLiteral("5", String.class), "char", "cast('5' AS char(1))"); }
@Test public void testStringToBigDecimal() throws Exception { helpTest( LANG_FACTORY.createLiteral("5", String.class), "bigdecimal", "cast('5' AS numeric(38,18))"); }
@Test public void testStringToLong() throws Exception { helpTest(LANG_FACTORY.createLiteral("5", String.class), "long", "cast('5' AS bigint)"); }
@Test public void testLongToString() throws Exception { helpTest( LANG_FACTORY.createLiteral(new Long(1), Long.class), "string", "cast(1 AS varchar(4000))"); }
@Test public void testStringToDouble() throws Exception { helpTest(LANG_FACTORY.createLiteral("5", String.class), "double", "cast('5' AS double)"); }
@Test public void testCharToString() throws Exception { helpTest(LANG_FACTORY.createLiteral(new Character('5'), Character.class), "string", "'5'"); }