Java Code Examples for com.alibaba.fastsql.sql.ast.SQLExpr

The following examples show how to use com.alibaba.fastsql.sql.ast.SQLExpr. These examples are extracted from open source projects. 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 Project: canal-1.1.3   Source File: SqlParser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 解析on条件
 *
 * @param expr sql expr
 * @param tableItem 表对象
 */
private static void visitOnCondition(SQLExpr expr, TableItem tableItem) {
    if (!(expr instanceof SQLBinaryOpExpr)) {
        throw new UnsupportedOperationException();
    }
    SQLBinaryOpExpr sqlBinaryOpExpr = (SQLBinaryOpExpr) expr;
    if (sqlBinaryOpExpr.getOperator() == BooleanAnd) {
        visitOnCondition(sqlBinaryOpExpr.getLeft(), tableItem);
        visitOnCondition(sqlBinaryOpExpr.getRight(), tableItem);
    } else if (sqlBinaryOpExpr.getOperator() == Equality) {
        FieldItem leftFieldItem = new FieldItem();
        visitColumn(sqlBinaryOpExpr.getLeft(), leftFieldItem);
        if (leftFieldItem.getColumnItems().size() != 1 || leftFieldItem.isMethod() || leftFieldItem.isBinaryOp()) {
            throw new UnsupportedOperationException("Unsupported for complex of on-condition");
        }
        FieldItem rightFieldItem = new FieldItem();
        visitColumn(sqlBinaryOpExpr.getRight(), rightFieldItem);
        if (rightFieldItem.getColumnItems().size() != 1 || rightFieldItem.isMethod()
            || rightFieldItem.isBinaryOp()) {
            throw new UnsupportedOperationException("Unsupported for complex of on-condition");
        }
        tableItem.getRelationFields().add(new RelationFieldsPair(leftFieldItem, rightFieldItem));
    } else {
        throw new UnsupportedOperationException("Unsupported for complex of on-condition");
    }
}
 
Example 2
Source Project: canal-1.1.3   Source File: MemoryTableMeta.java    License: Apache License 2.0 6 votes vote down vote up
private String getSqlName(SQLExpr sqlName) {
    if (sqlName == null) {
        return null;
    }

    if (sqlName instanceof SQLPropertyExpr) {
        SQLIdentifierExpr owner = (SQLIdentifierExpr) ((SQLPropertyExpr) sqlName).getOwner();
        return DruidDdlParser.unescapeName(owner.getName()) + "."
               + DruidDdlParser.unescapeName(((SQLPropertyExpr) sqlName).getName());
    } else if (sqlName instanceof SQLIdentifierExpr) {
        return DruidDdlParser.unescapeName(((SQLIdentifierExpr) sqlName).getName());
    } else if (sqlName instanceof SQLCharExpr) {
        return ((SQLCharExpr) sqlName).getText();
    } else if (sqlName instanceof SQLMethodInvokeExpr) {
        return DruidDdlParser.unescapeName(((SQLMethodInvokeExpr) sqlName).getMethodName());
    } else if (sqlName instanceof MySqlOrderingExpr) {
        return getSqlName(((MySqlOrderingExpr) sqlName).getExpr());
    } else {
        return sqlName.toString();
    }
}
 
Example 3
Source Project: canal-1.1.3   Source File: DruidDdlParser.java    License: Apache License 2.0 6 votes vote down vote up
private static void processName(DdlResult ddlResult, String schema, SQLExpr sqlName, boolean isOri) {
    if (sqlName == null) {
        if (StringUtils.isNotBlank(schema)) {
            ddlResult.setSchemaName(schema);
        }
        return;
    }

    String table = null;
    if (sqlName instanceof SQLPropertyExpr) {
        SQLIdentifierExpr owner = (SQLIdentifierExpr) ((SQLPropertyExpr) sqlName).getOwner();
        schema = unescapeName(owner.getName());
        table = unescapeName(((SQLPropertyExpr) sqlName).getName());
    } else if (sqlName instanceof SQLIdentifierExpr) {
        table = unescapeName(((SQLIdentifierExpr) sqlName).getName());
    }

    if (isOri) {
        ddlResult.setOriSchemaName(schema);
        ddlResult.setOriTableName(table);
    } else {
        ddlResult.setSchemaName(schema);
        ddlResult.setTableName(table);
    }
}
 
Example 4
@NotNull
private MycatSQLPrepareObject setStatement(String sql, SQLSetStatement sqlStatement, MycatDBContext uponDBContext) {
    return new SimpleSQLPrepareObjectPlanner(uponDBContext, sql) {

        @Override
        public void innerEun() {
            for (SQLAssignItem item : sqlStatement.getItems()) {
                String target = Objects.toString(item.getTarget());
                SQLExpr value = item.getValue();
                uponDBContext.setVariable(target, value);
            }
        }

        @Override
        public String innerExplain() {
            return sql;
        }
    };
}
 
Example 5
Source Project: Mycat2   Source File: SqlToExprTranslator.java    License: GNU General Public License v3.0 6 votes vote down vote up
private QueryPlan convertQuery(MySqlSelectQueryBlock sqlSelectQuery) {
    Blackboard blackboard = new Blackboard(rexBuilder, convertFrom(sqlSelectQuery.getFrom()));
    SQLExpr where = sqlSelectQuery.getWhere();
    if (where != null) {
        blackboard.addWhere(convertExpr(where));
    }
    List<SQLSelectItem> selectList = sqlSelectQuery.getSelectList();
    if (selectList != null) {
        blackboard.addSelect(convertSelect(selectList));
    }
    SQLOrderBy orderBy = sqlSelectQuery.getOrderBy();
    if (orderBy != null) {
        blackboard.addOrderBy(convertOrderBy(orderBy));
    }
    SQLSelectGroupByClause groupBy = sqlSelectQuery.getGroupBy();
    if (groupBy != null) {
        convertGroupBy(blackboard, groupBy);
    }
    SQLLimit limit = sqlSelectQuery.getLimit();
    if (limit != null) {
        convertLimit(blackboard, limit);
    }
    return blackboard.build();
}
 
Example 6
Source Project: Mycat2   Source File: ConditionCollector.java    License: 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 7
Source Project: Mycat2   Source File: ConditionCollector.java    License: 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 8
Source Project: Mycat2   Source File: ConditionCollector.java    License: 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 9
Source Project: Mycat2   Source File: ShowStatementRewriter.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static String rewriteShowTables(String defaultSchema, SQLShowTablesStatement ast) {
    SQLExpr where = ast.getWhere();
    SQLName from = ast.getFrom();
    SQLExpr like = ast.getLike();
    boolean full = ast.isFull();
    String schema = SQLUtils.normalize(from == null ? defaultSchema : from.getSimpleName());
    if (schema == null) {
        throw new MycatException(1046, "No database selected");
    }
    String schemaCondition = " TABLE_SCHEMA = '" + schema + "' ";
    String whereCondition = " " + (where == null ? "true" : where.toString()) + " ";
    String likeCondition = like == null ? " true " : " TABLE_NAME like " + " " + like.toString() + " ";
    String fullCondition = !full ? " true " : " TABLE_TYPE  = 'BASE TABLE' ";

    String sql = MessageFormat.format("select TABLE_NAME as {0} from information_schema.`TABLES` where {1} ",
            "`" + "Tables_in_" + schema + "`", String.join(" and ", schemaCondition, whereCondition, likeCondition, fullCondition)
    );
    LOGGER.info(ast + "->" + sql);
    return sql;
}
 
Example 10
Source Project: canal   Source File: SqlParser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 解析on条件
 *
 * @param expr sql expr
 * @param tableItem 表对象
 */
private static void visitOnCondition(SQLExpr expr, TableItem tableItem) {
    if (!(expr instanceof SQLBinaryOpExpr)) {
        throw new UnsupportedOperationException();
    }
    SQLBinaryOpExpr sqlBinaryOpExpr = (SQLBinaryOpExpr) expr;
    if (sqlBinaryOpExpr.getOperator() == BooleanAnd) {
        visitOnCondition(sqlBinaryOpExpr.getLeft(), tableItem);
        visitOnCondition(sqlBinaryOpExpr.getRight(), tableItem);
    } else if (sqlBinaryOpExpr.getOperator() == Equality) {
        FieldItem leftFieldItem = new FieldItem();
        visitColumn(sqlBinaryOpExpr.getLeft(), leftFieldItem);
        if (leftFieldItem.getColumnItems().size() != 1 || leftFieldItem.isMethod() || leftFieldItem.isBinaryOp()) {
            throw new UnsupportedOperationException("Unsupported for complex of on-condition");
        }
        FieldItem rightFieldItem = new FieldItem();
        visitColumn(sqlBinaryOpExpr.getRight(), rightFieldItem);
        if (rightFieldItem.getColumnItems().size() != 1 || rightFieldItem.isMethod()
            || rightFieldItem.isBinaryOp()) {
            throw new UnsupportedOperationException("Unsupported for complex of on-condition");
        }
        tableItem.getRelationFields().add(new RelationFieldsPair(leftFieldItem, rightFieldItem));
    } else {
        throw new UnsupportedOperationException("Unsupported for complex of on-condition");
    }
}
 
Example 11
Source Project: canal   Source File: MemoryTableMeta.java    License: Apache License 2.0 6 votes vote down vote up
private String getSqlName(SQLExpr sqlName) {
    if (sqlName == null) {
        return null;
    }

    if (sqlName instanceof SQLPropertyExpr) {
        SQLIdentifierExpr owner = (SQLIdentifierExpr) ((SQLPropertyExpr) sqlName).getOwner();
        return DruidDdlParser.unescapeName(owner.getName()) + "."
               + DruidDdlParser.unescapeName(((SQLPropertyExpr) sqlName).getName());
    } else if (sqlName instanceof SQLIdentifierExpr) {
        return DruidDdlParser.unescapeName(((SQLIdentifierExpr) sqlName).getName());
    } else if (sqlName instanceof SQLCharExpr) {
        return ((SQLCharExpr) sqlName).getText();
    } else if (sqlName instanceof SQLMethodInvokeExpr) {
        return DruidDdlParser.unescapeName(((SQLMethodInvokeExpr) sqlName).getMethodName());
    } else if (sqlName instanceof MySqlOrderingExpr) {
        return getSqlName(((MySqlOrderingExpr) sqlName).getExpr());
    } else {
        return sqlName.toString();
    }
}
 
Example 12
Source Project: canal   Source File: DruidDdlParser.java    License: Apache License 2.0 6 votes vote down vote up
private static void processName(DdlResult ddlResult, String schema, SQLExpr sqlName, boolean isOri) {
    if (sqlName == null) {
        if (StringUtils.isNotBlank(schema)) {
            ddlResult.setSchemaName(schema);
        }
        return;
    }

    String table = null;
    if (sqlName instanceof SQLPropertyExpr) {
        SQLIdentifierExpr owner = (SQLIdentifierExpr) ((SQLPropertyExpr) sqlName).getOwner();
        schema = unescapeName(owner.getName());
        table = unescapeName(((SQLPropertyExpr) sqlName).getName());
    } else if (sqlName instanceof SQLIdentifierExpr) {
        table = unescapeName(((SQLIdentifierExpr) sqlName).getName());
    }

    if (isOri) {
        ddlResult.setOriSchemaName(schema);
        ddlResult.setOriTableName(table);
    } else {
        ddlResult.setSchemaName(schema);
        ddlResult.setTableName(table);
    }
}
 
Example 13
Source Project: Mycat2   Source File: ContextExecuter.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean visit(SQLMethodInvokeExpr x) {
    List<SQLExpr> arguments = x.getArguments();
    String methodName = x.getMethodName();
    SQLObject parent = x.getParent();
    if (parent instanceof SQLReplaceable) {
        Map<String, MySQLFunction> functions = context.functions();
        if (functions != null) {
            MySQLFunction mySQLFunction = functions.get(methodName);
            if (mySQLFunction != null) {
                String[] strings = arguments.stream().map(i -> SQLUtils.normalize(Objects.toString(i))).toArray(i -> new String[i]);
                Object res = mySQLFunction.eval(context, strings);
                SQLExpr sqlExpr;
                if (res instanceof SQLValuableExpr){
                    sqlExpr =(SQLValuableExpr) res;
                }else {
                    sqlExpr = SQLExprUtils.fromJavaObject(res);
                }
                sqlExpr.setParent(parent);
                ((SQLReplaceable) parent).replace(x, sqlExpr);

                try {
                    if (parent instanceof SQLSelectItem) {
                        ((SQLSelectItem) parent).setAlias(x.toString());
                    }
                } catch (Throwable ignored) {

                }
            }
        }
    }
    return super.visit(x);
}
 
Example 14
Source Project: Mycat2   Source File: ContextExecuter.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean visit(SQLAssignItem x) {
    SQLExpr target = x.getTarget();//不处理
    SQLExpr value = x.getValue();
    value.accept(this);
    return false;//不需要再次遍历
}
 
Example 15
Source Project: Mycat2   Source File: BetweenNode.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public SQLObject toParseTree() {
    return new SQLBetweenExpr(
            (SQLExpr) testExpr.toParseTree(),
            (SQLExpr) begin.toParseTree(),
            (SQLExpr) end.toParseTree()
    );
}
 
Example 16
Source Project: Mycat2   Source File: SqlToExprTranslator.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void convertGroupBy(Blackboard blackboard, SQLSelectGroupByClause groupBy) {
    List<SqlValue> groupList = new ArrayList<>();
    for (SQLExpr item : groupBy.getItems()) {
        groupList.add(convertExpr(item));
    }
    SQLExpr having = groupBy.getHaving();
    if (having != null) {
        blackboard.addGroupBy(groupList, convertExpr(having));
    } else {
        blackboard.addGroupBy(groupList, null);
    }
}
 
Example 17
Source Project: Mycat2   Source File: SqlToExprTranslator.java    License: GNU General Public License v3.0 5 votes vote down vote up
private QueryPlan convertValuesTable(SQLValuesTableSource from) {
    for (SQLListExpr value : from.getValues()) {
        List<SQLExpr> items = value.getItems();
        for (SQLExpr item : items) {
            SqlValue sqlAbs = convertExpr(item);
        }
    }
    throw new UnsupportedOperationException();//类型问题,暂时不实现

}
 
Example 18
Source Project: Mycat2   Source File: RexTranslator.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void endVisit(SQLIntervalExpr x) {
    SQLExpr value = x.getValue();
    if (value instanceof SQLValuableExpr) {
        Object value1 = ((SQLValuableExpr) value).getValue();
        String unit = x.getUnit().name();
        result = relBuilder.intervalLiteral(value1, unit);
        return;
    }
    throw new UnsupportedOperationException();
}
 
Example 19
Source Project: Mycat2   Source File: RexTranslator.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void endVisit(SQLCastExpr x) {
    boolean auto = x.isTry();
    SQLExpr expr = x.getExpr();
    SQLDataType dataType = x.computeDataType();
    Integer prec = getPrec(dataType);
    Integer scale = getScale(dataType);
    String type = dataType.getName();
    result = relBuilder.cast(convertExpr(expr), Type.of(type,true).getBase(), prec, scale, auto);
}
 
Example 20
Source Project: Mycat2   Source File: Converters.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static SQLColumnDefinition getColumnDef(SQLExpr sqlExpr) {
    SQLColumnDefinition resolvedColumn = null;
    if (sqlExpr instanceof SQLIdentifierExpr) {
        resolvedColumn = ((SQLIdentifierExpr)sqlExpr).getResolvedColumn();
    } else if (sqlExpr instanceof SQLPropertyExpr) {
        resolvedColumn = ((SQLPropertyExpr)sqlExpr).getResolvedColumn();
    } else {
        return null;
    }
    return resolvedColumn;
}
 
Example 21
Source Project: Mycat2   Source File: ConditionCollector.java    License: GNU General Public License v3.0 5 votes vote down vote up
public boolean visit(SQLBinaryOpExprGroup x) {
    SQLBinaryOperator operator = x.getOperator();
    switch (operator) {
        default:
        case BooleanOr:
            failureBecauseIndeterminacy(x);
            break;
        case BooleanAnd:
            List<SQLExpr> items = x.getItems();
            int size = items.size();
            for (int i = 0; i < size; i++) {
                SQLExpr first = items.get(i);
                if (first instanceof SQLBinaryOpExpr) {
                    SQLBinaryOpExpr leftBinaryOpExpr = (SQLBinaryOpExpr) first;
                    ColumnValue firstValue = getWhereRangeLeftValue(leftBinaryOpExpr.getLeft(),
                            leftBinaryOpExpr.getOperator(),
                            leftBinaryOpExpr.getRight());
                    if (firstValue != null) {
                        for (int j = i + 1; j < size; j++) {
                            SQLExpr second = items.get(j);
                            addWhereRange(firstValue, second);
                        }
                    }
                }
            }
    }
    return super.visit(x);
}
 
Example 22
Source Project: Mycat2   Source File: ConditionCollector.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void addWhereRange(ColumnValue firstValue, SQLExpr second) {
    if (second instanceof SQLBinaryOpExpr) {
        SQLBinaryOpExpr rightBinaryOpExpr = (SQLBinaryOpExpr) second;
        ColumnValue secondValue = getWhereRangeRightValue(rightBinaryOpExpr.getLeft(),
                rightBinaryOpExpr.getOperator(),
                rightBinaryOpExpr.getRight());
        if (secondValue != null) {
            if (firstValue.tableSource.equals(secondValue.tableSource) &&
                    firstValue.column.equals(secondValue.column) && firstValue.operator.equals(secondValue.operator)) {
                addRangeValues(new ColumnRangeValue(firstValue.column, firstValue.value, secondValue.value, firstValue.tableSource));
            }
        }
    }
}
 
Example 23
Source Project: Mycat2   Source File: ConditionCollector.java    License: 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 24
Source Project: Mycat2   Source File: SelectSQLHandler.java    License: GNU General Public License v3.0 5 votes vote down vote up
private boolean hanldeInformationSchema(Response response, SQLSelectStatement ast, TableSourceExtractor tableSourceExtractor) {

        ast.accept(tableSourceExtractor);
        boolean cantainsInformation_schema = tableSourceExtractor.getTableSources().stream().anyMatch(new Predicate<SQLExprTableSource>() {
            @Override
            public boolean test(SQLExprTableSource sqlExprTableSource) {
                SQLExpr expr = sqlExprTableSource.getExpr();
                if (expr instanceof SQLPropertyExpr) {
                    SQLExpr owner = ((SQLPropertyExpr) expr).getOwner();
                    if (owner instanceof SQLIdentifierExpr) {
                        return "information_schema".equalsIgnoreCase(((SQLIdentifierExpr) owner).normalizedName());
                    }
                    return "information_schema".equalsIgnoreCase(((SQLPropertyExpr) expr).getName());

                }
                return false;
            }
        });
        if (cantainsInformation_schema) {
            try (DefaultConnection connection = JdbcRuntime.INSTANCE.getConnection(ReplicaSelectorRuntime.INSTANCE.getPrototypeOrFirstReplicaDataSource())) {
                try (RowBaseIterator rowBaseIterator = connection.executeQuery(ast.toString())) {
                    response.sendResultSet(() -> rowBaseIterator, () -> Arrays.asList(ast.toString()));
                    return true;
                }
            }
        }
        return false;
    }
 
Example 25
Source Project: Mycat2   Source File: ParseHelper.java    License: GNU General Public License v3.0 5 votes vote down vote up
default public Long concertLimitGetOffset(SQLLimit limitInfo) {
    SQLExpr offset = limitInfo.getOffset();
    if (offset == null) return 0L;
    if (offset instanceof SQLNumericLiteralExpr) {
        return ((SQLNumericLiteralExpr) offset).getNumber().longValue();
    }
    return null;
}
 
Example 26
Source Project: Mycat2   Source File: ParseHelper.java    License: GNU General Public License v3.0 5 votes vote down vote up
default public Long concertLimitGetLimit(SQLLimit limitInfo) {
    SQLExpr rowCount = limitInfo.getRowCount();
    if (rowCount == null) return 0L;
    if (rowCount instanceof SQLNumericLiteralExpr) {
        return ((SQLNumericLiteralExpr) rowCount).getNumber().longValue();
    }
    return null;
}
 
Example 27
Source Project: Mycat2   Source File: ShowStatementRewriter.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static String showTableStatus(MySqlShowTableStatusStatement ast, String databaseName, String tableName) {
    if (databaseName == null) {
        throw new MycatException(1046, "No database selected");
    }
    String like = ast.getLike() == null ? new SQLBooleanExpr(true).toString() : " TABLE_NAME like "+ast.getLike();
    SQLExpr where = ast.getWhere() == null ? new SQLBooleanExpr(true) : ast.getWhere();

    String schemaCondition = " TABLE_SCHEMA = '" + databaseName + "' ";
    String tableCondition = tableName != null ? " TABLE_NAME = '" + tableName + "' " : " true ";

    SQLSelectBuilderImpl sqlSelectBuilder = new SQLSelectBuilderImpl(DbType.mysql);
    String sql = sqlSelectBuilder
            .selectWithAlias("TABLE_NAME", "Name")
            .selectWithAlias("ENGINE", "Engine")
            .selectWithAlias("VERSION", "Version")
            .selectWithAlias("ROW_FORMAT", "Row_format")
            .selectWithAlias("AVG_ROW_LENGTH", "Avg_row_length")
            .selectWithAlias("DATA_LENGTH", "Data_length")
            .selectWithAlias("MAX_DATA_LENGTH", "Data_length")
            .selectWithAlias("INDEX_LENGTH", "Max_data_length")
            .selectWithAlias("DATA_FREE", "Data_free")
            .selectWithAlias("AUTO_INCREMENT", "Auto_increment")
            .selectWithAlias("CREATE_TIME", "UPDATE_TIME")
            .selectWithAlias("UPDATE_TIME", "Update_time")
            .selectWithAlias("CHECK_TIME", "Check_time")
            .selectWithAlias("TABLE_COLLATION", "Collation")
            .selectWithAlias("CHECKSUM", "Checksum")
            .selectWithAlias("CREATE_OPTIONS", "Create_options")
            .selectWithAlias("TABLE_COMMENT", "Comment")
            .from("information_schema.`TABLES`")
            .whereAnd(schemaCondition)
            .whereAnd(tableCondition)
            .whereAnd(like.toString())
            .whereAnd(where.toString())
            .toString();
    LOGGER.info(ast + "->" + sql);
    return sql;
}
 
Example 28
Source Project: canal   Source File: SqlParser.java    License: Apache License 2.0 5 votes vote down vote up
public static String parse4WhereItem(MySqlSelectQueryBlock sqlSelectQueryBlock) {
    SQLExpr sqlExpr = sqlSelectQueryBlock.getWhere();
    if (sqlExpr != null) {
        return SQLUtils.toMySqlString(sqlExpr);
    }
    return null;
}
 
Example 29
Source Project: Mycat2   Source File: SqlToExprTranslator.java    License: GNU General Public License v3.0 4 votes vote down vote up
private QueryPlan convertValues(SQLValuesQuery sqlSelectQuery) {
    List<SQLExpr> values = sqlSelectQuery.getValues();
    return null;
}
 
Example 30
Source Project: Mycat2   Source File: SqlToExprTranslator.java    License: GNU General Public License v3.0 4 votes vote down vote up
private SqlValue convertExpr(SQLExpr where) {
    RexTranslator rexTranslator = new RexTranslator(rexBuilder);
    where.accept(rexTranslator);
    return rexTranslator.result;
}