Search in sources :

Example 1 with SQLParserException

use of com.dangdang.ddframe.rdb.sharding.exception.SQLParserException in project sharding-jdbc by dangdangdotcom.

the class MySQLSelectVisitor method visit.

/**
     * LIMIT 解析.
     * 
     * @param x LIMIT表达式
     * @return false 停止遍历AST
     */
@Override
public boolean visit(final MySqlSelectQueryBlock.Limit x) {
    if (getParseContext().getParseContextIndex() > 0) {
        return super.visit(x);
    }
    print("LIMIT ");
    int offset = 0;
    int offSetIndex = -1;
    if (null != x.getOffset()) {
        if (x.getOffset() instanceof SQLNumericLiteralExpr) {
            offset = ((SQLNumericLiteralExpr) x.getOffset()).getNumber().intValue();
            printToken(Limit.OFFSET_NAME, String.valueOf(offset));
            print(", ");
        } else {
            offset = ((Number) getParameters().get(((SQLVariantRefExpr) x.getOffset()).getIndex())).intValue();
            offSetIndex = ((SQLVariantRefExpr) x.getOffset()).getIndex();
            print("?, ");
        }
    }
    int rowCount;
    int rowCountIndex = -1;
    if (x.getRowCount() instanceof SQLNumericLiteralExpr) {
        rowCount = ((SQLNumericLiteralExpr) x.getRowCount()).getNumber().intValue();
        printToken(Limit.COUNT_NAME, String.valueOf(rowCount));
    } else {
        rowCount = ((Number) getParameters().get(((SQLVariantRefExpr) x.getRowCount()).getIndex())).intValue();
        rowCountIndex = ((SQLVariantRefExpr) x.getRowCount()).getIndex();
        print("?");
    }
    if (offset < 0 || rowCount < 0) {
        throw new SQLParserException("LIMIT offset and row count can not be a negative value");
    }
    // "LIMIT {rowCount} OFFSET {offset}" will transform to "LIMIT {offset}, {rowCount}".So exchange parameter index
    if (offSetIndex > -1 && rowCountIndex > -1 && offSetIndex > rowCountIndex) {
        int tmp = rowCountIndex;
        rowCountIndex = offSetIndex;
        offSetIndex = tmp;
    }
    getParseContext().getParsedResult().getMergeContext().setLimit(new Limit(offset, rowCount, offSetIndex, rowCountIndex));
    return false;
}
Also used : SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLParserException(com.dangdang.ddframe.rdb.sharding.exception.SQLParserException) Limit(com.dangdang.ddframe.rdb.sharding.parser.result.merger.Limit)

Example 2 with SQLParserException

use of com.dangdang.ddframe.rdb.sharding.exception.SQLParserException in project sharding-jdbc by dangdangdotcom.

the class SQLUtil method getTypeByStart.

/**
     * 根据SQL第一个单词判断SQL类型.
     * 
     * @param sql SQL语句
     * @return SQL类型
     */
public static SQLStatementType getTypeByStart(final String sql) {
    //TODO: Use new Lexer Util.
    Lexer lexer = new Lexer(sql);
    lexer.nextToken();
    while (true) {
        switch(lexer.token()) {
            case SELECT:
                return SQLStatementType.SELECT;
            case INSERT:
                return SQLStatementType.INSERT;
            case UPDATE:
                return SQLStatementType.UPDATE;
            case DELETE:
                return SQLStatementType.DELETE;
            case EOF:
                throw new SQLParserException("Unsupported SQL statement: [%s]", sql);
            default:
                lexer.nextToken();
        }
    }
}
Also used : Lexer(com.alibaba.druid.sql.parser.Lexer) SQLParserException(com.dangdang.ddframe.rdb.sharding.exception.SQLParserException)

Aggregations

SQLParserException (com.dangdang.ddframe.rdb.sharding.exception.SQLParserException)2 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)1 Lexer (com.alibaba.druid.sql.parser.Lexer)1 Limit (com.dangdang.ddframe.rdb.sharding.parser.result.merger.Limit)1