Search in sources :

Example 51 with ParserException

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

the class OdpsLexerTest method test_error_1.

public void test_error_1() throws Exception {
    String str = "`aaa\n${PN}_events";
    OdpsLexer lexer = new OdpsLexer(str);
    int pos = lexer.pos();
    try {
        lexer.nextToken();
    } catch (ParserException error) {
        lexer.skipToNextLineOrParameter(pos);
        lexer.nextToken();
    }
    assertEquals(Token.IDENTIFIER, lexer.token());
    assertEquals("${PN}_events", lexer.stringVal());
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) OdpsLexer(com.alibaba.druid.sql.dialect.odps.parser.OdpsLexer)

Example 52 with ParserException

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

the class PGCommentTest method test_0.

public void test_0() {
    String sql = "/*multiline comment test" + ".\r\n multiline comment test select * from a;";
    PGSQLStatementParser parser;
    try {
        parser = new PGSQLStatementParser(sql);
        parser.parseStatementList();
    } catch (Exception e) {
        assertTrue(e instanceof ParserException);
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) ParserException(com.alibaba.druid.sql.parser.ParserException) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Example 53 with ParserException

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

the class MySqlExprParser method parseColumnRest.

public SQLColumnDefinition parseColumnRest(SQLColumnDefinition column) {
    if (lexer.token() == Token.ON) {
        lexer.nextToken();
        accept(Token.UPDATE);
        SQLExpr expr = this.expr();
        column.setOnUpdate(expr);
    }
    if (identifierEquals("CHARSET")) {
        lexer.nextToken();
        MySqlCharExpr charSetCollateExpr = new MySqlCharExpr();
        charSetCollateExpr.setCharset(lexer.stringVal());
        lexer.nextToken();
        if (identifierEquals("COLLATE")) {
            lexer.nextToken();
            charSetCollateExpr.setCollate(lexer.stringVal());
            lexer.nextToken();
        }
        column.setCharsetExpr(charSetCollateExpr);
        return parseColumnRest(column);
    }
    if (identifierEquals("AUTO_INCREMENT")) {
        lexer.nextToken();
        column.setAutoIncrement(true);
        return parseColumnRest(column);
    }
    if (identifierEquals("precision") && column.getDataType().getName().equalsIgnoreCase("double")) {
        lexer.nextToken();
    }
    if (lexer.token() == Token.PARTITION) {
        throw new ParserException("syntax error " + lexer.token() + " " + lexer.stringVal());
    }
    if (identifierEquals("STORAGE")) {
        lexer.nextToken();
        SQLExpr expr = expr();
        column.setStorage(expr);
    }
    if (lexer.token() == Token.AS) {
        lexer.nextToken();
        accept(Token.LPAREN);
        SQLExpr expr = expr();
        column.setAsExpr(expr);
        accept(Token.RPAREN);
    }
    if (identifierEquals("STORED")) {
        lexer.nextToken();
        column.setSorted(true);
    }
    if (identifierEquals("VIRTUAL")) {
        lexer.nextToken();
        column.setVirtual(true);
    }
    super.parseColumnRest(column);
    return column;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) MySqlCharExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 54 with ParserException

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

the class MySqlExprParser method parseReferenceOption.

protected Option parseReferenceOption() {
    Option option;
    if (lexer.token() == Token.RESTRICT || identifierEquals("RESTRICT")) {
        option = Option.RESTRICT;
        lexer.nextToken();
    } else if (identifierEquals("CASCADE")) {
        option = Option.CASCADE;
        lexer.nextToken();
    } else if (lexer.token() == Token.SET) {
        lexer.nextToken();
        accept(Token.NULL);
        option = Option.SET_NULL;
    } else if (identifierEquals("ON")) {
        lexer.nextToken();
        if (identifierEquals("ACTION")) {
            option = Option.NO_ACTION;
            lexer.nextToken();
        } else {
            throw new ParserException("syntax error, expect ACTION, actual " + lexer.token() + " " + lexer.stringVal());
        }
    } else {
        throw new ParserException("syntax error, expect ACTION, actual " + lexer.token() + " " + lexer.stringVal());
    }
    return option;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) Option(com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey.Option)

Example 55 with ParserException

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

the class MySqlLexer method scanIdentifier.

public void scanIdentifier() {
    final char first = ch;
    if (ch == '`') {
        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 tok = keywods.getKeyword(stringVal);
        if (tok != null) {
            token = tok;
        } else {
            token = Token.IDENTIFIER;
        }
    } else {
        final boolean firstFlag = isFirstIdentifierChar(first);
        if (!firstFlag) {
            throw new ParserException("illegal identifier");
        }
        mark = pos;
        bufPos = 1;
        char ch = '\0', last_ch;
        for (; ; ) {
            last_ch = ch;
            ch = charAt(++pos);
            if (!isIdentifierChar(ch)) {
                if (ch == '-' && pos < text.length() - 1) {
                    if (mark > 0 && text.charAt(mark - 1) == '.') {
                        break;
                    }
                    char next_char = text.charAt(pos + 1);
                    if (isIdentifierChar(next_char)) {
                        bufPos++;
                        continue;
                    }
                }
                if (last_ch == '-' && charAt(pos - 2) != '-') {
                    ch = last_ch;
                    bufPos--;
                    pos--;
                }
                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)

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