Search in sources :

Example 31 with ValueType

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

the class TnPlainValueTypes method getValueType.

// -----------------------------------------------------
// byType
// ------
/**
 * Get the value type by class type. <br>
 * The basic objects are prior to the basic interfaces basically,
 * but only when the ENUM is assignable from the class type, interfaces are prior.
 * Because frequently the ENUM has application own interfaces.
 * Actually Classification of DBFlute matches the pattern.
 * @param type The type of class. (NullAllowed: if null, returns object type)
 * @return The value type. (NotNull: if not found, returns object type)
 */
public ValueType getValueType(Class<?> type) {
    if (type == null) {
        return DEFAULT_OBJECT;
    }
    final boolean interfaceFirst = Enum.class.isAssignableFrom(type);
    ValueType valueType = null;
    if (interfaceFirst) {
        valueType = getBasicInterfaceValueType(type);
        if (valueType == null) {
            valueType = getBasicObjectValueType(type);
        }
    } else {
        valueType = getBasicObjectValueType(type);
        if (valueType == null) {
            valueType = getBasicInterfaceValueType(type);
        }
    }
    return valueType != null ? valueType : DEFAULT_OBJECT;
}
Also used : ValueType(org.dbflute.jdbc.ValueType)

Example 32 with ValueType

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

the class TnAbstractBasicSqlHandler method bindArgs.

// ===================================================================================
// Common Logic
// ============
// -----------------------------------------------------
// Arguments Handling
// ------------------
/**
 * @param conn The connection for the database. (NotNull)
 * @param ps The prepared statement for the SQL. (NotNull)
 * @param args The arguments for binding. (NullAllowed)
 * @param valueTypes The types of binding value. (NotNull)
 */
protected void bindArgs(Connection conn, PreparedStatement ps, Object[] args, ValueType[] valueTypes) {
    if (args == null) {
        return;
    }
    Object current = null;
    try {
        for (int i = 0; i < args.length; ++i) {
            final ValueType valueType = valueTypes[i];
            current = args[i];
            valueType.bindValue(conn, ps, i + 1, current);
        }
    } catch (SQLException e) {
        final SQLExceptionResource resource = createSQLExceptionResource();
        resource.setNotice("Failed to bind the value.");
        if (current != null) {
            resource.addResource("Bound Value", current);
        }
        handleSQLException(e, resource);
    }
}
Also used : ValueType(org.dbflute.jdbc.ValueType) SQLException(java.sql.SQLException) SQLExceptionResource(org.dbflute.bhv.exception.SQLExceptionResource)

Example 33 with ValueType

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

the class TnAbstractBasicSqlHandler method bindArgs.

/**
 * @param conn The connection for the database. (NotNull)
 * @param ps The prepared statement for the SQL. (NotNull)
 * @param args The arguments for binding. (NullAllowed)
 * @param argTypes The types of arguments. (NullAllowed: if args is null, this is also null)
 * @param beginIndex The index for beginning of binding.
 */
protected void bindArgs(Connection conn, PreparedStatement ps, Object[] args, Class<?>[] argTypes, int beginIndex) {
    if (args == null) {
        return;
    }
    Object current = null;
    try {
        for (int i = beginIndex; i < args.length; ++i) {
            current = args[i];
            final ValueType valueType = findValueType(argTypes[i], current);
            valueType.bindValue(conn, ps, i + 1, current);
        }
    } catch (SQLException e) {
        final SQLExceptionResource resource = createSQLExceptionResource();
        resource.setNotice("Failed to bind the value.");
        if (current != null) {
            resource.addResource("Bound Value", current);
        }
        handleSQLException(e, resource);
    }
}
Also used : ValueType(org.dbflute.jdbc.ValueType) SQLException(java.sql.SQLException) SQLExceptionResource(org.dbflute.bhv.exception.SQLExceptionResource)

Example 34 with ValueType

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

the class TnAbstractMapResultSetHandler method createRow.

protected Map<String, Object> createRow(ResultSet rs, Map<String, ValueType> propertyTypeMap) throws SQLException {
    final Map<String, Object> row = newRowMap();
    final Set<Entry<String, ValueType>> entrySet = propertyTypeMap.entrySet();
    int index = 0;
    for (Entry<String, ValueType> entry : entrySet) {
        final String propertyName = entry.getKey();
        final ValueType valueType = entry.getValue();
        final Object value = valueType.getValue(rs, index + 1);
        row.put(propertyName, value);
        ++index;
    }
    return row;
}
Also used : Entry(java.util.Map.Entry) ValueType(org.dbflute.jdbc.ValueType)

Example 35 with ValueType

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

the class TnAbstractMapResultSetHandler method createPropertyTypeMap.

protected Map<String, ValueType> createPropertyTypeMap(ResultSetMetaData rsmd) throws SQLException {
    final int count = rsmd.getColumnCount();
    final Map<String, ValueType> propertyTypeMap = newPropertyTypeMap();
    for (int i = 0; i < count; ++i) {
        final String propertyName = rsmd.getColumnLabel(i + 1);
        // because it can only use by-JDBC-type value type here
        final int columnType = rsmd.getColumnType(i + 1);
        final ValueType valueType = getValueType(columnType);
        propertyTypeMap.put(propertyName, valueType);
    }
    return propertyTypeMap;
}
Also used : ValueType(org.dbflute.jdbc.ValueType)

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