Search in sources :

Example 16 with NamedTable

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

the class TestSingleInsert method testDateTypes.

@Test
public void testDateTypes() throws Exception {
    NamedTable table = new NamedTable("temp", null, Mockito.mock(Table.class));
    ArrayList<ColumnReference> elements = new ArrayList<ColumnReference>();
    elements.add(new ColumnReference(table, "one", Mockito.mock(Column.class), Integer.class));
    elements.add(new ColumnReference(table, "two", Mockito.mock(Column.class), Date.class));
    elements.add(new ColumnReference(table, "three", Mockito.mock(Column.class), Timestamp.class));
    List<Expression> values = new ArrayList<Expression>();
    values.add(new Literal(1, DataTypeManager.DefaultDataClasses.INTEGER));
    values.add(new Literal(TimestampUtil.createDate(100, 01, 1), DataTypeManager.DefaultDataClasses.DATE));
    values.add(new Literal(TimestampUtil.createTimestamp(100, 01, 1, 0, 4, 0, 0), DataTypeManager.DefaultDataClasses.TIMESTAMP));
    ExpressionValueSource valueSource = new ExpressionValueSource(values);
    Insert insert = new Insert(table, elements, valueSource);
    SalesforceConnection connection = Mockito.mock(SalesforceConnection.class);
    Mockito.stub(connection.create(Mockito.any(DataPayload.class))).toAnswer(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            DataPayload payload = (DataPayload) invocation.getArguments()[0];
            List<DataPayload.Field> fields = payload.getMessageElements();
            assertEquals(3, fields.size());
            assertEquals(1, fields.get(0).value);
            assertEquals(TimestampUtil.createDate(100, 01, 1), fields.get(1).value);
            Calendar cal = (Calendar) fields.get(2).value;
            assertEquals(TimeZone.getTimeZone("GMT-1"), cal.getTimeZone());
            return 1;
        }
    });
    Mockito.stub(connection.upsert(Mockito.any(DataPayload.class))).toReturn(1);
    SalesForceExecutionFactory config = new SalesForceExecutionFactory();
    config.setMaxBulkInsertBatchSize(1);
    InsertExecutionImpl updateExecution = new InsertExecutionImpl(config, insert, connection, Mockito.mock(RuntimeMetadata.class), Mockito.mock(ExecutionContext.class));
    while (true) {
        try {
            updateExecution.execute();
            org.junit.Assert.assertArrayEquals(new int[] { 1 }, updateExecution.getUpdateCounts());
            break;
        } catch (DataNotAvailableException e) {
            continue;
        }
    }
    insert.setUpsert(true);
    updateExecution = new InsertExecutionImpl(config, insert, connection, Mockito.mock(RuntimeMetadata.class), Mockito.mock(ExecutionContext.class));
    while (true) {
        try {
            updateExecution.execute();
            org.junit.Assert.assertArrayEquals(new int[] { 1 }, updateExecution.getUpdateCounts());
            break;
        } catch (DataNotAvailableException e) {
            continue;
        }
    }
    Mockito.verify(connection).upsert(Mockito.any(DataPayload.class));
}
Also used : NamedTable(org.teiid.language.NamedTable) ArrayList(java.util.ArrayList) Insert(org.teiid.language.Insert) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) Timestamp(java.sql.Timestamp) Literal(org.teiid.language.Literal) ArrayList(java.util.ArrayList) List(java.util.List) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) ExpressionValueSource(org.teiid.language.ExpressionValueSource) NamedTable(org.teiid.language.NamedTable) Table(org.teiid.metadata.Table) Calendar(java.util.Calendar) SalesForceExecutionFactory(org.teiid.translator.salesforce.SalesForceExecutionFactory) SalesforceConnection(org.teiid.translator.salesforce.SalesforceConnection) Date(java.sql.Date) ExecutionContext(org.teiid.translator.ExecutionContext) Expression(org.teiid.language.Expression) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ColumnReference(org.teiid.language.ColumnReference) Test(org.junit.Test)

Example 17 with NamedTable

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

the class TestSetQueryImpl method example2.

public static SetQuery example2() throws Exception {
    // $NON-NLS-1$
    NamedTable group = new NamedTable("ted", null, null);
    // $NON-NLS-1$
    ColumnReference element = new ColumnReference(group, "nugent", null, String.class);
    DerivedColumn symbol = new DerivedColumn(null, element);
    List symbols = new ArrayList();
    symbols.add(symbol);
    List items = new ArrayList();
    items.add(group);
    // $NON-NLS-1$
    NamedTable group2 = new NamedTable("dave", null, null);
    // $NON-NLS-1$
    ColumnReference element2 = new ColumnReference(group2, "barry", null, String.class);
    DerivedColumn symbol2 = new DerivedColumn(null, element2);
    List symbols2 = new ArrayList();
    symbols2.add(symbol2);
    List items2 = new ArrayList();
    items2.add(group2);
    Select secondQuery = new Select(symbols2, false, items2, null, null, null, null);
    Select query = new Select(symbols, false, items, null, null, null, null);
    SetQuery setQuery = new SetQuery();
    setQuery.setOperation(SetQuery.Operation.UNION);
    setQuery.setAll(true);
    setQuery.setLeftQuery(query);
    setQuery.setRightQuery(secondQuery);
    return setQuery;
}
Also used : NamedTable(org.teiid.language.NamedTable) SetQuery(org.teiid.language.SetQuery) ArrayList(java.util.ArrayList) Select(org.teiid.language.Select) ArrayList(java.util.ArrayList) List(java.util.List) DerivedColumn(org.teiid.language.DerivedColumn) ColumnReference(org.teiid.language.ColumnReference)

Example 18 with NamedTable

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

the class IdentifierFunctionModifier method translate.

public List<?> translate(Function function) {
    List<Object> objs = new ArrayList<Object>();
    List<Expression> parms = function.getParameters();
    objs.add(function.getName().substring(function.getName().indexOf('_') + 1));
    objs.add(SQLConstants.Tokens.LPAREN);
    for (Iterator<Expression> iter = parms.iterator(); iter.hasNext(); ) {
        Expression expr = iter.next();
        if (expr instanceof ColumnReference) {
            boolean dotAll = false;
            boolean useSelector = false;
            ColumnReference cr = (ColumnReference) expr;
            Column c = cr.getMetadataObject();
            if (c != null) {
                if ("\"mode:properties\"".equalsIgnoreCase(c.getSourceName())) {
                    // $NON-NLS-1$
                    dotAll = true;
                    useSelector = true;
                } else if ("\"jcr:path\"".equalsIgnoreCase(c.getSourceName())) {
                    // $NON-NLS-1$
                    useSelector = true;
                }
            }
            if (useSelector) {
                NamedTable nt = ((ColumnReference) expr).getTable();
                if (nt.getCorrelationName() != null) {
                    objs.add(nt.getCorrelationName());
                } else {
                    objs.add(nt);
                }
            } else {
                objs.add(expr);
            }
            if (dotAll) {
                // $NON-NLS-1$
                objs.add(".*");
            }
        } else {
            objs.add(expr);
        }
        if (iter.hasNext()) {
            // $NON-NLS-1$
            objs.add(", ");
        }
    }
    objs.add(SQLConstants.Tokens.RPAREN);
    return objs;
}
Also used : NamedTable(org.teiid.language.NamedTable) Expression(org.teiid.language.Expression) Column(org.teiid.metadata.Column) ArrayList(java.util.ArrayList) ColumnReference(org.teiid.language.ColumnReference)

Example 19 with NamedTable

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

the class TestGroup method getGroup.

// ################ TEST GROUP METADATAID ######################
public Table getGroup(String groupName, TranslationUtility transUtil) throws Exception {
    // $NON-NLS-1$
    Select query = (Select) transUtil.parseCommand("SELECT 1 FROM " + groupName);
    NamedTable group = (NamedTable) query.getFrom().get(0);
    return group.getMetadataObject();
}
Also used : NamedTable(org.teiid.language.NamedTable) Select(org.teiid.language.Select)

Example 20 with NamedTable

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

the class TestMetadataObject method getGroupID.

// ################ TEST GROUP METADATAID ######################
public Table getGroupID(String groupName, TranslationUtility transUtil) {
    // $NON-NLS-1$
    Select query = (Select) transUtil.parseCommand("SELECT 1 FROM " + groupName);
    NamedTable group = (NamedTable) query.getFrom().get(0);
    return group.getMetadataObject();
}
Also used : NamedTable(org.teiid.language.NamedTable) Select(org.teiid.language.Select)

Aggregations

NamedTable (org.teiid.language.NamedTable)23 ColumnReference (org.teiid.language.ColumnReference)13 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)6 Select (org.teiid.language.Select)6 Table (org.teiid.metadata.Table)6 Column (org.teiid.metadata.Column)5 List (java.util.List)4 DerivedColumn (org.teiid.language.DerivedColumn)4 Expression (org.teiid.language.Expression)4 Insert (org.teiid.language.Insert)4 ExpressionValueSource (org.teiid.language.ExpressionValueSource)3 TableReference (org.teiid.language.TableReference)3 AbstractMetadataRecord (org.teiid.metadata.AbstractMetadataRecord)3 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)3 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)3 ExecutionContext (org.teiid.translator.ExecutionContext)3 TranslatorException (org.teiid.translator.TranslatorException)3 SalesForceExecutionFactory (org.teiid.translator.salesforce.SalesForceExecutionFactory)3 SalesforceConnection (org.teiid.translator.salesforce.SalesforceConnection)3