use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo 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.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.
the class DruidAlterTableParser method visitorParse.
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
SQLAlterTableStatement alterTable = (SQLAlterTableStatement) stmt;
String schemaName = schema == null ? null : schema.getName();
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, alterTable.getTableSource());
boolean support = false;
String msg = "The DDL is not supported, sql:";
for (SQLAlterTableItem alterItem : alterTable.getItems()) {
if (alterItem instanceof SQLAlterTableAddColumn || alterItem instanceof SQLAlterTableAddIndex || alterItem instanceof SQLAlterTableDropIndex || alterItem instanceof SQLAlterTableDropKey || alterItem instanceof SQLAlterTableDropPrimaryKey) {
support = true;
} else if (alterItem instanceof SQLAlterTableAddConstraint) {
SQLConstraint constraint = ((SQLAlterTableAddConstraint) alterItem).getConstraint();
if (constraint instanceof MySqlPrimaryKey) {
support = true;
}
} else if (alterItem instanceof MySqlAlterTableChangeColumn || alterItem instanceof MySqlAlterTableModifyColumn || alterItem instanceof SQLAlterTableDropColumnItem) {
List<SQLName> columnList = new ArrayList<>();
if (alterItem instanceof MySqlAlterTableChangeColumn) {
columnList.add(((MySqlAlterTableChangeColumn) alterItem).getColumnName());
} else if (alterItem instanceof MySqlAlterTableModifyColumn) {
columnList.add(((MySqlAlterTableModifyColumn) alterItem).getNewColumnDefinition().getName());
} else if (alterItem instanceof SQLAlterTableDropColumnItem) {
columnList = ((SQLAlterTableDropColumnItem) alterItem).getColumns();
}
support = !this.columnInfluenceCheck(columnList, schemaInfo.getSchemaConfig(), schemaInfo.getTable());
if (!support) {
msg = "The columns may be sharding keys or ER keys, are not allowed to alter sql:";
}
}
}
if (!support) {
msg = msg + stmt;
throw new SQLNonTransientException(msg);
}
String statement = RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema());
rrs.setStatement(statement);
if (RouterUtil.isNoSharding(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
RouterUtil.routeToSingleDDLNode(schemaInfo, rrs);
return schemaInfo.getSchemaConfig();
}
if (GlobalTableUtil.useGlobalTableCheck() && GlobalTableUtil.isGlobalTable(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
String sql = modifyColumnIfAlter(schemaInfo, rrs.getStatement(), alterTable);
rrs.setSrcStatement(sql);
sql = RouterUtil.removeSchema(sql, schemaInfo.getSchema());
rrs.setStatement(sql);
}
RouterUtil.routeToDDLNode(schemaInfo, rrs);
return schemaInfo.getSchemaConfig();
}
use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.
the class DruidCreateTableParser method visitorParse.
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
MySqlCreateTableStatement createStmt = (MySqlCreateTableStatement) stmt;
// disable create table select from
if (createStmt.getSelect() != null) {
String msg = "create table from other table not supported :" + stmt;
LOGGER.info(msg);
throw new SQLNonTransientException(msg);
}
// disable create table select from
if (createStmt.getLike() != null) {
String msg = "create table like other table not supported :" + stmt;
LOGGER.info(msg);
throw new SQLNonTransientException(msg);
}
String schemaName = schema == null ? null : schema.getName();
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, createStmt.getTableSource());
String statement = RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema());
rrs.setStatement(statement);
if (RouterUtil.isNoSharding(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
RouterUtil.routeToSingleDDLNode(schemaInfo, rrs);
return schemaInfo.getSchemaConfig();
}
sharingTableCheck(createStmt);
if (GlobalTableUtil.useGlobalTableCheck() && GlobalTableUtil.isGlobalTable(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
String sql = addColumnIfCreate(createStmt);
rrs.setSrcStatement(sql);
sql = RouterUtil.removeSchema(sql, schemaInfo.getSchema());
rrs.setStatement(sql);
}
try {
RouterUtil.routeToDDLNode(schemaInfo, rrs);
} catch (SQLException e) {
String msg = "Table '" + schemaInfo.getSchema() + "." + schemaInfo.getTable() + "' doesn't exist in the config of schema";
throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
}
return schemaInfo.getSchemaConfig();
}
use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.
the class DruidDropIndexParser 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();
SQLDropIndexStatement dropStmt = (SQLDropIndexStatement) stmt;
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, dropStmt.getTableName());
String statement = RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema());
rrs.setStatement(statement);
if (RouterUtil.isNoSharding(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
RouterUtil.routeToSingleDDLNode(schemaInfo, rrs);
return schemaInfo.getSchemaConfig();
}
RouterUtil.routeToDDLNode(schemaInfo, rrs);
return schemaInfo.getSchemaConfig();
}
use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.
the class DescribeHandler method handle.
public static void handle(String stmt, ServerConnection c) {
try {
SQLStatement statement = RouteStrategyFactory.getRouteStrategy().parserSQL(stmt);
MySqlExplainStatement describeStatement = (MySqlExplainStatement) statement;
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(c.getUser(), c.getSchema(), describeStatement.getTableName(), null);
c.routeSystemInfoAndExecuteSQL(RouterUtil.removeSchema(stmt, schemaInfo.getSchema()), schemaInfo, ServerParse.DESCRIBE);
} catch (Exception e) {
c.writeErrMessage(ErrorCode.ER_PARSE_ERROR, e.toString());
return;
}
}
Aggregations