use of io.mycat.MycatException in project Mycat2 by MyCATApache.
the class PartitionByFileMap method init.
@Override
public void init(ShardingTableHandler tableHandler, Map<String, Object> prot, Map<String, Object> range) {
URL resource = this.getClass().getClassLoader().getResource("");
LOGGER.info("PartitionByFileMap mapFile default path:{}", resource);
System.out.println("PartitionByFileMap mapFile default path:" + resource);
this.type = Objects.toString(prot.get("type"));
defaultNode = Integer.parseInt(Objects.toString(prot.get("defaultNode")));
switch(type) {
case "Integer":
transformation = Integer::parseInt;
break;
case "Byte":
transformation = Byte::parseByte;
break;
case "Char":
transformation = (i) -> i.charAt(0);
break;
case "String":
transformation = (i) -> i;
break;
case "Long":
transformation = Long::parseLong;
break;
case "Double":
transformation = Double::parseDouble;
break;
case "Float":
transformation = Float::parseFloat;
break;
case "Short":
transformation = Short::parseShort;
break;
case "Boolean":
transformation = Boolean::parseBoolean;
break;
case "BigInteger":
transformation = BigInteger::new;
break;
case "BigDecimal":
transformation = BigDecimal::new;
break;
default:
throw new MycatException("unsupport type!!");
}
Map<String, Object> map = getRangeFromPropertyOrRangeConfig(PartitionByFileMap.class, prot, range);
for (Entry<String, Object> entry : map.entrySet()) {
Object key = transformation.apply(entry.getKey());
int value = Integer.parseInt(Objects.toString(entry.getValue()));
app2Partition.put(key, value);
}
if (defaultNode > 0) {
app2Partition.put(DEFAULT_NODE, defaultNode);
}
partitionNum = new HashSet<>(app2Partition.values()).size();
}
use of io.mycat.MycatException in project Mycat2 by MyCATApache.
the class ShowStatementRewriter method rewriteShowTables.
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;
}
use of io.mycat.MycatException in project Mycat2 by MyCATApache.
the class MycatCalciteMySqlNodeVisitor method visit.
@Override
public boolean visit(SQLUnionQuery x) {
SqlNode[] nodes;
if (x.getRelations().size() > 2) {
nodes = new SqlNode[x.getRelations().size()];
for (int i = 0; i < x.getRelations().size(); i++) {
nodes[i] = convertToSqlNode(x.getRelations().get(i));
}
} else {
SqlNode left = convertToSqlNode(x.getLeft());
SqlNode right = convertToSqlNode(x.getRight());
nodes = new SqlNode[] { left, right };
}
// order by
SqlNodeList orderBySqlNode = null;
SQLOrderBy orderBy = x.getOrderBy();
if (orderBy != null) {
orderBySqlNode = convertOrderby(orderBy);
}
// limit
SqlNode offset = null;
SqlNode fetch = null;
SQLLimit limit = x.getLimit();
if (limit != null) {
offset = convertToSqlNode(limit.getOffset());
fetch = convertToSqlNode(limit.getRowCount());
}
SQLUnionOperator operator = x.getOperator();
SqlNode union = null;
switch(operator) {
case UNION_ALL:
union = new SqlBasicCall(SqlStdOperatorTable.UNION_ALL, nodes, SqlParserPos.ZERO);
break;
case UNION:
case DISTINCT:
union = new SqlBasicCall(SqlStdOperatorTable.UNION, nodes, SqlParserPos.ZERO);
break;
case INTERSECT:
union = new SqlBasicCall(SqlStdOperatorTable.INTERSECT, nodes, SqlParserPos.ZERO);
break;
case EXCEPT:
union = new SqlBasicCall(SqlStdOperatorTable.EXCEPT, nodes, SqlParserPos.ZERO);
break;
default:
throw new MycatException("unsupported join type: " + operator);
}
if (null == orderBy && null == offset && null == fetch) {
sqlNode = union;
} else {
// org/apache/calcite/calcite-core/1.23.0/calcite-core-1.23.0-sources.jar!/org/apache/calcite/sql/validate/SqlValidatorImpl.java:1353
sqlNode = new SqlOrderBy(SqlParserPos.ZERO, union, orderBySqlNode, offset, fetch);
}
return false;
}
use of io.mycat.MycatException in project Mycat2 by MyCATApache.
the class AnalyzeHanlder method onExecute.
@Override
protected Future<Void> onExecute(SQLRequest<MySqlAnalyzeStatement> request, MycatDataContext dataContext, Response response) {
MySqlAnalyzeStatement ast = request.getAst();
List<SQLExprTableSource> tableSources = Optional.ofNullable(ast.getTableSources()).orElse(Collections.emptyList());
if (tableSources.isEmpty()) {
return response.sendError(new MycatException("need tables"));
} else {
ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
resultSetBuilder.addColumnInfo("Table", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("Op", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("Msg_type", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("Msg_Text", JDBCType.VARCHAR);
for (SQLExprTableSource tableSource : tableSources) {
String schemaName = SQLUtils.normalize(tableSource.getSchema());
String tableName = SQLUtils.normalize(tableSource.getTableName());
resultSetBuilder.addObjectRowPayload(Arrays.asList(schemaName + "." + tableName, "analyze", "status", "OK"));
MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
TableHandler tableHandler = metadataManager.getTable(schemaName, tableName);
if (tableHandler == null) {
return response.sendError(new MycatException(tableSource + "不存在"));
}
StatisticCenter statisticCenter = MetaClusterCurrent.wrapper(StatisticCenter.class);
statisticCenter.fetchTableRowCount(tableHandler);
}
return response.sendResultSet(resultSetBuilder.build());
}
}
use of io.mycat.MycatException in project Mycat2 by MyCATApache.
the class MycatMonitorCallback method getSession.
static Session getSession() {
SessionThread thread = getThread();
Session curSession = thread.getCurSession();
if (curSession instanceof MycatSession) {
return curSession;
} else if (curSession instanceof MySQLClientSession) {
MycatSession mycatSession = ((MySQLClientSession) curSession).getMycat();
if (mycatSession == null) {
return curSession;
} else {
return mycatSession;
}
} else {
throw new MycatException("unknown session");
}
}
Aggregations