use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project druid by alibaba.
the class SQLServerExprParser method parserOutput.
protected SQLServerOutput parserOutput() {
if (lexer.identifierEquals("OUTPUT")) {
lexer.nextToken();
SQLServerOutput output = new SQLServerOutput();
final List<SQLSelectItem> selectList = output.getSelectList();
for (; ; ) {
final SQLSelectItem selectItem = parseSelectItem();
selectList.add(selectItem);
if (lexer.token() != Token.COMMA) {
break;
}
lexer.nextToken();
}
if (lexer.token() == Token.INTO) {
lexer.nextToken();
output.setInto(new SQLExprTableSource(this.name()));
if (lexer.token() == (Token.LPAREN)) {
lexer.nextToken();
this.exprList(output.getColumns(), output);
accept(Token.RPAREN);
}
}
return output;
}
return null;
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project druid by alibaba.
the class SQLServerOutput method clone.
public SQLServerOutput clone() {
SQLServerOutput x = new SQLServerOutput();
if (into != null) {
x.setInto(into.clone());
}
for (SQLExpr c : columns) {
SQLExpr c2 = c.clone();
c2.setParent(x);
x.columns.add(c2);
}
for (SQLSelectItem item : selectList) {
SQLSelectItem item2 = item.clone();
item2.setParent(x);
x.selectList.add(item2);
}
return x;
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project druid by alibaba.
the class SQLRefactorVisitor method visit.
public boolean visit(SQLPropertyExpr x) {
TableMapping mapping = null;
SchemaObject schemaObject = null;
boolean aliasOwer = false;
SQLObject ownerObject = x.getResolvedOwnerObject();
if (ownerObject instanceof SQLExprTableSource) {
SQLExprTableSource exprTableSource = (SQLExprTableSource) ownerObject;
if (exprTableSource.getAlias() != null && x.getOwner() instanceof SQLIdentifierExpr) {
if (FnvHash.hashCode64(exprTableSource.getAlias()) == ((SQLIdentifierExpr) x.getOwner()).nameHashCode64()) {
aliasOwer = true;
}
}
mapping = findMapping(exprTableSource);
schemaObject = (exprTableSource).getSchemaObject();
}
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);
}
if (x.getOwner() instanceof SQLIdentifierExpr && ((SQLIdentifierExpr) x.getOwner()).nameHashCode64() == mapping.getSrcTableHash() && !aliasOwer) {
x.setOwner(new SQLIdentifierExpr(quote(mapping.getDestTable())));
}
return false;
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectItem 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.SQLSelectItem in project druid by alibaba.
the class Issue1994 method test_for_issue.
public void test_for_issue() throws Exception {
String sql = "INSERT INTO MKTG_H_EXEC_RESULT_FACT\n" + "(THE_DATE, AREA_ID, SCENE_ID, MKTG_CNT, MKTG_SUC_CNT\n" + ", TASK_CNT, TASK_F_CNT, TASK_F_SUC_CNT, CON_CNT, CON_SUC_CNT)\n" + "SELECT TRUNC(SYSDATE), T1.AREA_ID\n" + ", RTRIM(TO_CHAR(T2.PID))\n" + ", SUM(T1.MKTG_CNT), SUM(T1.MKTG_SUC_CNT)\n" + ", SUM(T1.TASK_CNT), SUM(T1.TASK_F_CNT)\n" + ", SUM(T1.TASK_F_SUC_CNT), SUM(T1.CON_CNT)\n" + ", SUM(T1.CON_SUC_CNT)\n" + "FROM MKTG_H_EXEC_RESULT_FACT T1, (\n" + "SELECT DISTINCT MKTG_PLAN_LVL1_ID AS PID, MKTG_PLAN_LVL4_ID AS SCENE_ID\n" + "FROM DMN_MKTG_PLAN_TYPE\n" + "UNION ALL\n" + "SELECT DISTINCT MKTG_PLAN_LVL2_ID AS PID, MKTG_PLAN_LVL4_ID AS SCENE_ID\n" + "FROM DMN_MKTG_PLAN_TYPE_TWO\n" + "WHERE MKTG_PLAN_LVL2_ID <> MKTG_PLAN_LVL4_ID\n" + "UNION ALL\n" + "SELECT DISTINCT MKTG_PLAN_LVL3_ID AS PID, MKTG_PLAN_LVL4_ID AS SCENE_ID\n" + "FROM DMN_MKTG_PLAN_TYPE\n" + "WHERE MKTG_PLAN_LVL3_ID <> MKTG_PLAN_LVL4_ID\n" + ") T2\n" + "WHERE T1.THE_DATE = TRUNC(SYSDATE)\n" + "AND T1.SCENE_ID = T2.SCENE_ID\n" + "GROUP BY T1.AREA_ID, RTRIM(TO_CHAR(T2.PID))";
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
assertEquals(1, stmtList.size());
SQLStatement stmt = stmtList.get(0);
System.out.println(stmt);
SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ORACLE);
stmt.accept(statVisitor);
System.out.println("columns : " + statVisitor.getColumns());
assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "THE_DATE"));
assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "AREA_ID"));
assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "SCENE_ID"));
assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "MKTG_CNT"));
assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "MKTG_SUC_CNT"));
assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "TASK_CNT"));
assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "TASK_F_CNT"));
assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "TASK_F_SUC_CNT"));
assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "CON_CNT"));
assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "CON_SUC_CNT"));
assertTrue(statVisitor.containsColumn("DMN_MKTG_PLAN_TYPE", "MKTG_PLAN_LVL1_ID"));
assertTrue(statVisitor.containsColumn("DMN_MKTG_PLAN_TYPE", "MKTG_PLAN_LVL4_ID"));
assertTrue(statVisitor.containsColumn("DMN_MKTG_PLAN_TYPE_TWO", "MKTG_PLAN_LVL2_ID"));
assertTrue(statVisitor.containsColumn("DMN_MKTG_PLAN_TYPE_TWO", "MKTG_PLAN_LVL4_ID"));
assertTrue(statVisitor.containsColumn("DMN_MKTG_PLAN_TYPE", "MKTG_PLAN_LVL3_ID"));
OracleInsertStatement insertStmt = (OracleInsertStatement) stmt;
OracleSelectQueryBlock queryBlock = (OracleSelectQueryBlock) insertStmt.getQuery().getQueryBlock();
SQLSelectItem selectItem = queryBlock.getSelectList().get(0);
assertEquals("TRUNC(SYSDATE)", selectItem.toString());
}
Aggregations