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

The following examples show how to use com.alibaba.fastsql.sql.ast.SQLName. 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: 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 2
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 3
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 4
Source Project: Mycat2   Source File: Scope.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean visit(SQLExprTableSource x) {
    if (x.getExpr() instanceof SQLName) {
        if (x.getSchema() == null) {
            x.setSchema(defaultSchema);
        }
        String alias = x.getAlias();
        if (x.getAlias() == null) {
            alias = x.computeAlias();
        }
        SQLObject pre = scope.put(alias, x);
        if (pre != null) {
            throw new UnsupportedOperationException("歧义的别名:" + alias);
        }
        return false;
    }
    return super.visit(x);
}
 
Example 5
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 6
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 7
@Override
protected ExecuteCode onExecute(SQLRequest<SQLShowCreateTableStatement> request, MycatDataContext dataContext, Response response) {
    SQLShowCreateTableStatement ast = request.getAst();

    SQLName nameExpr = ast.getName();
    if (nameExpr == null) {
        response.sendError(new MycatException("table name is null"));
        return ExecuteCode.PERFORMED;
    }
    String schemaName = dataContext.getDefaultSchema();
    String tableName;
    if (nameExpr instanceof SQLIdentifierExpr) {
        tableName = ((SQLIdentifierExpr) nameExpr).normalizedName();
    }else if (nameExpr instanceof SQLPropertyExpr){
        schemaName =
                ((SQLIdentifierExpr)((SQLPropertyExpr) nameExpr).getOwner()).normalizedName();
        tableName = SQLUtils.normalize(((SQLPropertyExpr) nameExpr).getName());
    }else {
        response.proxyShow(ast);
        return ExecuteCode.PERFORMED;
    }
    TableHandler table = MetadataManager.INSTANCE.getTable(schemaName, tableName);
    if (table == null){
        response.proxyShow(ast);
        return ExecuteCode.PERFORMED;
    }
    String createTableSQL = table.getCreateTableSQL();

    ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
    resultSetBuilder.addColumnInfo("Table", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("Create Table", JDBCType.VARCHAR);
    resultSetBuilder.addObjectRowPayload(Arrays.asList(table.getTableName(),createTableSQL));
    response.sendResultSet(()->resultSetBuilder.build(),()->{throw  new UnsupportedOperationException();});
    return ExecuteCode.PERFORMED;
}
 
Example 8
Source Project: Mycat2   Source File: ShowTablesSQLHandler.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
    protected ExecuteCode onExecute(SQLRequest<SQLShowTablesStatement> request, MycatDataContext dataContext, Response response) {
        SQLShowTablesStatement ast = request.getAst();
        if (ast.getDatabase() == null && dataContext.getDefaultSchema() != null) {
            ast.setDatabase(new SQLIdentifierExpr(dataContext.getDefaultSchema()));
        }
        SQLName database = ast.getDatabase();
        if (database == null){
            response.sendError(new MycatException("NO DATABASES SELECTED"));
            return ExecuteCode.PERFORMED;
        }
        Optional<SchemaHandler> schemaHandler = Optional.ofNullable(MetadataManager.INSTANCE.getSchemaMap()).map(i -> i.get(SQLUtils.normalize(ast.getDatabase().toString())));
        String targetName = schemaHandler.map(i -> i.defaultTargetName()).map(name -> ReplicaSelectorRuntime.INSTANCE.getDatasourceNameByReplicaName(name, true, null)).orElse(null);
        if (targetName != null) {
            response.proxySelect(targetName, ast.toString());
        } else {
            response.proxyShow(ast);
        }
//        DDLManager.INSTANCE.updateTables();
//        String sql = ShowStatementRewriter.rewriteShowTables(dataContext.getDefaultSchema(), request.getAst());
//        LOGGER.info(sql);
//        //show 语句变成select 语句
//
//        try (RowBaseIterator query = MycatDBs.createClient(dataContext).query(sql)) {
//            //schema上默认的targetName;
//            try {
//                SQLShowTablesStatement showTablesStatement = request.getAst();
//                SQLName from = showTablesStatement.getFrom();
//                String schema = SQLUtils.normalize(from == null ? dataContext.getDefaultSchema() : from.getSimpleName());
//                if (WithDefaultTargetInfo(response, sql, query, schema)) return ExecuteCode.PERFORMED;
//            } catch (Exception e) {
//                LOGGER.error("", e);
//            }
//            response.sendResultSet(()->query, null);
//            return ExecuteCode.PERFORMED;
//        }
//        response.proxyShow(ast);
        return ExecuteCode.PERFORMED;
    }
 
Example 9
@Override
    protected ExecuteCode onExecute(SQLRequest<MySqlShowTableStatusStatement> request, MycatDataContext dataContext, Response response) {

        MySqlShowTableStatusStatement ast = request.getAst();
        if (ast.getDatabase() == null && dataContext.getDefaultSchema() != null) {
            ast.setDatabase(new SQLIdentifierExpr(dataContext.getDefaultSchema()));
        }
        SQLName database = ast.getDatabase();
        if (database == null){
            response.sendError(new MycatException("NO DATABASES SELECTED"));
            return ExecuteCode.PERFORMED;
        }
        Optional<SchemaHandler> schemaHandler = Optional.ofNullable(MetadataManager.INSTANCE.getSchemaMap()).map(i -> i.get(SQLUtils.normalize(ast.getDatabase().toString())));
        String targetName = schemaHandler.map(i -> i.defaultTargetName()).map(name -> ReplicaSelectorRuntime.INSTANCE.getDatasourceNameByReplicaName(name, true, null)).orElse(null);
        if (targetName != null) {
            response.proxySelect(targetName, ast.toString());
        } else {
            response.proxyShow(ast);
        }

//        try {
//            DDLManager.INSTANCE.updateTables();
//            String databaseName = ast.getDatabase() == null ? dataContext.getDefaultSchema() :
//                    SQLUtils.normalize(ast.getDatabase().getSimpleName());
//
//            String tableName = ast.getTableGroup() == null ? null
//                    : SQLUtils.normalize(ast.getTableGroup().getSimpleName());
//
//            String sql = ShowStatementRewriter.showTableStatus(ast, databaseName, tableName);
//
//            try (RowBaseIterator query = MycatDBs.createClient(dataContext).query(sql)) {
//                response.sendResultSet(() -> query, () -> {
//                    throw new UnsupportedOperationException();
//                });
//            }
//        }catch (Exception e){
//            LOGGER.error("",e);
//            response.sendError(e);
//        }
        return ExecuteCode.PERFORMED;
    }