Search in sources :

Example 36 with TableConfig

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

the class DDLRouteTest method testSpecialCharDDL.

@Test
public void testSpecialCharDDL() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    CacheService cacheService = new CacheService();
    RouteService routerService = new RouteService(cacheService);
    // alter table test
    String sql = " ALTER TABLE COMPANY\r\nADD COLUMN TEST  VARCHAR(255) NULL AFTER CREATE_DATE,\r\n CHARACTER SET = UTF8";
    sql = RouterUtil.getFixedSql(sql);
    List<String> dataNodes = new ArrayList<>();
    String tablename = RouterUtil.getTableName(sql, RouterUtil.getAlterTablePos(sql, 0));
    Map<String, TableConfig> tables = schema.getTables();
    TableConfig tc;
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    int nodeSize = dataNodes.size();
    int rs = ServerParse.parse(sql);
    int sqlType = rs & 0xff;
    RouteResultset rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) ArrayList(java.util.ArrayList) TableConfig(io.mycat.config.model.TableConfig) RouteService(io.mycat.route.RouteService) CacheService(io.mycat.cache.CacheService) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

Example 37 with TableConfig

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

the class MySQLTableStructureDetector method run.

@Override
public void run() {
    for (SchemaConfig schema : MycatServer.getInstance().getConfig().getSchemas().values()) {
        for (TableConfig table : schema.getTables().values()) {
            for (String dataNode : table.getDataNodes()) {
                try {
                    table.getReentrantReadWriteLock().writeLock().lock();
                    ConcurrentHashMap<String, List<String>> map = new ConcurrentHashMap<>();
                    table.setDataNodeTableStructureSQLMap(map);
                } finally {
                    table.getReentrantReadWriteLock().writeLock().unlock();
                }
                OneRawSQLQueryResultHandler resultHandler = new OneRawSQLQueryResultHandler(MYSQL_SHOW_CREATE_TABLE_COLMS, new MySQLTableStructureListener(dataNode, table));
                resultHandler.setMark("Table Structure");
                PhysicalDBNode dn = MycatServer.getInstance().getConfig().getDataNodes().get(dataNode);
                SQLJob sqlJob = new SQLJob(sqlPrefix + table.getName(), dn.getDatabase(), resultHandler, dn.getDbPool().getSource());
                sqlJob.run();
            }
        }
    }
}
Also used : OneRawSQLQueryResultHandler(io.mycat.sqlengine.OneRawSQLQueryResultHandler) PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) SchemaConfig(io.mycat.config.model.SchemaConfig) SQLJob(io.mycat.sqlengine.SQLJob) TableConfig(io.mycat.config.model.TableConfig) List(java.util.List) LinkedList(java.util.LinkedList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 38 with TableConfig

use of io.mycat.config.model.TableConfig 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)

Example 39 with TableConfig

use of io.mycat.config.model.TableConfig 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 40 with TableConfig

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

the class GlobalTableUtil method reGetColumnsForTable.

/**
 * 重新获得table 的列list
 * @param tableName
 */
private static void reGetColumnsForTable(String tableName) {
    MycatConfig config = MycatServer.getInstance().getConfig();
    if (globalTableMap != null && globalTableMap.get(tableName.toUpperCase()) != null) {
        TableConfig tableConfig = globalTableMap.get(tableName.toUpperCase());
        if (// consistencyCheck 在运行中
        tableConfig == null || isInnerColumnCheckFinished != 1)
            return;
        String nodeName = tableConfig.getDataNodes().get(0);
        Map<String, PhysicalDBNode> map = config.getDataNodes();
        for (String k2 : map.keySet()) {
            PhysicalDBNode dBnode = map.get(k2);
            if (nodeName.equals(dBnode.getName())) {
                PhysicalDBPool pool = dBnode.getDbPool();
                List<PhysicalDatasource> dsList = (List<PhysicalDatasource>) pool.genAllDataSources();
                for (PhysicalDatasource ds : dsList) {
                    if (ds instanceof MySQLDataSource) {
                        MySQLDataSource mds = (MySQLDataSource) dsList.get(0);
                        MySQLConsistencyChecker checker = new MySQLConsistencyChecker(mds, tableConfig.getName());
                        checker.checkInnerColumnExist();
                        // 运行一次就行了,不需要像consistencyCheck那样每个db都运行一次
                        return;
                    }
                }
            }
        }
    }
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) MySQLConsistencyChecker(io.mycat.backend.heartbeat.MySQLConsistencyChecker) PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) MySQLDataSource(io.mycat.backend.mysql.nio.MySQLDataSource) TableConfig(io.mycat.config.model.TableConfig) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) ArrayList(java.util.ArrayList) List(java.util.List) MycatConfig(io.mycat.config.MycatConfig)

Aggregations

TableConfig (io.mycat.config.model.TableConfig)84 SchemaConfig (io.mycat.config.model.SchemaConfig)26 SQLNonTransientException (java.sql.SQLNonTransientException)26 Test (org.junit.Test)18 RuleConfig (io.mycat.config.model.rule.RuleConfig)16 RouteResultset (io.mycat.route.RouteResultset)14 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)14 HashMap (java.util.HashMap)14 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)12 SlotFunction (io.mycat.route.function.SlotFunction)12 ArrayList (java.util.ArrayList)12 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)10 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)8 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)8 ConfigException (io.mycat.config.util.ConfigException)8 RouteResultsetNode (io.mycat.route.RouteResultsetNode)8 HashSet (java.util.HashSet)8 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)6 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)6 CacheService (io.mycat.cache.CacheService)6