Search in sources :

Example 6 with SQLParsingException

use of io.shardingjdbc.core.parsing.parser.exception.SQLParsingException 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 7 with SQLParsingException

use of io.shardingjdbc.core.parsing.parser.exception.SQLParsingException in project sharding-jdbc by shardingjdbc.

the class Lexer method nextToken.

/**
 * Analyse next token.
 */
public final void nextToken() {
    skipIgnoredToken();
    if (isVariableBegin()) {
        currentToken = new Tokenizer(input, dictionary, offset).scanVariable();
    } else if (isNCharBegin()) {
        currentToken = new Tokenizer(input, dictionary, ++offset).scanChars();
    } else if (isIdentifierBegin()) {
        currentToken = new Tokenizer(input, dictionary, offset).scanIdentifier();
    } else if (isHexDecimalBegin()) {
        currentToken = new Tokenizer(input, dictionary, offset).scanHexDecimal();
    } else if (isNumberBegin()) {
        currentToken = new Tokenizer(input, dictionary, offset).scanNumber();
    } else if (isSymbolBegin()) {
        currentToken = new Tokenizer(input, dictionary, offset).scanSymbol();
    } else if (isCharsBegin()) {
        currentToken = new Tokenizer(input, dictionary, offset).scanChars();
    } else if (isEnd()) {
        currentToken = new Token(Assist.END, "", offset);
    } else {
        throw new SQLParsingException(this, Assist.ERROR);
    }
    offset = currentToken.getEndPosition();
}
Also used : SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) Token(io.shardingjdbc.core.parsing.lexer.token.Token) Tokenizer(io.shardingjdbc.core.parsing.lexer.analyzer.Tokenizer)

Example 8 with SQLParsingException

use of io.shardingjdbc.core.parsing.parser.exception.SQLParsingException in project sharding-jdbc by shardingjdbc.

the class OrderByClauseParser method parseSelectOrderByItem.

private OrderItem parseSelectOrderByItem(final SelectStatement selectStatement) {
    SQLExpression sqlExpression = basicExpressionParser.parse(selectStatement);
    OrderDirection orderDirection = OrderDirection.ASC;
    if (lexerEngine.skipIfEqual(DefaultKeyword.ASC)) {
        orderDirection = OrderDirection.ASC;
    } else if (lexerEngine.skipIfEqual(DefaultKeyword.DESC)) {
        orderDirection = OrderDirection.DESC;
    }
    if (sqlExpression instanceof SQLNumberExpression) {
        return new OrderItem(((SQLNumberExpression) sqlExpression).getNumber().intValue(), orderDirection, getNullOrderDirection());
    }
    if (sqlExpression instanceof SQLIdentifierExpression) {
        return new OrderItem(SQLUtil.getExactlyValue(((SQLIdentifierExpression) sqlExpression).getName()), orderDirection, getNullOrderDirection(), selectStatement.getAlias(SQLUtil.getExactlyValue(((SQLIdentifierExpression) sqlExpression).getName())));
    }
    if (sqlExpression instanceof SQLPropertyExpression) {
        SQLPropertyExpression sqlPropertyExpression = (SQLPropertyExpression) sqlExpression;
        return new OrderItem(SQLUtil.getExactlyValue(sqlPropertyExpression.getOwner().getName()), SQLUtil.getExactlyValue(sqlPropertyExpression.getName()), orderDirection, getNullOrderDirection(), selectStatement.getAlias(SQLUtil.getExactlyValue(sqlPropertyExpression.getOwner().getName()) + "." + SQLUtil.getExactlyValue(sqlPropertyExpression.getName())));
    }
    if (sqlExpression instanceof SQLIgnoreExpression) {
        SQLIgnoreExpression sqlIgnoreExpression = (SQLIgnoreExpression) sqlExpression;
        return new OrderItem(sqlIgnoreExpression.getExpression(), orderDirection, getNullOrderDirection(), selectStatement.getAlias(sqlIgnoreExpression.getExpression()));
    }
    throw new SQLParsingException(lexerEngine);
}
Also used : SQLNumberExpression(io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression) SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) SQLPropertyExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPropertyExpression) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) SQLIdentifierExpression(io.shardingjdbc.core.parsing.parser.expression.SQLIdentifierExpression) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) SQLIgnoreExpression(io.shardingjdbc.core.parsing.parser.expression.SQLIgnoreExpression) OrderDirection(io.shardingjdbc.core.constant.OrderDirection)

Example 9 with SQLParsingException

use of io.shardingjdbc.core.parsing.parser.exception.SQLParsingException in project sharding-jdbc by shardingjdbc.

the class AbstractCreateParser method parse.

@Override
public DDLStatement parse() {
    lexerEngine.nextToken();
    lexerEngine.skipAll(getSkippedKeywordsBetweenCreateIndexAndKeyword());
    lexerEngine.skipAll(getSkippedKeywordsBetweenCreateAndKeyword());
    DDLStatement result = new DDLStatement();
    if (lexerEngine.skipIfEqual(DefaultKeyword.INDEX)) {
        lexerEngine.skipAll(getSkippedKeywordsBetweenCreateIndexAndIndexName());
        parseIndex(result);
    } else if (lexerEngine.skipIfEqual(DefaultKeyword.TABLE)) {
        lexerEngine.skipAll(getSkippedKeywordsBetweenCreateTableAndTableName());
    } else {
        throw new SQLParsingException("Can't support other CREATE grammar unless CREATE TABLE, CREATE INDEX.");
    }
    tableReferencesClauseParser.parseSingleTableWithoutAlias(result);
    return result;
}
Also used : SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) DDLStatement(io.shardingjdbc.core.parsing.parser.sql.ddl.DDLStatement)

Example 10 with SQLParsingException

use of io.shardingjdbc.core.parsing.parser.exception.SQLParsingException in project sharding-jdbc by shardingjdbc.

the class AbstractDropParser method parse.

@Override
public DDLStatement parse() {
    lexerEngine.nextToken();
    lexerEngine.skipAll(getSkippedKeywordsBetweenDropAndTable());
    DDLStatement result = new DDLStatement();
    if (lexerEngine.skipIfEqual(DefaultKeyword.INDEX)) {
        lexerEngine.skipAll(getSkippedKeywordsBetweenDropIndexAndIndexName());
        parseIndex(result);
    } else if (lexerEngine.skipIfEqual(DefaultKeyword.TABLE)) {
        lexerEngine.skipAll(getSkippedKeywordsBetweenDropTableAndTableName());
        tableReferencesClauseParser.parseSingleTableWithoutAlias(result);
    } else {
        throw new SQLParsingException("Can't support other DROP grammar unless DROP TABLE, DROP INDEX.");
    }
    return result;
}
Also used : SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) DDLStatement(io.shardingjdbc.core.parsing.parser.sql.ddl.DDLStatement)

Aggregations

SQLParsingException (io.shardingjdbc.core.parsing.parser.exception.SQLParsingException)12 LimitValue (io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)7 Limit (io.shardingjdbc.core.parsing.parser.context.limit.Limit)5 RowCountToken (io.shardingjdbc.core.parsing.parser.token.RowCountToken)5 OffsetToken (io.shardingjdbc.core.parsing.parser.token.OffsetToken)3 SQLExpression (io.shardingjdbc.core.parsing.parser.expression.SQLExpression)2 SQLNumberExpression (io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression)2 DDLStatement (io.shardingjdbc.core.parsing.parser.sql.ddl.DDLStatement)2 OrderDirection (io.shardingjdbc.core.constant.OrderDirection)1 LexerEngine (io.shardingjdbc.core.parsing.lexer.LexerEngine)1 Tokenizer (io.shardingjdbc.core.parsing.lexer.analyzer.Tokenizer)1 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 Token (io.shardingjdbc.core.parsing.lexer.token.Token)1 TokenType (io.shardingjdbc.core.parsing.lexer.token.TokenType)1 OrderItem (io.shardingjdbc.core.parsing.parser.context.OrderItem)1 SQLIdentifierExpression (io.shardingjdbc.core.parsing.parser.expression.SQLIdentifierExpression)1 SQLIgnoreExpression (io.shardingjdbc.core.parsing.parser.expression.SQLIgnoreExpression)1