那些开源的都不太靠谱,同事推荐这个druid解析sql。试了一下确实给力。
打包方法:
git clone https://github.com/alibaba/druid.git
mvn install -Dmaven.javadoc.skip=true -Dmaven.test.skip=true
会安装到.m2目录,copy一个项目用
target/druid-1.0.27-SNAPSHOT-sources.jar
示例代码:
import java.util.*;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import py4j.GatewayServer;
public class SQLParser {
public static List get_tables(String sql){
MySqlStatementParser parser = new MySqlStatementParser(sql);
List statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
statemen.accept(visitor);
Map result = visitor.getTables();
System.out.println("Tables : " + result);
List list = new ArrayList<>();
for ( TableStat.Name name : result.keySet() ) {
System.out.println( name );
list.add(name.getName());
}
return list;
}
public static void main(String[] args){
GatewayServer gatewayServer = new GatewayServer(new SQLParser());
gatewayServer.start();
System.out.println("Gateway Server Started");
}
}
打包命令:
javac -cp .:./py4j0.10.4.jar:./druid-1.0.27-SNAPSHOT.jar:$CLASSPATH SQLParser.java
java -cp .:./py4j0.10.4.jar:./druid-1.0.27-SNAPSHOT.jar:$CLASSPATH SQLParser
上一篇: mysql语法解析之使用jsqlparser
下一篇: mac os安装f#
0 Responses so far.