Search in sources :

Example 1 with ActiveFieldInfo

use of org.apache.apex.malhar.lib.db.jdbc.JdbcPOJOInputOperator.ActiveFieldInfo in project apex-malhar by apache.

the class JdbcPOJOPollInputOperator method setup.

@Override
public void setup(OperatorContext context) {
    super.setup(context);
    try {
        if (getColumnsExpression() == null) {
            StringBuilder columns = new StringBuilder();
            for (int i = 0; i < fieldInfos.size(); i++) {
                columns.append(fieldInfos.get(i).getColumnName());
                if (i < fieldInfos.size() - 1) {
                    columns.append(",");
                }
            }
            setColumnsExpression(columns.toString());
            LOG.debug("select expr {}", columns.toString());
        }
        if (columnDataTypes == null) {
            populateColumnDataTypes();
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    for (FieldInfo fi : fieldInfos) {
        columnFieldSetters.add(new ActiveFieldInfo(fi));
    }
}
Also used : ActiveFieldInfo(org.apache.apex.malhar.lib.db.jdbc.JdbcPOJOInputOperator.ActiveFieldInfo) SQLException(java.sql.SQLException) ActiveFieldInfo(org.apache.apex.malhar.lib.db.jdbc.JdbcPOJOInputOperator.ActiveFieldInfo) FieldInfo(org.apache.apex.malhar.lib.util.FieldInfo)

Example 2 with ActiveFieldInfo

use of org.apache.apex.malhar.lib.db.jdbc.JdbcPOJOInputOperator.ActiveFieldInfo in project apex-malhar by apache.

the class JdbcPOJOPollInputOperator method getTuple.

@SuppressWarnings("unchecked")
@Override
public Object getTuple(ResultSet result) {
    Object obj;
    try {
        obj = pojoClass.newInstance();
    } catch (InstantiationException | IllegalAccessException ex) {
        store.disconnect();
        throw new RuntimeException(ex);
    }
    try {
        for (int i = 0; i < columnDataTypes.size(); i++) {
            int type = columnDataTypes.get(i);
            ActiveFieldInfo afi = columnFieldSetters.get(i);
            switch(type) {
                case Types.CHAR:
                case Types.VARCHAR:
                    String strVal = result.getString(i + 1);
                    ((PojoUtils.Setter<Object, String>) afi.setterOrGetter).set(obj, strVal);
                    break;
                case Types.BOOLEAN:
                    boolean boolVal = result.getBoolean(i + 1);
                    ((PojoUtils.SetterBoolean<Object>) afi.setterOrGetter).set(obj, boolVal);
                    break;
                case Types.TINYINT:
                    byte byteVal = result.getByte(i + 1);
                    ((PojoUtils.SetterByte<Object>) afi.setterOrGetter).set(obj, byteVal);
                    break;
                case Types.SMALLINT:
                    short shortVal = result.getShort(i + 1);
                    ((PojoUtils.SetterShort<Object>) afi.setterOrGetter).set(obj, shortVal);
                    break;
                case Types.INTEGER:
                    int intVal = result.getInt(i + 1);
                    ((PojoUtils.SetterInt<Object>) afi.setterOrGetter).set(obj, intVal);
                    break;
                case Types.BIGINT:
                    long longVal = result.getLong(i + 1);
                    ((PojoUtils.SetterLong<Object>) afi.setterOrGetter).set(obj, longVal);
                    break;
                case Types.FLOAT:
                    float floatVal = result.getFloat(i + 1);
                    ((PojoUtils.SetterFloat<Object>) afi.setterOrGetter).set(obj, floatVal);
                    break;
                case Types.DOUBLE:
                    double doubleVal = result.getDouble(i + 1);
                    ((PojoUtils.SetterDouble<Object>) afi.setterOrGetter).set(obj, doubleVal);
                    break;
                case Types.DECIMAL:
                    BigDecimal bdVal = result.getBigDecimal(i + 1);
                    ((PojoUtils.Setter<Object, BigDecimal>) afi.setterOrGetter).set(obj, bdVal);
                    break;
                case Types.TIMESTAMP:
                    Timestamp tsVal = result.getTimestamp(i + 1);
                    ((PojoUtils.Setter<Object, Timestamp>) afi.setterOrGetter).set(obj, tsVal);
                    break;
                case Types.TIME:
                    Time timeVal = result.getTime(i + 1);
                    ((PojoUtils.Setter<Object, Time>) afi.setterOrGetter).set(obj, timeVal);
                    break;
                case Types.DATE:
                    Date dateVal = result.getDate(i + 1);
                    ((PojoUtils.Setter<Object, Date>) afi.setterOrGetter).set(obj, dateVal);
                    break;
                default:
                    throw new RuntimeException("unsupported data type " + type);
            }
        }
        return obj;
    } catch (SQLException e) {
        store.disconnect();
        throw new RuntimeException("fetching metadata", e);
    }
}
Also used : SQLException(java.sql.SQLException) Time(java.sql.Time) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) Date(java.sql.Date) ActiveFieldInfo(org.apache.apex.malhar.lib.db.jdbc.JdbcPOJOInputOperator.ActiveFieldInfo) JSONObject(org.codehaus.jettison.json.JSONObject)

Example 3 with ActiveFieldInfo

use of org.apache.apex.malhar.lib.db.jdbc.JdbcPOJOInputOperator.ActiveFieldInfo in project apex-malhar by apache.

the class JdbcPOJOPollInputOperator method activate.

@Override
public void activate(Context.OperatorContext context) {
    for (int i = 0; i < columnDataTypes.size(); i++) {
        final int type = columnDataTypes.get(i);
        JdbcPOJOInputOperator.ActiveFieldInfo activeFieldInfo = columnFieldSetters.get(i);
        switch(type) {
            case (Types.CHAR):
            case (Types.VARCHAR):
                activeFieldInfo.setterOrGetter = PojoUtils.createSetter(pojoClass, activeFieldInfo.fieldInfo.getPojoFieldExpression(), String.class);
                break;
            case (Types.BOOLEAN):
                activeFieldInfo.setterOrGetter = PojoUtils.createSetterBoolean(pojoClass, activeFieldInfo.fieldInfo.getPojoFieldExpression());
                break;
            case (Types.TINYINT):
                activeFieldInfo.setterOrGetter = PojoUtils.createSetterByte(pojoClass, activeFieldInfo.fieldInfo.getPojoFieldExpression());
                break;
            case (Types.SMALLINT):
                activeFieldInfo.setterOrGetter = PojoUtils.createSetterShort(pojoClass, activeFieldInfo.fieldInfo.getPojoFieldExpression());
                break;
            case (Types.INTEGER):
                activeFieldInfo.setterOrGetter = PojoUtils.createSetterInt(pojoClass, activeFieldInfo.fieldInfo.getPojoFieldExpression());
                break;
            case (Types.BIGINT):
                activeFieldInfo.setterOrGetter = PojoUtils.createSetterLong(pojoClass, activeFieldInfo.fieldInfo.getPojoFieldExpression());
                break;
            case (Types.FLOAT):
                activeFieldInfo.setterOrGetter = PojoUtils.createSetterFloat(pojoClass, activeFieldInfo.fieldInfo.getPojoFieldExpression());
                break;
            case (Types.DOUBLE):
                activeFieldInfo.setterOrGetter = PojoUtils.createSetterDouble(pojoClass, activeFieldInfo.fieldInfo.getPojoFieldExpression());
                break;
            case Types.DECIMAL:
                activeFieldInfo.setterOrGetter = PojoUtils.createSetter(pojoClass, activeFieldInfo.fieldInfo.getPojoFieldExpression(), BigDecimal.class);
                break;
            case Types.TIMESTAMP:
                activeFieldInfo.setterOrGetter = PojoUtils.createSetter(pojoClass, activeFieldInfo.fieldInfo.getPojoFieldExpression(), Timestamp.class);
                break;
            case Types.TIME:
                activeFieldInfo.setterOrGetter = PojoUtils.createSetter(pojoClass, activeFieldInfo.fieldInfo.getPojoFieldExpression(), Time.class);
                break;
            case Types.DATE:
                activeFieldInfo.setterOrGetter = PojoUtils.createSetter(pojoClass, activeFieldInfo.fieldInfo.getPojoFieldExpression(), Date.class);
                break;
            default:
                throw new RuntimeException("unsupported data type " + type);
        }
    }
    super.activate(context);
}
Also used : ActiveFieldInfo(org.apache.apex.malhar.lib.db.jdbc.JdbcPOJOInputOperator.ActiveFieldInfo) Time(java.sql.Time) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) Date(java.sql.Date)

Aggregations

ActiveFieldInfo (org.apache.apex.malhar.lib.db.jdbc.JdbcPOJOInputOperator.ActiveFieldInfo)3 BigDecimal (java.math.BigDecimal)2 Date (java.sql.Date)2 SQLException (java.sql.SQLException)2 Time (java.sql.Time)2 Timestamp (java.sql.Timestamp)2 FieldInfo (org.apache.apex.malhar.lib.util.FieldInfo)1 JSONObject (org.codehaus.jettison.json.JSONObject)1