jsqlparser 安装和使用

jsqlparser是sql语句解析工具,可以解析sql并分析语法。

安装

<dependency>
	<groupId>com.github.jsqlparser</groupId>
		<artifactId>jsqlparser</artifactId>
	<version>4.3</version>
</dependency>

使用

   String s = """
                CREATE TABLE `abc` (
                  `abc1` int NOT NULL AUTO_INCREMENT,
                  `abc2` varchar(255) DEFAULT NULL COMMENT 'comment',
                  `abc3` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'comment2',
                  UNIQUE KEY `abc1` (`abc1`),
                  KEY `abc2` (`abc2`),
                  KEY `abc3` (`abc3`)
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb""";

        Statement parse = CCJSqlParserUtil.parse(s);
        if (parse instanceof CreateTable t) {
            System.out.println(parse);
        }
    }

通过parse方法即可发现:
在这里插入图片描述
一些细节:

  • 不支持 # 语法,而只支持 – 对ssql的注释。
    如果使用#会提示#无法解析
  • 索引必须有名称,如果是没名称的索引会抛出 匹配到异常的(
  • 能够获取mysql数据类型,但是是会带varchar(255)的长度类型信息
  • 索引信息和列信息不在一个结构里面
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSqlParser是一个Java库,用于解析和分析SQL语句。以下是使用JSqlParser解析SQL语句表名的示例代码: ```java import java.io.StringReader; import java.util.ArrayList; import java.util.List; 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; import net.sf.jsqlparser.statement.select.SelectBody; import net.sf.jsqlparser.statement.select.Table; import net.sf.jsqlparser.statement.select.WithItem; public class ParseSQL { public static void main(String[] args) { String sql = "SELECT * FROM employees e INNER JOIN departments d ON e.department_id = d.department_id WHERE d.department_name = 'Sales'"; try { Statement statement = CCJSqlParserUtil.parse(new StringReader(sql)); if (statement instanceof Select) { Select selectStatement = (Select) statement; SelectBody selectBody = selectStatement.getSelectBody(); if (selectBody instanceof PlainSelect) { PlainSelect plainSelect = (PlainSelect) selectBody; List<Table> tables = getTables(plainSelect); for (Table table : tables) { String tableName = table.getName(); String aliasName = table.getAlias() != null ? table.getAlias().getName() : null; System.out.println("Table name: " + tableName); System.out.println("Table alias: " + aliasName); } } } } catch (JSQLParserException e) { e.printStackTrace(); } } private static List<Table> getTables(PlainSelect plainSelect) { List<Table> tables = new ArrayList<>(); tables.addAll(getTablesFromSelectItem(plainSelect.getSelectItems())); tables.addAll(getTablesFromJoin(plainSelect.getJoins())); tables.addAll(getTablesFromTable(plainSelect.getFromItem())); return tables; } private static List<Table> getTablesFromSelectItem(List selectItems) { List<Table> tables = new ArrayList<>(); for (Object selectItem : selectItems) { if (selectItem instanceof SelectExpressionItem) { SelectExpressionItem selectExpressionItem = (SelectExpressionItem) selectItem; if (selectExpressionItem.getExpression() instanceof Column) { Column column = (Column) selectExpressionItem.getExpression(); Table table = column.getTable(); if (table != null) { tables.add(table); } } } } return tables; } private static List<Table> getTablesFromJoin(List joins) { List<Table> tables = new ArrayList<>(); if (joins == null) { return tables; } for (Object join : joins) { if (join instanceof Join) { Join joinExpression = (Join) join; Table table = joinExpression.getRightItem(); if (table != null) { tables.add(table); } } } return tables; } private static List<Table> getTablesFromTable(FromItem fromItem) { List<Table> tables = new ArrayList<>(); if (fromItem instanceof Table) { Table table = (Table) fromItem; tables.add(table); } else if (fromItem instanceof SubSelect) { SubSelect subSelect = (SubSelect) fromItem; SelectBody selectBody = subSelect.getSelectBody(); if (selectBody instanceof PlainSelect) { PlainSelect plainSelect = (PlainSelect) selectBody; tables.addAll(getTables(plainSelect)); } } else if (fromItem instanceof LateralSubSelect) { LateralSubSelect lateralSubSelect = (LateralSubSelect) fromItem; SubSelect subSelect = lateralSubSelect.getSubSelect(); SelectBody selectBody = subSelect.getSelectBody(); if (selectBody instanceof PlainSelect) { PlainSelect plainSelect = (PlainSelect) selectBody; tables.addAll(getTables(plainSelect)); } } else if (fromItem instanceof ValuesList) { // do nothing } else { throw new UnsupportedOperationException("Unsupported from item: " + fromItem); } return tables; } } ``` 在这个例子中,我们解析了一个SQL查询,该查询涉及两个表:`employees`和`departments`。我们使用JSqlParser解析SQL语句,然后提取表名和表别名。我们首先解析`SELECT`语句,然后获取其`FROM`子句中的所有表。我们从`FROM`子句中提取表,然后从`JOIN`子句和`SELECT`列表中提取表。最后,我们打印出每个表的名称和别名(如果有的话)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值