question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add something to easily handle the mapping of Enum in database

See original GitHub issue

Hello.

I wonder if it would be possible to add something to easily handle mapping of Enum type in database ? A mapper which map an Enum Kotlin field into a VARCHAR database field.

Maybe adding a specific SqlType for the enums ?

What do you think about that ?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
vincentlauvlwjcommented, Nov 5, 2019

We can add a SqlType implementation to handle enums:

fun <E : Any, C : Enum<C>> BaseTable<E>.enum(name: String, typeRef: TypeReference<C>): BaseTable<E>.ColumnRegistration<C> {
    return registerColumn(name, EnumSqlType(typeRef.referencedType as Class<C>))
}

class EnumSqlType<C : Enum<C>>(val enumClass: Class<C>) : SqlType<C>(Types.VARCHAR, "varchar") {
    private val valueOf = enumClass.getDeclaredMethod("valueOf", String::class.java)

    override fun doSetParameter(ps: PreparedStatement, index: Int, parameter: C) {
        ps.setString(index, parameter.name)
    }

    override fun doGetResult(rs: ResultSet, index: Int): C? {
        return rs.getString(index)?.takeIf { it.isNotBlank() }?.let { enumClass.cast(valueOf(null, it)) }
    }
}

Usage:

object Employees : Table<Nothing>("t_employee") {
    // ...
    val sex by enum("sex", typeRef<Sex>())
}
1reaction
vincentlauvlwjcommented, Feb 2, 2020

Ktorm 2.7 is released.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add something to easily handle the mapping of Enum ... - GitHub
A mapper which map an Enum Kotlin field into a VARCHAR database field ... Add something to easily handle the mapping of Enum...
Read more >
Enum Mappings with Hibernate - The Complete Guide
Let me show you how to use Hibernate's standard enum mappings, create a flexible, custom mapping and map db-specific enum types.
Read more >
The best way to map an Enum Type with JPA and Hibernate
To map the Enum to a String database column type, you need to specify the EnumType.STRING value when using the @Enumerated annotation.
Read more >
Ways to save enums in database - Stack Overflow
Show activity on this post. What is the best way to save enums into a database? I know Java provides name() and valueOf()...
Read more >
Enum Mappings with Hibernate – The Complete Guide
In today's video I will talk about Enum mappings with JPA and Hibernate...Most developers use enums in their domain models. You can easily...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found