Search in sources :

Example 11 with SchemaInfo

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());
    }
}
Also used : MySqlShowIndexesStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStatement) MySqlShowKeysStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 12 with SchemaInfo

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);
    }
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SQLCreateIndexStatement(com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 13 with SchemaInfo

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();
}
Also used : SQLDropTableStatement(com.alibaba.druid.sql.ast.statement.SQLDropTableStatement) SQLNonTransientException(java.sql.SQLNonTransientException) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 14 with SchemaInfo

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();
}
Also used : SQLTruncateStatement(com.alibaba.druid.sql.ast.statement.SQLTruncateStatement) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 15 with SchemaInfo

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

Aggregations

SchemaInfo (com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)20 SQLNonTransientException (java.sql.SQLNonTransientException)10 SQLException (java.sql.SQLException)9 TableConfig (com.actiontech.dble.config.model.TableConfig)5 StructureMeta (com.actiontech.dble.meta.protocol.StructureMeta)4 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)4 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)3 StringPtr (com.actiontech.dble.plan.common.ptr.StringPtr)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)2 MySqlPrimaryKey (com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey)2 MySqlAlterTableChangeColumn (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn)2 MySqlAlterTableModifyColumn (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn)2 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)2 MySqlUnionQuery (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUnionQuery)2 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)2 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)2 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLCreateIndexStatement (com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement)1 SQLDropIndexStatement (com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement)1