use of com.actiontech.dble.config.model.TableConfig in project dble by actiontech.
the class DruidAlterTableParser method influenceKeyColumn.
/**
* this function is check if the name is the important column in any tables
* true -- the column influence some important column
* false -- safe
*/
private boolean influenceKeyColumn(SQLName name, SchemaConfig schema, String tableName) {
String columnName = name.toString();
Map<String, TableConfig> tableConfig = schema.getTables();
TableConfig changedTable = tableConfig.get(tableName);
if (changedTable == null) {
return false;
}
if (columnName.equalsIgnoreCase(changedTable.getPartitionColumn()) || columnName.equalsIgnoreCase(changedTable.getJoinKey())) {
return true;
}
// Traversal all the table node to find if some table is the child table of the changedTale
for (Map.Entry<String, TableConfig> entry : tableConfig.entrySet()) {
TableConfig tb = entry.getValue();
if (tb.getParentTC() != null && tableName.equalsIgnoreCase(tb.getParentTC().getName()) && columnName.equalsIgnoreCase(tb.getParentKey())) {
return true;
}
}
return false;
}
use of com.actiontech.dble.config.model.TableConfig in project dble by actiontech.
the class ServerConnection method routeSystemInfoAndExecuteSQL.
public void routeSystemInfoAndExecuteSQL(String stmt, SchemaUtil.SchemaInfo schemaInfo, int sqlType) {
ServerConfig conf = DbleServer.getInstance().getConfig();
UserConfig user = conf.getUsers().get(this.getUser());
if (user == null || !user.getSchemas().contains(schemaInfo.getSchema())) {
writeErrMessage("42000", "Access denied for user '" + this.getUser() + "' to database '" + schemaInfo.getSchema() + "'", ErrorCode.ER_DBACCESS_DENIED_ERROR);
return;
}
RouteResultset rrs = new RouteResultset(stmt, sqlType);
try {
if (RouterUtil.isNoSharding(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
RouterUtil.routeToSingleNode(rrs, schemaInfo.getSchemaConfig().getDataNode());
} else {
TableConfig tc = schemaInfo.getSchemaConfig().getTables().get(schemaInfo.getTable());
if (tc == null) {
String msg = "Table '" + schemaInfo.getSchema() + "." + schemaInfo.getTable() + "' doesn't exist";
writeErrMessage("42S02", msg, ErrorCode.ER_NO_SUCH_TABLE);
return;
}
RouterUtil.routeToRandomNode(rrs, schemaInfo.getSchemaConfig(), schemaInfo.getTable());
}
session.execute(rrs);
} catch (Exception e) {
executeException(e, stmt);
}
}
use of com.actiontech.dble.config.model.TableConfig 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.TableConfig in project dble by actiontech.
the class DruidSelectParser method isNeedCache.
private boolean isNeedCache(SchemaConfig schema) {
if (ctx.getTables() == null || ctx.getTables().size() == 0) {
return false;
}
TableConfig tc = schema.getTables().get(ctx.getTables().get(0));
if (tc == null || (ctx.getTables().size() == 1 && tc.isGlobalTable())) {
return false;
} else {
// single table
if (ctx.getTables().size() == 1) {
String tableName = ctx.getTables().get(0);
String primaryKey = schema.getTables().get(tableName).getPrimaryKey();
if (ctx.getRouteCalculateUnit().getTablesAndConditions().get(tableName) != null && ctx.getRouteCalculateUnit().getTablesAndConditions().get(tableName).get(primaryKey) != null && tc.getDataNodes().size() > 1) {
// primaryKey condition
return false;
}
}
return true;
}
}
use of com.actiontech.dble.config.model.TableConfig 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;
}
Aggregations