Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: dynamic property group by with alias #112

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public void appendSelect(DbSqlContext ctx, boolean subQuery) {
ctx.appendParseSelect(parsedFormula, alias);
}

@Override
public void appendGroupBy(DbSqlContext ctx, boolean subQuery) {
ctx.appendParseSelect(parsedFormula, null);
}

@Override
public boolean isLobForPlatform() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ default boolean isAggregationManyToOne() {
*/
void appendSelect(DbSqlContext ctx, boolean subQuery);

/**
* Append to group by.
*/
default void appendGroupBy(DbSqlContext ctx, boolean subQuery) {
appendSelect(ctx, subQuery);
}

/**
* Append to the from clause.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public final void appendGroupBy(DbSqlContext ctx, boolean subQuery) {
}
for (STreeProperty property : properties) {
if (!property.isAggregation()) {
property.appendSelect(ctx, subQuery);
property.appendGroupBy(ctx, subQuery);
}
}
for (SqlTreeNode child : children) {
Expand Down
48 changes: 48 additions & 0 deletions ebean-test/src/test/java/io/ebean/xtest/base/DtoQuery2Test.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.ebean.xtest.base;

import io.ebean.DB;
import io.ebean.Query;
import io.ebean.xtest.BaseTestCase;
import io.ebean.DtoQuery;
import io.ebean.QueryIterator;
Expand All @@ -9,6 +11,7 @@
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tests.model.basic.Contact;
import org.tests.model.basic.Customer;
import org.tests.model.basic.ResetBasicData;

Expand Down Expand Up @@ -406,6 +409,22 @@ void dto3_findList_settersMatch() {
assertThat(robs).isNotEmpty();
}

@Test
void dto_test_formula_with_group_by() {
ResetBasicData.reset();

Query<Contact> query = server().find(Contact.class)
.select("concat(first_name, last_name) as custname, count(*) as cnt")
.orderBy("custname desc");

List<DCustFormula> ret = query.asDto(DCustFormula.class).findList();

log.info(query.getGeneratedSql());
log.info(ret.toString());
assertThat(ret.get(0).getCustname()).isEqualTo("TracyRed");
assertThat(ret.get(0).getCnt()).isEqualTo(1L);
}

public static class DCust {

final Integer id;
Expand Down Expand Up @@ -614,4 +633,33 @@ public DCustCamelCols2 setNameFORMe(String nameFORMe) {
return this;
}
}

public static class DCustFormula {

String custname;
Long cnt;

public DCustFormula() {}

public void setCustname(String custname) {
this.custname = custname;
}

public String getCustname() {
return custname;
}

public void setCnt(Long cnt) {
this.cnt = cnt;
}

public Long getCnt() {
return cnt;
}

@Override
public String toString() {
return getCustname() + ": " + getCnt();
}
}
}
Loading