Skip to content

Commit 278e27c

Browse files
committed
Fix up support for other DBs
1 parent ec0cac1 commit 278e27c

7 files changed

Lines changed: 223 additions & 10 deletions

File tree

controller-jdbc/src/main/kotlin/io/exoquery/controller/jdbc/JdbcControllers.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ object JdbcControllers {
104104

105105
override val encodingConfig =
106106
encodingConfig.copy(
107-
additionalEncoders = encodingConfig.additionalEncoders + JsonObjectEncoding.encoders,
108-
additionalDecoders = encodingConfig.additionalDecoders + JsonObjectEncoding.decoders
107+
additionalEncoders = encodingConfig.additionalEncoders + JsonTextEncoding.encoders,
108+
additionalDecoders = encodingConfig.additionalDecoders + JsonTextEncoding.decoders
109109
)
110110

111111
companion object { }
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.exoquery.sql.h2
2+
3+
import io.exoquery.controller.JsonValue
4+
import io.exoquery.controller.SqlJsonValue
5+
import io.exoquery.sql.*
6+
import io.exoquery.sql.Sql
7+
import io.exoquery.controller.jdbc.JdbcControllers
8+
import io.exoquery.controller.runOn
9+
import io.kotest.core.spec.style.FreeSpec
10+
import io.kotest.matchers.shouldBe
11+
import kotlinx.serialization.Serializable
12+
13+
class JsonSpec: FreeSpec({
14+
val ds = TestDatabases.h2
15+
val ctx by lazy {
16+
JdbcControllers.H2(ds)
17+
}
18+
19+
beforeEach {
20+
ds.run("DELETE FROM JsonExample")
21+
}
22+
23+
"SqlJsonValue annotation works on" - {
24+
"inner data class" - {
25+
@SqlJsonValue
26+
@Serializable
27+
data class MyPerson(val name: String, val age: Int)
28+
29+
@Serializable
30+
data class Example(val id: Int, val value: MyPerson)
31+
32+
val je = Example(1, MyPerson("Alice", 30))
33+
34+
"should encode in json (with explicit serializer) and decode" {
35+
Sql("INSERT INTO JsonExample (id, \"value\") VALUES (1, ${Param.withSer(je.value, MyPerson.serializer())})").action().runOn(ctx)
36+
Sql("SELECT id, \"value\" FROM JsonExample").queryOf<Example>().runOn(ctx) shouldBe listOf(je)
37+
}
38+
}
39+
40+
"annotated field" {
41+
@Serializable
42+
data class MyPerson(val name: String, val age: Int)
43+
44+
@Serializable
45+
data class Example(val id: Int, @SqlJsonValue val value: MyPerson)
46+
47+
val je = Example(1, MyPerson("Joe", 123))
48+
Sql("""INSERT INTO JsonExample (id, "value") VALUES (1, '{"name":"Joe", "age":123}')""").action().runOn(ctx)
49+
val customers = Sql("SELECT id, \"value\" FROM JsonExample").queryOf<Example>().runOn(ctx)
50+
customers shouldBe listOf(je)
51+
}
52+
}
53+
54+
})

terpal-sql-jdbc/src/test/kotlin/io/exoquery/sql/mysql/JsonSpecSimple.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@ import io.kotest.matchers.shouldBe
1111
import kotlinx.serialization.Serializable
1212

1313
class JsonSpec: FreeSpec({
14-
val ds = TestDatabases.postgres
14+
val ds = TestDatabases.mysql
1515
val ctx by lazy {
16-
JdbcControllers.Postgres(ds)
16+
JdbcControllers.Mysql(ds)
1717
}
1818

1919
beforeEach {
20-
ds.run("DELETE FROM JsonbExample")
21-
ds.run("DELETE FROM JsonbExample2")
22-
ds.run("DELETE FROM JsonbExample3")
2320
ds.run("DELETE FROM JsonExample")
2421
}
2522

@@ -48,8 +45,8 @@ class JsonSpec: FreeSpec({
4845
data class Example(val id: Int, @SqlJsonValue val value: MyPerson)
4946

5047
val je = Example(1, MyPerson("Joe", 123))
51-
Sql("""INSERT INTO JsonbExample (id, value) VALUES (1, '{"name":"Joe", "age":123}')""").action().runOn(ctx)
52-
val customers = Sql("SELECT id, value FROM JsonbExample").queryOf<Example>().runOn(ctx)
48+
Sql("""INSERT INTO JsonExample (id, value) VALUES (1, '{"name":"Joe", "age":123}')""").action().runOn(ctx)
49+
val customers = Sql("SELECT id, value FROM JsonExample").queryOf<Example>().runOn(ctx)
5350
customers shouldBe listOf(je)
5451
}
5552
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.exoquery.sql.oracle
2+
3+
import io.exoquery.controller.JsonValue
4+
import io.exoquery.controller.SqlJsonValue
5+
import io.exoquery.sql.*
6+
import io.exoquery.sql.Sql
7+
import io.exoquery.controller.jdbc.JdbcControllers
8+
import io.exoquery.controller.runOn
9+
import io.kotest.core.spec.style.FreeSpec
10+
import io.kotest.matchers.shouldBe
11+
import kotlinx.serialization.Serializable
12+
13+
class JsonSpec: FreeSpec({
14+
val ds = TestDatabases.oracle
15+
val ctx by lazy {
16+
JdbcControllers.Oracle(ds)
17+
}
18+
19+
beforeEach {
20+
ds.run("DELETE FROM JsonExample")
21+
}
22+
23+
"SqlJsonValue annotation works on" - {
24+
"inner data class" - {
25+
@SqlJsonValue
26+
@Serializable
27+
data class MyPerson(val name: String, val age: Int)
28+
29+
@Serializable
30+
data class Example(val id: Int, val value: MyPerson)
31+
32+
val je = Example(1, MyPerson("Alice", 30))
33+
34+
"should encode in json (with explicit serializer) and decode" {
35+
Sql("INSERT INTO JsonExample (id, value) VALUES (1, ${Param.withSer(je.value, MyPerson.serializer())})").action().runOn(ctx)
36+
Sql("SELECT id, value FROM JsonExample").queryOf<Example>().runOn(ctx) shouldBe listOf(je)
37+
}
38+
}
39+
40+
"annotated field" {
41+
@Serializable
42+
data class MyPerson(val name: String, val age: Int)
43+
44+
@Serializable
45+
data class Example(val id: Int, @SqlJsonValue val value: MyPerson)
46+
47+
val je = Example(1, MyPerson("Joe", 123))
48+
Sql("""INSERT INTO JsonExample (id, value) VALUES (1, '{"name":"Joe", "age":123}')""").action().runOn(ctx)
49+
val customers = Sql("SELECT id, value FROM JsonExample").queryOf<Example>().runOn(ctx)
50+
customers shouldBe listOf(je)
51+
}
52+
}
53+
54+
})
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.exoquery.sql.sqlite
2+
3+
import io.exoquery.controller.JsonValue
4+
import io.exoquery.controller.SqlJsonValue
5+
import io.exoquery.sql.*
6+
import io.exoquery.sql.Sql
7+
import io.exoquery.controller.jdbc.JdbcControllers
8+
import io.exoquery.controller.runOn
9+
import io.kotest.core.spec.style.FreeSpec
10+
import io.kotest.matchers.shouldBe
11+
import kotlinx.serialization.Serializable
12+
13+
class JsonSpec: FreeSpec({
14+
val ds = TestDatabases.sqlite
15+
val ctx by lazy {
16+
JdbcControllers.Sqlite(ds)
17+
}
18+
19+
beforeEach {
20+
ds.run("DELETE FROM JsonExample")
21+
}
22+
23+
"SqlJsonValue annotation works on" - {
24+
"inner data class" - {
25+
@SqlJsonValue
26+
@Serializable
27+
data class MyPerson(val name: String, val age: Int)
28+
29+
@Serializable
30+
data class Example(val id: Int, val value: MyPerson)
31+
32+
val je = Example(1, MyPerson("Alice", 30))
33+
34+
"should encode in json (with explicit serializer) and decode" {
35+
Sql("INSERT INTO JsonExample (id, value) VALUES (1, ${Param.withSer(je.value, MyPerson.serializer())})").action().runOn(ctx)
36+
Sql("SELECT id, value FROM JsonExample").queryOf<Example>().runOn(ctx) shouldBe listOf(je)
37+
}
38+
}
39+
40+
"annotated field" {
41+
@Serializable
42+
data class MyPerson(val name: String, val age: Int)
43+
44+
@Serializable
45+
data class Example(val id: Int, @SqlJsonValue val value: MyPerson)
46+
47+
val je = Example(1, MyPerson("Joe", 123))
48+
Sql("""INSERT INTO JsonExample (id, value) VALUES (1, '{"name":"Joe", "age":123}')""").action().runOn(ctx)
49+
val customers = Sql("SELECT id, value FROM JsonExample").queryOf<Example>().runOn(ctx)
50+
customers shouldBe listOf(je)
51+
}
52+
}
53+
54+
})
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.exoquery.sql.sqlserver
2+
3+
import io.exoquery.controller.JsonValue
4+
import io.exoquery.controller.SqlJsonValue
5+
import io.exoquery.sql.*
6+
import io.exoquery.sql.Sql
7+
import io.exoquery.controller.jdbc.JdbcControllers
8+
import io.exoquery.controller.runOn
9+
import io.kotest.core.spec.style.FreeSpec
10+
import io.kotest.matchers.shouldBe
11+
import kotlinx.serialization.Serializable
12+
13+
class JsonSpec: FreeSpec({
14+
val ds = TestDatabases.sqlServer
15+
val ctx by lazy {
16+
JdbcControllers.SqlServer(ds)
17+
}
18+
19+
beforeEach {
20+
ds.run("DELETE FROM JsonExample")
21+
}
22+
23+
"SqlJsonValue annotation works on" - {
24+
"inner data class" - {
25+
@SqlJsonValue
26+
@Serializable
27+
data class MyPerson(val name: String, val age: Int)
28+
29+
@Serializable
30+
data class Example(val id: Int, val value: MyPerson)
31+
32+
val je = Example(1, MyPerson("Alice", 30))
33+
34+
"should encode in json (with explicit serializer) and decode" {
35+
Sql("INSERT INTO JsonExample (id, \"value\") VALUES (1, ${Param.withSer(je.value, MyPerson.serializer())})").action().runOn(ctx)
36+
Sql("SELECT id, \"value\" FROM JsonExample").queryOf<Example>().runOn(ctx) shouldBe listOf(je)
37+
}
38+
}
39+
40+
"annotated field" {
41+
@Serializable
42+
data class MyPerson(val name: String, val age: Int)
43+
44+
@Serializable
45+
data class Example(val id: Int, @SqlJsonValue val value: MyPerson)
46+
47+
val je = Example(1, MyPerson("Joe", 123))
48+
Sql("""INSERT INTO JsonExample (id, "value") VALUES (1, '{"name":"Joe", "age":123}')""").action().runOn(ctx)
49+
val customers = Sql("SELECT id, \"value\" FROM JsonExample").queryOf<Example>().runOn(ctx)
50+
customers shouldBe listOf(je)
51+
}
52+
}
53+
54+
})

terpal-sql-jdbc/src/test/resources/db/sqlserver-schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@ CREATE TABLE JavaTestEntity(
8484

8585
CREATE TABLE JsonExample(
8686
id INT PRIMARY KEY,
87-
value VARCHAR
87+
value VARCHAR(255)
8888
);

0 commit comments

Comments
 (0)