1
+ package org.babyfish.jimmer.sql.kt.cache
2
+
3
+ import org.babyfish.jimmer.meta.ImmutableProp
4
+ import org.babyfish.jimmer.meta.ImmutableType
5
+ import org.babyfish.jimmer.sql.cache.Cache
6
+ import org.babyfish.jimmer.sql.cache.caffeine.CaffeineValueBinder
7
+ import org.babyfish.jimmer.sql.cache.chain.ChainCacheBuilder
8
+ import org.babyfish.jimmer.sql.kt.KSqlClient
9
+ import org.babyfish.jimmer.sql.kt.ast.expression.eq
10
+ import org.babyfish.jimmer.sql.kt.common.AbstractQueryTest
11
+ import org.babyfish.jimmer.sql.kt.model.classic.book.Book
12
+ import org.babyfish.jimmer.sql.kt.model.classic.book.edition
13
+ import org.babyfish.jimmer.sql.kt.model.classic.book.fetchBy
14
+ import org.babyfish.jimmer.sql.kt.model.classic.book.id
15
+ import org.junit.Test
16
+ import java.time.Duration
17
+
18
+ // No problem, not bug
19
+ class Issue1066Test : AbstractQueryTest () {
20
+
21
+ private val _sqlClient : KSqlClient =
22
+ sqlClient {
23
+ setCacheFactory(
24
+ object : KCacheFactory {
25
+ override fun createObjectCache (type : ImmutableType ): Cache <* , * >? {
26
+ return ChainCacheBuilder <Any , Any >()
27
+ .add(
28
+ CaffeineValueBinder
29
+ .forObject<Any , Any >(type)
30
+ .maximumSize(1024 )
31
+ .duration(Duration .ofHours(1 ))
32
+ .build()
33
+ ).build()
34
+ }
35
+
36
+ override fun createAssociatedIdListCache (prop : ImmutableProp ): Cache <* , List <* >>? {
37
+ return ChainCacheBuilder <Any , List <* >>()
38
+ .add(
39
+ CaffeineValueBinder
40
+ .forProp<Any , List <* >>(prop)
41
+ .maximumSize(1024 )
42
+ .duration(Duration .ofHours(1 ))
43
+ .build()
44
+ ).build()
45
+ }
46
+
47
+ override fun createAssociatedIdCache (prop : ImmutableProp ): Cache <* , * >? {
48
+ return ChainCacheBuilder <Any , Any >()
49
+ .add(
50
+ CaffeineValueBinder
51
+ .forProp<Any , Any >(prop)
52
+ .maximumSize(1024 )
53
+ .duration(Duration .ofHours(1 ))
54
+ .build()
55
+ ).build()
56
+ }
57
+ }
58
+ )
59
+ }
60
+
61
+ @Test
62
+ fun test () {
63
+ for (i in 0 .. 2 ) {
64
+ executeAndExpect(
65
+ _sqlClient .createQuery(Book ::class ) {
66
+ where(table.edition eq 3 )
67
+ orderBy(table.id)
68
+ select(
69
+ table.fetchBy {
70
+ name()
71
+ store {
72
+ name()
73
+ }
74
+ }
75
+ )
76
+ }
77
+ ) {
78
+ sql(
79
+ """ select tb_1_.ID, tb_1_.NAME, tb_1_.STORE_ID
80
+ |from BOOK tb_1_
81
+ |where tb_1_.EDITION = ?
82
+ |order by tb_1_.ID asc""" .trimMargin()
83
+ )
84
+ // 0 load from cache; 1 load from cache
85
+ if (i == 0 ) {
86
+ statement(1 ).sql(
87
+ """ select tb_1_.ID, tb_1_.NAME, tb_1_.VERSION, tb_1_.WEBSITE
88
+ |from BOOK_STORE tb_1_
89
+ |where tb_1_.ID in (?, ?)""" .trimMargin()
90
+ )
91
+ }
92
+ rows(
93
+ """ [
94
+ |--->{
95
+ |--->--->"id":3,
96
+ |--->--->"name":"Learning GraphQL",
97
+ |--->--->"store":{
98
+ |--->--->--->"id":1,
99
+ |--->--->--->"name":"O'REILLY"
100
+ |--->--->}
101
+ |--->},{
102
+ |--->--->"id":6,
103
+ |--->--->"name":"Effective TypeScript",
104
+ |--->--->"store":{
105
+ |--->--->--->"id":1,
106
+ |--->--->--->"name":"O'REILLY"
107
+ |--->--->}
108
+ |--->},{
109
+ |--->--->"id":9,
110
+ |--->--->"name":"Programming TypeScript",
111
+ |--->--->"store":{
112
+ |--->--->--->"id":1,
113
+ |--->--->--->"name":"O'REILLY"
114
+ |--->--->}
115
+ |--->},{
116
+ |--->--->"id":12,
117
+ |--->--->"name":"GraphQL in Action",
118
+ |--->--->"store":{
119
+ |--->--->--->"id":2,
120
+ |--->--->--->"name":"MANNING"
121
+ |--->--->}
122
+ |--->}
123
+ |]""" .trimMargin()
124
+ )
125
+ }
126
+ }
127
+ }
128
+ }
0 commit comments