Пример #1
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);
        }
      }
    }
  }
Пример #2
0
 protected void execute(PreparedStatement statement) throws SQLException {
   statement.execute();
 }
Пример #3
0
 protected void setParameter(PreparedStatement statement, Object object, int i)
     throws SQLException {
   if (object == null) {
     statement.setNull(i, java.sql.Types.VARCHAR);
   } else if (object instanceof NullValue.Binary) {
     statement.setNull(i, java.sql.Types.BINARY);
   } else if (object instanceof NullValue.Decimal) {
     statement.setNull(i, java.sql.Types.DECIMAL);
   } else if (object instanceof NullValue.Double) {
     statement.setNull(i, java.sql.Types.DOUBLE);
   } else if (object instanceof NullValue.Integer) {
     statement.setNull(i, java.sql.Types.INTEGER);
   } else if (object instanceof NullValue.String) {
     statement.setNull(i, java.sql.Types.VARCHAR);
   } else if (object instanceof NullValue.Timestamp) {
     statement.setNull(i, java.sql.Types.TIMESTAMP);
   } else if (object instanceof String) {
     statement.setString(i, (String) object);
   } else if (object instanceof Byte) {
     statement.setByte(i, (Byte) object);
   } else if (object instanceof Short) {
     statement.setShort(i, (Short) object);
   } else if (object instanceof Integer) {
     statement.setInt(i, (Integer) object);
   } else if (object instanceof Long) {
     statement.setLong(i, (Long) object);
   } else if (object instanceof BigDecimal) {
     statement.setBigDecimal(i, (BigDecimal) object);
   } else if (object instanceof Date) {
     statement.setDate(i, (Date) object);
   } else if (object instanceof Double) {
     statement.setDouble(i, (Double) object);
   } else if (object instanceof Float) {
     statement.setFloat(i, (Float) object);
   } else if (object instanceof Boolean) {
     statement.setBoolean(i, (Boolean) object);
   } else if (object instanceof Timestamp) {
     statement.setTimestamp(i, (Timestamp) object);
   } else if (object instanceof java.util.Date) {
     statement.setTimestamp(i, new Timestamp(((java.util.Date) object).getTime()));
   } else {
     statement.setObject(i, object);
   }
 }
Пример #4
0
 protected void setQueryTimeout(PreparedStatement statement, int queryTimeoutInSeconds)
     throws SQLException {
   statement.setQueryTimeout(queryTimeoutInSeconds);
 }
Пример #5
0
 protected void setMaxRowsToFetch(PreparedStatement statement, int maxRowsToFetch)
     throws SQLException {
   statement.setMaxRows(maxRowsToFetch);
 }
Пример #6
0
 protected int[] executeBatch(PreparedStatement statement) throws SQLException {
   return statement.executeBatch();
 }
Пример #7
0
 protected void addBatch(PreparedStatement statement) throws SQLException {
   statement.addBatch();
 }
Пример #8
0
 protected SQLWarning getWarnings(PreparedStatement statement) throws SQLException {
   return statement.getWarnings();
 }
Пример #9
0
 protected void close(PreparedStatement statement) throws SQLException {
   statement.close();
 }
Пример #10
0
 protected int getUpdateCount(PreparedStatement statement) throws SQLException {
   return statement.getUpdateCount();
 }
Пример #11
0
 protected ResultSet getResultSet(PreparedStatement statement) throws SQLException {
   return statement.getResultSet();
 }
Пример #12
0
 protected ResultSet getGeneratedKeys(PreparedStatement statement) throws SQLException {
   return statement.getGeneratedKeys();
 }