com.alibaba.fastsql.sql.ast.statement.SQLTableSource Java Examples

The following examples show how to use com.alibaba.fastsql.sql.ast.statement.SQLTableSource. 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: ConditionCollector.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
private ColumnValue getWhereRangeRightValue(SQLExpr leftExpr, SQLBinaryOperator operator, SQLExpr rightExpr) {
    if (operator != SQLBinaryOperator.LessThanOrEqual) {
        return null;
    }
    SQLColumnDefinition column = null;
    Object value = null;
    if (leftExpr instanceof SQLName && rightExpr instanceof SQLValuableExpr) {
        column = ((SQLName) leftExpr).getResolvedColumn();
        value = ((SQLValuableExpr) rightExpr).getValue();
    }
    SQLTableSource tableSource = null;
    if (column != null && value != null) {
        tableSource = Converters.getColumnTableSource(leftExpr);
    }
    return new ColumnValue(column, operator, value, tableSource);
}
 
Example #2
Source File: ConditionCollector.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
private ColumnValue getWhereRangeLeftValue(SQLExpr leftExpr, SQLBinaryOperator operator, SQLExpr rightExpr) {
    boolean left = true;
    SQLColumnDefinition column = null;
    Object value = null;
    if (leftExpr instanceof SQLValuableExpr && rightExpr instanceof SQLName) {
        column = ((SQLName) rightExpr).getResolvedColumn();
        value = ((SQLValuableExpr) leftExpr).getValue();
        left = false;
    }
    SQLTableSource tableSource = null;
    if (column != null && value != null) {
        tableSource = Converters.getColumnTableSource(rightExpr);
    }
    //1<= id
    if (column != null && operator != null && value != null && tableSource != null) {
        return new ColumnValue(column, operator, value, tableSource);
    } else {
        return null;
    }
}
 
Example #3
Source File: ConditionCollector.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean visit(SQLBetweenExpr x) {
    boolean not = x.isNot();
    if (not) {
        failureBecauseIndeterminacy(x);
    } else {
        SQLExpr testExpr = x.getTestExpr();
        SQLExpr beginExpr = x.getBeginExpr();
        SQLExpr endExpr = x.getEndExpr();
        if (testExpr instanceof SQLName && beginExpr instanceof SQLValuableExpr
                && endExpr instanceof SQLValuableExpr) {
            SQLColumnDefinition column = ((SQLName) testExpr).getResolvedColumn();
            SQLTableSource source = Converters.getColumnTableSource(testExpr);
            if (source != null) {
                Object beginValue = ((SQLValuableExpr) beginExpr).getValue();
                Object endValue = ((SQLValuableExpr) endExpr).getValue();
                if (column != null && beginValue != null && endValue != null) {
                    addRangeValues(new ColumnRangeValue(column, beginValue, endValue, source));
                }
            }
        }
    }
    return super.visit(x);
}
 
Example #4
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 #5
Source File: ConditionCollector.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean visit(SQLInListExpr x) {
    boolean not = x.isNot();
    if (not) {
        failureBecauseIndeterminacy(x);
    } else {
        SQLExpr expr = x.getExpr();
        SQLColumnDefinition column = null;
        SQLTableSource columnTableSource = null;
        if (expr instanceof SQLName) {
            column = Converters.getColumnDef(expr);
            columnTableSource = Converters.getColumnTableSource(expr);
        }
        if (column != null && columnTableSource != null) {
            List<SQLExpr> targetList = x.getTargetList();
            for (SQLExpr sqlExpr : targetList) {
                if (sqlExpr instanceof SQLValuableExpr) {
                    Object value = ((SQLValuableExpr) sqlExpr).getValue();
                    addEqualValue(new ColumnValue(column, SQLBinaryOperator.Equality, value, columnTableSource));
                } else {
                    continue;
                }
            }

        }
    }
    return super.visit(x);
}
 
Example #6
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 #7
Source File: ColumnRangeValue.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public ColumnRangeValue(SQLColumnDefinition column, Object begin, Object end, SQLTableSource tableSource) {
    this.column = column;
    this.begin = begin;
    this.end = end;
    this.tableSource = tableSource;
}
 
Example #8
Source File: ColumnRangeValue.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public SQLTableSource getTableSource() {
    return tableSource;
}
 
Example #9
Source File: ColumnValue.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public ColumnValue(SQLColumnDefinition column, SQLBinaryOperator operator, Object value, SQLTableSource tableSource) {
    this.column = column;
    this.operator = operator;
    this.value = value;
    this.tableSource = tableSource;
}
 
Example #10
Source File: ColumnValue.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public SQLTableSource getTableSource() {
    return tableSource;
}
 
Example #11
Source File: SqlParser.java    From canal with Apache License 2.0 4 votes vote down vote up
public static String parse4FromTableSource(MySqlSelectQueryBlock sqlSelectQueryBlock) {
    SQLTableSource sqlTableSource = sqlSelectQueryBlock.getFrom();
    return SQLUtils.toMySqlString(sqlTableSource);
}