use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.
the class ShowIndex method response.
public static void response(ServerConnection c, String stmt) {
try {
String table;
String schema;
String strWhere = "";
// show index with where :druid has a bug :no where
int whereIndex = stmt.toLowerCase().indexOf("where");
if (whereIndex > 0) {
strWhere = stmt.substring(whereIndex);
stmt = stmt.substring(0, whereIndex);
}
StringBuilder sql = new StringBuilder();
boolean changeSQL = false;
SQLStatement statement = RouteStrategyFactory.getRouteStrategy().parserSQL(stmt);
if (statement instanceof MySqlShowIndexesStatement) {
MySqlShowIndexesStatement mySqlShowIndexesStatement = (MySqlShowIndexesStatement) statement;
table = StringUtil.removeBackQuote(mySqlShowIndexesStatement.getTable().getSimpleName());
schema = mySqlShowIndexesStatement.getDatabase() == null ? c.getSchema() : mySqlShowIndexesStatement.getDatabase().getSimpleName();
if (schema == null) {
c.writeErrMessage("3D000", "No database selected", ErrorCode.ER_NO_DB_ERROR);
return;
}
if (mySqlShowIndexesStatement.getDatabase() != null) {
mySqlShowIndexesStatement.setDatabase(null);
sql.append(mySqlShowIndexesStatement.toString());
changeSQL = true;
}
} else if (statement instanceof MySqlShowKeysStatement) {
MySqlShowKeysStatement mySqlShowKeysStatement = (MySqlShowKeysStatement) statement;
table = StringUtil.removeBackQuote(mySqlShowKeysStatement.getTable().getSimpleName());
schema = mySqlShowKeysStatement.getDatabase() == null ? c.getSchema() : mySqlShowKeysStatement.getDatabase().getSimpleName();
if (schema == null) {
c.writeErrMessage("3D000", "No database selected", ErrorCode.ER_NO_DB_ERROR);
return;
}
if (mySqlShowKeysStatement.getDatabase() != null) {
mySqlShowKeysStatement.setDatabase(null);
sql.append(mySqlShowKeysStatement.toString());
changeSQL = true;
}
} else {
c.writeErrMessage(ErrorCode.ER_PARSE_ERROR, stmt);
return;
}
// show index with where :druid has a bug :no where
if (changeSQL && whereIndex > 0 && !sql.toString().toLowerCase().contains("where")) {
sql.append(" ");
sql.append(strWhere);
}
if (DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
schema = StringUtil.removeBackQuote(schema).toLowerCase();
table = table.toLowerCase();
}
SchemaInfo schemaInfo = new SchemaInfo(schema, table);
c.routeSystemInfoAndExecuteSQL(sql.length() > 0 ? sql.toString() : stmt, schemaInfo, ServerParse.SHOW);
} catch (Exception e) {
c.writeErrMessage(ErrorCode.ER_PARSE_ERROR, e.toString());
}
}
use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.
the class DruidCreateIndexParser method visitorParse.
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
SQLCreateIndexStatement createStmt = (SQLCreateIndexStatement) stmt;
SQLTableSource tableSource = createStmt.getTable();
if (tableSource instanceof SQLExprTableSource) {
String schemaName = schema == null ? null : schema.getName();
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, (SQLExprTableSource) tableSource);
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();
} else {
String msg = "The DDL is not supported, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
}
use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.
the class DruidDropTableParser method visitorParse.
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
SQLDropTableStatement dropTable = (SQLDropTableStatement) stmt;
if (dropTable.getTableSources().size() > 1) {
String msg = "dropping multi-tables is not supported, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
String schemaName = schema == null ? null : schema.getName();
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, dropTable.getTableSources().get(0));
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 DruidTruncateTableParser 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();
SQLTruncateStatement truncateTable = (SQLTruncateStatement) stmt;
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, truncateTable.getTableSources().get(0));
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 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;
}
Aggregations