Search in sources :

Example 1 with ParseTimeException

use of org.dbflute.util.DfTypeUtil.ParseTimeException in project dbflute-core by dbflute.

the class DfParameterAutoDetectAssist method doDerivePropertyTypeFromTestValue.

protected String doDerivePropertyTypeFromTestValue(String testValue) {
    // test point
    if (testValue == null) {
        String msg = "The argument 'testValue' should be not null.";
        throw new IllegalArgumentException(msg);
    }
    final DfLanguageGrammar grammar = getLanguageGrammar();
    final String plainTypeName;
    if (Srl.startsWithIgnoreCase(testValue, "date '", "date'")) {
        plainTypeName = "Date";
    } else if (Srl.startsWithIgnoreCase(testValue, "timestamp '", "timestamp'")) {
        plainTypeName = "Timestamp";
    } else if (Srl.startsWithIgnoreCase(testValue, "time '", "time'")) {
        plainTypeName = "Time";
    } else {
        if (Srl.isQuotedSingle(testValue)) {
            final String unquoted = Srl.unquoteSingle(testValue);
            Timestamp timestamp = null;
            Time time = null;
            try {
                timestamp = DfTypeUtil.toTimestamp(unquoted);
            } catch (ParseTimestampException ignored) {
                try {
                    time = DfTypeUtil.toTime(unquoted);
                } catch (ParseTimeException andIgnored) {
                }
            }
            if (timestamp != null) {
                final String timeParts = DfTypeUtil.toString(timestamp, "HH:mm:ss.SSS");
                if (timeParts.equals("00:00:00.000")) {
                    plainTypeName = "Date";
                } else {
                    plainTypeName = "Timestamp";
                }
            } else if (time != null) {
                plainTypeName = "Time";
            } else {
                plainTypeName = "String";
            }
        } else if (Srl.isQuotedAnything(testValue, "(", ")")) {
            final String unquoted = Srl.unquoteAnything(testValue, "(", ")");
            final List<String> elementList = Srl.splitListTrimmed(unquoted, ",");
            if (elementList.size() > 0) {
                final String firstElement = elementList.get(0);
                // InScope for Date is unsupported at this analyzing
                if (Srl.isQuotedSingle(firstElement)) {
                    plainTypeName = "List" + grammar.buildGenericOneClassHint("String");
                } else {
                    final String elementType = doDeriveNonQuotedLiteralTypeFromTestValue(firstElement);
                    plainTypeName = "List" + grammar.buildGenericOneClassHint(elementType);
                }
            } else {
                plainTypeName = "List" + grammar.buildGenericOneClassHint("String");
            }
        } else {
            plainTypeName = doDeriveNonQuotedLiteralTypeFromTestValue(testValue);
        }
    }
    return plainTypeName;
}
Also used : ParseTimestampException(org.dbflute.util.DfTypeUtil.ParseTimestampException) Time(java.sql.Time) List(java.util.List) DfLanguageGrammar(org.dbflute.logic.generate.language.grammar.DfLanguageGrammar) Timestamp(java.sql.Timestamp) ParseTimeException(org.dbflute.util.DfTypeUtil.ParseTimeException)

Example 2 with ParseTimeException

use of org.dbflute.util.DfTypeUtil.ParseTimeException in project dbflute-core by dbflute.

the class DfAbsractDataWriter method processDate.

// -----------------------------------------------------
// Date
// ----
protected boolean processDate(String dataDirectory, 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 date
        return false;
    }
    final DfColumnMeta columnMeta = columnInfoMap.get(columnName);
    if (columnMeta != null) {
        final Class<?> columnType = getBindType(tableName, columnMeta);
        if (columnType != null) {
            if (!java.util.Date.class.isAssignableFrom(columnType)) {
                return false;
            }
            // only when column type specified
            final String resolved = resolveRelativeSysdate(dataDirectory, tableName, columnName, value);
            bindNotNullValueByColumnType(tableName, columnName, conn, ps, bindCount, resolved, columnType, rowNumber);
            return true;
        }
    }
    // if meta data is not found (basically no way)
    try {
        final Timestamp timestamp = DfTypeUtil.toTimestamp(value);
        ps.setTimestamp(bindCount, timestamp);
        return true;
    } catch (ParseTimestampException ignored) {
        // retry as time
        try {
            Time time = DfTypeUtil.toTime(value);
            ps.setTime(bindCount, time);
            return true;
        } catch (ParseTimeException ignored2) {
        }
        // couldn't parse as timestamp and time
        return false;
    }
}
Also used : DfColumnMeta(org.dbflute.logic.jdbc.metadata.info.DfColumnMeta) ParseTimestampException(org.dbflute.util.DfTypeUtil.ParseTimestampException) Time(java.sql.Time) Timestamp(java.sql.Timestamp) ParseTimeException(org.dbflute.util.DfTypeUtil.ParseTimeException)

Aggregations

Time (java.sql.Time)2 Timestamp (java.sql.Timestamp)2 ParseTimeException (org.dbflute.util.DfTypeUtil.ParseTimeException)2 ParseTimestampException (org.dbflute.util.DfTypeUtil.ParseTimestampException)2 List (java.util.List)1 DfLanguageGrammar (org.dbflute.logic.generate.language.grammar.DfLanguageGrammar)1 DfColumnMeta (org.dbflute.logic.jdbc.metadata.info.DfColumnMeta)1