-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathJsonColumnExample.kt
More file actions
194 lines (160 loc) · 7.73 KB
/
JsonColumnExample.kt
File metadata and controls
194 lines (160 loc) · 7.73 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package io.exoquery.sql.examples
import io.exoquery.controller.JsonValue
import io.exoquery.sql.Param
import io.exoquery.controller.SqlJsonValue
import io.exoquery.sql.Sql
import io.exoquery.controller.jdbc.JdbcControllers
import io.exoquery.controller.runOn
import io.zonky.test.db.postgres.embedded.EmbeddedPostgres
import kotlinx.serialization.Serializable
object JsonColumnExample1 {
@SqlJsonValue
@Serializable
data class MyPerson(val name: String, val age: Int)
@Serializable
data class JsonbExample(val id: Int, val jsonbValue: MyPerson)
suspend fun main() {
val postgres = EmbeddedPostgres.start()
postgres.run("CREATE TABLE JsonbExample(id SERIAL PRIMARY KEY, jsonbValue JSONB)")
val ctx = JdbcControllers.Postgres(postgres.postgresDatabase)
val je = JsonbExample(1, MyPerson("Alice", 30))
Sql("INSERT INTO JsonbExample (id, jsonbValue) VALUES (1, ${Param.withSer(je.jsonbValue, MyPerson.serializer())})").action().runOn(ctx)
//val customers = Sql("SELECT id, jsonbValue FROM JsonbExample").queryOf<JsonbExample>().runOn(ctx)
//println(customers)
val people = Sql("SELECT jsonbValue FROM JsonbExample").queryOf<MyPerson>().runOn(ctx)
println(people)
}
}
object JsonColumnExample2 {
@Serializable
data class MyPerson(val name: String, val age: Int)
@Serializable
data class JsonbExample(val id: Int, @SqlJsonValue val jsonbValue: MyPerson)
suspend fun main() {
val postgres = EmbeddedPostgres.start()
postgres.run("CREATE TABLE JsonbExample(id SERIAL PRIMARY KEY, jsonbValue JSONB)")
val ctx = JdbcControllers.Postgres(postgres.postgresDatabase)
val je = JsonbExample(1, MyPerson("Alice", 30))
//Sql("INSERT INTO JsonbExample (id, jsonbValue) VALUES (1, ${Param.withSer(je.jsonbValue, MyPerson.serializer())})").action().runOn(ctx)
Sql("""INSERT INTO JsonbExample (id, jsonbValue) VALUES (1, '{"name":"Joe", "age":123}')""").action().runOn(ctx)
val customers = Sql("SELECT id, jsonbValue FROM JsonbExample").queryOf<JsonbExample>().runOn(ctx)
println(customers)
}
}
object JsonColumnExample4 {
@Serializable
data class MyPerson(val name: String, val age: Int)
@Serializable
data class JsonbExample(val id: Int, val jsonbValue: JsonValue<MyPerson>)
suspend fun main() {
val postgres = EmbeddedPostgres.start()
postgres.run("CREATE TABLE JsonbExample(id SERIAL PRIMARY KEY, jsonbValue JSONB)")
val ctx = JdbcControllers.Postgres(postgres.postgresDatabase)
val je = JsonbExample(1, JsonValue(MyPerson("Alice", 30)))
Sql("INSERT INTO JsonbExample (id, jsonbValue) VALUES (1, ${Param.withSer(je.jsonbValue)})").action().runOn(ctx)
//Sql("""INSERT INTO JsonbExample (id, jsonbValue) VALUES (1, '{"name":"Joe", "age":123}')""").action().runOn(ctx)
val customers = Sql("SELECT id, jsonbValue FROM JsonbExample").queryOf<JsonbExample>().runOn(ctx)
println(customers)
}
}
// Like JsonColumnExample4 but just get the the json column as a single value
object JsonColumnExample5 {
@Serializable
data class MyPerson(val name: String, val age: Int)
@Serializable
data class JsonbExample(val id: Int, val jsonbValue: JsonValue<MyPerson>)
suspend fun main() {
val postgres = EmbeddedPostgres.start()
postgres.run("CREATE TABLE JsonbExample(id SERIAL PRIMARY KEY, jsonbValue JSONB)")
val ctx = JdbcControllers.Postgres(postgres.postgresDatabase)
val je = JsonbExample(1, JsonValue(MyPerson("Alice", 30)))
//Sql("INSERT INTO JsonbExample (id, jsonbValue) VALUES (1, ${Param.withSer(je.jsonbValue, MyPerson.serializer())})").action().runOn(ctx)
Sql("""INSERT INTO JsonbExample (id, jsonbValue) VALUES (1, '{"name":"Joe", "age":123}')""").action().runOn(ctx)
val jsonValues = Sql("SELECT jsonbValue FROM JsonbExample").queryOf<JsonValue<MyPerson>>().runOn(ctx)
println(jsonValues)
val parentValues = Sql("SELECT id, jsonbValue FROM JsonbExample").queryOf<JsonbExample>().runOn(ctx)
println(parentValues)
}
}
// Using JsonValue to read/write a datatype already supported by kotlinx-serialization
object JsonColumnExample6 {
@Serializable
data class JsonbExample(val id: Int, val jsonbValue: JsonValue<List<String>>)
suspend fun main() {
val postgres = EmbeddedPostgres.start()
postgres.run("CREATE TABLE JsonbExample(id SERIAL PRIMARY KEY, jsonbValue JSONB)")
val ctx = JdbcControllers.Postgres(postgres.postgresDatabase)
val jsonValue = JsonValue(listOf("Joe", "Jack"))
val je = JsonbExample(1, jsonValue)
Sql("INSERT INTO JsonbExample (id, jsonbValue) VALUES (1, ${Param.withSer(jsonValue)})").action().runOn(ctx)
val jsonValues = Sql("SELECT jsonbValue FROM JsonbExample").queryOf<JsonValue<List<String>>>().runOn(ctx)
println(jsonValues)
val parentValues = Sql("SELECT id, jsonbValue FROM JsonbExample").queryOf<JsonbExample>().runOn(ctx)
println(parentValues)
}
}
// Mix and match usage of JsonValue and @SqlJsonValue
object JsonColumnExample7 {
@Serializable
data class MyPerson(val name: String, val age: Int)
@Serializable
data class JsonbExample(val id: Int, @SqlJsonValue val jsonbValue: MyPerson)
suspend fun main() {
val postgres = EmbeddedPostgres.start()
postgres.run("CREATE TABLE JsonbExample(id SERIAL PRIMARY KEY, jsonbValue JSONB)")
val ctx = JdbcControllers.Postgres(postgres.postgresDatabase)
val joe = MyPerson("Joe", 123)
val jack = MyPerson("Jack", 456)
Sql("INSERT INTO JsonbExample (id, jsonbValue) VALUES (1, ${Param.withSer(JsonValue(joe))})").action().runOn(ctx)
Sql("INSERT INTO JsonbExample (id, jsonbValue) VALUES (2, ${Param.json(jack)})").action().runOn(ctx)
val parentValue = Sql("SELECT id, jsonbValue FROM JsonbExample").queryOf<JsonbExample>().runOn(ctx)
println(parentValue)
val jsonValue = Sql("SELECT jsonbValue FROM JsonbExample").queryOf<JsonValue<MyPerson>>().runOn(ctx)
println(jsonValue)
}
}
// Does not work:
//typealias MyPersonJson = @Serializable @SqlJsonValue JsonColumnExample3.MyPerson
//object JsonColumnExample3 {
//
// @Serializable
// data class MyPerson(val name: String, val age: Int)
//
// @Serializable
// data class JsonbExample(val id: Int, val jsonbValue: MyPersonJson)
//
// suspend fun main() {
// val postgres = EmbeddedPostgres.start()
// postgres.run("CREATE TABLE JsonbExample(id SERIAL PRIMARY KEY, jsonbValue JSONB)")
// val ctx = TerpalContext.Postgres(postgres.postgresDatabase)
// val je = JsonbExample(1, MyPerson("Alice", 30))
// //Sql("INSERT INTO JsonbExample (id, jsonbValue) VALUES (1, ${Param.withSer(je.jsonbValue, MyPerson.serializer())})").action().runOn(ctx)
// Sql("""INSERT INTO JsonbExample (id, jsonbValue) VALUES (1, '{"name":"Joe", "age":123}')""").action().runOn(ctx)
// val customers = Sql("SELECT id, jsonbValue FROM JsonbExample").queryOf<JsonbExample>().runOn(ctx)
// println(customers)
// }
//}
// Does not work:
//object JsonColumnExample31 {
//
// @Serializable
// data class MyPerson(val name: String, val age: Int)
//
// @Serializable
// data class JsonbExample(val id: Int, val jsonbValue: JsonValue<MyPerson>)
//
// suspend fun main() {
// val postgres = EmbeddedPostgres.start()
// postgres.run("CREATE TABLE JsonbExample(id SERIAL PRIMARY KEY, jsonbValue JSONB)")
// val ctx = TerpalContext.Postgres(postgres.postgresDatabase)
// val je = JsonbExample(1, JsonValue(MyPerson("Alice", 30)))
// //Sql("INSERT INTO JsonbExample (id, jsonbValue) VALUES (1, ${Param.withSer(je.jsonbValue, MyPerson.serializer())})").action().runOn(ctx)
// Sql("""INSERT INTO JsonbExample (id, jsonbValue) VALUES (1, '{"name":"Joe", "age":123}')""").action().runOn(ctx)
//
// val customers = Sql("SELECT jsonbValue FROM JsonbExample").queryOf<@SqlJsonValue MyPerson>().runOn(ctx)
// println(customers)
// }
//}
suspend fun main() {
JsonColumnExample7.main()
}