Search in sources :

Example 21 with ResultSetExecution

use of org.teiid.translator.ResultSetExecution in project teiid by teiid.

the class TestHotrodExecution method testServer.

@Test
public void testServer() throws Exception {
    InfinispanConnection connection = getConnection("default");
    CACHE_NAME = connection.getCache().getName();
    ResultSetExecution exec = null;
    Command command = null;
    UpdateExecution update = null;
    // the below also test one-2-one relation.
    command = UTILITY.parseCommand("DELETE FROM G2");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2, g3_e1, g3_e2 FROM G2");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertNull(exec.next());
    command = UTILITY.parseCommand("INSERT INTO G2 (e1, e2, g3_e1, g3_e2) values (1, 'one', 1, 'one')");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("INSERT INTO G2 (e1, e2, g3_e1, g3_e2) values (2, 'two', 2, 'two')");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2, g3_e1, g3_e2 FROM G2");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one", new Integer(1), "one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "two", new Integer(2), "two" }, exec.next().toArray());
    assertNull(exec.next());
    command = UTILITY.parseCommand("INSERT INTO G4 (e1, e2, G2_e1) values (1, 'one', 1)");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("INSERT INTO G4 (e1, e2, G2_e1) values (2, 'one-one', 1)");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("INSERT INTO G4 (e1, e2, G2_e1) values (3, 'two', 2)");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("INSERT INTO G4 (e1, e2, G2_e1) values (4, 'two-two', 2)");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2 FROM G4");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "one-one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(3), "two" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(4), "two-two" }, exec.next().toArray());
    assertNull(exec.next());
    command = UTILITY.parseCommand("SELECT g2.e1, g4.e1, g4.e2 FROM G2 g2 JOIN G4 g4 " + "ON g2.e1 = g4.G2_e1 WHERE g2.e2 = 'two'");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(2), new Integer(3), "two" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), new Integer(4), "two-two" }, exec.next().toArray());
    assertNull(exec.next());
    // updates
    command = UTILITY.parseCommand("UPDATE G2 SET e2 = 'two-m', g3_e2 = 'two-mm' WHERE e1 = 2");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2, g3_e1, g3_e2 FROM G2 ORDER BY e1");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one", new Integer(1), "one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "two-m", new Integer(2), "two-mm" }, exec.next().toArray());
    assertNull(exec.next());
    // complex updates
    command = UTILITY.parseCommand("UPDATE G4 SET e2 = 'two-2' WHERE e2 = 'two-two' OR e2 = 'one-one'");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2 FROM G4");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "two-2" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(3), "two" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(4), "two-2" }, exec.next().toArray());
    assertNull(exec.next());
    // deletes
    command = UTILITY.parseCommand("DELETE FROM G4 where e2 = 'two-2'");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2 FROM G4");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(3), "two" }, exec.next().toArray());
    assertNull(exec.next());
    command = UTILITY.parseCommand("DELETE FROM G2 where e1 = 1");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT * FROM G2");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(2), "two-m", new Integer(2), "two-mm", null, null }, exec.next().toArray());
    assertNull(exec.next());
    // upsert
    command = UTILITY.parseCommand("UPSERT INTO G2 (e1, e2, g3_e1, g3_e2) values (1, 'one', 1, 'one')");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT * FROM G2 order by e1");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one", new Integer(1), "one", null, null }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "two-m", new Integer(2), "two-mm", null, null }, exec.next().toArray());
    assertNull(exec.next());
    command = UTILITY.parseCommand("UPSERT INTO G2 (e1, e2, g3_e1, g3_e2) values (2, 'two', 2, 'two')");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT * FROM G2");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one", new Integer(1), "one", null, null }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "two", new Integer(2), "two", null, null }, exec.next().toArray());
    assertNull(exec.next());
    command = UTILITY.parseCommand("UPSERT INTO G4 (e1, e2, G2_e1) values (5, 'upsert', 2)");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2 FROM G4");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(3), "two" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(5), "upsert" }, exec.next().toArray());
    assertNull(exec.next());
    Timestamp timestamp = new Timestamp(1504889513361L);
    Date date = new Date(1504889513361L);
    Time time = new Time(1504889513361L);
    String sql = "UPSERT INTO G5 (e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, e17, e18) " + "values (512, 'one', convert(512.34, double), 512.25, 256, 1, 't', 1504889513361, convert(123445656.12313, bigdecimal), " + "convert(1332434343, biginteger), " + "{t '" + new SimpleDateFormat("HH:mm:ss").format(time) + "'}, " + "{ts '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp) + "'}, " + "{d '" + new SimpleDateFormat("yyyy-MM-dd").format(date) + "'}, " + "null, null, " + "convert('clob contents', clob), xmlparse(CONTENT '<a>foo</a>'), null)";
    System.out.println(sql);
    command = UTILITY.parseCommand(sql);
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, e17, e18 FROM G5");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    List<?> results = exec.next();
    assertEquals(new Integer(512), (Integer) results.get(0));
    assertEquals("one", (String) results.get(1));
    // assertEquals(512.34, (double)results.get(2));
    // assertEquals(512.25, (float)results.get(3));
    assertEquals(new Short("256"), results.get(4));
    assertEquals(new Byte("1"), results.get(5));
    assertEquals(new String("t"), results.get(6));
    assertEquals(new Long(1504889513361L), results.get(7));
    assertEquals(new BigDecimal("123445656.12313").toPlainString(), ((BigDecimal) results.get(8)).toPlainString());
    assertEquals(new BigInteger("1332434343").toString(), ((BigInteger) results.get(9)).toString());
    assertEquals(new Time(new SimpleDateFormat("HH:mm:ss").parse(time.toString()).getTime()), results.get(10));
    assertEquals(new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2017-09-08 11:51:53").getTime()), results.get(11));
    assertEquals(new Date(new SimpleDateFormat("yyyy-MM-dd").parse("2017-09-08").getTime()), results.get(12));
    assertEquals("clob contents", ObjectConverterUtil.convertToString(((Clob) results.get(15)).getCharacterStream()));
    assertEquals("<a>foo</a>", ObjectConverterUtil.convertToString(((SQLXML) results.get(16)).getCharacterStream()));
    assertNull(exec.next());
}
Also used : UpdateExecution(org.teiid.translator.UpdateExecution) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.sql.Date) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) ResultSetExecution(org.teiid.translator.ResultSetExecution) SQLXML(java.sql.SQLXML) Command(org.teiid.language.Command) InfinispanConnection(org.teiid.infinispan.api.InfinispanConnection) BigInteger(java.math.BigInteger) QueryExpression(org.teiid.language.QueryExpression) Clob(java.sql.Clob) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 22 with ResultSetExecution

use of org.teiid.translator.ResultSetExecution in project teiid by teiid.

the class MockConnector method createResultSetExecution.

@Override
public ResultSetExecution createResultSetExecution(QueryExpression query, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
    Properties groupProps = new Properties();
    // $NON-NLS-1$ //$NON-NLS-2$
    groupProps.setProperty("customName", "CustomTableA");
    NamedTable group = (NamedTable) query.getProjectedQuery().getFrom().get(0);
    AbstractMetadataRecord groupMD = group.getMetadataObject();
    TestCase.assertEquals(groupProps, groupMD.getProperties());
    DerivedColumn symbl = query.getProjectedQuery().getDerivedColumns().get(0);
    ColumnReference element = (ColumnReference) symbl.getExpression();
    Column elementMD = element.getMetadataObject();
    Properties elementProps = new Properties();
    // $NON-NLS-1$ //$NON-NLS-2$
    elementProps.setProperty("customPosition", "11");
    TestCase.assertEquals(0, elementMD.getLength());
    // $NON-NLS-1$
    TestCase.assertEquals("Foo", elementMD.getDefaultValue());
    // $NON-NLS-1$
    TestCase.assertEquals("TrimNulls", elementMD.getFormat());
    TestCase.assertEquals(String.class, elementMD.getJavaType());
    TestCase.assertEquals(null, elementMD.getMaximumValue());
    TestCase.assertEquals(null, elementMD.getMinimumValue());
    // $NON-NLS-1$
    TestCase.assertEquals("COLUMN1", elementMD.getNameInSource());
    // $NON-NLS-1$
    TestCase.assertEquals("STR", elementMD.getNativeType());
    TestCase.assertEquals(NullType.Nullable, elementMD.getNullType());
    TestCase.assertEquals(0, elementMD.getPosition());
    TestCase.assertEquals(0, elementMD.getPrecision());
    TestCase.assertEquals(0, elementMD.getScale());
    TestCase.assertEquals(SearchType.Searchable, elementMD.getSearchType());
    TestCase.assertEquals(false, elementMD.isAutoIncremented());
    TestCase.assertEquals(true, elementMD.isCaseSensitive());
    TestCase.assertEquals(elementProps, elementMD.getProperties());
    DerivedColumn symbl2 = query.getProjectedQuery().getDerivedColumns().get(1);
    ColumnReference element2 = (ColumnReference) symbl2.getExpression();
    Column elementMD2 = element2.getMetadataObject();
    Properties elementProps2 = new Properties();
    // $NON-NLS-1$ //$NON-NLS-2$
    elementProps2.setProperty("customPosition", "12");
    TestCase.assertEquals(10, elementMD2.getLength());
    // $NON-NLS-1$
    TestCase.assertEquals("23", elementMD2.getDefaultValue());
    // $NON-NLS-1$
    TestCase.assertEquals("YesFormat", elementMD2.getFormat());
    TestCase.assertEquals(Integer.class, elementMD2.getJavaType());
    // $NON-NLS-1$
    TestCase.assertEquals("1", elementMD2.getMaximumValue());
    // $NON-NLS-1$
    TestCase.assertEquals("100", elementMD2.getMinimumValue());
    // $NON-NLS-1$
    TestCase.assertEquals("COLUMN2", elementMD2.getNameInSource());
    // $NON-NLS-1$
    TestCase.assertEquals("INT", elementMD2.getNativeType());
    TestCase.assertEquals(NullType.No_Nulls, elementMD2.getNullType());
    TestCase.assertEquals(1, elementMD2.getPosition());
    TestCase.assertEquals(0, elementMD2.getPrecision());
    TestCase.assertEquals(10, elementMD2.getScale());
    TestCase.assertEquals(SearchType.Searchable, elementMD2.getSearchType());
    TestCase.assertEquals(true, elementMD2.isAutoIncremented());
    TestCase.assertEquals(false, elementMD2.isCaseSensitive());
    TestCase.assertEquals(elementProps2, elementMD2.getProperties());
    ResultSetExecution exec = Mockito.mock(ResultSetExecution.class);
    Mockito.stub(exec.next()).toReturn(null);
    return exec;
}
Also used : NamedTable(org.teiid.language.NamedTable) ResultSetExecution(org.teiid.translator.ResultSetExecution) Column(org.teiid.metadata.Column) DerivedColumn(org.teiid.language.DerivedColumn) Properties(java.util.Properties) AbstractMetadataRecord(org.teiid.metadata.AbstractMetadataRecord) DerivedColumn(org.teiid.language.DerivedColumn) ColumnReference(org.teiid.language.ColumnReference)

Example 23 with ResultSetExecution

use of org.teiid.translator.ResultSetExecution in project teiid by teiid.

the class TestExcelExecution method helpExecute.

private ArrayList helpExecute(String ddl, FileConnection connection, String query, boolean format) throws Exception {
    ExcelExecutionFactory translator = new ExcelExecutionFactory();
    translator.setFormatStrings(format);
    translator.start();
    TransformationMetadata metadata = RealMetadataFactory.fromDDL(ddl, "vdb", "excel");
    TranslationUtility utility = new TranslationUtility(metadata);
    Command cmd = utility.parseCommand(query);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    ResultSetExecution execution = translator.createResultSetExecution((QueryExpression) cmd, context, utility.createRuntimeMetadata(), connection);
    try {
        execution.execute();
        ArrayList results = new ArrayList();
        while (true) {
            List<?> row = execution.next();
            if (row == null) {
                break;
            }
            results.add(row);
        }
        return results;
    } finally {
        execution.close();
    }
}
Also used : ResultSetExecution(org.teiid.translator.ResultSetExecution) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) TranslationUtility(org.teiid.cdk.api.TranslationUtility) ArrayList(java.util.ArrayList)

Example 24 with ResultSetExecution

use of org.teiid.translator.ResultSetExecution in project teiid by teiid.

the class TestNativeSpreadsheet method testDirect.

@Test
public void testDirect() throws TranslatorException {
    SpreadsheetExecutionFactory sef = new SpreadsheetExecutionFactory();
    sef.setSupportsDirectQueryProcedure(true);
    String input = "call native('worksheet=x;query=$1 foo;limit=2', 'a')";
    TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility();
    Command command = util.parseCommand(input);
    ExecutionContext ec = Mockito.mock(ExecutionContext.class);
    RuntimeMetadata rm = Mockito.mock(RuntimeMetadata.class);
    GoogleSpreadsheetConnection connection = Mockito.mock(GoogleSpreadsheetConnection.class);
    RowsResult result = Mockito.mock(RowsResult.class);
    Mockito.stub(result.iterator()).toReturn(Arrays.asList(new SheetRow()).iterator());
    Mockito.stub(connection.executeQuery("x", "'a' foo", null, 2, 0)).toReturn(result);
    ResultSetExecution execution = (ResultSetExecution) sef.createExecution(command, ec, rm, connection);
    execution.execute();
    List<?> vals = execution.next();
    assertTrue(vals.get(0) instanceof Object[]);
}
Also used : TranslationUtility(org.teiid.cdk.api.TranslationUtility) RowsResult(org.teiid.translator.google.api.result.RowsResult) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) SheetRow(org.teiid.translator.google.api.result.SheetRow) Command(org.teiid.language.Command) GoogleSpreadsheetConnection(org.teiid.translator.google.api.GoogleSpreadsheetConnection) Test(org.junit.Test)

Example 25 with ResultSetExecution

use of org.teiid.translator.ResultSetExecution in project teiid by teiid.

the class TestMongoDBDirectQueryExecution method testDirect.

@Test
public void testDirect() throws Exception {
    Command cmd = this.utility.parseCommand("SELECT * FROM Customers");
    MongoDBConnection connection = Mockito.mock(MongoDBConnection.class);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    DBCollection dbCollection = Mockito.mock(DBCollection.class);
    DB db = Mockito.mock(DB.class);
    Mockito.stub(db.getCollection("MyTable")).toReturn(dbCollection);
    Mockito.stub(db.collectionExists(Mockito.anyString())).toReturn(true);
    Mockito.stub(connection.getDatabase()).toReturn(db);
    AggregationOutput output = Mockito.mock(AggregationOutput.class);
    Mockito.stub(output.results()).toReturn(new ArrayList<DBObject>());
    Mockito.stub(dbCollection.aggregate(Mockito.any(DBObject.class), Mockito.any(DBObject.class))).toReturn(output);
    Argument arg = new Argument(Direction.IN, null, String.class, null);
    arg.setArgumentValue(new Literal("MyTable;{$match:{\"id\":\"$1\"}};{$project:{\"_m0\":\"$user\"}}", String.class));
    Argument arg2 = new Argument(Direction.IN, null, String.class, null);
    arg2.setArgumentValue(new Literal("foo", String.class));
    ResultSetExecution execution = this.translator.createDirectExecution(Arrays.asList(arg, arg2), cmd, context, this.utility.createRuntimeMetadata(), connection);
    execution.execute();
    List<DBObject> pipeline = TestMongoDBQueryExecution.buildArray(new BasicDBObject("$match", new BasicDBObject("id", "foo")), new BasicDBObject("$project", new BasicDBObject("_m0", "$user")));
    Mockito.verify(dbCollection).aggregate(Mockito.eq(pipeline), Mockito.any(AggregationOptions.class));
}
Also used : AggregationOptions(com.mongodb.AggregationOptions) Argument(org.teiid.language.Argument) AggregationOutput(com.mongodb.AggregationOutput) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBCollection(com.mongodb.DBCollection) ResultSetExecution(org.teiid.translator.ResultSetExecution) BasicDBObject(com.mongodb.BasicDBObject) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) MongoDBConnection(org.teiid.mongodb.MongoDBConnection) Literal(org.teiid.language.Literal) DB(com.mongodb.DB) Test(org.junit.Test)

Aggregations

ResultSetExecution (org.teiid.translator.ResultSetExecution)50 Test (org.junit.Test)41 ExecutionContext (org.teiid.translator.ExecutionContext)22 FileReader (java.io.FileReader)19 Command (org.teiid.language.Command)17 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)15 QueryExpression (org.teiid.language.QueryExpression)12 List (java.util.List)11 ArrayList (java.util.ArrayList)10 TranslatorException (org.teiid.translator.TranslatorException)10 ExecutionFactory (org.teiid.translator.ExecutionFactory)8 TranslationUtility (org.teiid.cdk.api.TranslationUtility)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Connection (java.sql.Connection)4 ResultSet (java.sql.ResultSet)4 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)4 MongoDBConnection (org.teiid.mongodb.MongoDBConnection)4 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)4 UpdateExecution (org.teiid.translator.UpdateExecution)4 DatabaseMetaData (java.sql.DatabaseMetaData)3