use of com.alibaba.druid.sql.ast.SQLOrderBy in project druid by alibaba.
the class SQLSelectBuilderImpl method orderBy.
@Override
public SQLSelectBuilderImpl orderBy(String... columns) {
SQLSelect select = this.getSQLSelect();
SQLOrderBy orderBy = select.getOrderBy();
if (orderBy == null) {
orderBy = createOrderBy();
select.setOrderBy(orderBy);
}
for (String column : columns) {
SQLSelectOrderByItem orderByItem = SQLUtils.toOrderByItem(column, dbType);
orderBy.addItem(orderByItem);
}
return this;
}
use of com.alibaba.druid.sql.ast.SQLOrderBy in project druid by alibaba.
the class OrderByResolve method visit.
public boolean visit(SQLSelect x) {
SQLSelectQueryBlock queryBlock = x.getQueryBlock();
if (queryBlock == null) {
return super.visit(x);
}
if (x.getOrderBy() != null && queryBlock.isForUpdate() && queryBlock.getOrderBy() == null) {
queryBlock.setOrderBy(x.getOrderBy());
x.setOrderBy(null);
}
SQLOrderBy orderBy = queryBlock.getOrderBy();
if (orderBy == null) {
return super.visit(x);
}
if (!queryBlock.selectItemHasAllColumn(false)) {
List<SQLSelectOrderByItem> notContainsOrderBy = new ArrayList<SQLSelectOrderByItem>();
for (SQLSelectOrderByItem orderByItem : orderBy.getItems()) {
SQLExpr orderByExpr = orderByItem.getExpr();
if (orderByExpr instanceof SQLName) {
if (((SQLName) orderByExpr).hashCode64() == DBMS_RANDOM_VALUE) {
continue;
}
long hashCode64 = ((SQLName) orderByExpr).nameHashCode64();
SQLSelectItem selectItem = queryBlock.findSelectItem(hashCode64);
if (selectItem == null) {
queryBlock.addSelectItem(orderByExpr.clone());
}
}
}
if (notContainsOrderBy.size() > 0) {
for (SQLSelectOrderByItem orderByItem : notContainsOrderBy) {
queryBlock.addSelectItem(orderByItem.getExpr());
}
OracleSelectQueryBlock queryBlock1 = new OracleSelectQueryBlock();
queryBlock1.setFrom(queryBlock, "x");
x.setQuery(queryBlock1);
}
}
return super.visit(x);
}
use of com.alibaba.druid.sql.ast.SQLOrderBy in project druid by alibaba.
the class SQLRefactorVisitor method visit.
public boolean visit(SQLIdentifierExpr x) {
TableMapping mapping = null;
if (groupByLevel > 0 || havingLevel > 0) {
SQLSelectQueryBlock queryBlock = null;
for (SQLObject parent = x.getParent(); parent != null; parent = parent.getParent()) {
if (parent instanceof SQLSelectQueryBlock) {
queryBlock = (SQLSelectQueryBlock) parent;
break;
}
}
boolean matchAlias = false;
if (queryBlock != null) {
for (SQLSelectItem item : queryBlock.getSelectList()) {
if (item.alias_hash() == x.hashCode64()) {
matchAlias = true;
break;
}
}
}
if (matchAlias) {
SQLObject parent = x.getParent();
if (parent instanceof SQLOrderBy || parent instanceof SQLSelectGroupByClause) {
return false;
}
if (havingLevel > 0) {
boolean agg = false;
for (; parent != null; parent = parent.getParent()) {
if (parent instanceof SQLSelectQueryBlock) {
break;
}
if (parent instanceof SQLAggregateExpr) {
agg = true;
break;
}
}
if (!agg) {
return false;
}
}
}
}
SQLObject ownerObject = x.getResolvedOwnerObject();
if (ownerObject instanceof SQLExprTableSource) {
mapping = findMapping((SQLExprTableSource) ownerObject);
}
if (mapping == null) {
return false;
}
String srcName = x.getName();
String mappingColumn = mapping.getMappingColumn(srcName);
if (mappingColumn != null) {
x.setName(quote(mappingColumn));
}
SQLObject parent = x.getParent();
if (parent instanceof SQLSelectItem && ((SQLSelectItem) parent).getAlias() == null) {
((SQLSelectItem) parent).setAlias(srcName);
}
return false;
}
Aggregations