Home | History | Annotate | Download | only in parameter
      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.query.parameter
     18 
     19 import androidx.room.ext.L
     20 import androidx.room.ext.T
     21 import androidx.room.ext.typeName
     22 import androidx.room.solver.CodeGenScope
     23 import androidx.room.solver.types.StatementValueBinder
     24 import com.squareup.javapoet.TypeName
     25 
     26 /**
     27  * Binds Collection<T> (e.g. List<T>) into String[] query args.
     28  */
     29 class CollectionQueryParameterAdapter(val bindAdapter: StatementValueBinder)
     30             : QueryParameterAdapter(true) {
     31     override fun bindToStmt(inputVarName: String, stmtVarName: String, startIndexVarName: String,
     32                             scope: CodeGenScope) {
     33         scope.builder().apply {
     34             val itrVar = scope.getTmpVar("_item")
     35             beginControlFlow("for ($T $L : $L)", bindAdapter.typeMirror().typeName(), itrVar,
     36                     inputVarName).apply {
     37                         bindAdapter.bindToStmt(stmtVarName, startIndexVarName, itrVar, scope)
     38                         addStatement("$L ++", startIndexVarName)
     39                     }
     40             endControlFlow()
     41         }
     42     }
     43 
     44     override fun getArgCount(inputVarName: String, outputVarName: String, scope: CodeGenScope) {
     45         scope.builder()
     46                 .addStatement("final $T $L = $L.size()", TypeName.INT, outputVarName, inputVarName)
     47     }
     48 }
     49