Search in sources :

Example 46 with SchemaConfig

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

the class ShowFullTables method getTableSet.

private static Set<String> getTableSet(ServerConnection c, Map<String, String> parm) {
    TreeSet<String> tableSet = new TreeSet<String>();
    MycatConfig conf = MycatServer.getInstance().getConfig();
    Map<String, UserConfig> users = conf.getUsers();
    UserConfig user = users == null ? null : users.get(c.getUser());
    if (user != null) {
        Map<String, SchemaConfig> schemas = conf.getSchemas();
        for (String name : schemas.keySet()) {
            if (null != parm.get(SCHEMA_KEY) && parm.get(SCHEMA_KEY).toUpperCase().equals(name.toUpperCase())) {
                if (null == parm.get("LIKE_KEY")) {
                    tableSet.addAll(schemas.get(name).getTables().keySet());
                } else {
                    String p = "^" + parm.get("LIKE_KEY").replaceAll("%", ".*");
                    Pattern pattern = Pattern.compile(p, Pattern.CASE_INSENSITIVE);
                    Matcher ma;
                    for (String tname : schemas.get(name).getTables().keySet()) {
                        ma = pattern.matcher(tname);
                        if (ma.matches()) {
                            tableSet.add(tname);
                        }
                    }
                }
            }
        }
        ;
    }
    return tableSet;
}
Also used : Pattern(java.util.regex.Pattern) SchemaConfig(io.mycat.config.model.SchemaConfig) Matcher(java.util.regex.Matcher) TreeSet(java.util.TreeSet) MycatConfig(io.mycat.config.MycatConfig) UserConfig(io.mycat.config.model.UserConfig)

Example 47 with SchemaConfig

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

the class ShowFullTables 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");
    }
    //分库的schema,直接从SchemaConfig中获取所有表名
    Map<String, String> parm = buildFields(c, stmt);
    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;
    fields[i + 1] = PacketUtil.getField("Table_type  ", Fields.FIELD_TYPE_VAR_STRING);
    fields[i + 1].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.add(StringUtil.encode("BASE TABLE", 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 48 with SchemaConfig

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

the class ShowTables method getTableSet.

private static Set<String> getTableSet(ServerConnection c, Map<String, String> parm) {
    TreeSet<String> tableSet = new TreeSet<String>();
    MycatConfig conf = MycatServer.getInstance().getConfig();
    Map<String, UserConfig> users = conf.getUsers();
    UserConfig user = users == null ? null : users.get(c.getUser());
    if (user != null) {
        Map<String, SchemaConfig> schemas = conf.getSchemas();
        for (String name : schemas.keySet()) {
            if (null != parm.get(SCHEMA_KEY) && parm.get(SCHEMA_KEY).toUpperCase().equals(name.toUpperCase())) {
                if (null == parm.get("LIKE_KEY")) {
                    tableSet.addAll(schemas.get(name).getTables().keySet());
                } else {
                    String p = "^" + parm.get("LIKE_KEY").replaceAll("%", ".*");
                    Pattern pattern = Pattern.compile(p, Pattern.CASE_INSENSITIVE);
                    Matcher ma;
                    for (String tname : schemas.get(name).getTables().keySet()) {
                        ma = pattern.matcher(tname);
                        if (ma.matches()) {
                            tableSet.add(tname);
                        }
                    }
                }
            }
        }
        ;
    }
    return tableSet;
}
Also used : Pattern(java.util.regex.Pattern) SchemaConfig(io.mycat.config.model.SchemaConfig) Matcher(java.util.regex.Matcher) TreeSet(java.util.TreeSet) MycatConfig(io.mycat.config.MycatConfig) UserConfig(io.mycat.config.model.UserConfig)

Example 49 with SchemaConfig

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

the class ConfigTest method testDynamicYYYYMMTable.

/**
     * 测试 动态日期表
     *
     * @throws Exception
     */
@Test
public void testDynamicYYYYMMTable() throws Exception {
    SchemaConfig sc = this.schemas.get("dbtest1");
    Map<String, TableConfig> tbm = sc.getTables();
    Assert.assertTrue(tbm.size() == 32);
}
Also used : SchemaConfig(io.mycat.config.model.SchemaConfig) TableConfig(io.mycat.config.model.TableConfig) Test(org.junit.Test)

Example 50 with SchemaConfig

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

the class DQLRouteTest method test.

@Test
public void test() throws Exception {
    String stmt = "select * from `offer` where id = 100";
    SchemaConfig schema = schemaMap.get("mysqldb");
    RouteResultset rrs = new RouteResultset(stmt, 7);
    SQLStatementParser parser = null;
    if (schema.isNeedSupportMultiDBType()) {
        parser = new MycatStatementParser(stmt);
    } else {
        parser = new MySqlStatementParser(stmt);
    }
    SQLStatement statement;
    MycatSchemaStatVisitor visitor = null;
    try {
        statement = parser.parseStatement();
        visitor = new MycatSchemaStatVisitor();
    } catch (Exception t) {
        throw new SQLSyntaxErrorException(t);
    }
    ctx = new DruidShardingParseInfo();
    ctx.setSql(stmt);
    List<RouteCalculateUnit> taskList = visitorParse(rrs, statement, visitor);
    Assert.assertEquals(true, !taskList.get(0).getTablesAndConditions().isEmpty());
}
Also used : DruidShardingParseInfo(io.mycat.route.parser.druid.DruidShardingParseInfo) RouteCalculateUnit(io.mycat.route.parser.druid.RouteCalculateUnit) SchemaConfig(io.mycat.config.model.SchemaConfig) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MycatStatementParser(io.mycat.route.parser.druid.MycatStatementParser) MycatSchemaStatVisitor(io.mycat.route.parser.druid.MycatSchemaStatVisitor) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) Test(org.junit.Test)

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