private static StringBuilder getSql(FunctionRenderContext functionRenderContext) { String subquery = functionRenderContext.getArgument(0); if (startsWithIgnoreCase(subquery, "(select")) { int endIndex = subquery.length() - (subquery.charAt(subquery.length() - 1) == ')' ? 1 : 0); return new StringBuilder(subquery.length() - 2).append(subquery, 1, endIndex); } return new StringBuilder(subquery); }
protected Count getCount(FunctionRenderContext context) { if (context.getArgumentsSize() == 0) { throw new RuntimeException("The " + FUNCTION_NAME + " function needs at least one argument!"); } boolean distinct = false; int startIndex = 0; String maybeDistinct = context.getArgument(0); if (("'" + DISTINCT_QUALIFIER + "'").equalsIgnoreCase(maybeDistinct)) { distinct = true; startIndex++; } // Hibernate puts the columns for embeddables into a single string, so we have to count items List<String> expressions = new ArrayList<>(context.getArgumentsSize()); for (int i = startIndex; i < context.getArgumentsSize(); i++) { expressions.addAll(SqlUtils.getExpressionItems(context.getArgument(i))); } if (expressions.isEmpty()) { throw new RuntimeException("The " + AbstractCountFunction.FUNCTION_NAME + " function needs at least one expression to count! args=" + context); } return new Count(distinct, expressions); }
@Override public void render(FunctionRenderContext context) { if (context.getArgumentsSize() != 2) { throw new RuntimeException("The page position function needs exactly two arguments <base_query> and <entity_id>! args=" + context); } String subquery = context.getArgument(0); String subqueryStart = "(select "; int fromIndex; if (!startsWithIgnoreCase(subquery, subqueryStart)) { throw new IllegalArgumentException("Expected a subquery as the second parameter but was: " + subquery); } else if ((fromIndex = subquery.toLowerCase().indexOf(" from ")) < 1) { throw new IllegalArgumentException("Expected a subquery as the second parameter but was: " + subquery); } String id = subquery.substring(subqueryStart.length(), fromIndex); if (id.indexOf(',') > -1) { throw new IllegalArgumentException("Expected a subquery with a simple id but it was composite: " + subquery); } int dotIndex = id.indexOf('.'); if (dotIndex < 0) { throw new IllegalArgumentException("Expected that the id is fully qualified but it isn't: " + id); } String idName = id.substring(dotIndex + 1); renderPagePosition(context, idName); }
protected void renderLimitOffset(FunctionRenderContext functionRenderContext) { StringBuilder sqlSb = getSql(functionRenderContext); if (limitIncludesOffset) { // Careful, parameters are not supported in this case as that would require parameter rewriting or something like that String limit = functionRenderContext.getArgument(1); String offset = functionRenderContext.getArgument(2); if (limit.contains("?") || offset.contains("?")) { throw new IllegalArgumentException("Limit and offset in subquery can not be a parameter!"); } Integer limitValue = Integer.parseInt(limit); Integer offsetValue = Integer.parseInt(offset); dbmsDialect.appendExtendedSql(sqlSb, DbmsStatementType.SELECT, true, false, null, Integer.toString(limitValue + offsetValue), offset, null, null); } else { dbmsDialect.appendExtendedSql(sqlSb, DbmsStatementType.SELECT, true, false, null, functionRenderContext.getArgument(1), functionRenderContext.getArgument(2), null, null); } functionRenderContext.addChunk(sqlSb.toString()); }
@Override public void render(FunctionRenderContext functionRenderContext) { switch (functionRenderContext.getArgumentsSize()) { case 3: if (isNotNull(functionRenderContext.getArgument(1)) && isNotNull(functionRenderContext.getArgument(2))) { renderLimitOffset(functionRenderContext); return; } break; case 2: if (isNotNull(functionRenderContext.getArgument(1))) { renderLimitOnly(functionRenderContext); return; } break; default: break; } throw new RuntimeException("The limit function needs two or three non null arguments <sub_query>, <limit> and optionally <offset>! args=" + functionRenderContext); }
@Override protected void renderDiff(FunctionRenderContext context) { boolean firstContains = context.getArgument(0).contains("?"); if (firstContains && context.getArgument(0).length() != 1) { throw new IllegalArgumentException("Only simple parameters or expressions are allowed because of a needed reordering in SQL which is otherwise not possible! Expressions ['" + context.getArgument(0) + "', '" + context.getArgument(1) + "'] do not comply!"); } if (firstContains) { paramRenderer.start(context).addArgument(0).build(); } else { // Reuse the expression multiple times renderer.start(context).addArgument(0).build(); } } }
String offset = null; for (int i = 0; i < functionRenderContext.getArgumentsSize(); i++) { String argument = functionRenderContext.getArgument(i);
@Override protected void renderDiff(FunctionRenderContext context) { boolean firstContains = context.getArgument(0).contains("?"); boolean secondContains = context.getArgument(1).contains("?"); if (firstContains && context.getArgument(0).length() != 1 || secondContains && context.getArgument(1).length() != 1) { throw new IllegalArgumentException("Only simple parameters or expressions are allowed because of a needed reordering in SQL which is otherwise not possible! Expressions ['" + context.getArgument(0) + "', '" + context.getArgument(1) + "'] do not comply!"); } if (firstContains & secondContains) { bothParamRenderer.start(context).addArgument(0).addArgument(1).build(); } else { // Reuse the expression multiple times renderer.start(context).addArgument(0).addArgument(1).build(); } } }
int startIndex = 0; int argsSize = context.getArgumentsSize(); String maybeDistinct = context.getArgument(0); expression = context.getArgument(startIndex); String argument = context.getArgument(i); if ("'SEPARATOR'".equalsIgnoreCase(argument)) { mode = Mode.SEPARATOR;
protected void renderLimitOnly(FunctionRenderContext functionRenderContext) { StringBuilder sqlSb = getSql(functionRenderContext); dbmsDialect.appendExtendedSql(sqlSb, DbmsStatementType.SELECT, true, false, null, functionRenderContext.getArgument(1), null, null, null); functionRenderContext.addChunk(sqlSb.toString()); }
String subquery = functionRenderContext.getArgument(0); StringBuilder sb = new StringBuilder(); int subqueryEndIndex = subquery.lastIndexOf(" and 1=1"); String entityName = unquote(functionRenderContext.getArgument(1)); String valuesClause = unquote(functionRenderContext.getArgument(2)); String valuesAliases = unquote(functionRenderContext.getArgument(3)); String syntheticPredicate = unquote(functionRenderContext.getArgument(4)); String exampleQuerySqlAlias = syntheticPredicate.substring(0, syntheticPredicate.indexOf('.')); String valuesTableSqlAlias = subquery.substring(aliasStartIndex, aliasEndIndex);
@Override protected void renderPagePosition(FunctionRenderContext functionRenderContext, String idName) { String subquery = functionRenderContext.getArgument(0); String subqueryStart = "(select "; renderer.start(functionRenderContext).addParameter(subquery.substring(subqueryStart.length())).addArgument(1).addParameter(idName).build(); } }
@Override public void render(FunctionRenderContext context) { String operator = context.getArgument(0); List<String> elements = new ArrayList<>(context.getArgumentsSize() - 1); for (int argIdx = 1; argIdx < context.getArgumentsSize(); argIdx++) { String caseWhenExpression = context.getArgument(argIdx);
@Override protected void renderDiff(FunctionRenderContext context) { boolean firstContains = context.getArgument(0).contains("?"); boolean secondContains = context.getArgument(1).contains("?"); if (firstContains && context.getArgument(0).length() != 1 || secondContains && context.getArgument(1).length() != 1) { throw new IllegalArgumentException("Only simple parameters or expressions are allowed because of a needed reordering in SQL which is otherwise not possible! Expressions ['" + context.getArgument(0) + "', '" + context.getArgument(1) + "'] do not comply!"); } if (firstContains) { if (secondContains) { bothParamRenderer.start(context).addArgument(0).addArgument(1).build(); } else { firstParamRenderer.start(context).addArgument(0).addArgument(1).build(); } } else if (secondContains) { secondParamRenderer.start(context).addArgument(0).addArgument(1).build(); } else { // Reuse the expression multiple times renderer.start(context).addArgument(0).addArgument(1).build(); } } }
if (context.getArgumentsSize() > 0) { if (viewRootExpression != null) { String firstArgument = context.getArgument(0); if (viewRootIdPath.startsWith(firstArgument) && (viewRootIdPath.length() == firstArgument.length() || firstArgument.charAt(viewRootIdPath.length()) == '.')) { if (viewRootIdPath.equals(context.getArgument(0))) { context.addChunk(":"); context.addChunk(getIdParamName());
@Override protected void renderDiff(FunctionRenderContext context) { boolean firstContains = context.getArgument(0).contains("?"); boolean secondContains = context.getArgument(1).contains("?"); if (firstContains && context.getArgument(0).length() != 1 || secondContains && context.getArgument(1).length() != 1) { throw new IllegalArgumentException("Only simple parameters or expressions are allowed because of a needed reordering in SQL which is otherwise not possible! Expressions ['" + context.getArgument(0) + "', '" + context.getArgument(1) + "'] do not comply!"); } if (firstContains) { if (secondContains) { bothParamRenderer.start(context).addArgument(0).addArgument(1).build(); } else { firstParamRenderer.start(context).addArgument(0).addArgument(1).build(); } } else if (secondContains) { secondParamRenderer.start(context).addArgument(0).addArgument(1).build(); } else { // Reuse the expression multiple times renderer.start(context).addArgument(0).addArgument(1).build(); } } }
@Override protected void renderDiff(FunctionRenderContext context) { boolean firstContains = context.getArgument(0).contains("?"); boolean secondContains = context.getArgument(1).contains("?"); if (firstContains && context.getArgument(0).length() != 1 || secondContains && context.getArgument(1).length() != 1) { throw new IllegalArgumentException("Only simple parameters or expressions are allowed because of a needed reordering in SQL which is otherwise not possible! Expressions ['" + context.getArgument(0) + "', '" + context.getArgument(1) + "'] do not comply!"); } if (firstContains) { if (secondContains) { bothParamRenderer.start(context).addArgument(0).addArgument(1).build(); } else { firstParamRenderer.start(context).addArgument(0).addArgument(1).build(); } } else if (secondContains) { secondParamRenderer.start(context).addArgument(0).addArgument(1).build(); } else { // Reuse the expression multiple times renderer.start(context).addArgument(0).addArgument(1).build(); } } }
@Override protected void renderDiff(FunctionRenderContext context) { boolean firstContains = context.getArgument(0).contains("?"); boolean secondContains = context.getArgument(1).contains("?"); if (firstContains && context.getArgument(0).length() != 1 || secondContains && context.getArgument(1).length() != 1) { throw new IllegalArgumentException("Only simple parameters or expressions are allowed because of a needed reordering in SQL which is otherwise not possible! Expressions ['" + context.getArgument(0) + "', '" + context.getArgument(1) + "'] do not comply!"); } if (firstContains) { if (secondContains) { bothParamRenderer.start(context).addArgument(0).addArgument(1).build(); } else { firstParamRenderer.start(context).addArgument(0).addArgument(1).build(); } } else if (secondContains) { secondParamRenderer.start(context).addArgument(0).addArgument(1).build(); } else { // Reuse the expression multiple times renderer.start(context).addArgument(0).addArgument(1).build(); } } }