Last active
January 27, 2018 04:25
-
-
Save dcshock/7c085badd8969cf0568d3b9fc4de23bc to your computer and use it in GitHub Desktop.
Protocol Buffer to Postgres JSONB - Kotlin Exposed
This file contains hidden or 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
// Protocol Buffer (protobuf) V3 - Kotlin Exposed columnType | |
// Converts to and from jsonb fields in postgres. | |
class ProtoColumnType(val message: Message) : ColumnType() { | |
override fun sqlType() = "JSONB" | |
override fun valueFromDB(value: Any): Any { | |
if (value is PGobject) { | |
return JsonFormat.parser().ignoringUnknownFields().merge(value.value, message.toBuilder()) | |
} | |
throw RuntimeException("Can't parse object: ${value}") | |
} | |
override fun setParameter(stmt: PreparedStatement, index: Int, value: Any?) { | |
val json = JsonFormat.printer().print(value as GeneratedMessageV3) | |
val obj = PGobject() | |
obj.type = "jsonb" | |
obj.value = json | |
stmt.setObject(index, obj) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment