예제 #1
0
파일: Query.java 프로젝트: kptran/sql2o
 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;
 }
예제 #2
0
파일: Query.java 프로젝트: kptran/sql2o
 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;
 }
예제 #3
0
파일: Query.java 프로젝트: kptran/sql2o
 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;
 }
예제 #4
0
파일: Query.java 프로젝트: kptran/sql2o
 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;
 }
예제 #5
0
파일: Query.java 프로젝트: kptran/sql2o
  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();
    }
  }
예제 #6
0
파일: Query.java 프로젝트: kptran/sql2o
 public Query addParameter(String name, long value) {
   try {
     statement.setLong(name, value);
   } catch (SQLException ex) {
     throw new RuntimeException(ex);
   }
   return this;
 }
예제 #7
0
파일: Query.java 프로젝트: kptran/sql2o
 public Query addParameter(String name, int value) {
   try {
     statement.setInt(name, value);
   } catch (SQLException ex) {
     throw new Sql2oException(ex);
   }
   return this;
 }
예제 #8
0
파일: Query.java 프로젝트: kptran/sql2o
  /** ************ batch stuff ****************** */
  public Query addToBatch() {
    try {
      statement.addBatch();
    } catch (SQLException e) {
      throw new Sql2oException("Error while adding statement to batch", e);
    }

    return this;
  }
예제 #9
0
파일: Query.java 프로젝트: kptran/sql2o
  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;
  }
예제 #10
0
파일: Query.java 프로젝트: kptran/sql2o
 /** ************ 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);
   }
 }
예제 #11
0
파일: Query.java 프로젝트: kptran/sql2o
  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;
  }
예제 #12
0
파일: Query.java 프로젝트: kptran/sql2o
  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;
  }