Skip to content

Commit 0c606de

Browse files
committed
Example Length Function
1 parent 1239bce commit 0c606de

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/test/java/examples/animal/data/AnimalDataTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,26 @@ void testLikeUpperCase() {
805805
}
806806
}
807807

808+
@Test
809+
void testLength() {
810+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
811+
CommonSelectMapper mapper = sqlSession.getMapper(CommonSelectMapper.class);
812+
813+
SelectStatementProvider selectStatement = select(id, upper(animalName).as("animalname"), bodyWeight, brainWeight)
814+
.from(animalData)
815+
.where(Length.length(animalName), isGreaterThan(22))
816+
.orderBy(id)
817+
.build()
818+
.render(RenderingStrategies.MYBATIS3);
819+
820+
List<Map<String, Object>> animals = mapper.selectManyMappedRows(selectStatement);
821+
822+
assertThat(animals).hasSize(2);
823+
assertThat(animals.get(0)).containsEntry("ANIMALNAME", "LESSER SHORT-TAILED SHREW");
824+
assertThat(animals.get(1)).containsEntry("ANIMALNAME", "AFRICAN GIANT POUCHED RAT");
825+
}
826+
}
827+
808828
@Test
809829
void testNumericConstant() {
810830
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 examples.animal.data;
17+
18+
import org.mybatis.dynamic.sql.BindableColumn;
19+
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
20+
import org.mybatis.dynamic.sql.select.function.AbstractTypeConvertingFunction;
21+
22+
import java.sql.JDBCType;
23+
import java.util.Optional;
24+
25+
public class Length extends AbstractTypeConvertingFunction<Object, Integer, Length> {
26+
private Length(BindableColumn<Object> column) {
27+
super(column);
28+
}
29+
30+
@Override
31+
public Optional<JDBCType> jdbcType() {
32+
return Optional.of(JDBCType.INTEGER);
33+
}
34+
35+
@Override
36+
public Optional<String> typeHandler() {
37+
return Optional.empty();
38+
}
39+
40+
@Override
41+
public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
42+
return "length(" //$NON-NLS-1$
43+
+ column.renderWithTableAlias(tableAliasCalculator)
44+
+ ")"; //$NON-NLS-1$
45+
}
46+
47+
@Override
48+
protected Length copy() {
49+
return new Length(column);
50+
}
51+
52+
public static Length length(BindableColumn<?> column) {
53+
@SuppressWarnings("unchecked")
54+
BindableColumn<Object> c = (BindableColumn<Object>) column;
55+
return new Length(c);
56+
}
57+
}
58+

0 commit comments

Comments
 (0)