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

The following examples show how to use com.alibaba.druid.sql.ast.SQLOrderBy. These examples are extracted from open source projects. 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 Project: dble   Source File: ItemFuncGroupConcat.java    License: 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 Project: elasticsearch-sql   Source File: SqlParser.java    License: 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 Project: Mycat2   Source File: JoinParser.java    License: 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 Project: dble   Source File: TestMySQLItemVisitor.java    License: 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 Project: elasticsearch-sql   Source File: ElasticSqlExprParser.java    License: 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 Project: elasticsearch-sql   Source File: SqlParser.java    License: 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 Project: Zebra   Source File: MySQLSelectASTVisitor.java    License: 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 Project: Zebra   Source File: MergeContext.java    License: Apache License 2.0 4 votes vote down vote up
public SQLOrderBy getOrderBy() {
	return orderBy;
}
 
Example 9
Source Project: Zebra   Source File: MergeContext.java    License: Apache License 2.0 4 votes vote down vote up
public void setOrderBy(SQLOrderBy orderBy) {
	this.orderBy = orderBy;
}
 
Example 10
Source Project: Zebra   Source File: OrderByDataMerger.java    License: 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);
	}
}