- 浏览: 15696 次
- 性别:
- 来自: 深圳
最新评论
-
lhp__2006:
JSqlParser 如何统计sql语句中某个关键字的个数,例 ...
JSqlParser -
murray2081:
讲的不错
MINA源码分析的札记1--Write流程
homePage: http://jsqlparser.sourceforge.net
What is it
JSqlParser parses an SQL statement and translate it into a hierarchy of Java classes.
The generated hierarchy can be navigated using the Visitor Pattern
How it works
It is built using JavaCC. The core JavaCC grammar for SQL has been taken from Guido Draheim's site and has been changed in order to produce a hierarchy of Java classes.
How it can be used
The method CCJSqlParserManager.parse returns a class implementing Statement which can be used to navigate the structure representing the SQL statement.
To avoid using the instanceof operator as in:
if (statement instanceof Insert) {
// insert case
}
else if (statement instanceof Update) {
// update case
}
the hierarchy can be accessed using proper visitors:
void visit(Insert statement) {
// insert case
}
void visit(Update statement) {
// update case
}
Example: a tables names finder
With this example class you can easily get the list of tables in a select statement (including subqueries and joins and, as soon as the union code will be ready, unions)
Code using TablesNamesFinder:
[..]
/*
now you should use a class that implements StatementVisitor to decide what to do based on the kind of the statement, that is SELECT or INSERT etc. but here we are only interested in SELECTS
*/
[..]
What is it
JSqlParser parses an SQL statement and translate it into a hierarchy of Java classes.
The generated hierarchy can be navigated using the Visitor Pattern
How it works
It is built using JavaCC. The core JavaCC grammar for SQL has been taken from Guido Draheim's site and has been changed in order to produce a hierarchy of Java classes.
How it can be used
The method CCJSqlParserManager.parse returns a class implementing Statement which can be used to navigate the structure representing the SQL statement.
To avoid using the instanceof operator as in:
if (statement instanceof Insert) {
// insert case
}
else if (statement instanceof Update) {
// update case
}
the hierarchy can be accessed using proper visitors:
void visit(Insert statement) {
// insert case
}
void visit(Update statement) {
// update case
}
Example: a tables names finder
With this example class you can easily get the list of tables in a select statement (including subqueries and joins and, as soon as the union code will be ready, unions)
public class TablesNamesFinder implements SelectVisitor, FromItemVisitor, JoinVisitor, ExpressionVisitor, ItemsListVisitor { private List tables; public List getTableList(Select select) { tables = new ArrayList(); select.getSelectBody().accept(this); return tables; } public void visit(PlainSelect plainSelect) { plainSelect.getFromItem().accept(this); if (plainSelect.getJoins() != null) { for (Iterator joinsIt = plainSelect.getJoins().iterator(); joinsIt.hasNext();) { Join join = (Join) joinsIt.next(); join.getRightItem().accept(this); } } if (plainSelect.getWhere() != null) plainSelect.getWhere().accept(this); } public void visit(Union union) { for (Iterator iter = union.getPlainSelects().iterator(); iter.hasNext();) { PlainSelect plainSelect = (PlainSelect) iter.next(); visit(plainSelect); } } public void visit(Table tableName) { String tableWholeName = tableName.getWholeTableName(); tables.add(tableWholeName); } public void visit(SubSelect subSelect) { subSelect.getSelectBody().accept(this); } public void visit(Addition addition) { visitBinaryExpression(addition); } public void visit(AndExpression andExpression) { visitBinaryExpression(andExpression); } public void visit(Between between) { between.getLeftExpression().accept(this); between.getBetweenExpressionStart().accept(this); between.getBetweenExpressionEnd().accept(this); } public void visit(Column tableColumn) { } public void visit(Division division) { visitBinaryExpression(division); } public void visit(DoubleValue doubleValue) { } public void visit(EqualsTo equalsTo) { visitBinaryExpression(equalsTo); } public void visit(Function function) { } public void visit(GreaterThan greaterThan) { visitBinaryExpression(greaterThan); } public void visit(GreaterThanEquals greaterThanEquals) { visitBinaryExpression(greaterThanEquals); } public void visit(InExpression inExpression) { inExpression.getLeftExpression().accept(this); inExpression.getItemsList().accept(this); } public void visit(InverseExpression inverseExpression) { inverseExpression.getExpression().accept(this); } public void visit(IsNullExpression isNullExpression) { } public void visit(JdbcParameter jdbcParameter) { } public void visit(LikeExpression likeExpression) { visitBinaryExpression(likeExpression); } public void visit(ExistsExpression existsExpression) { existsExpression.getRightExpression().accept(this); } public void visit(LongValue longValue) { } public void visit(MinorThan minorThan) { visitBinaryExpression(minorThan); } public void visit(MinorThanEquals minorThanEquals) { visitBinaryExpression(minorThanEquals); } public void visit(Multiplication multiplication) { visitBinaryExpression(multiplication); } public void visit(NotEqualsTo notEqualsTo) { visitBinaryExpression(notEqualsTo); } public void visit(NullValue nullValue) { } public void visit(OrExpression orExpression) { visitBinaryExpression(orExpression); } public void visit(Parenthesis parenthesis) { parenthesis.getExpression().accept(this); } public void visit(StringValue stringValue) { } public void visit(Subtraction subtraction) { visitBinaryExpression(subtraction); } public void visitBinaryExpression(BinaryExpression binaryExpression) { binaryExpression.getLeftExpression().accept(this); binaryExpression.getRightExpression().accept(this); } public void visit(ExpressionList expressionList) { for (Iterator iter = expressionList.getExpressions().iterator(); iter.hasNext();) { Expression expression = (Expression) iter.next(); expression.accept(this); } } public void visit(DateValue dateValue) { } public void visit(TimestampValue timestampValue) { } public void visit(TimeValue timeValue) { } public void visit(CaseExpression caseExpression) { } public void visit(WhenClause whenClause) { } public void visit(AllComparisonExpression allComparisonExpression) { allComparisonExpression.GetSubSelect().getSelectBody().accept(this); } public void visit(AnyComparisonExpression anyComparisonExpression) { anyComparisonExpression.GetSubSelect().getSelectBody().accept(this); } public void visit(SubJoin subjoin) { subjoin.getLeft().accept(this); subjoin.getJoin().getRightItem().accept(this); } }
Code using TablesNamesFinder:
[..]
CCJSqlParserManager pm = new CCJSqlParserManager(); String sql = "SELECT * FROM MY_TABLE1, MY_TABLE2, (SELECT * FROM MY_TABLE3) LEFT OUTER JOIN MY_TABLE4 "+ " WHERE ID = (SELECT MAX(ID) FROM MY_TABLE5) AND ID2 IN (SELECT * FROM MY_TABLE6)" ; net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
/*
now you should use a class that implements StatementVisitor to decide what to do based on the kind of the statement, that is SELECT or INSERT etc. but here we are only interested in SELECTS
*/
if (statement instanceof Select) { Select selectStatement = (Select) statement; TablesNamesFinder tablesNamesFinder = new TablesNamesFinder(); List tableList = tablesNamesFinder.getTableList(selectStatement); for (Iterator iter = tableList.iterator(); iter.hasNext();) { System.out.println(tableName); } }
[..]
发表评论
-
AbstractPollingIoAcceptor
2010-11-19 10:44 0AbstractPollingIoAcceptor中使用了独立 ... -
MINA源码分析的札记4--NioProcessor
2010-11-13 11:50 0啊啊 -
MINA源码分析的札记3--IoProcessor
2010-11-13 11:49 0IoProcessor的大部分默认实现都在AbstractPo ... -
MINA源码分析的札记2--WriteRequest
2010-11-13 11:31 0关于WriteRequest的实现 -
MINA源码分析的札记1--Write流程
2010-11-13 11:26 2019从IoSession调用write的过程: IoSessio ... -
Java String.split 和 StringUtils.split
2010-11-08 13:41 1910Java代码 String[] strs ... -
初识iBATIS
2010-11-07 10:54 759iBATIS一词来源于“internet”和“abatis”的 ... -
ANTLR
2010-11-07 10:28 1691有一个可用的 ANSI SQL 语法。 您可以使用,以创建您自 ... -
SQL Query Parser
2010-11-07 10:18 950eclipse中包含的一个SQL解析的工具。 还没仔细研究。 ... -
SQLJEP
2010-11-07 10:10 898Homepage:http://sqljep.sourcefo ... -
Zql: open-source java SQL parser
2010-11-07 09:34 1981home page: http://www.gibello.c ... -
JavaCC
2010-11-07 09:30 974主页:https://javacc.dev.java.net ... -
Zql: a Java SQL parser
2010-11-07 09:19 1890What is Zql ? Zql is a SQL par ...
相关推荐
《JSqlParser:解析SQL语句的利器》 在Java世界中,处理SQL语句时,我们常常需要对SQL进行解析,以便理解其结构、提取关键信息或进行动态SQL生成。JSqlParser就是一个强大的开源库,专为此目的而设计。JSqlParser-...
**JSqlParser** 是一个Java实现的SQL解析库,它能够解析SQL语句并将其转换为抽象语法树(AST)。这个库对理解SQL查询、执行SQL动态构建或进行SQL分析非常有帮助。在这个压缩包中,包含了三个重要的文件: 1. **...
赠送jar包:jsqlparser-4.0.jar; 赠送原API文档:jsqlparser-4.0-javadoc.jar; 赠送源代码:jsqlparser-4.0-sources.jar; 赠送Maven依赖信息文件:jsqlparser-4.0.pom; 包含翻译后的API文档:jsqlparser-4.0-...
赠送jar包:jsqlparser-4.0.jar; 赠送原API文档:jsqlparser-4.0-javadoc.jar; 赠送源代码:jsqlparser-4.0-sources.jar; 赠送Maven依赖信息文件:jsqlparser-4.0.pom; 包含翻译后的API文档:jsqlparser-4.0-...
赠送jar包:jsqlparser-1.4.jar; 赠送原API文档:jsqlparser-1.4-javadoc.jar; 赠送源代码:jsqlparser-1.4-sources.jar; 赠送Maven依赖信息文件:jsqlparser-1.4.pom; 包含翻译后的API文档:jsqlparser-1.4-...
赠送jar包:jsqlparser-4.2.jar; 赠送原API文档:jsqlparser-4.2-javadoc.jar; 赠送源代码:jsqlparser-4.2-sources.jar; 赠送Maven依赖信息文件:jsqlparser-4.2.pom; 包含翻译后的API文档:jsqlparser-4.2-...
赠送jar包:jsqlparser-4.3.jar; 赠送原API文档:jsqlparser-4.3-javadoc.jar; 赠送源代码:jsqlparser-4.3-sources.jar; 赠送Maven依赖信息文件:jsqlparser-4.3.pom; 包含翻译后的API文档:jsqlparser-4.3-...
赠送jar包:jsqlparser-4.2.jar; 赠送原API文档:jsqlparser-4.2-javadoc.jar; 赠送源代码:jsqlparser-4.2-sources.jar; 赠送Maven依赖信息文件:jsqlparser-4.2.pom; 包含翻译后的API文档:jsqlparser-4.2-...
**JSqlParser详解** JSqlParser是一款开源的Java库,用于解析SQL语句。它能够帮助开发者解析SQL字符串,转化为可操作的抽象语法树(AST),进而方便进行SQL的分析、修改或生成。版本0.9.5是该库的一个重要更新,...
JSqlParser是一个用Java编写的开源SQL解析库,它可以解析SQL语句并生成抽象语法树(AST),也可以根据AST生成SQL语句。这意味着你可以利用它来分析、修改甚至生成SQL查询。 以下是一些JSqlParser的主要应用场景: ...
赠送jar包:jsqlparser-1.0.jar; 赠送原API文档:jsqlparser-1.0-javadoc.jar; 赠送源代码:jsqlparser-1.0-sources.jar; 赠送Maven依赖信息文件:jsqlparser-1.0.pom; 包含翻译后的API文档:jsqlparser-1.0-...
标题中的“基于jsqlparser的工具”指的是一个利用jsqlparser库开发的软件工具,它主要用于解析和操作SQL语句。jsqlparser是Java编写的一个开源项目,它提供了对SQL语句的解析和抽象语法树(AST)构建功能,允许...
赠送jar包:jsqlparser-2.0.jar; 赠送原API文档:jsqlparser-2.0-javadoc.jar; 赠送源代码:jsqlparser-2.0-sources.jar; 赠送Maven依赖信息文件:jsqlparser-2.0.pom; 包含翻译后的API文档:jsqlparser-2.0-...
然而,对于复杂的SQL语句处理,有时需要编写自定义的解析工具,这就是JSqlParser的用武之地。JSqlParser是一个用Java编写的开源项目,它提供了一个强大的SQL语法解释器,能够帮助开发者解析、理解和操作SQL语句。 ...
赠送jar包:jsqlparser-4.3.jar; 赠送原API文档:jsqlparser-4.3-javadoc.jar; 赠送源代码:jsqlparser-4.3-sources.jar; 赠送Maven依赖信息文件:jsqlparser-4.3.pom; 包含翻译后的API文档:jsqlparser-4.3-...
赠送jar包:jsqlparser-0.9.5.jar; 赠送原API文档:jsqlparser-0.9.5-javadoc.jar; 赠送源代码:jsqlparser-0.9.5-sources.jar; 包含翻译后的API文档:jsqlparser-0.9.5-javadoc-API文档-中文(简体)版.zip ...
**jsqlparser** 是一个Java编写的开源项目,主要用于解析SQL语句,提供了一种抽象语法树(AST)的表示方式,使得开发者可以方便地分析、理解和操作SQL语句。这个库广泛应用于需要处理SQL的场景,比如ORM框架、数据库...
赠送jar包:jsqlparser-1.4.jar; 赠送原API文档:jsqlparser-1.4-javadoc.jar; 赠送源代码:jsqlparser-1.4-sources.jar; 赠送Maven依赖信息文件:jsqlparser-1.4.pom; 包含翻译后的API文档:jsqlparser-1.4-...
赠送jar包:jsqlparser-0.9.5.jar 赠送原API文档:jsqlparser-0.9.5-javadoc.jar 赠送源代码:jsqlparser-0.9.5-sources.jar 包含翻译后的API文档:jsqlparser-0.9.5-javadoc-API文档-中文(简体)-英语-对照版.zip...
**jsqlparser-3.2.zip** 是一个包含与Java相关的库资源的压缩包,主要服务于SQL解析。这个包提供了三个关键组件:`jsqlparser-3.2.jar`、`jsqlparser-3.2-javadoc.jar` 和 `jsqlparser-3.2-sources.jar`。这些文件在...