use of org.pentaho.di.core.sql.SQLField in project pdi-dataservice-server-plugin by pentaho.
the class SqlTransGenerator method generateSelectStep.
private StepMeta generateSelectStep() {
List<SQLField> fields = sql.getSelectFields().getFields();
SelectValuesMeta meta = new SelectValuesMeta();
meta.allocate(fields.size(), 0, 0);
String[] selectNames = new String[fields.size()];
String[] selectRename = new String[fields.size()];
for (int i = 0; i < fields.size(); i++) {
SQLField sqlField = fields.get(i);
if (sqlField.getAggregation() == null) {
selectNames[i] = sqlField.getField();
selectRename[i] = sqlField.getAlias();
} else {
// agg field names are assigned in the group by
selectNames[i] = Const.NVL(sqlField.getAlias(), sqlField.getField());
}
}
meta.setSelectName(selectNames);
meta.setSelectRename(selectRename);
StepMeta stepMeta = new StepMeta("Select values", meta);
stepMeta.setLocation(xLocation, 50);
xLocation += 100;
stepMeta.setDraw(true);
return stepMeta;
}
use of org.pentaho.di.core.sql.SQLField in project pdi-dataservice-server-plugin by pentaho.
the class SqlTransGenerator method generateSortStep.
private StepMeta generateSortStep(RowMetaInterface rowMeta) throws KettleException {
List<SQLField> fields = sql.getOrderFields().getFields();
List<SQLField> selectFields = sql.getSelectFields().getFields();
SortRowsMeta meta = new SortRowsMeta();
meta.allocate(fields.size());
for (int i = 0; i < fields.size(); i++) {
SQLField sqlField = fields.get(i);
ValueMetaInterface valueMeta = rowMeta.searchValueMeta(sqlField.getField());
if (valueMeta == null) {
// This could be an alias used in an order by clause.
// In that case, we need to find the correct original name in the selectFields...
//
SQLField selectField = SQLField.searchSQLFieldByFieldOrAlias(selectFields, sqlField.getField());
if (selectField != null) {
// Yep, verify this original name...
//
valueMeta = rowMeta.searchValueMeta(selectField.getField());
} else {
valueMeta = rowMeta.searchValueMeta(sqlField.getAlias());
}
}
if (valueMeta == null) {
throw new KettleException("Unable to find field to sort on: " + sqlField.getField() + " nor the alias: " + sqlField.getAlias());
}
meta.getFieldName()[i] = valueMeta.getName();
meta.getAscending()[i] = sqlField.isAscending();
meta.getCaseSensitive()[i] = true;
}
meta.setSortSize("1000000");
StepMeta stepMeta = new StepMeta("Sort rows", meta);
stepMeta.setLocation(xLocation, 50);
xLocation += 100;
stepMeta.setDraw(true);
return stepMeta;
}
use of org.pentaho.di.core.sql.SQLField in project pdi-dataservice-server-plugin by pentaho.
the class DataServiceTestController method sqlFieldsToRowMeta.
private RowMetaInterface sqlFieldsToRowMeta(DataServiceExecutor dataServiceExec) throws KettleException {
List<SQLField> fields = dataServiceExec.getSql().getSelectFields().getFields();
RowMetaInterface rowMeta = dataServiceExec.getSql().getRowMeta();
RowMetaInterface sqlFieldsRowMeta = new RowMeta();
List<String> fieldNames = Arrays.asList(rowMeta.getFieldNames());
for (SQLField field : fields) {
int indexOfField = fieldNames.indexOf(field.getField());
sqlFieldsRowMeta.addValueMeta(rowMeta.getValueMeta(indexOfField));
}
return sqlFieldsRowMeta;
}
use of org.pentaho.di.core.sql.SQLField 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.core.sql.SQLField in project pdi-dataservice-server-plugin by pentaho.
the class DataServiceTestControllerTest method resultRowMetaIsUpdated.
@Test
public void resultRowMetaIsUpdated() throws Exception {
SQLField field = mock(SQLField.class);
List<SQLField> fields = new ArrayList<>();
fields.add(field);
when(field.getField()).thenReturn("testFieldName");
when(dataServiceExecutor.getSql().getSelectFields().getFields()).thenReturn(fields);
RowMeta rowMeta = new RowMeta();
rowMeta.addValueMeta(new ValueMetaString("testFieldName"));
when(dataServiceExecutor.getSql().getRowMeta()).thenReturn(rowMeta);
ArgumentCaptor<RowMetaInterface> argument = ArgumentCaptor.forClass(RowMetaInterface.class);
dataServiceTestController.executeSql();
verify(model, times(1)).setResultRowMeta(argument.capture());
assertThat(argument.getValue().getFieldNames(), equalTo(new String[] { "testFieldName" }));
}
Aggregations