Search in sources :

Example 6 with SchemaConfig

use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.

the class ServerConnection method routeSQL.

public RouteResultset routeSQL(String sql, int type) {
    // 检查当前使用的DB
    String db = this.schema;
    if (db == null) {
        writeErrMessage(ErrorCode.ERR_BAD_LOGICDB, "No MyCAT Database selected");
        return null;
    }
    SchemaConfig schema = MycatServer.getInstance().getConfig().getSchemas().get(db);
    if (schema == null) {
        writeErrMessage(ErrorCode.ERR_BAD_LOGICDB, "Unknown MyCAT Database '" + db + "'");
        return null;
    }
    // 路由计算
    RouteResultset rrs = null;
    try {
        rrs = MycatServer.getInstance().getRouterservice().route(MycatServer.getInstance().getConfig().getSystem(), schema, type, sql, this.charset, this);
    } catch (Exception e) {
        StringBuilder s = new StringBuilder();
        LOGGER.warn(s.append(this).append(sql).toString() + " err:" + e.toString(), e);
        String msg = e.getMessage();
        writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
        return null;
    }
    return rrs;
}
Also used : SchemaConfig(io.mycat.config.model.SchemaConfig) IOException(java.io.IOException) RouteResultset(io.mycat.route.RouteResultset)

Example 7 with SchemaConfig

use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.

the class GlobalTableUtil method getGlobalTable.

private static void getGlobalTable() {
    MycatConfig config = MycatServer.getInstance().getConfig();
    Map<String, SchemaConfig> schemaMap = config.getSchemas();
    SchemaConfig schemaMconfig = null;
    for (String key : schemaMap.keySet()) {
        if (schemaMap.get(key) != null) {
            schemaMconfig = schemaMap.get(key);
            Map<String, TableConfig> tableMap = schemaMconfig.getTables();
            if (tableMap != null) {
                for (String k : tableMap.keySet()) {
                    TableConfig table = tableMap.get(k);
                    if (table != null && table.isGlobalTable()) {
                        globalTableMap.put(table.getName().toUpperCase(), table);
                    }
                }
            }
        }
    }
}
Also used : SchemaConfig(io.mycat.config.model.SchemaConfig) TableConfig(io.mycat.config.model.TableConfig) MycatConfig(io.mycat.config.MycatConfig)

Example 8 with SchemaConfig

use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.

the class ShowMyCATCluster method getRows.

private static List<RowDataPacket> getRows(ServerConnection c) {
    List<RowDataPacket> rows = new LinkedList<RowDataPacket>();
    MycatConfig config = MycatServer.getInstance().getConfig();
    MycatCluster cluster = config.getCluster();
    Map<String, SchemaConfig> schemas = config.getSchemas();
    SchemaConfig schema = (c.getSchema() == null) ? null : schemas.get(c.getSchema());
    // 如果没有指定schema或者schema为null,则使用全部集群。
    if (schema == null) {
        Map<String, MycatNode> nodes = cluster.getNodes();
        for (MycatNode n : nodes.values()) {
            if (n != null && n.isOnline()) {
                rows.add(getRow(n, c.getCharset()));
            }
        }
    } else {
        Map<String, MycatNode> nodes = cluster.getNodes();
        for (MycatNode n : nodes.values()) {
            if (n != null && n.isOnline()) {
                rows.add(getRow(n, c.getCharset()));
            }
        }
    }
    if (rows.size() == 0) {
        alarm.error(Alarms.CLUSTER_EMPTY + c.toString());
    }
    return rows;
}
Also used : SchemaConfig(io.mycat.config.model.SchemaConfig) MycatNode(io.mycat.config.MycatNode) RowDataPacket(io.mycat.net.mysql.RowDataPacket) MycatCluster(io.mycat.config.MycatCluster) MycatConfig(io.mycat.config.MycatConfig) LinkedList(java.util.LinkedList)

Example 9 with SchemaConfig

use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.

the class ShowTables method response.

/**
	 * response method.
	 * @param c
	 */
public static void response(ServerConnection c, String stmt, int type) {
    String showSchemal = SchemaUtil.parseShowTableSchema(stmt);
    String cSchema = showSchemal == null ? c.getSchema() : showSchemal;
    SchemaConfig schema = MycatServer.getInstance().getConfig().getSchemas().get(cSchema);
    if (schema != null) {
        //不分库的schema,show tables从后端 mysql中查
        String node = schema.getDataNode();
        if (!Strings.isNullOrEmpty(node)) {
            c.execute(stmt, ServerParse.SHOW);
            return;
        }
    } else {
        c.writeErrMessage(ErrorCode.ER_NO_DB_ERROR, "No database selected");
        return;
    }
    //分库的schema,直接从SchemaConfig中获取所有表名
    Map<String, String> parm = buildFields(c, stmt);
    java.util.Set<String> tableSet = getTableSet(c, parm);
    int i = 0;
    byte packetId = 0;
    header.packetId = ++packetId;
    fields[i] = PacketUtil.getField("Tables in " + parm.get(SCHEMA_KEY), Fields.FIELD_TYPE_VAR_STRING);
    fields[i++].packetId = ++packetId;
    eof.packetId = ++packetId;
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    packetId = eof.packetId;
    for (String name : tableSet) {
        RowDataPacket row = new RowDataPacket(FIELD_COUNT);
        row.add(StringUtil.encode(name.toLowerCase(), c.getCharset()));
        row.packetId = ++packetId;
        buffer = row.write(buffer, c, true);
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // post write
    c.write(buffer);
}
Also used : SchemaConfig(io.mycat.config.model.SchemaConfig) RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 10 with SchemaConfig

use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.

the class DruidUpdateParserTest method throwExceptionParse.

public void throwExceptionParse(String sql, boolean throwException) throws NoSuchMethodException {
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement sqlStatement = statementList.get(0);
    MySqlUpdateStatement update = (MySqlUpdateStatement) sqlStatement;
    SchemaConfig schemaConfig = mock(SchemaConfig.class);
    Map<String, TableConfig> tables = mock(Map.class);
    TableConfig tableConfig = mock(TableConfig.class);
    String tableName = "hotnews";
    when((schemaConfig).getTables()).thenReturn(tables);
    when(tables.get(tableName)).thenReturn(tableConfig);
    when(tableConfig.getParentTC()).thenReturn(null);
    RouteResultset routeResultset = new RouteResultset(sql, 11);
    Class c = DruidUpdateParser.class;
    Method method = c.getDeclaredMethod("confirmShardColumnNotUpdated", new Class[] { SQLUpdateStatement.class, SchemaConfig.class, String.class, String.class, String.class, RouteResultset.class });
    method.setAccessible(true);
    try {
        method.invoke(c.newInstance(), update, schemaConfig, tableName, "ID", "", routeResultset);
        if (throwException) {
            System.out.println("未抛异常,解析通过则不对!");
            Assert.assertTrue(false);
        } else {
            System.out.println("未抛异常,解析通过,此情况分片字段可能在update语句中但是实际不会被更新");
            Assert.assertTrue(true);
        }
    } catch (Exception e) {
        if (throwException) {
            System.out.println(e.getCause().getClass());
            Assert.assertTrue(e.getCause() instanceof SQLNonTransientException);
            System.out.println("抛异常原因为SQLNonTransientException则正确");
        } else {
            System.out.println("抛异常,需要检查");
            Assert.assertTrue(false);
        }
    }
}
Also used : SchemaConfig(io.mycat.config.model.SchemaConfig) Method(java.lang.reflect.Method) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLNonTransientException(java.sql.SQLNonTransientException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MySqlUpdateStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement) SQLNonTransientException(java.sql.SQLNonTransientException) TableConfig(io.mycat.config.model.TableConfig) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) DruidUpdateParser(io.mycat.route.parser.druid.impl.DruidUpdateParser) RouteResultset(io.mycat.route.RouteResultset)

Aggregations

SchemaConfig (io.mycat.config.model.SchemaConfig)79 SystemConfig (io.mycat.config.model.SystemConfig)54 RouteResultset (io.mycat.route.RouteResultset)34 Test (org.junit.Test)31 TableConfig (io.mycat.config.model.TableConfig)12 MycatConfig (io.mycat.config.MycatConfig)9 RouteResultsetNode (io.mycat.route.RouteResultsetNode)7 SQLNonTransientException (java.sql.SQLNonTransientException)7 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)6 RowDataPacket (io.mycat.net.mysql.RowDataPacket)6 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)5 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)5 CacheService (io.mycat.cache.CacheService)5 MycatCluster (io.mycat.config.MycatCluster)5 UserConfig (io.mycat.config.model.UserConfig)5 ArrayList (java.util.ArrayList)5 NoSuchElementException (java.util.NoSuchElementException)5 EOFPacket (io.mycat.net.mysql.EOFPacket)4 FieldPacket (io.mycat.net.mysql.FieldPacket)4 RouteService (io.mycat.route.RouteService)4