use of com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr in project druid by alibaba.
the class SQLSelect method addWhere.
public boolean addWhere(SQLExpr where) {
if (where == null) {
return false;
}
if (query instanceof SQLSelectQueryBlock) {
((SQLSelectQueryBlock) query).addWhere(where);
return true;
}
if (query instanceof SQLUnionQuery) {
SQLSelectQueryBlock queryBlock = new SQLSelectQueryBlock(getDbType());
queryBlock.setFrom(new SQLSelect(query), "u");
queryBlock.addSelectItem(new SQLAllColumnExpr());
queryBlock.setParent(queryBlock);
query = queryBlock;
return true;
}
return false;
}
use of com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr in project druid by alibaba.
the class OracleRowNumToLimit method visit.
@Override
public boolean visit(OracleSelectQueryBlock x) {
context = new Context(context);
context.queryBlock = x;
SQLExpr where = x.getWhere();
if (where != null) {
where.accept(this);
}
SQLTableSource from = x.getFrom();
if (from != null) {
from.accept(this);
}
removeSelectListRowNum(x);
List<SQLSelectItem> selectList = x.getSelectList();
for (SQLSelectItem selectItem : selectList) {
selectItem.accept(this);
}
SQLExpr startWith = x.getStartWith();
if (startWith != null) {
startWith.accept(this);
}
boolean allColumn = false;
if (selectList.size() == 1) {
SQLExpr expr = selectList.get(0).getExpr();
if (expr instanceof SQLAllColumnExpr) {
allColumn = true;
} else if (expr instanceof SQLPropertyExpr && ((SQLPropertyExpr) expr).getName().equals("*")) {
allColumn = true;
}
}
if ((!allColumn) && x.getFrom() instanceof SQLSubqueryTableSource && ((SQLSubqueryTableSource) x.getFrom()).getSelect().getQuery() instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock subQuery = ((SQLSubqueryTableSource) x.getFrom()).getSelect().getQueryBlock();
List<SQLSelectItem> subSelectList = subQuery.getSelectList();
if (subSelectList.size() >= selectList.size()) {
boolean match = true;
for (int i = 0; i < selectList.size(); i++) {
if (!selectList.get(i).equals(subSelectList.get(i))) {
match = false;
break;
}
}
if (match) {
allColumn = true;
}
}
}
if (x.getParent() instanceof SQLSelect && x.getWhere() == null && x.getOrderBy() == null && allColumn && x.getLimit() != null && x.getFrom() instanceof SQLSubqueryTableSource && ((SQLSubqueryTableSource) x.getFrom()).getSelect().getQuery() instanceof SQLSelectQueryBlock) {
SQLSelect select = (SQLSelect) x.getParent();
SQLSelectQueryBlock subQuery = ((SQLSubqueryTableSource) x.getFrom()).getSelect().getQueryBlock();
subQuery.mergeLimit(x.getLimit());
x.setLimit(null);
select.setQuery(subQuery);
context.queryBlock = subQuery;
context.fixLimit();
subQuery.accept(this);
}
if (x.getParent() instanceof SQLUnionQuery && x.getWhere() == null && x.getOrderBy() == null && allColumn && x.getLimit() != null && x.getFrom() instanceof SQLSubqueryTableSource && ((SQLSubqueryTableSource) x.getFrom()).getSelect().getQuery() instanceof SQLSelectQueryBlock) {
SQLUnionQuery union = (SQLUnionQuery) x.getParent();
SQLSelectQueryBlock subQuery = ((SQLSubqueryTableSource) x.getFrom()).getSelect().getQueryBlock();
subQuery.mergeLimit(x.getLimit());
x.setLimit(null);
if (union.getLeft() == x) {
union.setLeft(subQuery);
} else {
union.setRight(subQuery);
}
context.queryBlock = subQuery;
context.fixLimit();
subQuery.accept(this);
}
context = context.parent;
return false;
}
use of com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr in project druid by alibaba.
the class Schema method findTable.
public SchemaObject findTable(SQLTableSource tableSource, SQLExpr expr) {
if (expr instanceof SQLAggregateExpr) {
SQLAggregateExpr aggregateExpr = (SQLAggregateExpr) expr;
String function = aggregateExpr.getMethodName();
if ("min".equalsIgnoreCase(function) || "max".equalsIgnoreCase(function)) {
SQLExpr arg = aggregateExpr.getArguments().get(0);
return findTable(tableSource, arg);
}
}
if (expr instanceof SQLPropertyExpr) {
String ownerName = ((SQLPropertyExpr) expr).getOwnernName();
return findTable(tableSource, ownerName);
}
if (expr instanceof SQLAllColumnExpr || expr instanceof SQLIdentifierExpr) {
if (tableSource instanceof SQLExprTableSource) {
return findTable(tableSource, tableSource.computeAlias());
}
if (tableSource instanceof SQLJoinTableSource) {
SQLJoinTableSource join = (SQLJoinTableSource) tableSource;
SchemaObject table = findTable(join.getLeft(), expr);
if (table == null) {
table = findTable(join.getRight(), expr);
}
return table;
}
return null;
}
return null;
}
use of com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr in project druid by alibaba.
the class TPCDS_ALL_Resolve method test_q01.
public void test_q01() throws Exception {
for (int q = 1; q <= 99; ++q) {
System.out.println("tpcds query-" + q);
System.out.println("-----------------------------------------------------");
String sql = TPCDS.getQuery(q);
final List<SQLStatement> statements = SQLUtils.parseStatements(sql, DbType.mysql);
for (SQLStatement stmt : statements) {
repository.resolve(stmt);
final SQLSelect select = ((SQLSelectStatement) stmt).getSelect();
final SQLSelectQueryBlock firstQueryBlock = select.getFirstQueryBlock();
if (firstQueryBlock == null) {
continue;
}
final List<SQLSelectItem> selectList = firstQueryBlock.getSelectList();
for (int i = 0; i < selectList.size(); i++) {
SQLSelectItem selectItem = selectList.get(i);
if (selectItem.getExpr() instanceof SQLAllColumnExpr) {
continue;
}
final SQLDataType dataType = selectItem.computeDataType();
if (dataType == null) {
// fail("dataType is null : " + selectItem);
}
}
}
}
}
use of com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr in project Mycat-Server by MyCATApache.
the class JoinParser method parserFields.
private void parserFields(List<SQLSelectItem> mysqlSelectList) {
// 显示的字段
String key = "";
String value = "";
String exprfield = "";
for (SQLSelectItem item : mysqlSelectList) {
if (item.getExpr() instanceof SQLAllColumnExpr) {
// *解析
setField(item.toString(), item.toString());
} else {
if (item.getExpr() instanceof SQLAggregateExpr) {
SQLAggregateExpr expr = (SQLAggregateExpr) item.getExpr();
key = getExprFieldName(expr);
setField(key, value);
} else if (item.getExpr() instanceof SQLMethodInvokeExpr) {
key = getMethodInvokeFieldName(item);
exprfield = getFieldName(item);
// value=item.getAlias();
setField(key, value, exprfield);
} else {
key = getFieldName(item);
value = item.getAlias();
setField(key, value);
}
}
}
}
Aggregations