Search in sources :

Example 26 with Expression

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

the class TestJDBCUpdateExecution method testPreparedInsertWithGeometry.

@Test
public void testPreparedInsertWithGeometry() throws Exception {
    // $NON-NLS-1$
    Insert command = (Insert) TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "insert into cola_markets(name,shape) values('foo124', ST_GeomFromText('POINT (300 100)', 8307))");
    Parameter param = new Parameter();
    param.setType(DataTypeManager.DefaultDataClasses.STRING);
    param.setValueIndex(0);
    List<Expression> values = ((ExpressionValueSource) command.getValueSource()).getValues();
    values.set(0, param);
    param = new Parameter();
    param.setType(DataTypeManager.DefaultDataClasses.GEOMETRY);
    param.setValueIndex(1);
    values.set(1, param);
    GeometryType value = new GeometryType();
    value.setSrid(123);
    command.setParameterValues(Arrays.asList(Arrays.asList("a", value)).iterator());
    Connection connection = Mockito.mock(Connection.class);
    PreparedStatement p = Mockito.mock(PreparedStatement.class);
    Mockito.stub(p.executeBatch()).toReturn(new int[] { 1 });
    // $NON-NLS-1$
    Mockito.stub(connection.prepareStatement("INSERT INTO COLA_MARKETS (NAME, SHAPE) VALUES (?, st_geomfromwkb(?, ?))")).toReturn(p);
    JDBCExecutionFactory config = new JDBCExecutionFactory();
    JDBCUpdateExecution updateExecution = new JDBCUpdateExecution(command, connection, new FakeExecutionContextImpl(), config);
    updateExecution.execute();
    Mockito.verify(p, Mockito.times(1)).addBatch();
    Mockito.verify(p, Mockito.times(1)).setObject(1, "a", Types.VARCHAR);
    Mockito.verify(p, Mockito.times(1)).setInt(3, 123);
}
Also used : GeometryType(org.teiid.core.types.GeometryType) FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) Expression(org.teiid.language.Expression) Connection(java.sql.Connection) Parameter(org.teiid.language.Parameter) PreparedStatement(java.sql.PreparedStatement) Insert(org.teiid.language.Insert) ExpressionValueSource(org.teiid.language.ExpressionValueSource) Test(org.junit.Test)

Example 27 with Expression

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

the class TestJDBCUpdateExecution method testBulkUpdate.

@Test
public void testBulkUpdate() throws Exception {
    // $NON-NLS-1$
    Insert command = (Insert) TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "insert into BQT1.SmallA (IntKey) values (1)");
    Parameter param = new Parameter();
    param.setType(Integer.class);
    param.setValueIndex(0);
    ExpressionValueSource evs = new ExpressionValueSource(Arrays.asList((Expression) param));
    command.setValueSource(evs);
    List<List<?>> vals = new ArrayList<List<?>>();
    for (int i = 0; i < 8; i++) {
        vals.add(Arrays.asList(i));
    }
    command.setParameterValues(vals.iterator());
    Connection connection = Mockito.mock(Connection.class);
    PreparedStatement p = Mockito.mock(PreparedStatement.class);
    Mockito.stub(p.executeBatch()).toReturn(new int[] { 1, 1 });
    // $NON-NLS-1$
    Mockito.stub(connection.prepareStatement("INSERT INTO SmallA (IntKey) VALUES (?)")).toReturn(p);
    JDBCExecutionFactory config = new JDBCExecutionFactory();
    config.setMaxPreparedInsertBatchSize(2);
    ResultSet r = Mockito.mock(ResultSet.class);
    ResultSetMetaData rs = Mockito.mock(ResultSetMetaData.class);
    Mockito.stub(r.getMetaData()).toReturn(rs);
    Mockito.stub(p.getGeneratedKeys()).toReturn(r);
    FakeExecutionContextImpl context = new FakeExecutionContextImpl();
    ((org.teiid.query.util.CommandContext) context.getCommandContext()).setReturnAutoGeneratedKeys(Collections.EMPTY_LIST);
    JDBCUpdateExecution updateExecution = new JDBCUpdateExecution(command, connection, context, config);
    updateExecution.execute();
    assertArrayEquals(new int[] { 1, 1, 1, 1, 1, 1, 1, 1 }, updateExecution.getUpdateCounts());
}
Also used : FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Insert(org.teiid.language.Insert) ResultSetMetaData(java.sql.ResultSetMetaData) Expression(org.teiid.language.Expression) ResultSet(java.sql.ResultSet) Parameter(org.teiid.language.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionValueSource(org.teiid.language.ExpressionValueSource) Test(org.junit.Test)

Example 28 with Expression

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

the class NetezzaExecutionFactory method start.

public void start() throws TranslatorException {
    super.start();
    // STRING FUNCTION MODIFIERS
    // //////////////////////////////////
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CHAR, new AliasModifier("chr"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LCASE, new AliasModifier("lower"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.UCASE, new AliasModifier("upper"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LOCATE, new LocateFunctionModifier(getLanguageFactory(), "INSTR", true));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CONCAT, new AliasModifier("||"));
    // /NUMERIC FUNCTION MODIFIERS
    // //////////////////////////////////
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CEILING, new AliasModifier("ceil"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.POWER, new AliasModifier("pow"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LOG, new AliasModifier("LN"));
    // /BIT FUNCTION MODIFIERS
    // //////////////////////////////////
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.BITAND, new AliasModifier("int4and"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.BITNOT, new AliasModifier("int4not"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.BITOR, new AliasModifier("int4or"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.BITXOR, new AliasModifier("int4xor"));
    // DATE FUNCTION MODIFIERS
    // ////////////////////////////////////////
    registerFunctionModifier(SourceSystemFunctions.YEAR, new ExtractFunctionModifier());
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.DAYOFYEAR, new ExtractModifier("DOY"));
    registerFunctionModifier(SourceSystemFunctions.QUARTER, new ExtractFunctionModifier());
    registerFunctionModifier(SourceSystemFunctions.MONTH, new ExtractFunctionModifier());
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.DAYOFMONTH, new ExtractModifier("DAY"));
    registerFunctionModifier(SourceSystemFunctions.WEEK, new ExtractFunctionModifier());
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.DAYOFWEEK, new ExtractModifier("DOW"));
    registerFunctionModifier(SourceSystemFunctions.HOUR, new ExtractFunctionModifier());
    registerFunctionModifier(SourceSystemFunctions.MINUTE, new ExtractFunctionModifier());
    registerFunctionModifier(SourceSystemFunctions.SECOND, new ExtractFunctionModifier());
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CURDATE, new AliasModifier("CURRENT_DATE"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CURTIME, new AliasModifier("CURRENT_TIME"));
    // SYSTEM FUNCTIONS
    // //////////////////////////////////
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.IFNULL, new AliasModifier("NVL"));
    // DATA TYPE CONVERSION
    // /////////////////////////////////////////
    ConvertModifier convertModifier = new ConvertModifier();
    // $NON-NLS-1$
    convertModifier.addTypeMapping("char(1)", FunctionModifier.CHAR);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("byteint", FunctionModifier.BYTE);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("smallint", FunctionModifier.SHORT);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("bigint", FunctionModifier.LONG);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("numeric(38)", FunctionModifier.BIGINTEGER);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("numeric(38,18)", FunctionModifier.BIGDECIMAL);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("varchar(4000)", FunctionModifier.STRING);
    // convertModifier.addTypeMapping("nvarchar(5)", FunctionModifier.BOOLEAN);
    // /NO BOOLEAN, INTEGER, FLOAT, DATE, TIME, TIMESTAMP, as they are directly available in netezza
    // /NO NULL, CLOB, BLOB, OBJECT, XML
    // /BOOLEAN--BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE, BIGINTEGER, BIGDECIMAL--AS IT DOESN'T WORK IMPLICITLY IN NETEZZA
    convertModifier.addConvert(FunctionModifier.BOOLEAN, FunctionModifier.INTEGER, new BooleanToNumericConversionModifier());
    convertModifier.addConvert(FunctionModifier.BOOLEAN, FunctionModifier.BYTE, new BooleanToNumericConversionModifier());
    convertModifier.addConvert(FunctionModifier.BOOLEAN, FunctionModifier.SHORT, new BooleanToNumericConversionModifier());
    convertModifier.addConvert(FunctionModifier.BOOLEAN, FunctionModifier.LONG, new BooleanToNumericConversionModifier());
    convertModifier.addConvert(FunctionModifier.BOOLEAN, FunctionModifier.FLOAT, new BooleanToNumericConversionModifier());
    convertModifier.addConvert(FunctionModifier.BOOLEAN, FunctionModifier.DOUBLE, new BooleanToNumericConversionModifier());
    convertModifier.addConvert(FunctionModifier.BOOLEAN, FunctionModifier.BIGINTEGER, new BooleanToNumericConversionModifier());
    convertModifier.addConvert(FunctionModifier.BOOLEAN, FunctionModifier.BIGDECIMAL, new BooleanToNumericConversionModifier());
    convertModifier.addConvert(FunctionModifier.BOOLEAN, FunctionModifier.STRING, new BooleanToStringConversionModifier());
    convertModifier.addConvert(FunctionModifier.STRING, FunctionModifier.BOOLEAN, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            Expression stringValue = function.getParameters().get(0);
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            return Arrays.asList("CASE WHEN ", stringValue, " IN ('false', '0') THEN '0' WHEN ", stringValue, " IS NOT NULL THEN '1' END");
        }
    });
    convertModifier.addTypeConversion(new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            Expression stringValue = function.getParameters().get(0);
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            return Arrays.asList("CASE WHEN ", stringValue, " = 0 THEN '0' WHEN ", stringValue, " IS NOT NULL THEN '1' END");
        }
    }, FunctionModifier.BOOLEAN);
    // //////STRING TO DATATYPE CONVERSION OTHER THAN DATE/TIME
    // $NON-NLS-1$
    convertModifier.addConvert(FunctionModifier.STRING, FunctionModifier.INTEGER, new CastModifier("integer"));
    // $NON-NLS-1$
    convertModifier.addConvert(FunctionModifier.STRING, FunctionModifier.FLOAT, new CastModifier("float"));
    // $NON-NLS-1$
    convertModifier.addConvert(FunctionModifier.STRING, FunctionModifier.DOUBLE, new CastModifier("double"));
    // /// STRING --> CHAR, BYTE, SHORT, LONG, BIGI, BIGD, BOOLEAN is taken care by Type Mapping
    // /// NO conversion support for NULL, CLOB, BLOB, OBJECT, XML
    // //STRING TO DATE/TIME CONVERSION////
    // ////////////////////////////////////
    // $NON-NLS-1$
    convertModifier.addConvert(FunctionModifier.STRING, FunctionModifier.DATE, new ConvertModifier.FormatModifier("to_date", DATE_FORMAT));
    // $NON-NLS-1$
    convertModifier.addConvert(FunctionModifier.STRING, FunctionModifier.TIME, new ConvertModifier.FormatModifier("to_timestamp", TIME_FORMAT));
    // $NON-NLS-1$
    convertModifier.addConvert(FunctionModifier.STRING, FunctionModifier.TIMESTAMP, new ConvertModifier.FormatModifier("to_timestamp", TIMESTAMP_FORMAT));
    // ////DATE/TIME INTERNAL CONVERSION/////////
    // $NON-NLS-1$
    convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.TIME, new CastModifier("TIME"));
    // $NON-NLS-1$
    convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.DATE, new CastModifier("DATE"));
    // $NON-NLS-1$
    convertModifier.addConvert(FunctionModifier.DATE, FunctionModifier.TIMESTAMP, new CastModifier("TIMESTAMP"));
    // convertModifier.addConvert(FunctionModifier.TIME, FunctionModifier.TIMESTAMP, new CastModifier("TIMESTAMP")); //TIME --> TIMESTAMP --DOESN't WORK IN NETEZZA-NO FUNCTION SUPPORT
    // //DATE/TIME to STRING CONVERION////
    // ///////////////////////////////////
    // $NON-NLS-1$
    convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.STRING, new ConvertModifier.FormatModifier("to_char", TIMESTAMP_FORMAT));
    // /NO NETEZAA FUNCTION for DATE, TIME to STRING
    convertModifier.setWideningNumericImplicit(true);
    registerFunctionModifier(SourceSystemFunctions.CONVERT, convertModifier);
    if (sqlExtensionsInstalled) {
        // $NON-NLS-1$ //$NON-NLS-2$
        addPushDownFunction("netezza", "regexp_extract", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING);
        // $NON-NLS-1$ //$NON-NLS-2$
        addPushDownFunction("netezza", "regexp_extract", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.INTEGER);
        // $NON-NLS-1$ //$NON-NLS-2$
        addPushDownFunction("netezza", "regexp_extract", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.STRING);
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        addPushDownFunction("netezza", "regexp_extract_all", RUNTIME_NAMES.STRING + "[]", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING);
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        addPushDownFunction("netezza", "regexp_extract_all", RUNTIME_NAMES.STRING + "[]", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.INTEGER);
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        addPushDownFunction("netezza", "regexp_extract_all", RUNTIME_NAMES.STRING + "[]", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.STRING);
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        addPushDownFunction("netezza", "regexp_extract_all_sp", RUNTIME_NAMES.STRING + "[]", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING);
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        addPushDownFunction("netezza", "regexp_extract_all_sp", RUNTIME_NAMES.STRING + "[]", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.INTEGER);
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        addPushDownFunction("netezza", "regexp_extract_all_sp", RUNTIME_NAMES.STRING + "[]", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.STRING);
        // $NON-NLS-1$ //$NON-NLS-2$
        addPushDownFunction("netezza", "regexp_extract_sp", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.INTEGER);
        // $NON-NLS-1$ //$NON-NLS-2$
        addPushDownFunction("netezza", "regexp_extract_sp", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.STRING);
        // $NON-NLS-1$ //$NON-NLS-2$
        addPushDownFunction("netezza", "regexp_instr", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING);
        // $NON-NLS-1$ //$NON-NLS-2$
        addPushDownFunction("netezza", "regexp_instr", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.INTEGER);
        // $NON-NLS-1$ //$NON-NLS-2$
        addPushDownFunction("netezza", "regexp_instr", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.STRING);
        // $NON-NLS-1$ //$NON-NLS-2$
        addPushDownFunction("netezza", "regexp_like", RUNTIME_NAMES.BOOLEAN, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING);
        // $NON-NLS-1$ //$NON-NLS-2$
        addPushDownFunction("netezza", "regexp_like", RUNTIME_NAMES.BOOLEAN, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING);
        // $NON-NLS-1$ //$NON-NLS-2$
        addPushDownFunction("netezza", "regexp_like", RUNTIME_NAMES.BOOLEAN, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.STRING);
        // $NON-NLS-1$ //$NON-NLS-2$
        addPushDownFunction("netezza", "regexp_match_count", RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING);
        // $NON-NLS-1$ //$NON-NLS-2$
        addPushDownFunction("netezza", "regexp_match_count", RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.INTEGER);
        // $NON-NLS-1$ //$NON-NLS-2$
        addPushDownFunction("netezza", "regexp_match_count", RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.STRING);
        // $NON-NLS-1$ //$NON-NLS-2$
        addPushDownFunction("netezza", "regexp_replace", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING);
        // $NON-NLS-1$ //$NON-NLS-2$
        addPushDownFunction("netezza", "regexp_replace", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.INTEGER);
        // $NON-NLS-1$ //$NON-NLS-2$
        addPushDownFunction("netezza", "regexp_replace", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.STRING);
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        addPushDownFunction("netezza", "regexp_replace_sp", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING + "[]");
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        addPushDownFunction("netezza", "regexp_replace_sp", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING + "[]", RUNTIME_NAMES.INTEGER);
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        addPushDownFunction("netezza", "regexp_replace_sp", RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING, RUNTIME_NAMES.STRING + "[]", RUNTIME_NAMES.INTEGER, RUNTIME_NAMES.STRING);
    }
}
Also used : LocateFunctionModifier(org.teiid.translator.jdbc.LocateFunctionModifier) ExtractFunctionModifier(org.teiid.translator.jdbc.ExtractFunctionModifier) ConvertModifier(org.teiid.translator.jdbc.ConvertModifier) Function(org.teiid.language.Function) Expression(org.teiid.language.Expression) LocateFunctionModifier(org.teiid.translator.jdbc.LocateFunctionModifier) FunctionModifier(org.teiid.translator.jdbc.FunctionModifier) ExtractFunctionModifier(org.teiid.translator.jdbc.ExtractFunctionModifier) AliasModifier(org.teiid.translator.jdbc.AliasModifier) ArrayList(java.util.ArrayList) List(java.util.List)

Example 29 with Expression

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

the class LeftOrRightFunctionModifier method translate.

@Override
public List<?> translate(Function function) {
    List<Expression> args = function.getParameters();
    Function func = null;
    if (function.getName().equalsIgnoreCase("left")) {
        // $NON-NLS-1$
        func = langFactory.createFunction(SourceSystemFunctions.SUBSTRING, Arrays.asList(args.get(0), langFactory.createLiteral(Integer.valueOf(1), TypeFacility.RUNTIME_TYPES.INTEGER), args.get(1)), String.class);
    } else if (function.getName().equalsIgnoreCase("right")) {
        // $NON-NLS-1$
        Function negIndex = // $NON-NLS-1$
        langFactory.createFunction(// $NON-NLS-1$
        "*", Arrays.asList(langFactory.createLiteral(Integer.valueOf(-1), TypeFacility.RUNTIME_TYPES.INTEGER), args.get(1)), Integer.class);
        func = langFactory.createFunction(SourceSystemFunctions.SUBSTRING, Arrays.asList(args.get(0), negIndex), String.class);
    }
    return Arrays.asList(func);
}
Also used : Function(org.teiid.language.Function) Expression(org.teiid.language.Expression)

Example 30 with Expression

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

the class TestLocateFunctionModifier method helpTestLocate.

/**
 * Create an expression containing a LOCATE function using a function name of
 * <code>locateFunctionName</code> with the parameter order of
 * <code>parameterOrder</code> and a string index base of
 * <code>stringIndexBase</code> and uses the arguments <code>args</code> and
 * pass it to the <code>Translator</code>'s LOCATE function modifier and
 * compare the resulting expression to <code>expectedStr</code>.
 *
 * @param locateFunctionName the name to use for the function modifier
 * @param parameterOrder an <code>enum</code> value as defined by
 *        {@link ParameterOrder} which represents the parameter order to use
 *        for the modified LOCATE() function
 * @param stringIndexBase the string index that represents the first character of a string
 * @param args an array of <code>IExpression</code>'s to use as the
 *             arguments to the LOCATE() function
 * @param expectedStr A string representing the modified expression
 * @return On success, the modified expression.
 * @throws Exception
 */
public void helpTestLocate(final String locateFunctionName, final boolean parameterOrder, Expression[] args, String expectedStr) throws Exception {
    Expression param1 = null;
    Expression param2 = null;
    Expression param3 = null;
    if (args.length > 0)
        param1 = args[0];
    if (args.length > 1)
        param2 = args[1];
    if (args.length > 2)
        param3 = args[2];
    Function func = null;
    if (param3 != null) {
        func = LANG_FACTORY.createFunction(SourceSystemFunctions.LOCATE, Arrays.asList(param1, param2, param3), Integer.class);
    } else {
        func = LANG_FACTORY.createFunction(SourceSystemFunctions.LOCATE, Arrays.asList(param1, param2), Integer.class);
    }
    JDBCExecutionFactory trans = new JDBCExecutionFactory() {

        @Override
        public void start() throws TranslatorException {
            super.start();
            registerFunctionModifier(SourceSystemFunctions.LOCATE, new LocateFunctionModifier(getLanguageFactory(), locateFunctionName, parameterOrder));
        }
    };
    trans.setUseBindVariables(false);
    trans.start();
    SQLConversionVisitor sqlVisitor = trans.getSQLConversionVisitor();
    sqlVisitor.append(func);
    // $NON-NLS-1$
    assertEquals("Modified function does not match", expectedStr, sqlVisitor.toString());
}
Also used : Function(org.teiid.language.Function) Expression(org.teiid.language.Expression)

Aggregations

Expression (org.teiid.language.Expression)61 ArrayList (java.util.ArrayList)18 ExpressionValueSource (org.teiid.language.ExpressionValueSource)18 Literal (org.teiid.language.Literal)17 TranslatorException (org.teiid.translator.TranslatorException)16 ColumnReference (org.teiid.language.ColumnReference)14 Function (org.teiid.language.Function)13 Column (org.teiid.metadata.Column)12 Insert (org.teiid.language.Insert)11 List (java.util.List)10 Table (org.teiid.metadata.Table)9 BigInteger (java.math.BigInteger)7 Test (org.junit.Test)7 Parameter (org.teiid.language.Parameter)7 Iterator (java.util.Iterator)5 Comparison (org.teiid.language.Comparison)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 FakeExecutionContextImpl (org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl)4 NamedTable (org.teiid.language.NamedTable)4