• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
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 androidx.room.solver.query.result
18 
19 import androidx.room.ext.L
20 import androidx.room.ext.N
21 import androidx.room.ext.RoomRxJava2TypeNames
22 import androidx.room.ext.T
23 import androidx.room.ext.arrayTypeName
24 import androidx.room.ext.typeName
25 import androidx.room.solver.CodeGenScope
26 import androidx.room.writer.DaoWriter
27 import com.squareup.javapoet.FieldSpec
28 import com.squareup.javapoet.MethodSpec
29 import com.squareup.javapoet.ParameterizedTypeName
30 import com.squareup.javapoet.TypeSpec
31 import javax.lang.model.element.Modifier
32 import javax.lang.model.type.TypeMirror
33 
34 /**
35  * Binds the result as an RxJava2 Flowable.
36  */
37 class FlowableQueryResultBinder(val typeArg: TypeMirror, val queryTableNames: Set<String>,
38                                 adapter: QueryResultAdapter?)
39     : BaseObservableQueryResultBinder(adapter) {
convertAndReturnnull40     override fun convertAndReturn(roomSQLiteQueryVar: String,
41                                   canReleaseQuery: Boolean,
42                                   dbField: FieldSpec,
43                                   inTransaction: Boolean,
44                                   scope: CodeGenScope) {
45         val callableImpl = TypeSpec.anonymousClassBuilder("").apply {
46             val typeName = typeArg.typeName()
47             superclass(ParameterizedTypeName.get(java.util.concurrent.Callable::class.typeName(),
48                     typeName))
49             addMethod(MethodSpec.methodBuilder("call").apply {
50                 returns(typeName)
51                 addException(Exception::class.typeName())
52                 addModifiers(Modifier.PUBLIC)
53                 addAnnotation(Override::class.java)
54                 createRunQueryAndReturnStatements(builder = this,
55                         roomSQLiteQueryVar = roomSQLiteQueryVar,
56                         inTransaction = inTransaction,
57                         dbField = dbField,
58                         scope = scope)
59             }.build())
60             if (canReleaseQuery) {
61                 addMethod(createFinalizeMethod(roomSQLiteQueryVar))
62             }
63         }.build()
64         scope.builder().apply {
65             val tableNamesList = queryTableNames.joinToString(",") { "\"$it\"" }
66             addStatement("return $T.createFlowable($N, new $T{$L}, $L)",
67                     RoomRxJava2TypeNames.RX_ROOM, DaoWriter.dbField,
68                     String::class.arrayTypeName(), tableNamesList, callableImpl)
69         }
70     }
71 }
72