Search in sources :

Example 1 with RowMeta

use of org.apache.hop.core.row.RowMeta in project hop by apache.

the class Database method getGeneratedKeys.

/**
 * @param ps The prepared insert statement to use
 * @return The generated keys in auto-increment fields
 * @throws HopDatabaseException in case something goes wrong retrieving the keys.
 */
public RowMetaAndData getGeneratedKeys(PreparedStatement ps) throws HopDatabaseException {
    ResultSet keys = null;
    try {
        // 1 row of keys
        keys = ps.getGeneratedKeys();
        ResultSetMetaData resultSetMetaData = keys.getMetaData();
        if (resultSetMetaData == null) {
            resultSetMetaData = ps.getMetaData();
        }
        IRowMeta rowMeta;
        if (resultSetMetaData == null) {
            rowMeta = new RowMeta();
            rowMeta.addValueMeta(new ValueMetaInteger("ai-key"));
        } else {
            rowMeta = getRowInfo(resultSetMetaData, false, false);
        }
        return new RowMetaAndData(rowMeta, getRow(keys, resultSetMetaData, rowMeta));
    } catch (Exception ex) {
        throw new HopDatabaseException("Unable to retrieve key(s) from auto-increment field(s)", ex);
    } finally {
        if (keys != null) {
            try {
                keys.close();
            } catch (SQLException e) {
                log.logError("Unable to close resultset of auto-generated keys", e);
            }
        }
    }
}
Also used : RowMeta(org.apache.hop.core.row.RowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) HopDatabaseBatchException(org.apache.hop.core.exception.HopDatabaseBatchException) HopException(org.apache.hop.core.exception.HopException) HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) HopValueException(org.apache.hop.core.exception.HopValueException)

Example 2 with RowMeta

use of org.apache.hop.core.row.RowMeta in project hop by apache.

the class Database method getAlterTableStatement.

public String getAlterTableStatement(String tableName, IRowMeta fields, String tk, boolean useAutoIncrement, String pk, boolean semicolon) throws HopDatabaseException {
    String retval = "";
    // Get the fields that are in the table now:
    IRowMeta tabFields = getTableFields(tableName);
    // Don't forget to quote these as well...
    databaseMeta.quoteReservedWords(tabFields);
    // Find the missing fields
    IRowMeta missing = new RowMeta();
    for (int i = 0; i < fields.size(); i++) {
        IValueMeta v = fields.getValueMeta(i);
        // Not found?
        if (tabFields.searchValueMeta(v.getName()) == null) {
            // nope --> Missing!
            missing.addValueMeta(v);
        }
    }
    if (missing.size() != 0) {
        for (int i = 0; i < missing.size(); i++) {
            IValueMeta v = missing.getValueMeta(i);
            retval += databaseMeta.getAddColumnStatement(tableName, v, tk, useAutoIncrement, pk, true);
        }
    }
    // Find the surplus fields
    IRowMeta surplus = new RowMeta();
    for (int i = 0; i < tabFields.size(); i++) {
        IValueMeta v = tabFields.getValueMeta(i);
        // Found in table, not in input ?
        if (fields.searchValueMeta(v.getName()) == null) {
            // yes --> surplus!
            surplus.addValueMeta(v);
        }
    }
    if (surplus.size() != 0) {
        for (int i = 0; i < surplus.size(); i++) {
            IValueMeta v = surplus.getValueMeta(i);
            retval += databaseMeta.getDropColumnStatement(tableName, v, tk, useAutoIncrement, pk, true);
        }
    }
    // 
    // OK, see if there are fields for which we need to modify the type...
    // (length, precision)
    // 
    IRowMeta modify = new RowMeta();
    for (int i = 0; i < fields.size(); i++) {
        IValueMeta desiredField = fields.getValueMeta(i);
        IValueMeta currentField = tabFields.searchValueMeta(desiredField.getName());
        if (desiredField != null && currentField != null) {
            String desiredDDL = databaseMeta.getFieldDefinition(desiredField, tk, pk, useAutoIncrement);
            String currentDDL = databaseMeta.getFieldDefinition(currentField, tk, pk, useAutoIncrement);
            boolean mod = !desiredDDL.equalsIgnoreCase(currentDDL);
            if (mod) {
                modify.addValueMeta(desiredField);
            }
        }
    }
    if (modify.size() > 0) {
        for (int i = 0; i < modify.size(); i++) {
            IValueMeta v = modify.getValueMeta(i);
            retval += databaseMeta.getModifyColumnStatement(tableName, v, tk, useAutoIncrement, pk, true);
        }
    }
    return retval;
}
Also used : IValueMeta(org.apache.hop.core.row.IValueMeta) RowMeta(org.apache.hop.core.row.RowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) HopExtensionPoint(org.apache.hop.core.extension.HopExtensionPoint)

Example 3 with RowMeta

use of org.apache.hop.core.row.RowMeta in project hop by apache.

the class Database method getOneRow.

public RowMetaAndData getOneRow(String sql, IRowMeta param, Object[] data) throws HopDatabaseException {
    ResultSet rs = openQuery(sql, param, data);
    if (rs != null) {
        // One value: a number
        Object[] row = getRow(rs);
        rowMeta = null;
        RowMeta tmpMeta = null;
        try {
            ResultSetMetaData md = rs.getMetaData();
            tmpMeta = getMetaFromRow(row, md);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
            } catch (Exception e) {
                log.logError("Unable to close resultset", e);
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (Exception e) {
                    log.logError("Unable to close prepared statement pstmt", e);
                }
                pstmt = null;
            }
            if (selStmt != null) {
                try {
                    selStmt.close();
                } catch (Exception e) {
                    log.logError("Unable to close prepared statement sel_stmt", e);
                }
                selStmt = null;
            }
        }
        return new RowMetaAndData(tmpMeta, row);
    } else {
        return null;
    }
}
Also used : RowMeta(org.apache.hop.core.row.RowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) FileObject(org.apache.commons.vfs2.FileObject) HopDatabaseBatchException(org.apache.hop.core.exception.HopDatabaseBatchException) HopException(org.apache.hop.core.exception.HopException) HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) HopValueException(org.apache.hop.core.exception.HopValueException)

Example 4 with RowMeta

use of org.apache.hop.core.row.RowMeta in project hop by apache.

the class Database method getMetaFromRow.

public RowMeta getMetaFromRow(Object[] row, ResultSetMetaData md) throws SQLException, HopDatabaseException {
    RowMeta meta = new RowMeta();
    for (int i = 0; i < md.getColumnCount(); i++) {
        IValueMeta valueMeta = getValueFromSqlType(md, i + 1, true, false);
        meta.addValueMeta(valueMeta);
    }
    return meta;
}
Also used : IValueMeta(org.apache.hop.core.row.IValueMeta) RowMeta(org.apache.hop.core.row.RowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) HopExtensionPoint(org.apache.hop.core.extension.HopExtensionPoint)

Example 5 with RowMeta

use of org.apache.hop.core.row.RowMeta in project hop by apache.

the class Database method getQueryFieldsFromDatabaseMetaData.

private IRowMeta getQueryFieldsFromDatabaseMetaData(String sql) throws Exception {
    ResultSet columns = connection.getMetaData().getColumns("", "", StringUtils.isNotBlank(sql) ? sql : databaseMeta.getName(), "");
    IRowMeta rowMeta = new RowMeta();
    while (columns.next()) {
        IValueMeta valueMeta = null;
        String name = columns.getString("COLUMN_NAME");
        String type = columns.getString("SOURCE_DATA_TYPE");
        int size = columns.getInt("COLUMN_SIZE");
        if (type.equals("Integer") || type.equals("Long")) {
            valueMeta = new ValueMetaInteger();
        } else if (type.equals("BigDecimal") || type.equals("BigNumber")) {
            valueMeta = new ValueMetaBigNumber();
        } else if (type.equals("Double") || type.equals("Number")) {
            valueMeta = new ValueMetaNumber();
        } else if (type.equals("String")) {
            valueMeta = new ValueMetaString();
        } else if (type.equals("Date")) {
            valueMeta = new ValueMetaDate();
        } else if (type.equals("Boolean")) {
            valueMeta = new ValueMetaBoolean();
        } else if (type.equals("Binary")) {
            valueMeta = new ValueMetaBinary();
        } else if (type.equals("Timestamp")) {
            valueMeta = new ValueMetaTimestamp();
        } else if (type.equals("Internet Address")) {
            valueMeta = new ValueMetaInternetAddress();
        }
        if (valueMeta != null) {
            valueMeta.setName(name);
            valueMeta.setComments(name);
            valueMeta.setLength(size);
            valueMeta.setOriginalColumnTypeName(type);
            valueMeta.setConversionMask(columns.getString("SOURCE_MASK"));
            valueMeta.setDecimalSymbol(columns.getString("SOURCE_DECIMAL_SYMBOL"));
            valueMeta.setGroupingSymbol(columns.getString("SOURCE_GROUPING_SYMBOL"));
            valueMeta.setCurrencySymbol(columns.getString("SOURCE_CURRENCY_SYMBOL"));
            rowMeta.addValueMeta(valueMeta);
        } else {
            log.logBasic("Database.getQueryFields() IValueMeta mapping not resolved for the column " + name);
            rowMeta = null;
            break;
        }
    }
    if (rowMeta != null && !rowMeta.isEmpty()) {
        return rowMeta;
    } else {
        throw new Exception("Error in Database.getQueryFields()");
    }
}
Also used : RowMeta(org.apache.hop.core.row.RowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) HopExtensionPoint(org.apache.hop.core.extension.HopExtensionPoint) HopDatabaseBatchException(org.apache.hop.core.exception.HopDatabaseBatchException) HopException(org.apache.hop.core.exception.HopException) HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) HopValueException(org.apache.hop.core.exception.HopValueException) IValueMeta(org.apache.hop.core.row.IValueMeta)

Aggregations

RowMeta (org.apache.hop.core.row.RowMeta)332 IRowMeta (org.apache.hop.core.row.IRowMeta)236 IValueMeta (org.apache.hop.core.row.IValueMeta)116 ValueMetaString (org.apache.hop.core.row.value.ValueMetaString)107 HopException (org.apache.hop.core.exception.HopException)94 Test (org.junit.Test)81 HopTransformException (org.apache.hop.core.exception.HopTransformException)45 ValueMetaInteger (org.apache.hop.core.row.value.ValueMetaInteger)43 ArrayList (java.util.ArrayList)40 ILoggingObject (org.apache.hop.core.logging.ILoggingObject)26 ValueMetaBase (org.apache.hop.core.row.value.ValueMetaBase)25 Variables (org.apache.hop.core.variables.Variables)23 IRowSet (org.apache.hop.core.IRowSet)22 DatabaseMeta (org.apache.hop.core.database.DatabaseMeta)22 ValueMetaDate (org.apache.hop.core.row.value.ValueMetaDate)22 FileObject (org.apache.commons.vfs2.FileObject)20 RowMetaAndData (org.apache.hop.core.RowMetaAndData)20 IVariables (org.apache.hop.core.variables.IVariables)20 HopDatabaseException (org.apache.hop.core.exception.HopDatabaseException)17 TransformMeta (org.apache.hop.pipeline.transform.TransformMeta)17