Search in sources :

Example 21 with MycatException

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();
}
Also used : MycatException(io.mycat.MycatException) URL(java.net.URL) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger)

Example 22 with MycatException

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;
}
Also used : MycatException(io.mycat.MycatException) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 23 with MycatException

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;
}
Also used : MycatException(io.mycat.MycatException) CalciteSqlBasicCall(com.alibaba.druid.support.calcite.CalciteSqlBasicCall) MySqlForceIndexHint(com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint)

Example 24 with MycatException

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());
    }
}
Also used : MySqlAnalyzeStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAnalyzeStatement) MycatException(io.mycat.MycatException) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) MetadataManager(io.mycat.MetadataManager) TableHandler(io.mycat.TableHandler) StatisticCenter(io.mycat.statistic.StatisticCenter) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource)

Example 25 with MycatException

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");
    }
}
Also used : MycatSession(io.mycat.proxy.session.MycatSession) SessionThread(io.mycat.proxy.reactor.SessionThread) MycatException(io.mycat.MycatException) MySQLClientSession(io.mycat.proxy.session.MySQLClientSession) MycatSession(io.mycat.proxy.session.MycatSession) Session(io.mycat.proxy.session.Session) MySQLClientSession(io.mycat.proxy.session.MySQLClientSession)

Aggregations

MycatException (io.mycat.MycatException)31 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLName (com.alibaba.druid.sql.ast.SQLName)3 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)3 MySQLClientSession (io.mycat.proxy.session.MySQLClientSession)3 SQLException (java.sql.SQLException)3 CalciteSqlBasicCall (com.alibaba.druid.support.calcite.CalciteSqlBasicCall)2 ColumnDefPacket (io.mycat.beans.mysql.packet.ColumnDefPacket)2 DatasourceConfig (io.mycat.config.DatasourceConfig)2 ProxyBufferImpl (io.mycat.proxy.buffer.ProxyBufferImpl)2 ReactorEnvThread (io.mycat.proxy.reactor.ReactorEnvThread)2 Future (io.vertx.core.Future)2 PromiseInternal (io.vertx.core.impl.future.PromiseInternal)2 Statement (java.sql.Statement)2 DateString (org.apache.calcite.util.DateString)2 TimeString (org.apache.calcite.util.TimeString)2 TimestampString (org.apache.calcite.util.TimestampString)2 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 SQLShowTablesStatement (com.alibaba.druid.sql.ast.statement.SQLShowTablesStatement)1