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

This will get a dto containing the enum as the result of the select. #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 31 additions & 0 deletions src/main/java/com/iciql/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,37 @@ public void appendSQL(SQLStatement stat, Object alias, Object value) {
addParameter(stat, alias, value);
}

/**
* INTERNAL
*
* @param stat the statement
* @param alias the alias object (can be null)
* @param value the value
*/
public void appendSelectSQL(SQLStatement stat, Object alias, Object value) {
if (Function.count() == value) {
stat.appendSQL("COUNT(*)");
return;
}
if (RuntimeParameter.PARAMETER == value) {
stat.appendSQL("?");
addParameter(stat, alias, value);
return;
}
Token token = Db.getToken(value);
if (token != null) {
token.appendSQL(stat, this);
return;
}
SelectColumn<T> col = getColumnByReference(value);
if (col != null) {
col.appendSQL(stat);
return;
}
stat.appendSQL("?");
addParameter(stat, alias, value);
}

/**
* INTERNAL
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/iciql/TableDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -1185,10 +1185,10 @@ <Y, X> void appendSelectList(SQLStatement stat, Query<Y> query, X x) {
if (def.isPrimitive) {
Object obj = def.getValue(x);
Object alias = query.getPrimitiveAliasByValue(obj);
query.appendSQL(stat, x, alias);
query.appendSelectSQL(stat, x, alias);
} else {
Object obj = def.getValue(x);
query.appendSQL(stat, x, obj);
query.appendSelectSQL(stat, x, obj);
}
}
}
Expand Down
61 changes: 61 additions & 0 deletions src/test/java/com/iciql/test/EnumsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.iciql.IciqlException;
import com.iciql.test.models.EnumModels;
import com.iciql.test.models.EnumModels.EnumIdModel;
import com.iciql.test.models.EnumModels.EnumJoin;
import com.iciql.test.models.EnumModels.EnumOrdinalModel;
import com.iciql.test.models.EnumModels.EnumStringModel;
import com.iciql.test.models.EnumModels.Genus;
Expand Down Expand Up @@ -59,6 +60,7 @@ public void testEnumQueries() {
testIntEnums(new EnumOrdinalModel());
testStringEnums(new EnumStringModel());
testStringEnumIds(new EnumStringModel());
testStringEnumsSelectObject();
}

private void testIntEnums(EnumModels e) {
Expand Down Expand Up @@ -136,6 +138,65 @@ private void testStringEnumIds(EnumModels e) {

}

private void testStringEnumsSelectObject() {
final EnumOrdinalModel eom = new EnumOrdinalModel();
final EnumStringModel esm = new EnumStringModel();
// ensure all records inserted
List<EnumJoin> enumJoinList = db.from(eom)
.innerJoin(esm).on(eom.id).is(esm.id)
.orderBy(eom.id)
.select(
new EnumJoin() {
{
id = eom.id;
genus = esm.genus();
}
});

assertEquals(5, enumJoinList.size());
assertEquals(enumJoinList.get(0).id.intValue(), 100);
assertEquals(enumJoinList.get(0).genus, Genus.PINUS);
assertEquals(enumJoinList.get(1).id.intValue(), 200);
assertEquals(enumJoinList.get(1).genus, Genus.QUERCUS);
assertEquals(enumJoinList.get(2).id.intValue(), 300);
assertEquals(enumJoinList.get(2).genus, Genus.BETULA);
assertEquals(enumJoinList.get(3).id.intValue(), 400);
assertEquals(enumJoinList.get(3).genus, Genus.JUGLANS);
assertEquals(enumJoinList.get(4).id.intValue(), 500);
assertEquals(enumJoinList.get(4).genus, Genus.ACER);

List<EnumJoin> enumJoinWhereList = db.from(eom)
.innerJoin(esm).on(eom.id).is(esm.id)
.where(esm.tree()).is(Tree.OAK)
.select(
new EnumJoin() {
{
id = eom.id;
genus = esm.genus();
}
});

assertEquals(1, enumJoinWhereList.size());
assertEquals(enumJoinWhereList.get(0).id.intValue(), 200);
assertEquals(enumJoinWhereList.get(0).genus, Genus.QUERCUS);

List<EnumJoin> enumJoinGroupByList = db.from(eom)
.innerJoin(esm).on(eom.id).is(esm.id)
.where(esm.tree()).is(Tree.BIRCH)
.groupBy(eom.id, esm.genus())
.select(
new EnumJoin() {
{
id = eom.id;
genus = esm.genus();
}
});

assertEquals(1, enumJoinGroupByList.size());
assertEquals(enumJoinGroupByList.get(0).id.intValue(), 300);
assertEquals(enumJoinGroupByList.get(0).genus, Genus.BETULA);
}

@Test
public void testMultipleEnumInstances() {
BadEnums b = new BadEnums();
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/iciql/test/models/EnumModels.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,9 @@ public static List<EnumStringModel> createList() {
new EnumStringModel(100, Tree.PINE, Genus.PINUS));
}
}

public static class EnumJoin {
public Integer id;
public Genus genus;
}
}