Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Adjust kotlinx-serialization & postgresql dependencies in core #1782

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions exposed-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import org.jetbrains.exposed.gradle.Versions

plugins {
kotlin("jvm") apply true
kotlin("plugin.serialization") apply true
}

repositories {
Expand All @@ -13,7 +12,6 @@ dependencies {
api(kotlin("stdlib"))
api(kotlin("reflect"))
api("org.jetbrains.kotlinx", "kotlinx-coroutines-core", Versions.kotlinCoroutines)
api("org.jetbrains.kotlinx", "kotlinx-serialization-json", Versions.kotlinxSerialization)
api("org.postgresql", "postgresql", Versions.postgre)
compileOnly("org.postgresql", "postgresql", Versions.postgre)
api("org.slf4j", "slf4j-api", "1.7.25")
}
Original file line number Diff line number Diff line change
Expand Up @@ -915,10 +915,10 @@ open class JsonColumnType<T : Any>(
override fun sqlType(): String = currentDialect.dataTypeProvider.jsonType()

override fun valueFromDB(value: Any): Any {
return when (value) {
is String -> deserialize(value)
is PGobject -> deserialize(value.value!!)
is ByteArray -> deserialize(value.decodeToString())
return when {
value is String -> deserialize(value)
value is ByteArray -> deserialize(value.decodeToString())
currentDialect is PostgreSQLDialect && value is PGobject -> deserialize(value.value!!)
else -> value
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

package org.jetbrains.exposed.sql

import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.EntityIDFunctionProvider
import org.jetbrains.exposed.dao.id.IdTable
Expand Down Expand Up @@ -107,20 +105,30 @@ fun <T : Any?> ExpressionWithColumnType<T>.varSamp(scale: Int = 2): VarSamp<T> =
* If none are provided, the root context item `'$'` will be used by default.
* **Note:** Multiple [path] arguments are not supported by all vendors; please check the documentation.
* @param toScalar If `true`, the extracted result is a scalar or text value; otherwise, it is a JSON object.
* @param serialize Optional function that encodes an object of type [T] to a JSON String.
* @param deserialize Optional function that decodes a JSON String to an object of type [T].
* **Note** If [serialize] is provided, [deserialize] must be provided as well, and vice versa.
*/
inline fun <reified T : Any> ExpressionWithColumnType<*>.jsonExtract(vararg path: String, toScalar: Boolean = true): JsonExtract<T> {
val columnType = when (T::class) {
String::class -> TextColumnType()
Boolean::class -> BooleanColumnType()
Long::class -> LongColumnType()
Int::class -> IntegerColumnType()
Short::class -> ShortColumnType()
Byte::class -> ByteColumnType()
Double::class -> DoubleColumnType()
Float::class -> FloatColumnType()
ByteArray::class -> BasicBinaryColumnType()
else -> {
JsonColumnType({ Json.Default.encodeToString(serializer<T>(), it) }, { Json.Default.decodeFromString(serializer<T>(), it) })
inline fun <reified T : Any> ExpressionWithColumnType<*>.jsonExtract(
vararg path: String,
toScalar: Boolean = true,
noinline serialize: ((T) -> String)? = null,
noinline deserialize: ((String) -> T)? = null
): JsonExtract<T> {
val columnType = if (serialize != null && deserialize != null) {
JsonColumnType(serialize, deserialize)
} else {
when (T::class) {
String::class -> TextColumnType()
Boolean::class -> BooleanColumnType()
Long::class -> LongColumnType()
Int::class -> IntegerColumnType()
Short::class -> ShortColumnType()
Byte::class -> ByteColumnType()
Double::class -> DoubleColumnType()
Float::class -> FloatColumnType()
ByteArray::class -> BasicBinaryColumnType()
else -> error("Unexpected type T: ${T::class.qualifiedName}. This type may require a custom serializer/deserializer.")
}
}
return JsonExtract(this, path = path, toScalar, this.columnType, columnType)
Expand Down
38 changes: 0 additions & 38 deletions exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.jetbrains.exposed.sql

import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.EntityIDFunctionProvider
import org.jetbrains.exposed.dao.id.IdTable
Expand Down Expand Up @@ -667,24 +664,6 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {
fun <T : Any> json(name: String, serialize: (T) -> String, deserialize: (String) -> T): Column<T> =
registerColumn(name, JsonColumnType(serialize, deserialize))

/**
* Creates a column, with the specified [name], for storing JSON data.
*
* **Note**: This column stores JSON either in non-binary text format or, if the vendor only supports 1 format, the default JSON type format.
* If JSON must be stored in binary format, and the vendor supports this, please use `jsonb()` instead.
*
* @param name Name of the column
* @param jsonConfig Configured instance of the `Json` class
* @param kSerializer Serializer responsible for the representation of a serial form of type [T].
* Defaults to a generic serializer for type [T]
*/
inline fun <reified T : Any> json(
name: String,
jsonConfig: Json,
kSerializer: KSerializer<T> = serializer<T>()
): Column<T> =
json(name, { jsonConfig.encodeToString(kSerializer, it) }, { jsonConfig.decodeFromString(kSerializer, it) })

/**
* Creates a column, with the specified [name], for storing JSON data in decomposed binary format.
*
Expand All @@ -697,23 +676,6 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {
fun <T : Any> jsonb(name: String, serialize: (T) -> String, deserialize: (String) -> T): Column<T> =
registerColumn(name, JsonBColumnType(serialize, deserialize))

/**
* Creates a column, with the specified [name], for storing JSON data in decomposed binary format.
*
* **Note**: JSON storage in binary format is not supported by all vendors; please check the documentation.
*
* @param name Name of the column
* @param jsonConfig Configured instance of the `Json` class
* @param kSerializer Serializer responsible for the representation of a serial form of type [T].
* Defaults to a generic serializer for type [T]
*/
inline fun <reified T : Any> jsonb(
name: String,
jsonConfig: Json,
kSerializer: KSerializer<T> = serializer<T>()
): Column<T> =
jsonb(name, { jsonConfig.encodeToString(kSerializer, it) }, { jsonConfig.decodeFromString(kSerializer, it) })

// Auto-generated values

/**
Expand Down
2 changes: 2 additions & 0 deletions exposed-java-time/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.jetbrains.exposed.gradle.Versions

plugins {
kotlin("jvm") apply true
Expand All @@ -15,6 +16,7 @@ dependencies {
api(project(":exposed-core"))
testImplementation(project(":exposed-dao"))
testImplementation(project(":exposed-tests"))
testImplementation("org.jetbrains.kotlinx", "kotlinx-serialization-json", Versions.kotlinxSerialization)
testImplementation("junit", "junit", "4.12")
testImplementation(kotlin("test-junit"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.between
Expand Down Expand Up @@ -273,7 +274,11 @@ open class JavaTimeBaseTest : DatabaseTestsBase() {
fun testDateTimeAsJsonB() {
val tester = object : Table("tester") {
val created = datetime("created")
val modified = jsonb<ModifierData>("modified", Json.Default)
val modified = jsonb(
"modified",
{ Json.Default.encodeToString(ModifierData.serializer(), it) },
{ Json.Default.decodeFromString<ModifierData>(it) }
)
}

withTables(excludeSettings = TestDB.allH2TestDB + TestDB.SQLITE + TestDB.SQLSERVER + TestDB.ORACLE, tester) {
Expand All @@ -296,9 +301,9 @@ open class JavaTimeBaseTest : DatabaseTestsBase() {

// PostgreSQL requires explicit type cast to timestamp for in-DB comparison
val dateModified = if (currentDialectTest is PostgreSQLDialect) {
tester.modified.jsonExtract<LocalDateTime>("${prefix}timestamp").castTo(JavaLocalDateTimeColumnType())
tester.modified.jsonExtractImpl<LocalDateTime>("${prefix}timestamp").castTo(JavaLocalDateTimeColumnType())
} else {
tester.modified.jsonExtract<LocalDateTime>("${prefix}timestamp")
tester.modified.jsonExtractImpl<LocalDateTime>("${prefix}timestamp")
}
val modifiedBeforeCreation = tester.select { dateModified less tester.created }.single()
assertEquals(2, modifiedBeforeCreation[tester.modified].userId)
Expand Down Expand Up @@ -393,3 +398,13 @@ object DateTimeSerializer : KSerializer<LocalDateTime> {
override fun serialize(encoder: Encoder, value: LocalDateTime) = encoder.encodeString(value.toString())
override fun deserialize(decoder: Decoder): LocalDateTime = LocalDateTime.parse(decoder.decodeString())
}

inline fun <reified T : Any> ExpressionWithColumnType<*>.jsonExtractImpl(
vararg path: String,
toScalar: Boolean = true
): JsonExtract<T> = jsonExtract<T>(
path = path,
toScalar = toScalar,
serialize = { Json.Default.encodeToString(serializer<T>(), it) },
deserialize = { Json.Default.decodeFromString(serializer<T>(), it) }
)
1 change: 1 addition & 0 deletions exposed-jodatime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
api("joda-time", "joda-time", "2.10.13")
testImplementation(project(":exposed-dao"))
testImplementation(project(":exposed-tests"))
testImplementation("org.jetbrains.kotlinx", "kotlinx-serialization-json", Versions.kotlinxSerialization)
testImplementation("junit", "junit", "4.12")
testImplementation(kotlin("test-junit"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,11 @@ open class JodaTimeBaseTest : DatabaseTestsBase() {
fun testDateTimeAsJsonB() {
val tester = object : Table("tester") {
val created = datetime("created")
val modified = jsonb<ModifierData>("modified", Json.Default)
val modified = jsonb(
"modified",
{ Json.Default.encodeToString(ModifierData.serializer(), it) },
{ Json.Default.decodeFromString<ModifierData>(it) }
)
}

withTables(excludeSettings = TestDB.allH2TestDB + TestDB.SQLITE + TestDB.SQLSERVER + TestDB.ORACLE, tester) {
Expand All @@ -211,9 +215,9 @@ open class JodaTimeBaseTest : DatabaseTestsBase() {

// PostgreSQL requires explicit type cast to timestamp for in-DB comparison
val dateModified = if (currentDialectTest is PostgreSQLDialect) {
tester.modified.jsonExtract<DateTime>("${prefix}timestamp").castTo(DateColumnType(true))
tester.modified.jsonExtractImpl<DateTime>("${prefix}timestamp").castTo(DateColumnType(true))
} else {
tester.modified.jsonExtract<DateTime>("${prefix}timestamp")
tester.modified.jsonExtractImpl<DateTime>("${prefix}timestamp")
}
val modifiedBeforeCreation = tester.select { dateModified less tester.created }.single()
assertEquals(2, modifiedBeforeCreation[tester.modified].userId)
Expand Down Expand Up @@ -256,3 +260,13 @@ object DateTimeSerializer : KSerializer<DateTime> {
override fun serialize(encoder: Encoder, value: DateTime) = encoder.encodeString(value.toString())
override fun deserialize(decoder: Decoder): DateTime = DateTime.parse(decoder.decodeString())
}

inline fun <reified T : Any> ExpressionWithColumnType<*>.jsonExtractImpl(
vararg path: String,
toScalar: Boolean = true
): JsonExtract<T> = jsonExtract<T>(
path = path,
toScalar = toScalar,
serialize = { Json.Default.encodeToString(serializer<T>(), it) },
deserialize = { Json.Default.decodeFromString(serializer<T>(), it) }
)
2 changes: 2 additions & 0 deletions exposed-kotlin-datetime/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.jetbrains.exposed.gradle.Versions

plugins {
kotlin("jvm") apply true
Expand All @@ -16,6 +17,7 @@ dependencies {
api("org.jetbrains.kotlinx", "kotlinx-datetime-jvm", "0.4.0")
testImplementation(project(":exposed-dao"))
testImplementation(project(":exposed-tests"))
testImplementation("org.jetbrains.kotlinx", "kotlinx-serialization-json", Versions.kotlinxSerialization)
testImplementation("junit", "junit", "4.12")
testImplementation(kotlin("test-junit"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.jetbrains.exposed.sql.kotlin.datetime
import kotlinx.datetime.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.between
Expand Down Expand Up @@ -264,7 +265,11 @@ open class KotlinTimeBaseTest : DatabaseTestsBase() {
fun testDateTimeAsJsonB() {
val tester = object : Table("tester") {
val created = datetime("created")
val modified = jsonb<ModifierData>("modified", Json.Default)
val modified = jsonb(
"modified",
{ Json.Default.encodeToString(ModifierData.serializer(), it) },
{ Json.Default.decodeFromString<ModifierData>(it) }
)
}

withTables(excludeSettings = TestDB.allH2TestDB + TestDB.SQLITE + TestDB.SQLSERVER + TestDB.ORACLE, tester) {
Expand All @@ -285,15 +290,15 @@ open class KotlinTimeBaseTest : DatabaseTestsBase() {
val allModifiedAsString = tester.slice(modifiedAsString).selectAll()
assertTrue(allModifiedAsString.all { it[modifiedAsString] == dateTimeNow.toString() })
// value extracted as json, with implicit LocalDateTime serializer() performing conversions
val modifiedAsJson = tester.modified.jsonExtract<LocalDateTime>("${prefix}timestamp", toScalar = false)
val modifiedAsJson = tester.modified.jsonExtractImpl<LocalDateTime>("${prefix}timestamp", toScalar = false)
val allModifiedAsJson = tester.slice(modifiedAsJson).selectAll()
assertTrue(allModifiedAsJson.all { it[modifiedAsJson] == dateTimeNow })

// PostgreSQL requires explicit type cast to timestamp for in-DB comparison
val dateModified = if (currentDialectTest is PostgreSQLDialect) {
tester.modified.jsonExtract<LocalDateTime>("${prefix}timestamp").castTo(KotlinLocalDateTimeColumnType())
tester.modified.jsonExtractImpl<LocalDateTime>("${prefix}timestamp").castTo(KotlinLocalDateTimeColumnType())
} else {
tester.modified.jsonExtract<LocalDateTime>("${prefix}timestamp")
tester.modified.jsonExtractImpl<LocalDateTime>("${prefix}timestamp")
}
val modifiedBeforeCreation = tester.select { dateModified less tester.created }.single()
assertEquals(2, modifiedBeforeCreation[tester.modified].userId)
Expand Down Expand Up @@ -378,3 +383,13 @@ object CitiesTime : IntIdTable("CitiesTime") {

@Serializable
data class ModifierData(val userId: Int, val timestamp: LocalDateTime)

inline fun <reified T : Any> ExpressionWithColumnType<*>.jsonExtractImpl(
vararg path: String,
toScalar: Boolean = true
): JsonExtract<T> = jsonExtract<T>(
path = path,
toScalar = toScalar,
serialize = { Json.Default.encodeToString(serializer<T>(), it) },
deserialize = { Json.Default.decodeFromString(serializer<T>(), it) }
)
1 change: 1 addition & 0 deletions exposed-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ repositories {

dependencies {
implementation("org.jetbrains.kotlinx", "kotlinx-coroutines-core", Versions.kotlinCoroutines)
implementation("org.jetbrains.kotlinx", "kotlinx-serialization-json", Versions.kotlinxSerialization)
implementation(project(":exposed-core"))
implementation(project(":exposed-jdbc"))
implementation(project(":exposed-dao"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class JsonBColumnTypeTests : DatabaseTestsBase() {
val result1 = tester.slice(isActive).selectAll().singleOrNull()
assertEquals(data1.active, result1?.get(isActive))

val storedUser = tester.jsonBColumn.jsonExtract<User>("${pathPrefix}user", toScalar = false)
val storedUser = tester.jsonBColumn.jsonExtractImpl<User>("${pathPrefix}user", toScalar = false)
val result2 = tester.slice(storedUser).selectAll().singleOrNull()
assertEquals(user1, result2?.get(storedUser))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class JsonColumnTypeTests : DatabaseTestsBase() {
val result1 = tester.slice(isActive).selectAll().singleOrNull()
assertEquals(data1.active, result1?.get(isActive))

val storedUser = tester.jsonColumn.jsonExtract<User>("${pathPrefix}user", toScalar = false)
val storedUser = tester.jsonColumn.jsonExtractImpl<User>("${pathPrefix}user", toScalar = false)
val result2 = tester.slice(storedUser).selectAll().singleOrNull()
assertEquals(user1, result2?.get(storedUser))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package org.jetbrains.exposed.sql.tests.shared.types

import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer
import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.insertAndGetId
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB

Expand Down Expand Up @@ -45,6 +44,30 @@ object JsonTestsData {
}
}

inline fun <reified T : Any> Table.json(
name: String,
jsonConfig: Json,
kSerializer: KSerializer<T> = serializer<T>()
): Column<T> =
json(name, { jsonConfig.encodeToString(kSerializer, it) }, { jsonConfig.decodeFromString(kSerializer, it) })

inline fun <reified T : Any> Table.jsonb(
name: String,
jsonConfig: Json,
kSerializer: KSerializer<T> = serializer<T>()
): Column<T> =
jsonb(name, { jsonConfig.encodeToString(kSerializer, it) }, { jsonConfig.decodeFromString(kSerializer, it) })

inline fun <reified T : Any> ExpressionWithColumnType<*>.jsonExtractImpl(
vararg path: String,
toScalar: Boolean = true
): JsonExtract<T> = jsonExtract<T>(
path = path,
toScalar = toScalar,
serialize = { Json.Default.encodeToString(serializer<T>(), it) },
deserialize = { Json.Default.decodeFromString(serializer<T>(), it) }
)

fun DatabaseTestsBase.withJsonTable(
exclude: List<TestDB> = emptyList(),
statement: Transaction.(tester: JsonTestsData.JsonTable, user1: User, data1: DataHolder) -> Unit
Expand Down