Search in sources :

Example 1 with SelectMetadataChange

use of org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange in project pentaho-kettle by pentaho.

the class SpoonDBDelegate method copyTable.

public boolean copyTable(DatabaseMeta sourceDBInfo, DatabaseMeta targetDBInfo, String tablename) {
    try {
        // 
        // Create a new transformation...
        // 
        TransMeta meta = new TransMeta();
        meta.addDatabase(sourceDBInfo);
        meta.addDatabase(targetDBInfo);
        // 
        // Add a note
        // 
        String note = BaseMessages.getString(PKG, "Spoon.Message.Note.ReadInformationFromTableOnDB", tablename, sourceDBInfo.getDatabaseName()) + // "Reads information from table ["+tablename+"]
        Const.CR;
        // on database ["+sourceDBInfo+"]"
        note += BaseMessages.getString(PKG, "Spoon.Message.Note.WriteInformationToTableOnDB", tablename, targetDBInfo.getDatabaseName());
        // the information to table
        // ["+tablename+"] on
        // database
        // ["+targetDBInfo+"]"
        NotePadMeta ni = new NotePadMeta(note, 150, 10, -1, -1);
        meta.addNote(ni);
        // 
        // create the source step...
        // 
        // "read
        String fromstepname = BaseMessages.getString(PKG, "Spoon.Message.Note.ReadFromTable", tablename);
        // from
        // ["+tablename+"]";
        TableInputMeta tii = new TableInputMeta();
        tii.setDatabaseMeta(sourceDBInfo);
        tii.setSQL("SELECT * FROM " + tablename);
        PluginRegistry registry = PluginRegistry.getInstance();
        String fromstepid = registry.getPluginId(StepPluginType.class, tii);
        StepMeta fromstep = new StepMeta(fromstepid, fromstepname, tii);
        fromstep.setLocation(150, 100);
        fromstep.setDraw(true);
        fromstep.setDescription(BaseMessages.getString(PKG, "Spoon.Message.Note.ReadInformationFromTableOnDB", tablename, sourceDBInfo.getDatabaseName()));
        meta.addStep(fromstep);
        // 
        // add logic to rename fields in case any of the field names contain
        // reserved words...
        // Use metadata logic in SelectValues, use SelectValueInfo...
        // 
        Database sourceDB = new Database(loggingObject, sourceDBInfo);
        sourceDB.shareVariablesWith(meta);
        sourceDB.connect();
        try {
            // Get the fields for the input table...
            RowMetaInterface fields = sourceDB.getTableFields(tablename);
            // See if we need to deal with reserved words...
            int nrReserved = targetDBInfo.getNrReservedWords(fields);
            if (nrReserved > 0) {
                SelectValuesMeta svi = new SelectValuesMeta();
                svi.allocate(0, 0, nrReserved);
                int nr = 0;
                // CHECKSTYLE:Indentation:OFF
                for (int i = 0; i < fields.size(); i++) {
                    ValueMetaInterface v = fields.getValueMeta(i);
                    if (targetDBInfo.isReservedWord(v.getName())) {
                        if (svi.getMeta()[nr] == null) {
                            svi.getMeta()[nr] = new SelectMetadataChange(svi);
                        }
                        svi.getMeta()[nr].setName(v.getName());
                        svi.getMeta()[nr].setRename(targetDBInfo.quoteField(v.getName()));
                        nr++;
                    }
                }
                String selstepname = BaseMessages.getString(PKG, "Spoon.Message.Note.HandleReservedWords");
                String selstepid = registry.getPluginId(StepPluginType.class, svi);
                StepMeta selstep = new StepMeta(selstepid, selstepname, svi);
                selstep.setLocation(350, 100);
                selstep.setDraw(true);
                selstep.setDescription(BaseMessages.getString(PKG, "Spoon.Message.Note.RenamesReservedWords", // 
                targetDBInfo.getPluginId()));
                meta.addStep(selstep);
                TransHopMeta shi = new TransHopMeta(fromstep, selstep);
                meta.addTransHop(shi);
                fromstep = selstep;
            }
            // 
            // Create the target step...
            // 
            // 
            // Add the TableOutputMeta step...
            // 
            String tostepname = BaseMessages.getString(PKG, "Spoon.Message.Note.WriteToTable", tablename);
            TableOutputMeta toi = new TableOutputMeta();
            toi.setDatabaseMeta(targetDBInfo);
            toi.setTableName(tablename);
            toi.setCommitSize(200);
            toi.setTruncateTable(true);
            String tostepid = registry.getPluginId(StepPluginType.class, toi);
            StepMeta tostep = new StepMeta(tostepid, tostepname, toi);
            tostep.setLocation(550, 100);
            tostep.setDraw(true);
            tostep.setDescription(BaseMessages.getString(PKG, "Spoon.Message.Note.WriteInformationToTableOnDB2", tablename, targetDBInfo.getDatabaseName()));
            meta.addStep(tostep);
            // 
            // Add a hop between the two steps...
            // 
            TransHopMeta hi = new TransHopMeta(fromstep, tostep);
            meta.addTransHop(hi);
            // OK, if we're still here: overwrite the current transformation...
            // Set a name on this generated transformation
            // 
            String name = "Copy table from [" + sourceDBInfo.getName() + "] to [" + targetDBInfo.getName() + "]";
            String transName = name;
            int nr = 1;
            if (spoon.delegates.trans.getTransformation(transName) != null) {
                nr++;
                transName = name + " " + nr;
            }
            meta.setName(transName);
            spoon.delegates.trans.addTransGraph(meta);
            spoon.refreshGraph();
            spoon.refreshTree();
        } finally {
            sourceDB.disconnect();
        }
    } catch (Exception e) {
        new ErrorDialog(spoon.getShell(), BaseMessages.getString(PKG, "Spoon.Dialog.UnexpectedError.Title"), BaseMessages.getString(PKG, "Spoon.Dialog.UnexpectedError.Message"), new KettleException(e.getMessage(), e));
        return false;
    }
    return true;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) TransMeta(org.pentaho.di.trans.TransMeta) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) TableOutputMeta(org.pentaho.di.trans.steps.tableoutput.TableOutputMeta) StepMeta(org.pentaho.di.trans.step.StepMeta) TableInputMeta(org.pentaho.di.trans.steps.tableinput.TableInputMeta) KettleException(org.pentaho.di.core.exception.KettleException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) SelectValuesMeta(org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta) PluginRegistry(org.pentaho.di.core.plugins.PluginRegistry) Database(org.pentaho.di.core.database.Database) SelectMetadataChange(org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange) NotePadMeta(org.pentaho.di.core.NotePadMeta) TransHopMeta(org.pentaho.di.trans.TransHopMeta)

Example 2 with SelectMetadataChange

use of org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange in project pentaho-metaverse by pentaho.

the class SelectValuesStepAnalyzer method getChangeRecords.

@Override
public Set<ComponentDerivationRecord> getChangeRecords(SelectValuesMeta selectValuesMeta) throws MetaverseAnalyzerException {
    validateState(null, selectValuesMeta);
    Set<ComponentDerivationRecord> changeRecords = new HashSet<ComponentDerivationRecord>();
    String inputFieldName;
    String outputFieldName;
    ComponentDerivationRecord changeRecord;
    // Process the fields/tabs in the same order as the real step does
    if (!Const.isEmpty(selectValuesMeta.getSelectName())) {
        String[] fieldNames = selectValuesMeta.getSelectName();
        String[] fieldRenames = selectValuesMeta.getSelectRename();
        int[] fieldLength = selectValuesMeta.getSelectLength();
        int[] fieldPrecision = selectValuesMeta.getSelectPrecision();
        for (int i = 0; i < fieldNames.length; i++) {
            inputFieldName = fieldNames[i];
            outputFieldName = fieldRenames[i];
            changeRecord = new ComponentDerivationRecord(inputFieldName, outputFieldName == null ? inputFieldName : outputFieldName, ChangeType.METADATA);
            Set<String> metadataChangedFields = new HashSet<String>();
            if (inputFieldName != null && outputFieldName != null && !inputFieldName.equals(outputFieldName)) {
                metadataChangedFields.add("name");
            }
            // Check for changes in field length
            if (fieldLength != null && fieldLength[i] != NOT_CHANGED) {
                metadataChangedFields.add("length");
            }
            // Check for changes in field precision
            if (fieldPrecision != null && fieldPrecision[i] != NOT_CHANGED) {
                metadataChangedFields.add("precision");
            }
            if (!metadataChangedFields.isEmpty()) {
                // Add all the changed metadata fields as a single operation
                changeRecord.addOperation(new Operation(DictionaryConst.PROPERTY_MODIFIED, StringUtils.join(metadataChangedFields, ",")));
            }
            changeRecords.add(changeRecord);
        }
    }
    if (!Const.isEmpty(selectValuesMeta.getMeta())) {
        String[] prevStepNames = parentTransMeta.getPrevStepNames(getStepName());
        SelectMetadataChange[] metadataChanges = selectValuesMeta.getMeta();
        if (metadataChanges != null) {
            for (SelectMetadataChange metadataChange : metadataChanges) {
                inputFieldName = metadataChange.getName();
                outputFieldName = metadataChange.getRename();
                changeRecord = new ComponentDerivationRecord(inputFieldName, outputFieldName == null ? inputFieldName : outputFieldName, ChangeType.METADATA);
                Set<String> metadataChangedFields = new HashSet<String>();
                // NOTE: We use equalsIgnoreCase instead of equals because that's how Select Values currently works
                if (inputFieldName != null && outputFieldName != null && !inputFieldName.equalsIgnoreCase(outputFieldName)) {
                    metadataChangedFields.add("name");
                }
                // Get the ValueMetaInterface for the input field, to determine if any of its metadata has changed
                if (prevFields == null) {
                    prevFields = getInputFields(selectValuesMeta);
                    if (prevFields == null) {
                        log.warn(Messages.getString("WARNING.CannotDetermineFieldType", inputFieldName));
                        continue;
                    }
                }
                RowMetaInterface rowMetaInterface = prevFields.get(prevStepNames[0]);
                ValueMetaInterface inputFieldValueMeta = null;
                if (rowMetaInterface == null) {
                    log.warn(Messages.getString("WARNING.CannotDetermineFieldType", inputFieldName));
                    continue;
                }
                inputFieldValueMeta = rowMetaInterface.searchValueMeta(inputFieldName);
                if (inputFieldValueMeta == null) {
                    log.warn(Messages.getString("WARNING.CannotDetermineFieldType", inputFieldName));
                    continue;
                }
                // Check for changes in field type
                if (inputFieldValueMeta.getType() != metadataChange.getType()) {
                    metadataChangedFields.add("type");
                }
                // Check for changes in field length
                if (metadataChange.getLength() != NOT_CHANGED) {
                    metadataChangedFields.add("length");
                }
                // Check for changes in field precision
                if (metadataChange.getPrecision() != NOT_CHANGED) {
                    metadataChangedFields.add("precision");
                }
                // Check for changes in storage type (binary to string, e.g.)
                if ((metadataChange.getStorageType() != -1) && (inputFieldValueMeta.getStorageType() != metadataChange.getStorageType())) {
                    metadataChangedFields.add("storagetype");
                }
                // Check for changes in conversion mask
                if ((metadataChange.getConversionMask() != null) && (inputFieldValueMeta.getConversionMask() == null || !inputFieldValueMeta.getConversionMask().equals(metadataChange.getConversionMask()))) {
                    metadataChangedFields.add("conversionmask");
                }
                // Check for changes in date format leniency
                if (inputFieldValueMeta.isDateFormatLenient() != metadataChange.isDateFormatLenient()) {
                    metadataChangedFields.add("dateformatlenient");
                }
                // Check for changes in date format locale
                if ((metadataChange.getDateFormatLocale() != null) && (inputFieldValueMeta.getDateFormatLocale() == null || !inputFieldValueMeta.getDateFormatLocale().toString().equals(metadataChange.getDateFormatLocale()))) {
                    metadataChangedFields.add("datelocale");
                }
                // Check for changes in date format locale
                if ((metadataChange.getDateFormatTimeZone() != null) && (inputFieldValueMeta.getDateFormatTimeZone() == null || !inputFieldValueMeta.getDateFormatTimeZone().toString().equals(metadataChange.getDateFormatTimeZone()))) {
                    metadataChangedFields.add("datetimezone");
                }
                // Check for changes in date format locale
                if (inputFieldValueMeta.isLenientStringToNumber() != metadataChange.isLenientStringToNumber()) {
                    metadataChangedFields.add("lenientnumberconversion");
                }
                // Check for changes in encoding
                if ((metadataChange.getDateFormatTimeZone() != null) && (inputFieldValueMeta.getStringEncoding() == null || !inputFieldValueMeta.getStringEncoding().equals(metadataChange.getDateFormatTimeZone()))) {
                    metadataChangedFields.add("encoding");
                }
                // Check for changes in encoding
                if ((metadataChange.getDecimalSymbol() != null) && (inputFieldValueMeta.getDecimalSymbol() == null || !inputFieldValueMeta.getDecimalSymbol().equals(metadataChange.getDecimalSymbol()))) {
                    metadataChangedFields.add("decimalsymbol");
                }
                // Check for changes in encoding
                if ((metadataChange.getGroupingSymbol() != null) && (inputFieldValueMeta.getGroupingSymbol() == null || !inputFieldValueMeta.getGroupingSymbol().equals(metadataChange.getGroupingSymbol()))) {
                    metadataChangedFields.add("groupsymbol");
                }
                // Check for changes in encoding
                if ((metadataChange.getCurrencySymbol() != null) && (inputFieldValueMeta.getCurrencySymbol() == null || !inputFieldValueMeta.getCurrencySymbol().equals(metadataChange.getCurrencySymbol()))) {
                    metadataChangedFields.add("currencysymbol");
                }
                if (!metadataChangedFields.isEmpty()) {
                    // Add all the changed metadata fields as a single operation
                    changeRecord.addOperation(new Operation(DictionaryConst.PROPERTY_MODIFIED, StringUtils.join(metadataChangedFields, ",")));
                }
                changeRecords.add(changeRecord);
            }
        }
    }
    return changeRecords;
}
Also used : RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) Operation(org.pentaho.metaverse.api.model.Operation) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) ComponentDerivationRecord(org.pentaho.metaverse.api.analyzer.kettle.ComponentDerivationRecord) SelectMetadataChange(org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange) HashSet(java.util.HashSet)

Example 3 with SelectMetadataChange

use of org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange in project pdi-dataservice-server-plugin by pentaho.

the class SqlTransGenerator method generateConversionStep.

private StepMeta generateConversionStep() {
    // Set conversion masks for each column, modify type where required.
    SelectValuesMeta meta = new SelectValuesMeta();
    meta.allocate(0, 0, serviceFields.size());
    meta.setMeta(serviceFields.getValueMetaList().stream().map(valueMeta -> getSelectMetadataChange(meta, valueMeta)).collect(Collectors.toList()).toArray(new SelectMetadataChange[serviceFields.size()]));
    StepMeta stepMeta = new StepMeta("Set Conversion", meta);
    stepMeta.setLocation(xLocation, 50);
    xLocation += 100;
    stepMeta.setDraw(true);
    return stepMeta;
}
Also used : SQL(org.pentaho.di.core.sql.SQL) CalculatorMetaFunction(org.pentaho.di.trans.steps.calculator.CalculatorMetaFunction) KettleException(org.pentaho.di.core.exception.KettleException) CalculatorMeta(org.pentaho.di.trans.steps.calculator.CalculatorMeta) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) InjectorMeta(org.pentaho.di.trans.steps.injector.InjectorMeta) SampleRowsMeta(org.pentaho.di.trans.steps.samplerows.SampleRowsMeta) TYPE_NONE(org.pentaho.di.core.row.ValueMetaInterface.TYPE_NONE) TransMeta(org.pentaho.di.trans.TransMeta) Const(org.pentaho.di.core.Const) DummyTransMeta(org.pentaho.di.trans.steps.dummytrans.DummyTransMeta) SQLAggregation(org.pentaho.di.core.sql.SQLAggregation) Condition(org.pentaho.di.core.Condition) StepMeta(org.pentaho.di.trans.step.StepMeta) StepMetaInterface(org.pentaho.di.trans.step.StepMetaInterface) MemoryGroupByMeta(org.pentaho.di.trans.steps.memgroupby.MemoryGroupByMeta) SelectValuesMeta(org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta) SelectMetadataChange(org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange) ConstantMeta(org.pentaho.di.trans.steps.constant.ConstantMeta) TYPE_NUMBER(org.pentaho.di.core.row.ValueMetaInterface.TYPE_NUMBER) TYPE_INTEGER(org.pentaho.di.core.row.ValueMetaInterface.TYPE_INTEGER) SQLField(org.pentaho.di.core.sql.SQLField) Utils(org.pentaho.di.core.util.Utils) IifFunction(org.pentaho.di.core.sql.IifFunction) Collectors(java.util.stream.Collectors) List(java.util.List) StreamInterface(org.pentaho.di.trans.step.errorhandling.StreamInterface) TransHopMeta(org.pentaho.di.trans.TransHopMeta) FilterRowsMeta(org.pentaho.di.trans.steps.filterrows.FilterRowsMeta) SQLFields(org.pentaho.di.core.sql.SQLFields) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) SortRowsMeta(org.pentaho.di.trans.steps.sort.SortRowsMeta) SelectValuesMeta(org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta) SelectMetadataChange(org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange) StepMeta(org.pentaho.di.trans.step.StepMeta)

Example 4 with SelectMetadataChange

use of org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange in project pentaho-metaverse by pentaho.

the class SelectValuesStepAnalyzer method getUsedFields.

@Override
protected Set<StepField> getUsedFields(SelectValuesMeta meta) {
    Set<StepField> usedFields = new HashSet<>();
    String[] fieldNames = meta.getSelectName();
    for (String fieldName : fieldNames) {
        usedFields.addAll(createStepFields(fieldName, getInputs()));
    }
    SelectMetadataChange[] selectMetadataChanges = meta.getMeta();
    for (SelectMetadataChange selectMetadataChange : selectMetadataChanges) {
        usedFields.addAll(createStepFields(selectMetadataChange.getName(), getInputs()));
    }
    return usedFields;
}
Also used : StepField(org.pentaho.metaverse.api.StepField) SelectMetadataChange(org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange) HashSet(java.util.HashSet)

Example 5 with SelectMetadataChange

use of org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange in project pentaho-metaverse by pentaho.

the class SelectValuesStepAnalyzer method isPassthrough.

@Override
protected boolean isPassthrough(StepField originalFieldName) {
    // there are no entries on the select tab AND the field is NOT on the deleted tab OR meta tab
    if (ArrayUtils.isEmpty(baseStepMeta.getSelectName())) {
        SelectMetadataChange[] changes = baseStepMeta.getMeta();
        boolean isOnMetaTab = false;
        for (int i = 0; i < changes.length; i++) {
            SelectMetadataChange change = changes[i];
            if (originalFieldName.getFieldName().equals(change.getName())) {
                isOnMetaTab = true;
                break;
            }
        }
        boolean isDeleted = ArrayUtils.contains(baseStepMeta.getDeleteName(), originalFieldName.getFieldName());
        return (!(isOnMetaTab || isDeleted));
    }
    return false;
}
Also used : SelectMetadataChange(org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange)

Aggregations

SelectMetadataChange (org.pentaho.di.trans.steps.selectvalues.SelectMetadataChange)13 Test (org.junit.Test)4 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)4 StepField (org.pentaho.metaverse.api.StepField)4 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)3 HashSet (java.util.HashSet)2 TableItem (org.eclipse.swt.widgets.TableItem)2 KettleException (org.pentaho.di.core.exception.KettleException)2 TransHopMeta (org.pentaho.di.trans.TransHopMeta)2 TransMeta (org.pentaho.di.trans.TransMeta)2 StepMeta (org.pentaho.di.trans.step.StepMeta)2 SelectValuesMeta (org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta)2 ComponentDerivationRecord (org.pentaho.metaverse.api.analyzer.kettle.ComponentDerivationRecord)2 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Before (org.junit.Before)1 Condition (org.pentaho.di.core.Condition)1 Const (org.pentaho.di.core.Const)1 NotePadMeta (org.pentaho.di.core.NotePadMeta)1 Database (org.pentaho.di.core.database.Database)1