Skip to content

Commit

Permalink
select distinct support
Browse files Browse the repository at this point in the history
  • Loading branch information
sanketsarang committed Sep 7, 2018
1 parent d42b10a commit 45e4492
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.blobcity</groupId>
<artifactId>db-java-adapter</artifactId>
<version>1.2.7-SNAPSHOT</version>
<version>1.2.8</version>
<packaging>jar</packaging>

<name>BlobCity DB Java Adapter</name>
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/com/blobcity/db/search/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
public class Query<T extends Db> implements ObjectJsonable, Sqlable {

private final boolean distinct;
private final List<String> selectColumnNames;
private final List<Class<T>> fromTables; //for backward compatibility upto 1.2.5
private final List<String> fromTablesString;
Expand All @@ -44,6 +45,14 @@ private Query(final List<String> selectColumnNames) {
this.selectColumnNames = selectColumnNames;
this.fromTables = new ArrayList<Class<T>>();
this.fromTablesString = new ArrayList<String>();
this.distinct = false;
}

private Query(final List<String> selectColumnNames, final boolean distinct) {
this.selectColumnNames = selectColumnNames;
this.fromTables = new ArrayList<Class<T>>();
this.fromTablesString = new ArrayList<String>();
this.distinct = true;
}

/**
Expand All @@ -64,6 +73,10 @@ public static Query select() {
public static Query select(final String... columnNames) {
return new Query(columnNames != null && columnNames.length > 0 ? Arrays.asList(columnNames) : Collections.EMPTY_LIST);
}

public static Query selectDistinct(final String... columnNames) {
return new Query(columnNames != null && columnNames.length > 0 ? Arrays.asList(columnNames) : Collections.EMPTY_LIST, true);
}

public static Query count() {
return select("COUNT(*)");
Expand Down Expand Up @@ -218,7 +231,13 @@ public String asSql() {
@Override
public String asSql(final String ds) {
final StringBuffer sb = new StringBuffer();
sb.append("SELECT ").append(StringUtil.join(selectColumnNames, ", ", "*", "`"));

if(distinct == true) {
sb.append("SELECT DISTINCT ").append(StringUtil.join(selectColumnNames, ", ", "*", "`"));
} else {
sb.append("SELECT ").append(StringUtil.join(selectColumnNames, ", ", "*", "`"));
}


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

0 comments on commit 45e4492

Please sign in to comment.