1 /*
2  * Copyright (C) 2016 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.types
18 
19 import androidx.room.ext.L
20 import androidx.room.ext.T
21 import androidx.room.solver.CodeGenScope
22 import javax.lang.model.type.TypeMirror
23 
24 /**
25  * A column adapter that uses a type converter to do the conversion. The type converter may be
26  * a composite one.
27  */
28 class CompositeAdapter(out: TypeMirror, val columnTypeAdapter: ColumnTypeAdapter,
29                        val intoStatementConverter: TypeConverter?,
30                        val fromCursorConverter: TypeConverter?)
31     : ColumnTypeAdapter(out, columnTypeAdapter.typeAffinity) {
readFromCursornull32     override fun readFromCursor(outVarName: String, cursorVarName: String, indexVarName: String,
33                                 scope: CodeGenScope) {
34         if (fromCursorConverter == null) {
35             return
36         }
37         scope.builder().apply {
38             val tmpCursorValue = scope.getTmpVar()
39             addStatement("final $T $L", columnTypeAdapter.outTypeName, tmpCursorValue)
40             columnTypeAdapter.readFromCursor(tmpCursorValue, cursorVarName, indexVarName, scope)
41             fromCursorConverter.convert(tmpCursorValue, outVarName, scope)
42         }
43     }
44 
bindToStmtnull45     override fun bindToStmt(stmtName: String, indexVarName: String, valueVarName: String,
46                             scope: CodeGenScope) {
47         if (intoStatementConverter == null) {
48             return
49         }
50         scope.builder().apply {
51             val tmpVar = scope.getTmpVar()
52             addStatement("final $T $L", columnTypeAdapter.out, tmpVar)
53             intoStatementConverter.convert(valueVarName, tmpVar, scope)
54             columnTypeAdapter.bindToStmt(stmtName, indexVarName, tmpVar, scope)
55         }
56     }
57 }
58