com.alibaba.fastsql.sql.ast.expr.SQLIdentifierExpr Java Examples

The following examples show how to use com.alibaba.fastsql.sql.ast.expr.SQLIdentifierExpr. 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: DruidDdlParser.java    From canal-1.1.3 with 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 #2
Source File: MemoryTableMeta.java    From canal with 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 File: DruidDdlParser.java    From canal with 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
Source File: Converters.java    From Mycat2 with 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 #5
Source File: ShowCreateTableSQLHandler.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@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 #6
Source File: ShowTablesSQLHandler.java    From Mycat2 with 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 #7
Source File: PreProcesssor.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean visit(SQLIdentifierExpr x) {
    SQLTableSource peek = stack.peek();
    if (peek != null) {
        SQLPropertyExpr propertyExpr = new SQLPropertyExpr(peek.computeAlias(), x.normalizedName());
        replaceInParent(x, propertyExpr, x.getParent());
    }
    return false;
}
 
Example #8
Source File: PreProcesssor.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
    public void endVisit(SQLIdentifierExpr x) {
        SQLTableSource peek = stack.peek();
        if (peek != null) {
//            SQLColumnDefinition columnDefinition =  peek.findColumn(x);
        }
        super.endVisit(x);
    }
 
Example #9
Source File: ShowTableStatusSQLHandler.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@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;
    }