2222 */
2323public 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