Search in sources :

Example 1 with RowsResult

use of org.teiid.translator.google.api.result.RowsResult 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 2 with RowsResult

use of org.teiid.translator.google.api.result.RowsResult in project teiid by teiid.

the class GoogleDataProtocolAPI method executeQuery.

/**
 * Most important method that will issue query [1] to specific worksheet. The columns in the query
 * should be identified by their real alphabetic name (A, B, C...).
 *
 * There is one important restriction to query. It should not contain offset and limit clauses.
 * To achieve functionality of offset and limit please use corresponding parameters in this method.
 *
 * [1] https://developers.google.com/chart/interactive/docs/querylanguage
 *
 * @param query The query defined in [1]
 * @param batchSize How big portions of data should be returned by one roundtrip to Google.
 * @return Iterable RowsResult that will actually perform the roundtrips to Google for data
 */
public RowsResult executeQuery(SpreadsheetInfo info, String worksheetTitle, String query, int batchSize, Integer offset, Integer limit) {
    String key = info.getSpreadsheetKey();
    RowsResult result = new RowsResult(new DataProtocolQueryStrategy(key, worksheetTitle, query), batchSize);
    if (offset != null)
        result.setOffset(offset);
    if (limit != null)
        result.setLimit(limit);
    return result;
}
Also used : RowsResult(org.teiid.translator.google.api.result.RowsResult)

Example 3 with RowsResult

use of org.teiid.translator.google.api.result.RowsResult in project teiid by teiid.

the class RowsResultTest method twoBatchCallsNeeded.

@Test
public void twoBatchCallsNeeded() {
    final List<SheetRow> firstPart = new ArrayList<SheetRow>();
    final List<SheetRow> secondPart = new ArrayList<SheetRow>();
    final List<SheetRow> all = new ArrayList<SheetRow>();
    firstPart.add(new SheetRow(new String[] { "a", "b", "c" }));
    firstPart.add(new SheetRow(new String[] { "a2", "b2", "c2" }));
    firstPart.add(new SheetRow(new String[] { "a3", "b3", "c3" }));
    secondPart.add(new SheetRow(new String[] { "a4", "b4", "c4" }));
    secondPart.add(new SheetRow(new String[] { "a5", "b5", "c5" }));
    all.addAll(firstPart);
    all.addAll(secondPart);
    RowsResult result = new RowsResult(new PartialResultExecutor() {

        int called = 0;

        @Override
        public List<SheetRow> getResultsBatch(int startIndex, int endIndex) {
            if (called++ > 2)
                Assert.fail("getResultsBatch at most twice");
            if (called == 1) {
                return firstPart;
            } else if (called == 2) {
                return secondPart;
            }
            // Shouldn't reach here.
            return null;
        }
    }, 3);
    int i = 0;
    for (SheetRow row : result) {
        Assert.assertEquals(row, all.get(i++));
    }
    Assert.assertEquals("Six rows should be in the result", 5, i);
}
Also used : SheetRow(org.teiid.translator.google.api.result.SheetRow) ArrayList(java.util.ArrayList) PartialResultExecutor(org.teiid.translator.google.api.result.PartialResultExecutor) List(java.util.List) ArrayList(java.util.ArrayList) RowsResult(org.teiid.translator.google.api.result.RowsResult) Test(org.junit.Test)

Example 4 with RowsResult

use of org.teiid.translator.google.api.result.RowsResult in project teiid by teiid.

the class RowsResultTest method noRows.

@Test
public void noRows() {
    final List<SheetRow> all = new ArrayList<SheetRow>();
    RowsResult result = new RowsResult(new PartialResultExecutor() {

        int called = 0;

        @Override
        public List<SheetRow> getResultsBatch(int startIndex, int endIndex) {
            if (called++ > 1)
                Assert.fail("getResultsBatch at once");
            return all;
        }
    }, 3);
    int i = 0;
    for (SheetRow row : result) {
        i++;
    }
    Assert.assertEquals("No iterations should be made", 0, i);
}
Also used : SheetRow(org.teiid.translator.google.api.result.SheetRow) ArrayList(java.util.ArrayList) PartialResultExecutor(org.teiid.translator.google.api.result.PartialResultExecutor) List(java.util.List) ArrayList(java.util.ArrayList) RowsResult(org.teiid.translator.google.api.result.RowsResult) Test(org.junit.Test)

Example 5 with RowsResult

use of org.teiid.translator.google.api.result.RowsResult in project teiid by teiid.

the class RowsResultTest method sixBatchCalls.

@Test
public void sixBatchCalls() {
    final List<SheetRow> all = new ArrayList<SheetRow>();
    all.add(new SheetRow(new String[] { "a", "b", "c" }));
    all.add(new SheetRow(new String[] { "a2", "b2", "c2" }));
    all.add(new SheetRow(new String[] { "a3", "b3", "c3" }));
    all.add(new SheetRow(new String[] { "a4", "b4", "c4" }));
    all.add(new SheetRow(new String[] { "a5", "b5", "c5" }));
    all.add(new SheetRow(new String[] { "a6", "b6", "c6" }));
    RowsResult result = new RowsResult(new PartialResultExecutor() {

        int called = 0;

        @Override
        public List<SheetRow> getResultsBatch(int startIndex, int endIndex) {
            if (called > 6)
                Assert.fail("getResultsBatch at least 6 times");
            if (called == 6)
                return new ArrayList<SheetRow>();
            return Collections.singletonList(all.get(called++));
        }
    }, 1);
    int i = 0;
    for (SheetRow row : result) {
        Assert.assertEquals(all.get(i++), row);
    }
    Assert.assertEquals("Six iterations should be made", 6, i);
}
Also used : SheetRow(org.teiid.translator.google.api.result.SheetRow) ArrayList(java.util.ArrayList) PartialResultExecutor(org.teiid.translator.google.api.result.PartialResultExecutor) List(java.util.List) ArrayList(java.util.ArrayList) RowsResult(org.teiid.translator.google.api.result.RowsResult) Test(org.junit.Test)

Aggregations

RowsResult (org.teiid.translator.google.api.result.RowsResult)8 Test (org.junit.Test)7 SheetRow (org.teiid.translator.google.api.result.SheetRow)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 PartialResultExecutor (org.teiid.translator.google.api.result.PartialResultExecutor)6 TranslationUtility (org.teiid.cdk.api.TranslationUtility)1 Command (org.teiid.language.Command)1 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)1 ExecutionContext (org.teiid.translator.ExecutionContext)1 ResultSetExecution (org.teiid.translator.ResultSetExecution)1 GoogleSpreadsheetConnection (org.teiid.translator.google.api.GoogleSpreadsheetConnection)1