private void buildParameterFromFilterItem(List<QueryParameter> parameters, FilterItem item) { if (item.isCompoundFilter()) { FilterItem[] childItems = item.getChildItems(); for (FilterItem childItem : childItems) { buildParameterFromFilterItem(parameters, childItem); } } else { if (item.getOperand() instanceof QueryParameter) { parameters.add((QueryParameter) item.getOperand()); } } }
private void buildParameterFromFilterItem(List<QueryParameter> parameters, FilterItem item) { if (item.isCompoundFilter()) { FilterItem[] childItems = item.getChildItems(); for (FilterItem childItem : childItems) { buildParameterFromFilterItem(parameters, childItem); } } else { if (item.getOperand() instanceof QueryParameter) { parameters.add((QueryParameter) item.getOperand()); } } }
/** * Filters the FilterItems such that only the FilterItems are returned, which contain SelectItems that are contained * in selectItemList * * @param filters * @param selectItemList * @return */ private static Set<FilterItem> applicableFilters(Collection<FilterItem> filters, Collection<SelectItem> selectItemList) { Set<SelectItem> items = new HashSet<SelectItem>(selectItemList); return filters.stream().filter(fi -> { Collection<SelectItem> fiSelectItems = new ArrayList<>(); fiSelectItems.add(fi.getSelectItem()); Object operand = fi.getOperand(); if (operand instanceof SelectItem) { fiSelectItems.add((SelectItem) operand); } return items.containsAll(fiSelectItems); }).collect(Collectors.toSet()); }
/** * Filters the FilterItems such that only the FilterItems are returned, which contain SelectItems that are contained * in selectItemList * * @param filters * @param selectItemList * @return */ private static Set<FilterItem> applicableFilters(Collection<FilterItem> filters, Collection<SelectItem> selectItemList) { Set<SelectItem> items = new HashSet<SelectItem>(selectItemList); return filters.stream().filter(fi -> { Collection<SelectItem> fiSelectItems = new ArrayList<>(); fiSelectItems.add(fi.getSelectItem()); Object operand = fi.getOperand(); if (operand instanceof SelectItem) { fiSelectItems.add((SelectItem) operand); } return items.containsAll(fiSelectItems); }).collect(Collectors.toSet()); }
/** * Determines if a particular {@link FilterItem} will have it's parameter (operand) replaced during SQL generation. * Such filter items should succesively have their parameters set at execution time. * * @param whereItem * @return */ public static boolean isPreparedParameterCandidate(FilterItem whereItem) { return !whereItem.isCompoundFilter() && !OperatorType.IN.equals(whereItem.getOperator()) && whereItem.getOperand() != null; }
/** * Determines if a particular {@link FilterItem} will have it's parameter (operand) replaced during SQL generation. * Such filter items should succesively have their parameters set at execution time. * * @param whereItem * @return */ public static boolean isPreparedParameterCandidate(FilterItem whereItem) { return !whereItem.isCompoundFilter() && !OperatorType.IN.equals(whereItem.getOperator()) && whereItem.getOperand() != null; }
private static void addEvaluatedSelectItems(List<SelectItem> result, FilterItem item) { final FilterItem[] orItems = item.getChildItems(); if (orItems != null) { for (FilterItem filterItem : orItems) { addEvaluatedSelectItems(result, filterItem); } } final SelectItem selectItem = item.getSelectItem(); if (selectItem != null && !result.contains(selectItem)) { result.add(selectItem); } final Object operand = item.getOperand(); if (operand != null && operand instanceof SelectItem && !result.contains(operand)) { result.add((SelectItem) operand); } }
private static void addEvaluatedSelectItems(List<SelectItem> result, FilterItem item) { final FilterItem[] orItems = item.getChildItems(); if (orItems != null) { for (FilterItem filterItem : orItems) { addEvaluatedSelectItems(result, filterItem); } } final SelectItem selectItem = item.getSelectItem(); if (selectItem != null && !result.contains(selectItem)) { result.add(selectItem); } final Object operand = item.getOperand(); if (operand != null && operand instanceof SelectItem && !result.contains(operand)) { result.add((SelectItem) operand); } }
@Override public String rewriteFilterItem(final FilterItem item) { if (item.getOperand() instanceof String && item.getOperand().equals("")) { // In Oracle empty strings are treated as null. Typical SQL constructs with an empty string do not work. return super.rewriteFilterItem(new FilterItem(item.getSelectItem(), item.getOperator(), null)); } else { return super.rewriteFilterItem(item); } } }
/** * Rewrites a (non-compound) {@link FilterItem} when it's operand has already been rewritten into a SQL literal. * * @param item * @param operandLiteral * @return */ protected String rewriteFilterItemWithOperandLiteral(FilterItem item, String operandLiteral) { final OperatorType operator = item.getOperator(); final SelectItem selectItem = item.getSelectItem(); final StringBuilder sb = new StringBuilder(); sb.append(selectItem.getSameQueryAlias(false)); FilterItem.appendOperator(sb, item.getOperand(), operator); sb.append(operandLiteral); return sb.toString(); }
@Override public String rewriteFilterItem(final FilterItem item) { if (item.getOperand() instanceof String && item.getOperand().equals("")) { // In Oracle empty strings are treated as null. Typical SQL constructs with an empty string do not work. return super.rewriteFilterItem(new FilterItem(item.getSelectItem(), item.getOperator(), null)); } else { return super.rewriteFilterItem(item); } } }
private static String buildWhereClauseItem(final FilterItem whereItem, final String queryObjectHandle) { final StringBuilder whereClauseItemBuilder = new StringBuilder(); whereClauseItemBuilder.append(queryObjectHandle); whereClauseItemBuilder.append("."); whereClauseItemBuilder.append(whereItem.getSelectItem().getColumn().getName()); whereClauseItemBuilder.append(whereItem.getOperator().toSql()); final Object operand = whereItem.getOperand(); if (operand instanceof String) { whereClauseItemBuilder.append("\""); } whereClauseItemBuilder.append(operand); if (operand instanceof String) { whereClauseItemBuilder.append("\""); } return whereClauseItemBuilder.toString(); } }
/** * Rewrites a (non-compound) {@link FilterItem} when it's operand has already been rewritten into a SQL literal. * * @param item * @param operandLiteral * @return */ protected String rewriteFilterItemWithOperandLiteral(FilterItem item, String operandLiteral) { final OperatorType operator = item.getOperator(); final SelectItem selectItem = item.getSelectItem(); final StringBuilder sb = new StringBuilder(); sb.append(selectItem.getSameQueryAlias(false)); FilterItem.appendOperator(sb, item.getOperand(), operator); sb.append(operandLiteral); return sb.toString(); }
final Object operand = whereItem.getOperand();
@Override public String rewriteFilterItem(FilterItem item) { final SelectItem selectItem = item.getSelectItem(); final Object itemOperand = item.getOperand(); final OperatorType operator = item.getOperator(); if (null != selectItem && itemOperand != null) { ColumnType columnType = selectItem.getExpectedColumnType(); if (columnType != null) { if (columnType.isTimeBased()) { // special logic for DB2 based time operands. StringBuilder sb = new StringBuilder(); sb.append(selectItem.getSameQueryAlias(true)); final Object operand = FilterItem.appendOperator(sb, itemOperand, operator); if (operand instanceof SelectItem) { final String selectItemString = ((SelectItem) operand).getSameQueryAlias(true); sb.append(selectItemString); } else { Date date = TimeComparator.toDate(itemOperand); if (date == null) { throw new IllegalStateException("Could not convert " + itemOperand + " to date"); } final String sqlValue = FormatHelper.formatSqlTime(columnType, date, true, "('", "')"); sb.append(sqlValue); } return sb.toString(); } } } return super.rewriteFilterItem(item); }
final Object operand = item.getOperand(); final OperatorType operator = item.getOperator();
private FilterItem copyFilterItem(FilterItem item, Object[] values, AtomicInteger parameterIndex) { if (item.isCompoundFilter()) { final FilterItem[] childItems = item.getChildItems(); final FilterItem[] newChildItems = new FilterItem[childItems.length]; for (int i = 0; i < childItems.length; i++) { final FilterItem childItem = childItems[i]; final FilterItem newChildItem = copyFilterItem(childItem, values, parameterIndex); newChildItems[i] = newChildItem; } final FilterItem newFilter = new FilterItem(item.getLogicalOperator(), newChildItems); return newFilter; } else { if (item.getOperand() instanceof QueryParameter) { final Object newOperand = values[parameterIndex.getAndIncrement()]; final FilterItem newFilter = new FilterItem(item.getSelectItem(), item.getOperator(), newOperand); return newFilter; } else { return item; } } }
private FilterItem copyFilterItem(FilterItem item, Object[] values, AtomicInteger parameterIndex) { if (item.isCompoundFilter()) { final FilterItem[] childItems = item.getChildItems(); final FilterItem[] newChildItems = new FilterItem[childItems.length]; for (int i = 0; i < childItems.length; i++) { final FilterItem childItem = childItems[i]; final FilterItem newChildItem = copyFilterItem(childItem, values, parameterIndex); newChildItems[i] = newChildItem; } final FilterItem newFilter = new FilterItem(item.getLogicalOperator(), newChildItems); return newFilter; } else { if (item.getOperand() instanceof QueryParameter) { final Object newOperand = values[parameterIndex.getAndIncrement()]; final FilterItem newFilter = new FilterItem(item.getSelectItem(), item.getOperator(), newOperand); return newFilter; } else { return item; } } }
@Override public void execute() throws MetaModelException { String sql = createSqlStatement(); logger.debug("Delete statement created: {}", sql); final boolean reuseStatement = !_inlineValues; final PreparedStatement st = _updateCallback.getPreparedStatement(sql, reuseStatement, false); try { if (reuseStatement) { int valueCounter = 1; final List<FilterItem> whereItems = getWhereItems(); for (FilterItem whereItem : whereItems) { if (JdbcUtils.isPreparedParameterCandidate(whereItem)) { Object operand = whereItem.getOperand(); _queryRewriter.setStatementParameter(st, valueCounter, whereItem.getSelectItem().getColumn(), operand); valueCounter++; } } } _updateCallback.executeDelete(st, reuseStatement); } catch (SQLException e) { throw JdbcUtils.wrapException(e, "execute delete statement: " + sql, JdbcActionType.UPDATE); } finally { if (_inlineValues) { FileHelper.safeClose(st); } } }
@Override public void execute() throws MetaModelException { String sql = createSqlStatement(); logger.debug("Delete statement created: {}", sql); final boolean reuseStatement = !_inlineValues; final PreparedStatement st = _updateCallback.getPreparedStatement(sql, reuseStatement, false); try { if (reuseStatement) { int valueCounter = 1; final List<FilterItem> whereItems = getWhereItems(); for (FilterItem whereItem : whereItems) { if (JdbcUtils.isPreparedParameterCandidate(whereItem)) { Object operand = whereItem.getOperand(); _queryRewriter.setStatementParameter(st, valueCounter, whereItem.getSelectItem().getColumn(), operand); valueCounter++; } } } _updateCallback.executeDelete(st, reuseStatement); } catch (SQLException e) { throw JdbcUtils.wrapException(e, "execute delete statement: " + sql, JdbcActionType.UPDATE); } finally { if (_inlineValues) { FileHelper.safeClose(st); } } }