use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.
the class ServerConnection method routeSQL.
public RouteResultset routeSQL(String sql, int type) {
// 检查当前使用的DB
String db = this.schema;
if (db == null) {
writeErrMessage(ErrorCode.ERR_BAD_LOGICDB, "No MyCAT Database selected");
return null;
}
SchemaConfig schema = MycatServer.getInstance().getConfig().getSchemas().get(db);
if (schema == null) {
writeErrMessage(ErrorCode.ERR_BAD_LOGICDB, "Unknown MyCAT Database '" + db + "'");
return null;
}
// 路由计算
RouteResultset rrs = null;
try {
rrs = MycatServer.getInstance().getRouterservice().route(MycatServer.getInstance().getConfig().getSystem(), schema, type, sql, this.charset, this);
} catch (Exception e) {
StringBuilder s = new StringBuilder();
LOGGER.warn(s.append(this).append(sql).toString() + " err:" + e.toString(), e);
String msg = e.getMessage();
writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
return null;
}
return rrs;
}
use of io.mycat.config.model.SchemaConfig 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.SchemaConfig in project Mycat-Server by MyCATApache.
the class ShowMyCATCluster method getRows.
private static List<RowDataPacket> getRows(ServerConnection c) {
List<RowDataPacket> rows = new LinkedList<RowDataPacket>();
MycatConfig config = MycatServer.getInstance().getConfig();
MycatCluster cluster = config.getCluster();
Map<String, SchemaConfig> schemas = config.getSchemas();
SchemaConfig schema = (c.getSchema() == null) ? null : schemas.get(c.getSchema());
// 如果没有指定schema或者schema为null,则使用全部集群。
if (schema == null) {
Map<String, MycatNode> nodes = cluster.getNodes();
for (MycatNode n : nodes.values()) {
if (n != null && n.isOnline()) {
rows.add(getRow(n, c.getCharset()));
}
}
} else {
Map<String, MycatNode> nodes = cluster.getNodes();
for (MycatNode n : nodes.values()) {
if (n != null && n.isOnline()) {
rows.add(getRow(n, c.getCharset()));
}
}
}
if (rows.size() == 0) {
alarm.error(Alarms.CLUSTER_EMPTY + c.toString());
}
return rows;
}
use of io.mycat.config.model.SchemaConfig in project Mycat-Server by MyCATApache.
the class ShowTables 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");
return;
}
//分库的schema,直接从SchemaConfig中获取所有表名
Map<String, String> parm = buildFields(c, stmt);
java.util.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;
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.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);
}
use of io.mycat.config.model.SchemaConfig 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);
}
}
}
Aggregations