Search in sources :

Example 41 with Literal

use of org.teiid.language.Literal in project teiid by teiid.

the class TestExtractFunctionModifier method test2.

public void test2() throws Exception {
    Literal arg1 = LANG_FACTORY.createLiteral(TimestampUtil.createTimestamp(104, 0, 21, 17, 5, 0, 0), Timestamp.class);
    // $NON-NLS-1$ //$NON-NLS-2$
    helpTestMod(arg1, "EXTRACT(MONTH FROM {ts '2004-01-21 17:05:00.0'})", "month");
}
Also used : Literal(org.teiid.language.Literal)

Example 42 with Literal

use of org.teiid.language.Literal in project teiid by teiid.

the class SybaseExecutionFactory method start.

public void start() throws TranslatorException {
    super.start();
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.MOD, new ModFunctionModifier("%", getLanguageFactory()));
    if (nullPlusNonNullIsNull()) {
        // $NON-NLS-1$
        registerFunctionModifier(SourceSystemFunctions.CONCAT, new AliasModifier("+"));
    } else {
        registerFunctionModifier(SourceSystemFunctions.CONCAT, new ConcatFunctionModifier(getLanguageFactory()) {

            @Override
            public List<?> translate(Function function) {
                // $NON-NLS-1$
                function.setName("+");
                return super.translate(function);
            }
        });
    }
    registerFunctionModifier(SourceSystemFunctions.LPAD, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            List<Expression> params = function.getParameters();
            return // $NON-NLS-1$
            Arrays.asList(// $NON-NLS-1$
            "RIGHT(REPLICATE(", // $NON-NLS-1$ //$NON-NLS-2$
            params.size() > 2 ? params.get(2) : new Literal(" ", TypeFacility.RUNTIME_TYPES.STRING), // $NON-NLS-1$ //$NON-NLS-2$
            ", ", params.get(1), ") + ", params.get(0), ", ", params.get(1), // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            ")");
        }
    });
    registerFunctionModifier(SourceSystemFunctions.RPAD, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            List<Expression> params = function.getParameters();
            return // $NON-NLS-1$ //$NON-NLS-2$
            Arrays.asList(// $NON-NLS-1$ //$NON-NLS-2$
            "LEFT(", // $NON-NLS-1$ //$NON-NLS-2$
            params.get(0), // $NON-NLS-1$ //$NON-NLS-2$
            " + REPLICATE(", // $NON-NLS-1$ //$NON-NLS-2$
            params.size() > 2 ? params.get(2) : new Literal(" ", TypeFacility.RUNTIME_TYPES.STRING), // $NON-NLS-1$ //$NON-NLS-2$
            ", ", params.get(1), "), ", params.get(1), // $NON-NLS-1$ //$NON-NLS-2$
            ")");
        }
    });
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LCASE, new AliasModifier("lower"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.IFNULL, new AliasModifier("isnull"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.UCASE, new AliasModifier("upper"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.REPEAT, new AliasModifier("replicate"));
    registerFunctionModifier(SourceSystemFunctions.SUBSTRING, new SubstringFunctionModifier(getLanguageFactory()));
    registerFunctionModifier(SourceSystemFunctions.DAYNAME, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.MONTHNAME, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.DAYOFWEEK, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.DAYOFYEAR, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.DAYOFMONTH, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.HOUR, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.MINUTE, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.QUARTER, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.SECOND, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.WEEK, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.LENGTH, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.ATAN2, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.TIMESTAMPADD, new EscapeSyntaxModifier() {

        @Override
        public List<?> translate(Function function) {
            if (!isFracSeconds(function)) {
                return super.translate(function);
            }
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            return Arrays.asList("dateadd(millisecond, ", function.getParameters().get(1), "/1000000, ", function.getParameters().get(2), ")");
        }
    });
    registerFunctionModifier(SourceSystemFunctions.TIMESTAMPDIFF, new EscapeSyntaxModifier() {

        @Override
        public List<?> translate(Function function) {
            if (!isFracSeconds(function)) {
                return super.translate(function);
            }
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            return Arrays.asList("datediff(millisecond, ", function.getParameters().get(1), ",", function.getParameters().get(2), ")*1000000");
        }
    });
    // add in type conversion
    convertModifier.setBooleanNullable(booleanNullable());
    // boolean isn't treated as bit, since it doesn't support null
    // byte is treated as smallint, since tinyint is unsigned
    // $NON-NLS-1$
    convertModifier.addTypeMapping("smallint", FunctionModifier.BYTE, FunctionModifier.SHORT);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("int", FunctionModifier.INTEGER);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("numeric(19,0)", FunctionModifier.LONG);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("real", FunctionModifier.FLOAT);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("double precision", FunctionModifier.DOUBLE);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("numeric(38, 0)", FunctionModifier.BIGINTEGER);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("numeric(38, 19)", FunctionModifier.BIGDECIMAL);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("char(1)", FunctionModifier.CHAR);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("varchar(4000)", FunctionModifier.STRING);
    convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.DATE, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            List<Object> result = new ArrayList<Object>();
            // $NON-NLS-1$
            result.add("cast(");
            result.addAll(convertDateToString(function));
            // $NON-NLS-1$
            result.add(" AS datetime)");
            return result;
        }
    });
    convertModifier.addConvert(FunctionModifier.TIME, FunctionModifier.STRING, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            return convertTimeToString(function);
        }
    });
    convertModifier.addConvert(FunctionModifier.DATE, FunctionModifier.STRING, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            return convertDateToString(function);
        }
    });
    convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.STRING, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            return convertTimestampToString(function);
        }
    });
    convertModifier.addNumericBooleanConversions();
    registerFunctionModifier(SourceSystemFunctions.CONVERT, convertModifier);
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.PARSETIMESTAMP, new SybaseFormatFunctionModifier("CONVERT(DATETIME, ", formatMap));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.FORMATTIMESTAMP, new SybaseFormatFunctionModifier("CONVERT(VARCHAR, ", formatMap));
}
Also used : ModFunctionModifier(org.teiid.translator.jdbc.ModFunctionModifier) EscapeSyntaxModifier(org.teiid.translator.jdbc.EscapeSyntaxModifier) Function(org.teiid.language.Function) ConcatFunctionModifier(org.teiid.translator.jdbc.oracle.ConcatFunctionModifier) FunctionModifier(org.teiid.translator.jdbc.FunctionModifier) ParseFormatFunctionModifier(org.teiid.translator.jdbc.ParseFormatFunctionModifier) ModFunctionModifier(org.teiid.translator.jdbc.ModFunctionModifier) Literal(org.teiid.language.Literal) AliasModifier(org.teiid.translator.jdbc.AliasModifier) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ConcatFunctionModifier(org.teiid.translator.jdbc.oracle.ConcatFunctionModifier)

Example 43 with Literal

use of org.teiid.language.Literal in project teiid by teiid.

the class TestSybaseIQConvertModifier method testTimestampToTime.

@Test
public void testTimestampToTime() throws Exception {
    Literal c = LANG_FACTORY.createLiteral(TimestampUtil.createTimestamp(89, 2, 3, 7, 8, 12, 99999), Timestamp.class);
    Function func = // $NON-NLS-1$
    LANG_FACTORY.createFunction(// $NON-NLS-1$
    "convert", new Expression[] { c, // $NON-NLS-1$
    LANG_FACTORY.createLiteral("time", String.class) }, java.sql.Time.class);
    // $NON-NLS-1$
    helpGetString1(func, "cast(CAST('1989-03-03 07:08:12.0' AS TIMESTAMP) AS time)");
}
Also used : Function(org.teiid.language.Function) Literal(org.teiid.language.Literal) Test(org.junit.Test)

Example 44 with Literal

use of org.teiid.language.Literal in project teiid by teiid.

the class TestLog10FunctionModifier method testModifier.

public void testModifier() {
    Literal arg = LANG_FACTORY.createLiteral(new Double(5.2), Double.class);
    // $NON-NLS-1$
    Function func = LANG_FACTORY.createFunction("log10", Arrays.asList(arg), Double.class);
    Log10FunctionModifier modifier = new Log10FunctionModifier(LANG_FACTORY);
    modifier.translate(func);
    // $NON-NLS-1$
    assertEquals("log", func.getName());
    assertEquals(Double.class, func.getType());
    List<Expression> outArgs = func.getParameters();
    assertEquals(2, outArgs.size());
    assertEquals(arg, outArgs.get(1));
    assertTrue(outArgs.get(1) instanceof Literal);
    Literal newArg = (Literal) outArgs.get(0);
    assertEquals(Integer.class, newArg.getType());
    assertEquals(new Integer(10), newArg.getValue());
    // $NON-NLS-1$
    assertEquals("log(10, 5.2)", SQLStringVisitor.getSQLString(func));
}
Also used : Function(org.teiid.language.Function) Log10FunctionModifier(org.teiid.translator.jdbc.hana.Log10FunctionModifier) Expression(org.teiid.language.Expression) Literal(org.teiid.language.Literal)

Example 45 with Literal

use of org.teiid.language.Literal in project teiid by teiid.

the class TestSybaseConvertModifier method testTimestampToDate.

@Test
public void testTimestampToDate() throws Exception {
    Literal c = LANG_FACTORY.createLiteral(TimestampUtil.createTimestamp(89, 2, 3, 7, 8, 12, 99999), Timestamp.class);
    Function func = // $NON-NLS-1$
    LANG_FACTORY.createFunction(// $NON-NLS-1$
    "convert", new Expression[] { c, // $NON-NLS-1$
    LANG_FACTORY.createLiteral("date", String.class) }, java.sql.Date.class);
    // $NON-NLS-1$
    helpGetString1(func, "cast(stuff(stuff(convert(varchar, CAST('1989-03-03 07:08:12.0' AS DATETIME), 102), 5, 1, '-'), 8, 1, '-') AS datetime)");
}
Also used : Function(org.teiid.language.Function) Literal(org.teiid.language.Literal) Test(org.junit.Test)

Aggregations

Literal (org.teiid.language.Literal)82 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)18 Expression (org.teiid.language.Expression)17 TranslatorException (org.teiid.translator.TranslatorException)16 Function (org.teiid.language.Function)15 Argument (org.teiid.language.Argument)12 ColumnReference (org.teiid.language.ColumnReference)10 Column (org.teiid.metadata.Column)10 Comparison (org.teiid.language.Comparison)9 ExpressionValueSource (org.teiid.language.ExpressionValueSource)7 List (java.util.List)5 Call (org.teiid.language.Call)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 BinaryWSProcedureExecution (org.teiid.translator.ws.BinaryWSProcedureExecution)4 DBCollection (com.mongodb.DBCollection)3 Timestamp (java.sql.Timestamp)3 Array (org.teiid.language.Array)3 Command (org.teiid.language.Command)3 Insert (org.teiid.language.Insert)3