Search in sources :

Example 1 with LexerEngine

use of io.shardingjdbc.core.parsing.lexer.LexerEngine in project sharding-jdbc by shardingjdbc.

the class SQLJudgeEngine method judge.

/**
 * judge SQL Type only.
 *
 * @return SQL statement
 */
public SQLStatement judge() {
    LexerEngine lexerEngine = LexerEngineFactory.newInstance(DatabaseType.MySQL, sql);
    lexerEngine.nextToken();
    while (true) {
        TokenType tokenType = lexerEngine.getCurrentToken().getType();
        if (tokenType instanceof Keyword) {
            if (isDQL(tokenType)) {
                return getDQLStatement();
            }
            if (isDML(tokenType)) {
                return getDMLStatement(tokenType);
            }
            if (isDDL(tokenType)) {
                return getDDLStatement();
            }
            if (isTCL(tokenType)) {
                return getTCLStatement();
            }
            if (isDAL(tokenType)) {
                return getDALStatement(tokenType, lexerEngine);
            }
        }
        if (tokenType instanceof Assist && Assist.END == tokenType) {
            throw new SQLParsingException("Unsupported SQL statement: [%s]", sql);
        }
        lexerEngine.nextToken();
    }
}
Also used : SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) LexerEngine(io.shardingjdbc.core.parsing.lexer.LexerEngine) TokenType(io.shardingjdbc.core.parsing.lexer.token.TokenType) DefaultKeyword(io.shardingjdbc.core.parsing.lexer.token.DefaultKeyword) MySQLKeyword(io.shardingjdbc.core.parsing.lexer.dialect.mysql.MySQLKeyword) Keyword(io.shardingjdbc.core.parsing.lexer.token.Keyword) Assist(io.shardingjdbc.core.parsing.lexer.token.Assist)

Example 2 with LexerEngine

use of io.shardingjdbc.core.parsing.lexer.LexerEngine in project sharding-jdbc by shardingjdbc.

the class SQLParsingEngine method parse.

/**
 * Parse SQL.
 *
 * @return parsed SQL statement
 */
public SQLStatement parse() {
    SQLStatement cachedSQLStatement = ParsingResultCache.getInstance().getSQLStatement(sql);
    if (null != cachedSQLStatement) {
        return cachedSQLStatement;
    }
    LexerEngine lexerEngine = LexerEngineFactory.newInstance(dbType, sql);
    lexerEngine.nextToken();
    return SQLParserFactory.newInstance(dbType, lexerEngine.getCurrentToken().getType(), shardingRule, lexerEngine).parse();
}
Also used : LexerEngine(io.shardingjdbc.core.parsing.lexer.LexerEngine) SQLStatement(io.shardingjdbc.core.parsing.parser.sql.SQLStatement)

Aggregations

LexerEngine (io.shardingjdbc.core.parsing.lexer.LexerEngine)2 MySQLKeyword (io.shardingjdbc.core.parsing.lexer.dialect.mysql.MySQLKeyword)1 Assist (io.shardingjdbc.core.parsing.lexer.token.Assist)1 DefaultKeyword (io.shardingjdbc.core.parsing.lexer.token.DefaultKeyword)1 Keyword (io.shardingjdbc.core.parsing.lexer.token.Keyword)1 TokenType (io.shardingjdbc.core.parsing.lexer.token.TokenType)1 SQLParsingException (io.shardingjdbc.core.parsing.parser.exception.SQLParsingException)1 SQLStatement (io.shardingjdbc.core.parsing.parser.sql.SQLStatement)1