Re: [ceylon-users] How to create an enum from database column

45 views
Skip to first unread message

Paco Soberón

unread,
Apr 8, 2018, 11:39:51 AM4/8/18
to ceylon...@googlegroups.com
Use a switch expression (https://ceylon-lang.org/documentation/1.3/reference/expression/switch/):

case (rs.getString(sex.columnName)) when ("M") m when ("F") F else null

Or, if you are sure about your database contents and don't want to make gender nullable, then:

case (rs.getString(sex.columnName)) when ("M") m else F


El sáb., 7 abr. 2018 20:16, Mohammad Als <karim...@gmail.com> escribió:


If a class is declared with an enum as given

abstract shared class Gender(shared String gender) of m | f {
string => gender;
}

shared object m extends Gender("M"){}
shared object f extends Gender("F"){}

shared class Person(shared Integer id, shared String firstName, shared String lastName, shared Gender gender) {}

where Gender is an enum.

Then, what is the correct way to write a JDBC row-mapper

shared RowMapper<Person> person_row_mapper = object satisfies RowMapper<Person> {
shared actual Person map(ResultSet rs) {
Person person = Person(
rs.getLong(personId.columnName),
rs.getString(firstName.columnName),
rs.getString(lastName.columnName),
////object extends Gender( rs.getString(sex.columnName) ) {}
//this does not work.... because Gender can only be of type m or f
);
return person;
}
};

--
You received this message because you are subscribed to the Google Groups "ceylon-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceylon-users...@googlegroups.com.
To post to this group, send email to ceylon...@googlegroups.com.
Visit this group at https://groups.google.com/group/ceylon-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceylon-users/fe326e02-8e0e-4407-b98c-61607a2d415e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paco Soberón

unread,
Apr 8, 2018, 11:41:46 AM4/8/18
to ceylon...@googlegroups.com
Sorry, lowercase f:

case (rs.getString(sex.columnName)) when ("M") m when ("F") f else null


case (rs.getString(sex.columnName)) when ("M") m else f
Reply all
Reply to author
Forward
0 new messages