diff --git a/pom.xml b/pom.xml index bff9a65..2aa1d2d 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.blobcity db-java-adapter - 1.2.7-SNAPSHOT + 1.2.8 jar BlobCity DB Java Adapter diff --git a/src/main/java/com/blobcity/db/search/Query.java b/src/main/java/com/blobcity/db/search/Query.java index bb652e2..9b0318f 100644 --- a/src/main/java/com/blobcity/db/search/Query.java +++ b/src/main/java/com/blobcity/db/search/Query.java @@ -22,6 +22,7 @@ */ public class Query implements ObjectJsonable, Sqlable { + private final boolean distinct; private final List selectColumnNames; private final List> fromTables; //for backward compatibility upto 1.2.5 private final List fromTablesString; @@ -44,6 +45,14 @@ private Query(final List selectColumnNames) { this.selectColumnNames = selectColumnNames; this.fromTables = new ArrayList>(); this.fromTablesString = new ArrayList(); + this.distinct = false; + } + + private Query(final List selectColumnNames, final boolean distinct) { + this.selectColumnNames = selectColumnNames; + this.fromTables = new ArrayList>(); + this.fromTablesString = new ArrayList(); + this.distinct = true; } /** @@ -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(*)"); @@ -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.");