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

Automapping enums properties #72

Closed
ianbondoc opened this issue Oct 12, 2021 · 5 comments
Closed

Automapping enums properties #72

ianbondoc opened this issue Oct 12, 2021 · 5 comments
Labels

Comments

@ianbondoc
Copy link

Is there a way for automapping a string to enums on select and enums to string when using as query parameter?

@davidmoten
Copy link
Owner

I'll have a look

@davidmoten
Copy link
Owner

What use case are you after, mapping a column directly to an enum or mapping a number of columns to a class where the fields are possibly enums? Both perhaps?

@ianbondoc
Copy link
Author

ianbondoc commented Oct 12, 2021

Was hoping it can support mapping a number of columns to to a class where some fields are enums, not sure what you mean mapping a column directly to an enum, maybe that's also useful if you're after a single column where the value is represented by a an enum,

the other use case i have is the following where the enum is used as a parameter to a statement:

       val status = SomeEnumStatus.PENDING
        db.select("SELECT * FROM some_table t WHERE t.status = :status")
            .parameter("status", status)
            .autoMap(SomeEntity::class.java)

BTW, thanks a lot for an amazing lib, good solution to what big companies like ibm fail to address

@davidmoten
Copy link
Owner

davidmoten commented Oct 13, 2021

Thanks, glad you find it useful.

I'd suggest you be quite specific with the parameter and pass .parameter("status", status.name()) or even better .parameter("status", status.value()) where value() is a custom method that carries the String name for use in db tables.

For automapping to an interface I suggest an approach like below. That way if you rename your enums (assuming that you serialize an enum to its name() method) then you won't destroy the coordination with the database:

interface PersonWithEnum {
        @Column
        String name();

        @Column("gender")
        String genderCharacter();
        
        default Gender gender() {
            return Gender.fromCode(genderCharacter());
        }
    }

I've added an example to DatabaseTest.java:

@Test
public void testAutoMapToEnum() {
try (Database db = Database.test()) {
db.select(PersonWithEnum.class) //
.get(x -> x.gender()) //
.test() //
.awaitDone(TIMEOUT_SECONDS, TimeUnit.SECONDS) //
.assertValues(Gender.MALE, Gender.FEMALE, Gender.MALE) //
.assertComplete();
}
}
public enum Gender {
MALE("M"), FEMALE("F"), OTHER("O");
private final String code;
private Gender(String code) {
this.code = code;
}
public static Gender fromCode(String code) {
if (code == null) {
return null;
}
for (Gender g : Gender.values()) {
if (code.equals(g.code)) {
return g;
}
}
return OTHER;
}
}
@Query("select name, gender from person order by name")
public interface PersonWithEnum {
@Column
String name();
@Column("gender")
String genderCharacter();
default Gender gender() {
return Gender.fromCode(genderCharacter());
}
}

@ianbondoc
Copy link
Author

thanks that makes sense, i just got used to spring where it provides converters for you, but this is good enough

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants