Search in sources :

Example 36 with SchemaConfig

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;
}
Also used : Pattern(java.util.regex.Pattern) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) Matcher(java.util.regex.Matcher) SchemaMeta(com.actiontech.dble.meta.SchemaMeta) TableConfig(com.actiontech.dble.config.model.TableConfig)

Example 37 with SchemaConfig

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

Example 38 with SchemaConfig

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;
}
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 39 with SchemaConfig

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;
}
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)

Example 40 with SchemaConfig

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);
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) ArrayList(java.util.ArrayList) TableConfig(com.actiontech.dble.config.model.TableConfig) CacheService(com.actiontech.dble.cache.CacheService) Test(org.junit.Test)

Aggregations

SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)70 TableConfig (com.actiontech.dble.config.model.TableConfig)16 SQLNonTransientException (java.sql.SQLNonTransientException)16 Test (org.junit.Test)15 RouteResultset (com.actiontech.dble.route.RouteResultset)6 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)5 ServerConfig (com.actiontech.dble.config.ServerConfig)5 UserConfig (com.actiontech.dble.config.model.UserConfig)5 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)5 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)5 CacheService (com.actiontech.dble.cache.CacheService)4 EOFPacket (com.actiontech.dble.net.mysql.EOFPacket)4 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)4 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)4 StringPtr (com.actiontech.dble.plan.common.ptr.StringPtr)4 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 PhysicalDBPool (com.actiontech.dble.backend.datasource.PhysicalDBPool)3 ERTable (com.actiontech.dble.config.model.ERTable)3 FirewallConfig (com.actiontech.dble.config.model.FirewallConfig)3