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

The following examples show how to use com.alibaba.fastsql.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: HBTParser.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
private CallExpr arrayExprExpr() {
    String funName = "array";
    lexer.nextToken();
    List<ParseNode> exprs = new ArrayList<>(3);
    Token token1 = lexer.token();
    if (token1 == Token.RBRACKET) {
        lexer.nextToken();
        return new CallExpr(funName, new ParenthesesExpr(Collections.emptyList()));
    }
    ParseNode expression = expression();
    exprs.add(expression);
    while (true) {
        if (lexer.token() == Token.RBRACKET) {
            lexer.nextToken();
            return new CallExpr(funName, new ParenthesesExpr(exprs));
        } else if (lexer.token() == Token.COMMA) {
            lexer.nextToken();
            exprs.add(expression());
        } else {
            throw new ParserException(lexer.info());
        }
    }
}
 
Example #2
Source File: SqlParser.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
/**
 * 解析sql
 *
 * @param sql sql
 * @return 视图对象
 */
public static SchemaItem parse(String sql) {
    try {
        SQLStatementParser parser = new MySqlStatementParser(sql);
        SQLSelectStatement statement = (SQLSelectStatement) parser.parseStatement();
        MySqlSelectQueryBlock sqlSelectQueryBlock = (MySqlSelectQueryBlock) statement.getSelect().getQuery();

        SchemaItem schemaItem = new SchemaItem();
        schemaItem.setSql(SQLUtils.toMySqlString(sqlSelectQueryBlock));
        SQLTableSource sqlTableSource = sqlSelectQueryBlock.getFrom();
        List<TableItem> tableItems = new ArrayList<>();
        SqlParser.visitSelectTable(schemaItem, sqlTableSource, tableItems, null);
        tableItems.forEach(tableItem -> schemaItem.getAliasTableItems().put(tableItem.getAlias(), tableItem));

        List<FieldItem> fieldItems = collectSelectQueryFields(sqlSelectQueryBlock);
        fieldItems.forEach(fieldItem -> schemaItem.getSelectFields().put(fieldItem.getFieldName(), fieldItem));

        schemaItem.init();

        if (schemaItem.getAliasTableItems().isEmpty() || schemaItem.getSelectFields().isEmpty()) {
            throw new ParserException("Parse sql error");
        }
        return schemaItem;
    } catch (Exception e) {
        throw new ParserException();
    }
}
 
Example #3
Source File: SqlParser.java    From canal with Apache License 2.0 5 votes vote down vote up
/**
 * 解析sql
 *
 * @param sql sql
 * @return 视图对象
 */
public static SchemaItem parse(String sql) {
    try {
        SQLStatementParser parser = new MySqlStatementParser(sql);
        SQLSelectStatement statement = (SQLSelectStatement) parser.parseStatement();
        MySqlSelectQueryBlock sqlSelectQueryBlock = (MySqlSelectQueryBlock) statement.getSelect().getQuery();

        SchemaItem schemaItem = new SchemaItem();
        schemaItem.setSql(SQLUtils.toMySqlString(sqlSelectQueryBlock));
        SQLTableSource sqlTableSource = sqlSelectQueryBlock.getFrom();
        List<TableItem> tableItems = new ArrayList<>();
        SqlParser.visitSelectTable(schemaItem, sqlTableSource, tableItems, null);
        tableItems.forEach(tableItem -> schemaItem.getAliasTableItems().put(tableItem.getAlias(), tableItem));

        List<FieldItem> fieldItems = collectSelectQueryFields(sqlSelectQueryBlock);
        fieldItems.forEach(fieldItem -> schemaItem.getSelectFields().put(fieldItem.getFieldName(), fieldItem));

        schemaItem.init();

        if (schemaItem.getAliasTableItems().isEmpty() || schemaItem.getSelectFields().isEmpty()) {
            throw new ParserException("Parse sql error");
        }
        return schemaItem;
    } catch (Exception e) {
        throw new ParserException();
    }
}
 
Example #4
Source File: MycatCalciteMySqlNodeVisitor.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public static TimeUnit[] getTimeUnit(SQLIntervalUnit unit) {
    TimeUnit[] timeUnits = new TimeUnit[2];
    switch (unit) {
        // case MICROSECOND:
        // timeUnits[0] = TimeUnit.MICROSECOND;
        // timeUnits[1] = TimeUnit.MICROSECOND;
        // break;
        case SECOND:
            timeUnits[0] = TimeUnit.SECOND;
            timeUnits[1] = TimeUnit.SECOND;
            break;
        case MINUTE:
            timeUnits[0] = TimeUnit.MINUTE;
            timeUnits[1] = TimeUnit.MINUTE;
            break;
        case HOUR:
            timeUnits[0] = TimeUnit.HOUR;
            timeUnits[1] = TimeUnit.HOUR;
            break;
        case DAY:
            timeUnits[0] = TimeUnit.DAY;
            timeUnits[1] = TimeUnit.DAY;
            break;
        case WEEK:
            timeUnits[0] = TimeUnit.WEEK;
            timeUnits[1] = TimeUnit.WEEK;
            break;
        case MONTH:
            timeUnits[0] = TimeUnit.MONTH;
            timeUnits[1] = TimeUnit.MONTH;
            break;
        case QUARTER:
            timeUnits[0] = TimeUnit.QUARTER;
            timeUnits[1] = TimeUnit.QUARTER;
            break;
        case YEAR:
            timeUnits[0] = TimeUnit.YEAR;
            timeUnits[1] = TimeUnit.YEAR;
            break;
        // case MINUTE_MICROSECOND:
        // timeUnits[0] = TimeUnit.MINUTE;
        // timeUnits[1] = TimeUnit.MICROSECOND;
        // break;
        case MINUTE_SECOND:
            timeUnits[0] = TimeUnit.MINUTE;
            timeUnits[1] = TimeUnit.SECOND;
            break;
        // case HOUR_MICROSECOND:
        // timeUnits[0] = TimeUnit.HOUR;
        // timeUnits[1] = TimeUnit.MICROSECOND;
        // break;
        case HOUR_SECOND:
            timeUnits[0] = TimeUnit.HOUR;
            timeUnits[1] = TimeUnit.SECOND;
            break;
        case HOUR_MINUTE:
            timeUnits[0] = TimeUnit.HOUR;
            timeUnits[1] = TimeUnit.MINUTE;
            break;
        // case DAY_MICROSECOND:
        // timeUnits[0] = TimeUnit.DAY;
        // timeUnits[1] = TimeUnit.MICROSECOND;
        // break;
        case DAY_SECOND:
            timeUnits[0] = TimeUnit.DAY;
            timeUnits[1] = TimeUnit.SECOND;
            break;
        case DAY_MINUTE:
            timeUnits[0] = TimeUnit.DAY;
            timeUnits[1] = TimeUnit.MINUTE;
            break;
        case DAY_HOUR:
            timeUnits[0] = TimeUnit.DAY;
            timeUnits[1] = TimeUnit.HOUR;
            break;
        case YEAR_MONTH:
            timeUnits[0] = TimeUnit.YEAR;
            timeUnits[1] = TimeUnit.MONTH;
            break;
        default:
            throw new ParserException("Unsupported time unit");
    }
    return timeUnits;
}