Search in sources :

Example 1 with StringPtr

use of com.actiontech.dble.plan.common.ptr.StringPtr in project dble by actiontech.

the class ReplaceableStringBuilder method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    for (Element ele : elements) {
        sb.append(ele.getSb());
        StringPtr rep = ele.getRepString();
        if (rep != null)
            sb.append(rep.get());
    }
    return sb.toString();
}
Also used : StringPtr(com.actiontech.dble.plan.common.ptr.StringPtr)

Example 2 with StringPtr

use of com.actiontech.dble.plan.common.ptr.StringPtr in project dble by actiontech.

the class DruidInsertParser method parserNoSharding.

private boolean parserNoSharding(ServerConnection sc, String contextSchema, SchemaInfo schemaInfo, RouteResultset rrs, MySqlInsertStatement insert) throws SQLException {
    if (RouterUtil.isNoSharding(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
        if (insert.getQuery() != null) {
            SQLSelectStatement selectStmt = new SQLSelectStatement(insert.getQuery());
            StringPtr sqlSchema = new StringPtr(schemaInfo.getSchema());
            if (!SchemaUtil.isNoSharding(sc, insert.getQuery().getQuery(), selectStmt, contextSchema, sqlSchema)) {
                return false;
            }
        }
        rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
        RouterUtil.routeToSingleNode(rrs, schemaInfo.getSchemaConfig().getDataNode());
        return true;
    }
    return false;
}
Also used : StringPtr(com.actiontech.dble.plan.common.ptr.StringPtr) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement)

Example 3 with StringPtr

use of com.actiontech.dble.plan.common.ptr.StringPtr in project dble by actiontech.

the class DruidReplaceParser method parserNoSharding.

/**
 * check if the nosharding tables are Involved
 *
 * @param sc
 * @param contextSchema
 * @param schemaInfo
 * @param rrs
 * @param replace
 * @return
 * @throws SQLException
 */
private boolean parserNoSharding(ServerConnection sc, String contextSchema, SchemaInfo schemaInfo, RouteResultset rrs, MySqlReplaceStatement replace) throws SQLException {
    if (RouterUtil.isNoSharding(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
        if (replace.getQuery() != null) {
            StringPtr sqlSchema = new StringPtr(schemaInfo.getSchema());
            // replace into ...select  if the both table is nosharding table
            if (!SchemaUtil.isNoSharding(sc, replace.getQuery(), contextSchema, sqlSchema)) {
                return false;
            }
        }
        rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
        RouterUtil.routeToSingleNode(rrs, schemaInfo.getSchemaConfig().getDataNode());
        return true;
    }
    return false;
}
Also used : StringPtr(com.actiontech.dble.plan.common.ptr.StringPtr)

Example 4 with StringPtr

use of com.actiontech.dble.plan.common.ptr.StringPtr in project dble by actiontech.

the class DruidSelectParser method executeComplexSQL.

private SchemaConfig executeComplexSQL(String schemaName, SchemaConfig schema, RouteResultset rrs, SQLSelectStatement selectStmt, ServerConnection sc) throws SQLException {
    StringPtr sqlSchema = new StringPtr(null);
    if (!SchemaUtil.isNoSharding(sc, selectStmt.getSelect().getQuery(), selectStmt, schemaName, sqlSchema)) {
        rrs.setSqlStatement(selectStmt);
        rrs.setNeedOptimizer(true);
        return schema;
    } else {
        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;
    }
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) StringPtr(com.actiontech.dble.plan.common.ptr.StringPtr)

Example 5 with StringPtr

use of com.actiontech.dble.plan.common.ptr.StringPtr 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)

Aggregations

StringPtr (com.actiontech.dble.plan.common.ptr.StringPtr)9 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)4 SchemaInfo (com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)3 SQLNonTransientException (java.sql.SQLNonTransientException)3 TableConfig (com.actiontech.dble.config.model.TableConfig)2 DateTimeFormat (com.actiontech.dble.plan.common.time.DateTimeFormat)1 MySQLTime (com.actiontech.dble.plan.common.time.MySQLTime)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 SQLJoinTableSource (com.alibaba.druid.sql.ast.statement.SQLJoinTableSource)1 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)1 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)1 MySqlDeleteStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement)1 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)1 MySqlUnionQuery (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUnionQuery)1 MySqlUpdateStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement)1