-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharrayOfString.kt
More file actions
42 lines (36 loc) · 1.25 KB
/
arrayOfString.kt
File metadata and controls
42 lines (36 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.ishroid.example.app.utils
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.ColumnType
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.statements.api.PreparedStatementApi
import java.lang.StringBuilder
fun Table.arrayOfString(name: String): Column<List<String>> =
registerColumn(name, StringArrayColumnType())
internal class StringArrayColumnType : ColumnType() {
override fun sqlType() = "varchar"
override fun valueFromDB(value: Any): List<String> {
return if (value is String) {
value.split(",")
} else
emptyList()
}
override fun valueToString(value: Any?): String {
return if (value is List<*>) {
val finalResult = StringBuilder()
value.forEachIndexed { index, any ->
if (index != 0)
finalResult.append(",")
finalResult.append(any)
}
finalResult.toString()
} else {
""
}
}
override fun setParameter(stmt: PreparedStatementApi, index: Int, value: Any?) {
if (value is List<*>) {
stmt[index] = valueToString(value)
} else
super.setParameter(stmt, index, value)
}
}