Search in sources :

Example 11 with ValueMetaNumber

use of org.pentaho.di.core.row.value.ValueMetaNumber in project pentaho-kettle by pentaho.

the class H2DatabaseMetaTest method testSQLStatements.

@Test
public void testSQLStatements() {
    assertEquals("TRUNCATE TABLE FOO", nativeMeta.getTruncateTableStatement("FOO"));
    assertEquals("SELECT * FROM FOO", nativeMeta.getSQLQueryFields("FOO"));
    assertEquals("SELECT 1 FROM FOO", nativeMeta.getSQLTableExists("FOO"));
    assertEquals("ALTER TABLE FOO ADD BAR TIMESTAMP", nativeMeta.getAddColumnStatement("FOO", new ValueMetaDate("BAR"), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR TIMESTAMP", nativeMeta.getAddColumnStatement("FOO", new ValueMetaTimestamp("BAR"), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR CHAR(1)", nativeMeta.getAddColumnStatement("FOO", new ValueMetaBoolean("BAR"), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR BIGINT", nativeMeta.getAddColumnStatement("FOO", new ValueMetaNumber("BAR", 10, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR BIGINT", nativeMeta.getAddColumnStatement("FOO", new ValueMetaBigNumber("BAR", 10, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR BIGINT", nativeMeta.getAddColumnStatement("FOO", new ValueMetaInteger("BAR", 10, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR DOUBLE", nativeMeta.getAddColumnStatement("FOO", new ValueMetaNumber("BAR", 0, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR INTEGER", nativeMeta.getAddColumnStatement("FOO", new ValueMetaNumber("BAR", 5, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR DECIMAL(10, 3)", nativeMeta.getAddColumnStatement("FOO", new ValueMetaNumber("BAR", 10, 3), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR DECIMAL(10, 3)", nativeMeta.getAddColumnStatement("FOO", new ValueMetaBigNumber("BAR", 10, 3), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR DECIMAL(21, 4)", nativeMeta.getAddColumnStatement("FOO", new ValueMetaBigNumber("BAR", 21, 4), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR TEXT", nativeMeta.getAddColumnStatement("FOO", new ValueMetaString("BAR", nativeMeta.getMaxVARCHARLength() + 2, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR VARCHAR(15)", nativeMeta.getAddColumnStatement("FOO", new ValueMetaString("BAR", 15, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR BIGINT", // Bug here - invalid SQL
    nativeMeta.getAddColumnStatement("FOO", new ValueMetaNumber("BAR", 10, -7), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR DECIMAL(22, 7)", nativeMeta.getAddColumnStatement("FOO", new ValueMetaBigNumber("BAR", 22, 7), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR DOUBLE", nativeMeta.getAddColumnStatement("FOO", new ValueMetaNumber("BAR", -10, 7), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR DECIMAL(5, 7)", nativeMeta.getAddColumnStatement("FOO", new ValueMetaNumber("BAR", 5, 7), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR UNKNOWN", nativeMeta.getAddColumnStatement("FOO", new ValueMetaInternetAddress("BAR"), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR IDENTITY", nativeMeta.getAddColumnStatement("FOO", new ValueMetaInteger("BAR"), "BAR", true, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR IDENTITY", nativeMeta.getAddColumnStatement("FOO", new ValueMetaNumber("BAR", 26, 8), "BAR", true, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR IDENTITY", nativeMeta.getAddColumnStatement("FOO", new ValueMetaNumber("BAR", 26, 8), "", true, "BAR", false));
    String lineSep = System.getProperty("line.separator");
    assertEquals("ALTER TABLE FOO DROP BAR" + lineSep, nativeMeta.getDropColumnStatement("FOO", new ValueMetaString("BAR", 15, 0), "", false, "", true));
    assertEquals("ALTER TABLE FOO ALTER BAR VARCHAR(15)", nativeMeta.getModifyColumnStatement("FOO", new ValueMetaString("BAR", 15, 0), "", false, "", true));
    assertEquals("ALTER TABLE FOO ALTER BAR VARCHAR(2147483647)", nativeMeta.getModifyColumnStatement("FOO", new ValueMetaString("BAR"), "", false, "", true));
    assertEquals("ALTER TABLE FOO ADD BAR SMALLINT", nativeMeta.getAddColumnStatement("FOO", new ValueMetaInteger("BAR", 4, 0), "", true, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR TINYINT", nativeMeta.getAddColumnStatement("FOO", new ValueMetaInteger("BAR", 2, 0), "", true, "", false));
    // do a boolean check
    odbcMeta.setSupportsBooleanDataType(true);
    assertEquals("ALTER TABLE FOO ADD BAR BOOLEAN", odbcMeta.getAddColumnStatement("FOO", new ValueMetaBoolean("BAR"), "", false, "", false));
    odbcMeta.setSupportsBooleanDataType(false);
    assertEquals("ALTER TABLE FOO ADD BAR IDENTITY", nativeMeta.getAddColumnStatement("FOO", new ValueMetaInteger("BAR"), "BAR", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR BIGINT", nativeMeta.getAddColumnStatement("FOO", new ValueMetaBigNumber("BAR", 10, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR DECIMAL(22, 0)", nativeMeta.getAddColumnStatement("FOO", new ValueMetaBigNumber("BAR", 22, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR VARCHAR(1)", nativeMeta.getAddColumnStatement("FOO", new ValueMetaString("BAR", 1, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR TEXT", nativeMeta.getAddColumnStatement("FOO", new ValueMetaString("BAR", 16777250, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR BLOB", nativeMeta.getAddColumnStatement("FOO", new ValueMetaBinary("BAR", 16777250, 0), "", false, "", false));
    assertEquals("insert into FOO(FOOKEY, FOOVERSION) values (0, 1)", nativeMeta.getSQLInsertAutoIncUnknownDimensionRow("FOO", "FOOKEY", "FOOVERSION"));
}
Also used : ValueMetaTimestamp(org.pentaho.di.core.row.value.ValueMetaTimestamp) ValueMetaBinary(org.pentaho.di.core.row.value.ValueMetaBinary) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) ValueMetaNumber(org.pentaho.di.core.row.value.ValueMetaNumber) ValueMetaBigNumber(org.pentaho.di.core.row.value.ValueMetaBigNumber) ValueMetaInternetAddress(org.pentaho.di.core.row.value.ValueMetaInternetAddress) ValueMetaBoolean(org.pentaho.di.core.row.value.ValueMetaBoolean) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) ValueMetaDate(org.pentaho.di.core.row.value.ValueMetaDate) Test(org.junit.Test)

Example 12 with ValueMetaNumber

use of org.pentaho.di.core.row.value.ValueMetaNumber in project pentaho-kettle by pentaho.

the class InfiniDbDatabaseMetaTest method testSQLStatements.

@Test
public void testSQLStatements() {
    assertEquals(" LIMIT 15", nativeMeta.getLimitClause(15));
    assertEquals("SELECT * FROM FOO LIMIT 0", nativeMeta.getSQLQueryFields("FOO"));
    assertEquals("SELECT * FROM FOO LIMIT 0", nativeMeta.getSQLTableExists("FOO"));
    assertEquals("SELECT FOO FROM BAR LIMIT 0", nativeMeta.getSQLQueryColumnFields("FOO", "BAR"));
    assertEquals("ALTER TABLE FOO ADD BAR DATETIME", nativeMeta.getAddColumnStatement("FOO", new ValueMetaDate("BAR"), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR DATETIME", nativeMeta.getAddColumnStatement("FOO", new ValueMetaTimestamp("BAR"), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR CHAR(1)", nativeMeta.getAddColumnStatement("FOO", new ValueMetaBoolean("BAR"), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR BIGINT", nativeMeta.getAddColumnStatement("FOO", new ValueMetaNumber("BAR", 10, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR BIGINT", nativeMeta.getAddColumnStatement("FOO", new ValueMetaBigNumber("BAR", 10, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR BIGINT", nativeMeta.getAddColumnStatement("FOO", new ValueMetaInteger("BAR", 10, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR INT", nativeMeta.getAddColumnStatement("FOO", new ValueMetaNumber("BAR", 0, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR INT", nativeMeta.getAddColumnStatement("FOO", new ValueMetaNumber("BAR", 5, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR DOUBLE", nativeMeta.getAddColumnStatement("FOO", new ValueMetaNumber("BAR", 10, 3), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR DOUBLE", nativeMeta.getAddColumnStatement("FOO", new ValueMetaBigNumber("BAR", 10, 3), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR DECIMAL(21, 4)", nativeMeta.getAddColumnStatement("FOO", new ValueMetaBigNumber("BAR", 21, 4), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR MEDIUMTEXT", nativeMeta.getAddColumnStatement("FOO", new ValueMetaString("BAR", nativeMeta.getMaxVARCHARLength() + 2, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR VARCHAR(15)", nativeMeta.getAddColumnStatement("FOO", new ValueMetaString("BAR", 15, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR DOUBLE", // Bug here - invalid SQL
    nativeMeta.getAddColumnStatement("FOO", new ValueMetaNumber("BAR", 10, -7), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR DECIMAL(22, 7)", nativeMeta.getAddColumnStatement("FOO", new ValueMetaBigNumber("BAR", 22, 7), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR DOUBLE", nativeMeta.getAddColumnStatement("FOO", new ValueMetaNumber("BAR", -10, 7), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR DOUBLE", nativeMeta.getAddColumnStatement("FOO", new ValueMetaNumber("BAR", 5, 7), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR  UNKNOWN", nativeMeta.getAddColumnStatement("FOO", new ValueMetaInternetAddress("BAR"), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY", nativeMeta.getAddColumnStatement("FOO", new ValueMetaInteger("BAR"), "BAR", true, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY", nativeMeta.getAddColumnStatement("FOO", new ValueMetaNumber("BAR", 26, 8), "BAR", true, "", false));
    String lineSep = System.getProperty("line.separator");
    assertEquals("ALTER TABLE FOO DROP BAR" + lineSep, nativeMeta.getDropColumnStatement("FOO", new ValueMetaString("BAR", 15, 0), "", false, "", true));
    assertEquals("ALTER TABLE FOO MODIFY BAR VARCHAR(15)", nativeMeta.getModifyColumnStatement("FOO", new ValueMetaString("BAR", 15, 0), "", false, "", true));
    assertEquals("ALTER TABLE FOO MODIFY BAR TINYTEXT", nativeMeta.getModifyColumnStatement("FOO", new ValueMetaString("BAR"), "", false, "", true));
    // some subclass of the MSSQL meta probably ...
    odbcMeta.setSupportsBooleanDataType(true);
    assertEquals("ALTER TABLE FOO ADD BAR BOOLEAN", odbcMeta.getAddColumnStatement("FOO", new ValueMetaBoolean("BAR"), "", false, "", false));
    odbcMeta.setSupportsBooleanDataType(false);
    assertEquals("ALTER TABLE FOO ADD BAR INT", nativeMeta.getAddColumnStatement("FOO", new ValueMetaInteger("BAR", 4, 0), "", true, "", false));
    // do a boolean check
    odbcMeta.setSupportsBooleanDataType(true);
    assertEquals("ALTER TABLE FOO ADD BAR BOOLEAN", odbcMeta.getAddColumnStatement("FOO", new ValueMetaBoolean("BAR"), "", false, "", false));
    odbcMeta.setSupportsBooleanDataType(false);
    assertEquals("ALTER TABLE FOO ADD BAR BIGINT NOT NULL PRIMARY KEY", nativeMeta.getAddColumnStatement("FOO", new ValueMetaInteger("BAR"), "BAR", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR BIGINT", nativeMeta.getAddColumnStatement("FOO", new ValueMetaBigNumber("BAR", 10, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR DECIMAL(22)", nativeMeta.getAddColumnStatement("FOO", new ValueMetaBigNumber("BAR", 22, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR CHAR(1)", nativeMeta.getAddColumnStatement("FOO", new ValueMetaString("BAR", 1, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR LONGTEXT", nativeMeta.getAddColumnStatement("FOO", new ValueMetaString("BAR", 16777250, 0), "", false, "", false));
    assertEquals("ALTER TABLE FOO ADD BAR LONGBLOB", nativeMeta.getAddColumnStatement("FOO", new ValueMetaBinary("BAR", 16777250, 0), "", false, "", false));
    assertEquals("LOCK TABLES FOO WRITE, BAR WRITE;" + lineSep, nativeMeta.getSQLLockTables(new String[] { "FOO", "BAR" }));
    assertEquals("UNLOCK TABLES", nativeMeta.getSQLUnlockTables(new String[] {}));
    assertEquals("insert into FOO(FOOKEY, FOOVERSION) values (1, 1)", nativeMeta.getSQLInsertAutoIncUnknownDimensionRow("FOO", "FOOKEY", "FOOVERSION"));
}
Also used : ValueMetaTimestamp(org.pentaho.di.core.row.value.ValueMetaTimestamp) ValueMetaBinary(org.pentaho.di.core.row.value.ValueMetaBinary) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) ValueMetaNumber(org.pentaho.di.core.row.value.ValueMetaNumber) ValueMetaBigNumber(org.pentaho.di.core.row.value.ValueMetaBigNumber) ValueMetaInternetAddress(org.pentaho.di.core.row.value.ValueMetaInternetAddress) ValueMetaBoolean(org.pentaho.di.core.row.value.ValueMetaBoolean) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) ValueMetaDate(org.pentaho.di.core.row.value.ValueMetaDate) Test(org.junit.Test)

Example 13 with ValueMetaNumber

use of org.pentaho.di.core.row.value.ValueMetaNumber in project pentaho-kettle by pentaho.

the class ExcelInput method fillRow.

/**
 * Build an empty row based on the meta-data...
 *
 * @return
 */
private Object[] fillRow(int startcolumn, ExcelInputRow excelInputRow) throws KettleException {
    Object[] r = new Object[data.outputRowMeta.size()];
    // Keep track whether or not we handled an error for this line yet.
    boolean errorHandled = false;
    // Set values in the row...
    KCell cell = null;
    for (int i = startcolumn; i < excelInputRow.cells.length && i - startcolumn < meta.getField().length; i++) {
        cell = excelInputRow.cells[i];
        int rowcolumn = i - startcolumn;
        if (cell == null) {
            r[rowcolumn] = null;
            continue;
        }
        ValueMetaInterface targetMeta = data.outputRowMeta.getValueMeta(rowcolumn);
        ValueMetaInterface sourceMeta = null;
        try {
            checkType(cell, targetMeta);
        } catch (KettleException ex) {
            if (!meta.isErrorIgnored()) {
                ex = new KettleCellValueException(ex, this.data.sheetnr, this.data.rownr, i, "");
                throw ex;
            }
            if (log.isBasic()) {
                logBasic(BaseMessages.getString(PKG, "ExcelInput.Log.WarningProcessingExcelFile", "" + targetMeta, "" + data.filename, ex.getMessage()));
            }
            if (!errorHandled) {
                data.errorHandler.handleLineError(excelInputRow.rownr, excelInputRow.sheetName);
                errorHandled = true;
            }
            if (meta.isErrorLineSkipped()) {
                return null;
            }
        }
        KCellType cellType = cell.getType();
        if (KCellType.BOOLEAN == cellType || KCellType.BOOLEAN_FORMULA == cellType) {
            r[rowcolumn] = cell.getValue();
            sourceMeta = data.valueMetaBoolean;
        } else {
            if (KCellType.DATE.equals(cellType) || KCellType.DATE_FORMULA.equals(cellType)) {
                Date date = (Date) cell.getValue();
                long time = date.getTime();
                int offset = TimeZone.getDefault().getOffset(time);
                r[rowcolumn] = new Date(time - offset);
                sourceMeta = data.valueMetaDate;
            } else {
                if (KCellType.LABEL == cellType || KCellType.STRING_FORMULA == cellType) {
                    String string = (String) cell.getValue();
                    switch(meta.getField()[rowcolumn].getTrimType()) {
                        case ExcelInputMeta.TYPE_TRIM_LEFT:
                            string = Const.ltrim(string);
                            break;
                        case ExcelInputMeta.TYPE_TRIM_RIGHT:
                            string = Const.rtrim(string);
                            break;
                        case ExcelInputMeta.TYPE_TRIM_BOTH:
                            string = Const.trim(string);
                            break;
                        default:
                            break;
                    }
                    r[rowcolumn] = string;
                    sourceMeta = data.valueMetaString;
                } else {
                    if (KCellType.NUMBER == cellType || KCellType.NUMBER_FORMULA == cellType) {
                        r[rowcolumn] = cell.getValue();
                        sourceMeta = data.valueMetaNumber;
                    } else {
                        if (log.isDetailed()) {
                            KCellType ct = cell.getType();
                            logDetailed(BaseMessages.getString(PKG, "ExcelInput.Log.UnknownType", ((ct != null) ? ct.toString() : "null"), cell.getContents()));
                        }
                        r[rowcolumn] = null;
                    }
                }
            }
        }
        ExcelInputField field = meta.getField()[rowcolumn];
        // 
        try {
            // 
            if (sourceMeta != null && sourceMeta.getType() != targetMeta.getType() && r[rowcolumn] != null) {
                ValueMetaInterface sourceMetaCopy = sourceMeta.clone();
                sourceMetaCopy.setConversionMask(field.getFormat());
                sourceMetaCopy.setGroupingSymbol(field.getGroupSymbol());
                sourceMetaCopy.setDecimalSymbol(field.getDecimalSymbol());
                sourceMetaCopy.setCurrencySymbol(field.getCurrencySymbol());
                switch(targetMeta.getType()) {
                    // 
                    case ValueMetaInterface.TYPE_NUMBER:
                    case ValueMetaInterface.TYPE_INTEGER:
                        switch(field.getType()) {
                            case ValueMetaInterface.TYPE_DATE:
                                // number to string conversion (20070522.00 --> "20070522")
                                // 
                                ValueMetaInterface valueMetaNumber = new ValueMetaNumber("num");
                                valueMetaNumber.setConversionMask("#");
                                Object string = sourceMetaCopy.convertData(valueMetaNumber, r[rowcolumn]);
                                // String to date with mask...
                                // 
                                r[rowcolumn] = targetMeta.convertData(sourceMetaCopy, string);
                                break;
                            default:
                                r[rowcolumn] = targetMeta.convertData(sourceMetaCopy, r[rowcolumn]);
                                break;
                        }
                        break;
                    // 
                    default:
                        r[rowcolumn] = targetMeta.convertData(sourceMetaCopy, r[rowcolumn]);
                }
            }
        } catch (KettleException ex) {
            if (!meta.isErrorIgnored()) {
                ex = new KettleCellValueException(ex, this.data.sheetnr, cell.getRow(), i, field.getName());
                throw ex;
            }
            if (log.isBasic()) {
                logBasic(BaseMessages.getString(PKG, "ExcelInput.Log.WarningProcessingExcelFile", "" + targetMeta, "" + data.filename, ex.toString()));
            }
            if (!errorHandled) {
                // check if we didn't log an error already for this one.
                data.errorHandler.handleLineError(excelInputRow.rownr, excelInputRow.sheetName);
                errorHandled = true;
            }
            if (meta.isErrorLineSkipped()) {
                return null;
            } else {
                r[rowcolumn] = null;
            }
        }
    }
    int rowIndex = meta.getField().length;
    // Do we need to include the filename?
    if (!Utils.isEmpty(meta.getFileField())) {
        r[rowIndex] = data.filename;
        rowIndex++;
    }
    // Do we need to include the sheetname?
    if (!Utils.isEmpty(meta.getSheetField())) {
        r[rowIndex] = excelInputRow.sheetName;
        rowIndex++;
    }
    // Do we need to include the sheet rownumber?
    if (!Utils.isEmpty(meta.getSheetRowNumberField())) {
        r[rowIndex] = new Long(data.rownr);
        rowIndex++;
    }
    // Do we need to include the rownumber?
    if (!Utils.isEmpty(meta.getRowNumberField())) {
        r[rowIndex] = new Long(getLinesWritten() + 1);
        rowIndex++;
    }
    // Possibly add short filename...
    if (!Utils.isEmpty(meta.getShortFileNameField())) {
        r[rowIndex] = data.shortFilename;
        rowIndex++;
    }
    // Add Extension
    if (!Utils.isEmpty(meta.getExtensionField())) {
        r[rowIndex] = data.extension;
        rowIndex++;
    }
    // add path
    if (!Utils.isEmpty(meta.getPathField())) {
        r[rowIndex] = data.path;
        rowIndex++;
    }
    // Add Size
    if (!Utils.isEmpty(meta.getSizeField())) {
        r[rowIndex] = new Long(data.size);
        rowIndex++;
    }
    // add Hidden
    if (!Utils.isEmpty(meta.isHiddenField())) {
        r[rowIndex] = new Boolean(data.hidden);
        rowIndex++;
    }
    // Add modification date
    if (!Utils.isEmpty(meta.getLastModificationDateField())) {
        r[rowIndex] = data.lastModificationDateTime;
        rowIndex++;
    }
    // Add Uri
    if (!Utils.isEmpty(meta.getUriField())) {
        r[rowIndex] = data.uriName;
        rowIndex++;
    }
    // Add RootUri
    if (!Utils.isEmpty(meta.getRootUriField())) {
        r[rowIndex] = data.rootUriName;
        rowIndex++;
    }
    return r;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KCell(org.pentaho.di.core.spreadsheet.KCell) Date(java.util.Date) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) KCellType(org.pentaho.di.core.spreadsheet.KCellType) ValueMetaNumber(org.pentaho.di.core.row.value.ValueMetaNumber) FileObject(org.apache.commons.vfs2.FileObject)

Example 14 with ValueMetaNumber

use of org.pentaho.di.core.row.value.ValueMetaNumber in project pentaho-kettle by pentaho.

the class MondrianHelper method createRectangularOutput.

/**
 * Outputs one row per tuple on the rows axis.
 *
 * @throws KettleDatabaseException
 *           in case some or other error occurs
 */
public void createRectangularOutput() throws KettleDatabaseException {
    final Axis[] axes = result.getAxes();
    if (axes.length != 2) {
        throw new KettleDatabaseException(BaseMessages.getString(PKG, "MondrianInputErrorOnlyTabular"));
    }
    headings = new ArrayList<>();
    rows = new ArrayList<>();
    final Axis rowsAxis = axes[1];
    final Axis columnsAxis = axes[0];
    int rowOrdinal = -1;
    int[] coords = { 0, 0 };
    for (Position rowPos : rowsAxis.getPositions()) {
        ++rowOrdinal;
        coords[1] = rowOrdinal;
        if (rowOrdinal == 0) {
            // First headings are for the members on the rows axis.
            for (Member rowMember : rowPos) {
                headings.add(rowMember.getHierarchy().getUniqueName());
            }
            // concatenate the unique names.
            for (Position columnPos : columnsAxis.getPositions()) {
                String heading = "";
                for (Member columnMember : columnPos) {
                    if (!heading.equals("")) {
                        heading += ", ";
                    }
                    heading += columnMember.getUniqueName();
                }
                headings.add(heading);
            }
        }
        List<Object> rowValues = new ArrayList<>();
        // The first row values describe the members on the rows axis.
        for (Member rowMember : rowPos) {
            rowValues.add(rowMember.getUniqueName());
        }
        // NOTE: Could also output all properties of each cell.
        for (int columnOrdinal = 0; columnOrdinal < columnsAxis.getPositions().size(); ++columnOrdinal) {
            coords[0] = columnOrdinal;
            final Cell cell = result.getCell(coords);
            rowValues.add(cell.getValue());
        }
        rows.add(rowValues);
    }
    outputRowMeta = new RowMeta();
    // column, keep scanning until we find one line that has an actual value
    if (rows.size() > 0) {
        int columnCount = rows.get(0).size();
        HashMap<Integer, ValueMetaInterface> valueMetaHash = new HashMap<>();
        for (int i = 0; i < rows.size(); i++) {
            List<Object> rowValues = rows.get(i);
            for (int c = 0; c < rowValues.size(); c++) {
                if (valueMetaHash.containsKey(new Integer(c))) {
                    // we have this value already
                    continue;
                }
                Object valueData = rowValues.get(c);
                if (valueData == null) {
                    // skip this row and look for the metadata in a new one
                    continue;
                }
                String valueName = headings.get(c);
                ValueMetaInterface valueMeta;
                if (valueData instanceof String) {
                    valueMeta = new ValueMetaString(valueName);
                } else if (valueData instanceof Date) {
                    valueMeta = new ValueMetaDate(valueName);
                } else if (valueData instanceof Boolean) {
                    valueMeta = new ValueMetaBoolean(valueName);
                } else if (valueData instanceof Integer) {
                    valueMeta = new ValueMetaInteger(valueName);
                    valueData = Long.valueOf(((Integer) valueData).longValue());
                } else if (valueData instanceof Short) {
                    valueMeta = new ValueMetaInteger(valueName);
                    valueData = Long.valueOf(((Short) valueData).longValue());
                } else if (valueData instanceof Byte) {
                    valueMeta = new ValueMetaInteger(valueName);
                    valueData = Long.valueOf(((Byte) valueData).longValue());
                } else if (valueData instanceof Long) {
                    valueMeta = new ValueMetaInteger(valueName);
                } else if (valueData instanceof Double) {
                    valueMeta = new ValueMetaNumber(valueName);
                } else if (valueData instanceof Float) {
                    valueMeta = new ValueMetaNumber(valueName);
                    valueData = Double.valueOf(((Float) valueData).doubleValue());
                } else if (valueData instanceof BigDecimal) {
                    valueMeta = new ValueMetaBigNumber(valueName);
                } else {
                    throw new KettleDatabaseException(BaseMessages.getString(PKG, "MondrianInputErrorUnhandledType", valueData.getClass().toString()));
                }
                valueMetaHash.put(c, valueMeta);
            }
            if (valueMetaHash.size() == columnCount) {
                // we're done
                break;
            }
        }
        // Build the list of valueMetas
        List<ValueMetaInterface> valueMetaList = new ArrayList<>();
        for (int c = 0; c < columnCount; c++) {
            if (valueMetaHash.containsKey(new Integer(c))) {
                valueMetaList.add(valueMetaHash.get(new Integer(c)));
            } else {
                // If the entire column is null, assume the missing data as String.
                // Irrelevant, anyway
                ValueMetaInterface valueMeta = new ValueMetaString(headings.get(c));
                valueMetaList.add(valueMeta);
            }
        }
        outputRowMeta.setValueMetaList(valueMetaList);
    }
    // Now that we painstakingly found the meta data that comes out of the
    // Mondrian database, cache it please...
    // 
    DBCacheEntry cacheEntry = new DBCacheEntry(databaseMeta.getName(), queryString);
    DBCache.getInstance().put(cacheEntry, outputRowMeta);
}
Also used : RowMeta(org.pentaho.di.core.row.RowMeta) HashMap(java.util.HashMap) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) ArrayList(java.util.ArrayList) ValueMetaBoolean(org.pentaho.di.core.row.value.ValueMetaBoolean) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) ValueMetaNumber(org.pentaho.di.core.row.value.ValueMetaNumber) ValueMetaBigNumber(org.pentaho.di.core.row.value.ValueMetaBigNumber) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger) ValueMetaBoolean(org.pentaho.di.core.row.value.ValueMetaBoolean) Member(mondrian.olap.Member) Cell(mondrian.olap.Cell) ValueMetaDate(org.pentaho.di.core.row.value.ValueMetaDate) Axis(mondrian.olap.Axis) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Position(mondrian.olap.Position) DBCacheEntry(org.pentaho.di.core.DBCacheEntry) Date(java.util.Date) ValueMetaDate(org.pentaho.di.core.row.value.ValueMetaDate) BigDecimal(java.math.BigDecimal) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger)

Example 15 with ValueMetaNumber

use of org.pentaho.di.core.row.value.ValueMetaNumber in project pentaho-kettle by pentaho.

the class MonetDBBulkLoader method init.

@Override
public boolean init(StepMetaInterface smi, StepDataInterface sdi) {
    meta = (MonetDBBulkLoaderMeta) smi;
    data = (MonetDBBulkLoaderData) sdi;
    if (super.init(smi, sdi)) {
        data.quote = environmentSubstitute(meta.getFieldEnclosure());
        data.separator = environmentSubstitute(meta.getFieldSeparator());
        String nulls = environmentSubstitute(meta.getNULLrepresentation());
        if (nulls == null) {
            data.nullrepresentation = new String();
        } else {
            data.nullrepresentation = nulls;
        }
        data.newline = Const.CR;
        String encoding = environmentSubstitute(meta.getEncoding());
        data.monetDateMeta = new ValueMetaDate("dateMeta");
        data.monetDateMeta.setConversionMask("yyyy/MM/dd");
        data.monetDateMeta.setStringEncoding(encoding);
        data.monetTimestampMeta = new ValueMetaDate("timestampMeta");
        data.monetTimestampMeta.setConversionMask("yyyy/MM/dd HH:mm:ss");
        data.monetTimestampMeta.setStringEncoding(encoding);
        data.monetTimeMeta = new ValueMetaDate("timeMeta");
        data.monetTimeMeta.setConversionMask("HH:mm:ss");
        data.monetTimeMeta.setStringEncoding(encoding);
        data.monetNumberMeta = new ValueMetaNumber("numberMeta");
        data.monetNumberMeta.setConversionMask("#.#");
        data.monetNumberMeta.setGroupingSymbol(",");
        data.monetNumberMeta.setDecimalSymbol(".");
        data.monetNumberMeta.setStringEncoding(encoding);
        data.bufferSize = Const.toInt(environmentSubstitute(meta.getBufferSize()), 100000);
        // Allocate the buffer
        // 
        // new byte[data.bufferSize][];
        data.rowBuffer = new String[data.bufferSize];
        data.bufferIndex = 0;
        // Make sure our database connection settings are consistent with our dialog settings by
        // altering the connection with an updated answer depending on the dialog setting.
        meta.getDatabaseMeta().setQuoteAllFields(meta.isFullyQuoteSQL());
        // Support parameterized database connection names
        String connectionName = meta.getDbConnectionName();
        if (!Utils.isEmpty(connectionName) && connectionName.startsWith("${") && connectionName.endsWith("}")) {
            meta.setDatabaseMeta(localTransMeta.findDatabase(environmentSubstitute(connectionName)));
        }
        // Schema-table combination...
        data.schemaTable = meta.getDatabaseMeta(this).getQuotedSchemaTableCombination(environmentSubstitute(meta.getSchemaName()), environmentSubstitute(meta.getTableName()));
        return true;
    }
    return false;
}
Also used : ValueMetaNumber(org.pentaho.di.core.row.value.ValueMetaNumber) ValueMetaDate(org.pentaho.di.core.row.value.ValueMetaDate)

Aggregations

ValueMetaNumber (org.pentaho.di.core.row.value.ValueMetaNumber)95 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)75 ValueMetaInteger (org.pentaho.di.core.row.value.ValueMetaInteger)52 Test (org.junit.Test)51 RowMeta (org.pentaho.di.core.row.RowMeta)39 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)34 ValueMetaDate (org.pentaho.di.core.row.value.ValueMetaDate)34 ValueMetaBoolean (org.pentaho.di.core.row.value.ValueMetaBoolean)32 ValueMetaBigNumber (org.pentaho.di.core.row.value.ValueMetaBigNumber)30 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)25 ValueMetaBinary (org.pentaho.di.core.row.value.ValueMetaBinary)20 ValueMetaTimestamp (org.pentaho.di.core.row.value.ValueMetaTimestamp)16 ValueMetaInternetAddress (org.pentaho.di.core.row.value.ValueMetaInternetAddress)15 KettleException (org.pentaho.di.core.exception.KettleException)12 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)11 KettleStepException (org.pentaho.di.core.exception.KettleStepException)7 ArrayList (java.util.ArrayList)6 Date (java.util.Date)6 RowSet (org.pentaho.di.core.RowSet)6 RowAdapter (org.pentaho.di.trans.step.RowAdapter)6