14
14
* limitations under the License.
15
15
*/
16
16
17
+ @file:Suppress(" unused" ) // tested in integration test project
18
+
17
19
package io.objectbox.kotlin
18
20
19
21
import io.objectbox.Box
20
22
import io.objectbox.BoxStore
21
23
import io.objectbox.Property
24
+ import io.objectbox.query.Query
22
25
import io.objectbox.query.QueryBuilder
26
+ import io.objectbox.relation.ToMany
23
27
import kotlin.reflect.KClass
24
28
25
29
inline fun <reified T > BoxStore.boxFor (): Box <T > = boxFor(T ::class .java)
@@ -28,9 +32,39 @@ inline fun <reified T> BoxStore.boxFor(): Box<T> = boxFor(T::class.java)
28
32
inline fun <T : Any > BoxStore.boxFor (clazz : KClass <T >): Box <T > = boxFor(clazz.java)
29
33
30
34
/* * An alias for the "in" method, which is a reserved keyword in Kotlin. */
31
- inline fun <reified T > QueryBuilder<T>.inValues (property : Property , values : LongArray ): QueryBuilder <T >?
35
+ inline fun <reified T > QueryBuilder<T>.inValues (property : Property , values : LongArray ): QueryBuilder <T >
32
36
= `in `(property, values)
33
37
34
38
/* * An alias for the "in" method, which is a reserved keyword in Kotlin. */
35
- inline fun <reified T > QueryBuilder<T>.inValues (property : Property , values : IntArray ): QueryBuilder <T >?
39
+ inline fun <reified T > QueryBuilder<T>.inValues (property : Property , values : IntArray ): QueryBuilder <T >
36
40
= `in `(property, values)
41
+
42
+ /* *
43
+ * Allows building a query for this Box instance with a call to [build][QueryBuilder.build] to return a [Query] instance.
44
+ * ```
45
+ * val query = box.query {
46
+ * equal(Entity_.property, value)
47
+ * }
48
+ * ```
49
+ */
50
+ inline fun <T > Box<T>.query (block : QueryBuilder <T >.() -> Unit ) : Query <T > {
51
+ val builder = query()
52
+ block(builder)
53
+ return builder.build()
54
+ }
55
+
56
+ /* *
57
+ * Allows making changes (adding and removing entities) to this ToMany with a call to
58
+ * [apply][ToMany.applyChangesToDb] the changes to the database.
59
+ * Can [reset][ToMany.reset] the ToMany before making changes.
60
+ * ```
61
+ * toMany.applyChangesToDb {
62
+ * add(entity)
63
+ * }
64
+ * ```
65
+ */
66
+ inline fun <T > ToMany<T>.applyChangesToDb (resetFirst : Boolean = false, body : ToMany <T >.() -> Unit ) {
67
+ if (resetFirst) reset()
68
+ body()
69
+ applyChangesToDb()
70
+ }
0 commit comments