Skip to content

Commit 068f921

Browse files
authored
Refactor SQLStatementBinderContext (#35499)
* Refactor CommonSQLStatementContext * Refactor SQLStatementBinderContext
1 parent 5a50d6a commit 068f921

File tree

11 files changed

+42
-21
lines changed

11 files changed

+42
-21
lines changed

infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/CommonSQLStatementContext.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,4 @@ public abstract class CommonSQLStatementContext implements SQLStatementContext {
3232
private final DatabaseType databaseType;
3333

3434
private final SQLStatement sqlStatement;
35-
36-
protected CommonSQLStatementContext(final SQLStatement sqlStatement) {
37-
databaseType = sqlStatement.getDatabaseType();
38-
this.sqlStatement = sqlStatement;
39-
}
4035
}

infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/SQLBindEngine.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ private boolean isNeedBind() {
7878

7979
private SQLStatement bindSQLStatement(final DatabaseType databaseType, final SQLStatement statement) {
8080
if (statement instanceof DMLStatement) {
81-
return new DMLStatementBindEngine(metaData, currentDatabaseName, hintValueContext).bind(databaseType, (DMLStatement) statement);
81+
return new DMLStatementBindEngine(metaData, currentDatabaseName, hintValueContext, databaseType).bind((DMLStatement) statement);
8282
}
8383
if (statement instanceof DDLStatement) {
84-
return new DDLStatementBindEngine(metaData, currentDatabaseName, hintValueContext).bind((DDLStatement) statement);
84+
return new DDLStatementBindEngine(metaData, currentDatabaseName, hintValueContext, databaseType).bind((DDLStatement) statement);
8585
}
8686
if (statement instanceof DALStatement) {
87-
return new DALStatementBindEngine(metaData, currentDatabaseName, hintValueContext).bind((DALStatement) statement);
87+
return new DALStatementBindEngine(metaData, currentDatabaseName, hintValueContext, databaseType).bind((DALStatement) statement);
8888
}
8989
if (statement instanceof DCLStatement) {
90-
return new DCLStatementBindEngine(metaData, currentDatabaseName, hintValueContext).bind((DCLStatement) statement);
90+
return new DCLStatementBindEngine(metaData, currentDatabaseName, hintValueContext, databaseType).bind((DCLStatement) statement);
9191
}
9292
return statement;
9393
}

infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/ExplainStatementBinder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public ExplainStatement bind(final ExplainStatement sqlStatement, final SQLState
4545
ExplainStatement result = copy(sqlStatement);
4646
SQLStatement explainSQLStatement = sqlStatement.getSqlStatement();
4747
SQLStatement boundSQLStatement = explainSQLStatement instanceof DMLStatement
48-
? new DMLStatementBindEngine(metaData, currentDatabaseName, hintValueContext).bind(binderContext.getDatabaseType(), (DMLStatement) explainSQLStatement)
48+
? new DMLStatementBindEngine(metaData, currentDatabaseName, hintValueContext, binderContext.getDatabaseType()).bind((DMLStatement) explainSQLStatement)
4949
: explainSQLStatement;
5050
result.setSqlStatement(boundSQLStatement);
5151
return result;

infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DALStatementBindEngine.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.shardingsphere.infra.binder.engine.statement.dal.ShowColumnsStatementBinder;
2525
import org.apache.shardingsphere.infra.binder.engine.statement.dal.ShowCreateTableStatementBinder;
2626
import org.apache.shardingsphere.infra.binder.engine.statement.dal.ShowIndexStatementBinder;
27+
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
2728
import org.apache.shardingsphere.infra.hint.HintValueContext;
2829
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
2930
import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.DALStatement;
@@ -45,14 +46,16 @@ public final class DALStatementBindEngine {
4546

4647
private final HintValueContext hintValueContext;
4748

49+
private final DatabaseType databaseType;
50+
4851
/**
4952
* Bind DAL statement.
5053
*
5154
* @param statement to be bound DAL statement
5255
* @return bound DAL statement
5356
*/
5457
public DALStatement bind(final DALStatement statement) {
55-
SQLStatementBinderContext binderContext = new SQLStatementBinderContext(metaData, currentDatabaseName, hintValueContext, statement);
58+
SQLStatementBinderContext binderContext = new SQLStatementBinderContext(metaData, currentDatabaseName, hintValueContext, databaseType, statement);
5659
if (statement instanceof OptimizeTableStatement) {
5760
return new OptimizeTableStatementBinder().bind((OptimizeTableStatement) statement, binderContext);
5861
}

infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DCLStatementBindEngine.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import lombok.RequiredArgsConstructor;
2121
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
2222
import org.apache.shardingsphere.infra.binder.engine.statement.dcl.DenyUserStatementBinder;
23+
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
2324
import org.apache.shardingsphere.infra.hint.HintValueContext;
2425
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
2526
import org.apache.shardingsphere.sql.parser.statement.core.statement.dcl.DCLStatement;
@@ -37,14 +38,16 @@ public final class DCLStatementBindEngine {
3738

3839
private final HintValueContext hintValueContext;
3940

41+
private final DatabaseType databaseType;
42+
4043
/**
4144
* Bind DCL statement.
4245
*
4346
* @param statement to be bound DCL statement
4447
* @return bound DCL statement
4548
*/
4649
public DCLStatement bind(final DCLStatement statement) {
47-
SQLStatementBinderContext binderContext = new SQLStatementBinderContext(metaData, currentDatabaseName, hintValueContext, statement);
50+
SQLStatementBinderContext binderContext = new SQLStatementBinderContext(metaData, currentDatabaseName, hintValueContext, databaseType, statement);
4851
if (statement instanceof DenyUserStatement) {
4952
return new DenyUserStatementBinder().bind((DenyUserStatement) statement, binderContext);
5053
}

infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DDLStatementBindEngine.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.shardingsphere.infra.binder.engine.statement.ddl.DropViewStatementBinder;
3232
import org.apache.shardingsphere.infra.binder.engine.statement.ddl.RenameTableStatementBinder;
3333
import org.apache.shardingsphere.infra.binder.engine.statement.ddl.TruncateStatementBinder;
34+
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
3435
import org.apache.shardingsphere.infra.hint.HintValueContext;
3536
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
3637
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterIndexStatement;
@@ -59,14 +60,16 @@ public final class DDLStatementBindEngine {
5960

6061
private final HintValueContext hintValueContext;
6162

63+
private final DatabaseType databaseType;
64+
6265
/**
6366
* Bind DDL statement.
6467
*
6568
* @param statement to be bound DDL statement
6669
* @return bound DDL statement
6770
*/
6871
public DDLStatement bind(final DDLStatement statement) {
69-
SQLStatementBinderContext binderContext = new SQLStatementBinderContext(metaData, currentDatabaseName, hintValueContext, statement);
72+
SQLStatementBinderContext binderContext = new SQLStatementBinderContext(metaData, currentDatabaseName, hintValueContext, databaseType, statement);
7073
if (statement instanceof CursorStatement) {
7174
return new CursorStatementBinder().bind((CursorStatement) statement, binderContext);
7275
}

infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DMLStatementBindEngine.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@ public final class DMLStatementBindEngine {
5050

5151
private final HintValueContext hintValueContext;
5252

53+
private final DatabaseType databaseType;
54+
5355
/**
5456
* Bind DML statement.
5557
*
56-
* @param databaseType database type
5758
* @param statement to be bound DML statement
5859
* @return bound DML statement
5960
*/
60-
public DMLStatement bind(final DatabaseType databaseType, final DMLStatement statement) {
61+
public DMLStatement bind(final DMLStatement statement) {
6162
SQLStatementBinderContext binderContext = new SQLStatementBinderContext(metaData, currentDatabaseName, hintValueContext, databaseType, statement);
6263
if (statement instanceof SelectStatement) {
6364
return new SelectStatementBinder().bind((SelectStatement) statement, binderContext);

infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/engine/statement/dml/DeleteStatementBinderTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
package org.apache.shardingsphere.infra.binder.engine.statement.dml;
1919

2020
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
21+
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
2122
import org.apache.shardingsphere.infra.hint.HintValueContext;
2223
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
2324
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
2425
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
26+
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
2527
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
2628
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.BinaryOperationExpression;
2729
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.LiteralExpressionSegment;
@@ -47,14 +49,16 @@
4749

4850
class DeleteStatementBinderTest {
4951

52+
private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
53+
5054
@Test
5155
void assertBind() {
5256
DeleteStatement deleteStatement = new SQL92DeleteStatement();
5357
SimpleTableSegment simpleTableSegment = new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order")));
5458
deleteStatement.setTable(simpleTableSegment);
5559
deleteStatement.setWhere(new WhereSegment(0, 0, new BinaryOperationExpression(0, 0, new ColumnSegment(0, 0, new IdentifierValue("status")),
5660
new LiteralExpressionSegment(0, 0, 0), "=", "status = 1")));
57-
DeleteStatement actual = new DeleteStatementBinder().bind(deleteStatement, new SQLStatementBinderContext(createMetaData(), "foo_db", new HintValueContext(), deleteStatement));
61+
DeleteStatement actual = new DeleteStatementBinder().bind(deleteStatement, new SQLStatementBinderContext(createMetaData(), "foo_db", new HintValueContext(), databaseType, deleteStatement));
5862
assertThat(actual, not(deleteStatement));
5963
assertThat(actual.getTable(), not(deleteStatement.getTable()));
6064
assertThat(actual.getTable(), instanceOf(SimpleTableSegment.class));

infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/engine/statement/dml/InsertStatementBinderTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
package org.apache.shardingsphere.infra.binder.engine.statement.dml;
1919

2020
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
21+
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
2122
import org.apache.shardingsphere.infra.hint.HintValueContext;
2223
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
2324
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
2425
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
26+
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
2527
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.InsertValuesSegment;
2628
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
2729
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.InsertColumnsSegment;
@@ -55,6 +57,8 @@
5557

5658
class InsertStatementBinderTest {
5759

60+
private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
61+
5862
@Test
5963
void assertBindInsertValues() {
6064
InsertStatement insertStatement = new SQL92InsertStatement();
@@ -63,7 +67,7 @@ void assertBindInsertValues() {
6367
new ColumnSegment(0, 0, new IdentifierValue("user_id")), new ColumnSegment(0, 0, new IdentifierValue("status")))));
6468
insertStatement.getValues().add(new InsertValuesSegment(0, 0, Arrays.asList(new LiteralExpressionSegment(0, 0, 1),
6569
new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, "OK"))));
66-
InsertStatement actual = new InsertStatementBinder().bind(insertStatement, new SQLStatementBinderContext(createMetaData(), "foo_db", new HintValueContext(), insertStatement));
70+
InsertStatement actual = new InsertStatementBinder().bind(insertStatement, new SQLStatementBinderContext(createMetaData(), "foo_db", new HintValueContext(), databaseType, insertStatement));
6771
assertThat(actual, not(insertStatement));
6872
assertTrue(actual.getTable().isPresent());
6973
assertTrue(insertStatement.getTable().isPresent());
@@ -108,7 +112,7 @@ void assertBindInsertSelectWithColumns() {
108112
insertStatement.setInsertSelect(new SubquerySegment(0, 0, subSelectStatement, ""));
109113
insertStatement.getValues().add(new InsertValuesSegment(0, 0, Arrays.asList(new LiteralExpressionSegment(0, 0, 1),
110114
new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, "OK"))));
111-
InsertStatement actual = new InsertStatementBinder().bind(insertStatement, new SQLStatementBinderContext(createMetaData(), "foo_db", new HintValueContext(), insertStatement));
115+
InsertStatement actual = new InsertStatementBinder().bind(insertStatement, new SQLStatementBinderContext(createMetaData(), "foo_db", new HintValueContext(), databaseType, insertStatement));
112116
assertThat(actual, not(insertStatement));
113117
assertTrue(actual.getTable().isPresent());
114118
assertTrue(insertStatement.getTable().isPresent());
@@ -132,7 +136,7 @@ void assertBindInsertSelectWithoutColumns() {
132136
insertStatement.setInsertSelect(new SubquerySegment(0, 0, subSelectStatement, ""));
133137
insertStatement.getValues().add(new InsertValuesSegment(0, 0, Arrays.asList(new LiteralExpressionSegment(0, 0, 1),
134138
new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, "OK"))));
135-
InsertStatement actual = new InsertStatementBinder().bind(insertStatement, new SQLStatementBinderContext(createMetaData(), "foo_db", new HintValueContext(), insertStatement));
139+
InsertStatement actual = new InsertStatementBinder().bind(insertStatement, new SQLStatementBinderContext(createMetaData(), "foo_db", new HintValueContext(), databaseType, insertStatement));
136140
assertThat(actual, not(insertStatement));
137141
assertTrue(actual.getTable().isPresent());
138142
assertTrue(insertStatement.getTable().isPresent());

infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/engine/statement/dml/SelectStatementBinderTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
package org.apache.shardingsphere.infra.binder.engine.statement.dml;
1919

2020
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
21+
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
2122
import org.apache.shardingsphere.infra.hint.HintValueContext;
2223
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
2324
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
2425
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
26+
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
2527
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
2628
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.BinaryOperationExpression;
2729
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.FunctionSegment;
@@ -53,6 +55,8 @@
5355

5456
class SelectStatementBinderTest {
5557

58+
private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
59+
5660
@Test
5761
void assertBind() {
5862
SelectStatement selectStatement = new SQL92SelectStatement();
@@ -67,7 +71,7 @@ void assertBind() {
6771
SimpleTableSegment simpleTableSegment = new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order")));
6872
selectStatement.setFrom(simpleTableSegment);
6973
selectStatement.setWhere(mockWhereSegment());
70-
SelectStatement actual = new SelectStatementBinder().bind(selectStatement, new SQLStatementBinderContext(createMetaData(), "foo_db", new HintValueContext(), selectStatement));
74+
SelectStatement actual = new SelectStatementBinder().bind(selectStatement, new SQLStatementBinderContext(createMetaData(), "foo_db", new HintValueContext(), databaseType, selectStatement));
7175
assertThat(actual, not(selectStatement));
7276
assertTrue(actual.getFrom().isPresent());
7377
assertThat(actual.getFrom().get(), not(simpleTableSegment));

infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/engine/statement/dml/UpdateStatementBinderTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
package org.apache.shardingsphere.infra.binder.engine.statement.dml;
1919

2020
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
21+
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
2122
import org.apache.shardingsphere.infra.hint.HintValueContext;
2223
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
2324
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
2425
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
26+
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
2527
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
2628
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.BinaryOperationExpression;
2729
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.LiteralExpressionSegment;
@@ -47,14 +49,16 @@
4749

4850
class UpdateStatementBinderTest {
4951

52+
private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
53+
5054
@Test
5155
void assertBind() {
5256
UpdateStatement updateStatement = new SQL92UpdateStatement();
5357
SimpleTableSegment simpleTableSegment = new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order")));
5458
updateStatement.setTable(simpleTableSegment);
5559
updateStatement.setWhere(new WhereSegment(0, 0, new BinaryOperationExpression(0, 0, new ColumnSegment(0, 0, new IdentifierValue("status")),
5660
new LiteralExpressionSegment(0, 0, 0), "=", "status = 1")));
57-
UpdateStatement actual = new UpdateStatementBinder().bind(updateStatement, new SQLStatementBinderContext(createMetaData(), "foo_db", new HintValueContext(), updateStatement));
61+
UpdateStatement actual = new UpdateStatementBinder().bind(updateStatement, new SQLStatementBinderContext(createMetaData(), "foo_db", new HintValueContext(), databaseType, updateStatement));
5862
assertThat(actual, not(updateStatement));
5963
assertThat(actual.getTable(), not(updateStatement.getTable()));
6064
assertThat(actual.getTable(), instanceOf(SimpleTableSegment.class));

0 commit comments

Comments
 (0)