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);
}
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();
}
}
}
}
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);
}
}
}
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);
}
}
}
}
}
}
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;
}
}
}
}
}
}
Aggregations