Search in sources :

Example 6 with SchemaInfo

use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.

the class DruidInsertParser method visitorParse.

@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
    MySqlInsertStatement insert = (MySqlInsertStatement) stmt;
    String schemaName = schema == null ? null : schema.getName();
    SQLExprTableSource tableSource = insert.getTableSource();
    SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, tableSource);
    if (!ServerPrivileges.checkPrivilege(sc, schemaInfo.getSchema(), schemaInfo.getTable(), CheckType.INSERT)) {
        String msg = "The statement DML privilege check is not passed, sql:" + stmt;
        throw new SQLNonTransientException(msg);
    }
    schema = schemaInfo.getSchemaConfig();
    String tableName = schemaInfo.getTable();
    if (parserNoSharding(sc, schemaName, schemaInfo, rrs, insert)) {
        return schema;
    }
    if (insert.getQuery() != null) {
        // insert into .... select ....
        String msg = "`INSERT ... SELECT Syntax` is not supported!";
        LOGGER.info(msg);
        throw new SQLNonTransientException(msg);
    }
    TableConfig tc = schema.getTables().get(tableName);
    if (tc == null) {
        String msg = "Table '" + schema.getName() + "." + tableName + "' doesn't exist";
        throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
    }
    if (tc.isGlobalTable()) {
        String sql = rrs.getStatement();
        if (tc.isAutoIncrement() || GlobalTableUtil.useGlobalTableCheck()) {
            sql = convertInsertSQL(schemaInfo, insert, sql, tc, GlobalTableUtil.useGlobalTableCheck());
        } else {
            sql = RouterUtil.removeSchema(sql, schemaInfo.getSchema());
        }
        rrs.setStatement(sql);
        RouterUtil.routeToMultiNode(false, rrs, tc.getDataNodes(), tc.isGlobalTable());
        rrs.setFinishedRoute(true);
        return schema;
    }
    if (tc.isAutoIncrement()) {
        String sql = convertInsertSQL(schemaInfo, insert, rrs.getStatement(), tc, false);
        rrs.setStatement(sql);
        SQLStatementParser parser = new MySqlStatementParser(sql);
        stmt = parser.parseStatement();
        insert = (MySqlInsertStatement) stmt;
    }
    // insert childTable will finished router while parser
    if (tc.getParentTC() != null) {
        parserChildTable(schemaInfo, rrs, insert, sc);
        return schema;
    }
    String partitionColumn = tc.getPartitionColumn();
    if (partitionColumn != null) {
        if (isMultiInsert(insert)) {
            parserBatchInsert(schemaInfo, rrs, partitionColumn, insert);
        } else {
            parserSingleInsert(schemaInfo, rrs, partitionColumn, insert);
        }
    } else {
        rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
        ctx.addTable(tableName);
    }
    return schema;
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLException(java.sql.SQLException) TableConfig(com.actiontech.dble.config.model.TableConfig) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 7 with SchemaInfo

use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.

the class DruidReplaceParser method visitorParse.

@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
    // data & object prepare
    MySqlReplaceStatement replace = (MySqlReplaceStatement) stmt;
    String schemaName = schema == null ? null : schema.getName();
    SQLExprTableSource tableSource = replace.getTableSource();
    SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, tableSource);
    // privilege check
    if (!ServerPrivileges.checkPrivilege(sc, schemaInfo.getSchema(), schemaInfo.getTable(), ServerPrivileges.CheckType.INSERT)) {
        String msg = "The statement DML privilege check is not passed, sql:" + stmt;
        throw new SQLNonTransientException(msg);
    }
    // No sharding table check
    schema = schemaInfo.getSchemaConfig();
    String tableName = schemaInfo.getTable();
    if (parserNoSharding(sc, schemaName, schemaInfo, rrs, replace)) {
        return schema;
    }
    if (replace.getQuery() != null) {
        // replace into ...select with sharding not supported
        String msg = "`INSERT ... SELECT Syntax` is not supported!";
        LOGGER.info(msg);
        throw new SQLNonTransientException(msg);
    }
    // check the config of target table
    TableConfig tc = schema.getTables().get(tableName);
    if (tc == null) {
        String msg = "Table '" + schema.getName() + "." + tableName + "' doesn't exist";
        throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
    }
    // if the target table is global table than
    if (tc.isGlobalTable()) {
        String sql = rrs.getStatement();
        if (tc.isAutoIncrement() || GlobalTableUtil.useGlobalTableCheck()) {
            sql = convertReplaceSQL(schemaInfo, replace, sql, tc, GlobalTableUtil.useGlobalTableCheck(), sc);
        } else {
            sql = RouterUtil.removeSchema(sql, schemaInfo.getSchema());
        }
        rrs.setStatement(sql);
        RouterUtil.routeToMultiNode(false, rrs, tc.getDataNodes(), tc.isGlobalTable());
        rrs.setFinishedRoute(true);
        return schema;
    }
    if (tc.isAutoIncrement()) {
        String sql = convertReplaceSQL(schemaInfo, replace, rrs.getStatement(), tc, false, sc);
        rrs.setStatement(sql);
        SQLStatementParser parser = new MySqlStatementParser(sql);
        stmt = parser.parseStatement();
        replace = (MySqlReplaceStatement) stmt;
    }
    // childTable can be route in this part
    if (tc.getParentTC() != null) {
        parserChildTable(schemaInfo, rrs, replace, sc);
        return schema;
    }
    String partitionColumn = tc.getPartitionColumn();
    if (partitionColumn != null) {
        if (isMultiReplace(replace)) {
            parserBatchInsert(schemaInfo, rrs, partitionColumn, replace);
        } else {
            parserSingleInsert(schemaInfo, rrs, partitionColumn, replace);
        }
    } else {
        rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
        ctx.addTable(tableName);
    }
    return schema;
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MySqlReplaceStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement) SQLException(java.sql.SQLException) TableConfig(com.actiontech.dble.config.model.TableConfig) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 8 with SchemaInfo

use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.

the class DruidSelectParser method visitorParse.

@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
    SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
    String schemaName = schema == null ? null : schema.getName();
    if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
        MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) sqlSelectQuery;
        if (mysqlSelectQuery.getInto() != null) {
            throw new SQLNonTransientException("select ... into is not supported!");
        }
        SQLTableSource mysqlFrom = mysqlSelectQuery.getFrom();
        if (mysqlFrom == null) {
            List<SQLSelectItem> selectList = mysqlSelectQuery.getSelectList();
            for (SQLSelectItem item : selectList) {
                SQLExpr itemExpr = item.getExpr();
                if (itemExpr instanceof SQLQueryExpr) {
                    rrs.setSqlStatement(selectStmt);
                    rrs.setNeedOptimizer(true);
                    return schema;
                }
            }
            RouterUtil.routeNoNameTableToSingleNode(rrs, schema);
            return schema;
        }
        SchemaInfo schemaInfo;
        if (mysqlFrom instanceof SQLExprTableSource) {
            SQLExprTableSource fromSource = (SQLExprTableSource) mysqlFrom;
            schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, fromSource);
            if (schemaInfo.isDual()) {
                RouterUtil.routeNoNameTableToSingleNode(rrs, schema);
                return schema;
            }
            if (matchSysTable(rrs, sc, schemaInfo)) {
                return schema;
            }
            if (schemaInfo.getSchemaConfig() == null) {
                String msg = "No Supported, sql:" + stmt;
                throw new SQLNonTransientException(msg);
            }
            if (!ServerPrivileges.checkPrivilege(sc, schemaInfo.getSchema(), schemaInfo.getTable(), CheckType.SELECT)) {
                String msg = "The statement DML privilege check is not passed, sql:" + stmt;
                throw new SQLNonTransientException(msg);
            }
            rrs.setSchema(schemaInfo.getSchema());
            rrs.setTable(schemaInfo.getTable());
            rrs.setTableAlias(schemaInfo.getTableAlias());
            rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
            schema = schemaInfo.getSchemaConfig();
            if (DbleServer.getInstance().getTmManager().getSyncView(schema.getName(), schemaInfo.getTable()) != null) {
                rrs.setNeedOptimizer(true);
                rrs.setSqlStatement(selectStmt);
                return schema;
            }
            if (RouterUtil.isNoSharding(schema, schemaInfo.getTable())) {
                RouterUtil.routeToSingleNode(rrs, schema.getDataNode());
                return schema;
            }
            TableConfig tc = schema.getTables().get(schemaInfo.getTable());
            if (tc == null) {
                String msg = "Table '" + schema.getName() + "." + schemaInfo.getTable() + "' doesn't exist";
                throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
            }
            super.visitorParse(schema, rrs, stmt, visitor, sc);
            if (visitor.isHasSubQuery()) {
                rrs.setSqlStatement(selectStmt);
                rrs.setNeedOptimizer(true);
                return schema;
            }
            parseOrderAggGroupMysql(schema, stmt, rrs, mysqlSelectQuery, tc);
            // select ...for update /in shard mode /in transaction
            if ((mysqlSelectQuery.isForUpdate() || mysqlSelectQuery.isLockInShareMode()) && !sc.isAutocommit()) {
                rrs.setCanRunInReadDB(false);
            }
        } else if (mysqlFrom instanceof SQLSubqueryTableSource || mysqlFrom instanceof SQLJoinTableSource || mysqlFrom instanceof SQLUnionQueryTableSource) {
            return executeComplexSQL(schemaName, schema, rrs, selectStmt, sc);
        }
    } else if (sqlSelectQuery instanceof MySqlUnionQuery) {
        return executeComplexSQL(schemaName, schema, rrs, selectStmt, sc);
    }
    return schema;
}
Also used : SQLException(java.sql.SQLException) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLNonTransientException(java.sql.SQLNonTransientException) MySqlUnionQuery(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUnionQuery) TableConfig(com.actiontech.dble.config.model.TableConfig) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 9 with SchemaInfo

use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.

the class DruidSingleUnitSelectParser method visitorParse.

@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
    SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
    String schemaName = schema == null ? null : schema.getName();
    if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
        MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) selectStmt.getSelect().getQuery();
        SQLTableSource mysqlFrom = mysqlSelectQuery.getFrom();
        if (mysqlFrom == null) {
            RouterUtil.routeNoNameTableToSingleNode(rrs, schema);
            return schema;
        }
        if (mysqlFrom instanceof SQLSubqueryTableSource || mysqlFrom instanceof SQLJoinTableSource || mysqlFrom instanceof SQLUnionQueryTableSource) {
            StringPtr sqlSchema = new StringPtr(null);
            if (SchemaUtil.isNoSharding(sc, selectStmt.getSelect().getQuery(), selectStmt, schemaName, sqlSchema)) {
                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());
                return schemaConfig;
            } else {
                super.visitorParse(schema, rrs, stmt, visitor, sc);
                return schema;
            }
        }
        SQLExprTableSource fromSource = (SQLExprTableSource) mysqlFrom;
        SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, fromSource);
        if (schemaInfo.getSchemaConfig() == null) {
            String msg = "No Supported, sql:" + stmt;
            throw new SQLNonTransientException(msg);
        }
        if (!ServerPrivileges.checkPrivilege(sc, schemaInfo.getSchema(), schemaInfo.getTable(), CheckType.SELECT)) {
            String msg = "The statement DML privilege check is not passed, sql:" + stmt;
            throw new SQLNonTransientException(msg);
        }
        rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
        schema = schemaInfo.getSchemaConfig();
        super.visitorParse(schema, rrs, stmt, visitor, sc);
        if (visitor.isHasSubQuery()) {
            this.getCtx().getRouteCalculateUnits().clear();
        }
        // change canRunInReadDB
        if ((mysqlSelectQuery.isForUpdate() || mysqlSelectQuery.isLockInShareMode()) && !sc.isAutocommit()) {
            rrs.setCanRunInReadDB(false);
        }
    } else if (sqlSelectQuery instanceof MySqlUnionQuery) {
        StringPtr sqlSchema = new StringPtr(null);
        if (SchemaUtil.isNoSharding(sc, selectStmt.getSelect().getQuery(), selectStmt, schemaName, sqlSchema)) {
            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());
            return schemaConfig;
        } else {
            super.visitorParse(schema, rrs, stmt, visitor, sc);
        }
    }
    return schema;
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLNonTransientException(java.sql.SQLNonTransientException) StringPtr(com.actiontech.dble.plan.common.ptr.StringPtr) MySqlUnionQuery(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUnionQuery) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 10 with SchemaInfo

use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.

the class ShowColumns method response.

public static void response(ServerConnection c, String stmt) {
    try {
        SQLStatement statement = RouteStrategyFactory.getRouteStrategy().parserSQL(stmt);
        MySqlShowColumnsStatement showColumnsStatement = (MySqlShowColumnsStatement) statement;
        String table = StringUtil.removeBackQuote(showColumnsStatement.getTable().getSimpleName());
        String schema = showColumnsStatement.getDatabase() == null ? c.getSchema() : showColumnsStatement.getDatabase().getSimpleName();
        if (schema == null) {
            c.writeErrMessage("3D000", "No database selected", ErrorCode.ER_NO_DB_ERROR);
            return;
        }
        String sql = stmt;
        if (showColumnsStatement.getDatabase() != null) {
            showColumnsStatement.setDatabase(null);
            sql = showColumnsStatement.toString();
        }
        if (DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
            schema = StringUtil.removeBackQuote(schema).toLowerCase();
            table = table.toLowerCase();
        }
        SchemaInfo schemaInfo = new SchemaInfo(schema, table);
        c.routeSystemInfoAndExecuteSQL(sql, schemaInfo, ServerParse.SHOW);
    } catch (Exception e) {
        c.writeErrMessage(ErrorCode.ER_PARSE_ERROR, e.toString());
    }
}
Also used : MySqlShowColumnsStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) 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