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());
}
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations