use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock 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.statement.SQLSelectQueryBlock in project druid by alibaba.
the class NameResolveVisitor method visit.
@Override
public boolean visit(SQLPropertyExpr x) {
String ownerName = x.getOwnernName();
if (ownerName == null) {
return super.visit(x);
}
for (SQLObject parent = x.getParent(); parent != null; parent = parent.getParent()) {
if (parent instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) parent;
SQLTableSource tableSource = queryBlock.findTableSource(ownerName);
if (tableSource == null) {
continue;
}
String alias = tableSource.computeAlias();
if (ownerName.equalsIgnoreCase(alias) && !ownerName.equals(alias)) {
x.setOwner(alias);
}
break;
}
}
return super.visit(x);
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock in project druid by alibaba.
the class ParameterizedOutputVisitorUtils method parameterizeHash.
public static long parameterizeHash(String sql, DbType dbType, SQLSelectListCache selectListCache, List<Object> outParameters, VisitorFeature... visitorFeatures) {
final SQLParserFeature[] features = outParameters == null ? defaultFeatures2 : defaultFeatures;
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType, features);
if (selectListCache != null) {
parser.setSelectListCache(selectListCache);
}
List<SQLStatement> statementList = parser.parseStatementList();
final int stmtSize = statementList.size();
if (stmtSize == 0) {
return 0L;
}
StringBuilder out = new StringBuilder(sql.length());
ParameterizedVisitor visitor = createParameterizedOutputVisitor(out, dbType);
if (outParameters != null) {
visitor.setOutputParameters(outParameters);
}
configVisitorFeatures(visitor, visitorFeatures);
if (stmtSize == 1) {
SQLStatement stmt = statementList.get(0);
if (stmt.getClass() == SQLSelectStatement.class) {
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
if (selectListCache != null) {
SQLSelectQueryBlock queryBlock = selectStmt.getSelect().getQueryBlock();
if (queryBlock != null) {
String cachedSelectList = queryBlock.getCachedSelectList();
long cachedSelectListHash = queryBlock.getCachedSelectListHash();
if (cachedSelectList != null) {
visitor.config(VisitorFeature.OutputSkipSelectListCacheString, true);
}
visitor.visit(selectStmt);
return FnvHash.fnv1a_64_lower(cachedSelectListHash, out);
}
}
visitor.visit(selectStmt);
} else if (stmt.getClass() == MySqlInsertStatement.class) {
MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
String columnsString = insertStmt.getColumnsString();
if (columnsString != null) {
long columnsStringHash = insertStmt.getColumnsStringHash();
visitor.config(VisitorFeature.OutputSkipInsertColumnsString, true);
((MySqlASTVisitor) visitor).visit(insertStmt);
return FnvHash.fnv1a_64_lower(columnsStringHash, out);
}
} else {
stmt.accept(visitor);
}
return FnvHash.fnv1a_64_lower(out);
}
for (int i = 0; i < statementList.size(); i++) {
if (i > 0) {
out.append(";\n");
}
SQLStatement stmt = statementList.get(i);
if (stmt.hasBeforeComment()) {
stmt.getBeforeCommentsDirect().clear();
}
Class<?> stmtClass = stmt.getClass();
if (stmtClass == SQLSelectStatement.class) {
// only for performance
SQLSelectStatement selectStatement = (SQLSelectStatement) stmt;
visitor.visit(selectStatement);
visitor.postVisit(selectStatement);
} else {
stmt.accept(visitor);
}
}
return FnvHash.fnv1a_64_lower(out);
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock 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;
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock in project druid by alibaba.
the class Issue1865 method test_for_select_group.
public void test_for_select_group() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "select * from t where id = 2 and name = 'wenshao'";
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType, SQLParserFeature.EnableSQLBinaryOpExprGroup);
SQLSelectStatement stmt = (SQLSelectStatement) parser.parseStatement();
SQLSelectQueryBlock queryBlock = stmt.getSelect().getQueryBlock();
assertTrue(queryBlock.removeCondition("name = 'wenshao'"));
assertEquals("SELECT *\n" + "FROM t\n" + "WHERE id = 2", stmt.toString());
assertTrue(queryBlock.removeCondition("id = 2"));
assertEquals("SELECT *\n" + "FROM t", stmt.toString());
}
Aggregations