Search in sources :

Example 1 with ValueType

use of org.dbflute.jdbc.ValueType in project dbflute-core by dbflute.

the class DfJdbcFacade method selectStringList.

/**
 * Select the list for records as string value using value types.
 * @param sql The SQL string. (NotNull)
 * @param columnValueTypeMap The map of selected columns to value types. (NotNull, ValueTypeNullAllowed)
 * @param converter The converter to convert to string value. (NullAllowed: means no conversion)
 * @param limit The limit size for fetching. (MinusAllowed: means no limit)
 * @return The list for result. (NotNull)
 */
public List<Map<String, String>> selectStringList(String sql, Map<String, ValueType> columnValueTypeMap, DfJFadStringConverter converter, int limit) {
    // [ATTENTION]: no use bind variables
    final List<Map<String, String>> resultList = new ArrayList<Map<String, String>>();
    Connection conn = null;
    Statement st = null;
    ResultSet rs = null;
    try {
        conn = getConnection();
        beginTransactionIfNeeds(conn);
        st = conn.createStatement();
        rs = st.executeQuery(sql);
        final DfJFadResultSetWrapper wrapper = new DfJFadResultSetWrapper(rs, columnValueTypeMap, converter);
        int count = 0;
        while (wrapper.next()) {
            if (isOverLimit(limit, count)) {
                break;
            }
            final Map<String, String> recordMap = StringKeyMap.createAsFlexibleOrdered();
            final Set<Entry<String, ValueType>> entrySet = columnValueTypeMap.entrySet();
            for (Entry<String, ValueType> entry : entrySet) {
                final String columnName = entry.getKey();
                final String value = wrapper.getString(columnName);
                recordMap.put(columnName, value);
            }
            resultList.add(recordMap);
            ++count;
        }
        commitTrasactionIfNeeds(conn);
    } catch (SQLException e) {
        handleSQLException(sql, e);
        // unreachable
        return null;
    } finally {
        rollbackTransactionIfNeeds(conn);
        closeResultSet(rs);
        closeStatement(st);
        closeConnection(conn);
    }
    return resultList;
}
Also used : ValueType(org.dbflute.jdbc.ValueType) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) Entry(java.util.Map.Entry) ResultSet(java.sql.ResultSet) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) StringKeyMap(org.dbflute.helper.StringKeyMap)

Example 2 with ValueType

use of org.dbflute.jdbc.ValueType in project dbflute-core by dbflute.

the class DfLReverseDataExtractor method createColumnValueTypeMap.

// ===================================================================================
// JDBC Handling
// =============
protected Map<String, ValueType> createColumnValueTypeMap(List<Column> columnList) {
    final Map<String, ValueType> valueTypeMap = new LinkedHashMap<String, ValueType>();
    for (Column column : columnList) {
        final String columnName = column.getName();
        // create value type for the column
        final ValueType valueType;
        if (column.isJavaNativeStringObject()) {
            if (column.isDbTypeStringClob()) {
                valueType = new StringClobType();
            } else {
                valueType = new StringType();
            }
        } else if (column.isJavaNativeDateObject()) {
            // date types should be treated correctly
            if (column.isJdbcTypeTime()) {
                valueType = new TimeType();
            } else if (column.isJdbcTypeTimestamp()) {
                valueType = new TimestampType();
            } else if (column.isJdbcTypeDate()) {
                if (column.isDbTypeOracleDate()) {
                    valueType = new UtilDateAsTimestampType();
                } else {
                    valueType = new UtilDateAsSqlDateType();
                }
            } else {
                // no way
                valueType = new TimestampType();
            }
        } else if (column.isJavaNativeBinaryObject()) {
            // unsupported BLOG as loda data
            valueType = new NullBytesType();
        } else {
            // other types are treated as string
            // because ReplaceSchema can accept them
            valueType = new StringType();
        }
        valueTypeMap.put(columnName, valueType);
    }
    return valueTypeMap;
}
Also used : UtilDateAsSqlDateType(org.dbflute.s2dao.valuetype.basic.UtilDateAsSqlDateType) ValueType(org.dbflute.jdbc.ValueType) Column(org.apache.torque.engine.database.model.Column) StringType(org.dbflute.s2dao.valuetype.basic.StringType) StringClobType(org.dbflute.s2dao.valuetype.plugin.StringClobType) TimestampType(org.dbflute.s2dao.valuetype.basic.TimestampType) UtilDateAsTimestampType(org.dbflute.s2dao.valuetype.basic.UtilDateAsTimestampType) UtilDateAsTimestampType(org.dbflute.s2dao.valuetype.basic.UtilDateAsTimestampType) LinkedHashMap(java.util.LinkedHashMap) TimeType(org.dbflute.s2dao.valuetype.basic.TimeType)

Example 3 with ValueType

use of org.dbflute.jdbc.ValueType in project dbflute-core by dbflute.

the class DfLReverseDataExtractor method processNormalData.

// ===================================================================================
// Normal Data
// ===========
protected DfLReverseDataResult processNormalData(Table table, String sql) {
    final DfJdbcFacade facade = new DfJdbcFacade(_dataSource);
    final Map<String, ValueType> valueTypeMap = createColumnValueTypeMap(table.getColumnList());
    final DfJFadStringConverter converter = createStringConverter();
    final Integer limit = _extractingLimit;
    final List<Map<String, String>> resultList = facade.selectStringList(sql, valueTypeMap, converter, limit);
    return new DfLReverseDataResult(resultList);
}
Also used : DfJFadStringConverter(org.dbflute.helper.jdbc.facade.DfJFadStringConverter) ValueType(org.dbflute.jdbc.ValueType) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DfJdbcFacade(org.dbflute.helper.jdbc.facade.DfJdbcFacade)

Example 4 with ValueType

use of org.dbflute.jdbc.ValueType in project dbflute-core by dbflute.

the class DfLReverseDataExtractor method processLargeData.

// ===================================================================================
// Large Data
// ==========
protected DfLReverseDataResult processLargeData(Table table, final String sql) {
    final DfJdbcFacade facade = new DfJdbcFacade(_dataSource);
    final Map<String, ValueType> valueTypeMap = createColumnValueTypeMap(table.getColumnList());
    final DfJFadStringConverter converter = createStringConverter();
    final DfJFadCursorCallback callback = facade.selectCursor(sql, valueTypeMap, converter);
    return new DfLReverseDataResult(callback);
}
Also used : DfJFadStringConverter(org.dbflute.helper.jdbc.facade.DfJFadStringConverter) ValueType(org.dbflute.jdbc.ValueType) DfJFadCursorCallback(org.dbflute.helper.jdbc.facade.DfJFadCursorCallback) DfJdbcFacade(org.dbflute.helper.jdbc.facade.DfJdbcFacade)

Example 5 with ValueType

use of org.dbflute.jdbc.ValueType in project dbflute-core by dbflute.

the class TnRowCreatorExtension method processDerivedMap.

protected void processDerivedMap(ResultSet rs, Map<String, Map<String, Integer>> selectIndexMap, Map<String, TnPropertyMapping> propertyCache, Object row) throws SQLException {
    final ConditionBean cb = ConditionBeanContext.getConditionBeanOnThread();
    final SqlClause sqlClause = cb.getSqlClause();
    if (!sqlClause.hasSpecifiedDerivingSubQuery()) {
        return;
    }
    final DerivedMappable mappable = (DerivedMappable) row;
    final List<String> derivingAliasList = sqlClause.getSpecifiedDerivingAliasList();
    DerivedTypeHandler typeHandler = null;
    for (String derivingAlias : derivingAliasList) {
        // propertyCache has alias name when derived-referrer as case-insensitive
        if (propertyCache.containsKey(derivingAlias)) {
            // already handled
            continue;
        }
        if (!derivingAlias.startsWith(DERIVED_MAPPABLE_ALIAS_PREFIX)) {
            // might be exception but no need to be strict here
            continue;
        }
        if (typeHandler == null) {
            // basically fixed instance returned
            typeHandler = cb.xgetDerivedTypeHandler();
            if (typeHandler == null) {
                // no way, just in case
                String msg = "Not found the type handler from condition-bean: " + cb.asTableDbName();
                throw new IllegalStateException(msg);
            }
        }
        final HpDerivingSubQueryInfo derivingInfo = sqlClause.getSpecifiedDerivingInfo(derivingAlias);
        final ValueType valueType = TnValueTypes.getValueType(typeHandler.findMappingType(derivingInfo));
        final String onQueryAlias = Srl.substringFirstRear(derivingAlias, DERIVED_MAPPABLE_ALIAS_PREFIX);
        Object selectedValue = getValue(rs, onQueryAlias, valueType, selectIndexMap);
        selectedValue = typeHandler.convertToMapValue(derivingInfo, selectedValue);
        mappable.registerDerivedValue(derivingAlias, selectedValue);
    }
}
Also used : DerivedMappable(org.dbflute.dbmeta.accessory.DerivedMappable) ValueType(org.dbflute.jdbc.ValueType) DerivedTypeHandler(org.dbflute.dbmeta.accessory.DerivedTypeHandler) SqlClause(org.dbflute.cbean.sqlclause.SqlClause) HpDerivingSubQueryInfo(org.dbflute.cbean.chelper.HpDerivingSubQueryInfo) ConditionBean(org.dbflute.cbean.ConditionBean)

Aggregations

ValueType (org.dbflute.jdbc.ValueType)35 MockValueType (org.dbflute.mock.MockValueType)8 LinkedHashMap (java.util.LinkedHashMap)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 DfJFadStringConverter (org.dbflute.helper.jdbc.facade.DfJFadStringConverter)4 DfJdbcFacade (org.dbflute.helper.jdbc.facade.DfJdbcFacade)4 Entry (java.util.Map.Entry)3 TnPropertyType (org.dbflute.s2dao.metadata.TnPropertyType)3 Connection (java.sql.Connection)2 Statement (java.sql.Statement)2 Timestamp (java.sql.Timestamp)2 Column (org.apache.torque.engine.database.model.Column)2 SQLExceptionResource (org.dbflute.bhv.exception.SQLExceptionResource)2 PluginValueTypeNotFoundException (org.dbflute.exception.PluginValueTypeNotFoundException)2 StringKeyMap (org.dbflute.helper.StringKeyMap)2 DfJFadCursorCallback (org.dbflute.helper.jdbc.facade.DfJFadCursorCallback)2 ExceptionMessageBuilder (org.dbflute.helper.message.ExceptionMessageBuilder)2