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.vo
18 
19 import androidx.room.compiler.processing.XMethodElement
20 import androidx.room.compiler.processing.XType
21 import androidx.room.parser.ParsedQuery
22 import androidx.room.solver.prepared.binder.PreparedQueryResultBinder
23 import androidx.room.solver.query.result.QueryResultBinder
24 
25 /**
26  * A class that holds information about a QueryFunction. It is self sufficient and must have all
27  * generics etc resolved once created.
28  */
29 sealed class QueryFunction(
30     val element: XMethodElement,
31     val query: ParsedQuery,
32     val returnType: XType,
33     val parameters: List<QueryParameter>
34 ) {
<lambda>null35     val sectionToParamMapping by lazy {
36         query.bindSections.map {
37             if (it.text.trim() == "?") {
38                 Pair(it, parameters.firstOrNull())
39             } else if (it.text.startsWith(":")) {
40                 val subName = it.text.substring(1)
41                 Pair(it, parameters.firstOrNull { it.sqlName == subName })
42             } else {
43                 Pair(it, null)
44             }
45         }
46     }
47 }
48 
49 /** A query function who's query is a SELECT statement. */
50 class ReadQueryFunction(
51     element: XMethodElement,
52     query: ParsedQuery,
53     returnType: XType,
54     parameters: List<QueryParameter>,
55     val inTransaction: Boolean,
56     val queryResultBinder: QueryResultBinder
57 ) : QueryFunction(element, query, returnType, parameters) {
58     val isProperty = element.isKotlinPropertyMethod()
59 }
60 
61 /** A query function who's query is a INSERT, UPDATE or DELETE statement. */
62 class WriteQueryFunction(
63     element: XMethodElement,
64     query: ParsedQuery,
65     returnType: XType,
66     parameters: List<QueryParameter>,
67     val preparedQueryResultBinder: PreparedQueryResultBinder
68 ) : QueryFunction(element, query, returnType, parameters)
69