use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project druid by alibaba.
the class SQLQueryExpr method computeDataType.
public SQLDataType computeDataType() {
if (subQuery == null) {
return null;
}
SQLSelectQueryBlock queryBlock = subQuery.getFirstQueryBlock();
if (queryBlock == null) {
return null;
}
List<SQLSelectItem> selectList = queryBlock.getSelectList();
if (selectList.size() == 1) {
return selectList.get(0).computeDataType();
}
return null;
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project druid by alibaba.
the class SQLEvalVisitorUtils method visit.
public static boolean visit(SQLEvalVisitor visitor, SQLQueryExpr x) {
if (WallVisitorUtils.isSimpleCountTableSource(null, ((SQLQueryExpr) x).getSubQuery())) {
x.putAttribute(EVAL_VALUE, 1);
return false;
}
if (x.getSubQuery().getQuery() instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) x.getSubQuery().getQuery();
boolean nullFrom = false;
if (queryBlock.getFrom() == null) {
nullFrom = true;
} else if (queryBlock.getFrom() instanceof SQLExprTableSource) {
SQLExpr expr = ((SQLExprTableSource) queryBlock.getFrom()).getExpr();
if (expr instanceof SQLIdentifierExpr) {
if ("dual".equalsIgnoreCase(((SQLIdentifierExpr) expr).getName())) {
nullFrom = true;
}
}
}
if (nullFrom) {
List<Object> row = new ArrayList<Object>(queryBlock.getSelectList().size());
for (int i = 0; i < queryBlock.getSelectList().size(); ++i) {
SQLSelectItem item = queryBlock.getSelectList().get(i);
item.getExpr().accept(visitor);
Object cell = item.getExpr().getAttribute(EVAL_VALUE);
row.add(cell);
}
List<List<Object>> rows = new ArrayList<List<Object>>(1);
rows.add(row);
Object result = rows;
queryBlock.putAttribute(EVAL_VALUE, result);
x.getSubQuery().putAttribute(EVAL_VALUE, result);
x.putAttribute(EVAL_VALUE, result);
return false;
}
}
return false;
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectItem 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.statement.SQLSelectItem in project druid by alibaba.
the class PagerUtils method limitOracle.
private static String limitOracle(SQLSelect select, String dbType, int offset, int count) {
SQLSelectQuery query = select.getQuery();
if (query instanceof SQLSelectQueryBlock) {
OracleSelectQueryBlock queryBlock = (OracleSelectQueryBlock) query;
if (queryBlock.getGroupBy() == null && select.getOrderBy() == null && offset <= 0) {
SQLExpr condition = new //
SQLBinaryOpExpr(//
new SQLIdentifierExpr("ROWNUM"), //
SQLBinaryOperator.LessThanOrEqual, //
new SQLNumberExpr(count), JdbcConstants.ORACLE);
if (queryBlock.getWhere() == null) {
queryBlock.setWhere(condition);
} else {
queryBlock.setWhere(new //
SQLBinaryOpExpr(//
queryBlock.getWhere(), //
SQLBinaryOperator.BooleanAnd, //
condition, JdbcConstants.ORACLE));
}
return SQLUtils.toSQLString(select, dbType);
}
}
OracleSelectQueryBlock countQueryBlock = new OracleSelectQueryBlock();
countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("XX"), "*")));
countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLIdentifierExpr("ROWNUM"), "RN"));
countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
countQueryBlock.setWhere(new //
SQLBinaryOpExpr(//
new SQLIdentifierExpr("ROWNUM"), //
SQLBinaryOperator.LessThanOrEqual, //
new SQLNumberExpr(count + offset), JdbcConstants.ORACLE));
if (offset <= 0) {
return SQLUtils.toSQLString(countQueryBlock, dbType);
}
OracleSelectQueryBlock offsetQueryBlock = new OracleSelectQueryBlock();
offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX"));
offsetQueryBlock.setWhere(new //
SQLBinaryOpExpr(//
new SQLIdentifierExpr("RN"), //
SQLBinaryOperator.GreaterThan, //
new SQLNumberExpr(offset), JdbcConstants.ORACLE));
return SQLUtils.toSQLString(offsetQueryBlock, dbType);
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project druid by alibaba.
the class PagerUtils method createCountUseSubQuery.
private static String createCountUseSubQuery(SQLSelect select, String dbType) {
SQLSelectQueryBlock countSelectQuery = createQueryBlock(dbType);
SQLSelectItem countItem = createCountItem(dbType);
countSelectQuery.getSelectList().add(countItem);
SQLSubqueryTableSource fromSubquery = new SQLSubqueryTableSource(select);
fromSubquery.setAlias("ALIAS_COUNT");
countSelectQuery.setFrom(fromSubquery);
SQLSelect countSelect = new SQLSelect(countSelectQuery);
SQLSelectStatement countStmt = new SQLSelectStatement(countSelect);
return SQLUtils.toSQLString(countStmt, dbType);
}
Aggregations