Skip to content

Commit 45e4492

Browse files
committed
select distinct support
1 parent d42b10a commit 45e4492

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.blobcity</groupId>
66
<artifactId>db-java-adapter</artifactId>
7-
<version>1.2.7-SNAPSHOT</version>
7+
<version>1.2.8</version>
88
<packaging>jar</packaging>
99

1010
<name>BlobCity DB Java Adapter</name>

src/main/java/com/blobcity/db/search/Query.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323
public class Query<T extends Db> implements ObjectJsonable, Sqlable {
2424

25+
private final boolean distinct;
2526
private final List<String> selectColumnNames;
2627
private final List<Class<T>> fromTables; //for backward compatibility upto 1.2.5
2728
private final List<String> fromTablesString;
@@ -44,6 +45,14 @@ private Query(final List<String> selectColumnNames) {
4445
this.selectColumnNames = selectColumnNames;
4546
this.fromTables = new ArrayList<Class<T>>();
4647
this.fromTablesString = new ArrayList<String>();
48+
this.distinct = false;
49+
}
50+
51+
private Query(final List<String> selectColumnNames, final boolean distinct) {
52+
this.selectColumnNames = selectColumnNames;
53+
this.fromTables = new ArrayList<Class<T>>();
54+
this.fromTablesString = new ArrayList<String>();
55+
this.distinct = true;
4756
}
4857

4958
/**
@@ -64,6 +73,10 @@ public static Query select() {
6473
public static Query select(final String... columnNames) {
6574
return new Query(columnNames != null && columnNames.length > 0 ? Arrays.asList(columnNames) : Collections.EMPTY_LIST);
6675
}
76+
77+
public static Query selectDistinct(final String... columnNames) {
78+
return new Query(columnNames != null && columnNames.length > 0 ? Arrays.asList(columnNames) : Collections.EMPTY_LIST, true);
79+
}
6780

6881
public static Query count() {
6982
return select("COUNT(*)");
@@ -218,7 +231,13 @@ public String asSql() {
218231
@Override
219232
public String asSql(final String ds) {
220233
final StringBuffer sb = new StringBuffer();
221-
sb.append("SELECT ").append(StringUtil.join(selectColumnNames, ", ", "*", "`"));
234+
235+
if(distinct == true) {
236+
sb.append("SELECT DISTINCT ").append(StringUtil.join(selectColumnNames, ", ", "*", "`"));
237+
} else {
238+
sb.append("SELECT ").append(StringUtil.join(selectColumnNames, ", ", "*", "`"));
239+
}
240+
222241

223242
if ((fromTables == null || fromTables.isEmpty()) && (fromTablesString == null || fromTablesString.isEmpty())) {
224243
throw new InternalAdapterException("No collection name set. Table name is a mandatory field queries.");

0 commit comments

Comments
 (0)