Search in sources :

Example 1 with SQLField

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;
}
Also used : SelectValuesMeta(org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta) SQLField(org.pentaho.di.core.sql.SQLField) StepMeta(org.pentaho.di.trans.step.StepMeta)

Example 2 with SQLField

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;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) SQLField(org.pentaho.di.core.sql.SQLField) SortRowsMeta(org.pentaho.di.trans.steps.sort.SortRowsMeta) StepMeta(org.pentaho.di.trans.step.StepMeta) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 3 with SQLField

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;
}
Also used : RowMeta(org.pentaho.di.core.row.RowMeta) SQLField(org.pentaho.di.core.sql.SQLField) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString)

Example 4 with SQLField

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);
}
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 5 with SQLField

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" }));
}
Also used : ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) RowMeta(org.pentaho.di.core.row.RowMeta) ArrayList(java.util.ArrayList) SQLField(org.pentaho.di.core.sql.SQLField) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

SQLField (org.pentaho.di.core.sql.SQLField)9 StepMeta (org.pentaho.di.trans.step.StepMeta)6 RowMeta (org.pentaho.di.core.row.RowMeta)3 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)3 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Matchers.anyString (org.mockito.Matchers.anyString)2 KettleException (org.pentaho.di.core.exception.KettleException)2 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)2 SQLFields (org.pentaho.di.core.sql.SQLFields)2 MemoryGroupByMeta (org.pentaho.di.trans.steps.memgroupby.MemoryGroupByMeta)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Before (org.junit.Before)1 Test (org.junit.Test)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1