package be.simplenotes.persistance.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.* internal 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) } } internal fun BaseTable.uuidBinary(name: String) = registerColumn(name, UuidBinarySqlType())