diff --git a/api/src/extensions/KtormExtensions.kt b/api/src/extensions/KtormExtensions.kt new file mode 100644 index 0000000..2b691e0 --- /dev/null +++ b/api/src/extensions/KtormExtensions.kt @@ -0,0 +1,27 @@ +package be.vandewalleh.extensions + +import me.liuwj.ktorm.schema.* +import java.nio.ByteBuffer +import java.sql.PreparedStatement +import java.sql.ResultSet +import java.sql.Types +import java.util.* + +class UuidBinarySqlType : SqlType(Types.BINARY, typeName = "uuidBinary") { + override fun doGetResult(rs: ResultSet, index: Int): UUID? { + val value = rs.getBytes(index) ?: return null + return ByteBuffer.wrap(value).let { b -> UUID(b.long, b.long) } + } + + override fun doSetParameter(ps: PreparedStatement, index: Int, parameter: UUID) { + val bytes = ByteBuffer.allocate(16) + .putLong(parameter.mostSignificantBits) + .putLong(parameter.leastSignificantBits) + .array() + ps.setBytes(index, bytes) + } +} + +fun BaseTable.uuidBinary(name: String): BaseTable.ColumnRegistration { + return registerColumn(name, UuidBinarySqlType()) +}