test4

import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.util.TablesNamesFinder;
import net.sf.jsqlparser.visitor.StatementVisitorAdapter;

import java.util.ArrayList;
import java.util.List;

public class StoredProcParserExample {
public static void main(String[] args) {
String storedProcDefinition = "CREATE PROCEDURE `get_products`()\n" +
"BEGIN\n" +
" SELECT * FROM products WHERE price > 100;\n" +
"END";

try {
// 解析存储过程定义
Statement stmt = CCJSqlParserUtil.parse(storedProcDefinition);

// 自定义Visitor来提取表名
TablesNamesVisitor visitor = new TablesNamesVisitor();
stmt.accept(visitor);
List<String> tableNames = visitor.getTableNames();

// 打印表名
for (String tableName : tableNames) {
System.out.println("Table: " + tableName);
}
} catch (Exception e) {
e.printStackTrace();
}
}

static class TablesNamesVisitor extends TablesNamesFinder {
private List<String> tableNames = new ArrayList<>();

@Override
public void visit(Table table) {
tableNames.add(table.getFullyQualifiedName());
super.visit(table);
}

public List<String> getTableNames() {
return tableNames;
}
}
}

posted @ 2023-06-21 11:08  消失的那两年  阅读(36)  评论(0)    收藏  举报