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基本使用

相关推荐
蓝黑2020几秒前
IntelliJ IDEA常用快捷键
java·ide·intellij-idea
Ysjt | 深2 分钟前
C++多线程编程入门教程(优质版)
java·开发语言·jvm·c++
shuangrenlong14 分钟前
slice介绍slice查看器
java·ubuntu
牧竹子14 分钟前
对原jar包解压后修改原class文件后重新打包为jar
java·jar
难以触及的高度24 分钟前
mysql中between and怎么用
数据库·mysql
数据小爬虫@25 分钟前
如何利用java爬虫获得淘宝商品评论
java·开发语言·爬虫
喜欢猪猪27 分钟前
面试题---深入源码理解MQ长轮询优化机制
java
Jacky(易小天)38 分钟前
MongoDB比较查询操作符中英对照表及实例详解
数据库·mongodb·typescript·比较操作符
草莓base1 小时前
【手写一个spring】spring源码的简单实现--bean对象的创建
java·spring·rpc
Karoku0661 小时前
【企业级分布式系统】ELK优化
运维·服务器·数据库·elk·elasticsearch