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; }
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; }
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; }
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; }
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(); } }
public Query addParameter(String name, long value) { try { statement.setLong(name, value); } catch (SQLException ex) { throw new RuntimeException(ex); } return this; }
public Query addParameter(String name, int value) { try { statement.setInt(name, value); } catch (SQLException ex) { throw new Sql2oException(ex); } return this; }
/** ************ batch stuff ****************** */ public Query addToBatch() { try { statement.addBatch(); } catch (SQLException e) { throw new Sql2oException("Error while adding statement to batch", e); } return this; }
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; }
/** ************ 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); } }
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; }
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; }