@Override public String toString() { return "EXPLAIN " + select.toString(); }
String pageSql = pageSelect.toString();
public boolean isxLock() { return StringUtils.endsWithIgnoreCase(StringUtils.trimAllWhitespace(this.select.toString()), StringUtils.trimAllWhitespace(SqlUtils.FOR_UPDATE)); }
public boolean issLock() { return StringUtils.endsWithIgnoreCase(StringUtils.trimAllWhitespace(this.select.toString()), StringUtils.trimAllWhitespace(SqlUtils.LOCK_IN_SHARE_MODE)); }
@Override public String toString() { String sql; String createOps = PlainSelect.getStringList(createOptionsStrings, false, false); sql = "CREATE " + (unlogged ? "UNLOGGED " : "") + (!"".equals(createOps) ? createOps + " " : "") + "TABLE " + (ifNotExists ? "IF NOT EXISTS " : "") + table; if (select != null) { sql += " AS " + (selectParenthesis ? "(" : "") + select.toString() + (selectParenthesis ? ")" : ""); } else { sql += " ("; sql += PlainSelect.getStringList(columnDefinitions, true, false); if (indexes != null && !indexes.isEmpty()) { sql += ", "; sql += PlainSelect.getStringList(indexes); } sql += ")"; String options = PlainSelect.getStringList(tableOptionsStrings, false, false); if (options != null && options.length() > 0) { sql += " " + options; } } return sql; } }
/** * convert to order by sql * * @param sql * @param orderBy * @return */ public static String converToOrderBySql(String sql, String orderBy) { //解析SQL Statement stmt = null; try { stmt = CCJSqlParserUtil.parse(sql); Select select = (Select) stmt; SelectBody selectBody = select.getSelectBody(); //处理body-去最外层order by List<OrderByElement> orderByElements = extraOrderBy(selectBody); String defaultOrderBy = PlainSelect.orderByToString(orderByElements); if (defaultOrderBy.indexOf('?') != -1) { throw new PageException("原SQL[" + sql + "]中的order by包含参数,因此不能使用OrderBy插件进行修改!"); } //新的sql sql = select.toString(); } catch (Throwable e) { throw new PageException("处理排序失败: " + e, e); } return sql + " order by " + orderBy; }
String result = select.toString(); return result;
@Override public String toString() { return selectQuery.toString(); }
public static String restructSelect(String sql, Function<Collection<Table>, String> conditionSupplier, Consumer<SelectColumn> columnFilter) throws JSQLParserException { Statement stmt = CCJSqlParserUtil.parse(sql); Select select = (Select) stmt; SelectBody selectBody = select.getSelectBody(); restruct(selectBody, conditionSupplier, columnFilter); return select.toString(); }
@Override public String toString() { String sql; String createOps = PlainSelect.getStringList(createOptionsStrings, false, false); sql = "CREATE " + (unlogged ? "UNLOGGED " : "") + (!"".equals(createOps) ? createOps + " " : "") + "TABLE " + (ifNotExists ? "IF NOT EXISTS " : "") + table; if (select != null) { sql += " AS " + (selectParenthesis ? "(" : "") + select.toString() + (selectParenthesis ? ")" : ""); } else { sql += " ("; sql += PlainSelect.getStringList(columnDefinitions, true, false); if (indexes != null && !indexes.isEmpty()) { sql += ", "; sql += PlainSelect.getStringList(indexes); } sql += ")"; String options = PlainSelect.getStringList(tableOptionsStrings, false, false); if (options != null && options.length() > 0) { sql += " " + options; } } return sql; } }
/** * convert to order by sql * * @param sql * @param orderBy * @return */ public static String converToOrderBySql(String sql, String orderBy) { //解析SQL Statement stmt = null; try { stmt = CCJSqlParserUtil.parse(sql); Select select = (Select) stmt; SelectBody selectBody = select.getSelectBody(); //处理body-去最外层order by List<OrderByElement> orderByElements = extraOrderBy(selectBody); String defaultOrderBy = PlainSelect.orderByToString(orderByElements); if (defaultOrderBy.indexOf('?') != -1) { throw new RuntimeException("原SQL[" + sql + "]中的order by包含参数,因此不能使用OrderBy插件进行修改!"); } //新的sql sql = select.toString(); } catch (Throwable e) { e.printStackTrace(); } return sql + " order by " + orderBy; }
/** * convert to order by sql * * @param sql * @param orderBy * @return */ public static String converToOrderBySql(String sql, String orderBy) { //解析SQL Statement stmt = null; try { stmt = CCJSqlParserUtil.parse(sql); Select select = (Select) stmt; SelectBody selectBody = select.getSelectBody(); //处理body-去最外层order by List<OrderByElement> orderByElements = extraOrderBy(selectBody); String defaultOrderBy = PlainSelect.orderByToString(orderByElements); if (defaultOrderBy.indexOf('?') != -1) { throw new RuntimeException("原SQL[" + sql + "]中的order by包含参数,因此不能使用OrderBy插件进行修改!"); } //新的sql sql = select.toString(); } catch (Throwable e) { e.printStackTrace(); } return sql + " order by " + orderBy; }
/** The query for obtaining values of parameters is almost the same with the original source query, except that we only need to distinct project the columns needed for the template expansion */ private static String getTemplateValuesQuery(String sql, List<SelectExpressionItem> templateColumns) throws JSQLParserException { Select select = (Select)CCJSqlParserUtil.parse(sql); PlainSelect plainSelect = (PlainSelect)select.getSelectBody(); plainSelect.setDistinct(new Distinct()); plainSelect.setSelectItems(ImmutableList.copyOf(templateColumns)); // SelectExpressionItem -> SelectItem return select.toString(); }
/** The query for obtaining values of parameters is almost the same with the original source query, except that we only need to distinct project the columns needed for the template expansion */ private static String getTemplateValuesQuery(String sql, List<SelectExpressionItem> templateColumns) throws JSQLParserException { Select select = (Select)CCJSqlParserUtil.parse(sql); PlainSelect plainSelect = (PlainSelect)select.getSelectBody(); plainSelect.setDistinct(new Distinct()); plainSelect.setSelectItems(ImmutableList.copyOf(templateColumns)); // SelectExpressionItem -> SelectItem return select.toString(); }
public String getCountSql(String sql, Pager pager, String database) { //校验是否支持该sql this.isSupportedSql(sql); if (CACHE.get(sql) != null) { return CACHE.get(sql); } //解析SQL Statement stmt; try { stmt = CCJSqlParserUtil.parse(sql); } catch (Throwable e) { //无法解析的用一般方法返回count语句 String countSql = super.getCountSql(sql, pager, database); CACHE.put(sql, countSql); return countSql; } Select select = (Select) stmt; SelectBody selectBody = select.getSelectBody(); //处理body-去order by processSelectBody(selectBody); //处理with-去order by processWithItemsList(select.getWithItemsList()); //处理为count查询 sqlToCount(select); String result = select.toString(); CACHE.put(sql, result); return result; }
/** * convert to order by sql * * @param sql * @param orderBy * @return */ public static String converToOrderBySql(String sql, String orderBy) { //解析SQL Statement stmt = null; try { stmt = CCJSqlParserUtil.parse(sql); Select select = (Select) stmt; SelectBody selectBody = select.getSelectBody(); //处理body-去最外层order by List<OrderByElement> orderByElements = extraOrderBy(selectBody); String defaultOrderBy = PlainSelect.orderByToString(orderByElements); if (defaultOrderBy.indexOf('?') != -1) { throw new PageException("原SQL[" + sql + "]中的order by包含参数,因此不能使用OrderBy插件进行修改!"); } //新的sql sql = select.toString(); } catch (Throwable e) { throw new PageException("处理排序失败: " + e, e); } return sql + " order by " + orderBy; }
/** * Create a new query with the changed projection and selection */ private static String getInstantiatedSQL(String sql, List<SelectItem> newColumns, List<SelectExpressionItem> templateColumns, List<String> values) throws JSQLParserException { Select select = (Select) CCJSqlParserUtil.parse(sql); PlainSelect plainSelect = (PlainSelect)select.getSelectBody(); Expression where = plainSelect.getWhere(); int size = templateColumns.size(); // both lists have the same size for (int i = 0; i < size; i++) { BinaryExpression condition = new EqualsTo(); condition.setLeftExpression(templateColumns.get(i).getExpression()); condition.setRightExpression(new StringValue("'" + values.get(i) + "'")); where = (where == null) ? condition : new AndExpression(where, condition); } plainSelect.setWhere(where); // where cannot be null plainSelect.setSelectItems(newColumns); return select.toString(); }
/** * Create a new query with the changed projection and selection */ private static String getInstantiatedSQL(String sql, List<SelectItem> newColumns, List<SelectExpressionItem> templateColumns, List<String> values) throws JSQLParserException { Select select = (Select) CCJSqlParserUtil.parse(sql); PlainSelect plainSelect = (PlainSelect)select.getSelectBody(); Expression where = plainSelect.getWhere(); int size = templateColumns.size(); // both lists have the same size for (int i = 0; i < size; i++) { BinaryExpression condition = new EqualsTo(); condition.setLeftExpression(templateColumns.get(i).getExpression()); condition.setRightExpression(new StringValue("'" + values.get(i) + "'")); where = (where == null) ? condition : new AndExpression(where, condition); } plainSelect.setWhere(where); // where cannot be null plainSelect.setSelectItems(newColumns); return select.toString(); }
/** * Method to substitute * from the select query. It add the columns name that are used in the mapping * @param select the query with or without * * @param variables the variables used in the mapping, this are the needed columns for our select query * @return the query with columns or functions in the projection part */ public String getMappingQuery(Select select, Set<Variable> variables) { VariableSet variableNames = new VariableSet(variables); if (select.getWithItemsList() != null) { for (WithItem withItem : select.getWithItemsList()) withItem.accept(new ReplaceStarSelectVisitor(false, null, variableNames)); } select.getSelectBody().accept(new ReplaceStarSelectVisitor(false, null, variableNames)); return select.toString(); }