mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved odps support
This commit is contained in:
@@ -337,14 +337,15 @@ public class SQLUtils {
|
||||
|
||||
return new SQLASTOutputVisitor(out);
|
||||
}
|
||||
|
||||
|
||||
@Deprecated
|
||||
public static SchemaStatVisitor createSchemaStatVisitor(List<SQLStatement> statementList, String dbType) {
|
||||
return createSchemaStatVisitor(dbType);
|
||||
}
|
||||
|
||||
public static SchemaStatVisitor createSchemaStatVisitor(String dbType) {
|
||||
if (JdbcUtils.ORACLE.equals(dbType) || JdbcUtils.ALI_ORACLE.equals(dbType)) {
|
||||
if (statementList.size() == 1) {
|
||||
return new OracleSchemaStatVisitor();
|
||||
} else {
|
||||
return new OracleSchemaStatVisitor();
|
||||
}
|
||||
return new OracleSchemaStatVisitor();
|
||||
}
|
||||
|
||||
if (JdbcUtils.MYSQL.equals(dbType) || //
|
||||
|
||||
@@ -153,6 +153,8 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
}
|
||||
|
||||
if (x.getSelect() != null) {
|
||||
println();
|
||||
print("AS");
|
||||
println();
|
||||
x.getSelect().accept(this);
|
||||
}
|
||||
|
||||
@@ -216,44 +216,91 @@ public class SQLStatementParser extends SQLParser {
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.DROP) {
|
||||
List<String> beforeComments = null;
|
||||
if (lexer.isKeepComments() && lexer.hasComment()) {
|
||||
beforeComments = lexer.readAndResetComments();
|
||||
}
|
||||
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token() == Token.TABLE || identifierEquals("TEMPORARY")) {
|
||||
|
||||
SQLDropTableStatement stmt = parseDropTable(false);
|
||||
|
||||
if (beforeComments != null) {
|
||||
stmt.addBeforeComment(beforeComments);
|
||||
}
|
||||
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
} else if (lexer.token() == Token.USER) {
|
||||
SQLStatement stmt = parseDropUser();
|
||||
if (beforeComments != null) {
|
||||
stmt.addBeforeComment(beforeComments);
|
||||
}
|
||||
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
} else if (lexer.token() == Token.INDEX) {
|
||||
SQLStatement stmt = parseDropIndex();
|
||||
if (beforeComments != null) {
|
||||
stmt.addBeforeComment(beforeComments);
|
||||
}
|
||||
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
} else if (lexer.token() == Token.VIEW) {
|
||||
SQLStatement stmt = parseDropView(false);
|
||||
|
||||
if (beforeComments != null) {
|
||||
stmt.addBeforeComment(beforeComments);
|
||||
}
|
||||
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
} else if (lexer.token() == Token.TRIGGER) {
|
||||
SQLStatement stmt = parseDropTrigger(false);
|
||||
|
||||
if (beforeComments != null) {
|
||||
stmt.addBeforeComment(beforeComments);
|
||||
}
|
||||
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
} else if (lexer.token() == Token.DATABASE) {
|
||||
SQLStatement stmt = parseDropDatabase(false);
|
||||
|
||||
if (beforeComments != null) {
|
||||
stmt.addBeforeComment(beforeComments);
|
||||
}
|
||||
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
} else if (lexer.token() == Token.FUNCTION) {
|
||||
SQLStatement stmt = parseDropFunction(false);
|
||||
|
||||
if (beforeComments != null) {
|
||||
stmt.addBeforeComment(beforeComments);
|
||||
}
|
||||
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
} else if (lexer.token() == Token.TABLESPACE) {
|
||||
SQLStatement stmt = parseDropTablespace(false);
|
||||
|
||||
if (beforeComments != null) {
|
||||
stmt.addBeforeComment(beforeComments);
|
||||
}
|
||||
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
} else if (lexer.token() == Token.PROCEDURE) {
|
||||
SQLStatement stmt = parseDropProcedure(false);
|
||||
|
||||
if (beforeComments != null) {
|
||||
stmt.addBeforeComment(beforeComments);
|
||||
}
|
||||
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
} else {
|
||||
@@ -1255,12 +1302,16 @@ public class SQLStatementParser extends SQLParser {
|
||||
}
|
||||
|
||||
protected SQLDropTableSpaceStatement parseDropTablespace(boolean acceptDrop) {
|
||||
SQLDropTableSpaceStatement stmt = new SQLDropTableSpaceStatement(getDbType());
|
||||
|
||||
if (lexer.isKeepComments() && lexer.hasComment()) {
|
||||
stmt.addBeforeComment(lexer.readAndResetComments());
|
||||
}
|
||||
|
||||
if (acceptDrop) {
|
||||
accept(Token.DROP);
|
||||
}
|
||||
|
||||
SQLDropTableSpaceStatement stmt = new SQLDropTableSpaceStatement(getDbType());
|
||||
|
||||
|
||||
accept(Token.TABLESPACE);
|
||||
|
||||
if (lexer.token() == Token.IF) {
|
||||
|
||||
@@ -317,9 +317,9 @@ public final class DruidStatService implements DruidStatServiceMBean {
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
|
||||
|
||||
if (!statementList.isEmpty()) {
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(statementList, dbType);
|
||||
statemen.accept(visitor);
|
||||
SQLStatement sqlStmt = statementList.get(0);
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
|
||||
sqlStmt.accept(visitor);
|
||||
map.put("parsedTable", visitor.getTables().toString());
|
||||
map.put("parsedFields", visitor.getColumns().toString());
|
||||
map.put("parsedConditions", visitor.getConditions().toString());
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.alibaba.druid.support.opds.udf;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.aliyun.odps.udf.UDF;
|
||||
|
||||
public class ExportColumns extends UDF {
|
||||
public String evaluate(String sql) {
|
||||
return evaluate(sql, null);
|
||||
}
|
||||
|
||||
public String evaluate(String sql, String dbType) {
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
|
||||
|
||||
for (SQLStatement stmt : statementList) {
|
||||
stmt.accept(visitor);
|
||||
}
|
||||
|
||||
StringBuffer buf = new StringBuffer();
|
||||
|
||||
for (TableStat.Column column : visitor.getColumns()) {
|
||||
if (buf.length() != 0) {
|
||||
buf.append(',');
|
||||
}
|
||||
buf.append(column.toString());
|
||||
}
|
||||
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.alibaba.druid.support.opds.udf;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.aliyun.odps.udf.UDF;
|
||||
|
||||
public class ExportTables extends UDF {
|
||||
public String evaluate(String sql) {
|
||||
return evaluate(sql, null);
|
||||
}
|
||||
|
||||
public String evaluate(String sql, String dbType) {
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
|
||||
|
||||
for (SQLStatement stmt : statementList) {
|
||||
stmt.accept(visitor);
|
||||
}
|
||||
|
||||
StringBuffer buf = new StringBuffer();
|
||||
|
||||
for (Map.Entry<TableStat.Name, TableStat> entry : visitor.getTables().entrySet()) {
|
||||
TableStat.Name name = entry.getKey();
|
||||
|
||||
if (buf.length() != 0) {
|
||||
buf.append(',');
|
||||
}
|
||||
buf.append(name.toString());
|
||||
}
|
||||
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user