java.sql.SQLSyntaxErrorException Java Examples

The following examples show how to use java.sql.SQLSyntaxErrorException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MySQLLexer.java    From tddl with Apache License 2.0 6 votes vote down vote up
/**
 * first <code>@@</code> is included
 */
protected void scanSystemVariable() throws SQLSyntaxErrorException {
    if (ch != '@' || sql[curIndex + 1] != '@') throw err("first char must be @@");
    offsetCache = curIndex + 2;
    sizeCache = 0;
    scanChar(2);
    if (ch == '`') {
        for (++sizeCache;; ++sizeCache) {
            if (scanChar() == '`') {
                ++sizeCache;
                if (scanChar() != '`') {
                    break;
                }
            }
        }
    } else {
        for (; CharTypes.isIdentifierChar(ch); ++sizeCache) {
            scanChar();
        }
    }
    updateStringValue(sql, offsetCache, sizeCache);
    token = MySQLToken.SYS_VAR;
}
 
Example #2
Source File: TableHandler.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
@Override
public SQLStatement preHandle(DumpFileContext context, String stmt) throws SQLSyntaxErrorException {
    SQLStatement sqlStatement = RouteStrategyFactory.getRouteStrategy().parserSQL(stmt);
    String tableName;
    if (sqlStatement instanceof MySqlCreateTableStatement) {
        tableName = StringUtil.removeBackQuote(((MySqlCreateTableStatement) sqlStatement).getTableSource().getName().getSimpleName());
        context.setTable(tableName);
        if (context.getTableType() == TableType.DEFAULT) {
            return null;
        }
        boolean isChanged = preHandleCreateTable(context, sqlStatement);
        return isChanged ? sqlStatement : null;
    } else if (sqlStatement instanceof SQLDropTableStatement) {
        tableName = StringUtil.removeBackQuote(((SQLDropTableStatement) sqlStatement).getTableSources().get(0).getName().getSimpleName());
        context.setTable(tableName);
    } else if (sqlStatement instanceof MySqlLockTableStatement) {
        tableName = StringUtil.removeBackQuote(((MySqlLockTableStatement) sqlStatement).getTableSource().getName().getSimpleName());
        context.setTable(tableName);
    }
    return null;
}
 
Example #3
Source File: MySQLExprParser.java    From tddl with Apache License 2.0 6 votes vote down vote up
/**
 * first <code>'('</code> has been consumed. At least one element. Consume last ')' after invocation <br/>
 * <code>'(' expr (',' expr)* ')'</code>
 */
private List<Expression> expressionList(List<Expression> exprList) throws SQLSyntaxErrorException {
    for (;;) {
        Expression expr = expression();
        exprList.add(expr);
        switch (lexer.token()) {
            case PUNC_COMMA:
                lexer.nextToken();
                break;
            case PUNC_RIGHT_PAREN:
                lexer.nextToken();
                return exprList;
            default:
                throw err("unexpected token: " + lexer.token());
        }
    }
}
 
Example #4
Source File: MySQLDMLParser.java    From mybatis-shard with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @return null if there is no hint
 */
private List<IndexHint> hintList() throws SQLSyntaxErrorException {
    IndexHint hint = hint();
    if (hint == null)
        return null;
    List<IndexHint> list;
    IndexHint hint2 = hint();
    if (hint2 == null) {
        list = new ArrayList<IndexHint>(1);
        list.add(hint);
        return list;
    }
    list = new LinkedList<IndexHint>();
    list.add(hint);
    list.add(hint2);
    for (; (hint2 = hint()) != null; list.add(hint2));
    return list;
}
 
Example #5
Source File: MySQLExprParser.java    From mybatis-shard with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * first <code>'('</code> has been consumed. At least one element. Consume
 * last ')' after invocation <br/>
 * <code>'(' expr (',' expr)* ')'</code>
 */
private List<Expression> expressionList(List<Expression> exprList) throws SQLSyntaxErrorException {
    for (;;) {
        Expression expr = expression();
        exprList.add(expr);
        switch (lexer.token()) {
        case PUNC_COMMA:
            lexer.nextToken();
            break;
        case PUNC_RIGHT_PAREN:
            lexer.nextToken();
            return exprList;
        default:
            throw err("unexpected token: " + lexer.token());
        }
    }
}
 
Example #6
Source File: MySQLDMLSelectParser.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
private List<Pair<Expression, String>> selectExprList() throws SQLSyntaxErrorException {
    Expression expr = exprParser.expression();
    String alias = as();
    List<Pair<Expression, String>> list;
    if (lexer.token() == PUNC_COMMA) {
        list = new LinkedList<Pair<Expression, String>>();
        list.add(new Pair<Expression, String>(expr, alias));
    } else {
        list = new ArrayList<Pair<Expression, String>>(1);
        list.add(new Pair<Expression, String>(expr, alias));
        return list;
    }
    for (; lexer.token() == PUNC_COMMA; list.add(new Pair<Expression, String>(expr, alias))) {
        lexer.nextToken();
        expr = exprParser.expression();
        alias = as();
    }
    return list;
}
 
Example #7
Source File: DefaultRouteStrategy.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
@Override
public SQLStatement parserSQL(String originSql) throws SQLSyntaxErrorException {
    SQLStatementParser parser = new MySqlStatementParser(originSql);

    /**
     * thrown SQL SyntaxError if parser error
     */
    try {
        List<SQLStatement> list = parser.parseStatementList();
        if (list.size() > 1) {
            throw new SQLSyntaxErrorException("MultiQueries is not supported,use single query instead ");
        }
        return list.get(0);
    } catch (Exception t) {
        LOGGER.info("routeNormalSqlWithAST", t);
        if (t.getMessage() != null) {
            throw new SQLSyntaxErrorException(t.getMessage());
        } else {
            throw new SQLSyntaxErrorException(t);
        }
    }
}
 
Example #8
Source File: HintSQLHandler.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
@Override
public RouteResultset route(SchemaConfig schema, int sqlType, String realSQL, ServerConnection sc,
                            LayerCachePool cachePool, String hintSQLValue, int hintSqlType, Map hintMap)
        throws SQLException {

    RouteResultset rrs = routeStrategy.route(schema, hintSqlType,
            hintSQLValue, sc, cachePool);

    if (rrs.isNeedOptimizer()) {
        throw new SQLSyntaxErrorException("Complex SQL not supported in hint");
    }
    // replace the sql of RRS
    if (ServerParse.CALL == sqlType) {
        rrs.setCallStatement(true);
    }

    RouteResultsetNode[] oldRsNodes = rrs.getNodes();
    RouteResultsetNode[] newRrsNodes = new RouteResultsetNode[oldRsNodes.length];
    for (int i = 0; i < newRrsNodes.length; i++) {
        newRrsNodes[i] = new RouteResultsetNode(oldRsNodes[i].getName(), sqlType, realSQL);
    }
    rrs.setNodes(newRrsNodes);

    return rrs;
}
 
Example #9
Source File: MySqlExprVisitor.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(Identifier node) {
    String name = node.getIdTextUpUnescape();
    if (ISequenceVal.SEQ_NEXTVAL.equals(name)) {
        if (node.getParent() == null) {
            throw new TddlNestableRuntimeException(new SQLSyntaxErrorException("SEQUENCE NAME IS NOT EMPTY."));
        }
        name = node.getParent().getIdTextUpUnescape();
        this.columnOrValue = ASTNodeFactory.getInstance().createSequenceValue(name);
    } else {
        IColumn column = ASTNodeFactory.getInstance().createColumn();
        if (node.getParent() != null) { // table.column
            column.setTableName(node.getParent().getIdTextUpUnescape());
        }
        column.setColumnName(node.getIdTextUpUnescape());
        this.columnOrValue = column;
    }
}
 
Example #10
Source File: MySQLLexer.java    From heisenberg with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #11
Source File: MySQLDMLParser.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
/**
 * @return argument itself if there is no union
 */
protected DMLQueryStatement buildUnionSelect(DMLSelectStatement select) throws SQLSyntaxErrorException {
    if (lexer.token() != KW_UNION) {
        return select;
    }
    DMLSelectUnionStatement union = new DMLSelectUnionStatement(select);
    for (; lexer.token() == KW_UNION;) {
        lexer.nextToken();
        boolean isAll = false;
        switch (lexer.token()) {
            case KW_ALL:
                isAll = true;
            case KW_DISTINCT:
                lexer.nextToken();
                break;
        }
        select = selectPrimary();
        union.addSelect(select, isAll);
    }
    union.setOrderBy(orderBy()).setLimit(limit());
    return union;
}
 
Example #12
Source File: MySQLExprParser.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
/**
 * first <code>'('</code> has been consumed. At least one element. Consume
 * last ')' after invocation <br/>
 * <code>'(' expr (',' expr)* ')'</code>
 */
private List<Expression> expressionList(List<Expression> exprList) throws SQLSyntaxErrorException {
    for (;;) {
        Expression expr = expression();
        exprList.add(expr);
        switch (lexer.token()) {
            case PUNC_COMMA:
                lexer.nextToken();
                break;
            case PUNC_RIGHT_PAREN:
                lexer.nextToken();
                return exprList;
            default:
                throw err("unexpected token: " + lexer.token());
        }
    }
}
 
Example #13
Source File: MySQLExprParser.java    From mybatis-shard with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * <code>higherPRJExpr ( ( '||' | 'OR' ) higherPRJExpr )*</code>
 * 
 * @throws java.sql.SQLSyntaxErrorException
 */
private Expression logicalOrExpression() throws SQLSyntaxErrorException {
    LogicalOrExpression or = null;
    for (Expression expr = logicalXORExpression();;) {
        switch (lexer.token()) {
        case OP_LOGICAL_OR:
        case KW_OR:
            lexer.nextToken();
            if (or == null) {
                or = new LogicalOrExpression();
                or.setCacheEvalRst(cacheEvalRst);
                or.appendOperand(expr);
                expr = or;
            }
            Expression newExpr = logicalXORExpression();
            or.appendOperand(newExpr);
            break;
        default:
            return expr;
        }
    }
}
 
Example #14
Source File: MySQLDDLParser.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
private IndexColumnName indexColumnName() throws SQLSyntaxErrorException {
    // col_name [(length)] [ASC | DESC]
    Identifier colName = identifier();
    Expression len = null;
    if (lexer.token() == PUNC_LEFT_PAREN) {
        lexer.nextToken();
        len = exprParser.expression();
        match(PUNC_RIGHT_PAREN);
    }
    switch (lexer.token()) {
        case KW_ASC:
            lexer.nextToken();
            return new IndexColumnName(colName, len, true);
        case KW_DESC:
            lexer.nextToken();
            return new IndexColumnName(colName, len, false);
        default:
            return new IndexColumnName(colName, len, true);
    }
}
 
Example #15
Source File: MySQLExprParser.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
/**
 * <code>higherPRJExpr ( ( '||' | 'OR' ) higherPRJExpr )*</code>
 * 
 * @throws SQLSyntaxErrorException
 */
private Expression logicalOrExpression() throws SQLSyntaxErrorException {
    LogicalOrExpression or = null;
    for (Expression expr = logicalXORExpression();;) {
        switch (lexer.token()) {
            case OP_LOGICAL_OR:
            case KW_OR:
                lexer.nextToken();
                if (or == null) {
                    or = new LogicalOrExpression();
                    or.setCacheEvalRst(cacheEvalRst);
                    or.appendOperand(expr);
                    expr = or;
                }
                Expression newExpr = logicalXORExpression();
                or.appendOperand(newExpr);
                break;
            default:
                return expr;
        }
    }
}
 
Example #16
Source File: MySQLLexer.java    From mybatis-shard with Eclipse Public License 1.0 6 votes vote down vote up
public MySQLLexer(char[] sql) throws SQLSyntaxErrorException {
    if ((this.sbuf = sbufRef.get()) == null) {
        this.sbuf = new char[1024];
        sbufRef.set(this.sbuf);
    }
    if (CharTypes.isWhitespace(sql[sql.length - 1])) {
        this.sql = sql;
    } else {
        this.sql = new char[sql.length + 1];
        System.arraycopy(sql, 0, this.sql, 0, sql.length);
    }
    this.eofIndex = this.sql.length - 1;
    this.sql[this.eofIndex] = MySQLLexer.EOI;
    scanChar();
    nextToken();
}
 
Example #17
Source File: MySQLExprParser.java    From mybatis-shard with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #18
Source File: MySQLDDLParser.java    From mybatis-shard with Eclipse Public License 1.0 6 votes vote down vote up
private IndexDefinition indexDefinition() throws SQLSyntaxErrorException {
    IndexDefinition.IndexType indexType = null;
    List<IndexColumnName> columns = new ArrayList<IndexColumnName>(1);
    if (lexer.token() == KW_USING) {
        lexer.nextToken();
        int tp = matchIdentifier("BTREE", "HASH");
        indexType = tp == 0 ? IndexDefinition.IndexType.BTREE : IndexDefinition.IndexType.HASH;
    }
    match(PUNC_LEFT_PAREN);
    for (int i = 0; lexer.token() != PUNC_RIGHT_PAREN; ++i) {
        if (i > 0)
            match(PUNC_COMMA);
        IndexColumnName indexColumnName = indexColumnName();
        columns.add(indexColumnName);
    }
    match(PUNC_RIGHT_PAREN);
    List<IndexOption> options = indexOptions();
    return new IndexDefinition(indexType, columns, options);
}
 
Example #19
Source File: MySQLExprParser.java    From heisenberg with Apache License 2.0 5 votes vote down vote up
/**
 * <code>('+'|'-'|'~'|'!'|'BINARY')* higherExpr</code><br/>
 * '!' has higher precedence
 */
private Expression unaryOpExpression(String consumed, String consumedUp) throws SQLSyntaxErrorException {
    if (consumed == null) {
        Expression expr;
        switch (lexer.token()) {
        case OP_EXCLAMATION:
            lexer.nextToken();
            expr = unaryOpExpression(null, null);
            return new NegativeValueExpression(expr).setCacheEvalRst(cacheEvalRst);
        case OP_PLUS:
            lexer.nextToken();
            return unaryOpExpression(null, null);
        case OP_MINUS:
            lexer.nextToken();
            expr = unaryOpExpression(null, null);
            return new MinusExpression(expr).setCacheEvalRst(cacheEvalRst);
        case OP_TILDE:
            lexer.nextToken();
            expr = unaryOpExpression(null, null);
            return new BitInvertExpression(expr).setCacheEvalRst(cacheEvalRst);
        case KW_BINARY:
            lexer.nextToken();
            expr = unaryOpExpression(null, null);
            return new CastBinaryExpression(expr).setCacheEvalRst(cacheEvalRst);
        }
    }
    return collateExpression(consumed, consumedUp);
}
 
Example #20
Source File: SQLSyntaxErrorExceptionTests.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create SQLSyntaxErrorException with null Throwable
 */
@Test
public void test8() {
    SQLSyntaxErrorException ex = new SQLSyntaxErrorException((Throwable)null);
    assertTrue(ex.getMessage() == null
            && ex.getSQLState() == null
            && ex.getCause() == null
            && ex.getErrorCode() == 0);
}
 
Example #21
Source File: MySQLExprParser.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
private Expression bitAndExpression(String consumed, String consumedUp) throws SQLSyntaxErrorException {
    for (Expression expr = bitShiftExpression(consumed, consumedUp);;) {
        switch (lexer.token()) {
            case OP_AMPERSAND:
                lexer.nextToken();
                Expression newExpr = bitShiftExpression(null, null);
                expr = new BitAndExpression(expr, newExpr).setCacheEvalRst(cacheEvalRst);
                break;
            default:
                return expr;
        }
    }
}
 
Example #22
Source File: SQLSyntaxErrorExceptionTests.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create SQLSyntaxErrorException with message, and Throwable
 */
@Test
public void test7() {
    SQLSyntaxErrorException ex = new SQLSyntaxErrorException(reason, t);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState() == null
            && cause.equals(ex.getCause().toString())
            && ex.getErrorCode() == 0);
}
 
Example #23
Source File: MySQLDMLInsertParser.java    From heisenberg with Apache License 2.0 5 votes vote down vote up
/**
 * @return null for not exist
 */
private List<Pair<Identifier, Expression>> onDuplicateUpdate() throws SQLSyntaxErrorException {
    if (lexer.token() != KW_ON) {
        return null;
    }
    lexer.nextToken();
    matchIdentifier("DUPLICATE");
    match(KW_KEY);
    match(KW_UPDATE);
    List<Pair<Identifier, Expression>> list;
    Identifier col = identifier();
    match(OP_EQUALS, OP_ASSIGN);
    Expression expr = exprParser.expression();
    if (lexer.token() == PUNC_COMMA) {
        list = new LinkedList<Pair<Identifier, Expression>>();
        list.add(new Pair<Identifier, Expression>(col, expr));
        for (; lexer.token() == PUNC_COMMA;) {
            lexer.nextToken();
            col = identifier();
            match(OP_EQUALS, OP_ASSIGN);
            expr = exprParser.expression();
            list.add(new Pair<Identifier, Expression>(col, expr));
        }
        return list;
    }
    list = new ArrayList<Pair<Identifier, Expression>>(1);
    list.add(new Pair<Identifier, Expression>(col, expr));
    return list;
}
 
Example #24
Source File: CobarHint.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
/**
 * @param offset index of first char of {@link #COBAR_HINT_PREFIX}
 */
public static CobarHint parserCobarHint(String sql, int offset) throws SQLSyntaxErrorException {
    CobarHint hint = new CobarHint();
    hint.currentIndex = offset;
    hint.parse(sql);
    return hint;
}
 
Example #25
Source File: SQLSyntaxErrorExceptionTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create SQLSyntaxErrorException with message, and Throwable
 */
@Test
public void test7() {
    SQLSyntaxErrorException ex = new SQLSyntaxErrorException(reason, t);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState() == null
            && cause.equals(ex.getCause().toString())
            && ex.getErrorCode() == 0);
}
 
Example #26
Source File: SQLSyntaxErrorExceptionTests.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create SQLSyntaxErrorException with Throwable
 */
@Test
public void test9() {
    SQLSyntaxErrorException ex = new SQLSyntaxErrorException(t);
    assertTrue(ex.getMessage().equals(cause)
            && ex.getSQLState() == null
            && cause.equals(ex.getCause().toString())
            && ex.getErrorCode() == 0);
}
 
Example #27
Source File: SQLSyntaxErrorExceptionTests.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create SQLSyntaxErrorException with message, SQLState, and Throwable
 */
@Test
public void test6() {
    SQLSyntaxErrorException ex = new SQLSyntaxErrorException(reason, state, t);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState().equals(state)
            && cause.equals(ex.getCause().toString())
            && ex.getErrorCode() == 0);
}
 
Example #28
Source File: SQLSyntaxErrorExceptionTests.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create SQLSyntaxErrorException with message, SQLState, and Throwable
 */
@Test
public void test6() {
    SQLSyntaxErrorException ex = new SQLSyntaxErrorException(reason, state, t);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState().equals(state)
            && cause.equals(ex.getCause().toString())
            && ex.getErrorCode() == 0);
}
 
Example #29
Source File: MySQLExprParser.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
/**
 * @param consumed not null means that a token that has been pre-consumed
 * stands for next token
 */
private Expression bitOrExpression(String consumed, String consumedUp) throws SQLSyntaxErrorException {
    for (Expression expr = bitAndExpression(consumed, consumedUp);;) {
        switch (lexer.token()) {
            case OP_VERTICAL_BAR:
                lexer.nextToken();
                Expression newExpr = bitAndExpression(null, null);
                expr = new BitOrExpression(expr, newExpr).setCacheEvalRst(cacheEvalRst);
                break;
            default:
                return expr;
        }
    }
}
 
Example #30
Source File: MySQLExprParser.java    From heisenberg with Apache License 2.0 5 votes vote down vote up
/**
 * @return {@link QueryExpression} or {@link InExpressionList}
 */
private Expression rightOprandOfIn() throws SQLSyntaxErrorException {
    match(PUNC_LEFT_PAREN);
    if (KW_SELECT == lexer.token()) {
        QueryExpression subq = subQuery();
        match(PUNC_RIGHT_PAREN);
        return subq;
    }
    return new InExpressionList(expressionList(new LinkedList<Expression>())).setCacheEvalRst(cacheEvalRst);
}