Search in sources :

Example 1 with MetadataManager

use of io.mycat.MetadataManager in project Mycat2 by MyCATApache.

the class ComplierContext method getTableObservable.

public Observable<Object[]> getTableObservable(String schemaName, String tableName) {
    MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    TableHandler tableHandler = metadataManager.getTable(schemaName, tableName);
    VisualTableHandler visualTableHandler = (VisualTableHandler) tableHandler;
    return visualTableHandler.scanAll();
}
Also used : MetadataManager(io.mycat.MetadataManager) TableHandler(io.mycat.TableHandler) VisualTableHandler(io.mycat.prototypeserver.mysql.VisualTableHandler) VisualTableHandler(io.mycat.prototypeserver.mysql.VisualTableHandler)

Example 2 with MetadataManager

use of io.mycat.MetadataManager in project Mycat2 by MyCATApache.

the class PrototypeHandlerImpl method showDataBase.

@Override
public List<Object[]> showDataBase(com.alibaba.druid.sql.ast.statement.SQLShowDatabasesStatement sqlShowDatabasesStatement) {
    MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    SQLExpr like = sqlShowDatabasesStatement.getLike();
    List<String> collect = metadataManager.showDatabases();
    if (like != null) {
        PatternMatcher matcher = PatternMatcher.createMysqlPattern(SQLUtils.normalize(like.toString()), false);
        collect = collect.stream().filter(i -> matcher.match(i)).collect(Collectors.toList());
    }
    return collect.stream().map(i -> new Object[] { i }).collect(Collectors.toList());
}
Also used : MetadataManager(io.mycat.MetadataManager) java.util(java.util) Connection(java.sql.Connection) TableHandler(io.mycat.TableHandler) MetaClusterCurrent(io.mycat.MetaClusterCurrent) LoggerFactory(org.slf4j.LoggerFactory) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Supplier(java.util.function.Supplier) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) PrepareExecutor(io.mycat.calcite.PrepareExecutor) SchemaHandler(io.mycat.calcite.table.SchemaHandler) ResultSet(java.sql.ResultSet) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) DatasourceConfig(io.mycat.config.DatasourceConfig) com.alibaba.druid.sql.dialect.mysql.ast.statement(com.alibaba.druid.sql.dialect.mysql.ast.statement) SQLUtils(com.alibaba.druid.sql.SQLUtils) Logger(org.slf4j.Logger) PatternMatcher(org.apache.doris.common.PatternMatcher) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) Collectors(java.util.stream.Collectors) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) NameMap(io.mycat.util.NameMap) Statement(java.sql.Statement) SQLTextLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr) MysqlVariableService(io.mycat.MysqlVariableService) com.alibaba.druid.sql.ast.statement(com.alibaba.druid.sql.ast.statement) MetadataManager(io.mycat.MetadataManager) PatternMatcher(org.apache.doris.common.PatternMatcher) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 3 with MetadataManager

use of io.mycat.MetadataManager in project Mycat2 by MyCATApache.

the class PrototypeHandlerImpl method showColumns.

@Override
public List<Object[]> showColumns(SQLShowColumnsStatement statement) {
    ArrayList<Object[]> objects = new ArrayList<>();
    String schemaName = SQLUtils.normalize(statement.getDatabase().getSimpleName());
    String tableName = Optional.ofNullable((SQLExpr) statement.getTable()).map(i -> SQLUtils.normalize(i.toString())).orElse(null);
    if (tableName == null && statement.getLike() != null) {
        String pattern = SQLUtils.normalize(statement.getLike().toString());
        PatternMatcher mysqlPattern = PatternMatcher.createMysqlPattern(pattern, false);
        MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
        SchemaHandler schemaHandler = metadataManager.getSchemaMap().get(schemaName);
        if (schemaHandler == null)
            return Collections.emptyList();
        List<String> tables = schemaHandler.logicTables().values().stream().map(i -> i.getTableName()).collect(Collectors.toList());
        tableName = tables.stream().filter(i -> mysqlPattern.match(i)).findFirst().orElse(null);
    }
    if (tableName == null) {
        return Collections.emptyList();
    }
    MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    TableHandler table = metadataManager.getTable(schemaName, tableName);
    if (table == null)
        return Collections.emptyList();
    String createTableSQL = table.getCreateTableSQL();
    MySqlCreateTableStatement sqlStatement = (MySqlCreateTableStatement) SQLUtils.parseSingleMysqlStatement(createTableSQL);
    List<SQLColumnDefinition> columns = new ArrayList<SQLColumnDefinition>();
    List<String> dataTypes = new ArrayList<String>();
    List<String> defaultValues = new ArrayList<String>();
    int name_len = -1, dataType_len = -1, defaultVal_len = 7, extra_len = 5;
    for (SQLTableElement element : sqlStatement.getTableElementList()) {
        if (element instanceof SQLColumnDefinition) {
            SQLColumnDefinition column = (SQLColumnDefinition) element;
            columns.add(column);
            String name = SQLUtils.normalize(column.getName().getSimpleName());
            if (name_len < name.length()) {
                name_len = name.length();
            }
            String dataType = column.getDataType().getName();
            if (column.getDataType().getArguments().size() > 0) {
                dataType += "(";
                for (int i = 0; i < column.getDataType().getArguments().size(); i++) {
                    if (i != 0) {
                        dataType += ",";
                    }
                    SQLExpr arg = column.getDataType().getArguments().get(i);
                    dataType += arg.toString();
                }
                dataType += ")";
            }
            if (dataType_len < dataType.length()) {
                dataType_len = dataType.length();
            }
            dataTypes.add(dataType);
            if (column.getDefaultExpr() == null) {
                defaultValues.add(null);
            } else {
                String defaultVal = SQLUtils.toMySqlString(column.getDefaultExpr());
                if (defaultVal.length() > 2 && defaultVal.charAt(0) == '\'' && defaultVal.charAt(defaultVal.length() - 1) == '\'') {
                    defaultVal = defaultVal.substring(1, defaultVal.length() - 1);
                }
                defaultValues.add(defaultVal);
                if (defaultVal_len < defaultVal.length()) {
                    defaultVal_len = defaultVal.length();
                }
            }
            if (column.isAutoIncrement()) {
                extra_len = "auto_increment".length();
            } else if (column.getOnUpdate() != null) {
                extra_len = "on update CURRENT_TIMESTAMP".length();
            }
        }
    }
    for (int i = 0; i < columns.size(); i++) {
        SQLColumnDefinition column = columns.get(i);
        String name = SQLUtils.normalize(column.getName().getSimpleName());
        String Field = name;
        String Type = dataTypes.get(i);
        String Collation = Optional.ofNullable(column.getCollateExpr()).map(s -> SQLUtils.normalize(s.toString())).orElse(null);
        String Null = column.containsNotNullConstaint() ? "NO" : "YES";
        String Key = sqlStatement.isPrimaryColumn(name) ? "PRI" : sqlStatement.isUNI(name) ? "UNI" : sqlStatement.isMUL(name) ? "MUL" : "";
        String Default = Optional.ofNullable(defaultValues.get(i)).orElse("NULL");
        String Extra = "";
        String Privileges = "select,insert,update,references";
        String Comment = Optional.ofNullable(column.getComment()).map(s -> s.toString()).orElse("");
        if (statement.isFull()) {
            objects.add(new Object[] { Field, Type, Collation, Null, Key, Default, Extra, Privileges, Comment });
        } else {
            objects.add(new Object[] { Field, Type, Null, Key, Default, Extra });
        }
    }
    return objects;
}
Also used : MetadataManager(io.mycat.MetadataManager) java.util(java.util) Connection(java.sql.Connection) TableHandler(io.mycat.TableHandler) MetaClusterCurrent(io.mycat.MetaClusterCurrent) LoggerFactory(org.slf4j.LoggerFactory) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Supplier(java.util.function.Supplier) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) PrepareExecutor(io.mycat.calcite.PrepareExecutor) SchemaHandler(io.mycat.calcite.table.SchemaHandler) ResultSet(java.sql.ResultSet) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) DatasourceConfig(io.mycat.config.DatasourceConfig) com.alibaba.druid.sql.dialect.mysql.ast.statement(com.alibaba.druid.sql.dialect.mysql.ast.statement) SQLUtils(com.alibaba.druid.sql.SQLUtils) Logger(org.slf4j.Logger) PatternMatcher(org.apache.doris.common.PatternMatcher) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) Collectors(java.util.stream.Collectors) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) NameMap(io.mycat.util.NameMap) Statement(java.sql.Statement) SQLTextLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr) MysqlVariableService(io.mycat.MysqlVariableService) com.alibaba.druid.sql.ast.statement(com.alibaba.druid.sql.ast.statement) SchemaHandler(io.mycat.calcite.table.SchemaHandler) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) MetadataManager(io.mycat.MetadataManager) TableHandler(io.mycat.TableHandler) PatternMatcher(org.apache.doris.common.PatternMatcher)

Example 4 with MetadataManager

use of io.mycat.MetadataManager in project Mycat2 by MyCATApache.

the class PrototypeHandlerImpl method showTables.

@Override
public List<Object[]> showTables(SQLShowTablesStatement statement) {
    String schemaName = SQLUtils.normalize(statement.getDatabase().getSimpleName());
    MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    NameMap<SchemaHandler> schemaMap = metadataManager.getSchemaMap();
    SchemaHandler schemaHandler = schemaMap.get(schemaName);
    if (schemaHandler == null)
        return Collections.emptyList();
    Collection<String> strings = schemaHandler.logicTables().values().stream().map(i -> i.getTableName()).collect(Collectors.toList());
    ;
    SQLExpr like = statement.getLike();
    if (like == null) {
        NameMap<TableHandler> tables = schemaHandler.logicTables();
        strings = tables.keySet();
    } else if (like instanceof SQLTextLiteralExpr) {
        PatternMatcher matcher = PatternMatcher.createMysqlPattern(((SQLTextLiteralExpr) like).getText(), false);
        strings = strings.stream().filter(i -> matcher.match(i)).collect(Collectors.toList());
    }
    if (statement.isFull()) {
        return strings.stream().sorted().map(i -> new Object[] { i, "BASE TABLE" }).collect(Collectors.toList());
    } else {
        return strings.stream().sorted().map(i -> new Object[] { i }).collect(Collectors.toList());
    }
}
Also used : SchemaHandler(io.mycat.calcite.table.SchemaHandler) MetadataManager(io.mycat.MetadataManager) java.util(java.util) Connection(java.sql.Connection) TableHandler(io.mycat.TableHandler) MetaClusterCurrent(io.mycat.MetaClusterCurrent) LoggerFactory(org.slf4j.LoggerFactory) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Supplier(java.util.function.Supplier) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) PrepareExecutor(io.mycat.calcite.PrepareExecutor) SchemaHandler(io.mycat.calcite.table.SchemaHandler) ResultSet(java.sql.ResultSet) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) DatasourceConfig(io.mycat.config.DatasourceConfig) com.alibaba.druid.sql.dialect.mysql.ast.statement(com.alibaba.druid.sql.dialect.mysql.ast.statement) SQLUtils(com.alibaba.druid.sql.SQLUtils) Logger(org.slf4j.Logger) PatternMatcher(org.apache.doris.common.PatternMatcher) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) Collectors(java.util.stream.Collectors) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) NameMap(io.mycat.util.NameMap) Statement(java.sql.Statement) SQLTextLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr) MysqlVariableService(io.mycat.MysqlVariableService) com.alibaba.druid.sql.ast.statement(com.alibaba.druid.sql.ast.statement) MetadataManager(io.mycat.MetadataManager) TableHandler(io.mycat.TableHandler) SQLTextLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr) PatternMatcher(org.apache.doris.common.PatternMatcher) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 5 with MetadataManager

use of io.mycat.MetadataManager in project Mycat2 by MyCATApache.

the class PrototypeHandlerImpl method showTableStatus.

@Override
public List<Object[]> showTableStatus(MySqlShowTableStatusStatement statement) {
    String database = SQLUtils.normalize(statement.getDatabase().getSimpleName());
    SQLExpr like = statement.getLike();
    PatternMatcher matcher = PatternMatcher.createMysqlPattern(SQLUtils.normalize(like.toString()), false);
    MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    SchemaHandler schemaHandler = metadataManager.getSchemaMap().get(database);
    if (schemaHandler == null)
        return Collections.emptyList();
    return schemaHandler.logicTables().values().stream().map(i -> i.getTableName()).filter(i -> matcher.match(i)).map(tableName -> {
        String Name = tableName;
        String Engine = "InnoDB";
        String Version = "10";
        String Row_format = "Dynamic";
        String Rows = "0";
        String Avg_row_length = "0";
        String Data_length = "800000";
        String Max_data_length = "0";
        String Index_length = "800000";
        String Data_free = "0";
        String Auto_increment = null;
        String Create_time = "2021-10-26 14:32:03";
        String Update_time = null;
        String Check_time = null;
        String Collation = "utf8_general_ci";
        String Checksum = null;
        String Create_options = null;
        String Comment = "";
        return new Object[] { tableName, Name, Engine, Version, Row_format, Rows, Avg_row_length, Data_length, Max_data_length, Index_length, Data_free, Auto_increment, Create_time, Update_time, Check_time, Collation, Checksum, Create_options, Comment };
    }).collect(Collectors.toList());
}
Also used : SchemaHandler(io.mycat.calcite.table.SchemaHandler) MetadataManager(io.mycat.MetadataManager) java.util(java.util) Connection(java.sql.Connection) TableHandler(io.mycat.TableHandler) MetaClusterCurrent(io.mycat.MetaClusterCurrent) LoggerFactory(org.slf4j.LoggerFactory) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Supplier(java.util.function.Supplier) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) PrepareExecutor(io.mycat.calcite.PrepareExecutor) SchemaHandler(io.mycat.calcite.table.SchemaHandler) ResultSet(java.sql.ResultSet) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) DatasourceConfig(io.mycat.config.DatasourceConfig) com.alibaba.druid.sql.dialect.mysql.ast.statement(com.alibaba.druid.sql.dialect.mysql.ast.statement) SQLUtils(com.alibaba.druid.sql.SQLUtils) Logger(org.slf4j.Logger) PatternMatcher(org.apache.doris.common.PatternMatcher) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) Collectors(java.util.stream.Collectors) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) NameMap(io.mycat.util.NameMap) Statement(java.sql.Statement) SQLTextLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr) MysqlVariableService(io.mycat.MysqlVariableService) com.alibaba.druid.sql.ast.statement(com.alibaba.druid.sql.ast.statement) MetadataManager(io.mycat.MetadataManager) PatternMatcher(org.apache.doris.common.PatternMatcher) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

MetadataManager (io.mycat.MetadataManager)34 ShardingTable (io.mycat.calcite.table.ShardingTable)22 Test (org.junit.Test)22 Partition (io.mycat.Partition)17 ShardingTableConfig (io.mycat.config.ShardingTableConfig)12 CustomRuleFunction (io.mycat.router.CustomRuleFunction)12 RangeVariable (io.mycat.RangeVariable)11 DrdsSqlCompiler (io.mycat.DrdsSqlCompiler)10 ValuePredicateAnalyzer (io.mycat.calcite.rewriter.ValuePredicateAnalyzer)10 java.util (java.util)10 Map (java.util.Map)10 RexNode (org.apache.calcite.rex.RexNode)10 TableHandler (io.mycat.TableHandler)9 DefaultConnection (io.mycat.datasource.jdbc.datasource.DefaultConnection)7 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)7 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)5 ImmutableMap (com.google.common.collect.ImmutableMap)5 MetaClusterCurrent (io.mycat.MetaClusterCurrent)5 MycatCalciteSupport (io.mycat.calcite.MycatCalciteSupport)5 Connection (java.sql.Connection)5