Search in sources :

Example 66 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.

the class OdpsLexer method scanIdentifier.

public void scanIdentifier() {
    final char first = ch;
    if (first == '`') {
        mark = pos;
        bufPos = 1;
        char ch;
        for (; ; ) {
            ch = charAt(++pos);
            if (ch == '`') {
                bufPos++;
                ch = charAt(++pos);
                break;
            } else if (ch == EOI) {
                throw new ParserException("illegal identifier");
            }
            bufPos++;
            continue;
        }
        this.ch = charAt(pos);
        stringVal = subString(mark, bufPos);
        token = Token.IDENTIFIER;
        return;
    }
    final boolean firstFlag = isFirstIdentifierChar(first);
    if (!firstFlag) {
        throw new ParserException("illegal identifier");
    }
    mark = pos;
    bufPos = 1;
    char ch;
    for (; ; ) {
        ch = charAt(++pos);
        if (!isIdentifierChar(ch)) {
            break;
        }
        bufPos++;
        continue;
    }
    this.ch = charAt(pos);
    if (ch == '@') {
        // for user identifier, like email, xx@alibaba-inc.com
        bufPos++;
        for (; ; ) {
            ch = charAt(++pos);
            if (ch != '-' && ch != '.' && !isIdentifierChar(ch)) {
                break;
            }
            bufPos++;
            continue;
        }
    }
    this.ch = charAt(pos);
    stringVal = addSymbol();
    Token tok = keywods.getKeyword(stringVal);
    if (tok != null) {
        token = tok;
    } else {
        token = Token.IDENTIFIER;
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) Token(com.alibaba.druid.sql.parser.Token)

Example 67 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.

the class OdpsStatementParser method parseStaticClause.

protected OdpsStatisticClause parseStaticClause() {
    if (identifierEquals("TABLE_COUNT")) {
        lexer.nextToken();
        return new OdpsStatisticClause.TableCount();
    } else if (identifierEquals("NULL_VALUE")) {
        lexer.nextToken();
        OdpsStatisticClause.NullValue null_value = new OdpsStatisticClause.NullValue();
        null_value.setColumn(this.exprParser.name());
        return null_value;
    } else if (identifierEquals("COLUMN_SUM")) {
        lexer.nextToken();
        OdpsStatisticClause.ColumnSum column_sum = new OdpsStatisticClause.ColumnSum();
        column_sum.setColumn(this.exprParser.name());
        return column_sum;
    } else if (identifierEquals("COLUMN_MAX")) {
        lexer.nextToken();
        OdpsStatisticClause.ColumnMax column_max = new OdpsStatisticClause.ColumnMax();
        column_max.setColumn(this.exprParser.name());
        return column_max;
    } else if (identifierEquals("COLUMN_MIN")) {
        lexer.nextToken();
        OdpsStatisticClause.ColumnMin column_min = new OdpsStatisticClause.ColumnMin();
        column_min.setColumn(this.exprParser.name());
        return column_min;
    } else if (identifierEquals("EXPRESSION_CONDITION")) {
        lexer.nextToken();
        OdpsStatisticClause.ExpressionCondition expr_condition = new OdpsStatisticClause.ExpressionCondition();
        expr_condition.setExpr(this.exprParser.expr());
        return expr_condition;
    } else {
        throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) OdpsStatisticClause(com.alibaba.druid.sql.dialect.odps.ast.OdpsStatisticClause)

Example 68 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.

the class MySqlSelectIntoParser method query.

@Override
public SQLSelectQuery query() {
    if (lexer.token() == (Token.LPAREN)) {
        lexer.nextToken();
        SQLSelectQuery select = query();
        accept(Token.RPAREN);
        return queryRest(select);
    }
    MySqlSelectQueryBlock queryBlock = new MySqlSelectQueryBlock();
    if (lexer.token() == Token.SELECT) {
        lexer.nextToken();
        if (lexer.token() == Token.HINT) {
            this.exprParser.parseHints(queryBlock.getHints());
        }
        if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
        }
        if (lexer.token() == (Token.DISTINCT)) {
            queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
            lexer.nextToken();
        } else if (identifierEquals("DISTINCTROW")) {
            queryBlock.setDistionOption(SQLSetQuantifier.DISTINCTROW);
            lexer.nextToken();
        } else if (lexer.token() == (Token.ALL)) {
            queryBlock.setDistionOption(SQLSetQuantifier.ALL);
            lexer.nextToken();
        }
        if (identifierEquals("HIGH_PRIORITY")) {
            queryBlock.setHignPriority(true);
            lexer.nextToken();
        }
        if (identifierEquals("STRAIGHT_JOIN")) {
            queryBlock.setStraightJoin(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_SMALL_RESULT")) {
            queryBlock.setSmallResult(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_BIG_RESULT")) {
            queryBlock.setBigResult(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_BUFFER_RESULT")) {
            queryBlock.setBufferResult(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_CACHE")) {
            queryBlock.setCache(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_NO_CACHE")) {
            queryBlock.setCache(false);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_CALC_FOUND_ROWS")) {
            queryBlock.setCalcFoundRows(true);
            lexer.nextToken();
        }
        parseSelectList(queryBlock);
        argsList = parseIntoArgs();
    }
    parseFrom(queryBlock);
    parseWhere(queryBlock);
    parseGroupBy(queryBlock);
    queryBlock.setOrderBy(this.exprParser.parseOrderBy());
    if (lexer.token() == Token.LIMIT) {
        queryBlock.setLimit(this.exprParser.parseLimit());
    }
    if (lexer.token() == Token.PROCEDURE) {
        lexer.nextToken();
        throw new ParserException("TODO");
    }
    parseInto(queryBlock);
    if (lexer.token() == Token.FOR) {
        lexer.nextToken();
        accept(Token.UPDATE);
        queryBlock.setForUpdate(true);
    }
    if (lexer.token() == Token.LOCK) {
        lexer.nextToken();
        accept(Token.IN);
        acceptIdentifier("SHARE");
        acceptIdentifier("MODE");
        queryBlock.setLockInShareMode(true);
    }
    return queryRest(queryBlock);
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)

Example 69 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.

the class MySqlSelectParser method query.

@Override
public SQLSelectQuery query() {
    if (lexer.token() == (Token.LPAREN)) {
        lexer.nextToken();
        SQLSelectQuery select = query();
        accept(Token.RPAREN);
        return queryRest(select);
    }
    MySqlSelectQueryBlock queryBlock = new MySqlSelectQueryBlock();
    if (lexer.token() == Token.SELECT) {
        lexer.nextToken();
        if (lexer.token() == Token.HINT) {
            this.exprParser.parseHints(queryBlock.getHints());
        }
        if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
        }
        if (lexer.token() == (Token.DISTINCT)) {
            queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
            lexer.nextToken();
        } else if (identifierEquals("DISTINCTROW")) {
            queryBlock.setDistionOption(SQLSetQuantifier.DISTINCTROW);
            lexer.nextToken();
        } else if (lexer.token() == (Token.ALL)) {
            queryBlock.setDistionOption(SQLSetQuantifier.ALL);
            lexer.nextToken();
        }
        if (identifierEquals("HIGH_PRIORITY")) {
            queryBlock.setHignPriority(true);
            lexer.nextToken();
        }
        if (identifierEquals("STRAIGHT_JOIN")) {
            queryBlock.setStraightJoin(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_SMALL_RESULT")) {
            queryBlock.setSmallResult(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_BIG_RESULT")) {
            queryBlock.setBigResult(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_BUFFER_RESULT")) {
            queryBlock.setBufferResult(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_CACHE")) {
            queryBlock.setCache(true);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_NO_CACHE")) {
            queryBlock.setCache(false);
            lexer.nextToken();
        }
        if (identifierEquals("SQL_CALC_FOUND_ROWS")) {
            queryBlock.setCalcFoundRows(true);
            lexer.nextToken();
        }
        parseSelectList(queryBlock);
        parseInto(queryBlock);
    }
    parseFrom(queryBlock);
    parseWhere(queryBlock);
    parseGroupBy(queryBlock);
    queryBlock.setOrderBy(this.exprParser.parseOrderBy());
    if (lexer.token() == Token.LIMIT) {
        queryBlock.setLimit(this.exprParser.parseLimit());
    }
    if (lexer.token() == Token.PROCEDURE) {
        lexer.nextToken();
        throw new ParserException("TODO");
    }
    parseInto(queryBlock);
    if (lexer.token() == Token.FOR) {
        lexer.nextToken();
        accept(Token.UPDATE);
        queryBlock.setForUpdate(true);
        if (identifierEquals("NO_WAIT")) {
            lexer.nextToken();
            queryBlock.setNoWait(true);
        } else if (identifierEquals("WAIT")) {
            lexer.nextToken();
            SQLExpr waitTime = this.exprParser.primary();
            queryBlock.setWaitTime(waitTime);
        }
    }
    if (lexer.token() == Token.LOCK) {
        lexer.nextToken();
        accept(Token.IN);
        acceptIdentifier("SHARE");
        acceptIdentifier("MODE");
        queryBlock.setLockInShareMode(true);
    }
    return queryRest(queryBlock);
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 70 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.

the class OracleExprParser method parseDataType.

public SQLDataType parseDataType() {
    if (lexer.token() == Token.CONSTRAINT || lexer.token() == Token.COMMA) {
        return null;
    }
    if (lexer.token() == Token.DEFAULT || lexer.token() == Token.NOT || lexer.token() == Token.NULL) {
        return null;
    }
    if (lexer.token() == Token.INTERVAL) {
        lexer.nextToken();
        if (identifierEquals("YEAR")) {
            lexer.nextToken();
            OracleDataTypeIntervalYear interval = new OracleDataTypeIntervalYear();
            if (lexer.token() == Token.LPAREN) {
                lexer.nextToken();
                interval.addArgument(this.expr());
                accept(Token.RPAREN);
            }
            accept(Token.TO);
            acceptIdentifier("MONTH");
            return interval;
        } else {
            acceptIdentifier("DAY");
            OracleDataTypeIntervalDay interval = new OracleDataTypeIntervalDay();
            if (lexer.token() == Token.LPAREN) {
                lexer.nextToken();
                interval.addArgument(this.expr());
                accept(Token.RPAREN);
            }
            accept(Token.TO);
            acceptIdentifier("SECOND");
            if (lexer.token() == Token.LPAREN) {
                lexer.nextToken();
                interval.getFractionalSeconds().add(this.expr());
                accept(Token.RPAREN);
            }
            return interval;
        }
    }
    String typeName;
    if (identifierEquals("LONG")) {
        lexer.nextToken();
        acceptIdentifier("RAW");
        typeName = "LONG RAW";
    } else {
        SQLName typeExpr = name();
        typeName = typeExpr.toString();
    }
    if ("TIMESTAMP".equalsIgnoreCase(typeName)) {
        OracleDataTypeTimestamp timestamp = new OracleDataTypeTimestamp();
        if (lexer.token() == Token.LPAREN) {
            lexer.nextToken();
            timestamp.addArgument(this.expr());
            accept(Token.RPAREN);
        }
        if (lexer.token() == Token.WITH) {
            lexer.nextToken();
            if (identifierEquals("LOCAL")) {
                lexer.nextToken();
                timestamp.setWithLocalTimeZone(true);
            } else {
                timestamp.setWithTimeZone(true);
            }
            acceptIdentifier("TIME");
            acceptIdentifier("ZONE");
        }
        return timestamp;
    }
    if (isCharType(typeName)) {
        SQLCharacterDataType charType = new SQLCharacterDataType(typeName);
        if (lexer.token() == Token.LPAREN) {
            lexer.nextToken();
            charType.addArgument(this.expr());
            if (identifierEquals("CHAR")) {
                lexer.nextToken();
                charType.setCharType(SQLCharacterDataType.CHAR_TYPE_CHAR);
            } else if (identifierEquals("BYTE")) {
                lexer.nextToken();
                charType.setCharType(SQLCharacterDataType.CHAR_TYPE_BYTE);
            }
            accept(Token.RPAREN);
        }
        return parseCharTypeRest(charType);
    }
    if (lexer.token() == Token.PERCENT) {
        lexer.nextToken();
        if (identifierEquals("TYPE")) {
            lexer.nextToken();
            typeName += "%TYPE";
        } else if (identifierEquals("ROWTYPE")) {
            lexer.nextToken();
            typeName += "%ROWTYPE";
        } else {
            throw new ParserException("syntax error : " + lexer.token() + " " + lexer.stringVal());
        }
    }
    SQLDataType dataType = new SQLDataTypeImpl(typeName);
    return parseDataTypeRest(dataType);
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) OracleDataTypeIntervalYear(com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalYear) OracleDataTypeTimestamp(com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeTimestamp) SQLDataType(com.alibaba.druid.sql.ast.SQLDataType) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLDataTypeImpl(com.alibaba.druid.sql.ast.SQLDataTypeImpl) OracleDataTypeIntervalDay(com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalDay)

Aggregations

ParserException (com.alibaba.druid.sql.parser.ParserException)98 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)25 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)18 SQLName (com.alibaba.druid.sql.ast.SQLName)16 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)12 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)10 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)9 Token (com.alibaba.druid.sql.parser.Token)9 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)5 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)5 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)5 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)5 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)4 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)4 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)4 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)4 OracleConstraint (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleConstraint)4 DbType (com.alibaba.druid.DbType)3 SQLDeclareItem (com.alibaba.druid.sql.ast.SQLDeclareItem)3 SQLPartitionByHash (com.alibaba.druid.sql.ast.SQLPartitionByHash)3