Search in sources :

Example 16 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException in project cobar by alibaba.

the class MySQLDDLParser method tableOption.

private boolean tableOption(TableOptions options) throws SQLSyntaxErrorException {
    Identifier id = null;
    Expression expr = null;
    os: switch(lexer.token()) {
        case KW_CHARACTER:
            lexer.nextToken();
            match(KW_SET);
            if (lexer.token() == OP_EQUALS) {
                lexer.nextToken();
            }
            id = identifier();
            options.setCharSet(id);
            break;
        case KW_COLLATE:
            lexer.nextToken();
            if (lexer.token() == OP_EQUALS) {
                lexer.nextToken();
            }
            id = identifier();
            options.setCollation(id);
            break;
        case KW_DEFAULT:
            // | [DEFAULT] COLLATE [=] collation_name
            switch(lexer.nextToken()) {
                case KW_CHARACTER:
                    lexer.nextToken();
                    match(KW_SET);
                    if (lexer.token() == OP_EQUALS) {
                        lexer.nextToken();
                    }
                    id = identifier();
                    options.setCharSet(id);
                    break os;
                case KW_COLLATE:
                    lexer.nextToken();
                    if (lexer.token() == OP_EQUALS) {
                        lexer.nextToken();
                    }
                    id = identifier();
                    options.setCollation(id);
                    break os;
                case IDENTIFIER:
                    SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
                    if (si != null) {
                        switch(si) {
                            case CHARSET:
                                lexer.nextToken();
                                if (lexer.token() == OP_EQUALS) {
                                    lexer.nextToken();
                                }
                                id = identifier();
                                options.setCharSet(id);
                                break os;
                        }
                    }
                default:
                    lexer.addCacheToke(KW_DEFAULT);
                    return false;
            }
        case KW_INDEX:
            // | INDEX DIRECTORY [=] 'absolute path to directory'
            lexer.nextToken();
            if (lexer.token() == IDENTIFIER && "DIRECTORY".equals(lexer.stringValueUppercase())) {
                if (lexer.nextToken() == OP_EQUALS) {
                    lexer.nextToken();
                }
                options.setIndexDir((LiteralString) exprParser.expression());
                break;
            }
            lexer.addCacheToke(KW_INDEX);
            return true;
        case KW_UNION:
            // | UNION [=] (tbl_name[,tbl_name]...)
            if (lexer.nextToken() == OP_EQUALS) {
                lexer.nextToken();
            }
            match(PUNC_LEFT_PAREN);
            List<Identifier> union = new ArrayList<Identifier>(2);
            for (int j = 0; lexer.token() != PUNC_RIGHT_PAREN; ++j) {
                if (j > 0)
                    match(PUNC_COMMA);
                id = identifier();
                union.add(id);
            }
            match(PUNC_RIGHT_PAREN);
            options.setUnion(union);
            break os;
        case IDENTIFIER:
            SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
            if (si != null) {
                switch(si) {
                    case CHARSET:
                        // CHARSET [=] charset_name
                        lexer.nextToken();
                        if (lexer.token() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        id = identifier();
                        options.setCharSet(id);
                        break os;
                    case ENGINE:
                        // ENGINE [=] engine_name
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        id = identifier();
                        options.setEngine(id);
                        break os;
                    case AUTO_INCREMENT:
                        // | AUTO_INCREMENT [=] value
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        expr = exprParser.expression();
                        options.setAutoIncrement(expr);
                        break os;
                    case AVG_ROW_LENGTH:
                        // | AVG_ROW_LENGTH [=] value
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        expr = exprParser.expression();
                        options.setAvgRowLength(expr);
                        break os;
                    case CHECKSUM:
                        // | CHECKSUM [=] {0 | 1}
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        switch(lexer.token()) {
                            case LITERAL_BOOL_FALSE:
                                lexer.nextToken();
                                options.setCheckSum(false);
                            case LITERAL_BOOL_TRUE:
                                lexer.nextToken();
                                options.setCheckSum(true);
                                break;
                            case LITERAL_NUM_PURE_DIGIT:
                                int intVal = lexer.integerValue().intValue();
                                lexer.nextToken();
                                if (intVal == 0) {
                                    options.setCheckSum(false);
                                } else {
                                    options.setCheckSum(true);
                                }
                                break;
                            default:
                                throw new SQLSyntaxErrorException("table option of CHECKSUM error");
                        }
                        break os;
                    case DELAY_KEY_WRITE:
                        // | DELAY_KEY_WRITE [=] {0 | 1}
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        switch(lexer.token()) {
                            case LITERAL_BOOL_FALSE:
                                lexer.nextToken();
                                options.setDelayKeyWrite(false);
                            case LITERAL_BOOL_TRUE:
                                lexer.nextToken();
                                options.setDelayKeyWrite(true);
                                break;
                            case LITERAL_NUM_PURE_DIGIT:
                                int intVal = lexer.integerValue().intValue();
                                lexer.nextToken();
                                if (intVal == 0) {
                                    options.setDelayKeyWrite(false);
                                } else {
                                    options.setDelayKeyWrite(true);
                                }
                                break;
                            default:
                                throw new SQLSyntaxErrorException("table option of DELAY_KEY_WRITE error");
                        }
                        break os;
                    case COMMENT:
                        // | COMMENT [=] 'string'
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        options.setComment((LiteralString) exprParser.expression());
                        break os;
                    case CONNECTION:
                        // | CONNECTION [=] 'connect_string'
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        options.setConnection((LiteralString) exprParser.expression());
                        break os;
                    case DATA:
                        // | DATA DIRECTORY [=] 'absolute path to directory'
                        lexer.nextToken();
                        matchIdentifier("DIRECTORY");
                        if (lexer.token() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        options.setDataDir((LiteralString) exprParser.expression());
                        break os;
                    case INSERT_METHOD:
                        // | INSERT_METHOD [=] { NO | FIRST | LAST }
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        switch(matchIdentifier("NO", "FIRST", "LAST")) {
                            case 0:
                                options.setInsertMethod(TableOptions.InsertMethod.NO);
                                break;
                            case 1:
                                options.setInsertMethod(TableOptions.InsertMethod.FIRST);
                                break;
                            case 2:
                                options.setInsertMethod(TableOptions.InsertMethod.LAST);
                                break;
                        }
                        break os;
                    case KEY_BLOCK_SIZE:
                        // | KEY_BLOCK_SIZE [=] value
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        options.setKeyBlockSize(exprParser.expression());
                        break os;
                    case MAX_ROWS:
                        // | MAX_ROWS [=] value
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        options.setMaxRows(exprParser.expression());
                        break os;
                    case MIN_ROWS:
                        // | MIN_ROWS [=] value
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        options.setMinRows(exprParser.expression());
                        break os;
                    case PACK_KEYS:
                        // | PACK_KEYS [=] {0 | 1 | DEFAULT}
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        switch(lexer.token()) {
                            case LITERAL_BOOL_FALSE:
                                lexer.nextToken();
                                options.setPackKeys(TableOptions.PackKeys.FALSE);
                                break;
                            case LITERAL_BOOL_TRUE:
                                lexer.nextToken();
                                options.setPackKeys(TableOptions.PackKeys.TRUE);
                                break;
                            case LITERAL_NUM_PURE_DIGIT:
                                int intVal = lexer.integerValue().intValue();
                                lexer.nextToken();
                                if (intVal == 0) {
                                    options.setPackKeys(TableOptions.PackKeys.FALSE);
                                } else {
                                    options.setPackKeys(TableOptions.PackKeys.TRUE);
                                }
                                break;
                            case KW_DEFAULT:
                                lexer.nextToken();
                                options.setPackKeys(TableOptions.PackKeys.DEFAULT);
                                break;
                            default:
                                throw new SQLSyntaxErrorException("table option of PACK_KEYS error");
                        }
                        break os;
                    case PASSWORD:
                        // | PASSWORD [=] 'string'
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        options.setPassword((LiteralString) exprParser.expression());
                        break os;
                    case ROW_FORMAT:
                        // {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT}
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        switch(lexer.token()) {
                            case KW_DEFAULT:
                                lexer.nextToken();
                                options.setRowFormat(TableOptions.RowFormat.DEFAULT);
                                break os;
                            case IDENTIFIER:
                                SpecialIdentifier sid = specialIdentifiers.get(lexer.stringValueUppercase());
                                if (sid != null) {
                                    switch(sid) {
                                        case DYNAMIC:
                                            lexer.nextToken();
                                            options.setRowFormat(TableOptions.RowFormat.DYNAMIC);
                                            break os;
                                        case FIXED:
                                            lexer.nextToken();
                                            options.setRowFormat(TableOptions.RowFormat.FIXED);
                                            break os;
                                        case COMPRESSED:
                                            lexer.nextToken();
                                            options.setRowFormat(TableOptions.RowFormat.COMPRESSED);
                                            break os;
                                        case REDUNDANT:
                                            lexer.nextToken();
                                            options.setRowFormat(TableOptions.RowFormat.REDUNDANT);
                                            break os;
                                        case COMPACT:
                                            lexer.nextToken();
                                            options.setRowFormat(TableOptions.RowFormat.COMPACT);
                                            break os;
                                    }
                                }
                            default:
                                throw new SQLSyntaxErrorException("table option of ROW_FORMAT error");
                        }
                }
            }
        default:
            return false;
    }
    return true;
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString) Expression(com.alibaba.cobar.parser.ast.expression.Expression) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 17 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException 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 18 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException in project cobar by alibaba.

the class MySQLExprParser method ordinaryFunction.

/**
 * id has been consumed. id must be a function name. current token must be
 * {@link MySQLToken#PUNC_LEFT_PAREN}
 *
 * @param idUpper must be name of a function
 * @return never null
 */
private FunctionExpression ordinaryFunction(String id, String idUpper) throws SQLSyntaxErrorException {
    idUpper = Identifier.unescapeName(idUpper);
    match(PUNC_LEFT_PAREN);
    FunctionExpression funcExpr;
    if (lexer.token() == PUNC_RIGHT_PAREN) {
        lexer.nextToken();
        funcExpr = functionManager.createFunctionExpression(idUpper, null);
    } else {
        List<Expression> args = expressionList(new LinkedList<Expression>());
        funcExpr = functionManager.createFunctionExpression(idUpper, args);
    }
    if (funcExpr == null) {
        throw new SQLSyntaxErrorException(id + "() is not a function");
    }
    funcExpr.setCacheEvalRst(cacheEvalRst);
    return funcExpr;
}
Also used : FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) 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) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException)

Example 19 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException in project cobar by alibaba.

the class SimpleHintParser method process.

@Override
public void process(CobarHint hint, String hintName, String sql) throws SQLSyntaxErrorException {
    Object value = parsePrimary(hint, sql);
    if (value instanceof Long)
        value = ((Long) value).intValue();
    Map<String, Object> properties = new HashMap<String, Object>(1, 1);
    properties.put(hintName, value);
    try {
        ParameterMapping.mapping(hint, properties);
    } catch (Throwable t) {
        throw new SQLSyntaxErrorException(t);
    }
}
Also used : HashMap(java.util.HashMap) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException)

Example 20 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException in project cobar by alibaba.

the class SQLParserDelegateTest method testProperlyEnd.

public void testProperlyEnd() throws SQLSyntaxErrorException {
    String sql = "select * from tb1;";
    SQLStatement stmt = SQLParserDelegate.parse(sql);
    Assert.assertEquals(DMLSelectStatement.class, stmt.getClass());
    sql = "select * from tb1 ;;;  ";
    stmt = SQLParserDelegate.parse(sql);
    Assert.assertEquals(DMLSelectStatement.class, stmt.getClass());
    sql = "select * from tb1 /***/  ";
    stmt = SQLParserDelegate.parse(sql);
    Assert.assertEquals(DMLSelectStatement.class, stmt.getClass());
    sql = "select * from tb1 ,  ";
    try {
        stmt = SQLParserDelegate.parse(sql);
        Assert.fail("should detect inproperly end");
    } catch (SQLSyntaxErrorException e) {
    }
    sql = "select * from tb1 ;,  ";
    try {
        stmt = SQLParserDelegate.parse(sql);
        Assert.fail("should detect inproperly end");
    } catch (SQLSyntaxErrorException e) {
    }
}
Also used : SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) SQLStatement(com.alibaba.cobar.parser.ast.stmt.SQLStatement)

Aggregations

SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)55 Test (org.junit.Test)14 Test (org.testng.annotations.Test)14 BaseTest (util.BaseTest)14 DataAccessException (org.jooq.exception.DataAccessException)9 HashMap (java.util.HashMap)7 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)6 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)6 TableConfig (io.mycat.config.model.TableConfig)6 SQLNonTransientException (java.sql.SQLNonTransientException)6 Expression (com.alibaba.cobar.parser.ast.expression.Expression)5 Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)4 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)4 SchemaConfig (io.mycat.config.model.SchemaConfig)4 DruidShardingParseInfo (io.mycat.route.parser.druid.DruidShardingParseInfo)4 MycatSchemaStatVisitor (io.mycat.route.parser.druid.MycatSchemaStatVisitor)4 MycatStatementParser (io.mycat.route.parser.druid.MycatStatementParser)4 SQLException (java.sql.SQLException)4 DataSource (javax.sql.DataSource)4