Search in sources :

Example 66 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class ShowVariables method response.

public static void response(ServerConnection c, String stmt) {
    String db = c.getSchema() != null ? c.getSchema() : SchemaUtil.getRandomDb();
    SchemaConfig schema = DbleServer.getInstance().getConfig().getSchemas().get(db);
    if (schema == null) {
        c.writeErrMessage("42000", "Unknown database '" + db + "'", ErrorCode.ER_BAD_DB_ERROR);
        return;
    }
    RouteResultset rrs = new RouteResultset(stmt, ServerParse.SHOW);
    try {
        RouterUtil.routeToSingleNode(rrs, schema.getRandomDataNode());
        ShowVariablesHandler handler = new ShowVariablesHandler(rrs, c.getSession2());
        handler.execute();
    } catch (Exception e) {
        // Could this only be ER_PARSE_ERROR?
        c.writeErrMessage(ErrorCode.ER_PARSE_ERROR, e.toString());
    }
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) RouteResultset(com.actiontech.dble.route.RouteResultset) ShowVariablesHandler(com.actiontech.dble.backend.mysql.nio.handler.ShowVariablesHandler)

Example 67 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class GlobalTableUtil method getGlobalTable.

private static void getGlobalTable() {
    ServerConfig config = DbleServer.getInstance().getConfig();
    for (Map.Entry<String, SchemaConfig> entry : config.getSchemas().entrySet()) {
        for (TableConfig table : entry.getValue().getTables().values()) {
            if (table.isGlobalTable()) {
                String tableName = table.getName();
                if (DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
                    tableName = tableName.toLowerCase();
                }
                globalTableMap.put(entry.getKey() + "." + tableName, table);
            }
        }
    }
}
Also used : ServerConfig(com.actiontech.dble.config.ServerConfig) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) TableConfig(com.actiontech.dble.config.model.TableConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 68 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class SchemaUtil method getSchemaInfo.

public static SchemaInfo getSchemaInfo(String user, String schema, SQLExpr expr, String tableAlias) throws SQLException {
    SchemaInfo schemaInfo = new SchemaInfo();
    if (expr instanceof SQLPropertyExpr) {
        SQLPropertyExpr propertyExpr = (SQLPropertyExpr) expr;
        schemaInfo.schema = StringUtil.removeBackQuote(propertyExpr.getOwner().toString());
        schemaInfo.table = StringUtil.removeBackQuote(propertyExpr.getName());
    } else if (expr instanceof SQLIdentifierExpr) {
        SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) expr;
        schemaInfo.schema = schema;
        schemaInfo.table = StringUtil.removeBackQuote(identifierExpr.getName());
        if (identifierExpr.getName().equalsIgnoreCase("dual") && tableAlias == null) {
            schemaInfo.dual = true;
            return schemaInfo;
        }
    }
    schemaInfo.tableAlias = tableAlias == null ? schemaInfo.table : tableAlias;
    if (schemaInfo.schema == null) {
        String msg = "No database selected";
        throw new SQLException(msg, "3D000", ErrorCode.ER_NO_DB_ERROR);
    }
    if (DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
        schemaInfo.table = schemaInfo.table.toLowerCase();
        schemaInfo.schema = schemaInfo.schema.toLowerCase();
    }
    if (MYSQL_SCHEMA.equalsIgnoreCase(schemaInfo.schema) || INFORMATION_SCHEMA.equalsIgnoreCase(schemaInfo.schema)) {
        return schemaInfo;
    } else {
        SchemaConfig schemaConfig = DbleServer.getInstance().getConfig().getSchemas().get(schemaInfo.schema);
        if (schemaConfig == null) {
            String msg = "Table " + StringUtil.getFullName(schemaInfo.schema, schemaInfo.table) + " doesn't exist";
            throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
        }
        if (user != null) {
            UserConfig userConfig = DbleServer.getInstance().getConfig().getUsers().get(user);
            if (!userConfig.getSchemas().contains(schemaInfo.schema)) {
                String msg = " Access denied for user '" + user + "' to database '" + schemaInfo.schema + "'";
                throw new SQLException(msg, "HY000", ErrorCode.ER_DBACCESS_DENIED_ERROR);
            }
        }
        schemaInfo.schemaConfig = schemaConfig;
        return schemaInfo;
    }
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) SQLException(java.sql.SQLException) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) UserConfig(com.actiontech.dble.config.model.UserConfig)

Example 69 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class ShowTableAlgorithm method execute.

public static void execute(ManagerConnection c, String tableInfo) {
    Matcher ma = PATTERN_FOR_TABLE_INFO.matcher(tableInfo);
    if (!ma.matches()) {
        c.writeErrMessage(ErrorCode.ER_UNKNOWN_ERROR, "The Correct Query Format Is:show @@algorithm where schema=? and table =?");
        return;
    }
    String schemaName = ma.group(2);
    String tableName = ma.group(4);
    if (DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
        schemaName = schemaName.toLowerCase();
        tableName = tableName.toLowerCase();
    }
    SchemaConfig schemaConfig = DbleServer.getInstance().getConfig().getSchemas().get(schemaName);
    TableType tableType;
    TableConfig tableConfig = null;
    if (schemaConfig == null) {
        c.writeErrMessage(ErrorCode.ER_UNKNOWN_ERROR, "the schema [" + schemaName + "] does not exists");
        return;
    } else if (schemaConfig.isNoSharding()) {
        tableType = TableType.BASE;
    } else {
        tableConfig = schemaConfig.getTables().get(tableName);
        if (tableConfig == null) {
            if (schemaConfig.getDataNode() == null) {
                c.writeErrMessage(ErrorCode.ER_UNKNOWN_ERROR, "the table [" + tableName + "] in schema [" + schemaName + "] does not exists");
                return;
            } else {
                tableType = TableType.BASE;
            }
        } else if (tableConfig.isGlobalTable()) {
            tableType = TableType.GLOBAL;
        } else if (tableConfig.getParentTC() != null) {
            tableType = TableType.CHILD;
        } else if (tableConfig.getRule() == null) {
            tableType = TableType.SHARDING_SINGLE;
        } else {
            tableType = TableType.SHARDING;
        }
    }
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = HEADER.write(buffer, c, true);
    // write fields
    for (FieldPacket field : FIELDS) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = EOF.write(buffer, c, true);
    // write rows
    byte packetId = EOF.getPacketId();
    for (RowDataPacket row : getRows(tableConfig, tableType, c.getCharset().getResults())) {
        row.setPacketId(++packetId);
        buffer = row.write(buffer, c, true);
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.setPacketId(++packetId);
    buffer = lastEof.write(buffer, c, true);
    // post write
    c.write(buffer);
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) Matcher(java.util.regex.Matcher) RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket) EOFPacket(com.actiontech.dble.net.mysql.EOFPacket) TableConfig(com.actiontech.dble.config.model.TableConfig) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.actiontech.dble.net.mysql.FieldPacket)

Example 70 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class MergeBuilder method constructByStatement.

public RouteResultset constructByStatement(String sql, SQLSelectStatement select) throws SQLException {
    ServerSchemaStatVisitor visitor = new ServerSchemaStatVisitor();
    DruidParser druidParser = new DruidSingleUnitSelectParser();
    RouteResultset rrs = new RouteResultset(sql, ServerParse.SELECT);
    LayerCachePool pool = DbleServer.getInstance().getRouterService().getTableId2DataNodeCache();
    SchemaConfig schemaConfig = schemaConfigMap.get(node.getReferedTableNodes().get(0).getSchema());
    return RouterUtil.routeFromParser(druidParser, schemaConfig, rrs, select, sql, pool, visitor, session.getSource());
}
Also used : DruidSingleUnitSelectParser(com.actiontech.dble.route.parser.druid.impl.DruidSingleUnitSelectParser) ServerSchemaStatVisitor(com.actiontech.dble.route.parser.druid.ServerSchemaStatVisitor) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) DruidParser(com.actiontech.dble.route.parser.druid.DruidParser) LayerCachePool(com.actiontech.dble.cache.LayerCachePool) RouteResultset(com.actiontech.dble.route.RouteResultset)

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