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
17package androidx.room.vo
18
19import androidx.room.ext.typeName
20import androidx.room.parser.ParsedQuery
21import androidx.room.solver.query.result.QueryResultBinder
22import com.squareup.javapoet.TypeName
23import javax.lang.model.element.ExecutableElement
24import javax.lang.model.type.TypeMirror
25
26/**
27 * A class that holds information about a QueryMethod.
28 * It is self sufficient and must have all generics etc resolved once created.
29 */
30data class QueryMethod(val element: ExecutableElement, val query: ParsedQuery, val name: String,
31                       val returnType: TypeMirror, val parameters: List<QueryParameter>,
32                       val inTransaction: Boolean,
33                       val queryResultBinder: QueryResultBinder) {
34    val sectionToParamMapping by lazy {
35        query.bindSections.map {
36            if (it.text.trim() == "?") {
37                Pair(it, parameters.firstOrNull())
38            } else if (it.text.startsWith(":")) {
39                val subName = it.text.substring(1)
40                Pair(it, parameters.firstOrNull {
41                    it.sqlName == subName
42                })
43            } else {
44                Pair(it, null)
45            }
46        }
47    }
48
49    val returnsValue by lazy {
50        returnType.typeName() != TypeName.VOID
51    }
52}
53