Search in sources :

Example 1 with MySQLToken

use of com.alibaba.cobar.parser.recognizer.mysql.MySQLToken in project cobar by alibaba.

the class MySQLLexer method nextToken.

public MySQLToken nextToken() throws SQLSyntaxErrorException {
    if (tokenCache2 != null) {
        tokenCache2 = null;
        return tokenCache;
    }
    if (tokenCache != null) {
        tokenCache = null;
        return token;
    }
    if (token == MySQLToken.EOF) {
        throw new SQLSyntaxErrorException("eof for sql is already reached, cannot get new token");
    }
    MySQLToken t;
    do {
        skipSeparator();
        t = nextTokenInternal();
    } while (inCStyleComment && inCStyleCommentIgnore || MySQLToken.PUNC_C_STYLE_COMMENT_END == t);
    return t;
}
Also used : SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) MySQLToken(com.alibaba.cobar.parser.recognizer.mysql.MySQLToken)

Example 2 with MySQLToken

use of com.alibaba.cobar.parser.recognizer.mysql.MySQLToken in project cobar by alibaba.

the class MySQLLexer method scanIdentifierFromNumber.

/**
 * NOTE: {@link MySQLToken#IDENTIFIER id} dosn't include <code>'.'</code>
 * for sake of performance issue (based on <i>shaojin.wensj</i>'s design).
 * However, it is not convenient for MySQL compatibility. e.g.
 * <code>".123f"</code> will be regarded as <code>".123"</code> and
 * <code>"f"</code> in MySQL, but in this {@link MySQLLexer}, it will be
 * <code>"."</code> and <code>"123f"</code> because <code>".123f"</code> may
 * be part of <code>"db1.123f"</code> and <code>"123f"</code> is the table
 * name.
 *
 * @param initSize how many char has already been consumed
 */
private void scanIdentifierFromNumber(int initOffset, int initSize) throws SQLSyntaxErrorException {
    offsetCache = initOffset;
    sizeCache = initSize;
    for (; CharTypes.isIdentifierChar(ch); ++sizeCache) {
        scanChar();
    }
    updateStringValue(sql, offsetCache, sizeCache);
    MySQLToken tok = keywods.getKeyword(stringValueUppercase);
    token = tok == null ? MySQLToken.IDENTIFIER : tok;
}
Also used : MySQLToken(com.alibaba.cobar.parser.recognizer.mysql.MySQLToken)

Example 3 with MySQLToken

use of com.alibaba.cobar.parser.recognizer.mysql.MySQLToken in project cobar by alibaba.

the class MySQLExprParser method expression.

/**
 * first token of this expression has been scanned, not yet consumed
 */
public Expression expression() throws SQLSyntaxErrorException {
    MySQLToken token = lexer.token();
    if (token == null) {
        token = lexer.nextToken();
    }
    if (token == EOF) {
        err("unexpected EOF");
    }
    Expression left = logicalOrExpression();
    if (lexer.token() == OP_ASSIGN) {
        lexer.nextToken();
        Expression right = expression();
        return new AssignmentExpression(left, right).setCacheEvalRst(cacheEvalRst);
    }
    return left;
}
Also used : AssignmentExpression(com.alibaba.cobar.parser.ast.expression.misc.AssignmentExpression) SubqueryAllExpression(com.alibaba.cobar.parser.ast.expression.misc.SubqueryAllExpression) MinusExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.MinusExpression) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) BitAndExpression(com.alibaba.cobar.parser.ast.expression.bit.BitAndExpression) ComparisionEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression) NegativeValueExpression(com.alibaba.cobar.parser.ast.expression.logical.NegativeValueExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) CastBinaryExpression(com.alibaba.cobar.parser.ast.expression.type.CastBinaryExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) BitInvertExpression(com.alibaba.cobar.parser.ast.expression.bit.BitInvertExpression) BitXORExpression(com.alibaba.cobar.parser.ast.expression.bit.BitXORExpression) FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) BitShiftExpression(com.alibaba.cobar.parser.ast.expression.bit.BitShiftExpression) BitOrExpression(com.alibaba.cobar.parser.ast.expression.bit.BitOrExpression) ComparisionGreaterThanOrEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanOrEqualsExpression) MatchExpression(com.alibaba.cobar.parser.ast.expression.primary.MatchExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) ComparisionGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanExpression) ComparisionLessOrGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessOrGreaterThanExpression) SubqueryAnyExpression(com.alibaba.cobar.parser.ast.expression.misc.SubqueryAnyExpression) RegexpExpression(com.alibaba.cobar.parser.ast.expression.string.RegexpExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) ArithmeticMultiplyExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticMultiplyExpression) ComparisionLessThanOrEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessThanOrEqualsExpression) ComparisionNullSafeEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) ArithmeticSubtractExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression) SoundsLikeExpression(com.alibaba.cobar.parser.ast.expression.string.SoundsLikeExpression) ArithmeticIntegerDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticIntegerDivideExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) LogicalNotExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalNotExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) AssignmentExpression(com.alibaba.cobar.parser.ast.expression.misc.AssignmentExpression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) LogicalXORExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalXORExpression) UserExpression(com.alibaba.cobar.parser.ast.expression.misc.UserExpression) ArithmeticAddExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression) ComparisionNotEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNotEqualsExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) ComparisionLessThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessThanExpression) ArithmeticModExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticModExpression) ArithmeticDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticDivideExpression) MySQLToken(com.alibaba.cobar.parser.recognizer.mysql.MySQLToken)

Example 4 with MySQLToken

use of com.alibaba.cobar.parser.recognizer.mysql.MySQLToken in project cobar by alibaba.

the class MySQLLexer method scanNumber.

/**
 * if first char is <code>.</code>, token may be {@link MySQLToken#PUNC_DOT}
 * if invalid char is presented after <code>.</code>
 */
protected void scanNumber() throws SQLSyntaxErrorException {
    offsetCache = curIndex;
    sizeCache = 1;
    final boolean fstDot = ch == '.';
    boolean dot = fstDot;
    boolean sign = false;
    int state = fstDot ? 1 : 0;
    for (; scanChar() != MySQLLexer.EOI; ++sizeCache) {
        switch(state) {
            case 0:
                if (CharTypes.isDigit(ch)) {
                } else if (ch == '.') {
                    dot = true;
                    state = 1;
                } else if (ch == 'e' || ch == 'E') {
                    state = 3;
                } else if (CharTypes.isIdentifierChar(ch)) {
                    scanIdentifierFromNumber(offsetCache, sizeCache);
                    return;
                } else {
                    token = MySQLToken.LITERAL_NUM_PURE_DIGIT;
                    return;
                }
                break;
            case 1:
                if (CharTypes.isDigit(ch)) {
                    state = 2;
                } else if (ch == 'e' || ch == 'E') {
                    state = 3;
                } else if (CharTypes.isIdentifierChar(ch) && fstDot) {
                    sizeCache = 1;
                    ch = sql[curIndex = offsetCache + 1];
                    token = MySQLToken.PUNC_DOT;
                    return;
                } else {
                    token = MySQLToken.LITERAL_NUM_MIX_DIGIT;
                    return;
                }
                break;
            case 2:
                if (CharTypes.isDigit(ch)) {
                } else if (ch == 'e' || ch == 'E') {
                    state = 3;
                } else if (CharTypes.isIdentifierChar(ch) && fstDot) {
                    sizeCache = 1;
                    ch = sql[curIndex = offsetCache + 1];
                    token = MySQLToken.PUNC_DOT;
                    return;
                } else {
                    token = MySQLToken.LITERAL_NUM_MIX_DIGIT;
                    return;
                }
                break;
            case 3:
                if (CharTypes.isDigit(ch)) {
                    state = 5;
                } else if (ch == '+' || ch == '-') {
                    sign = true;
                    state = 4;
                } else if (fstDot) {
                    sizeCache = 1;
                    ch = sql[curIndex = offsetCache + 1];
                    token = MySQLToken.PUNC_DOT;
                    return;
                } else if (!dot) {
                    if (CharTypes.isIdentifierChar(ch)) {
                        scanIdentifierFromNumber(offsetCache, sizeCache);
                    } else {
                        updateStringValue(sql, offsetCache, sizeCache);
                        MySQLToken tok = keywods.getKeyword(stringValueUppercase);
                        token = tok == null ? MySQLToken.IDENTIFIER : tok;
                    }
                    return;
                } else {
                    throw err("invalid char after '.' and 'e' for as part of number: " + ch);
                }
                break;
            case 4:
                if (CharTypes.isDigit(ch)) {
                    state = 5;
                    break;
                } else if (fstDot) {
                    sizeCache = 1;
                    ch = sql[curIndex = offsetCache + 1];
                    token = MySQLToken.PUNC_DOT;
                } else if (!dot) {
                    ch = sql[--curIndex];
                    --sizeCache;
                    updateStringValue(sql, offsetCache, sizeCache);
                    MySQLToken tok = keywods.getKeyword(stringValueUppercase);
                    token = tok == null ? MySQLToken.IDENTIFIER : tok;
                } else {
                    throw err("expect digit char after SIGN for 'e': " + ch);
                }
                return;
            case 5:
                if (CharTypes.isDigit(ch)) {
                    break;
                } else if (CharTypes.isIdentifierChar(ch)) {
                    if (fstDot) {
                        sizeCache = 1;
                        ch = sql[curIndex = offsetCache + 1];
                        token = MySQLToken.PUNC_DOT;
                    } else if (!dot) {
                        if (sign) {
                            ch = sql[curIndex = offsetCache];
                            scanIdentifierFromNumber(curIndex, 0);
                        } else {
                            scanIdentifierFromNumber(offsetCache, sizeCache);
                        }
                    } else {
                        token = MySQLToken.LITERAL_NUM_MIX_DIGIT;
                    }
                } else {
                    token = MySQLToken.LITERAL_NUM_MIX_DIGIT;
                }
                return;
        }
    }
    switch(state) {
        case 0:
            token = MySQLToken.LITERAL_NUM_PURE_DIGIT;
            return;
        case 1:
            if (fstDot) {
                token = MySQLToken.PUNC_DOT;
                return;
            }
        case 2:
        case 5:
            token = MySQLToken.LITERAL_NUM_MIX_DIGIT;
            return;
        case 3:
            if (fstDot) {
                sizeCache = 1;
                ch = sql[curIndex = offsetCache + 1];
                token = MySQLToken.PUNC_DOT;
            } else if (!dot) {
                updateStringValue(sql, offsetCache, sizeCache);
                MySQLToken tok = keywods.getKeyword(stringValueUppercase);
                token = tok == null ? MySQLToken.IDENTIFIER : tok;
            } else {
                throw err("expect digit char after SIGN for 'e': " + ch);
            }
            return;
        case 4:
            if (fstDot) {
                sizeCache = 1;
                ch = sql[curIndex = offsetCache + 1];
                token = MySQLToken.PUNC_DOT;
            } else if (!dot) {
                ch = sql[--curIndex];
                --sizeCache;
                updateStringValue(sql, offsetCache, sizeCache);
                MySQLToken tok = keywods.getKeyword(stringValueUppercase);
                token = tok == null ? MySQLToken.IDENTIFIER : tok;
            } else {
                throw err("expect digit char after SIGN for 'e': " + ch);
            }
            return;
    }
}
Also used : MySQLToken(com.alibaba.cobar.parser.recognizer.mysql.MySQLToken)

Aggregations

MySQLToken (com.alibaba.cobar.parser.recognizer.mysql.MySQLToken)4 Expression (com.alibaba.cobar.parser.ast.expression.Expression)1 ArithmeticAddExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression)1 ArithmeticDivideExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticDivideExpression)1 ArithmeticIntegerDivideExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticIntegerDivideExpression)1 ArithmeticModExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticModExpression)1 ArithmeticMultiplyExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticMultiplyExpression)1 ArithmeticSubtractExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression)1 MinusExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.MinusExpression)1 BitAndExpression (com.alibaba.cobar.parser.ast.expression.bit.BitAndExpression)1 BitInvertExpression (com.alibaba.cobar.parser.ast.expression.bit.BitInvertExpression)1 BitOrExpression (com.alibaba.cobar.parser.ast.expression.bit.BitOrExpression)1 BitShiftExpression (com.alibaba.cobar.parser.ast.expression.bit.BitShiftExpression)1 BitXORExpression (com.alibaba.cobar.parser.ast.expression.bit.BitXORExpression)1 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)1 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)1 ComparisionGreaterThanExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanExpression)1 ComparisionGreaterThanOrEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanOrEqualsExpression)1 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)1 ComparisionLessOrGreaterThanExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessOrGreaterThanExpression)1