Search in sources :

Example 41 with Timestamp

use of java.sql.Timestamp in project languagetool by languagetool-org.

the class MatchDatabase method list.

List<StoredWikipediaRuleMatch> list() throws SQLException {
    try (PreparedStatement prepSt = conn.prepareStatement("SELECT * FROM feed_matches");
        ResultSet resultSet = prepSt.executeQuery()) {
        List<StoredWikipediaRuleMatch> result = new ArrayList<>();
        while (resultSet.next()) {
            String ruleId = resultSet.getString("rule_id");
            String ruleSubId = resultSet.getString("rule_sub_id");
            String ruleDescription = resultSet.getString("rule_description");
            String ruleMessage = resultSet.getString("rule_message");
            String errorContext = resultSet.getString("error_context");
            String title = resultSet.getString("title");
            Date editDate = new Date(resultSet.getTimestamp("edit_date").getTime());
            Timestamp fixTimeStamp = resultSet.getTimestamp("fix_date");
            Date fixDate = fixTimeStamp != null ? new Date(resultSet.getTimestamp("fix_date").getTime()) : null;
            long diffId = resultSet.getLong("diff_id");
            long fixDiffId = resultSet.getLong("fix_diff_id");
            result.add(new StoredWikipediaRuleMatch(ruleId, ruleSubId, ruleDescription, ruleMessage, errorContext, title, editDate, fixDate, diffId, fixDiffId));
        }
        return result;
    }
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 42 with Timestamp

use of java.sql.Timestamp in project languagetool by languagetool-org.

the class MatchDatabase method updateRuleMatchDate.

private void updateRuleMatchDate(String tableName, Language language, Date date) {
    String updateSql = "UPDATE " + tableName + " SET check_date = ? WHERE language_code = ?";
    try (PreparedStatement updateSt = conn.prepareStatement(updateSql)) {
        updateSt.setTimestamp(1, new Timestamp(date.getTime()));
        updateSt.setString(2, language.getShortCode());
        int affected = updateSt.executeUpdate();
        if (affected == 0) {
            String insertSql = "INSERT INTO " + tableName + " (language_code, check_date) VALUES (?, ?)";
            try (PreparedStatement insertSt = conn.prepareStatement(insertSql)) {
                insertSt.setString(1, language.getShortCode());
                insertSt.setTimestamp(2, new Timestamp(date.getTime()));
                insertSt.execute();
            }
        }
    } catch (SQLException e) {
        throw new RuntimeException("Could not store date for " + language + " to database, table " + tableName, e);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 43 with Timestamp

use of java.sql.Timestamp in project languagetool by languagetool-org.

the class MatchDatabase method add.

void add(WikipediaRuleMatch ruleMatch) {
    String sql = "INSERT INTO feed_matches " + "(title, language_code, rule_id, rule_sub_id, rule_description, rule_message, rule_category, error_context, edit_date, diff_id) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    try (PreparedStatement prepSt = conn.prepareStatement(sql)) {
        prepSt.setString(1, StringUtils.abbreviate(ruleMatch.getTitle(), 255));
        prepSt.setString(2, ruleMatch.getLanguage().getShortCode());
        prepSt.setString(3, ruleMatch.getRule().getId());
        if (ruleMatch.getRule() instanceof AbstractPatternRule) {
            prepSt.setString(4, ((AbstractPatternRule) ruleMatch.getRule()).getSubId());
        } else {
            prepSt.setString(4, null);
        }
        prepSt.setString(5, StringUtils.abbreviate(ruleMatch.getRule().getDescription(), 255));
        prepSt.setString(6, StringUtils.abbreviate(ruleMatch.getMessage(), 255));
        if (ruleMatch.getRule().getCategory() != null) {
            prepSt.setString(7, StringUtils.abbreviate(ruleMatch.getRule().getCategory().getName(), 255));
        } else {
            prepSt.setString(7, "<no category>");
        }
        prepSt.setString(8, StringUtils.abbreviate(ruleMatch.getErrorContext(), 500));
        prepSt.setTimestamp(9, new Timestamp(ruleMatch.getEditDate().getTime()));
        prepSt.setLong(10, ruleMatch.getDiffId());
        prepSt.execute();
    } catch (SQLException e) {
        if (e.toString().contains("Incorrect string value")) {
            // Let's accept this - i.e. not crash - for now:
            // See http://stackoverflow.com/questions/1168036/ and http://stackoverflow.com/questions/10957238/
            System.err.println("Could not add rule match " + ruleMatch + " to database - stacktrace follows:");
            e.printStackTrace();
        } else {
            throw new RuntimeException("Could not add rule match " + ruleMatch + " to database", e);
        }
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Example 44 with Timestamp

use of java.sql.Timestamp in project languagetool by languagetool-org.

the class MatchDatabase method markedFixed.

/**
   * @return the number of affected rows, thus {@code 0} means the error was not found in the database
   */
int markedFixed(WikipediaRuleMatch ruleMatch) {
    String sql = "UPDATE feed_matches SET fix_date = ?, fix_diff_id = ? WHERE language_code = ? AND title = ? AND rule_id = ? AND error_context = ?";
    try (PreparedStatement prepSt = conn.prepareStatement(sql)) {
        prepSt.setTimestamp(1, new Timestamp(ruleMatch.getEditDate().getTime()));
        prepSt.setLong(2, ruleMatch.getDiffId());
        prepSt.setString(3, ruleMatch.getLanguage().getShortCode());
        prepSt.setString(4, ruleMatch.getTitle());
        // I'm not sure whether we should also consider the sub id...
        prepSt.setString(5, ruleMatch.getRule().getId());
        prepSt.setString(6, ruleMatch.getErrorContext());
        return prepSt.executeUpdate();
    } catch (SQLException e) {
        throw new RuntimeException("Could not mark rule match " + ruleMatch + " as fixed in database", e);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 45 with Timestamp

use of java.sql.Timestamp in project jOOQ by jOOQ.

the class TableRecordImpl method addRecordTimestamp.

/**
     * Set an updated timestamp value to a store query
     */
final Timestamp addRecordTimestamp(StoreQuery<?> store) {
    Timestamp result = null;
    if (isTimestampOrVersionAvailable()) {
        TableField<R, ?> timestamp = getTable().getRecordTimestamp();
        if (timestamp != null) {
            // Use Timestamp locally, to provide maximum precision
            result = new Timestamp(System.currentTimeMillis());
            addValue(store, timestamp, result);
        }
    }
    return result;
}
Also used : Timestamp(java.sql.Timestamp)

Aggregations

Timestamp (java.sql.Timestamp)3153 Test (org.junit.Test)526 Date (java.util.Date)458 PreparedStatement (java.sql.PreparedStatement)450 SQLException (java.sql.SQLException)367 ResultSet (java.sql.ResultSet)353 BigDecimal (java.math.BigDecimal)351 ArrayList (java.util.ArrayList)236 Date (java.sql.Date)218 Connection (java.sql.Connection)216 HashMap (java.util.HashMap)201 GenericValue (org.apache.ofbiz.entity.GenericValue)194 Calendar (java.util.Calendar)184 Time (java.sql.Time)173 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)167 Delegator (org.apache.ofbiz.entity.Delegator)157 SimpleDateFormat (java.text.SimpleDateFormat)150 IOException (java.io.IOException)129 Locale (java.util.Locale)129 Map (java.util.Map)111