com.alibaba.druid.sql.parser.ParserException Java Examples

The following examples show how to use com.alibaba.druid.sql.parser.ParserException. 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: ReverseEngineeringService.java    From youran with Apache License 2.0 6 votes vote down vote up
/**
 * 解析DDL
 *
 * @param dto
 * @return
 */
public List<SQLStatement> parse(ReverseEngineeringDTO dto) {
    List<SQLStatement> sqlStatements;
    try {
        sqlStatements = SQLUtils.parseStatements(dto.getDdl(), dto.getDbType());
    } catch (ParserException e) {
        LOGGER.warn("反向工程校验失败:{}", e);
        throw new BusinessException(ErrorCode.BAD_PARAMETER, "DDL解析失败:" + e.getMessage());
    }
    if (CollectionUtils.isEmpty(sqlStatements)) {
        throw new BusinessException(ErrorCode.BAD_PARAMETER, "未找到有效DDL语句");
    }
    for (SQLStatement sqlStatement : sqlStatements) {
        if (!(sqlStatement instanceof SQLCreateTableStatement)) {
            throw new BusinessException(ErrorCode.BAD_PARAMETER, "只支持create table语句,请删除多余的sql");
        }
    }
    return sqlStatements;
}
 
Example #2
Source File: EsColumnSyncManager.java    From DataLink with Apache License 2.0 6 votes vote down vote up
public static SQLStatementHolder getSQLStatementHolder(MediaSourceType mediaSourceType, String sqls) throws ErrorException {
    try {
        List<SQLStatementHolder> holders = DdlSqlUtils.buildSQLStatement(mediaSourceType, sqls, false);
        if (holders.isEmpty()) {
            throw new ErrorException(CodeContext.SQL_COUNT_ERROR_CODE, CodeContext.getErrorDesc(CodeContext.SQL_COUNT_ERROR_CODE));
        }
        if (holders.size() > 1) {
            throw new ErrorException(CodeContext.SQL_COUNT_ERROR_CODE, CodeContext.getErrorDesc(CodeContext.SQL_COUNT_ERROR_CODE));
        }
        SQLStatementHolder holder = holders.get(0);
        holder.check();
        if (holder.getSqlType() != SqlType.AlterTable) {
            throw new ErrorException(CodeContext.SQL_VALIDATE_ERROR_CODE, CodeContext.getErrorDesc(CodeContext.SQL_VALIDATE_ERROR_CODE));
        }
        return holder;
    } catch (ParserException e) {
        throw new ErrorException(CodeContext.SQL_SYNTAX_ERROR_CODE, CodeContext.getErrorDesc(CodeContext.SQL_SYNTAX_ERROR_CODE));
    }
}
 
Example #3
Source File: WriterUtil.java    From DataLink with Apache License 2.0 6 votes vote down vote up
public static void preCheckPrePareSQL(Configuration originalConfig, DataBaseType type) {
    List<Object> conns = originalConfig.getList(Constant.CONN_MARK, Object.class);
    Configuration connConf = Configuration.from(conns.get(0).toString());
    String table = connConf.getList(Key.TABLE, String.class).get(0);

    List<String> preSqls = originalConfig.getList(Key.PRE_SQL,
            String.class);
    List<String> renderedPreSqls = WriterUtil.renderPreOrPostSqls(
            preSqls, table);

    if (null != renderedPreSqls && !renderedPreSqls.isEmpty()) {
        LOG.info("Begin to preCheck preSqls:[{}].",
                StringUtils.join(renderedPreSqls, ";"));
        for(String sql : renderedPreSqls) {
            try{
                DBUtil.sqlValid(sql, type);
            }catch(ParserException e) {
                throw RdbmsException.asPreSQLParserException(type,e,sql);
            }
        }
    }
}
 
Example #4
Source File: WriterUtil.java    From DataLink with Apache License 2.0 6 votes vote down vote up
public static void preCheckPostSQL(Configuration originalConfig, DataBaseType type) {
    List<Object> conns = originalConfig.getList(Constant.CONN_MARK, Object.class);
    Configuration connConf = Configuration.from(conns.get(0).toString());
    String table = connConf.getList(Key.TABLE, String.class).get(0);

    List<String> postSqls = originalConfig.getList(Key.POST_SQL,
            String.class);
    List<String> renderedPostSqls = WriterUtil.renderPreOrPostSqls(
            postSqls, table);
    if (null != renderedPostSqls && !renderedPostSqls.isEmpty()) {

        LOG.info("Begin to preCheck postSqls:[{}].",
                StringUtils.join(renderedPostSqls, ";"));
        for(String sql : renderedPostSqls) {
            try{
                DBUtil.sqlValid(sql, type);
            }catch(ParserException e){
                throw RdbmsException.asPostSQLParserException(type,e,sql);
            }

        }
    }
}
 
Example #5
Source File: ElasticSqlExprParser.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
protected SQLExpr bracketRest(SQLExpr expr) {
    Number index;

    if (lexer.token() == Token.LITERAL_INT) {
        index = lexer.integerValue();
        lexer.nextToken();
    } else {
        throw new ParserException("error : " + lexer.stringVal());
    }

    if (expr instanceof SQLMethodInvokeExpr) {
        SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr) expr;
        methodInvokeExpr.getParameters().add(new SQLIntegerExpr(index));
    }
    lexer.nextToken();
    expr = primaryRest(expr);
    return expr;
}
 
Example #6
Source File: ElasticSqlExprParser.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
protected SQLExpr parseExtract() {
    SQLExpr expr;
    if (lexer.token() != Token.IDENTIFIER) {
        throw new ParserException("syntax error. " + lexer.info());
    }

    String unitVal = lexer.stringVal();
    SQLIntervalUnit unit = SQLIntervalUnit.valueOf(unitVal.toUpperCase());
    lexer.nextToken();

    accept(Token.FROM);

    SQLExpr value = expr();

    MySqlExtractExpr extract = new MySqlExtractExpr();
    extract.setValue(value);
    extract.setUnit(unit);
    accept(Token.RPAREN);

    expr = extract;

    return primaryRest(expr);
}
 
Example #7
Source File: ElasticSearchPreparedStatement.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
public String getExecutableSql() {
    if (parametersSize < 1) {
        return sql;
    }

    List<Object> parameters = new ArrayList<>(parametersSize);
    JdbcParameter jdbcParam;
    for (int i = 0; i < parametersSize; ++i) {
        jdbcParam = this.parameters[i];
        parameters.add(jdbcParam != null ? jdbcParam.getValue() : null);
    }

    try {
        SQLStatementParser parser = ESActionFactory.createSqlStatementParser(sql);
        List<SQLStatement> statementList = parser.parseStatementList();
        return SQLUtils.toSQLString(statementList, JdbcConstants.MYSQL, parameters, sqlFormatOption);
    } catch (ClassCastException | ParserException ex) {
        LOG.warn("format error", ex);
        return sql;
    }
}
 
Example #8
Source File: KuduColumnSyncManager.java    From DataLink with Apache License 2.0 5 votes vote down vote up
public static SQLStatementHolder getSQLStatementHolder(MediaSourceType mediaSourceType, String sqls) throws ErrorException {
    try {
        List<SQLStatementHolder> holders = DdlSqlUtils.buildSQLStatement(mediaSourceType, sqls);
        if (holders.isEmpty() || holders.size() > 1) {
            throw new ErrorException(CodeContext.SQL_COUNT_ERROR_CODE, CodeContext.getErrorDesc(CodeContext.SQL_COUNT_ERROR_CODE));
        }
        SQLStatementHolder holder = holders.get(0);
        holder.check();
        return holder;
    } catch (ParserException e) {
        throw new ErrorException(CodeContext.SQL_SYNTAX_ERROR_CODE, CodeContext.getErrorDesc(CodeContext.SQL_SYNTAX_ERROR_CODE));
    }
}
 
Example #9
Source File: ESActionFactory.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private static SQLExpr toSqlExpr(String sql) {
    SQLExprParser parser = new ElasticSqlExprParser(sql); //zhongshu-comment 这个SQLExprParser parser应该就是语法解析器
    SQLExpr expr = parser.expr(); //zhongshu-comment 这个expr应该就是解析sql之后得到的AST了

    //zhongshu-comment 调用parser.expr()方法解析完sql语句后,发现最后一个token不是End Of File的话,即该sql语句貌似是残缺的,可能是用户输入了一个未结束的sql
    if (parser.getLexer().token() != Token.EOF) {
        throw new ParserException("illegal sql expr : " + sql);
    }

    return expr;
}
 
Example #10
Source File: ElasticSqlSelectParser.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
protected MySqlUpdateStatement parseUpdateStatment() {
    MySqlUpdateStatement update = new MySqlUpdateStatement();

    lexer.nextToken();

    if (lexer.identifierEquals(FnvHash.Constants.LOW_PRIORITY)) {
        lexer.nextToken();
        update.setLowPriority(true);
    }

    if (lexer.identifierEquals(FnvHash.Constants.IGNORE)) {
        lexer.nextToken();
        update.setIgnore(true);
    }

    if (lexer.identifierEquals(FnvHash.Constants.COMMIT_ON_SUCCESS)) {
        lexer.nextToken();
        update.setCommitOnSuccess(true);
    }

    if (lexer.identifierEquals(FnvHash.Constants.ROLLBACK_ON_FAIL)) {
        lexer.nextToken();
        update.setRollBackOnFail(true);
    }

    if (lexer.identifierEquals(FnvHash.Constants.QUEUE_ON_PK)) {
        lexer.nextToken();
        update.setQueryOnPk(true);
    }

    if (lexer.identifierEquals(FnvHash.Constants.TARGET_AFFECT_ROW)) {
        lexer.nextToken();
        SQLExpr targetAffectRow = this.exprParser.expr();
        update.setTargetAffectRow(targetAffectRow);
    }

    if (lexer.identifierEquals(FnvHash.Constants.FORCE)) {
        lexer.nextToken();

        if (lexer.token() == Token.ALL) {
            lexer.nextToken();
            acceptIdentifier("PARTITIONS");
            update.setForceAllPartitions(true);
        } else if (lexer.identifierEquals(FnvHash.Constants.PARTITIONS)){
            lexer.nextToken();
            update.setForceAllPartitions(true);
        } else if (lexer.token() == Token.PARTITION) {
            lexer.nextToken();
            SQLName partition = this.exprParser.name();
            update.setForcePartition(partition);
        } else {
            throw new ParserException("TODO. " + lexer.info());
        }
    }

    while (lexer.token() == Token.HINT) {
        this.exprParser.parseHints(update.getHints());
    }

    SQLSelectParser selectParser = this.exprParser.createSelectParser();
    SQLTableSource updateTableSource = selectParser.parseTableSource();
    update.setTableSource(updateTableSource);

    accept(Token.SET);

    for (;;) {
        SQLUpdateSetItem item = this.exprParser.parseUpdateSetItem();
        update.addItem(item);

        if (lexer.token() != Token.COMMA) {
            break;
        }

        lexer.nextToken();
    }

    if (lexer.token() == (Token.WHERE)) {
        lexer.nextToken();
        update.setWhere(this.exprParser.expr());
    }

    update.setOrderBy(this.exprParser.parseOrderBy());
    update.setLimit(this.exprParser.parseLimit());

    return update;
}
 
Example #11
Source File: ElasticSqlExprParser.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
protected SQLExpr parseMatch() {

        MySqlMatchAgainstExpr matchAgainstExpr = new MySqlMatchAgainstExpr();

        if (lexer.token() == Token.RPAREN) {
            lexer.nextToken();
        } else {
            exprList(matchAgainstExpr.getColumns(), matchAgainstExpr);
            accept(Token.RPAREN);
        }

        acceptIdentifier("AGAINST");

        accept(Token.LPAREN);
        SQLExpr against = primary();
        matchAgainstExpr.setAgainst(against);

        if (lexer.token() == Token.IN) {
            lexer.nextToken();
            if (lexer.identifierEquals(FnvHash.Constants.NATURAL)) {
                lexer.nextToken();
                acceptIdentifier("LANGUAGE");
                acceptIdentifier("MODE");
                if (lexer.token() == Token.WITH) {
                    lexer.nextToken();
                    acceptIdentifier("QUERY");
                    acceptIdentifier("EXPANSION");
                    matchAgainstExpr.setSearchModifier(MySqlMatchAgainstExpr.SearchModifier.IN_NATURAL_LANGUAGE_MODE_WITH_QUERY_EXPANSION);
                } else {
                    matchAgainstExpr.setSearchModifier(MySqlMatchAgainstExpr.SearchModifier.IN_NATURAL_LANGUAGE_MODE);
                }
            } else if (lexer.identifierEquals(FnvHash.Constants.BOOLEAN)) {
                lexer.nextToken();
                acceptIdentifier("MODE");
                matchAgainstExpr.setSearchModifier(MySqlMatchAgainstExpr.SearchModifier.IN_BOOLEAN_MODE);
            } else {
                throw new ParserException("syntax error. " + lexer.info());
            }
        } else if (lexer.token() == Token.WITH) {
            throw new ParserException("TODO. " + lexer.info());
        }

        accept(Token.RPAREN);

        return primaryRest(matchAgainstExpr);
    }