com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem Java Examples
The following examples show how to use
com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ItemFuncGroupConcat.java From dble with GNU General Public License v2.0 | 6 votes |
@Override public SQLExpr toExpression() { SQLAggregateExpr aggregate = new SQLAggregateExpr(funcName()); if (hasWithDistinct()) { aggregate.setOption(SQLAggregateOption.DISTINCT); } if (orders != null) { SQLOrderBy orderBy = new SQLOrderBy(); for (Order order : orders) { SQLSelectOrderByItem orderItem = new SQLSelectOrderByItem(order.getItem().toExpression()); orderItem.setType(order.getSortOrder()); orderBy.addItem(orderItem); } aggregate.putAttribute(ItemFuncKeyWord.ORDER_BY, orderBy); } for (Item arg : args) { aggregate.addArgument(arg.toExpression()); } if (seperator != null) { SQLCharExpr sep = new SQLCharExpr(seperator); aggregate.putAttribute(ItemFuncKeyWord.SEPARATOR, sep); } return aggregate; }
Example #2
Source File: SqlParser.java From elasticsearch-sql with Apache License 2.0 | 6 votes |
private void addOrderByToSelect(Select select, List<SQLSelectOrderByItem> items, String alias) throws SqlParseException { for (SQLSelectOrderByItem sqlSelectOrderByItem : items) { SQLExpr expr = sqlSelectOrderByItem.getExpr(); Field f = FieldMaker.makeField(expr, null, null); String orderByName = f.toString(); if (sqlSelectOrderByItem.getType() == null) { sqlSelectOrderByItem.setType(SQLOrderingSpecification.ASC); //zhongshu-comment 默认是升序排序 } String type = sqlSelectOrderByItem.getType().toString(); orderByName = orderByName.replace("`", ""); if (alias != null) orderByName = orderByName.replaceFirst(alias + "\\.", ""); ScriptSortBuilder.ScriptSortType scriptSortType = judgeIsStringSort(expr); select.addOrderBy(f.getNestedPath(), orderByName, type, scriptSortType); } }
Example #3
Source File: SqlParser.java From elasticsearch-sql with Apache License 2.0 | 6 votes |
public JoinSelect parseJoinSelect(SQLQueryExpr sqlExpr) throws SqlParseException { MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) sqlExpr.getSubQuery().getQuery(); List<From> joinedFrom = findJoinedFrom(query.getFrom()); if (joinedFrom.size() != 2) throw new RuntimeException("currently supports only 2 tables join"); JoinSelect joinSelect = createBasicJoinSelectAccordingToTableSource((SQLJoinTableSource) query.getFrom()); List<Hint> hints = parseHints(query.getHints()); joinSelect.setHints(hints); String firstTableAlias = joinedFrom.get(0).getAlias(); String secondTableAlias = joinedFrom.get(1).getAlias(); Map<String, Where> aliasToWhere = splitAndFindWhere(query.getWhere(), firstTableAlias, secondTableAlias); Map<String, List<SQLSelectOrderByItem>> aliasToOrderBy = splitAndFindOrder(query.getOrderBy(), firstTableAlias, secondTableAlias); List<Condition> connectedConditions = getConditionsFlatten(joinSelect.getConnectedWhere()); joinSelect.setConnectedConditions(connectedConditions); fillTableSelectedJoin(joinSelect.getFirstTable(), query, joinedFrom.get(0), aliasToWhere.get(firstTableAlias), aliasToOrderBy.get(firstTableAlias), connectedConditions); fillTableSelectedJoin(joinSelect.getSecondTable(), query, joinedFrom.get(1), aliasToWhere.get(secondTableAlias), aliasToOrderBy.get(secondTableAlias), connectedConditions); updateJoinLimit(query.getLimit(), joinSelect); //todo: throw error feature not supported: no group bys on joins ? return joinSelect; }
Example #4
Source File: SqlParser.java From elasticsearch-sql with Apache License 2.0 | 6 votes |
private Map<String, List<SQLSelectOrderByItem>> splitAndFindOrder(SQLOrderBy orderBy, String firstTableAlias, String secondTableAlias) throws SqlParseException { Map<String, List<SQLSelectOrderByItem>> aliasToOrderBys = new HashMap<>(); aliasToOrderBys.put(firstTableAlias, new ArrayList<SQLSelectOrderByItem>()); aliasToOrderBys.put(secondTableAlias, new ArrayList<SQLSelectOrderByItem>()); if (orderBy == null) return aliasToOrderBys; List<SQLSelectOrderByItem> orderByItems = orderBy.getItems(); for (SQLSelectOrderByItem orderByItem : orderByItems) { if (orderByItem.getExpr().toString().startsWith(firstTableAlias + ".")) { aliasToOrderBys.get(firstTableAlias).add(orderByItem); } else if (orderByItem.getExpr().toString().startsWith(secondTableAlias + ".")) { aliasToOrderBys.get(secondTableAlias).add(orderByItem); } else throw new SqlParseException("order by field on join request should have alias before, got " + orderByItem.getExpr().toString()); } return aliasToOrderBys; }
Example #5
Source File: JoinParser.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
private void parserOrderBy(SQLOrderBy orderby) { if (orderby != null ){ for (int i = 0; i < orderby.getItems().size(); i++) { SQLSelectOrderByItem orderitem = orderby.getItems().get(i); tableFilter.addOrders(i, orderitem.getExpr().toString(), getSQLExprToAsc(orderitem.getType())); } } }
Example #6
Source File: DruidSelectOracleParser.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
protected void parseOrderAggGroupOracle(SQLStatement stmt, RouteResultset rrs, OracleSelectQueryBlock mysqlSelectQuery, SchemaConfig schema) { Map<String, String> aliaColumns = parseAggGroupCommon(schema, stmt,rrs, mysqlSelectQuery); OracleSelectQueryBlock oracleSelect= (OracleSelectQueryBlock) mysqlSelectQuery.getParent(); if(oracleSelect.getOrderBy() != null) { List<SQLSelectOrderByItem> orderByItems = oracleSelect.getOrderBy().getItems(); rrs.setOrderByCols(buildOrderByCols(orderByItems,aliaColumns)); } isNeedParseOrderAgg=false; }
Example #7
Source File: DruidSelectSqlServerParser.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
private void parseOrderAggGroupSqlServer(SchemaConfig schema, SQLStatement stmt, RouteResultset rrs, SQLServerSelectQueryBlock mysqlSelectQuery) { Map<String, String> aliaColumns = parseAggGroupCommon(schema, stmt,rrs, mysqlSelectQuery); SQLServerSelectQueryBlock oracleSelect= (SQLServerSelectQueryBlock) mysqlSelectQuery.getParent(); if(oracleSelect.getOrderBy() != null) { List<SQLSelectOrderByItem> orderByItems = oracleSelect.getOrderBy().getItems(); rrs.setOrderByCols(buildOrderByCols(orderByItems,aliaColumns)); } }
Example #8
Source File: MySqlSelectParser.java From baymax with Apache License 2.0 | 5 votes |
/** * 解析Orderby * @param result * @param plan * @param mysqlSelectQuery */ protected void parseOrderby(ParseResult result, ExecutePlan plan, MySqlSelectQueryBlock mysqlSelectQuery){ if (mysqlSelectQuery.getOrderBy() == null){ return; } List<SQLSelectOrderByItem> orderByItems = mysqlSelectQuery.getOrderBy().getItems(); if (orderByItems == null || orderByItems.size() == 0){ return; } List<SQLSelectItem> selectList = mysqlSelectQuery.getSelectList(); List<OrderbyColumn> orderbyColumns = new ArrayList<OrderbyColumn>(orderByItems.size()); for(SQLSelectOrderByItem item : orderByItems){ String name = StringUtil.removeDot(item.getExpr().toString()); if (result.getAliaColumns() != null){ String alias = result.getAliaColumns().get(name); if (alias != null){ // select user_id as uid ....order by user_id // 要把oderby的user_id转换为uid,以便结果集合并 orderbyColumns.add(new OrderbyColumn(alias, OrderbyColumn.buildOrderbyType(item.getType()))); continue; } } if (!result.isHasAllColumnExpr()){ // select列表中没有orderby的字段 添加,用于后面做合并 SQLIdentifierExpr exp = new SQLIdentifierExpr(name); // item SQLSelectItem addItem = new SQLSelectItem(); addItem.setExpr(exp); exp.setParent(item); selectList.add(addItem); } orderbyColumns.add(new OrderbyColumn(name, OrderbyColumn.buildOrderbyType(item.getType()))); } plan.setOrderbyColumns(orderbyColumns); }
Example #9
Source File: SqlParser.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
private void findOrderBy(MySqlSelectQueryBlock query, Select select) throws SqlParseException { SQLOrderBy orderBy = query.getOrderBy(); if (orderBy == null) { return; } List<SQLSelectOrderByItem> items = orderBy.getItems(); addOrderByToSelect(select, items, null); }
Example #10
Source File: SqlParser.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
private void fillTableSelectedJoin(TableOnJoinSelect tableOnJoin, MySqlSelectQueryBlock query, From tableFrom, Where where, List<SQLSelectOrderByItem> orderBys, List<Condition> conditions) throws SqlParseException { String alias = tableFrom.getAlias(); fillBasicTableSelectJoin(tableOnJoin, tableFrom, where, orderBys, query); tableOnJoin.setConnectedFields(getConnectedFields(conditions, alias)); tableOnJoin.setSelectedFields(new ArrayList<Field>(tableOnJoin.getFields())); tableOnJoin.setAlias(alias); tableOnJoin.fillSubQueries(); }
Example #11
Source File: ShardLimitSqlWithConditionRewrite.java From Zebra with Apache License 2.0 | 4 votes |
private void initConditionExpr() { SQLBinaryOperator startOperator, endOperator; // 上下线每个条件的边际表达式 SQLBinaryOpExpr startBoundaryExpr = null, endBoundaryExpr = null; for (SQLSelectOrderByItem orderByEle : context.getOrderBy().getItems()) { if (orderByEle.getType() == null || ((SQLOrderingSpecification) orderByEle.getType()).name().equals("ASC")) { startOperator = SQLBinaryOperator.GreaterThan; endOperator = SQLBinaryOperator.LessThan; } else { startOperator = SQLBinaryOperator.LessThan; endOperator = SQLBinaryOperator.GreaterThan; } try { SQLExpr starColumnExpr = convertColumnDataToSQLExpr(startData.get(orderByEle.getExpr().toString())); SQLExpr endColumnExpr = convertColumnDataToSQLExpr(endData.get(orderByEle.getExpr().toString())); SQLExpr compareExpr = orderByEle.getExpr(); // 如果orderby的column是一个表达式的结果,则把表达式作为新条件的比较变量 SQLObjectImpl selectItem = context.getSelectItemMap().get(orderByEle.getExpr().toString()); if (selectItem == null) { selectItem = context.getSelectItemMap().get(orderByEle.getExpr().toString().toLowerCase()); if (selectItem == null) { selectItem = context.getSelectItemMap().get(orderByEle.getExpr().toString().toUpperCase()); } } if (selectItem != null) { if (selectItem instanceof SQLSelectItem) { compareExpr = ((SQLSelectItem) selectItem).getExpr(); } } SQLBinaryOpExpr startExpr = new SQLBinaryOpExpr(compareExpr, startOperator, starColumnExpr); SQLBinaryOpExpr endExpr = new SQLBinaryOpExpr(compareExpr, endOperator, endColumnExpr); if (startBoundaryExpr != null) { // 在上一个边际表达式的基础上加上and条件 addStartConditionExpr(new SQLBinaryOpExpr(startBoundaryExpr, SQLBinaryOperator.BooleanAnd, startExpr)); addEndConditionExpr(new SQLBinaryOpExpr(endBoundaryExpr, SQLBinaryOperator.BooleanAnd, endExpr)); startBoundaryExpr = new SQLBinaryOpExpr(startBoundaryExpr, SQLBinaryOperator.BooleanAnd, new SQLBinaryOpExpr(compareExpr, SQLBinaryOperator.Equality, starColumnExpr)); endBoundaryExpr = new SQLBinaryOpExpr(endBoundaryExpr, SQLBinaryOperator.BooleanAnd, new SQLBinaryOpExpr(compareExpr, SQLBinaryOperator.Equality, endColumnExpr)); } else { addStartConditionExpr(startExpr); addEndConditionExpr(endExpr); startBoundaryExpr = new SQLBinaryOpExpr(compareExpr, SQLBinaryOperator.Equality, starColumnExpr); endBoundaryExpr = new SQLBinaryOpExpr(compareExpr, SQLBinaryOperator.Equality, endColumnExpr); } } catch (SQLException ignore) { } } // 最后补上边际条件 addStartConditionExpr(startBoundaryExpr); addEndConditionExpr(endBoundaryExpr); }
Example #12
Source File: OrderByDataMerger.java From Zebra with Apache License 2.0 | 4 votes |
public int compareOrderByEle(RowData o1, RowData o2, MergeContext mergeContext) { SQLOrderBy orderBy = mergeContext.getOrderBy(); List<SQLSelectOrderByItem> items = orderBy.getItems(); try { for (SQLSelectOrderByItem orderByEle : items) { SQLName identifier = (SQLName) orderByEle .getExpr(); String columnLabel = mergeContext.getColumnNameAliasMapping().get(identifier.getSimpleName()); if (columnLabel == null) { columnLabel = identifier.getSimpleName(); } Object value1 = o1.get(columnLabel).getValue(); Class<?> type1 = o1.get(columnLabel).getType(); Object value2 = o2.get(columnLabel).getValue(); Class<?> type2 = o2.get(columnLabel).getType(); if (!type1.equals(type2)) { throw new SQLException("Invalid data"); } if (!Comparable.class.isAssignableFrom(type1)) { throw new SQLException("Can not orderBy column : " + identifier + " which isn't comparable."); } @SuppressWarnings({ "unchecked", "rawtypes" }) int compareRes = ((Comparable) value1).compareTo((Comparable) value2); if (orderByEle.getType() == null || ((SQLOrderingSpecification) orderByEle.getType()).name().equals("ASC")) { if (compareRes != 0) { return compareRes; } } else { if (compareRes != 0) { return compareRes < 0 ? 1 : -1; } } } return 0; } catch (SQLException e) { throw new RuntimeException(e); } }
Example #13
Source File: SqlParser.java From elasticsearch-sql with Apache License 2.0 | 4 votes |
private void fillBasicTableSelectJoin(TableOnJoinSelect select, From from, Where where, List<SQLSelectOrderByItem> orderBys, MySqlSelectQueryBlock query) throws SqlParseException { select.getFrom().add(from); findSelect(query, select, from.getAlias()); select.setWhere(where); addOrderByToSelect(select, orderBys, from.getAlias()); }