Skip to content

Commit 33a0b94

Browse files
committed
Add specialized visitors for the different statements
1 parent 0a9a366 commit 33a0b94

File tree

8 files changed

+202
-30
lines changed

8 files changed

+202
-30
lines changed

src/main/java/org/mybatis/dynamic/sql/insert/render/GeneralInsertValuePhraseVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020

2121
import org.mybatis.dynamic.sql.SqlColumn;
2222
import org.mybatis.dynamic.sql.render.RenderingStrategy;
23-
import org.mybatis.dynamic.sql.util.ColumnMappingVisitor;
2423
import org.mybatis.dynamic.sql.util.ConstantMapping;
24+
import org.mybatis.dynamic.sql.util.GeneralInsertMappingVisitor;
2525
import org.mybatis.dynamic.sql.util.NullMapping;
2626
import org.mybatis.dynamic.sql.util.StringConstantMapping;
2727
import org.mybatis.dynamic.sql.util.ValueMapping;
2828

29-
public class GeneralInsertValuePhraseVisitor implements ColumnMappingVisitor<FieldAndValueAndParameters> {
29+
public class GeneralInsertValuePhraseVisitor implements GeneralInsertMappingVisitor<FieldAndValueAndParameters> {
3030

3131
protected RenderingStrategy renderingStrategy;
3232
private AtomicInteger sequence = new AtomicInteger(1);

src/main/java/org/mybatis/dynamic/sql/insert/render/ValuePhraseVisitor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,13 +19,13 @@
1919

2020
import org.mybatis.dynamic.sql.SqlColumn;
2121
import org.mybatis.dynamic.sql.render.RenderingStrategy;
22-
import org.mybatis.dynamic.sql.util.ColumnMappingVisitor;
2322
import org.mybatis.dynamic.sql.util.ConstantMapping;
23+
import org.mybatis.dynamic.sql.util.InsertMappingVisitor;
2424
import org.mybatis.dynamic.sql.util.NullMapping;
2525
import org.mybatis.dynamic.sql.util.PropertyMapping;
2626
import org.mybatis.dynamic.sql.util.StringConstantMapping;
2727

28-
public class ValuePhraseVisitor implements ColumnMappingVisitor<FieldAndValue> {
28+
public class ValuePhraseVisitor implements InsertMappingVisitor<FieldAndValue> {
2929

3030
protected RenderingStrategy renderingStrategy;
3131

src/main/java/org/mybatis/dynamic/sql/update/render/SetPhraseVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
2525
import org.mybatis.dynamic.sql.select.render.SelectRenderer;
2626
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
27-
import org.mybatis.dynamic.sql.util.ColumnMappingVisitor;
2827
import org.mybatis.dynamic.sql.util.ColumnToColumnMapping;
2928
import org.mybatis.dynamic.sql.util.ConstantMapping;
3029
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
3130
import org.mybatis.dynamic.sql.util.NullMapping;
3231
import org.mybatis.dynamic.sql.util.SelectMapping;
3332
import org.mybatis.dynamic.sql.util.StringConstantMapping;
33+
import org.mybatis.dynamic.sql.util.UpdateMappingVisitor;
3434
import org.mybatis.dynamic.sql.util.ValueMapping;
3535

36-
public class SetPhraseVisitor implements ColumnMappingVisitor<FragmentAndParameters> {
36+
public class SetPhraseVisitor implements UpdateMappingVisitor<FragmentAndParameters> {
3737

3838
private AtomicInteger sequence;
3939
private RenderingStrategy renderingStrategy;

src/main/java/org/mybatis/dynamic/sql/util/ColumnMappingVisitor.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,10 @@
2121
* may or may not be supported. For example, it makes no sense to map a column to another column in
2222
* an insert - so the ColumnToColumnMapping is only supported on update statements.
2323
*
24+
* Rather than implement this interface directly, we recommend implementing one of the derived
25+
* interfaces. The derived interfaces encapsulate the rules about which mappings are applicable to the
26+
* different types of statements.
27+
*
2428
* @author Jeff Butler
2529
*
2630
* @param <T> The type of object created by the visitor
@@ -32,19 +36,11 @@ public interface ColumnMappingVisitor<T> {
3236

3337
T visit(StringConstantMapping mapping);
3438

35-
default <R> T visit(ValueMapping<R> mapping) {
36-
throw new UnsupportedOperationException();
37-
}
39+
<R> T visit(ValueMapping<R> mapping);
3840

39-
default T visit(SelectMapping mapping) {
40-
throw new UnsupportedOperationException();
41-
}
41+
T visit(SelectMapping mapping);
4242

43-
default T visit(PropertyMapping mapping) {
44-
throw new UnsupportedOperationException();
45-
}
43+
T visit(PropertyMapping mapping);
4644

47-
default T visit(ColumnToColumnMapping columnMapping) {
48-
throw new UnsupportedOperationException();
49-
}
45+
T visit(ColumnToColumnMapping columnMapping);
5046
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2016-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.util;
17+
18+
public interface GeneralInsertMappingVisitor<T> extends ColumnMappingVisitor<T> {
19+
@Override
20+
default T visit(SelectMapping mapping) {
21+
throw new UnsupportedOperationException();
22+
}
23+
24+
@Override
25+
default T visit(PropertyMapping mapping) {
26+
throw new UnsupportedOperationException();
27+
}
28+
29+
@Override
30+
default T visit(ColumnToColumnMapping columnMapping) {
31+
throw new UnsupportedOperationException();
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2016-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.util;
17+
18+
public interface InsertMappingVisitor<T> extends ColumnMappingVisitor<T> {
19+
@Override
20+
default <R> T visit(ValueMapping<R> mapping) {
21+
throw new UnsupportedOperationException();
22+
}
23+
24+
@Override
25+
default T visit(SelectMapping mapping) {
26+
throw new UnsupportedOperationException();
27+
}
28+
29+
@Override
30+
default T visit(ColumnToColumnMapping columnMapping) {
31+
throw new UnsupportedOperationException();
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright 2016-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.util;
17+
18+
public interface UpdateMappingVisitor<T> extends ColumnMappingVisitor<T> {
19+
@Override
20+
default T visit(PropertyMapping mapping) {
21+
throw new UnsupportedOperationException();
22+
}
23+
}

src/test/java/org/mybatis/dynamic/sql/util/ColumnMappingVisitorTest.java

Lines changed: 97 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,63 @@
2525
class ColumnMappingVisitorTest {
2626

2727
@Test
28-
void testThatUnimplementedMethod1ThrowExceptions() {
28+
void testThatGeneralInsertVisitorErrorsForColumnToColumnMapping() {
2929
TestTable table = new TestTable();
30-
TestVisitor tv = new TestVisitor();
30+
GeneralInsertVisitor tv = new GeneralInsertVisitor();
3131
ColumnToColumnMapping mapping = ColumnToColumnMapping.of(table.id, table.description);
3232

3333
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
3434
}
3535

3636
@Test
37-
void testThatUnimplementedMethod2ThrowExceptions() {
37+
void testThatGeneralInsertVisitorErrorsForSelectMapping() {
3838
TestTable table = new TestTable();
39-
TestVisitor tv = new TestVisitor();
40-
ValueMapping<Integer> mapping = ValueMapping.of(table.id, () -> 3);
39+
GeneralInsertVisitor tv = new GeneralInsertVisitor();
40+
SelectMapping mapping = SelectMapping.of(table.id, SqlBuilder.select(table.id).from(table));
4141

4242
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
4343
}
4444

4545
@Test
46-
void testThatUnimplementedMethod3ThrowExceptions() {
46+
void testThatGeneralInsertVisitorErrorsForPropertyMapping() {
4747
TestTable table = new TestTable();
48-
TestVisitor tv = new TestVisitor();
48+
GeneralInsertVisitor tv = new GeneralInsertVisitor();
49+
PropertyMapping mapping = PropertyMapping.of(table.id, "id");
50+
51+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
52+
}
53+
54+
@Test
55+
void testThatInsertVisitorErrorsForColumnToColumnMapping() {
56+
TestTable table = new TestTable();
57+
InsertVisitor tv = new InsertVisitor();
58+
ColumnToColumnMapping mapping = ColumnToColumnMapping.of(table.id, table.description);
59+
60+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
61+
}
62+
63+
@Test
64+
void testThatInsertVisitorErrorsForSelectMapping() {
65+
TestTable table = new TestTable();
66+
InsertVisitor tv = new InsertVisitor();
4967
SelectMapping mapping = SelectMapping.of(table.id, SqlBuilder.select(table.id).from(table));
5068

5169
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
5270
}
5371

5472
@Test
55-
void testThatUnimplementedMethod4ThrowExceptions() {
73+
void testThatInsertVisitorErrorsForValueMapping() {
74+
TestTable table = new TestTable();
75+
InsertVisitor tv = new InsertVisitor();
76+
ValueMapping<Integer> mapping = ValueMapping.of(table.id, () -> 3);
77+
78+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
79+
}
80+
81+
@Test
82+
void testThatUpdateVisitorErrorsForPropertyMapping() {
5683
TestTable table = new TestTable();
57-
TestVisitor tv = new TestVisitor();
84+
UpdateVisitor tv = new UpdateVisitor();
5885
PropertyMapping mapping = PropertyMapping.of(table.id, "id");
5986

6087
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
@@ -72,7 +99,51 @@ public TestTable() {
7299
}
73100
}
74101

75-
private static class TestVisitor implements ColumnMappingVisitor<String> {
102+
private static class GeneralInsertVisitor implements GeneralInsertMappingVisitor<String> {
103+
@Override
104+
public String visit(NullMapping mapping) {
105+
return "Null Mapping";
106+
}
107+
108+
@Override
109+
public String visit(ConstantMapping mapping) {
110+
return "Constant Mapping";
111+
}
112+
113+
@Override
114+
public String visit(StringConstantMapping mapping) {
115+
return "String Constant Mapping";
116+
}
117+
118+
@Override
119+
public <R> String visit(ValueMapping<R> mapping) {
120+
return "Value Mapping";
121+
}
122+
}
123+
124+
private static class InsertVisitor implements InsertMappingVisitor<String> {
125+
@Override
126+
public String visit(NullMapping mapping) {
127+
return "Null Mapping";
128+
}
129+
130+
@Override
131+
public String visit(ConstantMapping mapping) {
132+
return "Constant Mapping";
133+
}
134+
135+
@Override
136+
public String visit(StringConstantMapping mapping) {
137+
return "String Constant Mapping";
138+
}
139+
140+
@Override
141+
public String visit(PropertyMapping mapping) {
142+
return "Property Mapping";
143+
}
144+
}
145+
146+
private static class UpdateVisitor implements UpdateMappingVisitor<String> {
76147
@Override
77148
public String visit(NullMapping mapping) {
78149
return "Null Mapping";
@@ -87,5 +158,21 @@ public String visit(ConstantMapping mapping) {
87158
public String visit(StringConstantMapping mapping) {
88159
return "String Constant Mapping";
89160
}
161+
162+
@Override
163+
public <R> String visit(ValueMapping<R> mapping) {
164+
// TODO Auto-generated method stub
165+
return null;
166+
}
167+
168+
@Override
169+
public String visit(SelectMapping mapping) {
170+
return "Select Mapping";
171+
}
172+
173+
@Override
174+
public String visit(ColumnToColumnMapping columnMapping) {
175+
return "Column to Column Mapping";
176+
}
90177
}
91178
}

0 commit comments

Comments
 (0)