Search in sources :

Example 36 with DfColumnMeta

use of org.dbflute.logic.jdbc.metadata.info.DfColumnMeta in project dbflute-core by dbflute.

the class DfAbsractDataWriter method processBinary.

// -----------------------------------------------------
// Binary
// ------
// -----------------------------------------------------
// Binary
// ------
protected boolean processBinary(String dataDirectory, File dataFile, String tableName, String columnName, String value, PreparedStatement ps, int bindCount, Map<String, DfColumnMeta> columnInfoMap, int rowNumber) throws SQLException {
    if (value == null || value.trim().length() == 0) {
        // cannot be binary
        return false;
    }
    final DfColumnMeta columnInfo = columnInfoMap.get(columnName);
    if (columnInfo == null) {
        // unsupported when meta data is not found
        return false;
    }
    final Class<?> columnType = getBindType(tableName, columnInfo);
    if (columnType == null) {
        // unsupported too
        return false;
    }
    if (!byte[].class.isAssignableFrom(columnType)) {
        // not binary
        return false;
    }
    // the value should be a path to a binary file
    // from data file's current directory
    final String path;
    final String trimmedValue = value.trim();
    if (trimmedValue.startsWith("/")) {
        // means absolute path
        path = trimmedValue;
    } else {
        final String dataFilePath = Srl.replace(dataFile.getAbsolutePath(), "\\", "/");
        final String baseDirPath = Srl.substringLastFront(dataFilePath, "/");
        path = baseDirPath + "/" + trimmedValue;
    }
    final File binaryFile = new File(path);
    if (!binaryFile.exists()) {
        throwLoadDataBinaryFileNotFoundException(tableName, columnName, path, rowNumber);
    }
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(binaryFile);
        final int fileSize = (int) binaryFile.length();
        final byte[] bytes = new byte[fileSize];
        fis.read(bytes);
        ps.setBytes(bindCount, bytes);
    } catch (IOException e) {
        throwLoadDataBinaryFileReadFailureException(tableName, columnName, path, rowNumber, e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException ignored) {
            }
        }
    }
    return true;
}
Also used : DfColumnMeta(org.dbflute.logic.jdbc.metadata.info.DfColumnMeta) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 37 with DfColumnMeta

use of org.dbflute.logic.jdbc.metadata.info.DfColumnMeta in project dbflute-core by dbflute.

the class DfAbsractDataWriter method processNull.

// ===================================================================================
// Process Binding
// ===============
// -----------------------------------------------------
// Null Value
// ----------
protected boolean processNull(String dataDirectory, String tableName, String columnName, Object value, PreparedStatement ps, int bindCount, Map<String, DfColumnMeta> columnInfoMap, int rowNumber) throws SQLException {
    if (!isNullValue(value)) {
        return false;
    }
    Map<String, Integer> cacheMap = _nullTypeCacheMap.get(tableName);
    if (cacheMap == null) {
        cacheMap = StringKeyMap.createAsFlexibleOrdered();
        _nullTypeCacheMap.put(tableName, cacheMap);
    }
    final Integer cachedType = cacheMap.get(columnName);
    if (cachedType != null) {
        // cache hit
        // basically no exception
        ps.setNull(bindCount, cachedType);
        return true;
    }
    final DfColumnMeta columnInfo = columnInfoMap.get(columnName);
    if (columnInfo != null) {
        // use mapped type at first
        final String mappedJdbcType = _columnHandler.getColumnJdbcType(columnInfo);
        final Integer mappedJdbcDefValue = TypeMap.getJdbcDefValueByJdbcType(mappedJdbcType);
        try {
            ps.setNull(bindCount, mappedJdbcDefValue);
            cacheMap.put(columnName, mappedJdbcDefValue);
        } catch (SQLException e) {
            // retry by plain type
            final int plainJdbcDefValue = columnInfo.getJdbcDefValue();
            try {
                ps.setNull(bindCount, plainJdbcDefValue);
                cacheMap.put(columnName, plainJdbcDefValue);
            } catch (SQLException ignored) {
                final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
                br.addNotice("Failed to execute setNull(bindCount, jdbcDefValue).");
                br.addItem("Column");
                br.addElement(tableName + "." + columnName);
                br.addElement(columnInfo.toString());
                br.addItem("Mapped JDBC Type");
                br.addElement(mappedJdbcType);
                br.addItem("First JDBC Def-Value");
                br.addElement(mappedJdbcDefValue);
                br.addItem("Retry JDBC Def-Value");
                br.addElement(plainJdbcDefValue);
                br.addItem("Retry Message");
                br.addElement(ignored.getMessage());
                String msg = br.buildExceptionMessage();
                throw new DfJDBCException(msg, e);
            }
        }
    } else {
        // basically no way
        // as default
        Integer tryType = Types.VARCHAR;
        try {
            ps.setNull(bindCount, tryType);
            cacheMap.put(columnName, tryType);
        } catch (SQLException e) {
            tryType = Types.NUMERIC;
            try {
                ps.setNull(bindCount, tryType);
                cacheMap.put(columnName, tryType);
            } catch (SQLException ignored) {
                tryType = Types.TIMESTAMP;
                try {
                    ps.setNull(bindCount, tryType);
                    cacheMap.put(columnName, tryType);
                } catch (SQLException iignored) {
                    tryType = Types.OTHER;
                    try {
                        // last try
                        ps.setNull(bindCount, tryType);
                        cacheMap.put(columnName, tryType);
                    } catch (SQLException iiignored) {
                        throw e;
                    }
                }
            }
        }
    }
    return true;
}
Also used : DfColumnMeta(org.dbflute.logic.jdbc.metadata.info.DfColumnMeta) DfJDBCException(org.dbflute.exception.DfJDBCException) SQLException(java.sql.SQLException) ExceptionMessageBuilder(org.dbflute.helper.message.ExceptionMessageBuilder)

Example 38 with DfColumnMeta

use of org.dbflute.logic.jdbc.metadata.info.DfColumnMeta in project dbflute-core by dbflute.

the class DfAbsractDataWriter method processBoolean.

// -----------------------------------------------------
// Boolean
// -------
protected boolean processBoolean(String tableName, String columnName, String value, Connection conn, PreparedStatement ps, int bindCount, Map<String, DfColumnMeta> columnInfoMap, int rowNumber) throws SQLException {
    if (value == null || value.trim().length() == 0) {
        // cannot be boolean
        return false;
    }
    final DfColumnMeta columnInfo = columnInfoMap.get(columnName);
    if (columnInfo != null) {
        final Class<?> columnType = getBindType(tableName, columnInfo);
        if (columnType != null) {
            if (!Boolean.class.isAssignableFrom(columnType)) {
                return false;
            }
            bindNotNullValueByColumnType(tableName, columnName, conn, ps, bindCount, value, columnType, rowNumber);
            return true;
        }
    }
    // if meta data is not found (basically no way)
    try {
        final Boolean booleanValue = DfTypeUtil.toBoolean(value);
        ps.setBoolean(bindCount, booleanValue);
        return true;
    } catch (ParseBooleanException ignored) {
        // couldn't parse as boolean
        return false;
    }
}
Also used : DfColumnMeta(org.dbflute.logic.jdbc.metadata.info.DfColumnMeta) ParseBooleanException(org.dbflute.util.DfTypeUtil.ParseBooleanException)

Example 39 with DfColumnMeta

use of org.dbflute.logic.jdbc.metadata.info.DfColumnMeta in project dbflute-core by dbflute.

the class DfAbsractDataWriter method processNumber.

// -----------------------------------------------------
// Number
// ------
protected boolean processNumber(String tableName, String columnName, String value, Connection conn, PreparedStatement ps, int bindCount, Map<String, DfColumnMeta> columnInfoMap, int rowNumber) throws SQLException {
    if (value == null || value.trim().length() == 0) {
        // cannot be number
        return false;
    }
    final DfColumnMeta columnInfo = columnInfoMap.get(columnName);
    if (columnInfo != null) {
        final Class<?> columnType = getBindType(tableName, columnInfo);
        if (columnType != null) {
            if (!Number.class.isAssignableFrom(columnType)) {
                return false;
            }
            bindNotNullValueByColumnType(tableName, columnName, conn, ps, bindCount, value, columnType, rowNumber);
            return true;
        }
    }
    // if meta data is not found (basically no way)
    value = filterBigDecimalValue(value);
    if (!isBigDecimalValue(value)) {
        return false;
    }
    final BigDecimal bigDecimalValue = getBigDecimalValue(columnName, value);
    try {
        final long longValue = bigDecimalValue.longValueExact();
        ps.setLong(bindCount, longValue);
        return true;
    } catch (ArithmeticException ignored) {
        ps.setBigDecimal(bindCount, bigDecimalValue);
        return true;
    }
}
Also used : DfColumnMeta(org.dbflute.logic.jdbc.metadata.info.DfColumnMeta) BigDecimal(java.math.BigDecimal)

Example 40 with DfColumnMeta

use of org.dbflute.logic.jdbc.metadata.info.DfColumnMeta in project dbflute-core by dbflute.

the class DfColumnValueConverter method processTimestamp.

protected String processTimestamp(String tableName, String columnName, Map<String, DfColumnMeta> columnMetaMap, String filteredValue, String before, String after) {
    if (!"$$Timestamp$$".equalsIgnoreCase(before)) {
        // no converted
        return null;
    }
    final DfColumnMeta columnMeta = columnMetaMap.get(columnName);
    final Class<?> boundType = _bindTypeProvider.provide(tableName, columnMeta);
    if (!Timestamp.class.isAssignableFrom(boundType)) {
        // no converted
        return null;
    }
    // process target here
    if (after.equalsIgnoreCase("$$ZeroPrefixMillis$$")) {
        // DBFlute default
        if (filteredValue != null && filteredValue.contains(".")) {
            final String front = Srl.substringLastFront(filteredValue, ".");
            final String millis = Srl.substringLastRear(filteredValue, ".");
            if (millis.length() == 1) {
                filteredValue = front + ".00" + millis;
            } else if (millis.length() == 2) {
                filteredValue = front + ".0" + millis;
            }
            // processed
            return filteredValue;
        }
    } else if (after.equalsIgnoreCase("$$ZeroSuffixMillis$$")) {
        if (filteredValue != null && filteredValue.contains(".")) {
            final String millis = Srl.substringLastRear(filteredValue, ".");
            if (millis.length() == 1) {
                filteredValue = filteredValue + "00";
            } else if (millis.length() == 2) {
                filteredValue = filteredValue + "0";
            }
            // processed
            return filteredValue;
        }
    }
    // no converted
    return null;
}
Also used : DfColumnMeta(org.dbflute.logic.jdbc.metadata.info.DfColumnMeta) Timestamp(java.sql.Timestamp)

Aggregations

DfColumnMeta (org.dbflute.logic.jdbc.metadata.info.DfColumnMeta)71 SQLException (java.sql.SQLException)16 Connection (java.sql.Connection)10 LinkedHashMap (java.util.LinkedHashMap)9 Map (java.util.Map)8 File (java.io.File)7 StringKeyMap (org.dbflute.helper.StringKeyMap)7 ResultSet (java.sql.ResultSet)5 Statement (java.sql.Statement)5 ArrayList (java.util.ArrayList)5 DfJDBCException (org.dbflute.exception.DfJDBCException)5 FileInputStream (java.io.FileInputStream)4 BigDecimal (java.math.BigDecimal)4 DatabaseMetaData (java.sql.DatabaseMetaData)4 PreparedStatement (java.sql.PreparedStatement)4 Timestamp (java.sql.Timestamp)4 HashMap (java.util.HashMap)4 StringSet (org.dbflute.helper.StringSet)4 DfDataRow (org.dbflute.helper.dataset.DfDataRow)4 DfDataTable (org.dbflute.helper.dataset.DfDataTable)4