예제 #1
0
파일: Query.java 프로젝트: aaberg/sql2o
  public Connection executeBatch() throws Sql2oException {
    long start = System.currentTimeMillis();
    try {
      logExecution();
      PreparedStatement statement = buildPreparedStatement();
      connection.setBatchResult(statement.executeBatch());
      this.currentBatchRecords = 0;
      try {
        connection.setKeys(this.returnGeneratedKeys ? statement.getGeneratedKeys() : null);
        connection.setCanGetKeys(this.returnGeneratedKeys);
      } catch (SQLException sqlex) {
        throw new Sql2oException(
            "Error while trying to fetch generated keys from database. If you are not expecting any generated keys, fix this error by setting the fetchGeneratedKeys parameter in the createQuery() method to 'false'",
            sqlex);
      }
    } catch (Throwable e) {
      this.connection.onException();
      throw new Sql2oException("Error while executing batch operation: " + e.getMessage(), e);
    } finally {
      closeConnectionIfNecessary();
    }

    long end = System.currentTimeMillis();
    logger.debug(
        "total: {} ms; executed batch [{}]",
        new Object[] {end - start, this.getName() == null ? "No name" : this.getName()});

    return this.connection;
  }
예제 #2
0
파일: Query.java 프로젝트: aaberg/sql2o
 public void close() {
   if (preparedStatement != null) {
     connection.removeStatement(preparedStatement);
     try {
       this.getQuirks().closeStatement(preparedStatement);
     } catch (Throwable ex) {
       logger.warn("Could not close statement.", ex);
     }
   }
 }
예제 #3
0
파일: Query.java 프로젝트: aaberg/sql2o
  public Query bind(final Object pojo) {
    Class clazz = pojo.getClass();
    Map<String, PojoIntrospector.ReadableProperty> propertyMap =
        PojoIntrospector.readableProperties(clazz);
    for (PojoIntrospector.ReadableProperty property : propertyMap.values()) {
      try {
        if (this.getParamNameToIdxMap().containsKey(property.name)) {

          @SuppressWarnings("unchecked")
          final Class<Object> type = (Class<Object>) property.type;
          this.addParameter(property.name, type, property.get(pojo));
        }
      } catch (IllegalArgumentException ex) {
        logger.debug("Ignoring Illegal Arguments", ex);
      } catch (IllegalAccessException | InvocationTargetException ex) {
        throw new RuntimeException(ex);
      }
    }
    return this;
  }
예제 #4
0
파일: Query.java 프로젝트: aaberg/sql2o
  public Connection executeUpdate() {
    long start = System.currentTimeMillis();
    try {
      logExecution();
      PreparedStatement statement = buildPreparedStatement();
      this.connection.setResult(statement.executeUpdate());
      this.connection.setKeys(this.returnGeneratedKeys ? statement.getGeneratedKeys() : null);
      connection.setCanGetKeys(this.returnGeneratedKeys);
    } catch (SQLException ex) {
      this.connection.onException();
      throw new Sql2oException("Error in executeUpdate, " + ex.getMessage(), ex);
    } finally {
      closeConnectionIfNecessary();
    }

    long end = System.currentTimeMillis();
    logger.debug(
        "total: {} ms; executed update [{}]",
        new Object[] {end - start, this.getName() == null ? "No name" : this.getName()});

    return this.connection;
  }
예제 #5
0
파일: Query.java 프로젝트: aaberg/sql2o
  public Object executeScalar() {
    long start = System.currentTimeMillis();
    try {
      logExecution();
      ResultSet rs = buildPreparedStatement().executeQuery();
      if (rs.next()) {
        Object o = getQuirks().getRSVal(rs, 1);
        long end = System.currentTimeMillis();
        logger.debug(
            "total: {} ms; executed scalar [{}]",
            new Object[] {end - start, this.getName() == null ? "No name" : this.getName()});
        return o;
      } else {
        return null;
      }

    } catch (SQLException e) {
      this.connection.onException();
      throw new Sql2oException(
          "Database error occurred while running executeScalar: " + e.getMessage(), e);
    } finally {
      closeConnectionIfNecessary();
    }
  }
예제 #6
0
파일: Query.java 프로젝트: aaberg/sql2o
 private void logExecution() {
   logger.debug("Executing query:{}{}", new Object[] {System.lineSeparator(), this.parsedQuery});
 }