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: 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 #3
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 #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: 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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
Source File: MySQLParser.java    From tddl with Apache License 2.0 5 votes vote down vote up
/**
 * @param expectTextUppercase must be upper-case
 * @return index (start from 0) of expected text which is first matched. -1 if none is matched.
 */
protected int equalsIdentifier(String... expectTextUppercases) throws SQLSyntaxErrorException {
    if (lexer.token() == MySQLToken.IDENTIFIER) {
        String id = lexer.stringValueUppercase();
        for (int i = 0; i < expectTextUppercases.length; ++i) {
            if (expectTextUppercases[i].equals(id)) {
                return i;
            }
        }
    }
    return -1;
}
 
Example #20
Source File: MySQLExprParser.java    From tddl 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);
}
 
Example #21
Source File: SQLSyntaxErrorExceptionTests.java    From hottub 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 #22
Source File: SetHandler.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
private static SQLStatement parseSQL(String stmt) throws SQLSyntaxErrorException {
    SQLStatementParser parser = new MySqlStatementParser(stmt);
    try {
        return parser.parseStatement();
    } catch (Exception t) {
        if (t.getMessage() != null) {
            throw new SQLSyntaxErrorException(t.getMessage());
        } else {
            throw new SQLSyntaxErrorException(t);
        }
    }
}
 
Example #23
Source File: SQLSyntaxErrorExceptionTests.java    From dragonwell8_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 #24
Source File: MySQLMTSParser.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
/**
 * first token <code>RELEASE</code> is scanned but not yet consumed
 */
public MTSReleaseStatement release() throws SQLSyntaxErrorException {
    match(KW_RELEASE);
    matchIdentifier("SAVEPOINT");
    Identifier id = identifier();
    match(EOF);
    return new MTSReleaseStatement(id);
}
 
Example #25
Source File: SQLSyntaxErrorExceptionTests.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Serialize a SQLSyntaxErrorException and make sure you can read it back properly
 */
@Test
public void test10() throws Exception {

    SQLSyntaxErrorException e =
            new SQLSyntaxErrorException(reason, state, errorCode, t);
    SQLSyntaxErrorException ex1 =
            createSerializedException(e);
    assertTrue(reason.equals(ex1.getMessage())
            && ex1.getSQLState().equals(state)
            && cause.equals(ex1.getCause().toString())
            && ex1.getErrorCode() == errorCode);
}
 
Example #26
Source File: TernaryOperationIT.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testTRIMWithAliasNegative() throws Exception {
    try {
        String sql = "select trim (leading.c) from a as leading";
        methodWatcher.executeQuery(sql);
        Assert.fail("Query is expected to fail with syntax error!");
    } catch (SQLSyntaxErrorException e) {
        Assert.assertEquals(SQLState.LANG_SYNTAX_ERROR, e.getSQLState());
    }
}
 
Example #27
Source File: MySQLDMLLoadParserTest.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
public void testLoadDataColumnsSet() throws SQLSyntaxErrorException {

        {
            String sql = "LOAD DATA CONCURRENT LOCAL INFILE 'd:/data.txt' REPLACE INTO TABLE XXXX CHARACTER SET utf8 FIELDS TERMINATED BY '\\t' ENCLOSED BY '' ESCAPED BY '\\\\' LINES STARTING BY '' TERMINATED BY '\\n' ignore 2 lines (id,name,address)";
            SQLStatement stmt = SQLParserDelegate.parse(sql);
            String output = output2MySQL(stmt, sql);
            Assert.assertEquals("LOAD DATA CONCURRENT LOCAL INFILE 'd:/data.txt' REPLACE INTO TABLE XXXXCHARACTER SET utf8 COLUMNS TERMINATED BY '\\t' ENCLOSED BY '' ESCAPED BY '\\\\' LINESSTARTING BY '' TERMINATED BY '\\n' IGNORE 2 LINES (ID, NAME, ADDRESS) ",
                output);
        }

    }
 
Example #28
Source File: MySQLExprParser.java    From heisenberg with Apache License 2.0 5 votes vote down vote up
/**
 * first '(' has been consumed
 */
private Timestampdiff timestampdiff() throws SQLSyntaxErrorException {
    IntervalPrimary.Unit unit = intervalPrimaryUnit();
    match(PUNC_COMMA);
    Expression interval = expression();
    match(PUNC_COMMA);
    Expression expr = expression();
    match(PUNC_RIGHT_PAREN);
    List<Expression> argument = new ArrayList<Expression>(2);
    argument.add(interval);
    argument.add(expr);
    Timestampdiff func = new Timestampdiff(unit, argument);
    func.setCacheEvalRst(cacheEvalRst);
    return func;
}
 
Example #29
Source File: MySQLExprParser.java    From tddl with Apache License 2.0 5 votes vote down vote up
/**
 * first '(' has been consumed
 */
private Timestampdiff timestampdiff() throws SQLSyntaxErrorException {
    IntervalPrimary.Unit unit = intervalPrimaryUnit();
    match(PUNC_COMMA);
    Expression interval = expression();
    match(PUNC_COMMA);
    Expression expr = expression();
    match(PUNC_RIGHT_PAREN);
    List<Expression> argument = new ArrayList<Expression>(2);
    argument.add(interval);
    argument.add(expr);
    Timestampdiff func = new Timestampdiff(unit, argument);
    func.setCacheEvalRst(cacheEvalRst);
    return func;
}
 
Example #30
Source File: XMLRuleLoader.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * tableRule标签结构:
 * <tableRule name="sharding-by-month">
 *     <rule>
 *         <columns>create_date</columns>
 *         <algorithm>partbymonth</algorithm>
 *     </rule>
 * </tableRule>
 * @param root
 * @throws SQLSyntaxErrorException
    */
private void loadTableRules(Element root) throws SQLSyntaxErrorException {
	//获取每个tableRule标签
	NodeList list = root.getElementsByTagName("tableRule");
	for (int i = 0, n = list.getLength(); i < n; ++i) {
		Node node = list.item(i);
		if (node instanceof Element) {
			Element e = (Element) node;
			//先判断是否重复
			String name = e.getAttribute("name");
			if (tableRules.containsKey(name)) {
				throw new ConfigException("table rule " + name
						+ " duplicated!");
			}
			//获取rule标签
			NodeList ruleNodes = e.getElementsByTagName("rule");
			int length = ruleNodes.getLength();
			if (length > 1) {
				throw new ConfigException("only one rule can defined :"
						+ name);
			}
			//目前只处理第一个,未来可能有多列复合逻辑需求
			//RuleConfig是保存着rule与function对应关系的对象
			RuleConfig rule = loadRule((Element) ruleNodes.item(0));
			String funName = rule.getFunctionName();
			//判断function是否存在,获取function
			AbstractPartitionAlgorithm func = functions.get(funName);
			if (func == null) {
				throw new ConfigException("can't find function of name :"
						+ funName);
			}
			rule.setRuleAlgorithm(func);
			//保存到tableRules
			tableRules.put(name, new TableRuleConfig(name, rule));
		}
	}
}