Ejemplo n.º 1
0
 public Query addParameter(String name, Long value) {
   try {
     if (value == null) {
       statement.setNull(name, Types.INTEGER);
     } else {
       statement.setLong(name, value);
     }
   } catch (SQLException ex) {
     throw new Sql2oException(ex);
   }
   return this;
 }
Ejemplo n.º 2
0
 public Query addParameter(String name, Integer value) {
   try {
     if (value == null) {
       statement.setNull(name, Types.INTEGER);
     } else {
       statement.setInt(name, value);
     }
   } catch (SQLException ex) {
     throw new RuntimeException(ex);
   }
   return this;
 }
Ejemplo n.º 3
0
 public Query addParameter(String name, Time value) {
   try {
     if (value == null) {
       statement.setNull(name, Types.TIME);
     } else {
       statement.setTime(name, value);
     }
   } catch (SQLException e) {
     throw new RuntimeException(e);
   }
   return this;
 }
Ejemplo n.º 4
0
 public Query addParameter(String name, String value) {
   try {
     if (value == null) {
       statement.setNull(name, Types.VARCHAR);
     } else {
       statement.setString(name, value);
     }
   } catch (Exception ex) {
     throw new RuntimeException(ex);
   }
   return this;
 }
Ejemplo n.º 5
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();
    }
  }
Ejemplo n.º 6
0
 public Query addParameter(String name, long value) {
   try {
     statement.setLong(name, value);
   } catch (SQLException ex) {
     throw new RuntimeException(ex);
   }
   return this;
 }
Ejemplo n.º 7
0
 public Query addParameter(String name, int value) {
   try {
     statement.setInt(name, value);
   } catch (SQLException ex) {
     throw new Sql2oException(ex);
   }
   return this;
 }
Ejemplo n.º 8
0
  /** ************ batch stuff ****************** */
  public Query addToBatch() {
    try {
      statement.addBatch();
    } catch (SQLException e) {
      throw new Sql2oException("Error while adding statement to batch", e);
    }

    return this;
  }
Ejemplo n.º 9
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;
  }
Ejemplo n.º 10
0
 /** ************ private stuff ************** */
 private void closeConnectionIfNecessary() {
   try {
     if (!this.connection.getJdbcConnection().isClosed()
         && this.connection.getJdbcConnection().getAutoCommit()
         && statement != null) {
       statement.close();
       this.connection.getJdbcConnection().close();
     }
   } catch (Exception ex) {
     throw new RuntimeException("Error while attempting to close connection", ex);
   }
 }
Ejemplo n.º 11
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;
  }
Ejemplo n.º 12
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;
  }