Add uuidBinary column type
This commit is contained in:
parent
a324f636da
commit
2e7e6d5e94
27
api/src/extensions/KtormExtensions.kt
Normal file
27
api/src/extensions/KtormExtensions.kt
Normal file
@ -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<java.util.UUID>(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 <E : Any> BaseTable<E>.uuidBinary(name: String): BaseTable<E>.ColumnRegistration<java.util.UUID> {
|
||||||
|
return registerColumn(name, UuidBinarySqlType())
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user