如何使用 JSQLParser 获取 Select 子句的主体 [英] How to get Select clause's body with JSQLParser

查看:186
本文介绍了如何使用 JSQLParser 获取 Select 子句的主体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 JsqlParser 0.8.0.

I'm using JsqlParser 0.8.0.

我有以下字符串:

String sql = "SELECT table alias FROM table;";

CCJSqlParserManager pm = new CCJSqlParserManager();

Select s = (Select) pm.parse(new StringReader(sql));

System.out.println(s.getSelectBody());

我希望打印 table AS alias,但是打印了整个 sql-query.如何仅打印 SelectBody.

I expected the table AS alias to be printed, but the whole sql-query was printed instead. How can I make printing only the SelectBody.

推荐答案

SelectBody 属于 PlainSelect 类型.这与您想要获取的 SelectItems 不同.这是获取这些项目的示例:

SelectBody is of type PlainSelect. This is not equivalent to the SelectItems which you want to get. This is an example for getting these items:

String sqlStr = "SELECT mytable alias FROM mytable";
Select select = (Select)CCJSqlParserUtil.parse(sqlStr);
System.out.println(select.getSelectBody());

PlainSelect pl = (PlainSelect)select.getSelectBody();
for (SelectItem item : pl.getSelectItems()) {
    System.out.println(item.toString());
}

顺便说一下,此示例使用的是 JSqlParser V0.9.3,但获取 SelectItems 的相关部分在 V0.8.0 中是相同的.

By the way this example is using JSqlParser V0.9.3 but the relevant parts of getting the SelectItems are identical in V0.8.0.

您不应使用关键字作为列名或表名.这可能会在很多方面混淆解析过程.但是,在 JSqlParser 的最新版本中允许使用越来越多的关键字.

You should not using keywords as column or tablenames. This could confuse in many ways the parsing process. However, more and more keywords are allowed in recent versions of JSqlParser.

这篇关于如何使用 JSQLParser 获取 Select 子句的主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆