Created
June 8, 2021 12:23
-
-
Save Oskang09/5c5021b2b01676e195e10b1a721a0aa2 to your computer and use it in GitHub Desktop.
Kotlin Exposed ORM Expansion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.databind.ObjectMapper | |
import com.fasterxml.jackson.databind.type.CollectionType | |
import org.jetbrains.exposed.sql.Column | |
import org.jetbrains.exposed.sql.ColumnType | |
import org.jetbrains.exposed.sql.Table | |
fun <T : Any> Table.array(name: String, clazz: Class<T>, jsonMapper: ObjectMapper = ObjectMapper()): Column<Array<T>> | |
= registerColumn(name, ArrayType(clazz, jsonMapper)) | |
class ArrayType<out T: Any>(private val clazz: Class<T>, private val mapper: ObjectMapper): ColumnType() { | |
override var nullable: Boolean = true | |
override fun sqlType() = "TEXT" | |
override fun valueFromDB(value: Any): Any { | |
val text = value.toString() | |
if (text == "") return mutableListOf<String>() | |
val javaType: CollectionType = mapper.typeFactory.constructCollectionType(MutableList::class.java, clazz) | |
return mapper.readValue(text, javaType) | |
} | |
override fun valueToDB(value: Any?): Any { | |
if (value == null) return "" | |
return mapper.writeValueAsString(value) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jetbrains.exposed.sql.Column | |
import org.jetbrains.exposed.sql.ColumnType | |
import org.jetbrains.exposed.sql.Table | |
fun <T : Enum<T>> Table.enum(name: String): Column<T> | |
= registerColumn(name, EnumType<T>()) | |
class EnumType<T : Enum<T>>: ColumnType() { | |
override var nullable: Boolean = true | |
override fun sqlType() = "TEXT" | |
override fun valueFromDB(value: Any): Any { | |
val text = value.toString() | |
if (text == "") return "" | |
return enumValueOf<Enum<T>>(text) | |
} | |
override fun valueToDB(value: Any?): Any { | |
if (value == null) return "" | |
return value.toString() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.databind.ObjectMapper | |
import org.jetbrains.exposed.sql.Column | |
import org.jetbrains.exposed.sql.ColumnType | |
import org.jetbrains.exposed.sql.Table | |
fun <T : Any> Table.json( | |
name: String, | |
clazz: Class<T>, | |
jsonMapper: ObjectMapper = ObjectMapper() | |
): Column<T> | |
= registerColumn(name, JSONType(clazz, jsonMapper)) | |
class JSONType<out T: Any>(private val clazz: Class<T>, private val mapper: ObjectMapper): ColumnType() { | |
override var nullable: Boolean = true | |
override fun sqlType() = "TEXT" | |
override fun valueFromDB(value: Any): Any { | |
val text = value.toString() | |
if (text == "") return clazz.newInstance() | |
return mapper.readValue(text, clazz) | |
} | |
override fun valueToDB(value: Any?): Any? { | |
if (value == null) return "" | |
return mapper.writeValueAsString(value) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment