Exemple #1
0
  @Override
  public void batchWrite(BatchUpdateHandler handler, List<String> batchedSQL) throws SQLException {
    Statement statement = null;
    try {
      statement = connection.createStatement();
      for (String row : batchedSQL) {
        addBatch(statement, row);
      }

      int[] batchResult = executeBatch(statement);
      handler.onBatchResult(Arrays.copyOf(batchResult, batchResult.length));

      ResultSet generatedKeys = getGeneratedKeys(statement);
      if (generatedKeys != null) {
        while (generatedKeys.next()) {
          handler.onGeneratedKey(generatedKeys.getObject(1));
        }
        generatedKeys.close();
      }

      SQLWarning warning = getWarnings(statement);
      if (warning != null) {
        handler.onWarning(warning);
      }
    } finally {
      if (statement != null) {
        try {
          close(statement);
        } catch (SQLException e) {
          LOGGER.error("Unable to close statement after batch write", e);
        }
      }
    }
  }
Exemple #2
0
  @Override
  public void batchWrite(BatchUpdateHandler handler, String SQL, List<Object[]> parameters)
      throws SQLException {
    PreparedStatement statement = null;
    try {
      statement = prepareBatchUpdateStatement(SQL);

      for (Object[] row : parameters) {
        for (int i = 0; i < row.length; i++) {
          setParameter(statement, row[i], i + 1);
        }
        addBatch(statement);
      }

      int[] batchResult = executeBatch(statement);
      handler.onBatchResult(batchResult);

      ResultSet generatedKeys = getGeneratedKeys(statement);
      if (generatedKeys != null) {
        while (generatedKeys.next()) {
          handler.onGeneratedKey(generatedKeys.getObject(1));
        }
        generatedKeys.close();
      }

      SQLWarning warning = getWarnings(statement);
      if (warning != null) {
        handler.onWarning(warning);
      }
    } finally {
      if (statement != null) {
        try {
          close(statement);
        } catch (SQLException e) {
          LOGGER.error("Unable to close statement after batch write", e);
        }
      }
    }
  }
Exemple #3
0
  @Override
  public void execute(
      ExecuteResultHandler handler,
      int maxRowsToFetch,
      int queryTimeoutInSeconds,
      String SQL,
      Object... parameters)
      throws SQLException {
    ResultSet resultSet = null;
    final PreparedStatement statement = prepareExecuteStatement(SQL);
    try {
      for (int i = 0; i < parameters.length; i++) {
        setParameter(statement, parameters[i], i + 1);
      }

      if (canCancelQueries()) {
        handler.onCancellableCallback(
            new Cancellable() {
              @Override
              public void cancel() throws Exception {
                statement.cancel();
              }
            });
      }

      setQueryTimeout(statement, queryTimeoutInSeconds);
      setMaxRowsToFetch(statement, maxRowsToFetch);
      execute(statement);

      if (canGetGeneratedKeys(SQL)) {
        ResultSet generatedKeys = getGeneratedKeys(statement);
        if (generatedKeys != null) {
          while (generatedKeys.next()) {
            handler.onGeneratedKey(generatedKeys.getObject(1));
          }
          generatedKeys.close();
        }
      }

      int resultSetCounter = 0;
      while (true) {
        int updateCount = getUpdateCount(statement);
        if (updateCount != -1) {
          handler.onUpdateCount(updateCount);
          if (getResultSet(statement) == null) {
            statement.getMoreResults();
            continue;
          }
        }
        resultSet = getResultSet(statement);
        if (resultSet == null) {
          if (statement.getMoreResults() == false) {
            break;
          }
          continue;
        }

        boolean gotCancel = false;
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        if (!handler.onResultSet(newResultSetInformation(resultSetMetaData, resultSetCounter))) {
          gotCancel = true;
        }

        SQLWarning warning = getWarnings(resultSet);
        if (warning != null) {
          handler.onWarning(warning);
        }

        while (resultSet.next() && !gotCancel) {
          Object[] row = new Object[resultSetMetaData.getColumnCount()];
          for (int i = 0; i < row.length; i++) {
            row[i] = resultSet.getObject(i + 1);
          }
          if (!handler.nextRow(row)) {
            gotCancel = true;
          }
        }
        statement.getMoreResults();
      }
      handler.onDone();
    } finally {
      if (resultSet != null) {
        try {
          close(resultSet);
        } catch (SQLException e) {
          LOGGER.error("Unable to close result set after query", e);
        }
      }
      if (statement != null) {
        try {
          close(statement);
        } catch (SQLException e) {
          LOGGER.error("Unable to close statement after query", e);
        }
      }
    }
  }
Exemple #4
0
 protected void close(ResultSet resultSet) throws SQLException {
   resultSet.close();
 }
Exemple #5
0
 protected SQLWarning getWarnings(ResultSet resultSet) throws SQLException {
   return resultSet.getWarnings();
 }