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