Search in sources :

Example 1 with Query

use of org.pentaho.di.trans.dataservice.clients.Query in project pdi-dataservice-server-plugin by pentaho.

the class TransDataServlet method handleRequest.

public void handleRequest(CarteRequest request) throws IOException {
    String sqlQuery = !Strings.isNullOrEmpty(request.getParameter(SQL)) ? request.getParameter(SQL) : request.getHeader(SQL);
    if (Strings.isNullOrEmpty(sqlQuery)) {
        String sqlParamMissing = "SQL not specified";
        logError(sqlParamMissing);
        request.respond(400).withMessage(sqlParamMissing);
        return;
    }
    String maxRowsValue = !Strings.isNullOrEmpty(request.getParameter(MAX_ROWS)) ? request.getParameter(MAX_ROWS) : request.getHeader(MAX_ROWS);
    final int maxRows = Const.toInt(maxRowsValue, -1);
    final String debugTransFile = request.getParameter("debugtrans");
    Map<String, String> parameters = collectParameters(request.getParameters());
    try {
        final Query query = client.prepareQuery(sqlQuery, maxRows, parameters);
        // For logging and tracking purposes, let's expose both the service transformation as well
        // as the generated transformation on this very carte instance
        List<Trans> transList = query.getTransList();
        for (Trans trans : transList) {
            monitorTransformation(trans);
        }
        if (!Strings.isNullOrEmpty(debugTransFile) && !transList.isEmpty()) {
            saveGeneratedTransformation(Iterables.getLast(transList).getTransMeta(), debugTransFile);
        }
        request.respond(200).with("binary/jdbc", new OutputStreamResponse() {

            @Override
            public void write(OutputStream outputStream) throws IOException {
                query.writeTo(outputStream);
            }
        });
    } catch (Exception e) {
        logError("Error executing SQL query: " + sqlQuery, e);
        request.respond(400).withMessage(Strings.nullToEmpty(e.getMessage()).trim());
    }
}
Also used : Query(org.pentaho.di.trans.dataservice.clients.Query) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Trans(org.pentaho.di.trans.Trans) IOException(java.io.IOException)

Example 2 with Query

use of org.pentaho.di.trans.dataservice.clients.Query in project pdi-dataservice-server-plugin by pentaho.

the class DataServiceTestController method executeSql.

public void executeSql() throws KettleException {
    resetMetrics();
    dataServiceExec = getNewDataServiceExecutor(true);
    updateOptimizationImpact(dataServiceExec);
    updateModel(dataServiceExec);
    AnnotationsQueryService annotationsQuery = getAnnotationsQueryService();
    Query query;
    if (dataService.isStreaming()) {
        XulRadio timeBasedRadio = (XulRadio) document.getElementById("time-based-radio");
        IDataServiceClientService.StreamingMode windowMode = timeBasedRadio.isSelected() ? IDataServiceClientService.StreamingMode.TIME_BASED : IDataServiceClientService.StreamingMode.ROW_BASED;
        model.setWindowMode(windowMode);
        query = annotationsQuery.prepareQuery(model.getSql(), model.getWindowMode(), model.getWindowSize(), model.getWindowEvery(), model.getWindowLimit(), ImmutableMap.<String, String>of());
    } else {
        query = annotationsQuery.prepareQuery(model.getSql(), model.getMaxRows(), ImmutableMap.<String, String>of());
    }
    if (null != query) {
        writeAnnotations(query);
        handleCompletion(dataServiceExec);
    } else {
        callback.onLogChannelUpdate();
        dataServiceExec.executeQuery(getDataServiceRowListener());
        pollForCompletion(dataServiceExec);
    }
}
Also used : XulRadio(org.pentaho.ui.xul.components.XulRadio) IDataServiceClientService(org.pentaho.di.trans.dataservice.client.api.IDataServiceClientService) Query(org.pentaho.di.trans.dataservice.clients.Query) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) AnnotationsQueryService(org.pentaho.di.trans.dataservice.clients.AnnotationsQueryService)

Example 3 with Query

use of org.pentaho.di.trans.dataservice.clients.Query in project pdi-dataservice-server-plugin by pentaho.

the class DataServiceTestControllerTest method initMocks.

@Before
public void initMocks() throws Exception {
    MockitoAnnotations.initMocks(this);
    when(dataService.getServiceTrans()).thenReturn(transMeta);
    when(transMeta.realClone(false)).thenReturn(transMeta);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ((OutputStream) invocation.getArguments()[0]).write(TEST_ANNOTATIONS.getBytes());
            return null;
        }
    }).when(annotationsQuery).writeTo(any(OutputStream.class));
    doAnswer(new Answer<Query>() {

        @Override
        public Query answer(InvocationOnMock invocation) {
            String sql = (String) invocation.getArguments()[0];
            if (null != sql && sql.startsWith("show annotations from ")) {
                return annotationsQuery;
            }
            return null;
        }
    }).when(annotationsQueryService).prepareQuery(any(String.class), anyInt(), any(Map.class));
    when(streamingExecution.getId()).thenReturn(TEST_TABLE_NAME);
    when(context.getServiceTransExecutor(TEST_TABLE_NAME)).thenReturn(streamingExecution);
    when(dataServiceExecutor.getServiceTrans()).thenReturn(mock(Trans.class));
    when(dataServiceExecutor.getGenTrans()).thenReturn(mock(Trans.class));
    when(dataServiceExecutor.getServiceTransMeta()).thenReturn(transMeta);
    when(dataServiceExecutor.getServiceTrans().getLogChannel()).thenReturn(mock(LogChannelInterface.class));
    when(dataServiceExecutor.getGenTrans().getLogChannel()).thenReturn(mock(LogChannelInterface.class));
    when(dataServiceExecutor.getSql()).thenReturn(mock(SQL.class));
    when(dataServiceExecutor.getSql().getSelectFields()).thenReturn(mock(SQLFields.class));
    when(dataServiceExecutor.getSql().getRowMeta()).thenReturn(new RowMeta());
    when(dataServiceExecutor.getSql().getSelectFields().getFields()).thenReturn(new ArrayList<SQLField>());
    when(rowMeta.getValueMetaList()).thenReturn(Collections.EMPTY_LIST);
    when(transMeta.listParameters()).thenReturn(new String[] { "foo", "bar" });
    when(transMeta.listVariables()).thenReturn(new String[] { "varFoo", "varBar" });
    when(transMeta.getStepFields(anyString())).thenReturn(rowMeta);
    when(transMeta.getVariable("varFoo")).thenReturn("varFooVal");
    when(transMeta.getVariable("varBar")).thenReturn("varBarVal");
    when(transMeta.getParameterDefault("foo")).thenReturn("fooVal");
    when(transMeta.getParameterDefault("bar")).thenReturn("barVal");
    when(bindingFactory.createBinding(same(model), anyString(), any(XulComponent.class), anyString())).thenReturn(binding);
    // mocks to deal with Xul multithreading.
    when(xulDomContainer.getDocumentRoot()).thenReturn(document);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ((Runnable) invocationOnMock.getArguments()[0]).run();
            return null;
        }
    }).when(document).invokeLater(any(Runnable.class));
    when(dataService.getName()).thenReturn(TEST_TABLE_NAME);
    when(dataService.isStreaming()).thenReturn(false);
    when(model.getWindowMode()).thenReturn(null);
    when(model.getWindowSize()).thenReturn(0L);
    when(model.getWindowEvery()).thenReturn(0L);
    when(model.getWindowLimit()).thenReturn(0L);
    dataServiceTestController = new DataServiceTestControllerTester();
    dataServiceTestController.setXulDomContainer(xulDomContainer);
    dataServiceTestController.setAnnotationsQueryService(annotationsQueryService);
}
Also used : Query(org.pentaho.di.trans.dataservice.clients.Query) RowMeta(org.pentaho.di.core.row.RowMeta) OutputStream(java.io.OutputStream) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) SQL(org.pentaho.di.core.sql.SQL) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) SQLFields(org.pentaho.di.core.sql.SQLFields) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SQLField(org.pentaho.di.core.sql.SQLField) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Trans(org.pentaho.di.trans.Trans) LogChannelInterface(org.pentaho.di.core.logging.LogChannelInterface) XulComponent(org.pentaho.ui.xul.XulComponent) Before(org.junit.Before)

Example 4 with Query

use of org.pentaho.di.trans.dataservice.clients.Query in project pdi-dataservice-server-plugin by pentaho.

the class StreamingDataServiceTestControllerTest method initMocks.

@Before
public void initMocks() throws Exception {
    MockitoAnnotations.initMocks(this);
    when(streamingDataService.getServiceTrans()).thenReturn(transMeta);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ((OutputStream) invocation.getArguments()[0]).write(TEST_ANNOTATIONS.getBytes());
            return null;
        }
    }).when(annotationsQuery).writeTo(any(OutputStream.class));
    doAnswer(new Answer<Query>() {

        @Override
        public Query answer(InvocationOnMock invocation) {
            String sql = (String) invocation.getArguments()[0];
            if (null != sql && sql.startsWith("show annotations from ")) {
                return annotationsQuery;
            }
            return null;
        }
    }).when(annotationsQueryService).prepareQuery(any(String.class), anyInt(), any(Map.class));
    when(transMeta.listParameters()).thenReturn(new String[] { "foo", "bar" });
    when(transMeta.getParameterDefault("foo")).thenReturn("fooVal");
    when(transMeta.getParameterDefault("bar")).thenReturn("barVal");
    when(bindingFactory.createBinding(same(model), anyString(), any(XulComponent.class), anyString())).thenReturn(binding);
    // mocks to deal with Xul multithreading.
    when(xulDomContainer.getDocumentRoot()).thenReturn(document);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ((Runnable) invocationOnMock.getArguments()[0]).run();
            return null;
        }
    }).when(document).invokeLater(any(Runnable.class));
    when(streamingDataService.getName()).thenReturn(TEST_TABLE_NAME);
    when(streamingDataService.isStreaming()).thenReturn(true);
    when(model.getWindowMode()).thenReturn(null);
    when(model.getWindowSize()).thenReturn(0L);
    when(model.getWindowEvery()).thenReturn(0L);
    when(model.getWindowLimit()).thenReturn(0L);
    dataServiceTestController = new DataServiceTestControllerTester();
    dataServiceTestController.setXulDomContainer(xulDomContainer);
    dataServiceTestController.setAnnotationsQueryService(annotationsQueryService);
}
Also used : Query(org.pentaho.di.trans.dataservice.clients.Query) OutputStream(java.io.OutputStream) Matchers.anyString(org.mockito.Matchers.anyString) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Map(java.util.Map) XulComponent(org.pentaho.ui.xul.XulComponent) Before(org.junit.Before)

Example 5 with Query

use of org.pentaho.di.trans.dataservice.clients.Query in project pdi-dataservice-server-plugin by pentaho.

the class TransDataServletTest method testLargeSQLQuery.

@Test
public void testLargeSQLQuery() throws Exception {
    parameters.put(HEADER_MAX_ROWS, TEST_MAX_ROWS);
    parameters.put(HEADER_SQL, TEST_LARGE_SQL_QUERY);
    Query query = mock(Query.class);
    doReturn(query).when(client).prepareQuery(TEST_LARGE_SQL_QUERY, Integer.valueOf(TEST_MAX_ROWS), ImmutableMap.<String, String>of());
    when(query.getTransList()).thenReturn(ImmutableList.of(serviceTrans, genTrans));
    when(request.getMethod()).thenReturn("POST");
    servlet.service(request, response);
    verify(logChannel, never()).logError(anyString(), (Throwable) any());
    verify(request, never()).getHeader(HEADER_SQL);
    verify(request, never()).getHeader(HEADER_MAX_ROWS);
}
Also used : Query(org.pentaho.di.trans.dataservice.clients.Query) Test(org.junit.Test)

Aggregations

Query (org.pentaho.di.trans.dataservice.clients.Query)6 OutputStream (java.io.OutputStream)3 Map (java.util.Map)2 Before (org.junit.Before)2 Test (org.junit.Test)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)2 Trans (org.pentaho.di.trans.Trans)2 XulComponent (org.pentaho.ui.xul.XulComponent)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 LogChannelInterface (org.pentaho.di.core.logging.LogChannelInterface)1 RowMeta (org.pentaho.di.core.row.RowMeta)1 SQL (org.pentaho.di.core.sql.SQL)1 SQLField (org.pentaho.di.core.sql.SQLField)1