use of com.actiontech.dble.config.model.TableConfig in project dble by actiontech.
the class DDLRouteTest method testDDLDefaultNode.
@Test
public void testDDLDefaultNode() throws Exception {
SchemaConfig schema = schemaMap.get("solo1");
CacheService cacheService = new CacheService(false);
RouteService routerService = new RouteService(cacheService);
// create table/view/function/..
String sql = " create table company(idd int)";
sql = RouterUtil.getFixedSql(sql);
String upsql = sql.toUpperCase();
String tablename = "COMPANY";
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(schema, sqlType, sql, 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 = "COMPANY";
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(schema, sqlType, sql, 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 = "COMPANY";
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(schema, sqlType, sql, 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 = "COMPANY";
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(schema, sqlType, sql, 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 = "COMPANY";
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(schema, sqlType, sql, null);
Assert.assertTrue("COMPANY".equals(tablename));
Assert.assertTrue(rrs.getNodes().length == nodeSize);
}
use of com.actiontech.dble.config.model.TableConfig in project dble by actiontech.
the class DDLRouteTest method testSpecialCharDDL.
@Test
public void testSpecialCharDDL() throws Exception {
SchemaConfig schema = schemaMap.get("TESTDB");
CacheService cacheService = new CacheService(false);
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 = "COMPANY";
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(schema, sqlType, sql, null);
Assert.assertTrue("COMPANY".equals(tablename));
Assert.assertTrue(rrs.getNodes().length == nodeSize);
}
use of com.actiontech.dble.config.model.TableConfig in project dble by actiontech.
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("Not passed without exception is not correct");
Assert.assertTrue(false);
} else {
System.out.println("Passed without exception. Maybe the partition key exists in update statement,but not update in fact");
Assert.assertTrue(true);
}
} catch (Exception e) {
if (throwException) {
System.out.println(e.getCause().getClass());
Assert.assertTrue(e.getCause() instanceof SQLNonTransientException);
System.out.println("SQLNonTransientException is expected");
} else {
System.out.println("need checked");
Assert.assertTrue(false);
}
}
}
use of com.actiontech.dble.config.model.TableConfig in project dble by actiontech.
the class RouterUtil method getRandomDataNode.
/**
* getRandomDataNode
*
* @param schema
* @param table
* @return datanode
* @author mycat
*/
private static String getRandomDataNode(SchemaConfig schema, String table) throws SQLException {
String dataNode = null;
Map<String, TableConfig> tables = schema.getTables();
TableConfig tc;
if (tables != null && (tc = tables.get(table)) != null) {
dataNode = tc.getRandomDataNode();
} else {
String msg = "Table '" + schema.getName() + "." + table + "' doesn't exist";
throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
}
return dataNode;
}
use of com.actiontech.dble.config.model.TableConfig in project dble by actiontech.
the class RouterUtil method routeToDDLNode.
public static void routeToDDLNode(SchemaInfo schemaInfo, RouteResultset rrs) throws SQLException {
String stmt = getFixedSql(removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
List<String> dataNodes;
Map<String, TableConfig> tables = schemaInfo.getSchemaConfig().getTables();
TableConfig tc = tables.get(schemaInfo.getTable());
if (tc != null) {
dataNodes = tc.getDataNodes();
} else {
String msg = "Table '" + schemaInfo.getSchema() + "." + schemaInfo.getTable() + "' doesn't exist";
throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
}
Iterator<String> iterator1 = dataNodes.iterator();
int nodeSize = dataNodes.size();
RouteResultsetNode[] nodes = new RouteResultsetNode[nodeSize];
for (int i = 0; i < nodeSize; i++) {
String name = iterator1.next();
nodes[i] = new RouteResultsetNode(name, ServerParse.DDL, stmt);
}
rrs.setNodes(nodes);
rrs.setSchema(schemaInfo.getSchema());
rrs.setTable(schemaInfo.getTable());
rrs.setFinishedRoute(true);
}
Aggregations