Skip to content

Commit

Permalink
Refactor SQLStatementBinderContext (#32049)
Browse files Browse the repository at this point in the history
* Refactor SelectStatementBinder

* Revise javadoc

* Refactor SQLStatementBinderContext

* Refactor SQLStatementBinderContext
  • Loading branch information
terrymanu authored Jul 9, 2024
1 parent 1afc200 commit bb6f5da
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ public final class CombineSegmentBinder {
*
* @param segment table segment
* @param statementBinderContext statement binder context
* @return bounded combine segment
* @return bound combine segment
*/
public static CombineSegment bind(final CombineSegment segment, final SQLStatementBinderContext statementBinderContext) {
ShardingSphereMetaData metaData = statementBinderContext.getMetaData();
String currentDatabaseName = statementBinderContext.getCurrentDatabaseName();
Map<String, TableSegmentBinderContext> externalTableBinderContexts = statementBinderContext.getExternalTableBinderContexts();
SelectStatement boundedLeftSelect = new SelectStatementBinder().bind(segment.getLeft().getSelect(), metaData, currentDatabaseName, externalTableBinderContexts);
SelectStatement boundedRightSelect = new SelectStatementBinder().bind(segment.getRight().getSelect(), metaData, currentDatabaseName, externalTableBinderContexts);
SubquerySegment boundedLeft = new SubquerySegment(segment.getLeft().getStartIndex(), segment.getLeft().getStopIndex(), segment.getLeft().getText());
boundedLeft.setSelect(boundedLeftSelect);
boundedLeft.setSubqueryType(segment.getLeft().getSubqueryType());
SelectStatement boundLeftSelect = new SelectStatementBinder().bind(segment.getLeft().getSelect(), metaData, currentDatabaseName, externalTableBinderContexts);
SelectStatement boundRightSelect = new SelectStatementBinder().bind(segment.getRight().getSelect(), metaData, currentDatabaseName, externalTableBinderContexts);
SubquerySegment boundLeft = new SubquerySegment(segment.getLeft().getStartIndex(), segment.getLeft().getStopIndex(), segment.getLeft().getText());
boundLeft.setSelect(boundLeftSelect);
boundLeft.setSubqueryType(segment.getLeft().getSubqueryType());
SubquerySegment boundedRight = new SubquerySegment(segment.getRight().getStartIndex(), segment.getRight().getStopIndex(), segment.getRight().getText());
boundedRight.setSelect(boundedRightSelect);
boundedRight.setSelect(boundRightSelect);
boundedRight.setSubqueryType(segment.getRight().getSubqueryType());
return new CombineSegment(segment.getStartIndex(), segment.getStopIndex(), boundedLeft, segment.getCombineType(), boundedRight);
return new CombineSegment(segment.getStartIndex(), segment.getStopIndex(), boundLeft, segment.getCombineType(), boundedRight);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final class WhereSegmentBinder {
* @param statementBinderContext statement binder context
* @param tableBinderContexts table binder contexts
* @param outerTableBinderContexts outer table binder contexts
* @return bounded where segment
* @return bound where segment
*/
public static WhereSegment bind(final WhereSegment segment, final SQLStatementBinderContext statementBinderContext,
final Map<String, TableSegmentBinderContext> tableBinderContexts, final Map<String, TableSegmentBinderContext> outerTableBinderContexts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;

import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -52,4 +53,11 @@ public final class SQLStatementBinderContext {
private final Map<String, TableSegmentBinderContext> externalTableBinderContexts = new CaseInsensitiveMap<>();

private final Collection<String> pivotColumnNames = new HashSet<>();

public SQLStatementBinderContext(final SQLStatement sqlStatement, final ShardingSphereMetaData metaData, final String currentDatabaseName) {
this.metaData = metaData;
this.currentDatabaseName = currentDatabaseName;
databaseType = sqlStatement.getDatabaseType();
variableNames = sqlStatement.getVariableNames();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private DeleteStatement bind(final DeleteStatement sqlStatement, final ShardingS
final Map<String, TableSegmentBinderContext> externalTableBinderContexts) {
DeleteStatement result = copy(sqlStatement);
Map<String, TableSegmentBinderContext> tableBinderContexts = new LinkedHashMap<>();
SQLStatementBinderContext statementBinderContext = new SQLStatementBinderContext(metaData, currentDatabaseName, sqlStatement.getDatabaseType(), sqlStatement.getVariableNames());
SQLStatementBinderContext statementBinderContext = new SQLStatementBinderContext(sqlStatement, metaData, currentDatabaseName);
statementBinderContext.getExternalTableBinderContexts().putAll(externalTableBinderContexts);
result.setTable(TableSegmentBinder.bind(sqlStatement.getTable(), statementBinderContext, tableBinderContexts, Collections.emptyMap()));
sqlStatement.getWhere().ifPresent(optional -> result.setWhere(WhereSegmentBinder.bind(optional, statementBinderContext, tableBinderContexts, Collections.emptyMap())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public InsertStatement bind(final InsertStatement sqlStatement, final ShardingSp
private InsertStatement bind(final InsertStatement sqlStatement, final ShardingSphereMetaData metaData, final String currentDatabaseName,
final Map<String, TableSegmentBinderContext> externalTableBinderContexts) {
InsertStatement result = copy(sqlStatement);
SQLStatementBinderContext statementBinderContext = new SQLStatementBinderContext(metaData, currentDatabaseName, sqlStatement.getDatabaseType(), sqlStatement.getVariableNames());
SQLStatementBinderContext statementBinderContext = new SQLStatementBinderContext(sqlStatement, metaData, currentDatabaseName);
statementBinderContext.getExternalTableBinderContexts().putAll(externalTableBinderContexts);
Map<String, TableSegmentBinderContext> tableBinderContexts = new LinkedHashMap<>();
Optional.ofNullable(sqlStatement.getTable()).ifPresent(optional -> result.setTable(SimpleTableSegmentBinder.bind(optional, statementBinderContext, tableBinderContexts)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public MergeStatement bind(final MergeStatement sqlStatement, final ShardingSphe
private MergeStatement bind(final MergeStatement sqlStatement, final ShardingSphereMetaData metaData, final String currentDatabaseName,
final Map<String, TableSegmentBinderContext> externalTableBinderContexts) {
MergeStatement result = copy(sqlStatement);
SQLStatementBinderContext statementBinderContext = new SQLStatementBinderContext(metaData, currentDatabaseName, sqlStatement.getDatabaseType(), sqlStatement.getVariableNames());
SQLStatementBinderContext statementBinderContext = new SQLStatementBinderContext(sqlStatement, metaData, currentDatabaseName);
statementBinderContext.getExternalTableBinderContexts().putAll(externalTableBinderContexts);
Map<String, TableSegmentBinderContext> targetTableBinderContexts = new CaseInsensitiveMap<>();
TableSegment boundedTargetTableSegment = TableSegmentBinder.bind(sqlStatement.getTarget(), statementBinderContext, targetTableBinderContexts, Collections.emptyMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private SelectStatement bind(final SelectStatement sqlStatement, final ShardingS
final Map<String, TableSegmentBinderContext> outerTableBinderContexts, final Map<String, TableSegmentBinderContext> externalTableBinderContexts) {
SelectStatement result = copy(sqlStatement);
Map<String, TableSegmentBinderContext> tableBinderContexts = new LinkedHashMap<>();
SQLStatementBinderContext statementBinderContext = new SQLStatementBinderContext(metaData, currentDatabaseName, sqlStatement.getDatabaseType(), sqlStatement.getVariableNames());
SQLStatementBinderContext statementBinderContext = new SQLStatementBinderContext(sqlStatement, metaData, currentDatabaseName);
statementBinderContext.getExternalTableBinderContexts().putAll(externalTableBinderContexts);
sqlStatement.getWithSegment()
.ifPresent(optional -> result.setWithSegment(WithSegmentBinder.bind(optional, statementBinderContext, tableBinderContexts, statementBinderContext.getExternalTableBinderContexts())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private UpdateStatement bind(final UpdateStatement sqlStatement, final ShardingS
final Map<String, TableSegmentBinderContext> externalTableBinderContexts) {
UpdateStatement result = copy(sqlStatement);
Map<String, TableSegmentBinderContext> tableBinderContexts = new LinkedHashMap<>();
SQLStatementBinderContext statementBinderContext = new SQLStatementBinderContext(metaData, currentDatabaseName, sqlStatement.getDatabaseType(), sqlStatement.getVariableNames());
SQLStatementBinderContext statementBinderContext = new SQLStatementBinderContext(sqlStatement, metaData, currentDatabaseName);
statementBinderContext.getExternalTableBinderContexts().putAll(externalTableBinderContexts);
TableSegment boundedTableSegment = TableSegmentBinder.bind(sqlStatement.getTable(), statementBinderContext, tableBinderContexts, Collections.emptyMap());
result.setTable(boundedTableSegment);
Expand Down

0 comments on commit bb6f5da

Please sign in to comment.