Search in sources :

Example 11 with Function

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

the class TestSubstringFunctionModifier method helpTestMod.

public void helpTestMod(Expression[] args, String expectedStr) throws Exception {
    Function func = // $NON-NLS-1$
    LANG_FACTORY.createFunction(// $NON-NLS-1$
    "substring", Arrays.asList(args), TypeFacility.RUNTIME_TYPES.STRING);
    OracleExecutionFactory trans = new OracleExecutionFactory();
    trans.start();
    SQLConversionVisitor sqlVisitor = trans.getSQLConversionVisitor();
    sqlVisitor.append(func);
    assertEquals(expectedStr, sqlVisitor.toString());
}
Also used : Function(org.teiid.language.Function) SQLConversionVisitor(org.teiid.translator.jdbc.SQLConversionVisitor)

Example 12 with Function

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

the class TestEscapeSyntaxModifier method testEscape.

public void testEscape() {
    // $NON-NLS-1$
    Literal arg1 = CommandBuilder.getLanuageFactory().createLiteral("arg1", String.class);
    // $NON-NLS-1$
    Literal arg2 = CommandBuilder.getLanuageFactory().createLiteral("arg2", String.class);
    // $NON-NLS-1$
    Function func = CommandBuilder.getLanuageFactory().createFunction("concat", Arrays.asList(arg1, arg2), Integer.class);
    helpTest(func, "{fn concat('arg1', 'arg2')}");
}
Also used : Function(org.teiid.language.Function) Literal(org.teiid.language.Literal)

Example 13 with Function

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

the class TestExtractFunctionModifier method helpTestMod.

public void helpTestMod(Expression c, String expectedStr, String target) throws Exception {
    Function func = LANG_FACTORY.createFunction(target, Arrays.asList(c), Integer.class);
    ExtractFunctionModifier mod = new ExtractFunctionModifier();
    JDBCExecutionFactory trans = new JDBCExecutionFactory();
    trans.registerFunctionModifier(target, mod);
    trans.start();
    SQLConversionVisitor sqlVisitor = trans.getSQLConversionVisitor();
    sqlVisitor.append(func);
    assertEquals(expectedStr, sqlVisitor.toString());
}
Also used : Function(org.teiid.language.Function)

Example 14 with Function

use of org.teiid.language.Function 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 15 with Function

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

the class SybaseExecutionFactory method handleTimeConversions.

private void handleTimeConversions() {
    if (!hasTimeType()) {
        // $NON-NLS-1$
        convertModifier.addTypeMapping("datetime", FunctionModifier.DATE, FunctionModifier.TIME, FunctionModifier.TIMESTAMP);
        convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.TIME, new FunctionModifier() {

            @Override
            public List<?> translate(Function function) {
                List<Object> result = new ArrayList<Object>();
                // $NON-NLS-1$
                result.add("cast(");
                boolean needsEnd = false;
                if (!nullPlusNonNullIsNull() && !ConcatFunctionModifier.isNotNull(function.getParameters().get(0))) {
                    // $NON-NLS-1$
                    result.add("CASE WHEN ");
                    result.add(function.getParameters().get(0));
                    // $NON-NLS-1$
                    result.add(" IS NOT NULL THEN ");
                    needsEnd = true;
                }
                // $NON-NLS-1$
                result.add("'1970-01-01 ' + ");
                result.addAll(convertTimeToString(function));
                if (needsEnd) {
                    // $NON-NLS-1$
                    result.add(" END");
                }
                // $NON-NLS-1$
                result.add(" AS datetime)");
                return result;
            }
        });
    } else {
        // $NON-NLS-1$
        convertModifier.addTypeMapping("datetime", FunctionModifier.DATE, FunctionModifier.TIMESTAMP);
        // $NON-NLS-1$
        convertModifier.addTypeMapping("time", FunctionModifier.TIME);
    }
}
Also used : 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) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Aggregations

Function (org.teiid.language.Function)173 Test (org.junit.Test)127 BigInteger (java.math.BigInteger)36 ArrayList (java.util.ArrayList)16 Literal (org.teiid.language.Literal)15 BigDecimal (java.math.BigDecimal)14 List (java.util.List)13 Expression (org.teiid.language.Expression)13 AliasModifier (org.teiid.translator.jdbc.AliasModifier)12 FunctionModifier (org.teiid.translator.jdbc.FunctionModifier)12 SQLConversionVisitor (org.teiid.translator.jdbc.SQLConversionVisitor)9 LinkedList (java.util.LinkedList)3 ColumnReference (org.teiid.language.ColumnReference)3 Comparison (org.teiid.language.Comparison)3 SearchedCase (org.teiid.language.SearchedCase)3 EscapeSyntaxModifier (org.teiid.translator.jdbc.EscapeSyntaxModifier)3 ModFunctionModifier (org.teiid.translator.jdbc.ModFunctionModifier)3 ConcatFunctionModifier (org.teiid.translator.jdbc.oracle.ConcatFunctionModifier)3 Timestamp (java.sql.Timestamp)2 AggregateFunction (org.teiid.language.AggregateFunction)2