Пример #1
0
 @VisibleForTesting
 static String convertSelectSql(String selectSql, Database db) {
   String newSelectSql = selectSql;
   newSelectSql = newSelectSql.replace("${_true}", db.getDialect().getTrueSqlValue());
   newSelectSql = newSelectSql.replace("${_false}", db.getDialect().getFalseSqlValue());
   return newSelectSql;
 }
Пример #2
0
  public <S> void execute(InputLoader<S> inputLoader, InputConverter<S> converter) {
    long count = 0;
    Connection readConnection = null;
    Statement stmt = null;
    ResultSet rs = null;
    Connection writeConnection = null;
    PreparedStatement writeStatement = null;
    try {
      writeConnection = db.getDataSource().getConnection();
      writeConnection.setAutoCommit(false);
      writeStatement = writeConnection.prepareStatement(converter.updateSql());

      readConnection = db.getDataSource().getConnection();
      readConnection.setAutoCommit(false);

      stmt =
          readConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
      stmt.setFetchSize(GROUP_SIZE);
      if (db.getDialect().getId().equals(MySql.ID)) {
        stmt.setFetchSize(Integer.MIN_VALUE);
      } else {
        stmt.setFetchSize(GROUP_SIZE);
      }
      rs = stmt.executeQuery(convertSelectSql(inputLoader.selectSql(), db));

      int cursor = 0;
      while (rs.next()) {
        if (converter.convert(inputLoader.load(rs), writeStatement)) {
          writeStatement.addBatch();
          cursor++;
          count++;
        }

        if (cursor == GROUP_SIZE) {
          writeStatement.executeBatch();
          writeConnection.commit();
          cursor = 0;
        }
      }
      if (cursor > 0) {
        writeStatement.executeBatch();
        writeConnection.commit();
      }

    } catch (SQLException e) {
      SqlUtil.log(LOGGER, e);
      throw processError(e);
    } catch (Exception e) {
      throw processError(e);
    } finally {
      DbUtils.closeQuietly(writeStatement);
      DbUtils.closeQuietly(writeConnection);
      DbUtils.closeQuietly(readConnection, stmt, rs);

      LOGGER.info("{} rows have been updated", count);
    }
  }
Пример #3
0
  private Queue<long[]> initGroupOfViolationIds(Database database) throws SQLException {
    Connection connection = database.getDataSource().getConnection();
    Statement stmt = null;
    ResultSet rs = null;
    try {
      connection.setAutoCommit(false);
      stmt = connection.createStatement();
      stmt.setFetchSize(10000);
      rs = stmt.executeQuery("select id from rule_failures");
      Queue<long[]> queue = new ConcurrentLinkedQueue<>();

      totalViolations = 0;
      long[] block = new long[VIOLATION_GROUP_SIZE];
      int cursor = 0;
      while (rs.next()) {
        block[cursor] = rs.getLong(1);
        cursor++;
        totalViolations++;
        if (cursor == VIOLATION_GROUP_SIZE) {
          queue.add(block);
          block = new long[VIOLATION_GROUP_SIZE];
          cursor = 0;
        }
      }
      if (cursor > 0) {
        queue.add(block);
      }
      return queue;
    } finally {
      DbUtils.closeQuietly(connection, stmt, rs);
    }
  }
Пример #4
0
 private void appendResourceConditions(StringBuilder sb) {
   sb.append(" s.status='P' AND s.islast=").append(database.getDialect().getTrueSqlValue());
   if (context.getBaseSnapshot() == null) {
     sb.append(" AND p.copy_resource_id IS NULL ");
   }
   if (!filter.getResourceQualifiers().isEmpty()) {
     sb.append(" AND s.qualifier IN ");
     appendInStatement(filter.getResourceQualifiers(), sb);
   }
   if (!filter.getResourceScopes().isEmpty()) {
     sb.append(" AND s.scope IN ");
     appendInStatement(filter.getResourceScopes(), sb);
   }
   appendDateConditions(sb);
   appendFavouritesCondition(sb);
   appendResourceNameCondition(sb);
   appendResourceKeyCondition(sb);
   appendResourceBaseCondition(sb);
 }
Пример #5
0
 private Map<Long, String> selectLongString(Database database, String sql) throws SQLException {
   Connection connection = database.getDataSource().getConnection();
   try {
     return new QueryRunner()
         .query(
             connection,
             sql,
             new ResultSetHandler<Map<Long, String>>() {
               @Override
               public Map<Long, String> handle(ResultSet rs) throws SQLException {
                 Map<Long, String> map = Maps.newHashMap();
                 while (rs.next()) {
                   map.put(rs.getLong(1), rs.getString(2));
                 }
                 return map;
               }
             });
   } finally {
     DbUtils.closeQuietly(connection);
   }
 }
Пример #6
0
 private void appendEscapeForSomeDb(StringBuilder sb) {
   if (database.getDialect().getId().equals(Oracle.ID)
       || database.getDialect().getId().equals(MsSql.ID)) {
     sb.append(" ESCAPE '\\'");
   }
 }