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();
}
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());
}
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;
}
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());
}
}
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());
}
Aggregations