List of usage examples for org.apache.commons.dbutils.handlers ArrayListHandler ArrayListHandler
public ArrayListHandler(RowProcessor convert)
From source file:org.efaps.db.AbstractPrintQuery.java
/** * The instance method executes exact one complete statement and populates * the result in the cached result {@link #cachedResult}. * * @param _complStmt complete statement instance to execute * @param _oneSelects lsit of OneSelects the statement is executed for * @return true if the query contains values, else false * @throws EFapsException on error/*from w w w .ja v a2 s .c o m*/ */ @SuppressWarnings("unchecked") protected boolean executeOneCompleteStmt(final String _complStmt, final List<OneSelect> _oneSelects) throws EFapsException { boolean ret = false; ConnectionResource con = null; try { AbstractPrintQuery.LOG.debug("Executing SQL: {}", _complStmt); List<Object[]> rows = null; boolean cached = false; if (isCacheEnabled()) { final QueryKey querykey = QueryKey.get(getKey(), _complStmt); final Cache<QueryKey, Object> cache = QueryCache.getSqlCache(); if (cache.containsKey(querykey)) { final Object object = cache.get(querykey); if (object instanceof List) { rows = (List<Object[]>) object; } cached = true; } } if (!cached) { con = Context.getThreadContext().getConnectionResource(); final Statement stmt = con.getConnection().createStatement(); final ResultSet rs = stmt.executeQuery(_complStmt); final ArrayListHandler handler = new ArrayListHandler(Context.getDbType().getRowProcessor()); rows = handler.handle(rs); rs.close(); stmt.close(); con.commit(); if (isCacheEnabled()) { QueryCache.put((ICacheDefinition) this, QueryKey.get(getKey(), _complStmt), rows); } } for (final Object[] row : rows) { for (final OneSelect onesel : _oneSelects) { onesel.addObject(row); } ret = true; } final List<Instance> tmpList = new ArrayList<>(); final Map<Instance, Integer> sortMap = new HashMap<>(); int i = 0; for (final Object[] row : rows) { final Instance instance; if (getMainType().getMainTable().getSqlColType() != null) { instance = Instance.get(Type.get((Long) row[this.typeColumnIndex - 1]), (Long) row[0]); } else { instance = Instance.get(getMainType(), (Long) row[0]); } sortMap.put(instance, i); tmpList.add(instance); i++; } if (this.enforceSorted) { for (final OneSelect onesel : _oneSelects) { onesel.sortByInstanceList(getInstanceList(), sortMap); } } else { getInstanceList().clear(); getInstanceList().addAll(tmpList); } } catch (final SQLException e) { throw new EFapsException(InstanceQuery.class, "executeOneCompleteStmt", e); } finally { if (con != null && con.isOpened()) { con.abort(); } } return ret; }
From source file:org.efaps.db.print.LinkFromSelect.java
/** * {@inheritDoc}//from w w w . j a v a2s . c o m */ @SuppressWarnings("unchecked") @Override protected boolean executeOneCompleteStmt(final String _complStmt, final List<OneSelect> _oneSelects) throws EFapsException { boolean ret = false; ConnectionResource con = null; try { LOG.debug("Executing SQLL: {}", _complStmt); List<Object[]> rows = null; boolean cached = false; if (isCacheEnabled()) { final QueryKey querykey = QueryKey.get(getKey(), _complStmt); final Cache<QueryKey, Object> cache = QueryCache.getSqlCache(); if (cache.containsKey(querykey)) { final Object object = cache.get(querykey); if (object instanceof List) { rows = (List<Object[]>) object; } cached = true; } } if (!cached) { con = Context.getThreadContext().getConnectionResource(); final Statement stmt = con.getConnection().createStatement(); final ResultSet rs = stmt.executeQuery(_complStmt.toString()); final ArrayListHandler handler = new ArrayListHandler(Context.getDbType().getRowProcessor()); rows = handler.handle(rs); rs.close(); stmt.close(); con.commit(); if (isCacheEnabled()) { final QueryKey querykey = QueryKey.get(getKey(), _complStmt); final Cache<QueryKey, Object> cache = QueryCache.getSqlCache(); cache.put(querykey, rows); } } for (final Object[] row : rows) { for (final OneSelect onesel : _oneSelects) { onesel.addObject(row); } ret = true; } } catch (final SQLException e) { throw new EFapsException(InstanceQuery.class, "executeOneCompleteStmt", e); } finally { if (con != null && con.isOpened()) { con.abort(); } } return ret; }
From source file:org.efaps.db.wrapper.SQLUpdate.java
/** * Check if the execution of the update is neccesary by comparing the values. * @param _con connection to be used/*from w w w. j a v a 2 s. c o m*/ * @return true if the execution of the update is necessary * @throws SQLException on error */ private boolean checkUpdateRequired(final Connection _con) throws SQLException { final SQLSelect select = new SQLSelect(); for (final AbstractColumnWithValue<?> colValue : getColumnWithValues()) { select.column(colValue.getColumnName()); } select.from(getTableName()); select.addPart(SQLPart.WHERE).addColumnPart(null, "ID").addPart(SQLPart.EQUAL).addValuePart(getId()); final Statement stmt = _con.createStatement(); final ResultSet rs = stmt.executeQuery(select.getSQL()); final ArrayListHandler handler = new ArrayListHandler(Context.getDbType().getRowProcessor()); final List<Object[]> rows = handler.handle(rs); rs.close(); stmt.close(); if (rows.size() == 1) { final Object[] values = rows.get(0); int idx = 0; final Iterator<AbstractColumnWithValue<?>> colIter = getColumnWithValues().iterator(); while (colIter.hasNext()) { final AbstractColumnWithValue<?> colValue = colIter.next(); final Object dbValue = values[idx]; final Object newValue = colValue.getValue(); if (dbValue instanceof Long) { if (newValue instanceof Long) { if (dbValue.equals(newValue)) { colIter.remove(); } } else if (newValue instanceof Integer) { if (dbValue.equals(new Long((Integer) newValue))) { colIter.remove(); } } } else if (dbValue instanceof BigDecimal) { if (newValue instanceof BigDecimal) { if (((BigDecimal) dbValue).compareTo((BigDecimal) newValue) == 0) { colIter.remove(); } } else if (newValue instanceof Long) { if (((BigDecimal) dbValue).compareTo(BigDecimal.valueOf((Long) newValue)) == 0) { colIter.remove(); } } else if (newValue instanceof Integer) { if (((BigDecimal) dbValue) .compareTo(BigDecimal.valueOf(new Long((Integer) newValue))) == 0) { colIter.remove(); } } } else if (dbValue instanceof Timestamp) { if (newValue instanceof Timestamp) { if (((Timestamp) dbValue).equals((Timestamp) newValue)) { colIter.remove(); } } } else if (dbValue instanceof Boolean) { if (newValue instanceof Boolean) { if (((Boolean) dbValue).equals(newValue)) { colIter.remove(); } } } else if (dbValue instanceof String) { if (newValue instanceof String) { if (((String) dbValue).trim().equals(((String) newValue).trim())) { colIter.remove(); } } } idx++; } } return !getColumnWithValues().isEmpty(); }