use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.
the class ShowTables method getTableSet.
public static Map<String, String> getTableSet(String cSchema, ShowCreateStmtInfo info) {
// remove the table which is not created but configured
SchemaMeta schemata = DbleServer.getInstance().getTmManager().getCatalogs().get(cSchema);
if (schemata == null) {
return new HashMap<>();
}
Map meta = schemata.getTableMetas();
TreeMap<String, String> tableMap = new TreeMap<>();
Map<String, SchemaConfig> schemas = DbleServer.getInstance().getConfig().getSchemas();
if (null != info.getLike()) {
String p = "^" + info.getLike().replaceAll("%", ".*");
Pattern pattern = Pattern.compile(p, Pattern.CASE_INSENSITIVE);
Matcher maLike;
for (TableConfig tbConfig : schemas.get(cSchema).getTables().values()) {
String tbName = tbConfig.getName();
maLike = pattern.matcher(tbName);
if (maLike.matches() && meta.get(tbName) != null) {
String tbType = tbConfig.getTableType() == TableConfig.TableTypeEnum.TYPE_GLOBAL_TABLE ? "GLOBAL TABLE" : "SHARDING TABLE";
tableMap.put(tbName, tbType);
}
}
} else {
for (TableConfig tbConfig : schemas.get(cSchema).getTables().values()) {
String tbName = tbConfig.getName();
if (meta.get(tbName) != null) {
String tbType = tbConfig.getTableType() == TableConfig.TableTypeEnum.TYPE_GLOBAL_TABLE ? "GLOBAL TABLE" : "SHARDING TABLE";
tableMap.put(tbName, tbType);
}
}
}
return tableMap;
}
use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.
the class HintSchemaHandler method route.
/**
* @param schema
* @param sqlType
* @param realSQL
* @param sc
* @param cachePool
* @param hintSQLValue
* @return
* @throws SQLNonTransientException
*/
@Override
public RouteResultset route(SchemaConfig schema, int sqlType, String realSQL, ServerConnection sc, LayerCachePool cachePool, String hintSQLValue, int hintSqlType, Map hintMap) throws SQLException {
SchemaConfig tempSchema = DbleServer.getInstance().getConfig().getSchemas().get(hintSQLValue);
if (tempSchema != null) {
return routeStrategy.route(tempSchema, sqlType, realSQL, sc, cachePool);
} else {
String msg = "can't find hint schema:" + hintSQLValue;
LOGGER.info(msg);
throw new SQLNonTransientException(msg);
}
}
use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.
the class DruidDeleteParser method visitorParse.
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
String schemaName = schema == null ? null : schema.getName();
MySqlDeleteStatement delete = (MySqlDeleteStatement) stmt;
SQLTableSource tableSource = delete.getTableSource();
SQLTableSource fromSource = delete.getFrom();
if (fromSource != null) {
tableSource = fromSource;
}
if (tableSource instanceof SQLJoinTableSource) {
StringPtr sqlSchema = new StringPtr(null);
if (!SchemaUtil.isNoSharding(sc, (SQLJoinTableSource) tableSource, stmt, schemaName, sqlSchema)) {
String msg = "DELETE query with multiple tables is not supported, sql:" + stmt;
throw new SQLNonTransientException(msg);
} else {
if (delete.getWhere() != null && !SchemaUtil.isNoSharding(sc, delete.getWhere(), schemaName, sqlSchema)) {
String msg = "DELETE query with sub-query is not supported, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
String realSchema = sqlSchema.get() == null ? schemaName : sqlSchema.get();
SchemaConfig schemaConfig = DbleServer.getInstance().getConfig().getSchemas().get(realSchema);
rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), realSchema));
RouterUtil.routeToSingleNode(rrs, schemaConfig.getDataNode());
rrs.setFinishedRoute(true);
return schema;
}
} else {
SQLExprTableSource deleteTableSource = (SQLExprTableSource) tableSource;
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, deleteTableSource);
if (!ServerPrivileges.checkPrivilege(sc, schemaInfo.getSchema(), schemaInfo.getTable(), CheckType.DELETE)) {
String msg = "The statement DML privilege check is not passed, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
schema = schemaInfo.getSchemaConfig();
rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
if (RouterUtil.isNoSharding(schema, schemaInfo.getTable())) {
if (delete.getWhere() != null && !SchemaUtil.isNoSharding(sc, delete.getWhere(), schemaName, new StringPtr(schemaInfo.getSchema()))) {
String msg = "DELETE query with sub-query is not supported, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
RouterUtil.routeToSingleNode(rrs, schema.getDataNode());
return schema;
}
super.visitorParse(schema, rrs, stmt, visitor, sc);
if (visitor.isHasSubQuery()) {
String msg = "DELETE query with sub-query is not supported, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
TableConfig tc = schema.getTables().get(schemaInfo.getTable());
if (tc != null && tc.isGlobalTable()) {
RouterUtil.routeToMultiNode(false, rrs, tc.getDataNodes(), tc.isGlobalTable());
rrs.setFinishedRoute(true);
return schema;
}
}
return schema;
}
use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.
the class DruidUpdateParser method visitorParse.
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
MySqlUpdateStatement update = (MySqlUpdateStatement) stmt;
SQLTableSource tableSource = update.getTableSource();
String schemaName = schema == null ? null : schema.getName();
if (tableSource instanceof SQLJoinTableSource) {
StringPtr sqlSchema = new StringPtr(null);
if (!SchemaUtil.isNoSharding(sc, (SQLJoinTableSource) tableSource, stmt, schemaName, sqlSchema)) {
String msg = "UPDATE query with multiple tables is not supported, sql:" + stmt;
throw new SQLNonTransientException(msg);
} else {
if (update.getWhere() != null && !SchemaUtil.isNoSharding(sc, update.getWhere(), schemaName, sqlSchema)) {
String msg = "UPDATE query with sub-query is not supported, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
String realSchema = sqlSchema.get() == null ? schemaName : sqlSchema.get();
SchemaConfig schemaConfig = DbleServer.getInstance().getConfig().getSchemas().get(realSchema);
rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), realSchema));
RouterUtil.routeToSingleNode(rrs, schemaConfig.getDataNode());
rrs.setFinishedRoute(true);
return schema;
}
} else {
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, (SQLExprTableSource) tableSource);
if (!ServerPrivileges.checkPrivilege(sc, schemaInfo.getSchema(), schemaInfo.getTable(), ServerPrivileges.CheckType.UPDATE)) {
String msg = "The statement DML privilege check is not passed, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
schema = schemaInfo.getSchemaConfig();
String tableName = schemaInfo.getTable();
rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
if (RouterUtil.isNoSharding(schema, tableName)) {
if (update.getWhere() != null && !SchemaUtil.isNoSharding(sc, update.getWhere(), schemaName, new StringPtr(schemaInfo.getSchema()))) {
String msg = "UPDATE query with sub-query is not supported, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
RouterUtil.routeToSingleNode(rrs, schema.getDataNode());
rrs.setFinishedRoute(true);
return schema;
}
super.visitorParse(schema, rrs, stmt, visitor, sc);
if (visitor.isHasSubQuery()) {
String msg = "UPDATE query with sub-query is not supported, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
TableConfig tc = schema.getTables().get(tableName);
if (tc.isGlobalTable()) {
if (GlobalTableUtil.useGlobalTableCheck()) {
String sql = convertUpdateSQL(schemaInfo, update, rrs.getStatement());
rrs.setStatement(sql);
}
RouterUtil.routeToMultiNode(false, rrs, tc.getDataNodes(), tc.isGlobalTable());
rrs.setFinishedRoute(true);
return schema;
}
String partitionColumn = tc.getPartitionColumn();
String joinKey = tc.getJoinKey();
confirmShardColumnNotUpdated(update, schema, tableName, partitionColumn, joinKey, rrs);
confirmChildColumnNotUpdated(update, schema, tableName);
if (schema.getTables().get(tableName).isGlobalTable() && ctx.getRouteCalculateUnit().getTablesAndConditions().size() > 1) {
throw new SQLNonTransientException("global table is not supported in multi table related update " + tableName);
}
if (ctx.getTables().size() == 0) {
ctx.addTable(schemaInfo.getTable());
}
}
return schema;
}
use of com.actiontech.dble.config.model.SchemaConfig 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);
}
Aggregations