Search in sources :

Example 1 with Keyword

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

the class WhereClauseParser method parseComparisonCondition.

private void parseComparisonCondition(final ShardingRule shardingRule, final SQLStatement sqlStatement, final List<SelectItem> items) {
    lexerEngine.skipIfEqual(Symbol.LEFT_PAREN);
    SQLExpression left = basicExpressionParser.parse(sqlStatement);
    if (lexerEngine.skipIfEqual(Symbol.EQ)) {
        parseEqualCondition(shardingRule, sqlStatement, left);
        lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN);
        return;
    }
    if (lexerEngine.skipIfEqual(DefaultKeyword.IN)) {
        parseInCondition(shardingRule, sqlStatement, left);
        lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN);
        return;
    }
    if (lexerEngine.skipIfEqual(DefaultKeyword.BETWEEN)) {
        parseBetweenCondition(shardingRule, sqlStatement, left);
        lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN);
        return;
    }
    if (sqlStatement instanceof SelectStatement && isRowNumberCondition(items, left)) {
        if (lexerEngine.skipIfEqual(Symbol.LT)) {
            parseRowCountCondition((SelectStatement) sqlStatement, false);
            return;
        }
        if (lexerEngine.skipIfEqual(Symbol.LT_EQ)) {
            parseRowCountCondition((SelectStatement) sqlStatement, true);
            return;
        }
        if (lexerEngine.skipIfEqual(Symbol.GT)) {
            parseOffsetCondition((SelectStatement) sqlStatement, false);
            return;
        }
        if (lexerEngine.skipIfEqual(Symbol.GT_EQ)) {
            parseOffsetCondition((SelectStatement) sqlStatement, true);
            return;
        }
    }
    List<Keyword> otherConditionOperators = new LinkedList<>(Arrays.asList(getCustomizedOtherConditionOperators()));
    otherConditionOperators.addAll(Arrays.asList(Symbol.LT, Symbol.LT_EQ, Symbol.GT, Symbol.GT_EQ, Symbol.LT_GT, Symbol.BANG_EQ, Symbol.BANG_GT, Symbol.BANG_LT, DefaultKeyword.LIKE, DefaultKeyword.IS));
    if (lexerEngine.skipIfEqual(otherConditionOperators.toArray(new Keyword[otherConditionOperators.size()]))) {
        lexerEngine.skipIfEqual(DefaultKeyword.NOT);
        parseOtherCondition(sqlStatement);
    }
    if (lexerEngine.skipIfEqual(DefaultKeyword.NOT)) {
        lexerEngine.nextToken();
        lexerEngine.skipIfEqual(Symbol.LEFT_PAREN);
        parseOtherCondition(sqlStatement);
        lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN);
    }
    lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN);
}
Also used : SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) DefaultKeyword(io.shardingjdbc.core.parsing.lexer.token.DefaultKeyword) Keyword(io.shardingjdbc.core.parsing.lexer.token.Keyword) LinkedList(java.util.LinkedList)

Example 2 with Keyword

use of io.shardingjdbc.core.parsing.lexer.token.Keyword 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)

Aggregations

DefaultKeyword (io.shardingjdbc.core.parsing.lexer.token.DefaultKeyword)2 Keyword (io.shardingjdbc.core.parsing.lexer.token.Keyword)2 LexerEngine (io.shardingjdbc.core.parsing.lexer.LexerEngine)1 MySQLKeyword (io.shardingjdbc.core.parsing.lexer.dialect.mysql.MySQLKeyword)1 Assist (io.shardingjdbc.core.parsing.lexer.token.Assist)1 TokenType (io.shardingjdbc.core.parsing.lexer.token.TokenType)1 SQLParsingException (io.shardingjdbc.core.parsing.parser.exception.SQLParsingException)1 SQLExpression (io.shardingjdbc.core.parsing.parser.expression.SQLExpression)1 SelectStatement (io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement)1 LinkedList (java.util.LinkedList)1