Druid SQL???????????????
?????SegmentFault ???????[ 2017/1/23 11:30:26 ] ????????????? SQL Server
????????????????????? Druid SQL ????????ι?????
????Demo ????
???????????????
/**
*
*
* @author beanlam
* @date 2017??1??10?? ????11:06:26
* @version 1.0
*
*/
public class ParserMain {
public static void main(String[] args) {
String sql = "select * from user order by id";
// ??? MySQL Parser
SQLStatementParser parser = new MySqlStatementParser(sql);
// ???Parser????????AST??????SQLStatement??AST
SQLStatement statement = parser.parseStatement();
// ???visitor??????AST
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
statement.accept(visitor);
System.out.println(visitor.getColumns());
System.out.println(visitor.getOrderByColumns());
}
}
????????????????????? Parser???????? SQLStatementParser ????????????????? SQL ???? Parser ????? MySqlStatementParser ??
????Parser ????????????? SQLStatement ?????????????????????????????
???????????
????Druid ??????????? ?????? ?? ??????? ???????? SQLParser ?? Lexer ??????? Parser ?????? Lexer??
public class SQLParser {
protected final Lexer lexer;
protected String dbType;
public SQLParser(String sql?? String dbType){
this(new Lexer(sql)?? dbType);
this.lexer.nextToken();
}
public SQLParser(String sql){
this(sql?? null);
}
public SQLParser(Lexer lexer){
this(lexer?? null);
}
public SQLParser(Lexer lexer?? String dbType){
this.lexer = lexer;
this.dbType = dbType;
}
}
?????????????? Druid ?????? Lexer ?????????????? Lexer ????????????? MySqlLexer
????Lexer ????????????????????????????Lexer???? Keywords ?????
????protected Keywords keywods = Keywords.DEFAULT_KEYWORDS;
????Keywords ??????? key ??????value ? Token ?????????????? Token ??????????????????“select” ?? Token ?????? Select Token???? “abc” ?? Token ???????????????????? Identifier Token??
?????? MySqlLexer ???????????丸??? Keywords ?????????????? Keywords?????????? Lexer ???????????????????????? MySqlLexer ????????????????????????? MySQL ????? SQL ?????????????
????Parser ?? Lexer ??????????? Parser ?????????????????? Lexer?????????Lexer ??t???????????????????? Parser ?????????
????Lexer ??t?????????????????????????????????????????? Lexer ?????????????????????????? Lexer ????ν???????????????????????
?????? Lexer ?У? nextToken() ???????????????????????????????????? SQL ??????????β?????????????????? token() ????????????????ν????????? Token ???????? Token ????????????Identifier????Lexer ????????? stringVal() ????????????????????????????
??????? Lexer ?? nextToken() ???????????????????????? if ???? switch ???????????????????????????????????????????ζ???????????????????????????????ж?????????????????????????????????????????????????? ???? ?????????????????????????????ж????????????????y??????????????
????Select ??????
???????? Lexer ?????????????????????? Parser ????????????? Demo ??????????????????????????????? parser.parseStatement() ????????????????????????????????????????????????
if (lexer.token() == Token.xxx) {
// ??????? xxx ????
return;
}
if (lexer.token() == Token.aaa) {
// ??????? aaa ????
return;
}
??????????????????? Select ???????????????????ù?????μ????
????if (lexer.token() == Token.SELECT) {
????statementList.add(parseSelect());
????continue;
????}
????????? parseSelect() ?????? MySqlStatementParser ?????????????????????????????????????????????????????
@Override
public SQLStatement parseSelect() {
MySqlSelectParser selectParser = new MySqlSelectParser(this.exprParser);
SQLSelect select = selectParser.select();
if (selectParser.returningFlag) {
return selectParser.updateStmt;
}
return new SQLSelectStatement(select?? JdbcConstants.MYSQL);
}
??????????????? MySQL Select ???? Parser???????? select() ???????н???????????? SQLSelect ??? SQLSelectStatement ??????? SQLSelectStatement ???????????? AST ??????????SQLSelect ??????????????
????????????????????????????????????? AST ???ν???
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11