Beispiel #1
0
  public Table executeAndFetchTable() {
    ResultSet rs;
    long start = System.currentTimeMillis();
    try {
      rs = statement.executeQuery();
      long afterExecute = System.currentTimeMillis();
      Table table = TableFactory.createTable(rs, this.isCaseSensitive());
      long afterClose = System.currentTimeMillis();

      logger.info(
          "total: {} ms, execution: {} ms, reading and parsing: {} ms; executed fetch table [{}]",
          new Object[] {
            afterClose - start,
            afterExecute - start,
            afterClose - afterExecute,
            this.getName() == null ? "No name" : this.getName()
          });

      return table;
    } catch (SQLException e) {
      throw new Sql2oException("Error while executing query", e);
    } finally {
      closeConnectionIfNecessary();
    }
  }
Beispiel #2
0
  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;
  }
Beispiel #3
0
  public Connection executeBatch() throws Sql2oException {
    long start = System.currentTimeMillis();
    try {
      statement.executeBatch();
    } catch (Throwable e) {
      this.connection.onException();
      throw new Sql2oException("Error while executing batch operation: " + e.getMessage(), e);
    } finally {
      closeConnectionIfNecessary();
    }

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

    return this.connection;
  }
Beispiel #4
0
  public <T> List<T> executeAndFetch(Class returnType) {
    List list = new ArrayList();
    PojoMetadata metadata =
        new PojoMetadata(returnType, this.isCaseSensitive(), this.getColumnMappings());
    try {
      // java.util.Date st = new java.util.Date();
      long start = System.currentTimeMillis();
      ResultSet rs = statement.executeQuery();
      long afterExecQuery = System.currentTimeMillis();

      ResultSetMetaData meta = rs.getMetaData();

      while (rs.next()) {

        Pojo pojo = new Pojo(metadata, this.isCaseSensitive());

        // Object obj = returnType.newInstance();
        for (int colIdx = 1; colIdx <= meta.getColumnCount(); colIdx++) {
          String colName = meta.getColumnName(colIdx);
          pojo.setProperty(colName, rs.getObject(colIdx));
        }

        list.add(pojo.getObject());
      }

      rs.close();
      long afterClose = System.currentTimeMillis();

      logger.info(
          "total: {} ms, execution: {} ms, reading and parsing: {} ms; executed [{}]",
          new Object[] {
            afterClose - start,
            afterExecQuery - start,
            afterClose - afterExecQuery,
            this.getName() == null ? "No name" : this.getName()
          });
    } catch (SQLException ex) {
      throw new Sql2oException("Database error", ex);
    } finally {
      closeConnectionIfNecessary();
    }

    return list;
  }
Beispiel #5
0
  public Connection executeUpdate() {
    long start = System.currentTimeMillis();
    try {
      this.connection.setResult(statement.executeUpdate());
      this.connection.setKeys(statement.getStatement().getGeneratedKeys());
      connection.setCanGetKeys(true);
    } catch (SQLException ex) {
      this.connection.onException();
      throw new Sql2oException("Error in executeUpdate, " + ex.getMessage(), ex);
    } finally {
      closeConnectionIfNecessary();
    }

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

    return this.connection;
  }
Beispiel #6
0
  public <T> List<T> executeScalarList() {
    long start = System.currentTimeMillis();
    List<T> list = new ArrayList<T>();
    try {
      ResultSet rs = this.statement.executeQuery();
      while (rs.next()) {
        list.add((T) rs.getObject(1));
      }

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

      return list;
    } catch (SQLException ex) {
      this.connection.onException();
      throw new Sql2oException(
          "Error occurred while executing scalar list: " + ex.getMessage(), ex);
    } finally {
      closeConnectionIfNecessary();
    }
  }
Beispiel #7
0
  public Object executeScalar() {
    long start = System.currentTimeMillis();
    try {
      ResultSet rs = this.statement.executeQuery();
      if (rs.next()) {
        Object o = rs.getObject(1);
        long end = System.currentTimeMillis();
        logger.info(
            "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();
    }
  }