Skip to content

Commit

Permalink
更新Antlr解析 支持between and 操作
Browse files Browse the repository at this point in the history
  • Loading branch information
akwei committed Jul 18, 2012
1 parent ccfa13f commit 9e82371
Show file tree
Hide file tree
Showing 12 changed files with 3,453 additions and 2,920 deletions.
Binary file added lib/druid-0.2.3.jar
Binary file not shown.
35 changes: 35 additions & 0 deletions src/main/java/halo/dal/analysis/antlr/AntlrParserDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,41 @@
*/
public interface AntlrParserDelegate {

int SQLOP_INSERT = 1;

int SQLOP_UPDATE = 2;

int SQLOP_DELETE = 3;

int SQLOP_SELECT = 4;

/**
* 设置当前sql操作的方式
*
* @param sqlOp
*/
void setSqlOp(int sqlOp);

int getSqlOp();

/**
* 是否含有from关键字
*
* @param hasSelectFrom
*/
void setHasSelectFrom(boolean hasSelectFrom);

boolean isHasSelectFrom();

/**
* 是否含有between and 关键字
*
* @param hasBetweenAnd
*/
void setHasBetweenAnd(boolean hasBetweenAnd);

boolean isHasBetweenAnd();

/**
* 当获得表信息时,触发的方法
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,36 @@ public class DefAntlrParserDelegate implements AntlrParserDelegate {

private List<ColExpr> colExprs = new ArrayList<ColExpr>();

private boolean hasBetweenAnd;

private boolean hasSelectFrom;

private int sqlOp;

public void setSqlOp(int sqlOp) {
this.sqlOp = sqlOp;
}

public int getSqlOp() {
return sqlOp;
}

public boolean isHasBetweenAnd() {
return this.hasBetweenAnd;
}

public boolean isHasSelectFrom() {
return hasSelectFrom;
}

public void setHasSelectFrom(boolean hasSelectFrom) {
this.hasSelectFrom = hasSelectFrom;
}

public void setHasBetweenAnd(boolean hasBetweenAnd) {
this.hasBetweenAnd = hasBetweenAnd;
}

public void onFindTable(String name, String alias) {
tables.add(new Table(name, alias));
}
Expand All @@ -24,4 +54,4 @@ public List<ColExpr> getColExprs() {
public List<Table> getTables() {
return tables;
}
}
}
52 changes: 52 additions & 0 deletions src/main/java/halo/dal/analysis/antlr/MockAntlrParserDelegate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package halo.dal.analysis.antlr;

import java.util.List;


public class MockAntlrParserDelegate implements AntlrParserDelegate {

public void setSqlOp(int sqlOp) {
// TODO Auto-generated method stub
}

public int getSqlOp() {
// TODO Auto-generated method stub
return 0;
}

public void setHasSelectFrom(boolean hasSelectFrom) {
// TODO Auto-generated method stub
}

public boolean isHasSelectFrom() {
// TODO Auto-generated method stub
return false;
}

public void setHasBetweenAnd(boolean hasBetweenAnd) {
// TODO Auto-generated method stub
}

public boolean isHasBetweenAnd() {
// TODO Auto-generated method stub
return false;
}

public void onFindTable(String name, String alias) {
// TODO Auto-generated method stub
}

public void onFindColExper(String column, String op) {
// TODO Auto-generated method stub
}

public List<Table> getTables() {
// TODO Auto-generated method stub
return null;
}

public List<ColExpr> getColExprs() {
// TODO Auto-generated method stub
return null;
}
}
30 changes: 11 additions & 19 deletions src/main/java/halo/dal/analysis/antlr/v3/AntlrV3SQLAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import halo.dal.analysis.SQLAnalyzer;
import halo.dal.analysis.SQLExpression;
import halo.dal.analysis.SQLInfo;
import halo.dal.analysis.SQLKeyErrException;
import halo.dal.analysis.SQLStruct;
import halo.dal.analysis.antlr.AntlrParserDelegate;
import halo.dal.analysis.antlr.ColExpr;
Expand All @@ -26,10 +25,6 @@ public class AntlrV3SQLAnalyzer implements SQLAnalyzer {

protected final static String SQL_BLANK = " ";

protected final static String SQL_KEY_SELECT = "select ";

protected final static String SQL_KEY_SELECT_ = "SELECT ";

public SQLInfo analyse(String sql, SQLStruct sqlStruct, Object[] values,
Map<String, Object> context) {
SQLInfoImpl info = new SQLInfoImpl();
Expand All @@ -47,20 +42,7 @@ public SQLInfo analyse(String sql, SQLStruct sqlStruct, Object[] values,
}

public SQLStruct parse(String sql, Map<String, Object> context) {
if (sql.indexOf(" between ") != -1 || sql.indexOf(" BETWEEN ") != -1) {
throw new SQLKeyErrException("not supported sql key: between ");
}
String _sql = sql.replaceAll("\\. {1,}", "\\.").trim();
// 对于只运行数据库函数时,不需要解析
SQLStruct sqlStruct = new SQLStruct();
if (_sql.startsWith(SQL_KEY_SELECT) || _sql.startsWith(SQL_KEY_SELECT_)) {
if (_sql.indexOf(" from ") == -1 && _sql.indexOf(" FROM ") == -1) {
sqlStruct.setCanParse(false);
return sqlStruct;
}
}
sqlStruct.setCanParse(true);
AntlrV3SQLLexer lexer = new AntlrV3SQLLexer(new ANTLRStringStream(_sql));
AntlrV3SQLLexer lexer = new AntlrV3SQLLexer(new ANTLRStringStream(sql));
CommonTokenStream tokens = new CommonTokenStream(lexer);
AntlrV3SQLParser parser = new AntlrV3SQLParser(tokens);
AntlrParserDelegate delegate = new DefAntlrParserDelegate();
Expand All @@ -71,6 +53,16 @@ public SQLStruct parse(String sql, Map<String, Object> context) {
catch (RecognitionException e) {
throw new RuntimeException(e);
}
// if (delegate.isHasBetweenAnd()) {
// throw new SQLKeyErrException("not supported sql key: between ");
// }
SQLStruct sqlStruct = new SQLStruct();
if (delegate.getSqlOp() == AntlrParserDelegate.SQLOP_SELECT
&& !delegate.isHasSelectFrom()) {
sqlStruct.setCanParse(false);
return sqlStruct;
}
sqlStruct.setCanParse(true);
Table table;
for (int i = 0; i < delegate.getTables().size(); i++) {
table = delegate.getTables().get(i);
Expand Down
Loading

0 comments on commit 9e82371

Please sign in to comment.