Skip to content

Commit a43c946

Browse files
committed
Some abstraction for the count functions
1 parent ff80a4f commit a43c946

File tree

4 files changed

+58
-73
lines changed

4 files changed

+58
-73
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.select.aggregate;
17+
18+
import java.util.Optional;
19+
import org.mybatis.dynamic.sql.BindableColumn;
20+
21+
/**
22+
* Count functions are implemented differently than the other aggregates. This is primarily to preserve
23+
* backwards compatibility. Count functions are configured as BindableColumns of type Long
24+
* as it is assumed that the count functions always return a number.
25+
*
26+
* @param <T> the subtype of this class
27+
*/
28+
public abstract class AbstractCount<T extends AbstractCount<T>> implements BindableColumn<Long> {
29+
private final String alias;
30+
31+
protected AbstractCount(String alias) {
32+
this.alias = alias;
33+
}
34+
35+
@Override
36+
public Optional<String> alias() {
37+
return Optional.ofNullable(alias);
38+
}
39+
40+
@Override
41+
public T as(String alias) {
42+
return copyWithAlias(alias);
43+
}
44+
45+
protected abstract T copyWithAlias(String alias);
46+
}

src/main/java/org/mybatis/dynamic/sql/select/aggregate/Count.java

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,18 @@
1515
*/
1616
package org.mybatis.dynamic.sql.select.aggregate;
1717

18-
import java.sql.JDBCType;
1918
import java.util.Objects;
20-
import java.util.Optional;
2119

2220
import org.mybatis.dynamic.sql.BasicColumn;
23-
import org.mybatis.dynamic.sql.BindableColumn;
2421
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
2522

26-
public class Count implements BindableColumn<Long> {
23+
public class Count extends AbstractCount<Count> {
2724

2825
private final BasicColumn column;
29-
private final String alias;
30-
31-
private Count(BasicColumn column) {
32-
this.column = Objects.requireNonNull(column);
33-
alias = null;
34-
}
3526

3627
private Count(BasicColumn column, String alias) {
28+
super(alias);
3729
this.column = Objects.requireNonNull(column);
38-
this.alias = alias;
3930
}
4031

4132
@Override
@@ -44,21 +35,11 @@ public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
4435
}
4536

4637
@Override
47-
public Optional<String> alias() {
48-
return Optional.ofNullable(alias);
49-
}
50-
51-
@Override
52-
public Count as(String alias) {
38+
protected Count copyWithAlias(String alias) {
5339
return new Count(column, alias);
5440
}
5541

56-
@Override
57-
public Optional<JDBCType> jdbcType() {
58-
return Optional.of(JDBCType.BIGINT);
59-
}
60-
6142
public static Count of(BasicColumn column) {
62-
return new Count(column);
43+
return new Count(column, null);
6344
}
6445
}

src/main/java/org/mybatis/dynamic/sql/select/aggregate/CountAll.java

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,16 @@
1515
*/
1616
package org.mybatis.dynamic.sql.select.aggregate;
1717

18-
import java.sql.JDBCType;
19-
import java.util.Optional;
20-
21-
import org.mybatis.dynamic.sql.BindableColumn;
2218
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
2319

24-
/**
25-
* CountAll seems like the other aggregates, but it is special because there is no column.
26-
* Rather than dealing with a useless and confusing abstraction, we simply implement
27-
* BindableColumn directly.
28-
*
29-
* @author Jeff Butler
30-
*/
31-
public class CountAll implements BindableColumn<Long> {
32-
33-
private final String alias;
20+
public class CountAll extends AbstractCount<CountAll> {
3421

3522
public CountAll() {
36-
alias = null;
23+
super(null);
3724
}
3825

3926
private CountAll(String alias) {
40-
this.alias = alias;
27+
super(alias);
4128
}
4229

4330
@Override
@@ -46,17 +33,7 @@ public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
4633
}
4734

4835
@Override
49-
public Optional<String> alias() {
50-
return Optional.ofNullable(alias);
51-
}
52-
53-
@Override
54-
public CountAll as(String alias) {
36+
protected CountAll copyWithAlias(String alias) {
5537
return new CountAll(alias);
5638
}
57-
58-
@Override
59-
public Optional<JDBCType> jdbcType() {
60-
return Optional.of(JDBCType.BIGINT);
61-
}
6239
}

src/main/java/org/mybatis/dynamic/sql/select/aggregate/CountDistinct.java

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,18 @@
1515
*/
1616
package org.mybatis.dynamic.sql.select.aggregate;
1717

18-
import java.sql.JDBCType;
1918
import java.util.Objects;
20-
import java.util.Optional;
2119

2220
import org.mybatis.dynamic.sql.BasicColumn;
23-
import org.mybatis.dynamic.sql.BindableColumn;
2421
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
2522

26-
public class CountDistinct implements BindableColumn<Long> {
23+
public class CountDistinct extends AbstractCount<CountDistinct> {
2724

2825
private final BasicColumn column;
29-
private final String alias;
30-
31-
private CountDistinct(BasicColumn column) {
32-
this.column = Objects.requireNonNull(column);
33-
alias = null;
34-
}
3526

3627
private CountDistinct(BasicColumn column, String alias) {
28+
super(alias);
3729
this.column = Objects.requireNonNull(column);
38-
this.alias = alias;
3930
}
4031

4132
@Override
@@ -44,21 +35,11 @@ public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
4435
}
4536

4637
@Override
47-
public Optional<String> alias() {
48-
return Optional.ofNullable(alias);
49-
}
50-
51-
@Override
52-
public CountDistinct as(String alias) {
38+
public CountDistinct copyWithAlias(String alias) {
5339
return new CountDistinct(column, alias);
5440
}
5541

56-
@Override
57-
public Optional<JDBCType> jdbcType() {
58-
return Optional.of(JDBCType.BIGINT);
59-
}
60-
6142
public static CountDistinct of(BasicColumn column) {
62-
return new CountDistinct(column);
43+
return new CountDistinct(column, null);
6344
}
6445
}

0 commit comments

Comments
 (0)