use of com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr in project druid by alibaba.
the class SchemaStatVisitor method handleSubQueryColumn.
protected Column handleSubQueryColumn(String alias, SQLObject query) {
if (query instanceof SQLSelect) {
query = ((SQLSelect) query).getQuery();
}
SQLSelectQueryBlock queryBlock = null;
List<SQLSelectItem> selectList = null;
if (query instanceof SQLSelectQueryBlock) {
queryBlock = (SQLSelectQueryBlock) query;
if (queryBlock.getGroupBy() != null) {
return null;
}
selectList = queryBlock.getSelectList();
}
if (selectList != null) {
boolean allColumn = false;
String allColumnOwner = null;
for (SQLSelectItem item : selectList) {
if (!item.getClass().equals(SQLSelectItem.class)) {
continue;
}
String itemAlias = item.getAlias();
SQLExpr itemExpr = item.getExpr();
String ident = itemAlias;
if (itemAlias == null) {
if (itemExpr instanceof SQLIdentifierExpr) {
ident = itemAlias = itemExpr.toString();
} else if (itemExpr instanceof SQLPropertyExpr) {
itemAlias = ((SQLPropertyExpr) itemExpr).getName();
ident = itemExpr.toString();
}
}
if (alias.equalsIgnoreCase(itemAlias)) {
Column column = (Column) itemExpr.getAttribute(ATTR_COLUMN);
if (column != null) {
return column;
} else {
SQLTableSource from = queryBlock.getFrom();
if (from instanceof SQLSubqueryTableSource) {
SQLSelect select = ((SQLSubqueryTableSource) from).getSelect();
Column subQueryColumn = handleSubQueryColumn(ident, select);
return subQueryColumn;
}
}
}
if (itemExpr instanceof SQLAllColumnExpr) {
allColumn = true;
} else if (itemExpr instanceof SQLPropertyExpr) {
SQLPropertyExpr propertyExpr = (SQLPropertyExpr) itemExpr;
if (propertyExpr.getName().equals("*")) {
SQLExpr owner = propertyExpr.getOwner();
if (owner instanceof SQLIdentifierExpr) {
allColumnOwner = ((SQLIdentifierExpr) owner).getName();
allColumn = true;
}
}
}
}
if (allColumn) {
SQLTableSource from = queryBlock.getFrom();
String tableName = getTable(from, allColumnOwner);
if (tableName != null) {
return new Column(tableName, alias);
}
}
}
return null;
}
use of com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr in project druid by alibaba.
the class WallVisitorUtils method check.
public static void check(WallVisitor visitor, SQLSelectItem x) {
SQLExpr expr = x.getExpr();
if (expr instanceof SQLVariantRefExpr) {
if (!isTopSelectItem(expr) && "@".equals(((SQLVariantRefExpr) expr).getName())) {
addViolation(visitor, ErrorCode.EVIL_NAME, "@ not allow", x);
}
}
if (visitor.getConfig().isSelectAllColumnAllow()) {
return;
}
if (//
expr instanceof SQLAllColumnExpr && x.getParent() instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) x.getParent();
SQLTableSource from = queryBlock.getFrom();
if (from instanceof SQLExprTableSource) {
addViolation(visitor, ErrorCode.SELECT_NOT_ALLOW, "'SELECT *' not allow", x);
}
}
}
use of com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr in project druid by alibaba.
the class PagerUtils method createCountItem.
private static SQLSelectItem createCountItem(String dbType) {
SQLAggregateExpr countExpr = new SQLAggregateExpr("COUNT");
countExpr.addArgument(new SQLAllColumnExpr());
SQLSelectItem countItem = new SQLSelectItem(countExpr);
return countItem;
}
use of com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr in project druid by alibaba.
the class PagerUtils method limitDB2.
private static String limitDB2(SQLSelect select, String dbType, int offset, int count) {
SQLSelectQuery query = select.getQuery();
SQLBinaryOpExpr gt = new //
SQLBinaryOpExpr(//
new SQLIdentifierExpr("ROWNUM"), //
SQLBinaryOperator.GreaterThan, //
new SQLNumberExpr(offset), JdbcConstants.DB2);
SQLBinaryOpExpr lteq = new //
SQLBinaryOpExpr(//
new SQLIdentifierExpr("ROWNUM"), //
SQLBinaryOperator.LessThanOrEqual, //
new SQLNumberExpr(count + offset), JdbcConstants.DB2);
SQLBinaryOpExpr pageCondition = new SQLBinaryOpExpr(gt, SQLBinaryOperator.BooleanAnd, lteq, JdbcConstants.DB2);
if (query instanceof SQLSelectQueryBlock) {
DB2SelectQueryBlock queryBlock = (DB2SelectQueryBlock) query;
if (offset <= 0) {
queryBlock.setFirst(new SQLNumberExpr(count));
return SQLUtils.toSQLString(select, dbType);
}
SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
SQLOrderBy orderBy = select.getOrderBy();
if (orderBy == null && select.getQuery() instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock selectQueryBlcok = (SQLSelectQueryBlock) select.getQuery();
orderBy = selectQueryBlcok.getOrderBy();
selectQueryBlcok.setOrderBy(null);
} else {
select.setOrderBy(null);
}
aggregateExpr.setOver(new SQLOver(orderBy));
queryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
DB2SelectQueryBlock countQueryBlock = new DB2SelectQueryBlock();
countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
countQueryBlock.setWhere(pageCondition);
return SQLUtils.toSQLString(countQueryBlock, dbType);
}
DB2SelectQueryBlock countQueryBlock = new DB2SelectQueryBlock();
countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("XX"), "*")));
SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
SQLOrderBy orderBy = select.getOrderBy();
aggregateExpr.setOver(new SQLOver(orderBy));
select.setOrderBy(null);
countQueryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
if (offset <= 0) {
return SQLUtils.toSQLString(countQueryBlock, dbType);
}
DB2SelectQueryBlock offsetQueryBlock = new DB2SelectQueryBlock();
offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX"));
offsetQueryBlock.setWhere(pageCondition);
return SQLUtils.toSQLString(offsetQueryBlock, dbType);
}
use of com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr in project Mycat_plus by coderczp.
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