com.alibaba.druid.sql.ast.SQLOrderBy Java Examples

The following examples show how to use com.alibaba.druid.sql.ast.SQLOrderBy. 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 vote down vote up
@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 vote down vote up
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 #3
Source File: JoinParser.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
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 #4
Source File: TestMySQLItemVisitor.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testOrderby() {
    MySqlSelectQueryBlock query = getQuery("select col1,col2  from table1 order by col1 asc, col2 desc ");
    SQLOrderBy orderBy = query.getOrderBy();
    int i = 0;
    for (SQLSelectOrderByItem p : orderBy.getItems()) {
        i++;
        String orderCol = "col" + i;
        SQLExpr expr = p.getExpr();
        MySQLItemVisitor v = new MySQLItemVisitor(this.currentDb, utf8Charset, null, null);
        expr.accept(v);
        Item item = v.getItem();
        Assert.assertEquals(true, orderCol.equals(item.getItemName()));
    }
}
 
Example #5
Source File: ElasticSqlExprParser.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
protected SQLAggregateExpr parseAggregateExprRest(SQLAggregateExpr aggregateExpr) {
    if (lexer.token() == Token.ORDER) {
        SQLOrderBy orderBy = this.parseOrderBy();
        aggregateExpr.putAttribute("ORDER BY", orderBy);
    }
    if (lexer.identifierEquals(FnvHash.Constants.SEPARATOR)) {
        lexer.nextToken();

        SQLExpr seperator = this.primary();
        seperator.setParent(aggregateExpr);

        aggregateExpr.putAttribute("SEPARATOR", seperator);
    }
    return aggregateExpr;
}
 
Example #6
Source File: SqlParser.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
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 #7
Source File: MySQLSelectASTVisitor.java    From Zebra with Apache License 2.0 4 votes vote down vote up
@Override
public boolean visit(SQLOrderBy x) {
	result.getMergeContext().increOrderByCount();
	result.getMergeContext().setOrderBy(x);
	return true;
}
 
Example #8
Source File: MergeContext.java    From Zebra with Apache License 2.0 4 votes vote down vote up
public SQLOrderBy getOrderBy() {
	return orderBy;
}
 
Example #9
Source File: MergeContext.java    From Zebra with Apache License 2.0 4 votes vote down vote up
public void setOrderBy(SQLOrderBy orderBy) {
	this.orderBy = orderBy;
}
 
Example #10
Source File: OrderByDataMerger.java    From Zebra with Apache License 2.0 4 votes vote down vote up
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);
	}
}