Search in sources :

Example 21 with TableConfig

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;
}
Also used : TableConfig(com.actiontech.dble.config.model.TableConfig) Map(java.util.Map)

Example 22 with TableConfig

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);
    }
}
Also used : ServerConfig(com.actiontech.dble.config.ServerConfig) TableConfig(com.actiontech.dble.config.model.TableConfig) UserConfig(com.actiontech.dble.config.model.UserConfig) SQLNonTransientException(java.sql.SQLNonTransientException) SQLException(java.sql.SQLException) IOException(java.io.IOException) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 23 with TableConfig

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;
}
Also used : MySqlDeleteStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement) SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) StringPtr(com.actiontech.dble.plan.common.ptr.StringPtr) TableConfig(com.actiontech.dble.config.model.TableConfig) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLJoinTableSource(com.alibaba.druid.sql.ast.statement.SQLJoinTableSource) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 24 with TableConfig

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;
    }
}
Also used : TableConfig(com.actiontech.dble.config.model.TableConfig)

Example 25 with TableConfig

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;
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) StringPtr(com.actiontech.dble.plan.common.ptr.StringPtr) TableConfig(com.actiontech.dble.config.model.TableConfig) MySqlUpdateStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Aggregations

TableConfig (com.actiontech.dble.config.model.TableConfig)32 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)16 SQLNonTransientException (java.sql.SQLNonTransientException)16 SQLException (java.sql.SQLException)7 RouteResultsetNode (com.actiontech.dble.route.RouteResultsetNode)6 SchemaInfo (com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)5 RouteResultset (com.actiontech.dble.route.RouteResultset)4 AbstractPartitionAlgorithm (com.actiontech.dble.route.function.AbstractPartitionAlgorithm)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)4 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)4 CacheService (com.actiontech.dble.cache.CacheService)3 ServerConfig (com.actiontech.dble.config.ServerConfig)3 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)3 Matcher (java.util.regex.Matcher)3 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)2 FetchStoreNodeOfChildTableHandler (com.actiontech.dble.backend.mysql.nio.handler.FetchStoreNodeOfChildTableHandler)2 EOFPacket (com.actiontech.dble.net.mysql.EOFPacket)2 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)2 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)2