You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2020/09/18 10:47:15 UTC
[shardingsphere] branch master updated: refactor InsertStatement to
different dialect Statement (#7506)
This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 20a43de refactor InsertStatement to different dialect Statement (#7506)
20a43de is described below
commit 20a43de8eb0da436abbbf9c199ac68e3434e6960
Author: DuanZhengqiang <st...@gmail.com>
AuthorDate: Fri Sep 18 05:47:02 2020 -0500
refactor InsertStatement to different dialect Statement (#7506)
* refactor InsertStatement in DMLStatement
* add unit test for SQLStatementUtils
---
.../impl/EncryptAssignmentParameterRewriter.java | 13 +--
...OnDuplicateKeyUpdateValueParameterRewriter.java | 3 +-
.../impl/EncryptInsertValueParameterRewriter.java | 3 +-
.../impl/EncryptAssignmentTokenGenerator.java | 9 +-
.../impl/EncryptInsertOnUpdateTokenGenerator.java | 13 ++-
.../GeneratedKeyAssignmentTokenGenerator.java | 10 +-
.../impl/ShardingInsertStatementValidator.java | 3 +-
.../impl/ShardingInsertStatementValidatorTest.java | 2 +-
.../statement/dml/InsertStatementContext.java | 7 +-
.../statement/SQLStatementContextFactoryTest.java | 7 +-
.../statement/impl/InsertStatementContextTest.java | 4 +-
.../parser/mysql/visitor/impl/MySQLDMLVisitor.java | 13 ++-
.../sql/common/statement/dml/InsertStatement.java | 109 +++++----------------
.../parser/sql/common/util/SQLStatementUtils.java | 82 ++++++++++++++++
.../statement/mysql/dml/MySQLInsertStatement.java | 86 ++++++++++++++++
.../oracle/dml/OracleInsertStatement.java | 34 +++++++
.../postgresql/dml/PostgreSQLInsertStatement.java | 53 ++++++++++
.../statement/sql92/dml/SQL92InsertStatement.java | 34 +++++++
.../sqlserver/dml/SQLServerInsertStatement.java | 51 ++++++++++
.../sql/common/util/SQLStatementUtilsTest.java | 95 ++++++++++++++++++
.../mysql/dml/MySQLInsertStatementTest.java | 10 +-
.../oracle/dml/OracleInsertStatementTest.java | 45 ---------
.../dml/PostgreSQLInsertStatementTest.java | 45 ---------
.../sql92/dml/SQL92InsertStatementTest.java | 45 ---------
.../dml/SQLServerInsertStatementTest.java | 45 ---------
.../statement/dml/impl/InsertStatementAssert.java | 33 ++++---
26 files changed, 534 insertions(+), 320 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptAssignmentParameterRewriter.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptAssignmentParameterRewriter.java
index c238fac..595d762 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptAssignmentParameterRewriter.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptAssignmentParameterRewriter.java
@@ -19,19 +19,20 @@ package org.apache.shardingsphere.encrypt.rewrite.parameter.impl;
import com.google.common.base.Preconditions;
import org.apache.shardingsphere.encrypt.rewrite.parameter.EncryptParameterRewriter;
-import org.apache.shardingsphere.sql.parser.binder.type.TableAvailable;
+import org.apache.shardingsphere.infra.rewrite.parameter.builder.ParameterBuilder;
+import org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.GroupedParameterBuilder;
+import org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.StandardParameterBuilder;
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.sql.parser.binder.statement.dml.UpdateStatementContext;
+import org.apache.shardingsphere.sql.parser.binder.type.TableAvailable;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.SetAssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.UpdateStatement;
-import org.apache.shardingsphere.infra.rewrite.parameter.builder.ParameterBuilder;
-import org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.GroupedParameterBuilder;
-import org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.StandardParameterBuilder;
+import org.apache.shardingsphere.sql.parser.sql.common.util.SQLStatementUtils;
import java.util.Collection;
import java.util.Collections;
@@ -50,7 +51,7 @@ public final class EncryptAssignmentParameterRewriter extends EncryptParameterRe
return true;
}
if (sqlStatementContext instanceof InsertStatementContext) {
- return (((InsertStatementContext) sqlStatementContext).getSqlStatement()).getSetAssignment().isPresent();
+ return SQLStatementUtils.getSetAssignmentSegment(((InsertStatementContext) sqlStatementContext).getSqlStatement()).isPresent();
}
return false;
}
@@ -69,7 +70,7 @@ public final class EncryptAssignmentParameterRewriter extends EncryptParameterRe
private SetAssignmentSegment getSetAssignmentSegment(final SQLStatement sqlStatement) {
if (sqlStatement instanceof InsertStatement) {
- Optional<SetAssignmentSegment> result = ((InsertStatement) sqlStatement).getSetAssignment();
+ Optional<SetAssignmentSegment> result = SQLStatementUtils.getSetAssignmentSegment((InsertStatement) sqlStatement);
Preconditions.checkState(result.isPresent());
return result.get();
}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptInsertOnDuplicateKeyUpdateValueParameterRewriter.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptInsertOnDuplicateKeyUpdateValueParameterRewriter.java
index 3c088af..29806a2 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptInsertOnDuplicateKeyUpdateValueParameterRewriter.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptInsertOnDuplicateKeyUpdateValueParameterRewriter.java
@@ -27,6 +27,7 @@ import org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.GroupedPar
import org.apache.shardingsphere.sql.parser.binder.segment.insert.values.OnDuplicateUpdateContext;
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext;
+import org.apache.shardingsphere.sql.parser.sql.common.util.SQLStatementUtils;
import java.util.Collection;
import java.util.LinkedList;
@@ -41,7 +42,7 @@ public final class EncryptInsertOnDuplicateKeyUpdateValueParameterRewriter exten
@Override
protected boolean isNeedRewriteForEncrypt(final SQLStatementContext sqlStatementContext) {
- return sqlStatementContext instanceof InsertStatementContext && ((InsertStatementContext) sqlStatementContext).getSqlStatement().getOnDuplicateKeyColumns().isPresent();
+ return sqlStatementContext instanceof InsertStatementContext && SQLStatementUtils.getOnDuplicateKeyColumnsSegment(((InsertStatementContext) sqlStatementContext).getSqlStatement()).isPresent();
}
@Override
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptInsertValueParameterRewriter.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptInsertValueParameterRewriter.java
index 596ed14..4080aee 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptInsertValueParameterRewriter.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptInsertValueParameterRewriter.java
@@ -28,6 +28,7 @@ import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext
import org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.util.SQLStatementUtils;
import java.util.ArrayList;
import java.util.Collection;
@@ -43,7 +44,7 @@ public final class EncryptInsertValueParameterRewriter extends EncryptParameterR
@Override
protected boolean isNeedRewriteForEncrypt(final SQLStatementContext sqlStatementContext) {
- return sqlStatementContext instanceof InsertStatementContext && !(((InsertStatementContext) sqlStatementContext).getSqlStatement()).getSetAssignment().isPresent();
+ return sqlStatementContext instanceof InsertStatementContext && !SQLStatementUtils.getSetAssignmentSegment(((InsertStatementContext) sqlStatementContext).getSqlStatement()).isPresent();
}
@Override
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptAssignmentTokenGenerator.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptAssignmentTokenGenerator.java
index 5cd3b53..9b1d50c 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptAssignmentTokenGenerator.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptAssignmentTokenGenerator.java
@@ -22,10 +22,11 @@ import org.apache.shardingsphere.encrypt.rewrite.token.generator.BaseEncryptSQLT
import org.apache.shardingsphere.encrypt.rewrite.token.pojo.EncryptAssignmentToken;
import org.apache.shardingsphere.encrypt.rewrite.token.pojo.EncryptLiteralAssignmentToken;
import org.apache.shardingsphere.encrypt.rewrite.token.pojo.EncryptParameterAssignmentToken;
-import org.apache.shardingsphere.sql.parser.binder.type.TableAvailable;
+import org.apache.shardingsphere.infra.rewrite.sql.token.generator.CollectionSQLTokenGenerator;
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.sql.parser.binder.statement.dml.UpdateStatementContext;
+import org.apache.shardingsphere.sql.parser.binder.type.TableAvailable;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.SetAssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
@@ -33,7 +34,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.P
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.UpdateStatement;
-import org.apache.shardingsphere.infra.rewrite.sql.token.generator.CollectionSQLTokenGenerator;
+import org.apache.shardingsphere.sql.parser.sql.common.util.SQLStatementUtils;
import java.util.Collection;
import java.util.Collections;
@@ -48,7 +49,7 @@ public final class EncryptAssignmentTokenGenerator extends BaseEncryptSQLTokenGe
@Override
protected boolean isGenerateSQLTokenForEncrypt(final SQLStatementContext sqlStatementContext) {
return sqlStatementContext instanceof UpdateStatementContext
- || (sqlStatementContext instanceof InsertStatementContext && (((InsertStatementContext) sqlStatementContext).getSqlStatement()).getSetAssignment().isPresent());
+ || (sqlStatementContext instanceof InsertStatementContext && SQLStatementUtils.getSetAssignmentSegment(((InsertStatementContext) sqlStatementContext).getSqlStatement()).isPresent());
}
@Override
@@ -65,7 +66,7 @@ public final class EncryptAssignmentTokenGenerator extends BaseEncryptSQLTokenGe
private SetAssignmentSegment getSetAssignmentSegment(final SQLStatement sqlStatement) {
if (sqlStatement instanceof InsertStatement) {
- Optional<SetAssignmentSegment> result = ((InsertStatement) sqlStatement).getSetAssignment();
+ Optional<SetAssignmentSegment> result = SQLStatementUtils.getSetAssignmentSegment((InsertStatement) sqlStatement);
Preconditions.checkState(result.isPresent());
return result.get();
}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptInsertOnUpdateTokenGenerator.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptInsertOnUpdateTokenGenerator.java
index a0dde20..77fcb11 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptInsertOnUpdateTokenGenerator.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptInsertOnUpdateTokenGenerator.java
@@ -22,13 +22,15 @@ import org.apache.shardingsphere.encrypt.rewrite.token.generator.BaseEncryptSQLT
import org.apache.shardingsphere.encrypt.rewrite.token.pojo.EncryptAssignmentToken;
import org.apache.shardingsphere.encrypt.rewrite.token.pojo.EncryptLiteralAssignmentToken;
import org.apache.shardingsphere.encrypt.rewrite.token.pojo.EncryptParameterAssignmentToken;
+import org.apache.shardingsphere.infra.rewrite.sql.token.generator.CollectionSQLTokenGenerator;
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.OnDuplicateKeyColumnsSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
-import org.apache.shardingsphere.infra.rewrite.sql.token.generator.CollectionSQLTokenGenerator;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.util.SQLStatementUtils;
import java.util.Collection;
import java.util.Collections;
@@ -42,15 +44,16 @@ public final class EncryptInsertOnUpdateTokenGenerator extends BaseEncryptSQLTok
@Override
protected boolean isGenerateSQLTokenForEncrypt(final SQLStatementContext sqlStatementContext) {
- return sqlStatementContext instanceof InsertStatementContext && (((InsertStatementContext) sqlStatementContext).getSqlStatement()).getOnDuplicateKeyColumns().isPresent();
+ return sqlStatementContext instanceof InsertStatementContext && SQLStatementUtils.getOnDuplicateKeyColumnsSegment(((InsertStatementContext) sqlStatementContext).getSqlStatement()).isPresent();
}
@Override
public Collection<EncryptAssignmentToken> generateSQLTokens(final InsertStatementContext insertStatementContext) {
Collection<EncryptAssignmentToken> result = new LinkedList<>();
- String tableName = insertStatementContext.getSqlStatement().getTable().getTableName().getIdentifier().getValue();
- Preconditions.checkState(insertStatementContext.getSqlStatement().getOnDuplicateKeyColumns().isPresent());
- OnDuplicateKeyColumnsSegment onDuplicateKeyColumnsSegment = insertStatementContext.getSqlStatement().getOnDuplicateKeyColumns().get();
+ InsertStatement insertStatement = insertStatementContext.getSqlStatement();
+ String tableName = insertStatement.getTable().getTableName().getIdentifier().getValue();
+ Preconditions.checkState(SQLStatementUtils.getOnDuplicateKeyColumnsSegment(insertStatement).isPresent());
+ OnDuplicateKeyColumnsSegment onDuplicateKeyColumnsSegment = SQLStatementUtils.getOnDuplicateKeyColumnsSegment(insertStatement).get();
Collection<AssignmentSegment> onDuplicateKeyColumnsSegments = onDuplicateKeyColumnsSegment.getColumns();
if (onDuplicateKeyColumnsSegments.isEmpty()) {
return result;
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/keygen/GeneratedKeyAssignmentTokenGenerator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/keygen/GeneratedKeyAssignmentTokenGenerator.java
index bbcdf9a..b7ebc2c 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/keygen/GeneratedKeyAssignmentTokenGenerator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/keygen/GeneratedKeyAssignmentTokenGenerator.java
@@ -19,13 +19,14 @@ package org.apache.shardingsphere.sharding.rewrite.token.generator.impl.keygen;
import com.google.common.base.Preconditions;
import lombok.Setter;
+import org.apache.shardingsphere.infra.rewrite.sql.token.generator.aware.ParametersAware;
import org.apache.shardingsphere.sharding.rewrite.token.pojo.GeneratedKeyAssignmentToken;
import org.apache.shardingsphere.sharding.rewrite.token.pojo.LiteralGeneratedKeyAssignmentToken;
import org.apache.shardingsphere.sharding.rewrite.token.pojo.ParameterMarkerGeneratedKeyAssignmentToken;
import org.apache.shardingsphere.sql.parser.binder.segment.insert.keygen.GeneratedKeyContext;
import org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
-import org.apache.shardingsphere.infra.rewrite.sql.token.generator.aware.ParametersAware;
+import org.apache.shardingsphere.sql.parser.sql.common.util.SQLStatementUtils;
import java.util.List;
import java.util.Optional;
@@ -40,15 +41,16 @@ public final class GeneratedKeyAssignmentTokenGenerator extends BaseGeneratedKey
@Override
protected boolean isGenerateSQLToken(final InsertStatement insertStatement) {
- return insertStatement.getSetAssignment().isPresent();
+ return SQLStatementUtils.getSetAssignmentSegment(insertStatement).isPresent();
}
@Override
public GeneratedKeyAssignmentToken generateSQLToken(final InsertStatementContext insertStatementContext) {
Optional<GeneratedKeyContext> generatedKey = insertStatementContext.getGeneratedKeyContext();
Preconditions.checkState(generatedKey.isPresent());
- Preconditions.checkState(insertStatementContext.getSqlStatement().getSetAssignment().isPresent());
- int startIndex = insertStatementContext.getSqlStatement().getSetAssignment().get().getStopIndex() + 1;
+ InsertStatement insertStatement = insertStatementContext.getSqlStatement();
+ Preconditions.checkState(SQLStatementUtils.getSetAssignmentSegment(insertStatement).isPresent());
+ int startIndex = SQLStatementUtils.getSetAssignmentSegment(insertStatement).get().getStopIndex() + 1;
return parameters.isEmpty() ? new LiteralGeneratedKeyAssignmentToken(startIndex, generatedKey.get().getColumnName(), generatedKey.get().getGeneratedValues().getLast())
: new ParameterMarkerGeneratedKeyAssignmentToken(startIndex, generatedKey.get().getColumnName());
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidator.java
index 19e3cd0..ed577b1 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidator.java
@@ -32,6 +32,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.Column
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.OnDuplicateKeyColumnsSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubquerySegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.util.SQLStatementUtils;
import java.util.Collection;
import java.util.Optional;
@@ -48,7 +49,7 @@ public final class ShardingInsertStatementValidator implements ShardingStatement
throw new ShardingSphereException("Cannot support Multiple-Table for '%s'.", sqlStatementContext.getSqlStatement());
}
InsertStatement sqlStatement = (InsertStatement) sqlStatementContext.getSqlStatement();
- Optional<OnDuplicateKeyColumnsSegment> onDuplicateKeyColumnsSegment = sqlStatement.getOnDuplicateKeyColumns();
+ Optional<OnDuplicateKeyColumnsSegment> onDuplicateKeyColumnsSegment = SQLStatementUtils.getOnDuplicateKeyColumnsSegment(sqlStatement);
String tableName = sqlStatement.getTable().getTableName().getIdentifier().getValue();
if (onDuplicateKeyColumnsSegment.isPresent() && isUpdateShardingKey(shardingRule, onDuplicateKeyColumnsSegment.get(), tableName)) {
throw new ShardingSphereException("INSERT INTO ... ON DUPLICATE KEY UPDATE can not support update for sharding column.");
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidatorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidatorTest.java
index b83cc54..b894502 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidatorTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidatorTest.java
@@ -127,7 +127,7 @@ public final class ShardingInsertStatementValidatorTest {
}
private InsertStatement createInsertStatement() {
- InsertStatement result = new MySQLInsertStatement();
+ MySQLInsertStatement result = new MySQLInsertStatement();
result.setTable(new SimpleTableSegment(0, 0, new IdentifierValue("user")));
ColumnSegment columnSegment = new ColumnSegment(0, 0, new IdentifierValue("id"));
AssignmentSegment assignmentSegment = new AssignmentSegment(0, 0, columnSegment, new ParameterMarkerExpressionSegment(0, 0, 1));
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/dml/InsertStatementContext.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/dml/InsertStatementContext.java
index de1882d..8c83b21 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/dml/InsertStatementContext.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/dml/InsertStatementContext.java
@@ -29,10 +29,12 @@ import org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext;
import org.apache.shardingsphere.sql.parser.binder.statement.CommonSQLStatementContext;
import org.apache.shardingsphere.sql.parser.binder.type.TableAvailable;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.AssignmentSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.OnDuplicateKeyColumnsSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubquerySegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.util.SQLStatementUtils;
import org.apache.shardingsphere.sql.parser.sql.common.util.TableExtractUtils;
import java.util.ArrayList;
@@ -104,10 +106,11 @@ public final class InsertStatementContext extends CommonSQLStatementContext<Inse
}
private Optional<OnDuplicateUpdateContext> getOnDuplicateKeyUpdateValueContext(final List<Object> parameters, final AtomicInteger parametersOffset) {
- if (!getSqlStatement().getOnDuplicateKeyColumns().isPresent()) {
+ Optional<OnDuplicateKeyColumnsSegment> onDuplicateKeyColumnsSegment = SQLStatementUtils.getOnDuplicateKeyColumnsSegment(getSqlStatement());
+ if (!onDuplicateKeyColumnsSegment.isPresent()) {
return Optional.empty();
}
- Collection<AssignmentSegment> onDuplicateKeyColumns = getSqlStatement().getOnDuplicateKeyColumns().get().getColumns();
+ Collection<AssignmentSegment> onDuplicateKeyColumns = onDuplicateKeyColumnsSegment.get().getColumns();
OnDuplicateUpdateContext onDuplicateUpdateContext = new OnDuplicateUpdateContext(onDuplicateKeyColumns, parameters, parametersOffset.get());
parametersOffset.addAndGet(onDuplicateUpdateContext.getParameterCount());
return Optional.of(onDuplicateUpdateContext);
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/statement/SQLStatementContextFactoryTest.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/statement/SQLStatementContextFactoryTest.java
index e9f8211..30e4faa 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/statement/SQLStatementContextFactoryTest.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/statement/SQLStatementContextFactoryTest.java
@@ -59,7 +59,10 @@ public final class SQLStatementContextFactoryTest {
@Test
public void assertSQLStatementContextCreatedWhenSQLStatementInstanceOfMySQLInsertStatement() {
- assertSQLStatementContextCreatedWhenSQLStatementInstanceOfInsertStatement(new MySQLInsertStatement());
+ MySQLInsertStatement insertStatement = new MySQLInsertStatement();
+ insertStatement.setSetAssignment(new SetAssignmentSegment(0, 0,
+ Collections.singleton(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("IdentifierValue")), null))));
+ assertSQLStatementContextCreatedWhenSQLStatementInstanceOfInsertStatement(insertStatement);
}
@Test
@@ -83,8 +86,6 @@ public final class SQLStatementContextFactoryTest {
}
private void assertSQLStatementContextCreatedWhenSQLStatementInstanceOfInsertStatement(final InsertStatement insertStatement) {
- insertStatement.setSetAssignment(new SetAssignmentSegment(0, 0,
- Collections.singleton(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("IdentifierValue")), null))));
insertStatement.setTable(new SimpleTableSegment(0, 0, new IdentifierValue("tbl")));
SQLStatementContext sqlStatementContext = SQLStatementContextFactory.newInstance(mock(SchemaMetaData.class), null, insertStatement);
assertNotNull(sqlStatementContext);
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/statement/impl/InsertStatementContextTest.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/statement/impl/InsertStatementContextTest.java
index dd5b003..89e6dd4 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/statement/impl/InsertStatementContextTest.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/statement/impl/InsertStatementContextTest.java
@@ -119,7 +119,7 @@ public final class InsertStatementContextTest {
public void assertGetGroupedParametersWithOnDuplicateParameters() {
SchemaMetaData schemaMetaData = mock(SchemaMetaData.class);
when(schemaMetaData.getAllColumnNames("tbl")).thenReturn(Arrays.asList("id", "name", "status"));
- InsertStatement insertStatement = new MySQLInsertStatement();
+ MySQLInsertStatement insertStatement = new MySQLInsertStatement();
insertStatement.setTable(new SimpleTableSegment(0, 0, new IdentifierValue("tbl")));
setUpInsertValues(insertStatement);
setUpOnDuplicateValues(insertStatement);
@@ -152,7 +152,7 @@ public final class InsertStatementContextTest {
new ParameterMarkerExpressionSegment(0, 0, 3), new ParameterMarkerExpressionSegment(0, 0, 4), new LiteralExpressionSegment(0, 0, "init"))));
}
- private void setUpOnDuplicateValues(final InsertStatement insertStatement) {
+ private void setUpOnDuplicateValues(final MySQLInsertStatement insertStatement) {
AssignmentSegment parameterMarkerExpressionAssignment = new AssignmentSegment(0, 0,
new ColumnSegment(0, 0, new IdentifierValue("on_duplicate_key_update_column_1")), new ParameterMarkerExpressionSegment(0, 0, 4));
AssignmentSegment literalExpressionAssignment = new AssignmentSegment(0, 0,
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/impl/MySQLDMLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/impl/MySQLDMLVisitor.java
index 73714dc..7a9644d 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/impl/MySQLDMLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/impl/MySQLDMLVisitor.java
@@ -102,10 +102,6 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.Joi
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SubqueryTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableSegment;
-import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLCallStatement;
-import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLDeleteStatement;
-import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLDoStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.UpdateStatement;
import org.apache.shardingsphere.sql.parser.sql.common.value.collection.CollectionValue;
@@ -114,6 +110,9 @@ import org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl.Boolea
import org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl.NumberLiteralValue;
import org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl.StringLiteralValue;
import org.apache.shardingsphere.sql.parser.sql.common.value.parametermarker.ParameterMarkerValue;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLCallStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLDeleteStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLDoStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLInsertStatement;
import java.util.Collection;
@@ -197,11 +196,11 @@ public final class MySQLDMLVisitor extends MySQLVisitor implements DMLVisitor {
@Override
public ASTNode visitReplace(final ReplaceContext ctx) {
// TODO :FIXME, since there is no segment for replaceValuesClause, ReplaceStatement is created by sub rule.
- InsertStatement result;
+ MySQLInsertStatement result;
if (null != ctx.replaceValuesClause()) {
- result = (InsertStatement) visit(ctx.replaceValuesClause());
+ result = (MySQLInsertStatement) visit(ctx.replaceValuesClause());
} else if (null != ctx.replaceSelectClause()) {
- result = (InsertStatement) visit(ctx.replaceSelectClause());
+ result = (MySQLInsertStatement) visit(ctx.replaceSelectClause());
} else {
result = new MySQLInsertStatement();
result.setSetAssignment((SetAssignmentSegment) visit(ctx.setAssignmentsClause()));
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/dml/InsertStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/dml/InsertStatement.java
index acc2114..dc8ee40 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/dml/InsertStatement.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/dml/InsertStatement.java
@@ -19,15 +19,11 @@ package org.apache.shardingsphere.sql.parser.sql.common.statement.dml;
import lombok.Getter;
import lombok.Setter;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.InsertValuesSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.SetAssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.InsertColumnsSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.OnDuplicateKeyColumnsSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubquerySegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WithSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.AbstractSQLStatement;
@@ -49,14 +45,8 @@ public abstract class InsertStatement extends AbstractSQLStatement implements DM
private InsertColumnsSegment insertColumns;
- private SetAssignmentSegment setAssignment;
-
private SubquerySegment insertSelect;
- private OnDuplicateKeyColumnsSegment onDuplicateKeyColumns;
-
- private WithSegment withSegment;
-
private final Collection<InsertValuesSegment> values = new LinkedList<>();
/**
@@ -78,15 +68,6 @@ public abstract class InsertStatement extends AbstractSQLStatement implements DM
}
/**
- * Get set assignment segment.
- *
- * @return set assignment segment
- */
- public Optional<SetAssignmentSegment> getSetAssignment() {
- return Optional.ofNullable(setAssignment);
- }
-
- /**
* Get insert select segment.
*
* @return insert select segment
@@ -96,106 +77,62 @@ public abstract class InsertStatement extends AbstractSQLStatement implements DM
}
/**
- * Get on duplicate key columns segment.
- *
- * @return on duplicate key columns segment
- */
- public Optional<OnDuplicateKeyColumnsSegment> getOnDuplicateKeyColumns() {
- return Optional.ofNullable(onDuplicateKeyColumns);
- }
-
- /**
- * Get with segment.
- *
- * @return with segment.
- */
- public Optional<WithSegment> getWithSegment() {
- return Optional.ofNullable(withSegment);
- }
-
- /**
* Judge is use default columns or not.
*
* @return is use default columns or not
*/
- public boolean useDefaultColumns() {
- return getColumns().isEmpty() && null == setAssignment;
- }
+ public abstract boolean useDefaultColumns();
/**
* Get column names.
*
* @return column names
*/
- public List<String> getColumnNames() {
- return null == setAssignment ? getColumnNamesForInsertColumns() : getColumnNamesForSetAssignment();
- }
-
- private List<String> getColumnNamesForInsertColumns() {
- List<String> result = new LinkedList<>();
- for (ColumnSegment each : getColumns()) {
- result.add(each.getIdentifier().getValue().toLowerCase());
- }
- return result;
- }
-
- private List<String> getColumnNamesForSetAssignment() {
- List<String> result = new LinkedList<>();
- for (AssignmentSegment each : setAssignment.getAssignments()) {
- result.add(each.getColumn().getIdentifier().getValue().toLowerCase());
- }
- return result;
- }
+ public abstract List<String> getColumnNames();
/**
* Get value list count.
*
* @return value list count
*/
- public int getValueListCount() {
- return null == setAssignment ? values.size() : 1;
- }
+ public abstract int getValueListCount();
/**
* Get value count for per value list.
*
* @return value count
*/
- public int getValueCountForPerGroup() {
- if (!values.isEmpty()) {
- return values.iterator().next().getValues().size();
- }
- if (null != setAssignment) {
- return setAssignment.getAssignments().size();
- }
- if (null != insertSelect) {
- return insertSelect.getSelect().getProjections().getProjections().size();
- }
- return 0;
- }
+ public abstract int getValueCountForPerGroup();
/**
* Get all value expressions.
*
* @return all value expressions
*/
- public List<List<ExpressionSegment>> getAllValueExpressions() {
- return null == setAssignment ? getAllValueExpressionsFromValues() : Collections.singletonList(getAllValueExpressionsFromSetAssignment());
+ public abstract List<List<ExpressionSegment>> getAllValueExpressions();
+
+ /**
+ * Get column names for insert columns.
+ *
+ * @return column names
+ */
+ protected List<String> getColumnNamesForInsertColumns() {
+ List<String> result = new LinkedList<>();
+ for (ColumnSegment each : getColumns()) {
+ result.add(each.getIdentifier().getValue().toLowerCase());
+ }
+ return result;
}
-
- private List<List<ExpressionSegment>> getAllValueExpressionsFromValues() {
+
+ /**
+ * Get all value expressions from values.
+ * @return all value expressions
+ */
+ protected List<List<ExpressionSegment>> getAllValueExpressionsFromValues() {
List<List<ExpressionSegment>> result = new ArrayList<>(values.size());
for (InsertValuesSegment each : values) {
result.add(each.getValues());
}
return result;
}
-
- private List<ExpressionSegment> getAllValueExpressionsFromSetAssignment() {
- List<ExpressionSegment> result = new ArrayList<>(setAssignment.getAssignments().size());
- for (AssignmentSegment each : setAssignment.getAssignments()) {
- result.add(each.getValue());
- }
- return result;
- }
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/SQLStatementUtils.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/SQLStatementUtils.java
new file mode 100644
index 0000000..f17ae9f
--- /dev/null
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/SQLStatementUtils.java
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.sql.parser.sql.common.util;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.SetAssignmentSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.OnDuplicateKeyColumnsSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WithSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.MySQLStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLInsertStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.PostgreSQLStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dml.PostgreSQLInsertStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.SQLServerStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerInsertStatement;
+
+import java.util.Optional;
+
+/**
+ * SQL statement utility class.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class SQLStatementUtils {
+
+ /**
+ * Get OnDuplicateKeyColumnsSegment.
+ *
+ * @param insertStatement InsertStatement
+ * @return OnDuplicateKeyColumnsSegment
+ */
+ public static Optional<OnDuplicateKeyColumnsSegment> getOnDuplicateKeyColumnsSegment(final InsertStatement insertStatement) {
+ if (insertStatement instanceof MySQLStatement) {
+ return ((MySQLInsertStatement) insertStatement).getOnDuplicateKeyColumns();
+ }
+ return Optional.empty();
+ }
+
+ /**
+ * Get SetAssignmentSegment.
+ *
+ * @param insertStatement InsertStatement
+ * @return SetAssignmentSegment
+ */
+ public static Optional<SetAssignmentSegment> getSetAssignmentSegment(final InsertStatement insertStatement) {
+ if (insertStatement instanceof MySQLStatement) {
+ return ((MySQLInsertStatement) insertStatement).getSetAssignment();
+ }
+ return Optional.empty();
+ }
+
+ /**
+ * Get WithSegment.
+ *
+ * @param insertStatement InsertStatement
+ * @return WithSegment
+ */
+ public static Optional<WithSegment> getWithSegment(final InsertStatement insertStatement) {
+ if (insertStatement instanceof PostgreSQLStatement) {
+ return ((PostgreSQLInsertStatement) insertStatement).getWithSegment();
+ }
+ if (insertStatement instanceof SQLServerStatement) {
+ return ((SQLServerInsertStatement) insertStatement).getWithSegment();
+ }
+ return Optional.empty();
+ }
+}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/mysql/dml/MySQLInsertStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/mysql/dml/MySQLInsertStatement.java
index 035dd43..58a7814 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/mysql/dml/MySQLInsertStatement.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/mysql/dml/MySQLInsertStatement.java
@@ -17,11 +17,97 @@
package org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.AssignmentSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.SetAssignmentSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.OnDuplicateKeyColumnsSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.MySQLStatement;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Optional;
+
/**
* MySQL insert statement.
*/
+@Getter
+@Setter
public final class MySQLInsertStatement extends InsertStatement implements MySQLStatement {
+
+ private SetAssignmentSegment setAssignment;
+
+ private OnDuplicateKeyColumnsSegment onDuplicateKeyColumns;
+
+ /**
+ * Get set assignment segment.
+ *
+ * @return set assignment segment
+ */
+ public Optional<SetAssignmentSegment> getSetAssignment() {
+ return Optional.ofNullable(setAssignment);
+ }
+
+ /**
+ * Get on duplicate key columns segment.
+ *
+ * @return on duplicate key columns segment
+ */
+ public Optional<OnDuplicateKeyColumnsSegment> getOnDuplicateKeyColumns() {
+ return Optional.ofNullable(onDuplicateKeyColumns);
+ }
+
+ @Override
+ public boolean useDefaultColumns() {
+ return getColumns().isEmpty() && null == setAssignment;
+ }
+
+ @Override
+ public List<String> getColumnNames() {
+ return null == setAssignment ? getColumnNamesForInsertColumns() : getColumnNamesForSetAssignment();
+ }
+
+ @Override
+ public int getValueListCount() {
+ return null == setAssignment ? getValues().size() : 1;
+ }
+
+ @Override
+ public int getValueCountForPerGroup() {
+ if (!getValues().isEmpty()) {
+ return getValues().iterator().next().getValues().size();
+ }
+ if (null != setAssignment) {
+ return setAssignment.getAssignments().size();
+ }
+ if (getInsertSelect().isPresent()) {
+ return getInsertSelect().get().getSelect().getProjections().getProjections().size();
+ }
+ return 0;
+ }
+
+ @Override
+ public List<List<ExpressionSegment>> getAllValueExpressions() {
+ return null == setAssignment ? getAllValueExpressionsFromValues() : Collections.singletonList(getAllValueExpressionsFromSetAssignment());
+ }
+
+ private List<String> getColumnNamesForSetAssignment() {
+ List<String> result = new LinkedList<>();
+ for (AssignmentSegment each : setAssignment.getAssignments()) {
+ result.add(each.getColumn().getIdentifier().getValue().toLowerCase());
+ }
+ return result;
+ }
+
+ private List<ExpressionSegment> getAllValueExpressionsFromSetAssignment() {
+ List<ExpressionSegment> result = new ArrayList<>(setAssignment.getAssignments().size());
+ for (AssignmentSegment each : setAssignment.getAssignments()) {
+ result.add(each.getValue());
+ }
+ return result;
+ }
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/oracle/dml/OracleInsertStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/oracle/dml/OracleInsertStatement.java
index 41ae54a..391780c 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/oracle/dml/OracleInsertStatement.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/oracle/dml/OracleInsertStatement.java
@@ -17,11 +17,45 @@
package org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.dml;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.OracleStatement;
+import java.util.List;
+
/**
* Oracle insert statement.
*/
public final class OracleInsertStatement extends InsertStatement implements OracleStatement {
+
+ @Override
+ public boolean useDefaultColumns() {
+ return getColumns().isEmpty();
+ }
+
+ @Override
+ public List<String> getColumnNames() {
+ return getColumnNamesForInsertColumns();
+ }
+
+ @Override
+ public int getValueListCount() {
+ return getValues().size();
+ }
+
+ @Override
+ public int getValueCountForPerGroup() {
+ if (!getValues().isEmpty()) {
+ return getValues().iterator().next().getValues().size();
+ }
+ if (getInsertSelect().isPresent()) {
+ return getInsertSelect().get().getSelect().getProjections().getProjections().size();
+ }
+ return 0;
+ }
+
+ @Override
+ public List<List<ExpressionSegment>> getAllValueExpressions() {
+ return getAllValueExpressionsFromValues();
+ }
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/dml/PostgreSQLInsertStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/dml/PostgreSQLInsertStatement.java
index d74deee..2cd7a18 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/dml/PostgreSQLInsertStatement.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/dml/PostgreSQLInsertStatement.java
@@ -17,11 +17,64 @@
package org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dml;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubquerySegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WithSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.PostgreSQLStatement;
+import java.util.List;
+import java.util.Optional;
+
/**
* PostgreSQL insert statement.
*/
+@Getter
+@Setter
public final class PostgreSQLInsertStatement extends InsertStatement implements PostgreSQLStatement {
+
+ private WithSegment withSegment;
+
+ /**
+ * Get with segment.
+ *
+ * @return with segment.
+ */
+ public Optional<WithSegment> getWithSegment() {
+ return Optional.ofNullable(withSegment);
+ }
+
+ @Override
+ public boolean useDefaultColumns() {
+ return getColumns().isEmpty();
+ }
+
+ @Override
+ public List<String> getColumnNames() {
+ return getColumnNamesForInsertColumns();
+ }
+
+ @Override
+ public int getValueListCount() {
+ return getValues().size();
+ }
+
+ @Override
+ public int getValueCountForPerGroup() {
+ if (!getValues().isEmpty()) {
+ return getValues().iterator().next().getValues().size();
+ }
+ Optional<SubquerySegment> insertSelectOptional = getInsertSelect();
+ if (insertSelectOptional.isPresent()) {
+ return insertSelectOptional.get().getSelect().getProjections().getProjections().size();
+ }
+ return 0;
+ }
+
+ @Override
+ public List<List<ExpressionSegment>> getAllValueExpressions() {
+ return getAllValueExpressionsFromValues();
+ }
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sql92/dml/SQL92InsertStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sql92/dml/SQL92InsertStatement.java
index 91db714..db9fe7b 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sql92/dml/SQL92InsertStatement.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sql92/dml/SQL92InsertStatement.java
@@ -17,11 +17,45 @@
package org.apache.shardingsphere.sql.parser.sql.dialect.statement.sql92.dml;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sql92.SQL92Statement;
+import java.util.List;
+
/**
* SQL92 insert statement.
*/
public final class SQL92InsertStatement extends InsertStatement implements SQL92Statement {
+
+ @Override
+ public boolean useDefaultColumns() {
+ return getColumns().isEmpty();
+ }
+
+ @Override
+ public List<String> getColumnNames() {
+ return getColumnNamesForInsertColumns();
+ }
+
+ @Override
+ public int getValueListCount() {
+ return getValues().size();
+ }
+
+ @Override
+ public int getValueCountForPerGroup() {
+ if (!getValues().isEmpty()) {
+ return getValues().iterator().next().getValues().size();
+ }
+ if (getInsertSelect().isPresent()) {
+ return getInsertSelect().get().getSelect().getProjections().getProjections().size();
+ }
+ return 0;
+ }
+
+ @Override
+ public List<List<ExpressionSegment>> getAllValueExpressions() {
+ return getAllValueExpressionsFromValues();
+ }
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sqlserver/dml/SQLServerInsertStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sqlserver/dml/SQLServerInsertStatement.java
index 151714a..22e9eda 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sqlserver/dml/SQLServerInsertStatement.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sqlserver/dml/SQLServerInsertStatement.java
@@ -17,11 +17,62 @@
package org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WithSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.SQLServerStatement;
+import java.util.List;
+import java.util.Optional;
+
/**
* SQLServer insert statement.
*/
+@Getter
+@Setter
public final class SQLServerInsertStatement extends InsertStatement implements SQLServerStatement {
+
+ private WithSegment withSegment;
+
+ /**
+ * Get with segment.
+ *
+ * @return with segment.
+ */
+ public Optional<WithSegment> getWithSegment() {
+ return Optional.ofNullable(withSegment);
+ }
+
+ @Override
+ public boolean useDefaultColumns() {
+ return getColumns().isEmpty();
+ }
+
+ @Override
+ public List<String> getColumnNames() {
+ return getColumnNamesForInsertColumns();
+ }
+
+ @Override
+ public int getValueListCount() {
+ return getValues().size();
+ }
+
+ @Override
+ public int getValueCountForPerGroup() {
+ if (!getValues().isEmpty()) {
+ return getValues().iterator().next().getValues().size();
+ }
+ if (getInsertSelect().isPresent()) {
+ return getInsertSelect().get().getSelect().getProjections().getProjections().size();
+ }
+ return 0;
+ }
+
+ @Override
+ public List<List<ExpressionSegment>> getAllValueExpressions() {
+ return getAllValueExpressionsFromValues();
+ }
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/util/SQLStatementUtilsTest.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/util/SQLStatementUtilsTest.java
new file mode 100644
index 0000000..bcab93f
--- /dev/null
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/util/SQLStatementUtilsTest.java
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.sql.parser.sql.common.util;
+
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.SetAssignmentSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.OnDuplicateKeyColumnsSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WithSegment;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLInsertStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dml.PostgreSQLInsertStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerInsertStatement;
+import org.junit.Test;
+
+import java.util.Collections;
+import java.util.Optional;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class SQLStatementUtilsTest {
+
+ @Test
+ public void assertGetOnDuplicateKeyColumnsSegmentWithOnDuplicateKeyColumnsSegmentForMySQL() {
+ MySQLInsertStatement insertStatement = new MySQLInsertStatement();
+ insertStatement.setOnDuplicateKeyColumns(new OnDuplicateKeyColumnsSegment(0, 0, Collections.emptyList()));
+ Optional<OnDuplicateKeyColumnsSegment> onDuplicateKeyColumnsSegment = SQLStatementUtils.getOnDuplicateKeyColumnsSegment(insertStatement);
+ assertTrue(onDuplicateKeyColumnsSegment.isPresent());
+ }
+
+ @Test
+ public void assertGetOnDuplicateKeyColumnsSegmentWithoutOnDuplicateKeyColumnsSegmentForMySQL() {
+ MySQLInsertStatement insertStatement = new MySQLInsertStatement();
+ Optional<OnDuplicateKeyColumnsSegment> onDuplicateKeyColumnsSegment = SQLStatementUtils.getOnDuplicateKeyColumnsSegment(insertStatement);
+ assertFalse(onDuplicateKeyColumnsSegment.isPresent());
+ }
+
+ @Test
+ public void assertGetSetAssignmentSegmentWithSetAssignmentSegmentForMySQL() {
+ MySQLInsertStatement insertStatement = new MySQLInsertStatement();
+ insertStatement.setSetAssignment(new SetAssignmentSegment(0, 0, Collections.emptyList()));
+ Optional<SetAssignmentSegment> setAssignmentSegment = SQLStatementUtils.getSetAssignmentSegment(insertStatement);
+ assertTrue(setAssignmentSegment.isPresent());
+ }
+
+ @Test
+ public void assertGetSetAssignmentSegmentWithoutSetAssignmentSegmentForMySQL() {
+ MySQLInsertStatement insertStatement = new MySQLInsertStatement();
+ Optional<SetAssignmentSegment> setAssignmentSegment = SQLStatementUtils.getSetAssignmentSegment(insertStatement);
+ assertFalse(setAssignmentSegment.isPresent());
+ }
+
+ @Test
+ public void assertGetWithSegmentWithWithSegmentForPostgreSQL() {
+ PostgreSQLInsertStatement insertStatement = new PostgreSQLInsertStatement();
+ insertStatement.setWithSegment(new WithSegment(0, 0, Collections.emptyList()));
+ Optional<WithSegment> withSegment = SQLStatementUtils.getWithSegment(insertStatement);
+ assertTrue(withSegment.isPresent());
+ }
+
+ @Test
+ public void assertGetWithSegmentWithoutWithSegmentForPostgreSQL() {
+ PostgreSQLInsertStatement insertStatement = new PostgreSQLInsertStatement();
+ Optional<WithSegment> withSegment = SQLStatementUtils.getWithSegment(insertStatement);
+ assertFalse(withSegment.isPresent());
+ }
+
+ @Test
+ public void assertGetWithSegmentWithWithSegmentForSQLServer() {
+ SQLServerInsertStatement insertStatement = new SQLServerInsertStatement();
+ insertStatement.setWithSegment(new WithSegment(0, 0, Collections.emptyList()));
+ Optional<WithSegment> withSegment = SQLStatementUtils.getWithSegment(insertStatement);
+ assertTrue(withSegment.isPresent());
+ }
+
+ @Test
+ public void assertGetWithSegmentWithoutWithSegmentForSQLServer() {
+ SQLServerInsertStatement insertStatement = new SQLServerInsertStatement();
+ Optional<WithSegment> withSegment = SQLStatementUtils.getWithSegment(insertStatement);
+ assertFalse(withSegment.isPresent());
+ }
+}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/mysql/dml/MySQLInsertStatementTest.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/mysql/dml/MySQLInsertStatementTest.java
index df2eeea..02baa55 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/mysql/dml/MySQLInsertStatementTest.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/mysql/dml/MySQLInsertStatementTest.java
@@ -52,7 +52,7 @@ public final class MySQLInsertStatementTest {
@Test
public void assertNotUseDefaultColumnsWithSetAssignment() {
- InsertStatement insertStatement = new MySQLInsertStatement();
+ MySQLInsertStatement insertStatement = new MySQLInsertStatement();
insertStatement.setSetAssignment(new SetAssignmentSegment(0, 0, Collections.emptyList()));
insertStatement.setInsertColumns(new InsertColumnsSegment(0, 0, Collections.emptyList()));
assertFalse(insertStatement.useDefaultColumns());
@@ -69,7 +69,7 @@ public final class MySQLInsertStatementTest {
@Test
public void assertGetColumnNamesForSetAssignment() {
- InsertStatement insertStatement = new MySQLInsertStatement();
+ MySQLInsertStatement insertStatement = new MySQLInsertStatement();
insertStatement.setSetAssignment(
new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), new LiteralExpressionSegment(0, 0, 1)))));
assertThat(insertStatement.getColumnNames().size(), is(1));
@@ -86,7 +86,7 @@ public final class MySQLInsertStatementTest {
@Test
public void assertGetValueListCountWithSetAssignment() {
- InsertStatement insertStatement = new MySQLInsertStatement();
+ MySQLInsertStatement insertStatement = new MySQLInsertStatement();
insertStatement.setSetAssignment(
new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), new LiteralExpressionSegment(0, 0, 1)))));
assertThat(insertStatement.getValueListCount(), is(1));
@@ -101,7 +101,7 @@ public final class MySQLInsertStatementTest {
@Test
public void assertGetValueCountForPerGroupWithSetAssignment() {
- InsertStatement insertStatement = new MySQLInsertStatement();
+ MySQLInsertStatement insertStatement = new MySQLInsertStatement();
insertStatement.setSetAssignment(
new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), new LiteralExpressionSegment(0, 0, 1)))));
assertThat(insertStatement.getValueCountForPerGroup(), is(1));
@@ -124,7 +124,7 @@ public final class MySQLInsertStatementTest {
@Test
public void assertGetAllValueExpressionsWithSetAssignment() {
- InsertStatement insertStatement = new MySQLInsertStatement();
+ MySQLInsertStatement insertStatement = new MySQLInsertStatement();
ExpressionSegment valueSegment = new LiteralExpressionSegment(0, 0, 1);
insertStatement.setSetAssignment(new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), valueSegment))));
assertThat(insertStatement.getAllValueExpressions().size(), is(1));
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/oracle/dml/OracleInsertStatementTest.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/oracle/dml/OracleInsertStatementTest.java
index f3cf580..1b81962 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/oracle/dml/OracleInsertStatementTest.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/oracle/dml/OracleInsertStatementTest.java
@@ -17,9 +17,7 @@
package org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.dml;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.InsertValuesSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.SetAssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.InsertColumnsSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
@@ -52,14 +50,6 @@ public final class OracleInsertStatementTest {
}
@Test
- public void assertNotUseDefaultColumnsWithSetAssignment() {
- InsertStatement insertStatement = new OracleInsertStatement();
- insertStatement.setSetAssignment(new SetAssignmentSegment(0, 0, Collections.emptyList()));
- insertStatement.setInsertColumns(new InsertColumnsSegment(0, 0, Collections.emptyList()));
- assertFalse(insertStatement.useDefaultColumns());
- }
-
- @Test
public void assertGetColumnNamesForInsertColumns() {
InsertStatement insertStatement = new OracleInsertStatement();
InsertColumnsSegment insertColumnsSegment = new InsertColumnsSegment(0, 0, Collections.singletonList(new ColumnSegment(0, 0, new IdentifierValue("col"))));
@@ -69,15 +59,6 @@ public final class OracleInsertStatementTest {
}
@Test
- public void assertGetColumnNamesForSetAssignment() {
- InsertStatement insertStatement = new OracleInsertStatement();
- insertStatement.setSetAssignment(
- new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), new LiteralExpressionSegment(0, 0, 1)))));
- assertThat(insertStatement.getColumnNames().size(), is(1));
- assertThat(insertStatement.getColumnNames().iterator().next(), is("col"));
- }
-
- @Test
public void assertGetValueListCountWithValues() {
InsertStatement insertStatement = new OracleInsertStatement();
insertStatement.getValues().add(new InsertValuesSegment(0, 0, Collections.singletonList(new LiteralExpressionSegment(0, 0, 1))));
@@ -86,14 +67,6 @@ public final class OracleInsertStatementTest {
}
@Test
- public void assertGetValueListCountWithSetAssignment() {
- InsertStatement insertStatement = new OracleInsertStatement();
- insertStatement.setSetAssignment(
- new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), new LiteralExpressionSegment(0, 0, 1)))));
- assertThat(insertStatement.getValueListCount(), is(1));
- }
-
- @Test
public void assertGetValueCountForPerGroupWithValues() {
InsertStatement insertStatement = new OracleInsertStatement();
insertStatement.getValues().add(new InsertValuesSegment(0, 0, Collections.singletonList(new LiteralExpressionSegment(0, 0, 1))));
@@ -101,14 +74,6 @@ public final class OracleInsertStatementTest {
}
@Test
- public void assertGetValueCountForPerGroupWithSetAssignment() {
- InsertStatement insertStatement = new OracleInsertStatement();
- insertStatement.setSetAssignment(
- new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), new LiteralExpressionSegment(0, 0, 1)))));
- assertThat(insertStatement.getValueCountForPerGroup(), is(1));
- }
-
- @Test
public void assertGetValueCountForPerGroupWithoutValuesAndSetAssignment() {
assertThat(new MySQLInsertStatement().getValueCountForPerGroup(), is(0));
}
@@ -122,14 +87,4 @@ public final class OracleInsertStatementTest {
assertThat(insertStatement.getAllValueExpressions().iterator().next().size(), is(1));
assertThat(insertStatement.getAllValueExpressions().iterator().next().iterator().next(), is(valueSegment));
}
-
- @Test
- public void assertGetAllValueExpressionsWithSetAssignment() {
- InsertStatement insertStatement = new OracleInsertStatement();
- ExpressionSegment valueSegment = new LiteralExpressionSegment(0, 0, 1);
- insertStatement.setSetAssignment(new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), valueSegment))));
- assertThat(insertStatement.getAllValueExpressions().size(), is(1));
- assertThat(insertStatement.getAllValueExpressions().iterator().next().size(), is(1));
- assertThat(insertStatement.getAllValueExpressions().iterator().next().iterator().next(), is(valueSegment));
- }
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/dml/PostgreSQLInsertStatementTest.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/dml/PostgreSQLInsertStatementTest.java
index d694bc1..3bf9074 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/dml/PostgreSQLInsertStatementTest.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/dml/PostgreSQLInsertStatementTest.java
@@ -17,9 +17,7 @@
package org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dml;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.InsertValuesSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.SetAssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.InsertColumnsSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
@@ -52,14 +50,6 @@ public final class PostgreSQLInsertStatementTest {
}
@Test
- public void assertNotUseDefaultColumnsWithSetAssignment() {
- InsertStatement insertStatement = new PostgreSQLInsertStatement();
- insertStatement.setSetAssignment(new SetAssignmentSegment(0, 0, Collections.emptyList()));
- insertStatement.setInsertColumns(new InsertColumnsSegment(0, 0, Collections.emptyList()));
- assertFalse(insertStatement.useDefaultColumns());
- }
-
- @Test
public void assertGetColumnNamesForInsertColumns() {
InsertStatement insertStatement = new PostgreSQLInsertStatement();
InsertColumnsSegment insertColumnsSegment = new InsertColumnsSegment(0, 0, Collections.singletonList(new ColumnSegment(0, 0, new IdentifierValue("col"))));
@@ -69,15 +59,6 @@ public final class PostgreSQLInsertStatementTest {
}
@Test
- public void assertGetColumnNamesForSetAssignment() {
- InsertStatement insertStatement = new PostgreSQLInsertStatement();
- insertStatement.setSetAssignment(
- new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), new LiteralExpressionSegment(0, 0, 1)))));
- assertThat(insertStatement.getColumnNames().size(), is(1));
- assertThat(insertStatement.getColumnNames().iterator().next(), is("col"));
- }
-
- @Test
public void assertGetValueListCountWithValues() {
InsertStatement insertStatement = new PostgreSQLInsertStatement();
insertStatement.getValues().add(new InsertValuesSegment(0, 0, Collections.singletonList(new LiteralExpressionSegment(0, 0, 1))));
@@ -86,14 +67,6 @@ public final class PostgreSQLInsertStatementTest {
}
@Test
- public void assertGetValueListCountWithSetAssignment() {
- InsertStatement insertStatement = new PostgreSQLInsertStatement();
- insertStatement.setSetAssignment(
- new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), new LiteralExpressionSegment(0, 0, 1)))));
- assertThat(insertStatement.getValueListCount(), is(1));
- }
-
- @Test
public void assertGetValueCountForPerGroupWithValues() {
InsertStatement insertStatement = new PostgreSQLInsertStatement();
insertStatement.getValues().add(new InsertValuesSegment(0, 0, Collections.singletonList(new LiteralExpressionSegment(0, 0, 1))));
@@ -101,14 +74,6 @@ public final class PostgreSQLInsertStatementTest {
}
@Test
- public void assertGetValueCountForPerGroupWithSetAssignment() {
- InsertStatement insertStatement = new PostgreSQLInsertStatement();
- insertStatement.setSetAssignment(
- new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), new LiteralExpressionSegment(0, 0, 1)))));
- assertThat(insertStatement.getValueCountForPerGroup(), is(1));
- }
-
- @Test
public void assertGetValueCountForPerGroupWithoutValuesAndSetAssignment() {
assertThat(new MySQLInsertStatement().getValueCountForPerGroup(), is(0));
}
@@ -122,14 +87,4 @@ public final class PostgreSQLInsertStatementTest {
assertThat(insertStatement.getAllValueExpressions().iterator().next().size(), is(1));
assertThat(insertStatement.getAllValueExpressions().iterator().next().iterator().next(), is(valueSegment));
}
-
- @Test
- public void assertGetAllValueExpressionsWithSetAssignment() {
- InsertStatement insertStatement = new PostgreSQLInsertStatement();
- ExpressionSegment valueSegment = new LiteralExpressionSegment(0, 0, 1);
- insertStatement.setSetAssignment(new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), valueSegment))));
- assertThat(insertStatement.getAllValueExpressions().size(), is(1));
- assertThat(insertStatement.getAllValueExpressions().iterator().next().size(), is(1));
- assertThat(insertStatement.getAllValueExpressions().iterator().next().iterator().next(), is(valueSegment));
- }
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sql92/dml/SQL92InsertStatementTest.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sql92/dml/SQL92InsertStatementTest.java
index 9385172..58f8a70 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sql92/dml/SQL92InsertStatementTest.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sql92/dml/SQL92InsertStatementTest.java
@@ -17,9 +17,7 @@
package org.apache.shardingsphere.sql.parser.sql.dialect.statement.sql92.dml;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.InsertValuesSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.SetAssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.InsertColumnsSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
@@ -52,14 +50,6 @@ public final class SQL92InsertStatementTest {
}
@Test
- public void assertNotUseDefaultColumnsWithSetAssignment() {
- InsertStatement insertStatement = new SQL92InsertStatement();
- insertStatement.setSetAssignment(new SetAssignmentSegment(0, 0, Collections.emptyList()));
- insertStatement.setInsertColumns(new InsertColumnsSegment(0, 0, Collections.emptyList()));
- assertFalse(insertStatement.useDefaultColumns());
- }
-
- @Test
public void assertGetColumnNamesForInsertColumns() {
InsertStatement insertStatement = new SQL92InsertStatement();
InsertColumnsSegment insertColumnsSegment = new InsertColumnsSegment(0, 0, Collections.singletonList(new ColumnSegment(0, 0, new IdentifierValue("col"))));
@@ -69,15 +59,6 @@ public final class SQL92InsertStatementTest {
}
@Test
- public void assertGetColumnNamesForSetAssignment() {
- InsertStatement insertStatement = new SQL92InsertStatement();
- insertStatement.setSetAssignment(
- new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), new LiteralExpressionSegment(0, 0, 1)))));
- assertThat(insertStatement.getColumnNames().size(), is(1));
- assertThat(insertStatement.getColumnNames().iterator().next(), is("col"));
- }
-
- @Test
public void assertGetValueListCountWithValues() {
InsertStatement insertStatement = new SQL92InsertStatement();
insertStatement.getValues().add(new InsertValuesSegment(0, 0, Collections.singletonList(new LiteralExpressionSegment(0, 0, 1))));
@@ -86,14 +67,6 @@ public final class SQL92InsertStatementTest {
}
@Test
- public void assertGetValueListCountWithSetAssignment() {
- InsertStatement insertStatement = new SQL92InsertStatement();
- insertStatement.setSetAssignment(
- new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), new LiteralExpressionSegment(0, 0, 1)))));
- assertThat(insertStatement.getValueListCount(), is(1));
- }
-
- @Test
public void assertGetValueCountForPerGroupWithValues() {
InsertStatement insertStatement = new SQL92InsertStatement();
insertStatement.getValues().add(new InsertValuesSegment(0, 0, Collections.singletonList(new LiteralExpressionSegment(0, 0, 1))));
@@ -101,14 +74,6 @@ public final class SQL92InsertStatementTest {
}
@Test
- public void assertGetValueCountForPerGroupWithSetAssignment() {
- InsertStatement insertStatement = new SQL92InsertStatement();
- insertStatement.setSetAssignment(
- new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), new LiteralExpressionSegment(0, 0, 1)))));
- assertThat(insertStatement.getValueCountForPerGroup(), is(1));
- }
-
- @Test
public void assertGetValueCountForPerGroupWithoutValuesAndSetAssignment() {
assertThat(new MySQLInsertStatement().getValueCountForPerGroup(), is(0));
}
@@ -122,14 +87,4 @@ public final class SQL92InsertStatementTest {
assertThat(insertStatement.getAllValueExpressions().iterator().next().size(), is(1));
assertThat(insertStatement.getAllValueExpressions().iterator().next().iterator().next(), is(valueSegment));
}
-
- @Test
- public void assertGetAllValueExpressionsWithSetAssignment() {
- InsertStatement insertStatement = new SQL92InsertStatement();
- ExpressionSegment valueSegment = new LiteralExpressionSegment(0, 0, 1);
- insertStatement.setSetAssignment(new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), valueSegment))));
- assertThat(insertStatement.getAllValueExpressions().size(), is(1));
- assertThat(insertStatement.getAllValueExpressions().iterator().next().size(), is(1));
- assertThat(insertStatement.getAllValueExpressions().iterator().next().iterator().next(), is(valueSegment));
- }
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sqlserver/dml/SQLServerInsertStatementTest.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sqlserver/dml/SQLServerInsertStatementTest.java
index aa65e01..ce695af 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sqlserver/dml/SQLServerInsertStatementTest.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sqlserver/dml/SQLServerInsertStatementTest.java
@@ -17,9 +17,7 @@
package org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.InsertValuesSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.SetAssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.InsertColumnsSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
@@ -52,14 +50,6 @@ public final class SQLServerInsertStatementTest {
}
@Test
- public void assertNotUseDefaultColumnsWithSetAssignment() {
- InsertStatement insertStatement = new SQLServerInsertStatement();
- insertStatement.setSetAssignment(new SetAssignmentSegment(0, 0, Collections.emptyList()));
- insertStatement.setInsertColumns(new InsertColumnsSegment(0, 0, Collections.emptyList()));
- assertFalse(insertStatement.useDefaultColumns());
- }
-
- @Test
public void assertGetColumnNamesForInsertColumns() {
InsertStatement insertStatement = new SQLServerInsertStatement();
InsertColumnsSegment insertColumnsSegment = new InsertColumnsSegment(0, 0, Collections.singletonList(new ColumnSegment(0, 0, new IdentifierValue("col"))));
@@ -69,15 +59,6 @@ public final class SQLServerInsertStatementTest {
}
@Test
- public void assertGetColumnNamesForSetAssignment() {
- InsertStatement insertStatement = new SQLServerInsertStatement();
- insertStatement.setSetAssignment(
- new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), new LiteralExpressionSegment(0, 0, 1)))));
- assertThat(insertStatement.getColumnNames().size(), is(1));
- assertThat(insertStatement.getColumnNames().iterator().next(), is("col"));
- }
-
- @Test
public void assertGetValueListCountWithValues() {
InsertStatement insertStatement = new SQLServerInsertStatement();
insertStatement.getValues().add(new InsertValuesSegment(0, 0, Collections.singletonList(new LiteralExpressionSegment(0, 0, 1))));
@@ -86,14 +67,6 @@ public final class SQLServerInsertStatementTest {
}
@Test
- public void assertGetValueListCountWithSetAssignment() {
- InsertStatement insertStatement = new SQLServerInsertStatement();
- insertStatement.setSetAssignment(
- new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), new LiteralExpressionSegment(0, 0, 1)))));
- assertThat(insertStatement.getValueListCount(), is(1));
- }
-
- @Test
public void assertGetValueCountForPerGroupWithValues() {
InsertStatement insertStatement = new SQLServerInsertStatement();
insertStatement.getValues().add(new InsertValuesSegment(0, 0, Collections.singletonList(new LiteralExpressionSegment(0, 0, 1))));
@@ -101,14 +74,6 @@ public final class SQLServerInsertStatementTest {
}
@Test
- public void assertGetValueCountForPerGroupWithSetAssignment() {
- InsertStatement insertStatement = new SQLServerInsertStatement();
- insertStatement.setSetAssignment(
- new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), new LiteralExpressionSegment(0, 0, 1)))));
- assertThat(insertStatement.getValueCountForPerGroup(), is(1));
- }
-
- @Test
public void assertGetValueCountForPerGroupWithoutValuesAndSetAssignment() {
assertThat(new MySQLInsertStatement().getValueCountForPerGroup(), is(0));
}
@@ -122,14 +87,4 @@ public final class SQLServerInsertStatementTest {
assertThat(insertStatement.getAllValueExpressions().iterator().next().size(), is(1));
assertThat(insertStatement.getAllValueExpressions().iterator().next().iterator().next(), is(valueSegment));
}
-
- @Test
- public void assertGetAllValueExpressionsWithSetAssignment() {
- InsertStatement insertStatement = new SQLServerInsertStatement();
- ExpressionSegment valueSegment = new LiteralExpressionSegment(0, 0, 1);
- insertStatement.setSetAssignment(new SetAssignmentSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, new IdentifierValue("col")), valueSegment))));
- assertThat(insertStatement.getAllValueExpressions().size(), is(1));
- assertThat(insertStatement.getAllValueExpressions().iterator().next().size(), is(1));
- assertThat(insertStatement.getAllValueExpressions().iterator().next().iterator().next(), is(valueSegment));
- }
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/dml/impl/InsertStatementAssert.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/dml/impl/InsertStatementAssert.java
index c8001f3..9d99a18 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/dml/impl/InsertStatementAssert.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/dml/impl/InsertStatementAssert.java
@@ -19,6 +19,11 @@ package org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statemen
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.SetAssignmentSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.OnDuplicateKeyColumnsSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WithSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.util.SQLStatementUtils;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.insert.InsertColumnsClauseAssert;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.insert.InsertValuesClauseAssert;
@@ -27,7 +32,8 @@ import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.s
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.table.TableAssert;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.with.WithClauseAssert;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.dml.InsertStatementTestCase;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
+
+import java.util.Optional;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -78,14 +84,15 @@ public final class InsertStatementAssert {
}
private static void assertSetClause(final SQLCaseAssertContext assertContext, final InsertStatement actual, final InsertStatementTestCase expected) {
+ Optional<SetAssignmentSegment> setAssignmentSegment = SQLStatementUtils.getSetAssignmentSegment(actual);
if (null != expected.getSetClause()) {
- assertTrue(assertContext.getText("Actual set assignment segment should exist."), actual.getSetAssignment().isPresent());
- SetClauseAssert.assertIs(assertContext, actual.getSetAssignment().get(), expected.getSetClause());
+ assertTrue(assertContext.getText("Actual set assignment segment should exist."), setAssignmentSegment.isPresent());
+ SetClauseAssert.assertIs(assertContext, setAssignmentSegment.get(), expected.getSetClause());
} else {
- assertFalse(assertContext.getText("Actual set assignment segment should not exist."), actual.getSetAssignment().isPresent());
+ assertFalse(assertContext.getText("Actual set assignment segment should not exist."), setAssignmentSegment.isPresent());
}
}
-
+
private static void assertInsertSelectClause(final SQLCaseAssertContext assertContext, final InsertStatement actual, final InsertStatementTestCase expected) {
if (null != expected.getSelectTestCase()) {
assertTrue(assertContext.getText("Actual insert select segment should exist."), actual.getInsertSelect().isPresent());
@@ -96,20 +103,22 @@ public final class InsertStatementAssert {
}
private static void assertOnDuplicateKeyColumns(final SQLCaseAssertContext assertContext, final InsertStatement actual, final InsertStatementTestCase expected) {
+ Optional<OnDuplicateKeyColumnsSegment> onDuplicateKeyColumnsSegment = SQLStatementUtils.getOnDuplicateKeyColumnsSegment(actual);
if (null != expected.getOnDuplicateKeyColumns()) {
- assertTrue(assertContext.getText("Actual on duplicate key columns segment should exist."), actual.getOnDuplicateKeyColumns().isPresent());
- OnDuplicateKeyColumnsAssert.assertIs(assertContext, actual.getOnDuplicateKeyColumns().get(), expected.getOnDuplicateKeyColumns());
+ assertTrue(assertContext.getText("Actual on duplicate key columns segment should exist."), onDuplicateKeyColumnsSegment.isPresent());
+ OnDuplicateKeyColumnsAssert.assertIs(assertContext, onDuplicateKeyColumnsSegment.get(), expected.getOnDuplicateKeyColumns());
} else {
- assertFalse(assertContext.getText("Actual on duplicate key columns segment should not exist."), actual.getOnDuplicateKeyColumns().isPresent());
+ assertFalse(assertContext.getText("Actual on duplicate key columns segment should not exist."), onDuplicateKeyColumnsSegment.isPresent());
}
}
-
+
private static void assertWithClause(final SQLCaseAssertContext assertContext, final InsertStatement actual, final InsertStatementTestCase expected) {
+ Optional<WithSegment> withSegment = SQLStatementUtils.getWithSegment(actual);
if (null != expected.getWithClause()) {
- assertTrue(assertContext.getText("Actual with segment should exist."), actual.getWithSegment().isPresent());
- WithClauseAssert.assertIs(assertContext, actual.getWithSegment().get(), expected.getWithClause());
+ assertTrue(assertContext.getText("Actual with segment should exist."), withSegment.isPresent());
+ WithClauseAssert.assertIs(assertContext, withSegment.get(), expected.getWithClause());
} else {
- assertFalse(assertContext.getText("Actual with segment should not exist."), actual.getWithSegment().isPresent());
+ assertFalse(assertContext.getText("Actual with segment should not exist."), withSegment.isPresent());
}
}
}