@SuppressWarnings("unchecked") private Object convertParameter(Object value) { if (value == null) { return null; } Converter converter = getQuirks().converterOf(value.getClass()); if (converter == null) { // let's try to add parameter AS IS return value; } return converter.toDatabaseParam(value); }
public void close() { if(preparedStatement != null) { connection.removeStatement(preparedStatement); try { this.getQuirks().closeStatement(preparedStatement); } catch (Throwable ex) { logger.warn("Could not close statement.", ex); } } }
public Object executeScalar() { long start = System.currentTimeMillis(); logExecution(); try (final PreparedStatement ps = buildPreparedStatement(); final ResultSet rs = ps.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(); } }
public <V> V executeScalar(Class<V> returnType){ try { Converter<V> converter; //noinspection unchecked converter = throwIfNull(returnType, getQuirks().converterOf(returnType)); //noinspection unchecked logExecution(); return executeScalar(converter); } catch (ConverterException e) { throw new Sql2oException("Error occured while converting value from database to type " + returnType, e); } }
@SuppressWarnings("unchecked") private <T> ResultSetHandler<T> newScalarResultSetHandler(final Class<T> returnType) { final Quirks quirks = getQuirks(); try { final Converter<T> converter = throwIfNull(returnType, quirks.converterOf(returnType)); return new ResultSetHandler<T>() { public T handle(ResultSet resultSet) throws SQLException { Object value = quirks.getRSVal(resultSet, 1); try { return (converter.convert(value)); } catch (ConverterException e) { throw new Sql2oException("Error occurred while converting value from database to type " + returnType, e); } } }; } catch (ConverterException e) { throw new Sql2oException("Can't get converter for type " + returnType, e); } }
@SuppressWarnings("unchecked") private Object convertParameter(Object value) { if (value == null) { return null; } Converter converter = getQuirks().converterOf(value.getClass()); if (converter == null) { // let's try to add parameter AS IS return value; } return converter.toDatabaseParam(value); }
private <T> Object converterValue(Object value) { if(value instanceof Enum){ Converter<T> converter = (Converter<T>) this.getQuirks().converterOf(value.getClass()); if (null != converter) { try { return converter.toDatabaseParam((T) value); } catch (Exception e) { throw new RuntimeException(e); } } } return value; }
public void close() { if (preparedStatement != null) { connection.removeStatement(preparedStatement); try { this.getQuirks().closeStatement(preparedStatement); } catch (Throwable ex) { log.warn("Could not close statement.", ex); } } }
private PreparedStatement buildPreparedStatement(boolean allowArrayParameters) { log.debug("Parameters => {}", paramIndexValues.values()); // prepare statement creation if (preparedStatement == null) { try { if (columnNames != null && columnNames.length > 0) { preparedStatement = connection.getJdbcConnection().prepareStatement(parsedQuery, columnNames); } else if (returnGeneratedKeys) { preparedStatement = connection.getJdbcConnection().prepareStatement(parsedQuery, Statement.RETURN_GENERATED_KEYS); } else { preparedStatement = connection.getJdbcConnection().prepareStatement(parsedQuery); } } catch (SQLException ex) { throw new Sql2oException(String.format("Error preparing statement - %s", ex.getMessage()), ex); } connection.registerStatement(preparedStatement); } // parameters assignation to query for (Map.Entry<Integer, Object> entry : paramIndexValues.entrySet()) { try { getQuirks().setParameter(preparedStatement, entry.getKey(), entry.getValue()); } catch (SQLException e) { throw new RuntimeException(String.format("Error adding parameter '%s' - %s", entry.getKey(), e.getMessage()), e); } } paramIndexValues.clear(); return preparedStatement; }
public void close() { if(preparedStatement != null) { connection.removeStatement(preparedStatement); try { this.getQuirks().closeStatement(preparedStatement); } catch (Throwable ex) { logger.warn("Could not close statement.", ex); } } }
public Object executeScalar() { long start = System.currentTimeMillis(); try { logExecution(); ResultSet rs = buildPreparedStatement().executeQuery(); if (rs.next()) { Object o = getQuirks().getRSVal(rs, 1); if (Anima.of().isEnableSQLStatistic() && log.isDebugEnabled()) { long end = System.currentTimeMillis(); log.debug("total: {} ms; executed scalar [{}]", 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(); } }
public Object executeScalar() { long start = System.currentTimeMillis(); logExecution(); try (final PreparedStatement ps = buildPreparedStatement(); final ResultSet rs = ps.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(); } }
public <V> V executeScalar(Class<V> returnType){ try { Converter<V> converter; //noinspection unchecked converter = throwIfNull(returnType, getQuirks().converterOf(returnType)); //noinspection unchecked logExecution(); return executeScalar(converter); } catch (ConverterException e) { throw new Sql2oException("Error occured while converting value from database to type " + returnType, e); } }
public <V> V executeScalar(Class<V> returnType) { try { Converter<V> converter; //noinspection unchecked converter = throwIfNull(returnType, getQuirks().converterOf(returnType)); //noinspection unchecked logExecution(); return executeScalar(converter); } catch (ConverterException e) { throw new Sql2oException("Error occured while converting value from database to type " + returnType, e); } }
@SuppressWarnings("unchecked") private <T> ResultSetHandler<T> newScalarResultSetHandler(final Class<T> returnType) { final Quirks quirks = getQuirks(); try { final Converter<T> converter = throwIfNull(returnType, quirks.converterOf(returnType)); return new ResultSetHandler<T>() { public T handle(ResultSet resultSet) throws SQLException { Object value = quirks.getRSVal(resultSet, 1); try { return (converter.convert(value)); } catch (ConverterException e) { throw new Sql2oException("Error occurred while converting value from database to type " + returnType, e); } } }; } catch (ConverterException e) { throw new Sql2oException("Can't get converter for type " + returnType, e); } }
private <T> ResultSetHandler<T> newScalarResultSetHandler(final Class<T> returnType) { final Quirks quirks = getQuirks(); try { final Converter<T> converter = throwIfNull(returnType, quirks.converterOf(returnType)); return resultSet -> { Object value = quirks.getRSVal(resultSet, 1); try { return (converter.convert(value)); } catch (ConverterException e) { throw new Sql2oException("Error occurred while converting value from database to type " + returnType, e); } }; } catch (ConverterException e) { throw new Sql2oException("Can't get converter for type " + returnType, e); } }