Skip to content

Commit b009401

Browse files
committed
DATAMONGO-2138 - Create TypedQuery and TypedCriteria
1 parent 2cfcc86 commit b009401

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2010-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.query
17+
18+
import org.bson.Document
19+
import kotlin.reflect.KProperty1
20+
21+
/**
22+
* @author Tjeu Kayim
23+
*/
24+
class TypedCriteria<DOC> : CriteriaDefinition<DOC> {
25+
override fun getCriteriaObject(): Document = chain.criteriaObject
26+
27+
override fun getKey(): String? = chain.key
28+
29+
private var chain = Criteria()
30+
31+
infix fun <T : Any> KProperty1<DOC, T>.gt(value: T) {
32+
chain = chain.and(name).gt(value)
33+
}
34+
35+
infix fun <T : Any> KProperty1<DOC, T>.isEqualTo(value: T) {
36+
chain = chain.and(name).isEqualTo(value)
37+
}
38+
}
39+
40+
fun <T> typedCriteria(block: TypedCriteria<T>.() -> Unit): CriteriaDefinition<T> {
41+
val typedCriteria = TypedCriteria<T>()
42+
typedCriteria.block()
43+
return typedCriteria
44+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2010-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.query
17+
18+
/**
19+
* Type safe query builder.
20+
*
21+
* @author Tjeu Kayim
22+
*/
23+
class TypedQuery<T : Any>(
24+
criteria: CriteriaDefinition<T>? = null
25+
) : GenericQuery<T> {
26+
override val query = if (criteria == null) Query() else Query(criteria)
27+
}
28+
29+
fun <T: Any> typedQuery(block: TypedCriteria<T>.() -> Unit): TypedQuery<T> {
30+
return TypedQuery(typedCriteria(block))
31+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2010-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.mongodb.core.query
18+
19+
import com.nhaarman.mockito_kotlin.mock
20+
import org.bson.types.ObjectId
21+
import org.junit.Assert.assertEquals
22+
import org.junit.Test
23+
import org.springframework.data.mongodb.core.MongoOperations
24+
import org.springframework.data.mongodb.core.find
25+
import org.springframework.data.mongodb.core.mapping.Document
26+
27+
/**
28+
* @author Tjeu Kayim
29+
*/
30+
class TypedQueryTest {
31+
32+
val operations: MongoOperations = mock()
33+
34+
@Test
35+
fun `Typed query gt and isEqualTo`() {
36+
val classic = Criteria("price").gt(1100)
37+
.and("available").isEqualTo(true)
38+
39+
val typed = typedCriteria<Book> {
40+
Book::price gt 1100
41+
Book::available isEqualTo true
42+
}
43+
44+
assertEquals(classic.criteriaObject, typed.criteriaObject)
45+
}
46+
47+
@Test
48+
fun `MongoOperations find by typed query`() {
49+
operations.find<Book>(typedQuery { Book::price gt 1100 })
50+
}
51+
52+
@Test
53+
fun `Test TypedQuery`() {
54+
val classic = Query(
55+
Criteria()
56+
.and("price").gt(1100)
57+
.and("available").isEqualTo(true)
58+
)
59+
60+
val typed = typedQuery<Book> {
61+
Book::price gt 1100
62+
Book::available isEqualTo true
63+
}.query
64+
65+
assertEquals(classic.queryObject, typed.queryObject)
66+
}
67+
}
68+
69+
@Document("books")
70+
data class Book(
71+
val id: ObjectId,
72+
val name: String,
73+
val price: Int,
74+
val available: Boolean
75+
)
76+
77+
@Document("books")
78+
data class AnotherBook(
79+
val id: ObjectId,
80+
val name: String,
81+
val price: Int,
82+
val available: Boolean
83+
)

0 commit comments

Comments
 (0)