JSqlParser 解析 sql

目录

前言

JSqlParser是一个 SQL 语句解析器。它将 SQL 语句转换为可遍历的 Java 类层次结构,可以方便的用代码对 SQL 语句进行解析,修改等操作。

官网 api 文档和 github 地址如下:

jsqlparser API 文档

jsqlparser github 地址

一、Maven依赖

xml 复制代码
        <!-- 4.9版本是适用于jdk8的最后一个版本-->
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>4.9</version>
        </dependency>

二、获取sql中的表名

java 复制代码
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.util.TablesNamesFinder;
import java.util.Set;

public class MainServer {
    public static void main(String[] args) throws JSQLParserException {
        String sql = "SELECT e.employee_id, e.first_name, e.last_name, d.department_name, e.salary " +
                "FROM employees e " +
                "LEFT JOIN departments d ON e.department_id = d.department_id " +
                "WHERE e.salary > ( " +
                "    SELECT AVG(salary) " +
                "    FROM staff " +
                "    WHERE department_id = e.department_id " +
                ");";
        Set<String> tableNames = TablesNamesFinder.findTables(sql);
		//输出: staff, departments, employees
        System.out.println(tableNames);
    }
}

三、获取sql中的具体信息

java 复制代码
import javassist.CannotCompileException;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;


public class MainServer {
    public static void main(String[] args) throws CannotCompileException, JSQLParserException {
	
        String sql = "SELECT DISTINCT e.employee_id, e.first_name, e.last_name, d.department_name, e.salary " +
                "FROM employees e " +
                "LEFT JOIN departments d ON e.department_id = d.department_id " +
                "WHERE e.salary > ( " +
                "    SELECT AVG(salary) " +
                "    FROM staff " +
                "    WHERE department_id = e.department_id " +
                ") " +
                "GROUP BY d.department_id, e.employee_id, e.first_name, e.last_name, d.department_name, e.salary " +
                "HAVING COUNT(*) = 1  " +
                "ORDER BY d.department_name " +
                "LIMIT 10 OFFSET 20;";
        Statement statement = CCJSqlParserUtil.parse(sql);

        //如果是 select 语句
        if (statement instanceof Select) {
            Select selectStatement = (Select) statement;
            PlainSelect plainSelect = selectStatement.getPlainSelect();
            System.out.println("【DISTINCT 子句】:" + plainSelect.getDistinct());
            System.out.println("【查询字段】:" + plainSelect.getSelectItems());
            System.out.println("【FROM 表】:" + plainSelect.getFromItem());
            System.out.println("【JOIN 子句】:" + plainSelect.getJoins());
            System.out.println("【WHERE 子句】:" + plainSelect.getWhere());
            System.out.println("【GROUP BY 子句】:" + plainSelect.getGroupBy());
            System.out.println("【HAVING 子句】:" +plainSelect.getHaving());
            System.out.println("【ORDER BY 子句】:" + plainSelect.getOrderByElements());
            System.out.println("【LIMIT 子句】:" + plainSelect.getLimit());
            System.out.println("【OFFSET 子句】:" + plainSelect.getOffset());
        }
    }
}

输出:

java 复制代码
【DISTINCT 子句】:DISTINCT
【查询字段】:[e.employee_id, e.first_name, e.last_name, d.department_name, e.salary]
【FROM 表】:employees e
【JOIN 子句】:[LEFT JOIN departments d ON e.department_id = d.department_id]
【WHERE 子句】:e.salary > (SELECT AVG(salary) FROM staff WHERE department_id = e.department_id)
【GROUP BY 子句】:GROUP BY d.department_id, e.employee_id, e.first_name, e.last_name, d.department_name, e.salary
【HAVING 子句】:COUNT(*) = 1
【ORDER BY 子句】:[d.department_name]
【LIMIT 子句】: LIMIT 10
【OFFSET 子句】: OFFSET 20

参考:
JSqlParser实战指南
JSQLParser 解析复杂SQL
JSqlParser入门系列
jsqlparser基本使用

相关推荐
为将者,自当识天晓地。9 分钟前
c++多线程
java·开发语言
daqinzl17 分钟前
java获取机器ip、mac
java·mac·ip
激流丶33 分钟前
【Kafka 实战】如何解决Kafka Topic数量过多带来的性能问题?
java·大数据·kafka·topic
Themberfue36 分钟前
Java多线程详解⑤(全程干货!!!)线程安全问题 || 锁 || synchronized
java·开发语言·线程·多线程·synchronized·
时差9531 小时前
【面试题】Hive 查询:如何查找用户连续三天登录的记录
大数据·数据库·hive·sql·面试·database
让学习成为一种生活方式1 小时前
R包下载太慢安装中止的解决策略-R语言003
java·数据库·r语言
晨曦_子画1 小时前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin
Mephisto.java1 小时前
【大数据学习 | kafka高级部分】kafka的优化参数整理
大数据·sql·oracle·kafka·json·database
秋意钟1 小时前
MySQL日期类型选择建议
数据库·mysql
南宫生1 小时前
贪心算法习题其三【力扣】【算法学习day.20】
java·数据结构·学习·算法·leetcode·贪心算法