use of io.mycat.config.model.SystemConfig in project Mycat_plus by coderczp.
the class RouteStrategyFactory method init.
public static void init() {
SystemConfig config = MycatServer.getInstance().getConfig().getSystem();
String defaultSqlParser = config.getDefaultSqlParser();
defaultSqlParser = defaultSqlParser == null ? "" : defaultSqlParser;
// 修改为ConcurrentHashMap,避免并发问题
strategyMap.putIfAbsent("druidparser", new DruidMycatRouteStrategy());
defaultStrategy = strategyMap.get(defaultSqlParser);
if (defaultStrategy == null) {
defaultStrategy = strategyMap.get("druidparser");
defaultSqlParser = "druidparser";
}
config.setDefaultSqlParser(defaultSqlParser);
isInit = true;
}
use of io.mycat.config.model.SystemConfig in project Mycat_plus by coderczp.
the class DDLRouteTest method testDDLDefaultNode.
@Test
public void testDDLDefaultNode() throws Exception {
SchemaConfig schema = schemaMap.get("solo1");
CacheService cacheService = new CacheService();
RouteService routerService = new RouteService(cacheService);
// create table/view/function/..
String sql = " create table company(idd int)";
sql = RouterUtil.getFixedSql(sql);
String upsql = sql.toUpperCase();
// TODO:modify by zhuam 小写表名,转为大写比较
String tablename = RouterUtil.getTableName(sql, RouterUtil.getCreateTablePos(upsql, 0));
tablename = tablename.toUpperCase();
List<String> dataNodes = new ArrayList<>();
Map<String, TableConfig> tables = schema.getTables();
TableConfig tc;
if (tables != null && (tc = tables.get(tablename)) != null) {
dataNodes = tc.getDataNodes();
}
int nodeSize = dataNodes.size();
if (nodeSize == 0 && schema.getDataNode() != null) {
nodeSize = 1;
}
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);
// drop table test
sql = " drop table COMPANY";
sql = RouterUtil.getFixedSql(sql);
upsql = sql.toUpperCase();
tablename = RouterUtil.getTableName(sql, RouterUtil.getDropTablePos(upsql, 0));
tables = schema.getTables();
if (tables != null && (tc = tables.get(tablename)) != null) {
dataNodes = tc.getDataNodes();
}
nodeSize = dataNodes.size();
if (nodeSize == 0 && schema.getDataNode() != null) {
nodeSize = 1;
}
rs = ServerParse.parse(sql);
sqlType = rs & 0xff;
rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
Assert.assertTrue("COMPANY".equals(tablename));
Assert.assertTrue(rrs.getNodes().length == nodeSize);
// drop table test
sql = " drop table if exists COMPANY";
sql = RouterUtil.getFixedSql(sql);
upsql = sql.toUpperCase();
tablename = RouterUtil.getTableName(sql, RouterUtil.getDropTablePos(upsql, 0));
tables = schema.getTables();
if (tables != null && (tc = tables.get(tablename)) != null) {
dataNodes = tc.getDataNodes();
}
nodeSize = dataNodes.size();
if (nodeSize == 0 && schema.getDataNode() != null) {
nodeSize = 1;
}
rs = ServerParse.parse(sql);
sqlType = rs & 0xff;
rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
Assert.assertTrue("COMPANY".equals(tablename));
Assert.assertTrue(rrs.getNodes().length == nodeSize);
// alter table
sql = " alter table COMPANY add COLUMN name int ;";
sql = RouterUtil.getFixedSql(sql);
upsql = sql.toUpperCase();
tablename = RouterUtil.getTableName(sql, RouterUtil.getAlterTablePos(upsql, 0));
tables = schema.getTables();
if (tables != null && (tc = tables.get(tablename)) != null) {
dataNodes = tc.getDataNodes();
}
nodeSize = dataNodes.size();
if (nodeSize == 0 && schema.getDataNode() != null) {
nodeSize = 1;
}
rs = ServerParse.parse(sql);
sqlType = rs & 0xff;
rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
Assert.assertTrue("COMPANY".equals(tablename));
Assert.assertTrue(rrs.getNodes().length == nodeSize);
// truncate table;
sql = " truncate table COMPANY";
sql = RouterUtil.getFixedSql(sql);
upsql = sql.toUpperCase();
tablename = RouterUtil.getTableName(sql, RouterUtil.getTruncateTablePos(upsql, 0));
tables = schema.getTables();
if (tables != null && (tc = tables.get(tablename)) != null) {
dataNodes = tc.getDataNodes();
}
nodeSize = dataNodes.size();
if (nodeSize == 0 && schema.getDataNode() != null) {
nodeSize = 1;
}
rs = ServerParse.parse(sql);
sqlType = rs & 0xff;
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.SystemConfig in project Mycat_plus by coderczp.
the class DDLRouteTest method testDDL.
/**
* ddl deal test
*
* @throws Exception
*/
@Test
public void testDDL() throws Exception {
SchemaConfig schema = schemaMap.get("TESTDB");
CacheService cacheService = new CacheService();
RouteService routerService = new RouteService(cacheService);
// create table/view/function/..
String sql = " create table company(idd int)";
sql = RouterUtil.getFixedSql(sql);
String upsql = sql.toUpperCase();
// TODO : modify by zhuam
// 小写表名,需要额外转为 大写 做比较
String tablename = RouterUtil.getTableName(sql, RouterUtil.getCreateTablePos(upsql, 0));
tablename = tablename.toUpperCase();
List<String> dataNodes = new ArrayList<>();
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);
// drop table test
sql = " drop table COMPANY";
sql = RouterUtil.getFixedSql(sql);
upsql = sql.toUpperCase();
tablename = RouterUtil.getTableName(sql, RouterUtil.getDropTablePos(upsql, 0));
tables = schema.getTables();
if (tables != null && (tc = tables.get(tablename)) != null) {
dataNodes = tc.getDataNodes();
}
nodeSize = dataNodes.size();
rs = ServerParse.parse(sql);
sqlType = rs & 0xff;
rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
Assert.assertTrue("COMPANY".equals(tablename));
Assert.assertTrue(rrs.getNodes().length == nodeSize);
// alter table
sql = " alter table COMPANY add COLUMN name int ;";
sql = RouterUtil.getFixedSql(sql);
upsql = sql.toUpperCase();
tablename = RouterUtil.getTableName(sql, RouterUtil.getAlterTablePos(upsql, 0));
tables = schema.getTables();
if (tables != null && (tc = tables.get(tablename)) != null) {
dataNodes = tc.getDataNodes();
}
nodeSize = dataNodes.size();
rs = ServerParse.parse(sql);
sqlType = rs & 0xff;
rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
Assert.assertTrue("COMPANY".equals(tablename));
Assert.assertTrue(rrs.getNodes().length == nodeSize);
// truncate table;
sql = " truncate table COMPANY";
sql = RouterUtil.getFixedSql(sql);
upsql = sql.toUpperCase();
tablename = RouterUtil.getTableName(sql, RouterUtil.getTruncateTablePos(upsql, 0));
tables = schema.getTables();
if (tables != null && (tc = tables.get(tablename)) != null) {
dataNodes = tc.getDataNodes();
}
nodeSize = dataNodes.size();
rs = ServerParse.parse(sql);
sqlType = rs & 0xff;
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.SystemConfig in project Mycat_plus by coderczp.
the class DeleteSqlParseTest method testDeleteToRoute.
@Test
public void testDeleteToRoute() throws SQLNonTransientException {
String sql = "delete t from offer as t ";
SchemaConfig schema = schemaMap.get("config");
RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
Assert.assertEquals(128, rrs.getNodes().length);
}
use of io.mycat.config.model.SystemConfig in project Mycat_plus by coderczp.
the class DruidMysqlRouteStrategyTest method testOr.
/**
* 测试or语句的路由
*
* @throws Exception
*/
@Test
public void testOr() throws Exception {
// 0-200M=0
// 200M1-400M=1
// 400M1-600M=2
// 600M1-800M=3
// 800M1-1000M=4
SchemaConfig schema = schemaMap.get("TESTDB");
String sql = "select * from customer where sharding_id=10000 or 1=1;";
RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
Assert.assertTrue(rrs.getNodes().length == 2);
Assert.assertTrue(rrs.getNodes()[0].getName().equals("dn1"));
Assert.assertTrue(rrs.getNodes()[1].getName().equals("dn2"));
sql = "select * from customer where sharding_id = 10000 or sharding_id = 10010";
rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
Assert.assertTrue(rrs.getNodes()[0].getName().equals("dn1"));
Assert.assertTrue(rrs.getNodes()[1].getName().equals("dn2"));
sql = "select * from customer where sharding_id = 10000 or user_id = 'wangwu'";
rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
Assert.assertTrue(rrs.getNodes()[0].getName().equals("dn1"));
Assert.assertTrue(rrs.getNodes()[1].getName().equals("dn2"));
}
Aggregations