Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b76c667

Browse files
Tests
1 parent 8d82cfc commit b76c667

File tree

3 files changed

+172
-12
lines changed

3 files changed

+172
-12
lines changed

‎src/main/java/org/mybatis/dynamic/sql/SqlColumn.java‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@
6363
* </li>
6464
* </ol>
6565
*
66-
* <p>The test code for this library contains an example of a proper extension of this class.
66+
* <p>The test code for this library contains an example of a proper extension of this class. In most cases, the code
67+
* for the overriding methods can be simply copied from this class and then the return types can be changed to the
68+
* subclass's covariant type (this pre-supposes that you create a {@code copyBuilder} method that returns a new builder
69+
* populated with all current class attributes).
6770
*
6871
* @param <T> the Java type associated with the column
6972
*/
@@ -139,7 +142,11 @@ public Optional<String> javaProperty() {
139142
*/
140143
@Override
141144
public SqlColumn<T> descending() {
142-
return copyBuilder().withDescendingPhrase(" DESC").build(); //$NON-NLS-1$
145+
return setDescending(copyBuilder()).build();
146+
}
147+
148+
protected <B extends AbstractBuilder<T, ?>> B setDescending(B builder) {
149+
return cast(builder.withDescendingPhrase(" DESC")); //$NON-NLS-1$
143150
}
144151

145152
/**
@@ -152,7 +159,11 @@ public SqlColumn<T> descending() {
152159
*/
153160
@Override
154161
public SqlColumn<T> as(String alias) {
155-
return copyBuilder().withAlias(alias).build();
162+
return setAlias(copyBuilder(), alias).build();
163+
}
164+
165+
protected <B extends AbstractBuilder<T, ?>> B setAlias(B builder, String alias) {
166+
return cast(builder.withAlias(alias));
156167
}
157168

158169
/**
@@ -163,7 +174,11 @@ public SqlColumn<T> as(String alias) {
163174
* @return a new column that will be rendered with the specified table qualifier
164175
*/
165176
public SqlColumn<T> qualifiedWith(String tableQualifier) {
166-
return copyBuilder().withTableQualifier(tableQualifier).build();
177+
return setTableQualifier(copyBuilder(), tableQualifier).build();
178+
}
179+
180+
protected <B extends AbstractBuilder<T, ?>> B setTableQualifier(B builder, String tableQualifier) {
181+
return cast(builder.withTableQualifier(tableQualifier));
167182
}
168183

169184
/**
@@ -178,8 +193,11 @@ public SqlColumn<T> qualifiedWith(String tableQualifier) {
178193
* @return a new column aliased with a camel case version of the column name
179194
*/
180195
public SqlColumn<T> asCamelCase() {
181-
return copyBuilder()
182-
.withAlias("\"" + StringUtilities.toCamelCase(name) + "\"").build(); //$NON-NLS-1$ //$NON-NLS-2$
196+
return setCamelCaseAlias(copyBuilder()).build();
197+
}
198+
199+
protected <B extends AbstractBuilder<T, ?>> B setCamelCaseAlias(B builder) {
200+
return cast(builder.withAlias("\"" + StringUtilities.toCamelCase(name) + "\"")); //$NON-NLS-1$ //$NON-NLS-2$
183201
}
184202

185203
@Override
@@ -301,6 +319,11 @@ protected <S extends SqlColumn<?>> S cast(SqlColumn<?> column) {
301319
return (S) column;
302320
}
303321

322+
@SuppressWarnings("unchecked")
323+
protected <B extends AbstractBuilder<?, ?>> B cast(AbstractBuilder<?, ?> builder) {
324+
return (B) builder;
325+
}
326+
304327
/**
305328
* This method will add all current attributes to the specified builder. It is useful when creating
306329
* new class instances that only change one attribute - we set all current attributes, then
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright 2016-2025 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+
* https://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 examples.simple;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.mybatis.dynamic.sql.ParameterTypeConverter;
22+
import org.mybatis.dynamic.sql.SqlTable;
23+
import org.mybatis.dynamic.sql.render.RenderingStrategies;
24+
25+
class ExtendedColumnTest {
26+
27+
private final SqlTable table = SqlTable.of("foo");
28+
private final PrimaryKeyColumn<Integer> bar = new PrimaryKeyColumn.Builder<Integer>()
29+
.withName("first_name")
30+
.withTable(table)
31+
.isPrimaryKeyColumn(true)
32+
.build();
33+
private final ParameterTypeConverter<Integer, String> ptc = Object::toString;
34+
35+
@Test
36+
void testPropagatedDescending() {
37+
var baz = bar.descending();
38+
39+
assertThat(baz.isPrimaryKeyColumn()).isTrue();
40+
}
41+
42+
@Test
43+
void testPropagatedAlias() {
44+
var baz = bar.as("fred");
45+
46+
assertThat(baz.alias()).hasValue("fred");
47+
assertThat(baz.isPrimaryKeyColumn()).isTrue();
48+
}
49+
50+
@Test
51+
void testPropagatedQualifiedWith() {
52+
var baz = bar.qualifiedWith("fred");
53+
54+
assertThat(baz.isPrimaryKeyColumn()).isTrue();
55+
}
56+
57+
@Test
58+
void testPropagatedAsCamelCase() {
59+
var baz = bar.asCamelCase();
60+
61+
assertThat(baz.isPrimaryKeyColumn()).isTrue();
62+
}
63+
64+
@Test
65+
void testPropagatedWithTypeHandler() {
66+
var baz = bar.withTypeHandler("barney");
67+
68+
assertThat(baz.typeHandler()).hasValue("barney");
69+
assertThat(baz.isPrimaryKeyColumn()).isTrue();
70+
}
71+
72+
@Test
73+
void testPropagatedRenderingStrategy() {
74+
var baz = bar.withRenderingStrategy(RenderingStrategies.MYBATIS3);
75+
76+
assertThat(baz.renderingStrategy()).hasValue(RenderingStrategies.MYBATIS3);
77+
assertThat(baz.isPrimaryKeyColumn()).isTrue();
78+
}
79+
80+
@Test
81+
void testPropagatedParameterTypeConverter() {
82+
var baz = bar.withParameterTypeConverter(ptc);
83+
84+
assertThat(baz.convertParameterType(11)).isEqualTo("11");
85+
assertThat(baz.isPrimaryKeyColumn()).isTrue();
86+
}
87+
88+
@Test
89+
void testPropagatedJavaType() {
90+
var baz = bar.withJavaType(Integer.class);
91+
92+
assertThat(baz.javaType()).hasValue(Integer.class);
93+
assertThat(baz.isPrimaryKeyColumn()).isTrue();
94+
}
95+
96+
@Test
97+
void testPropagatedJavaProperty() {
98+
var baz = bar.withJavaProperty("id");
99+
100+
assertThat(baz.javaProperty()).hasValue("id");
101+
assertThat(baz.isPrimaryKeyColumn()).isTrue();
102+
}
103+
104+
@Test
105+
void testAll() {
106+
PrimaryKeyColumn<Integer> baz = bar.descending()
107+
.as("fred")
108+
.qualifiedWith("fred")
109+
.asCamelCase()
110+
.withTypeHandler("barney")
111+
.withRenderingStrategy(RenderingStrategies.MYBATIS3)
112+
.withParameterTypeConverter(ptc)
113+
.withJavaType(Integer.class)
114+
.withJavaProperty("id");
115+
116+
assertThat(baz.alias()).hasValue("\"firstName\"");
117+
assertThat(baz.typeHandler()).hasValue("barney");
118+
assertThat(baz.renderingStrategy()).hasValue(RenderingStrategies.MYBATIS3);
119+
assertThat(baz.convertParameterType(11)).isEqualTo("11");
120+
assertThat(baz.javaType()).hasValue(Integer.class);
121+
assertThat(baz.javaProperty()).hasValue("id");
122+
assertThat(baz.isPrimaryKeyColumn()).isTrue();
123+
}
124+
}

‎src/test/java/examples/simple/PrimaryKeyColumn.java‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1+
/*
2+
* Copyright 2016-2025 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+
* https://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+
*/
116
package examples.simple;
217

318
import org.mybatis.dynamic.sql.ParameterTypeConverter;
419
import org.mybatis.dynamic.sql.SqlColumn;
520
import org.mybatis.dynamic.sql.render.RenderingStrategy;
6-
import org.mybatis.dynamic.sql.util.StringUtilities;
721

822
public class PrimaryKeyColumn<T> extends SqlColumn<T> {
923
private final boolean isPrimaryKeyColumn;
@@ -19,23 +33,22 @@ public boolean isPrimaryKeyColumn() {
1933

2034
@Override
2135
public PrimaryKeyColumn<T> descending() {
22-
return copyBuilder().withDescendingPhrase(" DESC").build();//$NON-NLS-1$
36+
return setDescending(copyBuilder()).build();
2337
}
2438

2539
@Override
2640
public PrimaryKeyColumn<T> as(String alias) {
27-
return copyBuilder().withAlias(alias).build();
41+
return setAlias(copyBuilder(), alias).build();
2842
}
2943

3044
@Override
3145
public PrimaryKeyColumn<T> qualifiedWith(String tableQualifier) {
32-
return copyBuilder().withTableQualifier(tableQualifier).build();
46+
return setTableQualifier(copyBuilder(), tableQualifier).build();
3347
}
3448

3549
@Override
3650
public PrimaryKeyColumn<T> asCamelCase() {
37-
return copyBuilder()
38-
.withAlias("\"" + StringUtilities.toCamelCase(name) + "\"").build(); //$NON-NLS-1$ //$NON-NLS-2$
51+
return setCamelCaseAlias(copyBuilder()).build();
3952
}
4053

4154
@Override

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /